Python Programming:
Control Flow (Conditions)
S1, Y2 (2023-2024)
Contents
• What is Control Flow?
• Conditional Statements
- if Statement
- if-else Statement
- if-elif-else Statement
• Boolean Values and Expressions
- Boolean Values (True and False)
- Expressions
• Exercise 1
• Exercise 2
2
What is Control Flow?
Control flow refers to the order in which a program's statements are executed. It is the
sequence of events or instructions that determine the execution path of a program.
Control flow is crucial in programming because it allows you to direct the flow of
your code, making decisions and executing actions based on certain conditions.
Key elements of control flow include:
• Sequential Execution: Code is executed in the order it appears, from top to bottom.
• Conditional Execution: Using constructs like if, else, and elif to make decisions
based on conditions. Certain code blocks are executed only if specific conditions are
met.
• Loops: Employing constructs like for and while loops to execute a block of code
repeatedly. Loops can iterate over a sequence or continue execution until a certain
condition is met.
• Exception Handling: Using try, except, else, and finally blocks to handle
exceptions and errors in a controlled manner.
3
Conditional Statements
Conditional statements are programming constructs that enable a program to make
decisions and execute different blocks of code based on certain conditions. In other
words, these statements allow you to control the flow of your program by evaluating
whether a given condition is true or false and then executing specific code
accordingly.
In Python, the primary conditional statements are:
• if Statement: The if statement is used to execute a block of code if a specified
condition is true.
Example:
x = 10
if x > 0:
print("x is positive")
4
Conditional Statements
In Python, the primary conditional statements are:
• if-else Statement:
The if-else statement allows you to execute one block of code if the condition is
true and another block if the condition is false.
Example:
x = -5
if x > 0:
print("x is positive")
else:
print("x is non-positive")
5
Conditional Statements
In Python, the primary conditional statements are:
• if-elif-else Statement:
The if-elif-else statement allows you to check multiple conditions and execute
different blocks of code based on the first true condition encountered.
Example:
x=0
if x > 0:
print("x is positive")
elif x == 0:
print("x is zero")
else:
print("x is negative")
6
Boolean Values and Expressions
• Boolean values and expressions are fundamental concepts in programming that
involve logical operations and conditions.
• In Python, the primary boolean data type is bool, and it has two possible values:
True and False.
• Boolean values are often used in conditional statements and expressions to control
the flow of a program.
Here are some key concepts related to boolean values and expressions in Python:
Boolean Values (True and False):
There two values (True and False) only.
Example:
x = True
y = False
7
Boolean Values and Expressions
Here are some key concepts related to boolean values and expressions in Python:
Comparison Operators:
Comparison Name Symbol Example
Equality == result = (5 = = 5) # True
Inequality != result = (5 != 3) # True
Greater Than > result = (10 > 5) # True
Less Than < result = (10 < 5) # False
Greater Than or Equal To >= result = (10 >= 5) # True
Less Than or Equal To <= result = (10 <= 5) # False
In programming, an expression is a combination of values, variables, operators, and function
calls that can be evaluated to produce a result. Expressions are the building blocks of code that
perform computations and yield a value. They can be as simple as a single variable or as complex as
a combination of multiple operations.
8
Boolean Values and Expressions
Here are some key concepts related to boolean values and expressions in Python:
Logical Operators:
Logical Name Symbol Example
AND and result = (True and False) # False
OR or result = (True or False) # True
NOT not result = not True # False
Membership Operators:
Membership Name Symbol Example
IN in my_list = [1, 2, 3, 4, 5]
result = (3 in my_list) # True
NOT IN not in my_list = [1, 2, 3, 4, 5]
result = (6 not in my_list) # True
9
Boolean Values and Expressions
Here are some key concepts related to boolean values and expressions in Python:
Identity Operators:
Identity Name Symbol Example
IS is x = [1, 2, 3]
y = [1, 2, 3]
result = (x is y) # False (different objects)
NOT IS is not x = [1, 2, 3]
y = [1, 2, 3]
result = (x is not y) # True
Boolean expressions are frequently used in control flow statements like if, elif, and while to make
decisions based on conditions. They also play a crucial role in creating more complex logic in
programs. Understanding boolean values and expressions is essential for writing effective and
logical code.
10
Exercise 1: Checks Whether - Positive, Negative or Zero
Explanation of the code:
1. The user is prompted to enter an integer using input( ).
2. The input is converted to an integer using int( ).
3. The program uses conditional statements (if, elif, and else) to
check whether the number is positive, negative, or zero.
4. The result is printed to the console.
You can run this program and test it with different input values to
see how the conditions are evaluated. This exercise helps you
practice using basic conditions in Python.
11
Exercise 1: Checks Whether - Positive, Negative or Zero
We'll create a program that checks whether a given number is positive, negative, or
zero.
# Exercise: Positive, Negative, or Zero
# Step 1: Get input from the user (an integer)
user_input = input("Enter an integer: ")
# Step 2: Convert the input to an integer (assuming the user enters a valid integer)
number = int(user_input)
# Step 3: Use conditions to check whether the number is positive, negative, or zero
if number > 0:
print(f"{number} is positive.")
elif number < 0:
print(f"{number} is negative.")
else:
print("The number is zero.")
12
Exercise 2: Simple Grade Calculator
Explanation of the code:
1. The user is prompted to enter the marks (assumed to be a float
between 0 and 100).
2. The program uses multiple if and elif statements to check the
range of marks and determine the corresponding grade.
3. The calculated grade is then displayed to the user.
You can run this program and test it with different input values to
see how it determines the grade based on the specified ranges.
This exercise helps you practice using multiple conditions and
handling different scenarios.
13
Exercise 2: Simple Grade Calculator
Write a programming involves multiple conditions and user input and we'll build a
simple grade calculator.
# Step 1: Get input from the user (marks out of 100)
marks = float(input("Enter the marks (out of 100): "))
# Step 2: Use conditions to determine the grade
if 90 <= marks <= 100:
grade = 'A'
elif 80 <= marks < 90:
grade = 'B'
elif 70 <= marks < 80:
grade = 'C'
elif 60 <= marks < 70:
grade = 'D'
elif 0 <= marks < 60:
grade = 'F'
else:
grade = 'Invalid input. Marks should be between 0 and 100.'
# Step 3: Display the grade to the user
print(f"Grade: {grade}")
14
Thanks for attention,
Any Question?
15