Python

python - for문

에이미103 2019. 7. 17. 14:58

 

my_list = [1,2,3,4,5] #list
my_sum = 0

for tmp in range(len(my_list)): # :(인덴트)를 이용
    my_sum += my_list[tmp]

print("합계 :{}" .format(my_sum))

my_list = [1,2,3,4,5] #list
new_list =[tmp * 2 for tmp in my_list]
print(new_list)

my_list = [1,2,3,4,5] #list
new_list =[tmp * 2 for tmp in my_list if tmp % 2 == 0]
print(new_list)

'Python' 카테고리의 다른 글

python - 날짜(date, time, datetime)  (0) 2019.07.15
python - if문  (0) 2019.07.14
python - Data Type - bool  (0) 2019.07.14
python - set type  (0) 2019.07.14
python - dict  (0) 2019.07.14