PYTHON BASICS • LESSON 13

for Loops in Python

Learn how to repeat code efficiently using Python for loops. You will work with sequences, range(), lists, strings, output prediction, debugging, and data analytics examples.

1. Why Do We Need Loops?

Imagine you want to print the numbers 1 to 5. Without a loop, you would have to write the same instruction repeatedly.

print(1)
print(2)
print(3)
print(4)
print(5)

A loop lets Python repeat an instruction automatically.

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

CHECK YOUR UNDERSTANDING

What is the main purpose of a loop?

2. Basic for Loop Syntax

for item in sequence:
    # code to repeat

The loop takes one item at a time from the sequence and executes the indented code.

The colon : and indentation are important.

Fill in the Blank

Complete the loop syntax: for item in items ___

3. Looping Through a List

A for loop can process every item in a list.

cities = ["Delhi", "Dehradun", "Mumbai"]

for city in cities:
    print(city)

Python takes each city from the list and stores it temporarily in the variable city.

CHECK YOUR UNDERSTANDING

How many times will the print statement execute?

4. Predict the Output

for fruit in ["Apple", "Mango", "Banana"]:
    print(fruit)

CHECK YOUR UNDERSTANDING

What is printed first?

5. Using range()

range() is commonly used when you want a sequence of numbers.

for number in range(5):
    print(number)

This prints:

0
1
2
3
4

Remember: range(5) starts at 0 and stops before 5.

CHECK YOUR UNDERSTANDING

What numbers are produced by range(4)?

6. range(start, stop)

You can specify both the starting and stopping values.

for number in range(2, 6):
    print(number)

Output:

2
3
4
5

CHECK YOUR UNDERSTANDING

What does range(3, 7) produce?

7. range(start, stop, step)

A third value can control the step size.

for number in range(0, 10, 2):
    print(number)

Output:

0
2
4
6
8

CHECK YOUR UNDERSTANDING

What is the step size in range(0, 10, 2)?

8. Looping Through a String

Strings are sequences too, so a loop can process them one character at a time.

word = "DATA"

for letter in word:
    print(letter)

CHECK YOUR UNDERSTANDING

How many times does the loop run for the string 'DATA'?

9. Using a Loop to Calculate a Total

Loops are useful for processing numerical data.

sales = [10000, 15000, 12000]

total = 0

for amount in sales:
    total = total + amount

print(total)

The loop adds every sales value to total.

CHECK YOUR UNDERSTANDING

What is the final total?

10. for Loops in Data Analytics

In analytics, loops can be used to process records, calculate totals, inspect values, and apply business rules.

sales = [120000, 85000, 150000, 90000]

for sale in sales:
    if sale >= 100000:
        print("Target Achieved")
    else:
        print("Below Target")

CHECK YOUR UNDERSTANDING

How many times will 'Target Achieved' be printed?

11. Complete the Code

Fill in the Blank

Complete: ___ number in range(5):

Fill in the Blank

Complete: for number in ___(10):

Fill in the Blank

Complete: for item in items___

12. Debugging a for Loop

Look carefully at this code:

numbers = [1, 2, 3]

for number in numbers
    print(number)

What is missing?

CHECK YOUR UNDERSTANDING

What is missing after 'for number in numbers'?

13. Predict the Output

total = 0

for number in range(1, 4):
    total = total + number

print(total)

CHECK YOUR UNDERSTANDING

What is the final value of total?

14. Mini Challenge — Analyze Sales

Consider this sales dataset:

sales = [45000, 120000, 75000, 150000, 90000]

You want to identify every sale that is at least ₹100,000.

for sale in sales:
    if sale >= 100000:
        print(sale)

CHECK YOUR UNDERSTANDING

How many sales will be printed?

15. Final Challenge — Count High-Value Sales

A company wants to count how many transactions are worth at least ₹100,000.

sales = [85000, 125000, 140000, 45000, 210000]

count = 0

for sale in sales:
    if sale >= 100000:
        count = count + 1

print(count)

CHECK YOUR UNDERSTANDING

What will the program print?

Lesson 13 Recap

  • ✓ A for loop repeats code for each item in a sequence.
  • ✓ Lists and strings can be processed using for.
  • ✓ range() generates sequences of numbers.
  • ✓ range(start, stop) excludes the stop value.
  • ✓ A third argument controls the step size.
  • ✓ Loops are useful for processing datasets.
  • ✓ Loops can be combined with if conditions.
  • ✓ Correct indentation and the colon are essential.

Next: Lesson 14 — while Loops