Loops in Python – for & while loop (break, continue, pass) #6 || Python ka Pitara || DeathCode - DeathCode

Python Series – Part 6: Loops in Python – Full Detailed Guide

🧠 Objective:

  • Python ke 2 primary loops: for and while
  • range() ka use
  • Nested loops
  • Infinite loop se bachne ke tareeke
  • Real-world examples + practice questions

πŸŽ‰ Introduction

"Bhai imagine karo agar tumhe ek list ke 100 items ko print karna ho... kya 100 print() likhoge?

Nahi na! πŸ˜…

Isi liye Python me hote hain Loops – jo kaam baar baar karte hain bina code repeat kiye.

Aaj ke video me hum sikhenge Python ke Loops – real examples, deep concept aur end me kuch solid practice bhi milega!"


πŸ” Section 1: Why Loops?

"Agar koi kaam repeat karna ho β€” jaise 1 se 100 tak number print karna, ya har student ko β€˜Pass’ likhna β€” hum loop ka use karte hain.

Isse humara code chhota, readable aur efficient hota hai."

Example:

print("Hello")
print("Hello")
print("Hello")

Versus:

for i in range(3):
    print("Hello")

πŸ” Section 2: For Loop

"For loop tab use hota hai jab hume pata ho ki hume kitni baar repeat karna hai."

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

🧠 range(n):

  • range(5) means 0 to 4
  • range(start, end, step) for more control

Examples:

for i in range(1, 6):
    print(f"{i} x 2 = {i*2}")
names = ["Rahul", "Aman", "Simran"]
for name in names:
    print("Hello", name)

πŸŒ€ Section 3: While Loop

"Jab hume nahi pata kitni baar repeat karna hai, aur repeat karna hai jab tak koi condition true ho – tab use hota hai while loop."

count = 0
while count < 5:
    print("Count is:", count)
    count += 1

Important:

  • while me hamesha condition update karna zaruri hai β€” warna infinite loop ban sakta hai.

Real Example:

password = ""
while password != "deathcode":
    password = input("Enter password: ")
print("Access Granted βœ…")

πŸ”„ Section 4: Break, Continue, Pass

"Loops me teen powerful cheeze aur hoti hain:"

πŸ”Ή break

for i in range(10):
    if i == 5:
        break
    print(i)

πŸ”Ή continue

for i in range(5):
    if i == 2:
        continue
    print(i)

πŸ”Ή pass

for i in range(3):
    pass  # Will be implemented later

πŸͺœ Section 5: Nested Loops

"Loop ke andar loop bhi chalta hai! Isse hum grids ya combinations bana sakte hain."

for i in range(1, 4):
    for j in range(1, 4):
        print(f"{i} x {j} = {i*j}")

Output:

1 x 1 = 1
1 x 2 = 2
...
3 x 3 = 9

🎁 Section 6: Practice Time – Real Examples

"Ab kuch aise questions jinka answer tum YouTube comment section me likh sakte ho ya khud practice karo!"

βœ… Practice 1:

Print numbers from 10 to 1 (reverse)

βœ… Practice 2:

Print table of 7 using for loop

βœ… Practice 3:

Ask user to enter numbers until they type 0, then show total sum

βœ… Practice 4:

Print star triangle:

*
**
***
****

🏁 Outro

"To bhai, loops ke bina programming adhuri hai β€” aur aaj tumne seekha for, while, break, continue, nested loops β€” sab kuch ek hi video me.

Agle video me hum sikhenge Functions in Python β€” jahan hum apne khud ke tools banayenge!

Don’t forget to practice, aur agar kuch doubt ho to comment section me pooch lena! πŸ”₯"

Β© 2024 DeathCode. All Rights Reserved.