Download presentation
Presentation is loading. Please wait.
1
Logical Operators, Boolean Data Types
Let’s get coding!
2
Logical Operators Python has a set of logical operators that allow you to combine Boolean expressions in a single IF selection statement These are the three we will be using: and or not
3
Logical Operators: “and”
“and” can be used to combine two different Boolean expressions These selection statements will only hold true if both conditions are met True and True True True and False False False and True False False and False False
4
Logical Operators: “and”
a = 5 b = 10 print (a > b and a > 1) print (a > 1 and b > a) print (a == 5 and b < 100) print (a > 1 and b < 1 and b > a) print (a > 1 and b > 1 and b > a)
5
Logical Operators: “and”
a = 5 b = 10 print (a > b and a > 1) False print (a > 1 and b > a) print (a == 5 and b < 100) print (a > 1 and b < 1 and b > a) print (a > 1 and b > 1 and b > a)
6
Logical Operators: “and”
a = 5 b = 10 print (a > b and a > 1) False print (a > 1 and b > a) True print (a == 5 and b < 100) print (a > 1 and b < 1 and b > a) print (a > 1 and b > 1 and b > a)
7
Logical Operators: “and”
a = 5 b = 10 print (a > b and a > 1) False print (a > 1 and b > a) True print (a == 5 and b < 100) True print (a > 1 and b < 1 and b > a) print (a > 1 and b > 1 and b > a)
8
Logical Operators: “and”
a = 5 b = 10 print (a > b and a > 1) False print (a > 1 and b > a) True print (a == 5 and b < 100) True print (a > 1 and b < 1 and b > a) False print (a > 1 and b > 1 and b > a)
9
Logical Operators: “and”
a = 5 b = 10 print (a > b and a > 1) False print (a > 1 and b > a) True print (a == 5 and b < 100) True print (a > 1 and b < 1 and b > a) False print (a > 1 and b > 1 and b > a) True
10
Logical Operators: “and”
You can use the “and” operator to combine as many Boolean expressions as you would like In any event, they would all need to hold True in order for the selection statement to be True
11
Logical Operators: “or”
“or” can also combine two expressions But this time, the selection statement will hold true if at least one of the conditions are met True or True True True or False True False or True True False or False False
12
Logical Operators: “or”
a = 5 b = 10 print (a > b or a > 1) print (a > 1 or b > a) print (a == 5 or b < 100) print (a > 1 or b < 1 or b > a) print (a > 1 or b > 1 or b > a)
13
Logical Operators: “or”
a = 5 b = 10 print (a > b or a > 1) True print (a > 1 or b > a) print (a == 5 or b < 100) print (a > 1 or b < 1 or b > a) print (a > 1 or b > 1 or b > a)
14
Logical Operators: “or”
a = 5 b = 10 print (a > b or a > 1) True print (a > 1 or b > a) True print (a == 5 or b < 100) print (a > 1 or b < 1 or b > a) print (a > 1 or b > 1 or b > a)
15
Logical Operators: “or”
a = 5 b = 10 print (a > b or a > 1) True print (a > 1 or b > a) True print (a == 5 or b < 100) True print (a > 1 or b < 1 or b > a) print (a > 1 or b > 1 or b > a)
16
Logical Operators: “or”
a = 5 b = 10 print (a > b or a > 1) True print (a > 1 or b > a) True print (a == 5 or b < 100) True print (a > 1 or b < 1 or b > a) True print (a > 1 or b > 1 or b > a)
17
Logical Operators: “or”
a = 5 b = 10 print (a > b or a > 1) True print (a > 1 or b > a) True print (a == 5 or b < 100) True print (a > 1 or b < 1 or b > a) True print (a > 1 or b > 1 or b > a) True
18
Logical Operators: “not”
“not” operates by reversing the statement In other words, for statements that hold True, it will yield False and vice versa
19
Logical Operators: “not”
a = 5 b = 10 print (not a > b) print (not a > 1) print (not a == 5) print (not b < 1) print (not b > a)
20
Logical Operators: “not”
a = 5 b = 10 print (not a > b) True print (not a > 1) print (not a == 5) print (not b < 1) print (not b > a)
21
Logical Operators: “not”
a = 5 b = 10 print (not a > b) True print (not a > 1) False print (not a == 5) print (not b < 1) print (not b > a)
22
Logical Operators: “not”
a = 5 b = 10 print (not a > b) True print (not a > 1) False print (not a == 5) False print (not b < 1) print (not b > a)
23
Logical Operators: “not”
a = 5 b = 10 print (not a > b) True print (not a > 1) False print (not a == 5) False print (not b < 1) True print (not b > a)
24
Logical Operators: “not”
a = 5 b = 10 print (not a > b) True print (not a > 1) False print (not a == 5) False print (not b < 1) True print (not b > a) False
25
Logical Operators: “not”
password = “secret” user_password = input(“Password: “) if not user_password == password: print (“Invalid! Wrong password”) else: print(“Welcome!”)
26
Warning About Logical Operators
choice = input(“Try again?“) if choice == “Yes” or “yes”: # this causes a problem because “yes” loop( ) # is not a Boolean expression else: print(“Okay goodbye”)
27
Warning About Logical Operators
I’ve seen many of you guys try using these operators but incorrectly In this situation, when we want Python to consider a conjunction of two conditions, we need to make sure that Python reads them both as conditions exclusively The word “yes” in this case and really any single string is not a condition in itself, it will actually just hold True all the time So let’s take a look at the correct way to use these operators …
28
Warning About Logical Operators
choice = input(“Try again?“) if choice == “Yes” or choice == “yes”: loop( ) else: print(“Okay goodbye”)
29
Augmented Assignment Operator
I’ve been holding out on you slightly … Python has a shortcut for the augmentations we’ve been implementing in our codes When we want to add, subtract, multiply, divide, etc continuously on a variable, we can use the augmented assignment operators
30
Augmented Assignment Operator
Example Equal to += C += 5 C = C + 5 –= C –= 4 C = C – 4 *= C *= 3 C = C * 3 /= C /= 2 C = C / 2 %= C %= 6 C = C % 6
31
Warning About Logical Operators
def function(): x = 10 if x == 0: print(x) else: x –= 1 function()
32
Boolean Data Types As we discussed, there are such data types we call Boolean data types, or “bool”s for short These are data types like integers, floats and strings It only consists of two options: True/False However, these Boolean data types can be held in variables
33
Boolean Data Types As we discussed, there are such data types we call Boolean data types, or “bool”s for short These are data types like integers, floats and strings It only consists of two options: True/False However, these Boolean data types can be held in variables
34
Boolean Data Types variable = True if variable: print (“It works!”)
35
Boolean Data Types This method of assigning variables as bool’s can be helpful when programs become complex and have various different “states” We may use variables to hold Boolean values and we can flip their values once a certain condition is met
36
Boolean Data Types loop = True choice = input(“Again?”)
def function(): loop = True choice = input(“Again?”) if choice == “no”: loop = False elif loop: function()
37
Mini Hackathon
38
Intermission: The Akinator
39
The Akinator: How it works
40
The Akinator: How it works
Females
41
The Akinator: How it works
Female Adults
42
The Akinator: How it works
Female Adults with Red Hair
43
Mini Hackathon Good luck!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.