Python Control Statements
Learn Python control statements easily with examples: if, else, loops, nested conditions, break and continue in simple language.
Introduction
Imagine you're driving a car. Sometimes you go straight, sometimes you turn, and sometimes you stop. Programming works in a very similar way! A program doesn't always run in a straight line—it makes decisions and repeats actions. That's where control statements in Python come in.
Control statements help your program decide what to do next. Whether it's checking a condition, repeating a task, or stopping execution, these tools are essential for building real-world applications.
📋 Table of Contents
What are Control Statements?
Control statements are used to control the flow of a program.
In simple words: They help your program make decisions and repeat actions.
Understanding if Statement
What is if? The if statement checks a condition. If the condition is true, it runs the code.
age = 18
if age >= 18:
print("You can vote")
You can vote
age >= 18 is trueUsing else Statement
What is else? The else block runs when the condition is false.
age = 15
if age >= 18:
print("You can vote")
else:
print("You cannot vote")
You cannot vote
Using elif Statement
What is elif? elif means "else if". It checks multiple conditions.
marks = 75
if marks >= 90:
print("Grade A")
elif marks >= 60:
print("Grade B")
else:
print("Grade C")
Grade B
Nested Conditions
What are Nested Conditions? When you use an if inside another if, it's called nesting.
age = 20
citizen = True
if age >= 18:
if citizen:
print("You can vote")
You can vote
Real-Life Example of Conditions
🏧 ATM Withdrawal System
balance = 5000
withdraw = 2000
if withdraw <= balance:
print("Transaction Successful")
else:
print("Insufficient Balance")
Introduction to Loops
Loops are used to repeat a block of code multiple times.
Think of it like brushing your teeth daily—you repeat the same action!
for Loop in Python
What is for loop? A for loop is used when you know how many times to repeat.
for i in range(5):
print("Hello")
Hello Hello Hello Hello Hello
while Loop in Python
What is while loop? A while loop runs as long as the condition is true.
i = 1
while i <= 3:
print(i)
i += 1
1 2 3
Difference Between for and while Loop
Break Statement
What is break? break stops the loop immediately.
for i in range(5):
if i == 3:
break
print(i)
0 1 2
Continue Statement
What is continue? continue skips the current iteration.
for i in range(5):
if i == 2:
continue
print(i)
0 1 3 4
Real-Life Loop Examples
🔢 Print Even Numbers
for i in range(1, 11):
if i % 2 == 0:
print(i)
🔐 Password Retry System
attempts = 3
while attempts > 0:
password = input("Enter password: ")
if password == "1234":
print("Access Granted")
break
else:
print("Try Again")
attempts -= 1
Complete Program Example
marks = int(input("Enter marks: "))
if marks >= 90:
print("Excellent")
elif marks >= 50:
print("Pass")
else:
print("Fail")
print("\nCounting attempts:")
for i in range(1, 4):
print("Attempt", i)
Common Mistakes to Avoid
= instead of === assigns a value, while == compares two values — use == in conditions.while loop without a proper exit condition will run forever and crash your program.Conclusion
Control statements are the backbone of programming in Python. They allow your program to think, decide, and repeat actions—just like humans do in daily life. Once you understand these concepts, you'll be able to build smarter and more interactive programs. Keep practicing and try creating your own examples—it's the best way to learn!
❓ FAQs
They control the flow of execution like decisions and loops.
if starts a condition, elif checks additional conditions.
Use it when you know how many times to repeat.
It stops the loop immediately.
Yes, if the condition never becomes false (infinite loop).
Elevate your craft — from first spark to full-stack brilliance

0 Comments
If you have any doubts, Please let me know