Master For Loop Syntax in Python with W3Schools Guide

Logan Jul 13, 2026

Python, one of the most popular programming languages, is widely used for its simplicity, readability, and robust features. One of its most fundamental constructs is the loop, which allows repetitive execution of a block of code. The 'for' loop in Python is an essential tool for iteration over a sequence, simplifying tasks that would otherwise require complex logic and code repetition.

For Loop vs While Loop in Python — Explained Simply 🐍
For Loop vs While Loop in Python — Explained Simply 🐍

In this article, we'll explore the 'for' loop syntax in Python, as taught by the renowned programming resource, W3Schools. We'll delve into its basic structure, how to iterate through lists, tuples, strings, and other sequences, and also cover the 'for-else' construct. Let's dive right in!

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

Understanding the Basic Syntax

The fundamental syntax of a 'for' loop in Python is straightforward and follows a simple structure:

Python Conditional Statements & Loops
Python Conditional Statements & Loops

```python for variable in sequence: # code to execute ```

Here, 'variable' is a placeholder for the current item being processed, and 'sequence' is the iterable object (like a list, tuple, or string) that the loop will traverse.

Let's take a look at a simple example:

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

```python fruits = ["apple", "banana", "cherry"] for fruit in fruits: print(fruit) ```

Iterating Through Lists

Lists are the most common iterable objects in Python. They allow random ordering,tcp and provide a convenient way to group related data.

```python numbers = [1, 2, 3, 4, 5] for num in numbers: print(num * 2) ```

This code will output:

Python Cheat Sheet for Beginners 2026 | Python Basics, Syntax, Loops, Functions & Variables
Python Cheat Sheet for Beginners 2026 | Python Basics, Syntax, Loops, Functions & Variables

``` 2 4 6 8 10 ```

Iterating Through Tuples

Tuples are similar to lists, but they are immutable (can't be changed). They are defined by placing all the items in parentheses, separated by commas.

```python point = (1, 2) for coordinate in point: print(coordinate) ```

The output will be:

Coding For Beginners Python - Learn the Basics - Loops
Coding For Beginners Python - Learn the Basics - Loops

``` 1 2 ```

Remember, unlike lists, you can't add, remove, or modify elements in tuples.

Iterating Through Strings

an image of a computer screen with the text'basic while loop sytax '
an image of a computer screen with the text'basic while loop sytax '
Python Loops Cheat Sheet for Beginners
Python Loops Cheat Sheet for Beginners
While Loop in Python | Syntax, Example
While Loop in Python | Syntax, Example
For Loop in Python (Complete Guide)
For Loop in Python (Complete Guide)
While loop in Python Easy to learn 🤩
While loop in Python Easy to learn 🤩
python Loops for loop and while loop
python Loops for loop and while loop
Python For Loop vs While Loop Explained for Beginners
Python For Loop vs While Loop Explained for Beginners
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
Complete Python Basics Guide 🐍 | Loops for Beginners|
Complete Python Basics Guide 🐍 | Loops for Beginners|
Python Syntax Explained Simply 🐍
Python Syntax Explained Simply 🐍
Python Cheat Sheet for Beginners | Syntax, Loops, Functions, OOP & Quick Revision Notes
Python Cheat Sheet for Beginners | Syntax, Loops, Functions, OOP & Quick Revision Notes
Python Exit For Loop with Example Programs
Python Exit For Loop with Example Programs
the instructions for how to use python loops
the instructions for how to use python loops
Python Loops Explained (For Loop & While Loop Guide)
Python Loops Explained (For Loop & While Loop Guide)
Python Loops Explained for Beginners (Easy Guide)
Python Loops Explained for Beginners (Easy Guide)
Python While Loop
Python While Loop
Python while Loop: Python Tutorial on while Loop with Examples
Python while Loop: Python Tutorial on while Loop with Examples
Python for Loops: The Pythonic Way – Real Python
Python for Loops: The Pythonic Way – Real Python
Coding For Beginners Python - Learn the Basics - Loops
Coding For Beginners Python - Learn the Basics - Loops
18 Python while Loop Examples and Exercises
18 Python while Loop Examples and Exercises

A string in Python is also a sequence of characters, and you can iterate through it like any other sequence.

```python word = "hello" for letter in word: print(letter) ```

The output will be:

``` h e l l o ```

Using the Enumerate Function

Sometimes, you need to know the index of each item in the sequence. Python's built-in 'enumerate' function is useful in such cases.

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

The output will include the index of each fruit:

``` Index: 0, Fruit: apple Index: 1, Fruit: banana Index: 2, Fruit: cherry ```

Using the 'for-else' Construct

Python's 'for-else' construct is unique. It's like an 'if-else' statement, but for loops. The 'else' block is executed after the 'for' loop has finished running, only if the loop didn't encounter a 'break' statement.

```python numbers = [1, 2, 3, 4, 5] found = False for num in numbers: if num == 6: found = True break if not found: print("Number not found") ```

If '6' is in the list, it prints nothing. If '6' is not in the list, it prints "Number not found".

Python's 'for' loop is a powerful tool for repetitive tasks. Its simplicity and versatility make it one of the core constructs in the language. Keep practicing and exploring to master it!