-
'23.3.5(일) 파이썬 입문 <9 : if문>코딩/파이썬 2023. 3. 5. 21:07
# 파이썬 제어문 # if문 실습 # 기본 형식 print(type(True)) # 0이 아닌 수, "abc", [1,2,3...], (1,2,3,...) ... 비어 있지 않을 때 print(type(False)) # 0, "", [], (), {}, ... 비어있는 것 # 예1 if True: #들여쓰기 indent 를 잘해야 함 print("GOOD") # 예2 if True: print("Bad") else: print("Good!") #관계 연산자 # >, >-, <, <-, ==, != # 예 3 print(">>>") x = 15 y = 10 print(x == y) # 양변이 같은 경우 참 print( x != y) # 양변이 다른 경우 참 print(">>>>") # 예 4 city = "Seoul" if city: print("You are in: ", city) else : print("Please enter your city") # 논리연산자(중요) # and, or , not print(">>>>") a = 75 b = 40 c = 10 print('and: ', a>b and b > c) # and는 모두 만족할 때 print('or: ' , a> b or b >c) # or는 최소 하나만 만족할 때 print('not: ', not a > b) # not은 반대로 출력 print( not True) print( not False) # 산술, 관계, 논리 우선순위 # 산술(+, - 등) > 관계(크거나 작다 등) > 논리(and, ot 등) 순으로 실행됨 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('False') print(">>>") id1 = 'vip' id2 = 'admin' grade = 'platinum' if id1 == 'vip' or id2 == 'admin': print("관리자 입장") if id2 == 'admin' and grade =='platinum': print('최상위 관리자') # 다중 조건문 print('>>>>') num = 50 if num >= 90: print('Grade : A') elif num >=80: print('Grade : A') else: print('과락') # 중첩 조건문 (if문 안에 if문이 있는 것) print('>>>') grade = 'B' total = 95 if grade == 'A' : if total >= 90 : print('장학금 100%') elif total >=80 : print('장학금 80%') else: print('장학금 50%') else: print('장학금 없음') # in, not in print(">>>") q = [10, 20, 30] #리스트 w = {70, 80 ,90, 100} #집합 e = {"name" : "lee", "city" : "Seoul", "grade" : "A"} # 딕셔너리 r = (10,12, 14) #튜플 print( 15 in q) print(90 in w) print(12 not in r) print("name" in e) # key를 줘야함 print("Seoul" in e.values()) # values 안에 확인하고 싶을 때
더보기<class 'bool'>
<class 'bool'>
GOOD
Bad
>>>
False
True
>>>>
You are in: Seoul
>>>>
and: True
or: True
not: False
False
True
>>>
e1 : True
e2 : False
e3 : True
e4 : False
>>>
Pass
>>>
관리자 입장
최상위 관리자
>>>>
과락
>>>
장학금 없음
>>>
False
True
False
True
True'코딩 > 파이썬' 카테고리의 다른 글
'23.3.7(화) 파이썬 입문 <11 : while 구문> (0) 2023.03.05 '23.3.5(일) 파이썬 입문 <10: for문> (0) 2023.03.05 '23.3.4(토) 파이썬 입문 <8 : 집합> (0) 2023.03.04 '23.3.4(토) 파이썬 입문 <7: 딕셔너리> (0) 2023.03.04 '23.3.4(토) 파이썬 입문 <6 : 튜플> (0) 2023.03.04