0% found this document useful (0 votes)
8 views20 pages

Python Part 3

This document provides a detailed overview of loops in Python, specifically focusing on for and while loops, along with their syntax and usage. It includes examples of loop operations, the range function, and control statements like break and continue. Additionally, it covers nested loops and various pattern printing exercises to enhance programming logic skills.

Uploaded by

satyamanand638
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views20 pages

Python Part 3

This document provides a detailed overview of loops in Python, specifically focusing on for and while loops, along with their syntax and usage. It includes examples of loop operations, the range function, and control statements like break and continue. Additionally, it covers nested loops and various pattern printing exercises to enhance programming logic skills.

Uploaded by

satyamanand638
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

---

🐍 PYTHON PROGRAMMING
📘 PART 3: LOOPS (for, while),
break, continue & PATTERNS
(DETAILED)

> Is part ke baad tum repeat hone

💪
wale kaam easily kara paoge
Aur logic strong ho jaayega
---

1️⃣ Loop kya hota hai?

📌 Simple language me:


Jab ek kaam baar-baar repeat
karna ho, tab loop use hota hai

🧠 Real-life example:
Attendance lena (40 students)

Table print karna (1 se 10)

Numbers print karna (1 se 100)


👉 Har baar same code likhna ❌
👉 Loop use karna ✅
---

2️⃣ Types of Loops in Python

Python me mainly 2 loops hote


hain:

1. for loop
2. while loop

---

3️⃣ FOR LOOP (Most Used)

📌 Jab pata ho kitni baar loop


chalega

🔹 Syntax:
for variable in sequence:
statement
---

Example 1️⃣: Numbers print (0 to 4)

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

Output:

0
1
2
3
4
---

4️⃣ range() function (Very Important)

range(start, stop, step)

Form​Meaning

range(5)​ 0 to 4
range(1, 6)​ 1 to 5
range(1, 10, 2)​ 1,3,5,7,9

Example:
for i in range(1, 11):
print(i)

👉 1 se 10 print hoga
---

5️⃣ Table Program (Classic)

Example: Table of 5

n=5

for i in range(1, 11):


print(n, "x", i, "=", n*i)

---

6️⃣ WHILE LOOP

📌 Jab condition true rahe tab tak


loop chale

🔹 Syntax:
while condition:
statement
---

Example:

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

Output:

1
2
3
4
5
👉 i = i + 1 bahut important hai
Nahi to infinite loop 😵

---

7️⃣ for vs while (Difference)

for loop​while loop

Fixed times​ Condition based


Easy​Flexible
range use​manual control
---

8️⃣ break statement

📌 Loop ko turant rok deta hai


Example:

for i in range(1, 10):


if i == 5:
break
print(i)

Output:
1
2
3
4

---

9️⃣ continue statement

📌 Ek iteration skip karta hai


Example:

for i in range(1, 6):


if i == 3:
continue
print(i)

Output:

1
2
4
5

---

🔟 pass statement
📌 Abhi kuch nahi karna, baad me
code likhenge

for i in range(5):
pass

👉 Error nahi aayega


---

1️⃣1️⃣ Nested Loop (Loop ke andar


Loop)

📌 Mostly pattern programs me


use hota hai
Example:

for i in range(1, 4):


for j in range(1, 4):
print(i, j)

---

1️⃣2️⃣ PATTERN PROGRAMS (VERY


IMPORTANT)

⭐ Pattern 1:
*
*
*
*

for i in range(4):
print("*")

---

⭐ Pattern 2:
*
**
***
****
for i in range(1, 5):
print("*" * i)

---

⭐ Pattern 3:
1
12
123
1234

for i in range(1, 5):


for j in range(1, i+1):
print(j, end="")
print()

---

⭐ Pattern 4:
****
***
**
*

for i in range(4, 0, -1):


print("*" * i)
---

🧪 Practice Questions (Must Do)


1️⃣ 1 se 100 tak numbers print karo
2️⃣ Even numbers (1–50)
3️⃣ Table of any number (user input)
4️⃣ Factorial using loop
5️⃣ Pattern print karo (star / number)

---

🎯 PART 3 RESULT
Ab tum:

for & while loops use kar sakte ho

Infinite loop samajh gaye ho

Pattern programs bana sakte ho

🔥
Logic level kaafi improve ho gaya

You might also like