파이썬의 제어문에 대해서 알아보자
if 조건:
맞을경우 실행
else:
조건이 맞지않을 경우 실행
def plus(a, b):
if type(b) is int or type(b) is float:
return a+b
else:
return None
print(plus(12, 1.2))
if 조건:
맞을경우 실행
elif 조건:
elif 조건이 맞으면 실행
else:
모든 조건이 맞지않을 경우 실행
def age_check(age):
print(f"you are {age}")
if age < 18:
print("you can't drink")
elif age == 18 or 19:
print("you are new to this!")
elif age>20 and age
print("you are still kind of young")
else:
print("enjoy your drink")
age_check(18)
for item in data:
실행문
days = ("Mon", "Tue", "Wed", "Thu", "Fri")
for day in days:
print(day)
while 조건:
조건문:
continue
조건문:
break
data = 10
while data > 0:
if data % 2 == 1:
print(data)
continue
data = 10
while data:
if data <= 2:
print(data)
break
data = 1
while data < 10:
data++
print(data)
cf) 파이썬에는 switch 문이 없다.
https://nomadcoders.co/python-for-beginners
https://docs.python.org/3/library/stdtypes.html#comparisons
https://docs.python.org/3/reference/compound_stmts.html