MODULE 4
Python Booleans, Operators and If…Else
A. Rationale
Python's Booleans, operators, and conditional statements, such as If...Else,
constitutefoundational components of its control flow mechanisms, enabling logical
decision-makingand program execution. Booleans serve as binary indicators,
representing true or false values, pivotal for evaluating conditions and controlling
program behavior. Python's diverse set of operators, including arithmetic, comparison,
logical, and identity operators, facilitate complexexpression evaluation and manipulation.
The If...Else statement provides a structured approach to implementing conditional logic,
allowing programmers to execute different codeblocks based on specified conditions.
Together, Booleans, operators, and If...Else statementsempower Python developers to
create dynamic and responsive applications capable of handling various scenarios and
user interactions effectively.
B. Objectives
1. Grasp the concept of Python Booleans, operators, and If...Else
statements, discerningtheir role in program logic and control flow.
2. Utilize Python's operators and conditional statements effectively to construct
logical expressions and control program behavior based on specified conditions.
3. Evaluate Python code snippets containing Booleans, operators, and If...Else
statements to identify logical errors, inefficiencies, or opportunities for
optimization. 4. Develop Python programs that employ Booleans, operators, and
If...Else statementstoimplement complex decision-making logic and conditional
execution pathways, demonstrating proficiency in program design and
implementation.
C. Pre-Test
1. How do Python Booleans and operators contribute to logical expression
evaluationand control flow within program structures?
2. What role does the If...Else statement play in executing different code blocks
basedonspecified conditions in Python programming?
3. In what ways can understanding Python Booleans, operators, and If...Else
statementsenhance the precision and efficiency of program logic and decision-
making processes?
D. Learning Activities
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.
1 | Mo d u l e 4 - Pyt hon
When you compare two values, the expression is evaluated and Python returns the
Booleananswer:
When you run a condition in an if statement, Python returns True or
False:
Evaluate Values and Variables
The bool() function allows you to evaluate any value, and give you True or False in
return.
Evaluate a string and a number:
Evaluate two variables:
Most Values are True
Almost any value is evaluated to True if it has some sort of content.
Any string is True, except empty strings.
Any number is True, except 0.
Any list, tuple, set, and dictionary are True, except empty ones.
The following will return True:
2 | Mo d u l e 4 - Pyt hon
Some Values are False
In fact, there are not many values that evaluate to False, except empty values, such as (),
[], {}, "", the number 0, and the value None. And of course the value False evaluates to
False.
The following will return False:
One more value, or object in this case, evaluates to False, and that is if you have an
object that is made from a class with a __len__ function that returns 0 or False:
Functions can Return a Boolean
You can create functions that returns a Boolean Value.
Print the answer of a function:
You can execute code based on the Boolean answer of a function.
Print "YES!" if the function returns True, otherwise print "NO!":
3 | Mo d u l e 4 - Pyt hon
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.
Check if an object is an integer or not:
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:
Python divides the operators in the following groups:
1. Arithmetic operators
2. Assignment operators
3. Comparison operators
4. Logical operators
5. Identity operators
6. Membership operators
7. Bitwise operators
Python Arithmetic Operators
Arithmetic operators are used with numeric values to perform common mathematical
operations:
4 | Mo d u l e 4 - Pyt hon
Python Assignment Operators
Assignment operators are used to assign values to variables:
Python Comparison Operators
Comparison operators are used to compare two values:
Python Logical Operators
Logical operators are used to combine conditional statements:
Python Identity Operators
Identity operators are used to compare the objects, not if they are equal, but if they are
actually the same object, with the same memory location:
Operator Precedence
Operator precedence describes the order in which operations are performed.
Parentheses has the highest precedence, meaning that expressions inside
parentheses must beevaluated first:
Multiplication * has higher precedence than addition +, and therefor multiplications are
evaluated before additions:
If two operators have the same precedence, the expression is evaluated fromleft to right.
Addition + and subtraction - has the same precedence, and therefore we evaluate the
expression from left to right:
6 | Mo d u l e 4 - Pyt hon
Python If ... Else
Python Conditions and If statements
Python supports the usual logical conditions from mathematics: 1.
Equals: a == b
2. Not Equals: a != b
3. Less than: a < b
4. Less than or equal to: a <= b
5. Greater than: a > b
6. Greater than or equal to: a >= b
These conditions can be used in several ways, most commonly in "if statements" and
loops. An "if statement" is written by using the if keyword.
If statement:
In this example we use two variables, a and b, which are used as part of the if
statement totest whether b is greater than a. As a is 33, and b is 200, we know that
200 is greater than33, and so we print to screen that "b is greater than a".
Indentation
Python relies on indentation (whitespace at the beginning of a line) to define scope
inthecode. Other programming languages often use curly-brackets for this purpose.
If statement, without indentation (will raise an error):
7 | Mo d u l e 4 - Pyt hon
Elif
The elif keyword is Python's way of saying "if the previous conditions were
not true, thentrythis condition".
In this example a is equal to b, so the first condition is not true, but the elif condition is
true, so we print to screen that "a and b are equal".
Else
The else keyword catches anything which isn't caught by the preceding
conditions.
In this example a is greater than b, so the first condition is not true, also the elif
conditionisnot true, so we go to the else condition and print to screen that "a is
greater than b".
You can also have an else without the elif:
Short Hand If
If you have only one statement to execute, you can put it on the same line as the if
statement.
8 | Mo d u l e 4 - Pyt hon
One line if statement:
Short Hand If ... Else
If you have only one statement to execute, one for if, and one for else, you can put
it all onthe same line.
One line if else statement:
This technique is known as Ternary Operators, or Conditional
Expressions. You can also have multiple else statements on the
same line.
One line if else statement, with 3 conditions:
And
The and keyword is a logical operator, and is used to combine conditional statements.
Test if a is greater than b, AND if c is greater than a:
Or
The or keyword is a logical operator, and is used to combine conditional statements.
Test if a is greater than b, OR if a is greater than c:
9 | Mo d u l e 4 - Pyt hon
Not
The not keyword is a logical operator, and is used to reverse the result of the
conditional statement.
Test if a is NOT greater than b:
Nested If
You can have if statements inside if statements, this is called nested if
statements.
The pass Statement
if statements cannot be empty, but if you for some reason have an if statement with
nocontent, put in the pass statement to avoid getting an error.
E. Formative Test
F. References
1. [Link] 2.
[Link] 3.
[Link]
10 | Mo d u l e 4 - Pyt hon