[개념] Chapter04-2 : 파이썬 반복문 for
카테고리: Python
인프런 강의 프로그래밍 시작하기 : 파이썬 입문을 듣고 정리한 내용입니다✏️.
For문 실습
기본 For문
for v1 in range(10): # 0 ~ 9
print('v1 is :', v1)
#v1 is : 0
#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
for v2 in range(1,11): # 1 ~ 10
print('v2 is :', v2)
#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
#v2 is : 10
for v3 in range(1, 11, 2):
print('v3 is :', v3)
#v3 is : 1
#v3 is : 3
#v3 is : 5
#v3 is : 7
#v3 is : 9
1 ~ 1000합
sum1 = 0
for v in range(1,1001):
sum1 += v
print('1 ~ 1000 Sum :', sum1)
print('1 ~ 1000 Sum :', sum(range(1,1001)))
# 출력 : 1 ~ 1000 Sum : 500500
print(type(range(1,11)))
# 출력 : <class 'range'>
print('1 ~ 1000 4의배수의 합 :', sum(range(4,1001,4)))
# 출력 : 1 ~ 1000 4의배수의 합 : 125500
Iterables 자료형 반복
- 문자열, 리스트, 튜플, 집합, 사전
- iterable 리턴 함수 : range, reversed, enumerate, filter, map, zip
예제1
names = ['kim', 'Park', 'cho', 'Lee', 'Choi', 'Yoo']
for n in names:
print('You are :', n)
#You are : kim
#You are : Park
#You are : cho
#You are : Lee
#You are : Choi
#You are : Yoo
예제2
lotto_number = [11, 19, 21, 28, 36, 37]
for n in lotto_number:
print("Current number :", n)
#Current number : 11
#Current number : 19
#Current number : 21
#Current number : 28
#Current number : 36
#Current number : 37
예제3
word = "Beautiful"
for s in word:
print('word :', s)
#word : B
#word : e
#word : a
#word : u
#word : t
#word : i
#word : f
#word : u
#word : l
예제4
my_info = {
"Name" : 'Lee',
"Age" : 33,
"City" : 'Seoul',
}
for k in my_info:
print('key :', k)
#key : Name
#key : Age
#key : City
for k in my_info:
print('value :', my_info[k])
print('value :', my_info.get(k))
#value : Lee
#value : 33
#value : Seoul
for v in my_info.values():
print("value :", v)
#value : Lee
#value : 33
#value : Seoul
예제5
name = 'FineAppLE'
for n in name:
if n.isupper():
print(n)
else:
print(n.upper())
#F
#I
#N
#E
#A
#P
#P
#L
#E
Break
numbers = [14, 3, 4, 7, 10, 24, 17, 2, 33, 15, 34, 36, 38]
for num in numbers:
if num == 34:
print('Found : 34!')
break
else:
print('Not found :', num)
Continue
lt = ["1", 2, 5 ,True, 4.3, complex(4)]
for v in lt:
if type(v) is bool: # bool형이면 skip
continue
else:
print("Current type:", v, type(v))
# Current type: 1 <class 'str'>
# Current type: 2 <class 'int'>
# Current type: 5 <class 'int'>
# Current type: 4.3 <class 'float'>
# Current type: (4+0j) <class 'complex'>
for-else
numbers = [14, 3, 4, 7, 10, 24, 17, 2, 33, 15, 34, 36, 38]
for num in numbers:
if num == 55:
print("Found : 55")
break
else:
print('Not Found : 55')
# Not Found : 55
구구단 출력
for i in range(2, 10):
for j in range(1, 10):
print('{:4d}'.format(i*j), end = '')
print()
# 2 4 6 8 10 12 14 16 18
# 3 6 9 12 15 18 21 24 27
# 4 8 12 16 20 24 28 32 36
# 5 10 15 20 25 30 35 40 45
# 6 12 18 24 30 36 42 48 54
# 7 14 21 28 35 42 49 56 63
# 8 16 24 32 40 48 56 64 72
# 9 18 27 36 45 54 63 72 81
변환 예제
name2 = 'Aceman'
print('Reversed', reversed(name2))
# Reversed <reversed object at 0x000001B736F93BE0>
print('List', list(reversed(name2)))
#List ['n', 'a', 'm', 'e', 'c', 'A']
print('Tuple', tuple(reversed(name2)))
#Tuple ('n', 'a', 'm', 'e', 'c', 'A')
print('Set', set(reversed(name2))) # 순서X
# Set {'e', 'A', 'c', 'a', 'n', 'm'}
댓글남기기