#1 Data Analytics Program in India
₹2,499₹1,499Enroll Now
5 min read

Control Flow - For Loops

Learn to repeat code easily with for loops

What You'll Learn

  • What loops are
  • Using for loops
  • Looping through lists
  • Using range()

What is a Loop?

A loop repeats code multiple times. Instead of writing same code again and again, write it once in a loop!

Without loop:

code.py
print("Hello")
print("Hello")
print("Hello")

With loop:

code.py
for i in range(3):
    print("Hello")

Both do the same thing!

Basic for Loop

code.py
for i in range(5):
    print(i)

Output:

0 1 2 3 4

Note: Starts at 0, not 1!

Using range()

range() creates a sequence of numbers:

range(stop):

code.py
for i in range(3):
    print(i)
# Shows: 0, 1, 2

range(start, stop):

code.py
for i in range(1, 4):
    print(i)
# Shows: 1, 2, 3

range(start, stop, step):

code.py
for i in range(0, 10, 2):
    print(i)
# Shows: 0, 2, 4, 6, 8

Looping Through Lists

Visit each item in a list:

code.py
fruits = ["apple", "banana", "orange"]

for fruit in fruits:
    print(fruit)

Output:

apple banana orange

Practice Examples

Example 1: Count to 10

code.py
for i in range(1, 11):
    print(i)
# Shows: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Example 2: Times table

code.py
number = 5

for i in range(1, 11):
    result = number * i
    print(f"{number} x {i} = {result}")

Output:

5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 ...

Example 3: Sum numbers

code.py
total = 0

for i in range(1, 6):
    total += i
    print(f"Adding {i}, total is now {total}")

print(f"Final total: {total}")

Example 4: Shopping list

code.py
items = ["milk", "eggs", "bread"]

print("Shopping List:")
for item in items:
    print(f"- {item}")

Looping Through Strings

Each letter is an item:

code.py
word = "Python"

for letter in word:
    print(letter)

Output:

P y t h o n

Using Index with enumerate()

Get both position and value:

code.py
fruits = ["apple", "banana", "orange"]

for index, fruit in enumerate(fruits):
    print(f"{index}: {fruit}")

Output:

0: apple 1: banana 2: orange

Start from 1:

code.py
for index, fruit in enumerate(fruits, 1):
    print(f"{index}: {fruit}")

Output:

1: apple 2: banana 3: orange

Nested Loops

Loop inside a loop:

code.py
for i in range(1, 4):
    for j in range(1, 4):
        print(f"{i} x {j} = {i * j}")
    print("---")

break - Stop Loop Early

code.py
for i in range(10):
    if i == 5:
        break  # Stop here
    print(i)
# Shows: 0, 1, 2, 3, 4

Example: Find number

code.py
numbers = [3, 7, 2, 9, 5]

for num in numbers:
    if num == 9:
        print("Found 9!")
        break

continue - Skip to Next

code.py
for i in range(5):
    if i == 2:
        continue  # Skip 2
    print(i)
# Shows: 0, 1, 3, 4

Example: Print odd numbers

code.py
for i in range(10):
    if i % 2 == 0:
        continue  # Skip even numbers
    print(i)
# Shows: 1, 3, 5, 7, 9

Real-World Examples

Example 1: Calculate average

code.py
scores = [85, 90, 78, 92, 88]
total = 0

for score in scores:
    total += score

average = total / len(scores)
print(f"Average: {average}")

Example 2: Find max number

code.py
numbers = [45, 23, 67, 12, 89, 34]
max_num = numbers[0]

for num in numbers:
    if num > max_num:
        max_num = num

print(f"Maximum: {max_num}")

Example 3: Create username list

code.py
names = ["John", "Sarah", "Mike"]

for name in names:
    username = name.lower() + "123"
    print(f"Username: {username}")

Common Mistakes

Mistake 1: Forgetting colon

code.py
# Wrong
for i in range(5)
    print(i)

# Correct
for i in range(5):
    print(i)

Mistake 2: Wrong indentation

code.py
# Wrong
for i in range(5):
print(i)

# Correct
for i in range(5):
    print(i)

Mistake 3: Modifying list while looping

code.py
# Risky
numbers = [1, 2, 3]
for num in numbers:
    numbers.remove(num)  # Don't do this!

Quick Reference

Basic loop:

code.py
for i in range(5):
    print(i)

Loop through list:

code.py
for item in items:
    print(item)

With index:

code.py
for i, item in enumerate(items):
    print(i, item)

Stop early:

code.py
break

Skip one:

code.py
continue

Summary

  • for loops repeat code
  • range() creates number sequences
  • Loop through lists, strings, anything
  • Use break to stop early
  • Use continue to skip one
  • Always use colon and indentation

What's Next?

Now let's learn about while loops for different kinds of repetition!

SkillsetMaster - AI, Web Development & Data Analytics Courses