Skip to content

Commit e62f2ba

Browse files
authored
Merge pull request #729 from codewithdhruba01/doc/question
doc: Add Practice Questions Section to Loops in Python Documentation
2 parents d3f2e08 + 0b4ed2c commit e62f2ba

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed

docs/python/python-loops.md

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,134 @@ for num in numbers:
300300
| Nested loops | Working with multi-dimensional data |
301301
| List comprehensions | Creating lists with concise syntax |
302302

303+
304+
305+
### **Practice Questions:**
306+
307+
#### **1. Print Even Numbers**
308+
309+
Write a Python program to print all **even numbers from 1 to 20** using a `for` loop.
310+
311+
**Expected Output:**
312+
313+
```
314+
2 4 6 8 10 12 14 16 18 20
315+
```
316+
317+
---
318+
319+
#### **2. Sum of Natural Numbers**
320+
321+
Write a program to calculate the **sum of the first 10 natural numbers** using a loop.
322+
323+
**Expected Output:**
324+
325+
```
326+
The sum is: 55
327+
```
328+
329+
---
330+
331+
#### **3. Loop Through a String**
332+
333+
Given the string:
334+
335+
```python
336+
word = "Python"
337+
```
338+
339+
Write a loop to print each **character on a new line**.
340+
341+
---
342+
343+
#### **4. Reverse a List Using Loop**
344+
345+
Without using the built-in `reverse()` or slicing, write a program to **print a list in reverse order** using a loop.
346+
347+
```python
348+
numbers = [1, 2, 3, 4, 5]
349+
```
350+
351+
**Expected Output:**
352+
353+
```
354+
5
355+
4
356+
3
357+
2
358+
1
359+
```
360+
361+
---
362+
363+
#### **5. Multiplication Table**
364+
365+
Write a Python program to **print the multiplication table of 7** using a loop.
366+
367+
**Expected Output:**
368+
369+
```
370+
7 x 1 = 7
371+
7 x 2 = 14
372+
...
373+
7 x 10 = 70
374+
```
375+
376+
---
377+
378+
#### **6. Using `break` Statement**
379+
380+
Write a loop that prints numbers from 1 to 10.
381+
382+
* If the number is **5**, use `break` to stop the loop.
383+
384+
**Expected Output:**
385+
386+
```
387+
1
388+
2
389+
3
390+
4
391+
```
392+
393+
---
394+
395+
#### **7. Using `continue` Statement**
396+
397+
Write a loop that prints numbers from 1 to 10.
398+
399+
* **Skip** printing the number **5** using `continue`.
400+
401+
**Expected Output:**
402+
403+
```
404+
1
405+
2
406+
3
407+
4
408+
6
409+
7
410+
8
411+
9
412+
10
413+
```
414+
415+
---
416+
417+
#### **8. Nested Loop – Pattern Printing**
418+
419+
Write a program using **nested loops** to print the following pattern:
420+
421+
```
422+
*
423+
* *
424+
* * *
425+
* * * *
426+
* * * * *
427+
```
428+
429+
---
430+
303431
## Conclusion
304432

305433
Loops are fundamental to programming in Python. Whether you're processing data, automating tasks, or building complex algorithms, mastering `for` and `while` loops will make your code more efficient and powerful. Practice with different data types and loop patterns to become proficient in using loops effectively.

0 commit comments

Comments
 (0)