Python - Conditionals and Booleans
** Conditionals and Booleans
# Comparisons:
# Equal: ==
# Not Equal: !=
# Greater Than: >
# Less Than: <
# Greater or Equal: >=
# Less or Equal: <=
# Object Identity: is
==========================================
language = 'Python'
if language == 'Python':
print("Conditional was True")
---------------------------------
Conditional was True
---------------------------------
==========================================
language = 'Python'
if language == 'Python':
print("Language is Python")
else:
print("No match")
---------------------------------
Language is Python
---------------------------------
==========================================
language = 'Java'
if language == 'Python':
print("Language is Python")
elif language == 'Java':
print("Language is Java")
else:
print("No match")
---------------------------------
Language is Java
---------------------------------
==========================================
language = 'JavaScript'
if language == 'Python':
print("Language is Python")
elif language == 'Java':
print("Language is Java")
elif language == 'JavaScript':
print("Language is JavaScript")
else:
print("No match")
---------------------------------
Language is JavaScript
---------------------------------
==========================================
# and
# or
# not
user = 'Admin'
logged_in = True
if user == 'Admin' and logged_in:
print("Admin Page")
else:
print("Bad Creds")
---------------------------------
Admin Page
---------------------------------
==========================================
# and
# or
# not
user = 'Admin'
logged_in = False
if user == 'Admin' and logged_in:
print("Admin Page")
else:
print("Bad Creds")
---------------------------------
Bad Creds
---------------------------------
==========================================
# and
# or
# not
user = 'Admin'
logged_in = False
if user == 'Admin' or logged_in:
print("Admin Page")
else:
print("Bad Creds")
---------------------------------
Admin Page
---------------------------------
==========================================
# and
# or
# not
user = 'Admin'
logged_in = False
if not logged_in:
print("Please Log In")
else:
print("Welcome")
---------------------------------
Please Log In
---------------------------------
# False Valuse:
# False
# None
# Zero of any numeric type
# Any empty sequence. ex) '', (), [].
# Any empty mapping. ex) {}.
==========================================
a = [1, 2, 3]
b = [1, 2, 3]
print(a == b)
print(a is b)
print(id(a))
print(id(b))
b = a
print(a is b)
print(id(a) == id(b))
---------------------------------
True
False
15258632
16011944
True
True
---------------------------------
==========================================
condition = False
if condition: # 조건이 참일 때만 실행.
print("Evaluated to True")
else:
print("Evaluated to False")
---------------------------------
Evaluated to False
---------------------------------
==========================================
condition = True
if condition:
print("Evaluated to True")
else:
print("Evaluated to False")
---------------------------------
Evaluated to True
---------------------------------
==========================================
condition = 10
if condition:
print("Evaluated to True")
else:
print("Evaluated to False")
---------------------------------
Evaluated to True
---------------------------------
==========================================
# False Valuse:
# False
# None
# Zero of any numeric type
# Any empty sequence. ex) '', (), [].
# Any empty mapping. ex) {}.
---------------------------------
--------------------------------------------------------------------------------