etc are used to compare values and return a boolean. Logical operators like and, or, not are used to combine multiple boolean expressions. When comparing floating point numbers for equality, a small tolerance epsilon is used due to precision limitations."> etc are used to compare values and return a boolean. Logical operators like and, or, not are used to combine multiple boolean expressions. When comparing floating point numbers for equality, a small tolerance epsilon is used due to precision limitations.">
[go: up one dir, main page]

0% found this document useful (0 votes)
50 views10 pages

3.2 - Boolean Expressions, Comparison & Logical Operators

Python uses boolean values True and False to represent true/false conditions. Comparison operators like ==, !=, <, > etc are used to compare values and return a boolean. Logical operators like and, or, not are used to combine multiple boolean expressions. When comparing floating point numbers for equality, a small tolerance epsilon is used due to precision limitations.

Uploaded by

karlstephan065
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views10 pages

3.2 - Boolean Expressions, Comparison & Logical Operators

Python uses boolean values True and False to represent true/false conditions. Comparison operators like ==, !=, <, > etc are used to compare values and return a boolean. Logical operators like and, or, not are used to combine multiple boolean expressions. When comparing floating point numbers for equality, a small tolerance epsilon is used due to precision limitations.

Uploaded by

karlstephan065
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Boolean Expressions & Comparison Operators

Python has two values True and False of type bool , which are useful
for expressing and storing yes/no or true/false kind of data.

1 >>> True
2 True
3 >>> False
4 False
5 >>> type(True)
6 <class 'bool'>
7 >>> type(False)
8 <class 'bool'>

comparison operators, also known as relational operators, are used to


compare two values, such as numbers or string.
The result of such comparison is always a bool value i.e. True or False .

Examples:
1 >>> 10 == 10 # are numbers equal?
2 True
3 >>> 10 == 20
4 False
5
6 >>> x = 5
7 >>> y = 10
8 >>> x == y
9 False
10 >>> x != y
11 True
12 >>> x < y
13 True
14 >>> x > y
15 False
16
17 # We can use variables to store results of boolean expression
18 # (just like we did for arithmetic expressions)
19 >>> x = 3
20 >>> is_positive = x > 0
21 >>> is_positive
22 True
23
24 >>> x = 5
25 >>> y = 5
26 >>> is_equal = x == y
27 >>> is_equal
28 True

A boolean expression is an expression that evaluates to either True


or False . Examples above show how boolean expressions are created
using comparison operators.

Important
Common error is using = (single equals sign) instead of ==
(double equals sign)
= is the assignment operator, used to create variable and assign it

a value
== is a comparison operator used to check for equality between

two values

List of comparison operators

x == y — True if x is equal to y , otherwise False


x != y — True if x is not equal to y , otherwise False

x < y — True if x is less than y , otherwise False

x > y — True if x is greater than y , otherwise False

x <= y — True if x is less than or equal to y , otherwise False

x >= y — True if x is greater than or equal to y , otherwise False


Order of operations

All comparison operators (e.g. == , != , etc.) have same priority and are
evaluated from left to right.
All arithmetic and string operators have higher priority than comparison
operators.

1 >>> x = 5
2 >>> x + 1 == 6
3 True

Try it!
Write a program that take an integer as input from the user and
displays on your screen whether it is true or false that such integer is
even.
Program should run as follows:
Output

Enter a number: 5
5 is an even number: False

Show answer
Comparing strings

Comparison operators work for strings as well.


The comparison is done alphabetically i.e. following a dictionary order

1 >>> "cat" == "cat"


2 True
3 >>> "cat" == "dog"
4 False
5 >>> "cat" != "dog"
6 True
7
8 # letter "c" in "cat" appears before "d" alphabetically
9 >>> "cat" < "dog"
10 True
11
12 # uppercase letters A-Z appear before lowercase a-z alphabetically
13 >>> "cat" < "Dog"
14 False
15
16 # We can compare different types for equality
17 >>> "cat" == 123
18 False
19
20 # But inequality is not allowed
21 >>> "cat" < 123
22 TypeError: '<' not supported between instances of 'str' and 'int'
23
24 # All of the above examples work the same when using variables
25 >>> s1 = "cat"
26 >>> s2 = "dog"
27 >>> s1 == s2
28 False
Equality and floating point numbers

Consider following example:

1 >>> x = 1.1 + 2.2


2 >>> x
3 3.3000000000000003
4 >>> x == 3.3 # what?
5 False

As we saw earlier, a floating-point number is stored with 64-bit finite


precision.
This means that a number may not be stores as precisely as we would
like.
To account for this, when we want to check if two floating point
numbers are equal, we should check if they are “close enough”
Correct way to check for equality of floating-point numbers:

1 # First define how close two numbers need to be


2 epsilon = 0.000001
3
4 >>> x = 1.1 + 2.2
5 >>> x
6 3.3000000000000003
7
8 # Check if x and 3.3 are within epsilon distance
9 >>> abs(x - 3.3) < epsilon
10 True
Logical Operators

Logical operators are useful to combine multiple conditions.


Logical operators take boolean expressions as operands and produce a
result of type bool .

Python has 3 boolean operators:


not — a unary operator
and — binary operator

or — binary operator

Suppose x is a variable of type bool :

x not x

False True

True False

not x evaluates to the opposite value of x .

Suppose x and y are variables of type bool :

x y x and y x y x or y

True True True True True True

True False False True False True

False True False False True True


x y x and y x y x or y

False False False False False False

x and y evaluates to True if and only if both x and y are True .


x or y evaluates to False if and only if both x and y are False .

Order of operations

In order of higher to lower priority:


not

and

or

As usual, you can use parentheses in order to change the priority.


Examples:
What does b and not a or b evaluate to if a = False and b = True ?
b and not a or b
True and not False or True
True and True or True
True or True
True

What does a and not (a or b) evaluate to if a = True and b = False ?


a and not (a or b)
True and not (True or False)
True and not True
True and False
False

Order of operations

Operator Associativity
() (parentheses) -
** Right
Unary - -
* , / , // , % Left
Binary + , - Left
== , != , < , > , <= , >= Left
not -
and Left
or Left
= (assignment) Right

You don’t need to memorize all this, use parenthesis when in doubt!
Try these examples in Thonny

Change the value of x and see results of boolean expressions.

1 x = 30
2 # Is an even number greater than 20?
3 print(x % 2 == 0 and x > 20)
4
5 x = 10
6 # Is an even number or a multiple of 5 greater than 20?
7 print(x % 2 == 0 or x % 5 == 0 and x > 20)
8
9 # Is a multiple of 2 or 5, greater than 20?
10 print((x % 2 == 0 or x % 5 == 0) and x > 20)

Try it!
Write a program that takes 3 integers x, y, z as inputs and prints
out True if y is an even number between x and z , False
otherwise. Assume all 3 numbers will be different.

1 # Retrieve inputs from the user


2 x = int(input("Enter the x: "))
3 y = int(input("Enter the y: "))
4 z = int(input("Enter the z: "))
5
6 # Write code below

Show answer

You might also like