Skip to main content
  • Python is available now. All other compilers are coming soon.

    Python
  • Popular Tutorials

    • Getting Started With Python
    • Python if Statement
    • while Loop in Python
    • Python Lists
    • Dictionaries in Python
    All Python Tutorials

    Reference Materials

    Built-in FunctionsList MethodsDictionary MethodsString Methods
    View all

Online Compilers

  • Python Compiler
  • Python Playground
  • Python Projects
  • Exercises

Tutorials

  • Learn Python
  • Python for Beginners
  • Variables and Types
  • Python Strings
  • Python Lists
  • Python If Else
  • Python For Loops
  • Python Functions
  • Python Classes

Examples

  • Python Examples
  • Hello World
  • Add Two Numbers
  • Even or Odd
  • Factorial
  • Fibonacci
  • Prime Check
  • Palindrome Check

Company

  • About
  • Contact
  • Privacy Policy
  • Terms of Use
  • Disclaimer

© 2026 ILoveCompiler. All rights reserved.

  1. Home
  2. Python Tutorial
  3. Python for Loops

Control flow · Tutorial 11

Python for Loops

Iterate over strings, lists, ranges, and other iterables with Python's for loop.

Open the compilerPractice exercises
beginner8 min readUpdated November 19, 20263 learning goals
On this page
  1. Iterating over a string
  2. Counted repetition
  3. Accumulators
  4. Building a new list
  5. Nested loops
  6. Dictionaries
  7. Practice

What you will learn

  • Iterate over sequences with for
  • Use range() for counted repetition
  • Build totals and lists inside loops
Before this lesson: Python while Loops

A for loop visits each item in an iterable, one at a time. Strings, lists, tuples, dictionaries, sets, and ranges are all iterable.

Python
names = ["Ada", "Grace", "Linus"]

for name in names:
    print(f"Hello, {name}!")
Expected output

Hello, Ada! Hello, Grace! Hello, Linus!

The loop variable (name) is assigned to the next item on each iteration. It remains available after the loop, but relying on that is usually a mistake.

Iterating over a string#

Python
word = "Python"

for letter in word:
    print(letter)

Each character is a one-character string.

Counted repetition#

Python
for number in range(5):
    print(number)
Expected output

0 1 2 3 4

range(5) produces 0 through 4. See the next tutorial for start, stop, and step variations.

Accumulators#

Python
numbers = [12, 7, 19, 4]
total = 0

for number in numbers:
    total += number

average = total / len(numbers)
print(f"Average: {average:.2f}")

Initialise the accumulator before the loop. Inside, update it with each item.

Building a new list#

Python
numbers = [1, 2, 3, 4, 5, 6]
evens = []

for number in numbers:
    if number % 2 == 0:
        evens.append(number)

print(evens)

A list comprehension expresses the same idea more compactly:

Python
evens = [number for number in numbers if number % 2 == 0]

Use a comprehension when it remains easy to read. Use a loop when the body needs several statements or logging.

Nested loops#

Python
for row in range(1, 4):
    for column in range(1, 4):
        print(row * column, end=" ")
    print()
Expected output

1 2 3 2 4 6 3 6 9

The inner loop completes all of its iterations for each single iteration of the outer loop.

Choose descriptive loop variables

for item in items is fine, but for student in students or for row in rows documents the data. Avoid reusing a name that already refers to a list.

Dictionaries#

Iterating over a dictionary yields its keys by default:

Python
scores = {"Ada": 90, "Grace": 95}

for name in scores:
    print(name, scores[name])

Use .items() when you need both keys and values:

Python
for name, score in scores.items():
    print(f"{name}: {score}")

Practice#

Given a list of temperatures, count how many are above 25 degrees and compute the average. Then rewrite the solution with a list comprehension for the warm days.

Previous tutorialPython while LoopsNext tutorialPython range() and enumerate()

Related tutorials

beginner

Python range() and enumerate()

Generate number sequences with range and track positions with enumerate while iterating.

7 min read
beginner

Python break, continue, and Loop else

Control loop execution with break and continue, and learn how the rarely used loop else clause works.

6 min read

On this page

  1. Iterating over a string
  2. Counted repetition
  3. Accumulators
  4. Building a new list
  5. Nested loops
  6. Dictionaries
  7. Practice

Run the examples

Every Python block has a Run in compiler button that opens the code in a new tab.