Python Course
Loops in Python
In real programming, we often need to repeat tasks multiple times. For example:
- Printing student names
- Processing thousands of records
- Reading files line by line
- Running calculations repeatedly
Writing the same code again and again is inefficient. Python solves this problem using loops.
A loop allows a block of code to execute repeatedly until a condition becomes false or data ends.
Why Loops Are Important
Imagine printing numbers from 1 to 5 without loops:
print(1)
print(2)
print(3)
print(4)
print(5)
This works — but what if you need 10,000 numbers? Loops automate repetition safely and efficiently.
Types of Loops in Python
Python mainly provides two looping mechanisms:
- for loop → repeat over a sequence
- while loop → repeat while a condition is true
The for Loop
A for loop is used when we know how many times we want to repeat something or when iterating through data collections.
Basic syntax:
# for loop syntax
for variable in sequence:
# code block
Loop Using range()
The most common way to use a for loop is with
the built-in range() function.
range() generates numbers automatically.
# Printing numbers from 1 to 5
for i in range(1, 6):
print(i) # prints each value of i
1 2 3 4 5
range(1,6)starts from 1 and stops before 6.- The loop runs once for each generated number.
istores the current value during each iteration.
Understanding range() Clearly
| Expression | Meaning | Output |
|---|---|---|
| range(5) | Starts at 0 | 0 1 2 3 4 |
| range(1,5) | Start & stop | 1 2 3 4 |
| range(1,10,2) | Step size = 2 | 1 3 5 7 9 |
Looping Through a List
Loops are commonly used to process collections like lists.
# Iterating through a list
courses = ["Python", "SQL", "AI"]
for course in courses:
print("Learning:", course)
Learning: Python Learning: SQL Learning: AI
- The loop automatically accesses each item.
- No indexing is required.
- This pattern is heavily used in real applications.
The while Loop
A while loop runs as long as a condition remains true.
Use while loops when repetition depends on logic, not fixed counts.
# while loop example
count = 1
while count <= 5:
print(count)
count += 1 # updating condition
1 2 3 4 5
- The loop continues while condition is True.
- Updating the variable prevents infinite looping.
Infinite Loops (Important Concept)
If the condition never becomes False, the loop runs forever.
# Infinite loop example (avoid)
while True:
print("Running forever")
Always ensure loop conditions eventually stop.
break Statement
The break statement stops a loop immediately.
# Using break
for num in range(1,10):
if num == 5:
break # exit loop
print(num)
1 2 3 4
- Loop terminates once condition matches.
continue Statement
The continue statement skips
the current iteration only.
# Using continue
for num in range(1,6):
if num == 3:
continue # skip 3
print(num)
1 2 4 5
Nested Loops
A loop inside another loop is called a nested loop. Used in matrices, tables, and pattern generation.
# Nested loop example
for i in range(1,4):
for j in range(1,4):
print(i, j)
1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3
Real-World Example: Order Processing System
Loops commonly process repeated business data.
# Processing customer orders
orders = [250, 450, 300, 150]
total_sales = 0
for order in orders:
total_sales += order # accumulate total
print("Total Sales:", total_sales)
Total Sales: 1150
- Each order is processed automatically.
- Loops eliminate repetitive calculations.
Practice
Which loop is used to iterate through sequences?
Which loop runs based on a condition?
Which keyword immediately stops a loop?
Which keyword skips only one iteration?
Quick Quiz
Does range(5) include number 5?
Which statement exits loop completely?
A loop inside another loop is called?
Loop Concepts Summary
| Concept | Purpose |
|---|---|
| for loop | Iterate over sequences |
| while loop | Repeat based on condition |
| range() | Generate numbers |
| break | Stop loop instantly |
| continue | Skip iteration |
| Nested loop | Loop inside loop |
Recap: Loops automate repetition, process data efficiently, and form the backbone of automation and large-scale programs.
Next up: Strings in Python.