0% found this document useful (0 votes)
46 views4 pages

Python Conditionals and Booleans Explained

The document covers conditionals and booleans in Python programming, explaining the use of 'if', 'elif', and 'else' statements along with boolean operations like 'and', 'or', and 'not'. It also discusses object identity with the 'is' operator and the evaluation of different values to True or False. Key examples illustrate how to check conditions and the significance of variable assignment and comparison.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views4 pages

Python Conditionals and Booleans Explained

The document covers conditionals and booleans in Python programming, explaining the use of 'if', 'elif', and 'else' statements along with boolean operations like 'and', 'or', and 'not'. It also discusses object identity with the 'is' operator and the evaluation of different values to True or False. Key examples illustrate how to check conditions and the significance of variable assignment and comparison.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Programming with Corey Schafer

Lecture 6 - Conditionals and Booleans


if True:
print("Conditional is True")
→ Conditional is True

if False:
print("Conditional is True")

• Single = sign refers to assigning a value to a variable while double == sign refers to equality

language = "Java"
if language == "Python":
print("Language is Python")
else:
print("No match")
→ No match

language = "Java"
if language == "Python":
print("Language is Python")
elif language == "Java":
print("Language is Java")
else:
print("No match")
→ Language is Java

• Boolean operations – ‘and’, ‘or’ and ‘not’


user = "Admin"
logged_in = True
if user == "Admin" and logged_in == True:
print("Admin Page")
else:
print("Bad Credentials")
→ Admin page
Both conditions have to be true

user = "Admin"
logged_in = True
if user == "Admin" and logged_in:
print("Admin Page")
else:
print("Bad Credentials")
→ Admin page
It is the same thing as above but more concise

user = "Admin"
logged_in = False
if user == "Admin" or logged_in:
print("Admin Page")
else:
print("Bad Credentials")
→ Admin page
Only one condition has to be true

logged_in = False
if not logged_in:
print("Please Log In")
else:
print("Welcome!")
→ Please Log In
It means that if it is not false, then print ‘Please Log In’

• Not operator is only used to switch boolean i.e. True to False and False to True

• Object identity ‘is’ used to check if values have same ID/if they are the same object in memory
a = [1, 2, 3]
b = [1, 2, 3]
print(a == b)
→ True
a = [1, 2, 3]
b = [1, 2, 3]
print(a is b)
→ False
It is because these are two different objects in memory
In other words print(a is b) is print(id(a) == id(b))

a = [1, 2, 3]
b = [1, 2, 3]
print(id(a))
print(id(b))
print(a is b)
→ 2133587353728
2133587501824
True
It shows that they have different IDs

a = [1, 2, 3]
b=a
print(id(a))
print(id(b))
print(a is b)
→ 2587122753664
2587122753664
True
It shows that they have the same IDs

• False values
condition = False
if condition:
print("Evaluated to True")
else:
print("Evaluated to False")
→ Evaluated to False

condition = None
if condition:
print("Evaluated to True")
else:
print("Evaluated to False")
→ Evaluated to False
None evaluates to False

condition = 0
if condition:
print("Evaluated to True")
else:
print("Evaluated to False")
→ Evaluated to False
Zero evaluates to False. All other numbers evaluate to True (including negative and float)

condition = "" / () / [] / {}
if condition:
print("Evaluated to True")
else:
print("Evaluated to False")
→ Evaluated to False / Evaluated to False / Evaluated to False / Evaluated to False
Empty string "", empty tuple (), empty list [] and empty mapping (dictionary) {}evaluate to False
These can be used to check if a string, tuple, list or dictionary has values or not

• All other things evaluate to True


condition = "Test"
if condition:
print("Evaluated to True")
else:
print("Evaluated to False")
→ Evaluated to True

You might also like