Condition
if Example
x = 10
if x < 10:
print("한자리수")
elif x < 100:
print("두자리수")
else:
print("세자리 이상")
True / False
자료형 | 참 | 거짓 |
---|---|---|
숫자 | 0이 아닌 숫자 | 0 |
문자열 | "abc" | "" |
None | None | |
리스트 | [1,2,3] | [ ] |
튜플 | (1,2,3) | ( ) |
딕셔너리 | {"a":"b"} | { } |
Pass
if 조건문 안에서 특정 블럭/문장을 수행하지 않고 그냥 Skip
if n < 10:
pass
else:
print(n)
Switch 대체 구문
Sample 1
today = 'Saturday' if today is 'Sunday': print('Order the spinach pizza.') elif today is 'Monday': print('Order the mushroom pizza.') elif today is 'Tuesday': print('Order the pepperoni pizza.') elif today is 'Wednesday': print('Order the veggie pizza.') elif today is 'Thursday': print('Order the bbq chicken pizza.') elif today is 'Friday': print('Order the sausage pizza.') elif today is 'Saturday': print('Order the Hawaiian pizza.')
Sample 2
specials = {'Sunday' : 'spinach', 'Monday' : 'mushroom', 'Tuesday' : 'pepperoni', 'Wednesday' : 'veggie', 'Thursday' : 'bbq chicken', 'Friday' : 'sausage', 'Saturday' : 'Hawaiian'} def order(day): pizza = specials[day] print('Order the {} pizza.'.format(pizza)) # order the Saturday special! order('Saturday')