A while loop repeats a block of code as long as a given condition remains True. It is best used when you don't know in advance how many times you need to repeat.
count = 1
while count <= 5:
print("Batch", count)
count += 1 # IMPORTANT: update the counterA for loop iterates over a sequence (list, string, range, etc.) — it is the most commonly used loop in Python, especially for data analytics tasks.
courses = ["SEO", "Google Ads", "Meta Ads"]
for course in courses:
print("Course:", course)The range() function generates a sequence of numbers, most often used to control how many times a for loop repeats.
| Syntax | Meaning | Example Output |
|---|---|---|
| range(5) | 0 to 4 (5 numbers) | 0,1,2,3,4 |
| range(2, 6) | 2 to 5 | 2,3,4,5 |
| range(0, 10, 2) | 0 to 9, step 2 | 0,2,4,6,8 |
| range(10, 0, -1) | 10 down to 1 | 10,9,8,...1 |
for i in range(1, 6):
print("Student", i)A nested loop is a loop inside another loop — the inner loop completes all its iterations for every single iteration of the outer loop. Commonly used for grids, tables, and patterns.
for i in range(1, 4): # outer loop
for j in range(1, 4): # inner loop
print(f"({i},{j})", end=" ")
print() # new line after each outer iteration| Keyword | Effect |
|---|---|
| break | Immediately exits the loop completely |
| continue | Skips the current iteration and moves to the next one |
| pass | Does nothing — a placeholder for future code |
for i in range(1, 11):
if i == 6:
break # stop the loop completely at 6
if i % 2 == 0:
continue # skip even numbers, go to next iteration
print(i)Visualizing a loop as a flowchart helps beginners understand exactly when a loop repeats and when it stops.
Figure 6.1 — Loop execution flow: the condition is checked before every repetition; the loop exits the moment the condition becomes False.
Pattern programs are a classic way to build strong nested-loop logic. Here are two common examples:
rows = 5
for i in range(1, rows+1):
print("*" * i)rows = 4
for i in range(1, rows+1):
for j in range(1, i+1):
print(j, end=" ")
print()Basic (5 Questions)
1. Print numbers 1 to 10 using a for loop.
2. Print numbers 10 down to 1 using a while loop.
3. Print all even numbers between 1 and 20 using range() with a step.
4. Print the multiplication table of 7 using a for loop.
5. Use a while loop to print 'Learning Python' 5 times.
Intermediate (5 Questions)
1. Write a program to calculate the sum of all numbers from 1 to 100 using a loop.
2. Print a right-angled triangle pattern of stars for n rows (take n as input).
3. Use break to stop a loop as soon as it finds the first number divisible by 7 in a range.
4. Use continue to print only odd numbers between 1 and 30.
5. Write a nested loop to print a 5x5 grid of asterisks.
Advanced (5 Questions)
1. Write a program to check if a number is prime using a for loop and break.
2. Print a pyramid pattern of numbers as shown in this module, but reversed (largest row first).
3. Write a program to reverse a string using a while loop (without using slicing).
4. Build a simple 'Attendance Counter': loop through a list of student statuses and count Present vs Absent.
5. Write a program that prints the Fibonacci series up to n terms using a while loop.
Q1. Which loop is best when you don't know how many times to repeat?
Q2. What does range(2, 10, 2) generate?
Q3. What does 'break' do inside a loop?
Q4. What causes an infinite while loop?
Q5. What does 'pass' do in Python?
Assignment: Student Attendance & Fee Tracker
Expected Output: A numbered list of student names printed correctly, correctly skipping any 'X' names and stopping immediately upon reaching 'STOP'.
Use this space to write down key points, doubts, and your own examples from today's session.