Topic 1.
If-else statement
What Is an If-Else Statement?
An if-else statement allows your program to make
decisions and execute different blocks of code
depending on whether a condition is True or False.
It helps your program “branch” — instead of always
following the same path, it can behave differently
under different conditions.
Basic Structure (Syntax)
If condition:
# Code to execute if condition is True
Else:
# Code to execute if condition is False
🔍 Key Points
If: starts the conditional statement.
Condition: an expression that returns either True or
False.
:: indicates the start of a new block.
Indentation: the indented lines after if or else belong
to that block (Python uses indentation instead of { }
like other languages).
Else: optional, runs only if the if condition is False.
Example 1: Simple If-Else
Age = 18
If age >= 18:
Print(“You are eligible to vote.”)
Else:
Print(“You are not eligible to vote.”)
Output:
You are eligible to vote.
Explanation:
The condition age >= 18 evaluates to True, so the if
block runs.
If age were less than 18, the else block would run
instead.
1. If-Else for Checking Even or Odd Number
Number = 17
If number % 2 == 0:
Print(f”{number} is Even”)
Else:
Print(f”{number} is Odd”)
Explanation:
Uses the modulus operator % to check remainder
when dividing by 2.
If remainder = 0 → even, else → odd.
2. If-Else for Greeting Based on Time
Hour = 15
If hour < 12:
Print(“Good morning!”)
Else:
Print(“Good evening!”)
The output depends on the value of hour.
Realistic example similar to time-based responses
in programs.
3. One-Line (Ternary) If-Else Expression
Password = “1234”
Print(“Access granted” if password == “1234” else
“Access denied”)
Explanation:
Compact form of if-else, perfect for short
conditions.
Executes one of two expressions on a single line.
Topic 2. If-elif-else statement
What is an if-elif-else statement?
An if-elif-else statement is used in Python (and many
other languages) for decision-making.
It checks conditions one by one, and executes the
first block whose condition is True.
If none are True, the else block runs.
Syntax:
If condition1:
# code if condition1 is true
Elif condition2:
# code if condition2 is true
Else:
# code if none are true
Example 1: Temperature check
Temperature = 15
If temperature > 30:
Print(“It’s a hot day!”)
Elif temperature >= 20:
Print(“It’s a pleasant day!”)
Else:
Print(“It’s a bit chilly.”)
Output: It’s a bit chilly.
Example 2: Grading system
Score = 82
If score >= 90:
Print(“Grade: A”)
Elif score >= 75:
Print(“Grade: B”)
Elif score >= 60:
Print(“Grade: C”)
Else:
Print(“Grade: F”)
Output: Grade: B
Example 3: Traffic light signal
Signal = “yellow”
If signal == “red”:
Print(“Stop!”)
Elif signal == “yellow”:
Print(“Get ready!”)
Else:
Print(“Go!”)
Output: Get ready!
Topic 3. Iteration with example
What is Iteration?
Iteration means repeating a set of instructions until a
certain condition is met or for a certain number of
times.
In Python, this is mainly done using loops, such as:
For loop → repeats a fixed number of times (through
a sequence)
While loop → repeats until a condition becomes
False
Example 1: Printing each fruit name
Using a for loop to iterate through a list.
Fruits = [“apple”, “banana”, “cherry”]
For fruit in fruits:
Print(“I like”, fruit)
Output:
I like apple
I like banana
I like cherry
The loop iterates over each item in the list.
Example 2: Counting down with a while loop
Using a while loop to repeat until a condition is false.
Count = 5
While count > 0:
Print(“Countdown:”, count)
Count -= 1
Output:
Countdown: 5
Countdown: 4
Countdown: 3
Countdown: 2
Countdown: 1
The loop iterates as long as count > 0.
Example 3: Sum of even numbers
Using a for loop with range() to sum even numbers.
Total = 0
For num in range(2, 11, 2): # starts from 2, ends
before 11, steps by 2
Total += num
Print(“Sum of even numbers:”, total)
Output:
Sum of even numbers: 30
Topic [Link] Loop with example
What is a For Loop?
A for loop is used to repeat a block of code a specific
number of times or to go through items in a
sequence (like a list, string, or range).
It’s very useful when you know how many times you
want to run the code.
Syntax:
For variable in sequence:
# code to repeat
Example 1: Printing numbers from 1 to 5
For I in range(1, 6):
Print(“Number:”, i)
Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
The loop iterates 5 times, each time assigning I a
new number.
Example 2: Displaying each character in a word
Word = “Smile”
For letter in word:
Print(letter)
Output:
S
M
I
L
E
The loop goes through each character in the
string.
Example 3: Calculating the square of numbers
For num in [2, 4, 6, 8]:
Print(“Square of”, num, “is”, num ** 2)
Output:
Square of 2 is 4
Square of 4 is 16
Square of 6 is 36
Square of 8 is 64
The loop runs for each number in the list and
prints its square.