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

Loops Python

This handout explains loops in Python, detailing their purpose, types (for and while), and syntax. It covers loop control statements like break and continue, as well as nested loops and provides practice exercises. The document emphasizes the efficiency of loops in reducing code repetition and improving programming logic.

Uploaded by

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

Loops Python

This handout explains loops in Python, detailing their purpose, types (for and while), and syntax. It covers loop control statements like break and continue, as well as nested loops and provides practice exercises. The document emphasizes the efficiency of loops in reducing code repetition and improving programming logic.

Uploaded by

Tamseel Akhund
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

🧠 Step-by-Step Handout: Loops in Python

📌 1. What is a Loop?
A loop is used to repeat a block of code multiple times.
👉 Instead of writing the same code again and again, we use a loop.
❌ Without Loop Example
print("Welcome")
print("Welcome")
print("Welcome")
print("Welcome")
✅ With Loop Example
for i in range(4):
print("Welcome")
✔ Same output
✔ Less code
✔ More efficient
🎯 2. Why Do We Use Loops?
We use loops when:
 🔁 We want to repeat a task multiple times
 📊 We want to work with lists, numbers, or data
 🧮 We want to perform calculations repeatedly
 📥 We want to take input multiple times

🔢 3. Types of Loops in Python


In Python, there are two main types of loops:

1. for loop
2. while loop

🔵 4. The for Loop


📘 Definition:
A for loop is used when we know how many times we want to repeat something.

🔹 Syntax:
for variable in sequence:
# code block

🔹 Example 1: Print Numbers 1 to 5


for i in range(1, 6):
print(i)

🔎 Explanation:

 range(1, 6) means start from 1 and stop before 6


 Loop runs 5 times
 i stores the current number

✔ Output:

1
2
3
4
5
🔹 Example 2: Loop Through a List
fruits = ["Apple", "Banana", "Mango"]

for fruit in fruits:


print(fruit)
✔ Output:
Apple
Banana
Mango
👉 The loop runs once for each item in the list.

🔹 Example 3: Table of 2
for i in range(1, 11):
print("2 x", i, "=", 2*i)

🟢 5. The while Loop


📘 Definition:

A while loop runs as long as a condition is TRUE.

🔹 Syntax:
while condition:
# code block

🔹 Example 1: Print Numbers 1 to 5


i = 1

while i <= 5:
print(i)
i = i + 1

🔎 Explanation:

 Start from 1
 Continue while i <= 5
 Increase i each time
 Stops when condition becomes FALSE
⚠ Important:
If you forget to increase i, the loop will run forever!
This is called an infinite loop ❌

🔹 Example 2: Simple Password Check


password = ""

while password != "python123":


password = input("Enter Password: ")

print("Access Granted!")

👉 Loop continues until correct password is entered.

🔁 6. Difference Between for and while


for loop while loop
Used when number of repetitions is known Used when repetitions depend on condition
Works with sequences (list, range, string) Works with condition
Easier for counting More flexible

🛑 7. Loop Control Statements


These help control loop behavior.

🔹 1. break
Stops the loop immediately.
for i in range(1, 6):
if i == 3:
break
print(i)
✔ Output:
1
2

🔹 2. continue
Skips current iteration.
for i in range(1, 6):
if i == 3:
continue
print(i)
✔ Output:
1
2
4
5

🟢 8. Nested Loops (Loop inside Loop)


for i in range(1, 4):
for j in range(1, 4):
print(i, j)
✔ Output:

1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3

👉 Commonly used in patterns and matrices.

📝 9. Practice Exercises for Students


1. Print numbers from 10 to 1 using loop.
2. Print even numbers from 1 to 20.
3. Print multiplication table of any number.
4. Take 5 numbers from user and print their sum.
5. Create a simple number guessing game.

🎓 10. Summary
 A loop repeats code.
 Python has 2 main loops:
o for
o while
 break stops loop.
 continue skips one round.
 Loops save time and reduce code repetition.

💡 Final Advice for Beginners


✔ Always update your variable in while loop
✔ Be careful of infinite loops
✔ Practice small examples daily
✔ First understand logic, then write code

You might also like