๐Ÿ’ป์ ํ”„ ํˆฌ ํŒŒ์ด์ฌ 3์žฅ

๐Ÿ™„if ์กฐ๊ฑด๋ฌธ

๋“ค์—ฌ์“ฐ๊ธฐ

if๋ฌธ์„ ๋งŒ๋“ค๋•Œ๋Š” if ์กฐ๊ฑด๋ฌธ:๋ฐ”๋กœ ์•„๋ž˜ ๋ฌธ์žฅ๋ถ€ํ„ฐ if๋ฌธ์— ์†ํ•˜๋Š” ๋ชจ๋“ ๋ฌธ์žฅ์— ๋“ค์—ฌ์“ฐ๊ธฐ(identation)๋ฅผ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

๋น„๊ต์—ฐ์‚ฐ์ž

x < y : x๊ฐ€ y๋ณด๋‹ค ์ž‘๋‹ค.
x > y : x๊ฐ€ y๋ณด๋‹ค ํฌ๋‹ค.
x == y : x์™€ y๊ฐ€ ๊ฐ™๋‹ค.
x != y : x์™€ y๊ฐ€ ๊ฐ™์ง€ ์•Š๋‹ค.
x >= y : x๊ฐ€ y๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๋‹ค.
x <= y : x๊ฐ€ y๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค.

and, or , not

x or y : x์™€ y ๋‘˜ ์ค‘์— ํ•˜๋‚˜๋งŒ ์ฐธ์ด๋ฉด ์ฐธ์ด๋‹ค.
x and y : x์™€ y๋ชจ๋‘ ์ฐธ์ด์–ด์•ผ ์ฐธ์ด๋‹ค.
not x : x๊ฐ€ ๊ฑฐ์ง“์ด๋ฉด ์ฐธ์ด๋‹ค.

x in s, x not in s

ํŒŒ์ด์ฌ์€ ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์—์„œ ์‰ฝ๊ฒŒ ๋ณผ ์ˆ˜ ์—†๋Š” ์žฌ๋ฏธ์žˆ๋Š” ์กฐ๊ฑด๋ฌธ์„ ์ œ๊ณตํ•œ๋‹ค.

x in ๋ฆฌ์ŠคํŠธ
x in ํŠœํ”Œ
x in ๋ฌธ์ž์—ด

x not in ๋ฆฌ์ŠคํŠธ
x not in ํŠœํ”Œ
x not in ๋ฌธ์ž์—ด

>>> 1 in [1,2,3]    > 1์ด [1,2,3]์•ˆ์— ์žˆ๋Š”๊ฐ€?
True
>>> 1 not in [1,2,3]  > 1์ด [1,2,3]์•ˆ์— ์—†๋Š”๊ฐ€?
False
>>>

์‘์šฉ

>>> pocket = ['paper', 'cellphone', 'money']
>>> if 'money' in pocket:
...     print("you can ride a taxi")
... else:
...     print("you walk")
...
you can ride a taxi

pass ์‚ฌ์šฉ๋ฒ•

pass๋Š” ์กฐ๊ฑด๋ฌธ์— ํ–‰๋™์„ ๋ถ€์—ฌํ•  ๋•Œ ์•„๋ฌด๊ฒƒ๋„ ํ•˜์ง€ ๋ง๋ผํ•  ๋•Œ ์“ฐ์ธ๋‹ค.

>>> pocket = ['paper', 'money', 'cellphone']
>>> if 'money' in pocket:
...     pass
... else:
...     print('card up')

๋‹ค์–‘ํ•œ ์กฐ๊ฑด์„ ํŒ๋‹จํ•˜๋Š” elif


'์ฃผ๋จธ๋‹ˆ์— ๋ˆ์ด ์žˆ์œผ๋ฉด ํƒ์‹œ๋ฅผ ํƒ€๊ณ , ์ฃผ๋จธ๋‹ˆ์— ๋ˆ์€ ์—†์ง€๋งŒ ์นด๋“œ๊ฐ€ ์žˆ์œผ๋ฉด ํƒ์‹œ๋ฅผ ํƒ€๊ณ , ๋ˆ๋„ ์—†๊ณ  ์นด๋“œ๋„ ์—†์œผ๋ฉด ๊ฑธ์–ด ๊ฐ€๋ผ.'

์ด ๋ฌธ์žฅ์„ ๋ณด๋ฉด ์กฐ๊ฑด์„ ํŒ๋‹จํ•˜๋Š” ๋ถ€๋ถ„์ด ๋‘๊ตฐ๋ฐ๊ฐ€ ์žˆ๋‹ค. ๋จผ์ € ์ฃผ๋จธ๋‹ˆ์— ๋ˆ์ด ์žˆ๋Š”์ง€๋ฅผ ํŒ๋‹จํ•ด์•ผ ํ•˜๊ณ  ์ฃผ๋จธ๋‹ˆ์— ๋ˆ์ด ์—†์œผ๋ฉด ๋‹ค์‹œ ์นด๋“œ๊ฐ€ ์žˆ๋Š”์ง€ ํŒ๋‹จํ•ด์•ผ ํ•œ๋‹ค.

if์™€ else๋งŒ์œผ๋กœ ์œ„ ๋ฌธ์žฅ์„ ํ‘œํ˜„ํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•  ์ˆ˜ ์žˆ๋‹ค.

>>> pocket = ['paper', 'cellphone']
>>> card = True 
>>> if 'money' in pocket:
...		print("ํƒ์‹œํƒ€๊ณ ๊ฐ€๋ผ")
... else: 
...		if card:
...			print("ํƒ์‹œํƒ€๊ณ ๊ฐ€๋ผ")
...		else:
...			print("๊ฑธ์–ด๊ฐ€๋ผ")
...
ํƒ์‹œ๋ฅผ ํƒ€๊ณ  ๊ฐ€๋ผ 
        

