1.1.1 if, elif & else
In programming, we often need to control the flow of our program based on different conditions or repeat certain actions multiple times. There are two main types of control structures, Decision Making and Looping:
Decision Making
Decision making in programming allows the program to choose different actions based on conditions. Python provides a variety of conditional statements to implement decision making.
If Statement
The if statement allows us to make decisions based on conditions. If the condition is true, it runs a block of code.
# Syntax of if statement
if condition:
# code to run if the condition is true
Example: If the temperature is above degrees, we print a message.
temperature = 35
if temperature > 30:
print("It is a hot day")
If-else Statement
The if-else statement allows us to execute one block of code if a condition is true and another block if the condition is false.
# Syntax of if-else statement
if condition:
# code to run if the condition is true
else:
# code to run if the condition is false
Example:
temperature = 15
if temperature > 30:
print("It's a hot day")
else:
print("It's not a hot day")
Short Hand if-else Statement
Python also allows a short-hand if-else statement that can be written in a single line.
# Syntax of short hand if-else statement
action_if_true if condition else action_if_false
temperature = 15
m = "It's a hot day" if (temperature > 30) else "It's not a hot day"
print(m)
Class Activity
Write an if-else statement and a short-hand if-else statement to check if a number is even or odd and print the appropriate message.
if-elif-else Statement
The if-elif-else statement allows us to check multiple conditions and execute different blocks of code for each condition.
# Syntax of if-elif-else statement
if condition1:
# code to run if condition1 is true
elif condition2:
# code to run if condition2 is true
else:
# code to run if none of the conditions are true
Example:
weather = "cloudy" # The output depends on the value stored in the variable weather
if weather == "sunny":
print("Wear sunglasses")
elif weather == "rainy":
print("Take an umbrella")
else:
print("Enjoy your day!")
Class Activity
Write an if-elif-else statement to check if a number is positive, negative, or zero.
Looping Constructs
Loops help us repeat actions, making our code more efficient and easier to read. There are two main types of loops in Python: while loops and for loops.
while Loop
A while loop runs as long as a condition is true. It checks the condition before each iteration and stops running when the condition is no longer true.
# Syntax of while loop
while condition:
# code to run while the condition is true
Example: Add to a number until it reaches .
number = 1
while number < 10:
print(number)
number += 1
Class Activity
Write a Python program that prints even and counts the odd numbers from to using a while loop.
for Loop
A for loop repeats a block of code a specific number of times. It is commonly used to iterate over a sequence (like a list, tuple, or string).
# Syntax of for loop
for variable in sequence:
# code to run for each element in the sequence
Example 1: Say "Hello" to each friend in a list of friends.
friends = ["Ahmad", "Ali", "Hassan"]
for friend in friends:
print("Hello", friend)
Explanation: In this example, the code goes through each friend in the list and prints a greeting message for each one.
Class Activity
- Write a for loop using
range()to print the even numbers from to . - Write a Python program that prints the first multiples of using a for loop and the
range()function.