🟢 Beginner  ·  Lesson 09

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.

💡 At a Glance
PointDetails
Course AreaCore Python
Basic programming concepts used to write Python programs.
Main Useprinting tables and patterns
Example Fileloops.py
Practice FocusRun, 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.

TermMeaning
for loopLoop used to repeat over a sequence or range.
while loopLoop that continues while a condition remains true.
range()Function used to generate a sequence of numbers for loops.
breakStatement used to stop a loop immediately.
continueStatement 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.

Basic Pattern
for number in range(1, 6):
    print(number, end=" ")
print()
count = 3
while count > 0:
    print("Count:", count)
    count -= 1

Complete Example Program

Python – loops.py
# for loop
for number in range(1, 6):
    print(number, end=" ")

print()

# while loop
count = 3
while count > 0:
    print("Count:", count)
    count -= 1

Expected Output

1 2 3 4 5 Count: 3 Count: 2 Count: 1

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 = 3 stores a value in count.
  • while count > 0: repeats the following indented statements.
  • print("Count:", count) displays information or calculated result on the screen.
  • count -= 1 stores 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

  1. Type the program in loops.py and run it.
  2. Change input values or sample data and observe the new output.
  3. Create one example related to printing tables and patterns.
  4. 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

TermMeaning
for loopLoop used to repeat over a sequence or range.
while loopLoop that continues while a condition remains true.
range()Function used to generate a sequence of numbers for loops.
breakStatement used to stop a loop immediately.
continueStatement 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 करें.

Basic Pattern
for number in range(1, 6):
    print(number, end=" ")
print()
count = 3
while count > 0:
    print("Count:", count)
    count -= 1

Complete Example Program

Python – loops.py
# for loop
for number in range(1, 6):
    print(number, end=" ")

print()

# while loop
count = 3
while count > 0:
    print("Count:", count)
    count -= 1

Expected Output

1 2 3 4 5 Count: 3 Count: 2 Count: 1

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 = 3 stores a value in count.
  • while count > 0: repeats the following indented statements.
  • print("Count:", count) displays information or calculated result on the screen.
  • count -= 1 stores 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

  1. Program ko loops.py file me type karke run karein.
  2. Values change karke output compare karein.
  3. printing tables and patterns par ek छोटा example banayen.
  4. 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.

← Back to Python Tutorial