University of Constantine 3 Salah Boubnider
Faculty of Process Engineering
Department of Process Engineering
ADVANCED PYTHON PROGRAMMING
COURSE
CHAPTER III : CONDITIONAL STRUCTURES IN PYTHON
CREATED BY :HALIMA MAHIDEB
1
COURSE PLAN
1. Introduction
2. The Basic Form of an if Statement
3. The if … else statement
4. The if ..elif… else statement
5. Nested if structures
6. Ternary operator
7. Match Case statement
8. Predicated and Boolean value
2
Chapter II: Basic Concepts in Python III : conditional structures in Python
1. WHAT ARE CONDITIONAL STRUCTURES?
Conditional structures allow a program to make decisions based on certain conditions.
A condition is a logical expression that evaluates to True or False.
They control the flow of execution — deciding which code block should run.
In Python, the main conditional statements are:
o if
o if...else
o if...elif...else
They are used in almost every program to make the code dynamic and intelligent
Conclusion:
Conditional statements are the foundation of program logic, enabling computers to make decisions just like humans.
3
Chapter II: Basic Concepts in Python III : conditional structures in Python
2. THE BASIC FORM OF CONDITION
Definition
The if statement allows you to execute a block of code only if a condition is true.
Syntax
if condition:
# block of statements
if → keyword that starts the condition
condition → an expression that returns True or False
: → required colon at the end of the line
Indented block → the code that runs only if the condition is true
4
Chapter II: Basic Concepts in Python III : conditional structures in Python
3. THE IF...ELSE STATEMENT
Definition
The if...else statement allows a program to choose
between two possible actions:
One block executes if the condition is True
The other executes if the condition is False
Syntax
If condition:
# block executed when condition is
True else:
# block executed when condition is False
Key Points
• The if and else must align at the same indentation level.
• Only one of the two blocks executes.
• Conditions can use comparison or logical operators.
• else doesn’t have a condition — it’s the “otherwise” case.
5
Chapter II: Basic Concepts in Python III : conditional structures in Python
4. THE IF...ELIF….ELSE STATEMENT
Definition: When you need to test more than two
conditions, Python provides the
if...elif...else structure.
It allows the program to check conditions in
sequence and execute only one block — the first
one whose condition is True.
Syntax
If condition1:
# block executed when condition is True
elif condition2:
# block 2 executes if condition2 is
True elif condition3:
# block 3 executes if condition3 is
True else:
# executes if all previous conditions are False
6
Chapter II: Basic Concepts in Python III : conditional structures in Python
4. THE IF..ELIF….ELSE STATEMENT
Best Practices
Arrange conditions from most specific to most general.
Use logical operators (and, or, not) to combine tests.
Keep indentation consistent for readability.
Common Mistakes
• Forgetting the colon : after conditions.
• Misaligned indentation.
• Writing multiple independent if statements instead of one chain (causing multiple blocks to run).
7
Chapter II: Basic Concepts in Python III : conditional structures in Python
5. NESTED IF STRUCTURES
Definition
A nested if is an if statement inside another if block.
It allows you to test a secondary condition only when a primary condition is true.
Syntax
if condition1:
if condition2:
# code executed when both are True
else:
# code executed when condition1 is
True but condition2 is False
else:
# code executed when condition1 is False
8
Chapter II: Basic Concepts in Python III : conditional structures in Python
5. NESTED IF STRUCTURES
When to Use Nested if:
When one condition must be validated only if another
is True. Example:
• Check if a person is a student,
• then check if their grade qualifies for a scholarship.
Best Practices
Avoid too many nested levels — it makes code harder to read.
Try using logical operators (and, or) to simplify:
if x > 0 and x % 2 == 0:
print("x is a positive even number.")
Keep indentation consistent to show hierarchy clearly.
9
Chapter II: Basic Concepts in Python III : conditional structures in Python
6. TERNARY OPERATOR
Definition : In Python, the ternary operator (also called a
conditional expression) is a one-line shortcut for an if-else
statement.
It allows you to assign a value based on a condition — all in one
line.
Syntax
value_if_true if condition else value_if_false
When you can use a ternary operator
You can use it only when your if...else chooses one of
two values — for example, when assigning or returning
something.
10
Chapter II: Basic Concepts in Python III : conditional structures in Python
7. MATCH–CASE STATEMENT Example: Choose a grade message
grade = input("Enter your grade (A, B,
Introduced in Python 3.10 as a new control structure. C, D, F): ")
Similar to switch–case in other languages.
Used to compare one value against multiple patterns. match grade:
More powerful — supports pattern matching, not just case "A":
equality tests. print("Excellent! ")
case "B":
Syntax print("Good job ")
match variable: case "C":
case pattern1: print("You passed ")
# code case "D":
block case print("You can do better next
pattern2: time ")
# code case "F":
block case _: print("Fail ")
# default case case _:
print("Invalid grade")
Key points:
match = keyword (like
Output
switch) case = each
Enter your grade (A, B, C, D, F): B
possible match
Good job
_ = wildcard → acts like “default”
1
1
Chapter II: Basic Concepts in Python III : conditional structures in Python
. PREDICATES AND BOOLEAN VALUES
Definition of a Predicate
A predicate is an expression that can be either True or False.
It represents a logical condition used to make decisions in a program.
Example:
x = 10
x>5 # Predicate →
True x == 8 #
Predicate → False
age >= 18 and age < 60 # Predicate → True or False
isinstance(x, int) # Predicate → True or False
In Python, every conditional test is a predicate:
It evaluates to one of two Boolean values: True or False.
12
Chapter II: Basic Concepts in Python III : conditional structures in Python
7. PREDICATES AND BOOLEAN VALUES
Boolean Data Type
Python evaluates objects as True or False according to their content:
Type: bool
Has only two possible values:
True
False Type Evaluated as False if... Otherwise
You can check the type: any non-zero number
int 0
type(True) # <class →
'bool'> True
float 0.0 any non-zero → True
In Python, any value (not only True any non-empty string
or str empty string ""
→
False) can be used in a predicate or True
condition,
list, tuple, set, dict empty ([], (), {}) non-empty → True
because Python automatically
converts NoneType always False —
it to a Boolean value when it is
tested
13
Chapter II: Basic Concepts in Python III : conditional structures in Python
7. PREDICATES AND BOOLEAN VALUES
Boolean Data Type
Examples
#
Integers
if 5:
print("5 is True") # Executed
if 0: executed if [1, 2]:
print("0 is True") # Not executed print("Non-empty list is True") # Executed
#
Strings
if
"Hello":
print("Non-empty string is True") #
Executed
if "":
print("Empty string is True")# Not
executed
# Lists
if []:
print("Empty list is True") # Not
Explanation bool(3) # True
When Python evaluates a condition: bool("") #
It calls the built-in function bool(value) to False
convert the object to True or False. bool("Hi") #
Example: True bool([])
bool(0) # False # False
14
Chapter II: Basic Concepts in Python III : conditional structures in Python
7. PREDICATES AND BOOLEAN VALUES
Boolean Expressions Example
age = 20
is_adult = age
>= 18
print(is_adult)
#
True
A Boolean expression combines values, variables, and operators to produce a True or False result.
15
Chapter II: Basic Concepts in Python III : conditional structures in Python
7. PREDICATES AND BOOLEAN VALUES
Comparison Operators
Definition
Comparison operators are used to compare two values and return a Boolean result (True or False).
Operator Meaning Example Result
== Equal to 5 == 5 True
!= Not equal to 5 != 3 True
> Greater than 10 > 8 True
< Less than 4<2 False
>= Greater or equal 5 >= 5 True
<= Less or equal 3 <= 2 False
Comparison operators are often used inside if statements to form predicates.
16
Chapter II: Basic Concepts in Python III : conditional structures in Python
7. PREDICATES AND BOOLEAN VALUES
Combining Conditions with Logical Operators
Logical Operators
Operator Meaning Example
and True if both are True x > 0 and y > 0
or True if one or both are x > 0 or y > 0
True
not Negates a condition not(x > 0)
Comparison operators are often used inside if statements to form predicates.
17
Chapter II: Basic Concepts in Python III : conditional structures in Python
7. PREDICATES AND BOOLEAN VALUES
Combining Conditions with Logical Operators
Example:
18
Chapter II: Basic Concepts in Python III : conditional structures in Python
7. PREDICATES AND BOOLEAN VALUES
Summary
Predicates are logical expressions that evaluate to True or
False.
Boolean values are the results of these predicates.
Comparison and logical operators form the core of
decision-making in Python.
19