Presentation is loading. Please wait.

Presentation is loading. Please wait.

Boolean Expressions and Logical Operators CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington Credits:

Similar presentations


Presentation on theme: "Boolean Expressions and Logical Operators CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington Credits:"— Presentation transcript:

1 Boolean Expressions and Logical Operators CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington Credits: a significant part of this material has been created by Dr. Darin Brezeale and Dr. Gian Luca Mariottini 1

2 The Boolean Type Expressions of type boolean can only have two values: True, or False. – True and False are reserved keywords in Python. They are also literals. 2 >>> 3 > 2 True >>> type(True) >>> a = 3 == 2 >>> a False >>> a = (3 == 2) >>> a False Preferred style (parenthesize) >>> b = 4; >>> a = (b != 5) >>> a True

3 Relational operators The following operators generate boolean results: == equality != not equal < less than > greater than <= less than or equal to >= greater than or equal to 3

4 Boolean Operators The following Boolean operators can be used to produce Boolean expressions and results: not and or Make the truth tables for these operators 4

5 Using BooleanOperators 5 >>> a = 3 >>> b = 4 >>> (a == b) or (a+b == 7) True >>> (a == b) and (a+b == 7) False >>> not(a == b) True

6 Combining operators What does this line do? >>> (3 == 5) and (2 = 0) 6

7 Combining operators What does this line do? >>> (3 == 5) and (2 = 0) I don't know, and I don't need to know. – Use parentheses to make the meaning of these statements clear. >>> ((3 == 5) and (2 = 0)  True >>> (3 == 5) and ((2 = 0))  False 7

8 The if statement if boolean_expression: statement #only one statement Example: x = 2 if (x % 2) == 0: print(x, 'is even') -Indentation groups the statements together. Typically 4 white spaces are used for indenting. -Copy/paste problem: try to copy/paste this code and run it. What happens? … This is another reason to type it yourself. 8

9 The if statement if condition: statement …. statement Draw the flow of control diagram. 9 Block of instructions or suite

10 The if-else statement if condition: statement …. statement else: statement …. statement Draw the flow of control diagram. 10

11 The if-elif-else statement if condition1: # suite elif condition2: # suite elif condition3: # suite … else: # suite If condition1 evaluates to True the other ones will not be evaluated. Compare this with multiple if statements (at the same level) Draw the flow of control diagram. 11

12 What does it mean to be equal? Floats are approximations of real numbers. >>> u = 11111113 >>> v = -11111111 >>> w = 7.51111111 >>> (u + v) + w 9.51111111 >>> u+ (v + w) 9.5111111044837 12

13 What does it mean to be equal? Solution: >>> u = 11111113 >>> v = -11111111 >>> w = 7.51111111 >>> x = (u + v) + w 9.51111111 >>> y = u+ (v + w) 9.5111111044837 >>> abs(x-y)<0.0000001 # abs computes the absolute value 13

14 What does it mean to be equal? Two different variables point to objects that have the same value. Two different variables point to the same object (same ID). – Use the is function to see if two variables point to the same object. Use the id function to get the object ID. 14

15 What does it mean to be equal? Book example (I used int instead of float): >>> a_int = 1 >>> b_int = 1 >>> c_int = b_int >>> a_int == b_int >>> b_int == c_int >>> a_int is b_int >>> b_int is c_int >>> id(a_int) >>> id(b_int) >>> id(a_int) 15

16 More on object creation Book says: “When you assign the same object to different variables using separate assignments, you create separate objects that happen to have the same value” – Example above I say: The return value of an expression becomes a new object: >>> id(‘a’+’b’) >>> id(‘c’+’d’) >>> id(‘a’+’b’) * notice that the above expressions were not mapped to variables. We said that in such cases we ‘lose’ the computed value. However, the newly computed value still has an id. How can these all happen at the same time? You should ask yourself these questions. 16

17 Chained relational operators >>> 0 <= x <= 5 Is : (0<=x) and (x <= 5) -Be careful in using them!: >>> 4 < 5 == True # You may think it should be True Is: (4<5) and (5 ==True) # It evaluates to False 17

18 Practice Construct boolean expressions (compute the tables) Write examples that use conditional statements. 18


Download ppt "Boolean Expressions and Logical Operators CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington Credits:"

Similar presentations


Ads by Google