W3schools Python For Loop Tutorial Guide

Logan Jul 13, 2026

Python, an exceptionally versatile and widely-used programming language, offers a wealth of features for iterating over data. One such feature is the `for` loop, which allows you to execute a block of code for each item in a sequence. W3Schools provides a comprehensive guide on using `for` loops in Python, ensuring that beginners and veterans alike can master this essential concept.

Python Loops Cheat Sheet for Beginners
Python Loops Cheat Sheet for Beginners

Before diving into the nuances of `for` loops, let's clarify what they are and when to use them. In essence, a `for` loop iterates over a sequence (like a list, tuple, or string) and executes a block of code for each element in that sequence. It's particularly useful when you need to perform the same action repeatedly, with adjustments based on the current iteration.

Python For Loop | How To Use Python For Loops? | For Loops ⋆
Python For Loop | How To Use Python For Loops? | For Loops ⋆

Understanding Python's `for` Loop Syntax

Python's `for` loop follows a straightforward syntax. It begins with the `for` keyword, followed by a loop variable name, the `in` keyword, and the sequence to iterate over. The loop body is then defined by indentation, typically using four spaces or a tab character.

Follow @CodeWithAdya for more Python notes, coding tips, and beginner-friendly content πŸ’»πŸ’œ #Python
Follow @CodeWithAdya for more Python notes, coding tips, and beginner-friendly content πŸ’»πŸ’œ #Python

The basic syntax looks like this:

```python for variable in sequence: # Loop body (indented) ```

Iterating Over a List

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

One of the most common use cases for `for` loops is iterating over lists. In Python, lists are ordered collections of items separated by commas and enclosed in square brackets. Here's a simple example:

```python fruits = ["apple", "banana", "cherry"] for fruit in fruits: print(f"Today's special: {fruit}") ```

This script will print:

```plaintext Today's special: apple Today's special: banana Today's special: cherry ```

Iterating Over a Range

Python while Loop: Python Tutorial on while Loop with Examples
Python while Loop: Python Tutorial on while Loop with Examples

Python's `range()` function generates a sequence of numbers starting from zero by default, up to but not including the specified `end` parameter. `for` loops can iterate over these ranges, making them perfect for creating repetitive actions. Here's an example:

```python for i in range(5): print(i) ```

This script will print the numbers 0 through 4, each on a new line.

Looping with Indexes using `enumerate()`

While loop in Python Easy to learn 🀩
While loop in Python Easy to learn 🀩

Sometimes, you may need to know both the current element's value and its index in the sequence. In such cases, use the `enumerate()` function. It adds an index to the iterations, enabling you to access both at once.

Here's an example using `enumerate()` to print both the index and value of each item in a list:

Python for Loops: The Pythonic Way – Real Python
Python for Loops: The Pythonic Way – Real Python
Python For Loop vs While Loop Explained for Beginners
Python For Loop vs While Loop Explained for Beginners
For Loop & While Loop Explained | Python for Beginners
For Loop & While Loop Explained | Python for Beginners
Python For Loop Explained 🐍 | Complete Beginner Cheat Sheet
Python For Loop Explained 🐍 | Complete Beginner Cheat Sheet
Master Python Loops: The Ultimate Beginner’s Cheat Sheet πŸ”„πŸ
Master Python Loops: The Ultimate Beginner’s Cheat Sheet πŸ”„πŸ
Python Exit For Loop with Example Programs
Python Exit For Loop with Example Programs
Python Loops Explained for Beginners (Easy Guide)
Python Loops Explained for Beginners (Easy Guide)
18 Python while Loop Examples and Exercises
18 Python while Loop Examples and Exercises
the instructions for how to use python loops
the instructions for how to use python loops
For Loop, While Loop & Nested Loops Cheat Sheet
For Loop, While Loop & Nested Loops Cheat Sheet
python Loops for loop and while loop
python Loops for loop and while loop
9 Python Loop Tricks Every Beginner Should Learn (Cheat Sheet)
9 Python Loop Tricks Every Beginner Should Learn (Cheat Sheet)
Coding For Beginners Python - Learn the Basics - Loops
Coding For Beginners Python - Learn the Basics - Loops
Codin For Beginners Python - Learn the Basics - Loops
Codin For Beginners Python - Learn the Basics - Loops
Python Loops Explained (For Loop & While Loop Guide)
Python Loops Explained (For Loop & While Loop Guide)
Complete Python Basics Guide 🐍 | Loops for Beginners|
Complete Python Basics Guide 🐍 | Loops for Beginners|
Python Loops Complete Cheatsheet πŸŒ€ | for, while, break, continue, nested loops & patterns
Python Loops Complete Cheatsheet πŸŒ€ | for, while, break, continue, nested loops & patterns
Python Loops Cheat Sheet
Python Loops Cheat Sheet
How Python Break for loop With Examples
How Python Break for loop With Examples
For Loop in Python (Complete Guide)
For Loop in Python (Complete Guide)

```python fruits = ["apple", "banana", "cherry"] for index, fruit in enumerate(fruits): print(f"{index}: {fruit}") ```

This script will print:

```plaintext 0: apple 1: banana 2: cherry ```

Looping Through Dictionaries

Dictionaries, ordered collections of key-value pairs, can also be looped through using `for` loops, albeit with different syntax. By default, dictionaries loop through their keys. Here's an example:

```python fruit_prices = {"apple": 0.5, "banana": 0.3, "cherry": 0.7} for fruit in fruit_prices: print(f"The price of {fruit} is ${fruit_prices[fruit]}") ```

If you need to loop through both keys and values, use the `.items()` method:

```python for fruit, price in fruit_prices.items(): print(f"The price of {fruit} is ${price}") ```

emannating a good understanding of `for` loops and their variations is crucial for any Python developer. W3Schools' detailed guides and hands-on examples will help you grasp these concepts quickly and confidently.

Now that you've explored Python's `for` loops and their applications, why not try writing some loops of your own? Practice makes perfect, and using `for` loops to automate repetitive tasks will soon become second nature. Happy coding!