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

Loops in Python

Uploaded by

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

Loops in Python

Uploaded by

padhako.harsh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Loops in Python

1. Introduction
● In programming, sometimes we need to repeat a set of instructions multiple times.

● Instead of writing the same code again and again, we use loops.

● Python provides two main types of loops:

1. for loop

2. while loop

● We can also use nested loops and control statements (break, continue, pass).

👉 Ask students: “Can you think of a daily life example where we repeat tasks again and again?”
(e.g., brushing teeth daily, checking each student’s homework, going through pages of a book)

2. The for loop


● The for loop is used when we know how many times we want to repeat the task.

● It is often used to iterate over a sequence like a list, tuple, string, or a range of
numbers.

Syntax:
for variable in sequence:
# body of loop

Example 1: Using range()


for i in range(5):
print("Hello Python")

👉 Output: Prints "Hello Python" 5 times.


Example 2: Iterating through a list
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)

3. The while loop


● The while loop is used when we do not know in advance how many times we need to
repeat.

● It keeps running as long as the condition is true.

Syntax:
while condition:
# body of loop

Example 1:
count = 1
while count <= 5:
print("Count is:", count)
count += 1

👉 Output: Prints numbers from 1 to 5.

⚠️Important: Always update the variable inside a while loop. Otherwise, you may create an
infinite loop.

4. Control Statements in Loops


Python gives us keywords to control the flow inside loops:

break → Exit the loop immediately.

for i in range(10):
if i == 5:
break
print(i)
1.

continue → Skip the current iteration and go to the next.

for i in range(5):
if i == 2:
continue
print(i)

2.

pass → A placeholder; does nothing but is syntactically required.

for i in range(3):
pass # Later code can be added here

3.

5. Nested Loops
● A loop inside another loop.

● Example: Printing a pattern.

for i in range(1, 4): # Outer loop


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

👉 Output:

11
12
13
21
22
23
31
32
33
6. Real-life Applications of Loops
● Printing multiplication tables.

● Searching in a list.

● Summing numbers from 1 to n.

● Creating patterns (triangles, stars, grids).

● Reading data line by line from files.

Example: Multiplication Table


num = 5
for i in range(1, 11):
print(num, "x", i, "=", num*i)

7. Difference Between for and while


Feature for loop while loop

Use case When number of iterations When iterations unknown


known

Example Traversing a list, range Reading until condition


usage met

Risk Rarely infinite Can easily become infinite

8. Quick Recap
● Loops reduce repetition in code.

● for loop → iterate over a sequence.

● while loop → repeat until a condition is false.

● Control with break, continue, pass.

● Loops can be nested.


9. Short Classroom Activity
👉 Ask students:

1. Print numbers from 10 to 1 using a loop.

2. Write a loop to calculate the sum of first 20 natural numbers.

3. Print this pattern using nested loops:

*
**
***
****

✨ End with:
“Loops are the backbone of programming. Once you master them, you can solve
many problems easily.”

You might also like