Download presentation
Presentation is loading. Please wait.
1
ECS15 boolean
2
Boolean expressions Have value True or False Examples: answer == “b”
2+2 == 4 2+2 == 5 “cat” != “Cat” x= 5 x==5 x!=5 answer == “b” x < 30 2+2 == 4 2+2 == 5 “cat” != “Cat”
3
Comparison Operators ==; ‘a’==“a” !=; ‘a’!=“A”; 4!=5 >= <= <
4
Boolean algebra Named after George Boole (1815-1864)
Main idea: you can write down logic as mathematical formulas, as well as in sentences. Logic as a computational system. Python does some of this computation! >>> 2+2==4 and 2+1 ==3 True >>> 2+2==4 and 2+1==4 False >>> 2+2==4 or 2+2==5 >>> 2+2==4 or 2+1==3 >>> not 2+2==4 >>> not 2+2==3 >>>
5
and True if both are true True and True == False and True ==
False and False == True and False == >>> 2+2==4 and 2+2==5 False >>> False and True >>> True and False >>> True and True True >>> False and False >>>
6
and True if both are true True and True == True
False and True == False False and False == False True and False == False >>> 2+2==4 and 2+2==5 False >>> False and True >>> True and False >>> True and True True >>> False and False >>>
7
Or True if one or the other is true True or True == True or False ==
False or True == False or False == >>> "cat"=="cat" and "cat"=="dog" False >>> "cat"=="cat" or "cat"=="dog" True >>> False or True == True >>> False or True == False
8
Or True if one or the other is true True or True == True
True or False == True False or True == True False or False == False >>> "cat"=="cat" and "cat"=="dog" False >>> "cat"=="cat" or "cat"=="dog" True >>> False or True == True >>> False or True == False
9
Not not True == not False ==
>>> True and (False or not False) True >>> False or not False and False False >>> False or not (False and False)
10
Not not True == False not False == True
>>> True and (False or not False) True >>> False or not False and False False >>> False or not (False and False)
11
Operators and, or and not are Boolean operators
Addition is a numeric operator 2+2 Concatenation is a string operator “drive”+”way”
12
In if statements if reply != “a” and reply != “b”:
print (“Please answer a or b”) if reply == “a” or reply == “b”: print (“Thank you!”)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.