if - The University of Texas at Arlington

advertisement
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
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.
>>> 3 > 2
True
>>> type(True)
<type 'bool'>
Preferred style
(parenthesize)
>>> a = 3 == 2
>>> a
False
>>> a = (3 == 2)
>>> a
False
>>> b = 4;
>>> a = (b != 5)
>>> a
True
2
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
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
Using BooleanOperators
>>> a = 3
>>> b = 4
>>> (a == b) or (a+b == 7)
True
>>> (a == b) and (a+b == 7)
False
>>> not(a == b)
True
5
Combining operators
• What does this line do?
>>> (3 == 5) and (2 < 3) or (3 >= 0)
6
Combining operators
• What does this line do?
>>> (3 == 5) and (2 < 3) or (3 >= 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 < 3)) or (3 >= 0)
>>> (3 == 5) and ((2 < 3) or (3 >= 0))
 True
 False
7
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
The if statement
if condition:
statement
statement
….
statement
Block of instructions or suite
Draw the flow of control diagram.
9
The if-else statement
if condition:
statement
statement
….
statement
else:
statement
statement
….
statement
Draw the flow of control diagram.
10
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
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
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
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
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
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
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
Practice
• Construct boolean expressions (compute the
tables)
• Write examples that use conditional
statements .
18
Download