조건문(if ~ else) 사용법
두 개 이상의 인자값이 서로 타입이 다를 경우
def plus(a, b):
if type(b) is str:
return None
else:
return a + b
plus(12, "10") # None(아무것도 안뜸)
print(plus(12, 10)) # 22
정수나 실수가 들어올 경우에 연산하는 경우
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)) # 13.2
print(plus(12, 1)) # 13
plus(12, "awos")) # None(아무것도 안뜸)
조건문(if ~ elif ~ else) 사용법
def age_check(age):
print(f"yor are {age}")
if age < 18:
print("you cant drink")
elif age == 18:
print("you are new to this!")
elif age > 20 and age < 25:
print("you are still kind of young")
else:
print("enjoy your drink")
age_check(18) # you are 18
# you are new to this!
age_check(15) # you are 15
# you cant drink
age_check(23) # you are 23
# you are still kind of young
반복문(for ~ in ~) 사용법
일반적인 반복문
days = ("Mon", "Tue", "Wed", "Thu", "Fri")
for day in days:
print(day)
# Mon
# Tue
# Wed
# Thu
# Fri
for문안에서의 조건문
days = ("Mon", "Tue", "Wed", "Thu", "Fri")
for day in days:
if day is "Wed":
break
else:
print(day)
# Mon
# Tue
for문에서 list가 String인 경우
for letter in "aerowos":
print(letter)
# a
# e
# r
# o
# w
# o
# s