Python Lesson 5
Python Lesson 5
Python Booleans
• Boolean Values
• Evaluate Values and Variables
• Most Values are True
• Some Values are False
• Functions can Return a Boolean
• Python - Booleans Exercises
Python Operators
• Python Arithmetic Operators
• Python Assignment Operators
• Python Comparison Operators
• Python Logical Operators
• Python Identity Operators
• Python Membership Operators
• Python Bitwise Operators
Python Booleans
Booleans represent one of two values: True or False.
Boolean Values
In programming you often need to know if an expression is True or False.
You can evaluate any expression in Python, and get one of two answers, True or False.
When you compare two values, the expression is evaluated and Python returns the Boolean answer:
Example
print(10 > 9)
print(10 == 9)
print(10 < 9)
When you run a condition in an if statement, Python returns True or False:
Example
Print a message based on whether the condition is True or False:
a = 200
b = 33
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")
Python also has many built-in functions that return a boolean value, like the isinstance() function, which
can be used to determine if an object is of a certain data type:
Example
Check if an object is an integer or not:
x = 200
print(isinstance(x, int))
Exercise:
The statement below would print a Boolean value, which one?
print(10 > 9)
Python Operators
Operators are used to perform operations on variables and values.
In the example below, we use the + operator to add together two values:
Example : print(10 + 5)
<< Zero fill left shift Shift left by pushing zeros in from the right and let the leftmost bits fall off
>> Signed right shift Shift right by pushing copies of the leftmost bit in from the left, and let the
rightmost bits fall off