2. 제어문

If문

PythonExam.pi
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
a = 10
if a > 5:
print("big")
else:
print("small")


n = -2
if n > 0:
print('양수')
elif n < 0:
print('음수')
else:
print('0')

order = 'spam'
if order == 'spam':
price = 1000
elif order == 'egg':
price = 500
elif order == 'spagetti':
price = 2000
else:
price = 0

print(price)

For문

PythonExam.pi
1
2
3
4
5
6
7
8
9
a = ['cat', 'cow', 'tiger']

for animal in a:
print(animal)

for x in range(10):
print(x, end=" ")


while문

PythonExam.pi
1
2
3
4
5
6
count = 1
while count < 11:
print(count, end=' ')
count += 1
else:
print('')

break, continue, else

PythonExam.pi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
i = 0
while i < 10:
i += 1
if i < 5:
continue
print(i, end=' ')
if i > 10:
break
else:
print('else block')

print('done')


i = 0
while True:
print(i)
if i > 5:
break
i += 1

Share