for Loop and while Loop
for Loop और while Loop
What are Loops in Python?
Loops in Python means loops repeat a block of code. Python mainly uses for loops and while loops.
In real programs, this topic helps in printing tables and patterns. Learn the idea first, then type the program yourself and compare the output.
| Point | Details |
|---|---|
| Course Area | Core Python Basic programming concepts used to write Python programs. |
| Main Use | printing tables and patterns |
| Example File | loops.py |
| Practice Focus | Run, change values, and explain the output line by line. |
Why should you learn this?
- It is useful for printing tables and patterns.
- It connects with processing multiple records.
- It improves your ability to read, write and debug Python programs.
Important Terms
These terms are used directly in this lesson. Understand them before memorising the code.
| Term | Meaning |
|---|---|
| for loop | Loop used to repeat over a sequence or range. |
| while loop | Loop that continues while a condition remains true. |
| range() | Function used to generate a sequence of numbers for loops. |
| break | Statement used to stop a loop immediately. |
| continue | Statement used to skip the current loop step and move to the next one. |
Syntax / Basic Pattern
The simple pattern is: prepare data, apply the concept, then show the result.
for number in range(1, 6):
print(number, end=" ")
print()
count = 3
while count > 0:
print("Count:", count)
count -= 1Complete Example Program
# for loop
for number in range(1, 6):
print(number, end=" ")
print()
# while loop
count = 3
while count > 0:
print("Count:", count)
count -= 1Expected Output
Program Explanation
for number in range(1, 6):repeats the following indented statements.print(number, end=" ")displays information or calculated result on the screen.print()displays information or calculated result on the screen.count = 3stores a value in count.while count > 0:repeats the following indented statements.print("Count:", count)displays information or calculated result on the screen.count -= 1stores a value in count -.
Where will you use it?
- Printing tables and patterns.
- Processing multiple records.
- Repeating a task until condition is met.
Common Mistakes
- Writing code with wrong indentation.
- Using input() value directly in calculations without converting it to int or float.
- Using unclear variable names that make the program difficult to understand.
Practice Tasks
- Type the program in
loops.pyand run it. - Change input values or sample data and observe the new output.
- Create one example related to printing tables and patterns.
- Write 5 lines explaining the logic in your own words.
Summary
Loops in Python is not a theory-only topic. You should be able to explain the meaning, write the example, run it successfully, and use it in a small practical program.
Python Loops क्या है?
Python Loops ka matlab hai: Loops repeat a block of code. Python mainly uses for loops and while loops. Simple words me, ye topic practical Python programs likhne me direct use hota hai.
Is topic ko sirf definition ke liye nahi, balki printing tables and patterns jaise real examples ke liye practice karein.
यह क्यों सीखना जरूरी है?
- Ye printing tables and patterns me kaam aata hai.
- Ye processing multiple records se bhi connected hai.
- Isse aap code ka output aur errors better samajh paate hain.
Important Terms
| Term | Meaning |
|---|---|
| for loop | Loop used to repeat over a sequence or range. |
| while loop | Loop that continues while a condition remains true. |
| range() | Function used to generate a sequence of numbers for loops. |
| break | Statement used to stop a loop immediately. |
| continue | Statement used to skip the current loop step and move to the next one. |
Syntax / Basic Pattern
Basic idea: pehle data तैयार करें, phir Python logic apply करें, aur finally result display करें.
for number in range(1, 6):
print(number, end=" ")
print()
count = 3
while count > 0:
print("Count:", count)
count -= 1Complete Example Program
# for loop
for number in range(1, 6):
print(number, end=" ")
print()
# while loop
count = 3
while count > 0:
print("Count:", count)
count -= 1Expected Output
Program Explanation
for number in range(1, 6):repeats the following indented statements.print(number, end=" ")displays information or calculated result on the screen.print()displays information or calculated result on the screen.count = 3stores a value in count.while count > 0:repeats the following indented statements.print("Count:", count)displays information or calculated result on the screen.count -= 1stores a value in count -.
Practical Uses
- Printing tables and patterns.
- Processing multiple records.
- Repeating a task until condition is met.
Common Mistakes
- Writing code with wrong indentation.
- Using input() value directly in calculations without converting it to int or float.
- Using unclear variable names that make the program difficult to understand.
Practice Tasks
- Program ko
loops.pyfile me type karke run karein. - Values change karke output compare karein.
- printing tables and patterns par ek छोटा example banayen.
- Logic ko apne words me 5 lines me likhein.
सारांश
Loops in Python ko tab complete maanenge jab aap iska meaning, example, output aur practical use clearly explain kar saken.