[Python]If 구문
파이썬의 조건 구문 If에 대해서 알아보자
# 파이썬 제어문 : if # 기본형식 print(type(True)) # 0이 아닌수, "문자열", [1,2,3...], (1,2,3,...) print(type(False)) # 0, "", [], (), {} ... # 예1 if True: print("is True") if 1: print("is True") if (1, 2, 3): print("is True") # 예2 if False: print("is Trie") else: print("is False") print() # 관계 연산자 # >, >=, <, <=, ==, != x = 10 y = 15 print(f'x = {x}, y = {y}') print(x == y) print(x != y) print(x > y) print(x >= y) print(x < y) print(x <= y) print() # 예3 city = "" if city: print("You are live in:", city) else: print("please enter your city") city = "Seoul" if city: print("You are live in:", city) else: print("please enter your city") print() # 논리연산자 # and, or, not a = 55 b = 40 c = 10 print(f'a = {a}, b = {b}, c = {c}') print('a > b and b > c is', a > b and b > c) print('a > b or b > c is', a > b or b > c) print('not a > b is', not a > b) print(not True) print(not False) print() # 산술, 관계, 논리 우선순위 # 산술 > 관계 > 논리 print('e1 :', 3 + 12 > 7 + 3) print('e2 :', 5 + 10 * 3 > 7 + 3 * 20) print('e3 :', 5 + 10 > 3 and 7 + 3 == 10) print('e4 :', 5 + 10 > 0 and not 7 + 3 == 10) print() score1 = 90 score2 = 'A' # 복수조건을 모두 만족할 때 if score1 >= 90 and score2 == 'A': print('Pass') else: print('Fail') print() id1 = 'vip' id2 = 'admin' grade = 'platinum' if id1 == 'vip' or id2 == 'admin': print('관리자 입장') if id2 == 'admin' and grade == 'platinum': print("최상위 관리자") print() # 예4 : 다중조건문 num = 90 if num >= 90: print('Grade : A') elif num >= 80: print('Grade : B') elif num >= 70: print('Grade : C') else: print('과락') # 예5 : 중첩 조건문 grade = 'A' total = 95 if grade == 'A': if total >= 90: print('장학금 100%') elif total >= 80: print('장학금 80%') else: print('장학금 50%') else: print('장학금 없음') print() # in, not in q = [10, 20, 30] w = {70, 80, 90, 100} e = {"name": "jigi", "city": "Seoul", "grade": 'A'} r = (10, 12, 14) print(15 in q) # False print(90 in w) # True print(12 not in r) # False print("name" in e) # True print("Seoul" in e.values()) # True
결과 :
<class 'bool'> <class 'bool'> is True is True is True is False x = 10, y = 15 False True False False True True please enter your city You are live in: Seoul a = 55, b = 40, c = 10 a > b and b > c is True a > b or b > c is True not a > b is False False True e1 : True e2 : False e3 : True e4 : False Pass 관리자 입장 최상위 관리자 Grade : A 장학금 100% False True False True True
최신 댓글