Master Python For Loops: W3Schools Guide

Logan Jul 13, 2026

Embarking on your Python journey, you'll soon encounter one of its most fundamental elements: loops. Loops enable your program to repeat a block of code multiple times, governed by a condition. They streamline your code and make it more efficient, handling repetitive tasks with ease.

How to use while loops in Python
How to use while loops in Python

Before diving into Python's for loops, let's briefly touch upon a related concept, the range function. This function generates a sequence of numbers starting from 0 by default, increasing by 1 (you can customize these values), up to the specified stop value, excluding it.

Best Free Websites to Learn Python for Beginners
Best Free Websites to Learn Python for Beginners

Understanding For Loops in Python

In Python, the for loop is used to iterate over a sequence (like list, tuple, dictionary, set, or string) or other iterable objects. It's a control桁 structure that executes a block of code for each element in an iterable, making it ideal for situations where you need to perform the same action on each item of a collection.

Python Exit For Loop with Example Programs
Python Exit For Loop with Example Programs

The syntax of a for loop in Python is as follows: `for variable in iterable:`. Here, `variable` is the name of the variable that takes the value of each item in the iterable on each iteration, and `iterable` is the sequence or iterable object you're looping over.

For Loops with Sequence Types

an image of a computer screen with the text python written in green and white on it
an image of a computer screen with the text python written in green and white on it

The most common use case for for loops is to iterate over sequence types like lists and tuples. In each iteration, the for loop assigns the next item in the sequence to the loop variable and executes the indented block of code. Here's a simple example:

```html fruits = ['apple', 'banana', 'cherry'] for fruit in fruits: print(f"I love {fruit}") ```

This code will output:

``` I love apple I love banana I love cherry ```

For Loops with Other Iterables

10 Fun Python Exercises for Kids
10 Fun Python Exercises for Kids

Python's for loop can also work with other iterable types, including dictionaries, sets, and custom objects that implement the `__iter__` method. Let's explore each case:

  • Dictionaries: To iterate over a dictionary, you can use `for key in dictionary:` which will loop over the keys, or `for key, value in dictionary.items():` to loop over both keys and values.
  • Sets: For sets, `for item in set:` behaves similarly to lists, iterating over the items. However, remember that sets are unordered and do not support indexing.
  • Custom Objects: If an object implements the `__iter__` method, you can use it in a for loop. This method should return an iterable object (like an iterator or a generator) that the for loop can use to iterate over the object's elements.

Advanced For Loop Techniques

Python For Loop Explained 🐍 | Complete Beginner Cheat Sheet
Python For Loop Explained 🐍 | Complete Beginner Cheat Sheet

Python for loops are versatile and offer several powerful features that allow you to control the looping behavior and perform various operations on the loop variables. Let's explore some advanced for loop techniques.

Before we proceed, it's essential to note that understanding these techniques will require familiarity with list comprehensions, which are a concise way to create lists in Python. If you're not already acquainted, consider reading about them here: https://www.w3schools.com/python/python_for_loop.asp.

Top 5 Websites to Learn Python for Free
Top 5 Websites to Learn Python for Free
the instructions for how to use python loops
the instructions for how to use python loops
While loop in Python Easy to learn 🀩
While loop in Python Easy to learn 🀩
For Loop & While Loop Explained | Python for Beginners
For Loop & While Loop Explained | Python for Beginners
Python Learning Path for Beginners 2026 πŸš€
Python Learning Path for Beginners 2026 πŸš€
9 Python Loop Tricks Every Beginner Should Learn (Cheat Sheet)
9 Python Loop Tricks Every Beginner Should Learn (Cheat Sheet)
How to Create A Bouncing Ball in Python?
How to Create A Bouncing Ball in Python?
30 Python Projects For Beginners and Advanced Learners (2023)
30 Python Projects For Beginners and Advanced Learners (2023)
Python Cheat Sheet for Beginners 2026 | Python Basics, Syntax, Loops, Functions & Variables
Python Cheat Sheet for Beginners 2026 | Python Basics, Syntax, Loops, Functions & Variables
30 Python Projects For Beginners | Easy To Advanced Python Project Ideas
30 Python Projects For Beginners | Easy To Advanced Python Project Ideas
9 Python Projects for Beginners (Build Your Portfolio Fast)
9 Python Projects for Beginners (Build Your Portfolio Fast)
the best learning sites for python
the best learning sites for python
python Loops for loop and while loop
python Loops for loop and while loop
How to use the "for Loop" in Python
How to use the "for Loop" in Python
30-Day Python Learning Plan for Beginners | Complete Python Roadmap to Learn Coding Fast
30-Day Python Learning Plan for Beginners | Complete Python Roadmap to Learn Coding Fast
Machine Learning with Python Tutorial
Machine Learning with Python Tutorial
Python Tutorial
Python Tutorial
Python Loops Explained (For Loop & While Loop Guide)
Python Loops Explained (For Loop & While Loop Guide)
🐍 Python Loops Explained | For Loop, While Loop, Break, Continue & Pass
🐍 Python Loops Explained | For Loop, While Loop, Break, Continue & Pass

Looping with Enumerate

The `enumerate` function adds a counter to your iterable and returns it in a form of enumerate object. This can be useful when you need to loop over a sequence and use both its index and value. Here's an example:

```html fruits = ['apple', 'banana', 'cherry'] for index, fruit in enumerate(fruits): print(f"{index + 1}. I love {fruit}") ```

This code will output:

``` 1. I love apple 2. I love banana 3. I love cherry ```

Using For Loops with Break and Continue

The `break` statement, when executed in a loop, causes the loop to terminate prematurely. It can be used to exit a loop when a certain condition is met. On the other hand, the `continue` statement skips the rest of the current loop iteration and moves on to the next iteration. Here's an example demonstrating both:

```html for i in range(10): if i == 5: break elif i % 2 == 0: continue print(i) ```

In this example, the loop will only print the odd numbers between 0 and 10, and it will stop printing at 5.

With that, we've covered the essentials and some advanced aspects of for loops in Python. Now, you're ready to apply these concepts to your projects. Happy coding, and remember that practice makes perfect! Don't hesitate to experiment with different use cases and explore the `/r/python` subreddit for inspiration and help. Until next time, keep your loops running smoothly!