๋ณต์žกํ•ด๋ณด์ด๋‹ˆ ๋‹ค์ค‘์กฐ๊ฑด ํŒ๋‹จ๊ธฐ๋Šฅ์ธ elif๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค.

>>> pocket = ['paper', 'cellphone']
>>> card = True
>>> if 'money' in pocket:
...     print("taxi")
... elif card:
...     print("texi")
... else:
...     print("walk")
...
texi
>>>

์ฆ‰ elif๋Š” ์ด์ „ ์กฐ๊ฑด๋ฌธ์ด ๊ฑฐ์ง“์ผ ๋•Œ ์ˆ˜ํ–‰๋œ๋‹ค.

์กฐ๊ฑด๋ถ€ ํ‘œํ˜„์‹

if score >=60:
	message = "success"
else:
	message = "failure"
   

์œ„ ์ฝ”๋“œ๋Š” score๊ฐ€ 60์ด์ƒ์ผ ๊ฒฝ์šฐ message์— ๋ฌธ์ž์—ด "success"๋ฅผ, ์•„๋‹ ๊ฒฝ์šฐ์—๋Š” "failure"๋ฅผ ๋Œ€์ž…ํ•˜๋Š” ์ฝ”๋“œ์ด๋‹ค.

ํŒŒ์ด์ฌ ์กฐ๊ฑด๋ถ€ ํ‘œํ˜„์‹(conditional expression)์„ ์‚ฌ์šฉํ•˜๋ฉด ์œ„ ์ฝ”๋“œ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฐ„๋‹จํžˆ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

message = "success" if score >= 60 else "failure"

์กฐ๊ฑด๋ถ€ ํ‘œํ˜„์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜ํ•œ๋‹ค.

์กฐ๊ฑด๋ฌธ์ด ์ฐธ์ธ ๊ฒฝ์šฐ if ์กฐ๊ฑด๋ฌธ else ์กฐ๊ฑด๋ฌธ์ด ๊ฑฐ์ง“์ธ ๊ฒฝ์šฐ

์กฐ๊ฑด๋ถ€ ํ‘œํ˜„์‹์€ ๊ฐ€์กฑ์„ฑ์ด ์œ ๋ฆฌํ•˜๊ณ  ํ•œ ์ค„๋กœ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์–ด ํ™œ์šฉ์„ฑ์ด ์ข‹๋‹ค.

๐Ÿฅ‚while๋ฌธ

while๋ฌธ ์ฒ˜์Œ์œผ๋กœ ๋Œ์•„๊ฐ€๊ธฐ

1๋ถ€ํ„ฐ 10 ๊นŒ์ง€์˜ ์ˆซ์ž ์ค‘ ํ™€์ˆ˜๋งŒ ์ถœ๋ ฅํ•˜๊ธฐ
continue๋ฌธ์„ ์จ์„œ ๋Œ์•„๊ฐ„๋‹ค.

>>> a = 0 
>>> while a < 10:
...	a = a + 1
...	if a % 2 == 0: continue   >>> a๋ฅผ2๋กœ ๋‚˜๋ˆ„์—ˆ์„ ๋•Œ ๋‚˜๋จธ์ง€๊ฐ€ 0์ด ์•„๋‹ˆ๋ฉด ๋งจ ์ฒ˜์Œ์œผ๋กœ ๋Œ์•„๊ฐ„๋‹ค. 
...		print(a)
...
1
3
5
7
9

๐Ÿ”for๋ฌธ

์ „ํ˜•์ ์ธ for๋ฌธ

for๋ฌธ์˜ ๊ธฐ๋ณธ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

for ๋ณ€์ˆ˜ in ๋ฆฌ์ŠคํŠธ(๋˜๋Š” ํŠœํ”Œ,๋ฌธ์ž์—ด):
	์ˆ˜ํ–‰ํ•  ๋ฌธ์žฅ1
	์ˆ˜ํ–‰ํ•  ๋ฌธ์žฅ2
	...

์˜ˆ์ œ

>>> test_list = ['one','two','three']
>>> for i in test_list:   >>>one,two,three๋ฅผ ์ˆœ์„œ๋Œ€๋กœ i์— ๋Œ€์ž… 
...     print(i)
...
one
two
three

๋‹ค์–‘ํ•œ for๋ฌธ์˜ ์‚ฌ์šฉ

>>> a = [(1,2),(3,4),(5,6)]
>>> for (first, last) in a:
...	print(first+last)
...
3  > first:1 last:2
7  > first:3 last:4
11 > first:5 last:6

for๋ฌธ๊ณผ ํ•จ๊ป˜ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” range ํ•จ์ˆ˜

for๋ฌธ์€ ์ˆซ์ž๋ฆฌ์ŠคํŠธ๋ฅผ ์ž๋™์œผ๋กœ ๋งŒ๋“ค์–ด ์ฃผ๋Š” rangeํ•จ์ˆ˜์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค. ๋‹ค์Œ์€ rangeํ•จ์ˆ˜์˜ ๊ฐ„๋‹จํ•œ ์‚ฌ์šฉ๋ฒ•์ด๋‹ค.

>>> a = range(10)
>>> a
range(0,10) >>0,1,2,3,4,5,6,7,8,9

๋์ž๋ฆฌ ์ˆซ์ž๋Š” ํฌํ•จ๋˜์ง€ ์•Š๋Š”๋‹ค.

for๋ฌธ์˜ rangeํ•จ์ˆ˜ ์ด์šฉ๋ฒ•

>>> add = 0 
>>> for i in range(1,11):
...	add = add + i
...
>>> print(add)
55

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