Lesson 4: Control Flow in Python (Conditions & Loops)

Control Flow in Python (Conditions & Loops) 🚦

Bro, aaj hum ek aisa topic karne ja rahe hain jo tera Python game hi change kar dega. Naam hai — Control Flow. Suna hai na kabhi ye line — “Code ko control karna seekh le, duniya tere niche hai”? Bas wahi vibe hai aaj. 😂

Ab tak humne variables, input/output aur strings samjhe the. Par ab time hai thoda logic lagane ka — decisions lene ka, aur actions repeat karne ka. Yani ke — conditions aur loops.


Conditional Statements (if, elif, else) 🔍

Ab maan le bro tu ek program likh raha hai jo user ki age check karta hai aur decide karta hai wo driving license ke layak hai ya nahi. Aisa logic likhne ke liye hum use karte hain if-elif-else statements.

Basic Example

age = int(input("Enter your age: "))

if age >= 18:
    print("Bhai tu drive kar sakta hai!")
else:
    print("Abhi chhota hai bro, cycle chala 😅")

Yahan pe scene simple hai — agar condition true hai (age >= 18), toh first block chalega. Nahi toh else wala.

Tu aise multiple conditions bhi check kar sakta hai:

marks = int(input("Enter your marks: "))

if marks >= 90:
    print("A1 student! 🔥")
elif marks >= 75:
    print("Pretty solid bro 😎")
elif marks >= 50:
    print("Pass ho gaya, bas!")
else:
    print("Tu toh legend hai, next time 😅")

See? Python ekdum readable hai — jaise English mein baat kar rahe ho. Bas indentation (space) ka dhyaan rakhna, warna Python bolega “IndentationError” aur tu bolega “Bhai ye kya hua?” 😂


Nested If Statements

Kabhi kabhi condition ke andar bhi condition lagani padti hai — ye hota hai nested if. Example dekh:

age = int(input("Enter your age: "))

if age >= 18:
    license = input("Do you have a license? (yes/no): ")
    if license.lower() == "yes":
        print("Tu full driver hai bro 🏎️")
    else:
        print("Apply kar le yaar, warna police pakdegi 😜")
else:
    print("Abhi baccha hai tu, grow up first 😆")

So basically, Python mein hum condition ke andar bhi logic daal sakte hain. Thoda dhyan rakhna spacing ka, warna code ulta seedha ho jayega.


Comparison & Logical Operators — Thoda Brain Gym 🧠

Conditions likhte time hum ye operators use karte hain:

  • == → Equal to
  • != → Not equal to
  • > → Greater than
  • < → Less than
  • >= / <= → Greater/Less or equal

Logical combinations bhi kar sakte ho using:

  • and → dono true hone chahiye
  • or → koi ek true chalega
  • not → opposite kar dega
age = 20
has_id = True

if age >= 18 and has_id:
    print("Access granted bro 😎")
else:
    print("No entry 🚫")

Logical thinking hi code ka soul hai bro. Python tere logic ko poora obey karta hai — bas tu soch sahi direction mein.


Loops in Python 🔁

Ab maan le tu chah raha hai ek hi kaam baar baar karwana — jaise 10 logon ko message bhejna ya ek pattern print karna. Ab manually 10 print likh dega? Bhai nahi! Iske liye loop hai.

1. for Loop

Jab tu ek sequence pe iterate karna chahe — list, string, range — tab use hota hai for loop.

for i in range(5):
    print("Hello Bro", i)

Output:

Hello Bro 0
Hello Bro 1
Hello Bro 2
Hello Bro 3
Hello Bro 4

Yahan range(5) means 0 se 4 tak. Agar tu chaahe start aur end specify kar sakta hai: range(1, 6) → 1 se 5 tak.

String pe bhi loop chal jaata hai:

for ch in "Python":
    print(ch)

Har letter ek ek karke print hoga. Easy & readable.


2. while Loop

Jab tak ek condition true hai tab tak repeat karna ho — tab while loop aata hai. Thoda dangerous bhi hai, kyunki agar condition false na ho, toh infinite loop ban jaata hai 😅

count = 1

while count <= 5:
    print("Loop number:", count)
    count += 1

Output:

Loop number: 1
Loop number: 2
Loop number: 3
Loop number: 4
Loop number: 5

Always remember: increment ya decrement likhna mat bhoolna, warna tera program kabhi khatam nahi hoga 😂


