SAMANTUS Python for Data Analytics — Complete Training Manual MODULE 06
← Back to Course Index

Loops


On This Page

Module 6: Loops


6.1  while Loop

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.

► Basic while Loop
count = 1
while count <= 5:
    print("Batch", count)
    count += 1   # IMPORTANT: update the counter
Output
Batch 1
Batch 2
Batch 3
Batch 4
Batch 5
✗ Common Mistake
Forgetting to update the counter variable (like count += 1) creates an infinite loop that never stops. Always double-check that your loop's condition will eventually become False.

6.2  for Loop

A for loop iterates over a sequence (list, string, range, etc.) — it is the most commonly used loop in Python, especially for data analytics tasks.

► Basic for Loop
courses = ["SEO", "Google Ads", "Meta Ads"]
for course in courses:
    print("Course:", course)
Output
Course: SEO
Course: Google Ads
Course: Meta Ads

6.3  The range() Function

The range() function generates a sequence of numbers, most often used to control how many times a for loop repeats.

SyntaxMeaningExample Output
range(5)0 to 4 (5 numbers)0,1,2,3,4
range(2, 6)2 to 52,3,4,5
range(0, 10, 2)0 to 9, step 20,2,4,6,8
range(10, 0, -1)10 down to 110,9,8,...1
► Using range() in a for Loop
for i in range(1, 6):
    print("Student", i)
Output
Student 1
Student 2
Student 3
Student 4
Student 5

6.4  Nested Loops

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.

► Nested Loop Example
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
Output
(1,1) (1,2) (1,3)
(2,1) (2,2) (2,3)
(3,1) (3,2) (3,3)

6.5  break, continue, pass

KeywordEffect
breakImmediately exits the loop completely
continueSkips the current iteration and moves to the next one
passDoes nothing — a placeholder for future code
► break and continue in Action
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)
Output
1
3
5
✓ Instructor Tip
Use a simple analogy: break is like leaving the classroom entirely, continue is like skipping one exercise and moving to the next, and pass is like a placeholder sticky note for later.

6.6  Loop Flowchart & Logical Thinking

Visualizing a loop as a flowchart helps beginners understand exactly when a loop repeats and when it stops.

... ... Start Loop Condition still True? Run loop body repeat True False Exit Loop
Figure — Loop execution flow.

Figure 6.1 — Loop execution flow: the condition is checked before every repetition; the loop exits the moment the condition becomes False.

6.7  Pattern Programs

Pattern programs are a classic way to build strong nested-loop logic. Here are two common examples:

► Pattern 1: Right-Angled Triangle
rows = 5
for i in range(1, rows+1):
    print("*" * i)
Output
*
**
***
****
*****
► Pattern 2: Number Pyramid
rows = 4
for i in range(1, rows+1):
    for j in range(1, i+1):
        print(j, end=" ")
    print()
Output
1
1 2
1 2 3
1 2 3 4

6.8  Practical & Logic Building Exercises

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.

6.9  Module Quiz (MCQs)

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?

ℹ Answer Key
1-b, 2-a, 3-b, 4-b, 5-b

6.10  Module Assignment

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'.

6.11  Interview Questions — Module 6


6.12  Student Notes Page

Use this space to write down key points, doubts, and your own examples from today's session.