Python For loop

Theory

A for loop lets you iterate over a sequence of values. In Python, the sequence can be a list, tuple, string, range, dictionary, or any iterable.

Basic idea

In a for loop you specify a loop variable and an iterable to step through:

for item in iterable:
    # use item inside the loop

Example 1 — Tuple of colours

Here the loop variable color takes each value from a tuple:

for color in ('red', 'orange', 'yellow', 'green', 'cyan', 'blue', 'violet'):
    print(color)

Example 2 — Numbering items with enumerate

Instead of manually updating a counter, use enumerate to get the index (starting at 1) and the value:

colors = ('red', 'orange', 'yellow', 'green', 'cyan', 'blue', 'violet')

for i, color in enumerate(colors, start=1):
    print(f"{i}-th color of the rainbow is {color}")

Example 3 — Different types in one iterable

The items in a sequence can be of mixed types:

for item in (1, 2, 3, 'one', 'two', 'three'):
    print(item)

Example 4 — Looping over a range

range(start, stop, step) generates a sequence of integers:

for n in range(1, 6):  # 1, 2, 3, 4, 5
    print(n)

Example 5 — Strings are iterable

Iterating over a string yields its characters:

for ch in "Python":
    print(ch)

Example 6 — Dictionaries

Use .items() to get key–value pairs:

capitals = {"UK": "London", "France": "Paris", "Japan": "Tokyo"}

for country, city in capitals.items():
    print(f"{city} is the capital of {country}")

Example 7 — break, continue, and for…else

break exits the loop early; continue skips to the next iteration. The optional else block runs only if the loop wasn’t terminated by break.

numbers = [2, 4, 6, 9, 12]

for x in numbers:
    if x % 2 != 0:
        print("Found an odd number:", x)
        break
else:
    print("All numbers were even")  # runs only if no break happened

Example 8 — List comprehension (compact looping)

A concise way to build lists from a loop:

squares_of_evens = [n*n for n in range(1, 11) if n % 2 == 0]
print(squares_of_evens)  # [4, 16, 36, 64, 100]

Key takeaways: use for to iterate over any iterable; prefer enumerate for counters; use range for integer sequences; and remember control tools like break, continue, and for…else.

Problems

No items found.

FAQ

Find quick answers to common questions about our lessons, pricing, scheduling, and how Exact Science can help your child excel.
Where do you hold your classes?
We hold our classes online or on-site on Saturdays at our branch in Pimlico Academy, London.
You can find our timetable here.
What do you need to start learning online?
For lessons you only need a computer or phone with a microphone, camera and Internet access. Wherever you are - in London, Nottingham, New York or Bali - online lessons will be at hand.
When can I take the trial lesson?
You can get acquainted with the school at any time convenient for you. To do this, just leave a request and sign up for a lesson.
What should I expect from the trial lesson?
The trial lesson is a 30-minute online session designed to get a sense of how your child approaches mathematical thinking and problem solving. (In practice, it often runs a bit longer if the student is engaged!)

We typically explore a range of fun and challenging problems drawn from competitions. We adapt the difficulty based on how the student responds, aiming to make it both accessible and stimulating.

After the session, we’ll have a quick conversation with the parent to share observations and suggest a personalised path forward.
I can't attend class, what should I do?
It is OK, it happens! Students have the opportunity to cancel a lesson up to 8 hours before the scheduled time without loss of payment. So you can reschedule it for a convenient time, and the teacher will have the opportunity to
I don't have much free time, will I have time to study?
Learning can take place at your own pace. We will select a convenient schedule and at any time we will help you change the schedule, take a break or adjust the program.
How long is one lesson?
All classes last 1 hour.

Meet our team

Our teachers will tell you how to prepare for exams, help you cope with difficult tasks and win the Olympiad

They will tell you about the pitfalls of exams and the most common mistakes, and explain how to avoid them
George Ionitsa
Founder &
Maths and Coding Coach

What our students and parents say about learning with us

"Olympiad Maths Lessons helped me a lot to get the Gold medal in Junior Maths Challenge"
St. Paul's Student
"Thanks to the 'Data Science' and 'Coding in Python' lessons I got accepted to my dream university."
Michael
Data Science Student
Warwick University
"Great courses, which thoroughly explained topics beyond the capability of the GCSE answer sheet. Thanks so much."
Ivan
GCSE Student in Dubai
"Financial Mathematics! Best course to understand Python and Mathematics behind Finance!"
Gleb
VC Investor
"We got silver in PMC! Thanks George!"
Mum of St. Paul's Student
Prepare for the Primary Maths Challenge
"My daughter took a batch of 10 classes with George to understand Python with Turtle. I found George extremely knowledgeable and accessible."
Dad of Latymer School Student
Python with Turtle