Nested Loops — Jab loop ke andar loop chale 😅

Kabhi kabhi tu chaahe ek pattern print karna ya matrix process karna — tab ek loop ke andar dusra loop chahiye.

for i in range(3):
    for j in range(2):
        print(f"i={i}, j={j}")

Output:

i=0, j=0
i=0, j=1
i=1, j=0
i=1, j=1
i=2, j=0
i=2, j=1

Thoda confusing lag sakta hai pehle, par jab tu try karega, automatically clear ho jaayega.


Loop Controls — Jab tu decide kare kab loop chale ya ruk jaaye 🧩

Python ke paas teen main loop control tools hain:

  • break – loop ko turant tod deta hai.
  • continue – current iteration skip karke next pe chala jaata hai.
  • pass – kuch nahi karta, placeholder jaise hai.

Example 1: break

for num in range(1, 10):
    if num == 5:
        break
    print(num)

Output:

1
2
3
4

Jaise hi number 5 mila, loop ne bola “Bas ab main jaa raha hu” 😂

Example 2: continue

for num in range(1, 6):
    if num == 3:
        continue
    print(num)

Output:

1
2
4
5

Yahan 3 skip ho gaya, baaki sab chala.

Example 3: pass

for x in range(3):
    if x == 1:
        pass
    print("Value:", x)

Pass basically ek “chill” keyword hai. Kuch nahi karega, par syntax maintain rakhega.


Mini Project: Number Guessing Game 🎯

Ab aagayi asli maza ki baat bro — ek chhota sa project banate hain jisme tera control flow knowledge poora test hoga. Game simple hai — computer ek random number sochega aur user ko guess karna hai.

Goal:

  • Computer ek random number choose kare.
  • User se guesses le.
  • Condition check kare: guess bada hai ya chhota.
  • Correct hone par loop break ho jaaye.

Code:

import random

print("🎯 Welcome to Number Guessing Game!")
secret = random.randint(1, 20)

attempts = 0

while True:
    guess = int(input("Guess a number between 1 and 20: "))
    attempts += 1

    if guess == secret:
        print(f"🔥 Correct! You guessed it in {attempts} tries.")
        break
    elif guess < secret:
        print("Too low bro, thoda bada soch!")
    else:
        print("Too high bro, thoda kam kar!")

print("Game Over 😎")

Example Run:

🎯 Welcome to Number Guessing Game!
Guess a number between 1 and 20: 10
Too low bro, thoda bada soch!
Guess a number between 1 and 20: 17
Too high bro, thoda kam kar!
Guess a number between 1 and 20: 15
🔥 Correct! You guessed it in 3 tries.
Game Over 😎

Dekha? Ye chhota sa project condition + loop + break sabka combo hai. Aise small projects karte rehna — ye hi tera logic strong banayega.


Common Mistakes 🚫

  • 1. Condition ke baad colon (:) bhool jaate hain.
  • 2. Indentation galat kar dete hain (Python is obsessed with spaces 😂).
  • 3. Infinite loops ban jaate hain — condition kabhi false nahi hoti.
  • 4. Comparison aur assignment me confusion (== vs =).

Ek baar galti kar, fir fix kar — tab hi yaad rahega. Ye debugging ka asli taste hai bro 😎


Bro-to-Bro Advice 💬

Bro, control flow basically code ka brain hai. Ye decide karta hai kaunse path pe code chalega aur kab rukega. Agar tu ye samaj gaya, toh tu Python me kuch bhi bana sakta hai — from automation scripts to games.

Mujhe yaad hai jab maine pehli baar loop likha tha, wo kabhi khatam hi nahi hua 😅. Screen pe bas “Hello” chhapta jaa raha tha — tab samjha “while True” ka matlab kya hota hai 😂

So tu bhi dar mat. Experiment kar, galti kar, aur fix kar. Ye hi learning process hai.


Next Steps 🚀

  • Practice kar loops aur conditions ke saath — jaise “FizzBuzz” problem.
  • Ek mini calculator bana, jisme user input aur if-else logic use ho.
  • Next blog mein hum seekhenge — Functions in Python (Code reuse, parameters, return values… sab). Wahan se tu ek level aur upar jayega 🔥

Tab tak ke liye — coding kar, debugging kar, aur coffee pee ke chill kar ☕😎

— Python + Coffee = Code 💻☕

Post a Comment

0 Comments