Introduction
In the previous tutorial, we learned about variables, data
types, and basic operations. But programs become powerful when they can make
decisions and repeat tasks. That’s where control flow comes in. In
this tutorial, we’ll cover if statements, for loops, and while loops — the building blocks of logic in Python.
Step 1: Making Decisions with if
Statements
The if statement lets your
program choose what to do based on conditions.
age = 18
if age >= 18:
print("You are
an adult.")
else:
print("You are a minor.")
·
if checks the condition (age
>= 18).
·
If true, it runs the first
block.
·
If false, it runs the else
block.
Now , change the age = 17, again run the code
Now , change the age = 19, again run the code
You can add more conditions with
elif (short for else if):
Code:
marks = 85
if marks >= 90:
print("Grade:
A")
elif marks >= 75:
print("Grade:
B")
else:
print("Grade: C")
#Try changing marks to see different outputs.
Output:
Grade: B
Now change the marks = 70,
and run the code again
Now change the marks = 91,
and run the code again
Step 2: Repeating Tasks with for
Loops
A for loop is used to iterate
over sequences like lists, strings, or ranges.
Code:
fruits = ["apple", "banana",
"cherry"]
for fruit in fruits:
print(fruit)
Output:
apple
banana
cherry
You can also use range()
to loop over numbers:
Code:
for i in range(5):
print(i)
Output:
0
1
2
3
4
This prints numbers from 0 to 4.
Note: range(5) generates numbers
starting at 0 and ending at 4 (not including 5).
Step 3: Repeating Until a Condition with while Loops
A while loop runs as long as
a condition is true.
Code:
count = 1
while count <= 5:
print("Count
is:", count)
count += 1
Output:
Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
·
The loop continues until count becomes greater than 5.
·
count += 1 increases the
value each time.
Be careful: if the condition never becomes false, you’ll
create an infinite loop.
Step 4: Combining Logic
You can combine if statements with loops for more complex
behaviour.
Code:
numbers = [1, 2, 3, 4, 5]
for num in numbers:
if num % 2 == 0:
print(num,
"is even")
else:
print(num,
"is odd")
Output:
1 is odd
2 is even
3 is odd
4 is even
5 is odd
This checks each number and prints whether it’s even or
odd.
Mini Exercise
Try this challenge:
1. Write a program that asks the user for a number.
2. If the number is positive, print "Positive number".
3. If it’s negative, print "Negative number".
4.
If it’s zero, print "Zero".
5. Then, use a for loop to print all numbers from 1 up to that
number.
Code:
num = int(input("Enter a number: "))
if num > 0:
print("Positive
number")
elif num < 0:
print("Negative
number")
else:
print("Zero")
for i in range(1, num + 1):
print(i)
Output:
Enter a number: 4
Positive number
1
2
3
4
Conclusion
In this tutorial, you learned how to:
·
Use if, elif, and else to
make decisions
·
Use for loops to iterate
over sequences
·
Use while loops to repeat
until a condition is false
·
Combine logic for more
powerful programs
Control flow is the logic engine of Python. With
these tools, you can write programs that respond to input, process data, and
automate tasks.
In the next tutorial, we’ll explore functions — how
to organize code into reusable blocks that make your programs cleaner and more
efficient.