True or False
참, 거짓값을 나타내는 표현
==!==1 == 1 # True
2 != 4 # True
3 == 5 # False
'7' == 7 # False
bool
bool 변수는 True(참)또는 False(거짓) 값만 가진다.
bool_one = 5 != 7
bool_two = 1 + 1 != 2
bool_three = 3 * 3 == 9
print(bool_one) # True
print(bool_two) # False
print(bool_three) # True
if condition:
statement
if 뒤의 condition이 참일 때 statement가 실행
song = 'Mr. Brightside'
if song == 'Mr. Brightside':
print('You are right!')
# Output: You are right!
조건문을 사용할 때 비교를 위해 사용하는 연산자
< : Less than> : Greater than<= : Less than or equal to>= : Greater than or equal toalbum_rating = 9.8
if album_rating >= 9:
print("The album is masterpiece")
# Output: The album is masterpiece
조건문이 하나 이상의 조건을 필요로할 때 사용하는 연산자
andornotand
양 조건을 모두 만족하여야할 때 사용
album_rating = 9.8
album_release_year = 2010
if album_rating >= 9 and 2010 <= album_release_year < 2020:
print("The album is masterpiece in 2010s")
# Output: The album is masterpiece in 2010s
or
양 조건 중 하나만 만족하면 될 시 사용
or 에서 두 조건중 하나만 True일때에도 True 값을 가진다
True or (3 + 4 == 7) # True
(1 - 1 == 0) or False # True
(2 < 0) or True # True
(3 == 8) or (3 > 4) # False
daily_expense = 40
monthly_expense = 3000
if daily_expense >= 50 or monthly_expense >= 2500:
print ('You should cut back on your spending!')
# Output: You should cut back on your spending!
not
논리값을 뒤집는 연산자
not True == False
not False == True
not 1 + 1 == 2 # False
not 7 < 0 # True
album_rating = 9.8
album_maker_gender = 'male'
if not album_rating <= 9.0 and not album_maker_gender == 'female':
print("The album is masterpiece between male solo albums")
# Output: The album is masterpiece between male solo albums
album_rating이 9.0 이하가 아니고(9.0보다 높고), album_maker_gender가 female이 아니기 때문에 조건 충족
if condition:
statement1
else:
statement2
condition이 True 일 때 statement1 출력, False 일 때 statement2 출력
height = 180
thickness = 20
if height >= 170 and thickness >=30:
print("Go")
else:
print("Stop")
# Output: Stop
if condition:
statement1
elif condition2:
statement2
else:
statement3
condition이 True 때 statement1 출력, condition2가 True일 때 statement2 출력, False일 때 statement3 출력
album_rating = 92
if album_rating >= 90:
print('masterpiece')
elif album_rating >=80:
print('B')
elif album_rating >=70:
print('C')
elif album_rating >=60:
print('D')
else:
print('F')
# Output: masterpiece
notnot을 이해하는데 약간 헷갈려서 고생했다.
결국 '논리를 뒤집는' 것 뿐인데, 생각보다 한번에 와닿지 않았다.
나중에 not이 어떻게 쓰이는지를 한번 찾아보고 싶다.
':'항상 조건문 condition의 마지막에는 : 가 붙는 걸 잊지 말아야 할 것 같다.
if문을 활용하여 내 식대로 코드를 짤 때 : 를 까먹고 안붙인 경우가 많았어서 계속 유념하고 있어야할 듯 하다.