Python Control Statements Explained in Simple Way 🐍🔄


Python control flow example showing if else, loops, match case and while loop
Python control statements tutorial explaining if, for loop, while loop, match case, and practical examples in simple English.

Control Flow means the order in which statements execute in a program. Control statements help us make decisions and repeat tasks.


1️⃣ If Statement

If statement is used to make decisions. We use it when we want to execute code only if a condition is true.

📌 Where we use it?

  • Validating input
  • Checking conditions
  • Making decisions

Example:

age = 18

if age >= 18:
    print("You can vote")
else:
    print("You cannot vote")

2️⃣ For Loop

For loop is used when we know how many times we want to repeat. It works with list, tuple, set, range.

📌 Where we use it?

  • Loop through list items
  • Print numbers in range
  • Process collections

Example:

for i in range(1, 6):
    print(i)

3️⃣ While Loop

While loop is used when we don’t know how many times to repeat. It runs while condition is true.

📌 Where we use it?

  • User input loops
  • Menu driven programs
  • Unknown iteration count

Example:

count = 1

while count <= 5:
    print(count)
    count += 1

4️⃣ Do While Loop

Python does NOT have a direct do-while loop. But we can simulate it using while True.

📌 Where we use it?

  • Run code at least once
  • Menu programs

Example:

while True:
    num = int(input("Enter positive number: "))
    if num > 0:
        print("Valid number")
        break

5️⃣ Match Case

Match case is like switch statement. It is cleaner than multiple if-else.

📌 Where we use it?

  • Menu selection
  • Multiple fixed values
  • Pattern matching

Example:

day = 2

match day:
    case 1:
        print("Monday")
    case 2:
        print("Tuesday")
    case _:
        print("Other day")

6️⃣ Foreach Loop

Python does not use the word foreach, but for loop works like foreach.

Example:

names = ["Anna", "John", "Sam"]

for name in names:
    print(name)

📌 Extra Practical Examples

✔ Circumference of Circle (With Validation)

r = float(input("Enter radius: "))

if r > 0:
    pi = 3.14
    circumference = 2 * pi * r
    print("Circumference =", circumference)
else:
    print("Radius must be positive")

✔ Hypotenuse of Right Triangle

a = float(input("Enter first side: "))
b = float(input("Enter second side: "))

c = (a*a + b*b) ** 0.5
print("Hypotenuse =", c)

✔ Largest of 3 Numbers

n1 = 60
n2 = 50
n3 = 30

if n1 > n2 and n1 > n3:
    print(n1)
elif n2 > n1 and n2 > n3:
    print(n2)
else:
    print(n3)

✔ Even Numbers Using For Loop

my_list = [7, 22, 14, 9, 8, 21, 13]

for i in my_list:
    if i % 2 == 0:
        print(i)

✔ Linear Search with Break

target = 43
my_list = [2,5,4,6,10,9]

for i in my_list:
    if i == target:
        print("Found")
        break
else:
    print("Not Found")

✔ Sum Until User Enters 0 (While Loop)

total = 0

while True:
    num = int(input("Enter number: "))
    if num == 0:
        break
    total += num

print("Total =", total)

🎯 Control statements are the heart of programming. They help us build logical, dynamic and interactive programs 🚀.

📚 Related Articles

Article No Article Title & Link
1 🐍 Getting Started with Python for Data Science – Fundamentals Guide 🚀
2 🐍 Python Collections (Arrays) Simplified: List, Tuple, Set, & Dictionary 🐍
3 🐍 Understanding Python Lists in Simple Way 📋
4 🐍 Understanding Python Tuples in Simple Way 📦
5 🐍 Understanding Python Sets in Simple Way 🎴
6 🐍 Understanding Python Range & Dictionaries 📘
7 🐍Python Operators Guide: Arithmetic, Logical, & Precedence Explained 🐍
8 🐍 Python Control Statements Complete Guide 🔄
9 🐍 Python Functions Explained in Simple Way ⚙️
10 🐍 Python Map, Filter, Lambda & Modules – Simple Notes
11 🐍 Python OOP Concepts – Simple Short Notes
12 🐍  NumPy, Pandas & Web Scraping – Simple Python Notes📊

Post a Comment

Previous Post Next Post