Download presentation

Presentation is loading. Please wait.

Published byFreddy Ventre Modified over 2 years ago

1
Lilian Blot PART II: BOOLEAN EXPRESSION Core Elements Autumn 2013 TPOP 1

2
Lilian Blot Overview Boolean Type (bool) Boolean Expression Autumn 2013 TPOP 2

3
Lilian Blot BOOLEAN & CONDITIONS More on Data Type and Operators Autumn 2013 TPOP 3

4
Lilian Blot Comparing Numbers We can compare numbers using comparators such as: >, =, <=, ==, != Autumn 2013 TPOP 4 >>> 10 < 7 False >>> 10 > 7 True Python shell The boolean expression returns True or False (note the capital letter). The returned value is a Boolean, a data type that can take only two values, True or False.

5
Lilian Blot Comparing Variables Values in variables can be compared, and the result can be stored in a variable. Autumn 2013 TPOP 5 >>> my_condition = 10 < 7 >>> type(my_condition) >>> small = 6 >>> big = 2000 >>> is_greater = (big >= small) >>> print is_greater True Python shell

6
Lilian Blot Pitfalls Make sure to compare comparable objects Autumn 2013 TPOP 6 >>> 7 <= 6 True >>> 7 >= 6 False >>> 7 >= 6 True Python shell Note: strings are compared alphabetically Exact equality between floating-point numbers is a dangerous concept. see video: http://www.youtube.com/watch?v=3fr1rcElLCI&feature=relmfu http://www.youtube.com/watch?v=3fr1rcElLCI&feature=relmfu Confusing Assignment = and equality operator == a common mistake

7
Lilian Blot Boolean Expression composition Boolean expressions can be combined to form complex expressions composition can be done using three operators and, or, not Autumn 2013 TPOP 7 >>> discount = ((age < 26) or (is_student == True)) >>> resit = not((average >= 40) and (compensable < 40)) Python shell

8
Lilian Blot Truth Tables andTrueFalse True False Autumn 2013 TPOP 8 orTrueFalse True FalseTrueFalse not TrueFalse True

9
Lilian Blot Operator Precedence OperatorName +, - Unary plus and minus (e.g. -10) ** Exponentiation not *, /, //, % Multiplication, division, integer division, and remainder +, - Binary plus and minus (e.g. 3-10) = Comparison ==, != Equality and or =, +=, -=, *=, /=, //=, %= Assignment operators Autumn 2013 TPOP 9

10
Lilian Blot Additional Material Videos: Boolean: http://www.youtube.com/watch?v=Hhe19V12i28&feature=relmfu http://www.youtube.com/watch?v=Hhe19V12i28&feature=relmfu Autumn 2013 TPOP 10

11
Lilian Blot Exercises Give a truth table that shows the (Boolean) value of each of the following Boolean expressions, for every possible combination of input values. a) not (P and Q) b) (not P) and Q c) (not P) or (not Q) d) (P and Q) or R e) (P or R) and (Q or R) Hint: including columns for intermediate expression is helpful. Autumn 2013 TPOP 11

12
Lilian Blot Exercises Here is some practice on writing complex Boolean expressions. Your task is to convert the English description into a Python Boolean expression. The variables to be used in the expressions are: dogSize with string values "small", "medium", and "large" dogHasSpots with Boolean values True or False dogAge with positive integer values dogColor with string values "white", "black", "red", and "brown" catSize with string values "small", "medium", and "large" catHasSpots with Boolean values True or False catColor with string values "white", "black", "orange", and "tabby" catEyeColor with string values "green", "brown", "blue" catAge with positive integer values An old dog is one with an age greater than seven years while an old cat has an age greater than or equal to nine years. A cat and a dog are young if they are younger than three years old. Autumn 2013 TPOP 12

13
Lilian Blot Exercises Write a Boolean expression that captures "Dogs that are large and have spots or cats that are white but do not have blue eyes." Young cats and dogs of small size and with brown eyes Old cats and dogs, where cats are white and dogs are not black. Autumn 2013 TPOP 13

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google