-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfor.py
More file actions
44 lines (33 loc) · 965 Bytes
/
for.py
File metadata and controls
44 lines (33 loc) · 965 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# for loops = execute a block of code a fixed number of times.
# You can iterate over a range, string, sequence, etc.
# 1부터 10까지 count
for x in range(1, 11):
print(x)
print("Happy new year!")
# 10부터 1까지 count
for x in reversed(range(1, 11)):
print(x)
print("Happy new year!")
# 1부터 10까지 2칸 간격으로 count
for x in range(1, 11, 2):
print(x)
print("Happy new year!")
# 10부터 1까지 거꾸로 count. 0에 도착하기 이전에 for loop가 끝나는 것으로 보인다.
for x in range(10, 0, -1):
print(x)
# 문자열도 반복 가능함
credit_card = "1234-456-789-156"
for x in credit_card:
print(x)
# 반복문에서 건너 뛰려면 continue 사용 (아래에서 13 건너뜀)
for x in range(1, 21):
if x == 13:
continue
else:
print(x)
# 반복문을 빠져나오려면 break 사용
for x in range(1, 21):
if x == 13:
break
else:
print(x)