-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path5python.html
More file actions
200 lines (187 loc) · 4.92 KB
/
5python.html
File metadata and controls
200 lines (187 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Python Chapter 5 - Loops</title>
<style>
body {
font-family: 'Segoe UI', sans-serif;
background: #f9fafb;
margin: 0;
padding: 0;
}
header {
background: #2d3748;
color: white;
text-align: center;
padding: 20px 0;
}
.section {
background: white;
margin: 20px;
padding: 20px;
border-radius: 15px;
box-shadow: 0 5px 15px rgba(0,0,0,0.1);
}
h2 {
color: #2b6cb0;
}
p, li {
line-height: 1.6;
}
.code-block, .output-block {
background: #f4f4f4;
font-family: monospace;
padding: 10px;
border-left: 4px solid #4299e1;
margin-top: 10px;
border-radius: 5px;
}
.output-block {
background: #e6ffe6;
border-left: 4px solid #38a169;
}
.wrong {
background: #ffe6e6;
border-left: 4px solid #e53e3e;
}
ul {
padding-left: 20px;
}
</style>
</head>
<body>
<header>
<h1>Chapter 5: Loops in Python</h1>
<p>Loops ka use repeat karne ke liye hota hai – sab kuch samjho bariki se with rules, mistakes aur examples.</p>
</header>
<div class="section">
<h2>1. for Loop</h2>
<p><b>Meaning:</b> Kisi sequence ke har element ke liye ek kaam baar-baar karna.</p>
<p><b>Syntax:</b></p>
<div class="code-block">
for variable in sequence:<br>
# do something
</div>
<h3>Example:</h3>
<div class="code-block">
fruits = ["apple", "banana", "mango"]<br>
for fruit in fruits:<br>
print(fruit)
</div>
<div class="output-block">
apple<br>
banana<br>
mango
</div>
</div>
<div class="section">
<h2>2. range() with for Loop</h2>
<p><b>range()</b> number series banata hai. Last value exclusive hoti hai.</p>
<div class="code-block">
for i in range(1, 6):<br>
print(i)
</div>
<div class="output-block">
1<br>2<br>3<br>4<br>5
</div>
</div>
<div class="section">
<h2>3. Common Mistakes in for Loop</h2>
<div class="code-block wrong">
for i in 5:<br>
print(i) # ❌ 5 is not iterable
</div>
<div class="code-block">
for i in range(5):<br>
print(i) # ✅ Correct
</div>
</div>
<div class="section">
<h2>4. while Loop</h2>
<p><b>Meaning:</b> Jab tak condition true hai, tab tak loop chalega.</p>
<div class="code-block">
count = 1<br>
while count <= 5:<br>
print(count)<br>
count += 1
</div>
<div class="output-block">
1<br>2<br>3<br>4<br>5
</div>
</div>
<div class="section">
<h2>5. Infinite Loop Mistake</h2>
<div class="code-block wrong">
while True:<br>
print("Infinite loop") # ❌ No stop condition
</div>
</div>
<div class="section">
<h2>6. break Statement</h2>
<p><b>Meaning:</b> Loop ko turant band kar deta hai.</p>
<div class="code-block">
for i in range(1, 6):<br>
if i == 3:<br>
break<br>
print(i)
</div>
<div class="output-block">
1<br>2
</div>
</div>
<div class="section">
<h2>7. continue Statement</h2>
<p><b>Meaning:</b> Current iteration skip karta hai, agla chalu kar deta hai.</p>
<div class="code-block">
for i in range(1, 6):<br>
if i == 3:<br>
continue<br>
print(i)
</div>
<div class="output-block">
1<br>2<br>4<br>5
</div>
</div>
<div class="section">
<h2>8. else with Loops</h2>
<p><b>Meaning:</b> Agar loop break nahi hota to <code>else</code> part run hota hai.</p>
<div class="code-block">
for i in range(3):<br>
print(i)<br>
else:<br>
print("Loop done")
</div>
<div class="output-block">
0<br>1<br>2<br>Loop done
</div>
</div>
<div class="section">
<h2>9. else skipped when break used</h2>
<div class="code-block">
for i in range(3):<br>
if i == 1:<br>
break<br>
print(i)<br>
else:<br>
print("Loop done")
</div>
<div class="output-block">
0
</div>
</div>
<div class="section">
<h2>10. Nested Loops</h2>
<p><b>Meaning:</b> Ek loop ke andar doosra loop (jaise table banate ho).</p>
<div class="code-block">
for i in range(1, 4):<br>
for j in range(1, 3):<br>
print(f"{i} x {j} = {i*j}")
</div>
<div class="output-block">
1 x 1 = 1<br>1 x 2 = 2<br>2 x 1 = 2<br>2 x 2 = 4<br>3 x 1 = 3<br>3 x 2 = 6
</div>
</div>
</body>
</html>