[Python] for 문 (for i in list/tuple/string)


[Python] for 문 (for i in list/tuple/string)

for 변수 in 리스트(또는 튜플, 문자열): 수행할 문장1 수행할 문장2 ... ================= >>> test_list = ['one', 'two', 'three'] >>> for i in test_list: ... print(i) ... one two three ================= while문에서 살펴본 continue문을 for문에서도 사용할 수 있다. 즉 for문 안의 문장을 수행하는 도중에 continue문을 만나면 for문의 처음으로 돌아가게 된다. 문제: 1부터10까지 더하기. >>> add = 0 >>> for i in range(1, 11): ... add = add + i ... >>> print(add) 55 변형 출제. >>> a = [1,2,3,4] >>> result = [] >>> for num in a: ... result.append(num*3) #append(x)는 리스트의 맨 마지막에 x를 추가하는 함수이다 ... >...


#for문 #python

원문링크 : [Python] for 문 (for i in list/tuple/string)