# Section05-2 # 파이썬 흐름제어(제어문) # 반복문 실습 # 코딩의 핵심 -> 조건 해결 중요 # 기본 반복문 사용(while, for) v1 = 1 while v1 < 11: print("v1 is :", v1) v1 += 1 # 0부터 range()에 선언한 숫자 미만까지 v2 변수에 넣어서 for문 돌린다. for v2 in range(10): print("v2 is :", v2) # 0~10 까지 v3 변수에 넣어서 for문 돌린다. for v3 in range(1, 11): print("v3 is :", v3) # 0~10 까지 중 2번째 숫자(짝수)를 꺼내서 v3 변수에 넣어서 for문 돌린다. for v4 in range(1, 11, 2): print("v4 is :", v4) # 1 ~ 100합 sum1 = 0 cnt1 = 1 while cnt1 <= 100: sum1 += cnt1 cnt1 += 1 print('1 ~ 100 합 : ', sum1) # while로 구한 값 print('1 ~ 100 합 : ', sum(range(1, 101))) # sum(리스트) 으로 구한 값 print('1 ~ 100 안에 3의 배수의 합 : ', sum(range(1, 101, 3))) # 시퀀스(순서가 있는) 자료형 반복 # 문자열, 리스트, 튜플, 집합, 사전 # iterable 리턴 함수 : range, reversed, enumerate, filter, map, zip # 예제1 names = ["Kim", "Park", "Cho", "Lee", "Choi", "Yoo"] for name in names: print("You are", name) # 예제2 lotto_numbers = [11, 19, 21, 28, 36, 37] for number in lotto_numbers: print("Your number", number) # 예제3 word = 'dreams' for s in word: print('word : ', s) # 예제4 my_info = { "name": "Kim", "age": 33, "city": "Seoul" } for key in my_info: print(key, ":", my_info[key]) for val in my_info.values(): print(val) # 예제5 name = 'KennRY' for n in name: if n.isupper(): # isupper() 대문자인지 확인 print(n) else: print(n.upper()) numbers = [14, 3, 4, 7, 10, 24, 17, 2, 33, 15, 34, 36, 38] # break for num in numbers: if num == 33: print("found : 33!") break else: print("not found : ", num) # continue lt = ["1", 2, 5, True, 4.3, complex(4)] for v in lt: if type(v) is float: continue print("type:", type(v)) print("multiply by 2:", v * 3) # for-else 실습 numbers = [14, 3, 4, 7, 10, 24, 17, 2, 33, 15, 34, 36, 38] for num in numbers: if num == 33: # 45 print("found : 33!") break else: print("not found : ", num) else: print("Not Found 39...") # flag 사용 f = True numbers = [14, 3, 4, 7, 10, 24, 17, 2, 33, 15, 34, 36, 38] while f: for v in numbers: if v == 33: print("found : 33!") f = False print("not found : ", v) # else 구문 정리(반복문이 정상적으로 수행 된 경우 else 블럭 수행) # 예제1 i = 1 while i <= 10: print('i : ', i) if i == 6: break i += 1 else: print('else block run!') # 예제2 j = 1 while j <= 10: print('j : ', j) if j == 11: break j += 1 else: print('else block run!') # 중첩 for 문 구구단 출력 for i in range(1, 11): for j in range(1, 11): print('{:4d}'.format(i * j), end='') print() # 자료 구조 변환 예제 name = 'Niceman' print('reversed : ', reversed(name)) print('list : ', list(reversed(name))) print('list : ', tuple(reversed(name))) print('list : ', set(reversed(name))) # 순서X
v1 is : 1 v1 is : 2 v1 is : 3 v1 is : 4 v1 is : 5 v1 is : 6 v1 is : 7 v1 is : 8 v1 is : 9 v1 is : 10 v2 is : 0 v2 is : 1 v2 is : 2 v2 is : 3 v2 is : 4 v2 is : 5 v2 is : 6 v2 is : 7 v2 is : 8 v2 is : 9 v3 is : 1 v3 is : 2 v3 is : 3 v3 is : 4 v3 is : 5 v3 is : 6 v3 is : 7 v3 is : 8 v3 is : 9 v3 is : 10 v4 is : 1 v4 is : 3 v4 is : 5 v4 is : 7 v4 is : 9 1 ~ 100 합 : 5050 1 ~ 100 합 : 5050 1 ~ 100 안에 3의 배수의 합 : 1717 You are Kim You are Park You are Cho You are Lee You are Choi You are Yoo Your number 11 Your number 19 Your number 21 Your number 28 Your number 36 Your number 37 word : d word : r word : e word : a word : m word : s name : Kim age : 33 city : Seoul Kim 33 Seoul K E N N R Y not found : 14 not found : 3 not found : 4 not found : 7 not found : 10 not found : 24 not found : 17 not found : 2 found : 33! type: <class 'str'> multiply by 2: 111 type: <class 'int'> multiply by 2: 6 type: <class 'int'> multiply by 2: 15 type: <class 'bool'> multiply by 2: 3 type: <class 'complex'> multiply by 2: (12+0j) not found : 14 not found : 3 not found : 4 not found : 7 not found : 10 not found : 24 not found : 17 not found : 2 found : 33! not found : 14 not found : 3 not found : 4 not found : 7 not found : 10 not found : 24 not found : 17 not found : 2 found : 33! not found : 33 not found : 15 not found : 34 not found : 36 not found : 38 i : 1 i : 2 i : 3 i : 4 i : 5 i : 6 j : 1 j : 2 j : 3 j : 4 j : 5 j : 6 j : 7 j : 8 j : 9 j : 10 else block run! 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100 reversed : <reversed object at 0x0000023C36908E80> list : ['n', 'a', 'm', 'e', 'c', 'i', 'N'] list : ('n', 'a', 'm', 'e', 'c', 'i', 'N') list : {'e', 'a', 'c', 'i', 'N', 'n', 'm'}