For Loop in Python Example w3schools Tutorial Guide

Logan Jul 13, 2026

In the realm of programming, loops are instrumental in executing blocks of code repeatedly until a specified condition is met. One of the most fundamental types of loops is the 'for' loop. Let's delve into the usage of for loop in Python, with insights drawn from the esteemed W3Schools.

Python Loops Explained for Beginners (Easy Guide)
Python Loops Explained for Beginners (Easy Guide)

The for loop in Python is primarily employed to iterate over a sequence (such as a list, tuple, or string) or other iterable objects. Unlike a while loop, a for loop doesn't require a counter variable to keep track of iteration.

Python For Loop vs While Loop Explained for Beginners
Python For Loop vs While Loop Explained for Beginners

The Basic Syntax of For Loop in Python

The basic syntax of a for loop in Python follows a straightforward structure. It iterates over an iterable object and executes the block of code for each item in the object.

Python for Loops: The Pythonic Way – Real Python
Python for Loops: The Pythonic Way – Real Python

Here's a simple example from W3Schools illustrating the syntax: for variable in iterable: indented-statement(s).

Initializing the For Loop

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

To initialize a for loop, you declare a variable followed by the 'for' keyword, specify the iterable object, and finally, use a colon to denote the start of the code block.

Example: for fruit in fruits:

Accessing Elements in the Iteration

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

Within the indented code block (the body of the loop), you can access the current item of the iterable. Using the example above, the variable fruit will take on the values of 'apple', 'banana', and 'cherry' in each iteration.

You can perform various operations on this variable, such as printing it: print(fruit).

For Loop with Range Function

For Loop in Python (Complete Guide)
For Loop in Python (Complete Guide)

The range function in Python is often used with for loops to generate a sequence of numbers within a specified range.

The syntax is: for variable in range(start, stop, step): where start is the initial value (default is 0), stop is the end value (excluding), and step is the increment value (default is 1).

Master Python Loops: The Ultimate Beginner’s Cheat Sheet πŸ”„πŸ
Master Python Loops: The Ultimate Beginner’s Cheat Sheet πŸ”„πŸ
18 Python while Loop Examples and Exercises
18 Python while Loop Examples and Exercises
Python while Loop: Python Tutorial on while Loop with Examples
Python while Loop: Python Tutorial on while Loop with Examples
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
How to use while loops in Python
How to use while loops in Python
Python For Loop Explained 🐍 | Complete Beginner Cheat Sheet
Python For Loop Explained 🐍 | Complete Beginner Cheat Sheet
For Loop vs While Loop in Python β€” Explained Simply 🐍
For Loop vs While Loop in Python β€” Explained Simply 🐍
While Loop in Python | Syntax, Example
While Loop in Python | Syntax, Example
How Python Break for loop With Examples
How Python Break for loop With Examples
Python Loops Cheat Sheet
Python Loops Cheat Sheet
Python For Loop | How To Use Python For Loops? | For Loops ⋆
Python For Loop | How To Use Python For Loops? | For Loops ⋆
Nested Loops in Python with Example
Nested Loops in Python with Example
9 Python Loop Tricks Every Beginner Should Learn (Cheat Sheet)
9 Python Loop Tricks Every Beginner Should Learn (Cheat Sheet)
For Loop & While Loop Explained | Python for Beginners
For Loop & While Loop Explained | Python for Beginners
Loops in Python πŸ’»
Loops in Python πŸ’»
For Loop vs While Loop in Python | Key Differences Explained
For Loop vs While Loop in Python | Key Differences Explained
Codin For Beginners Python - Learn the Basics - Loops
Codin For Beginners Python - Learn the Basics - Loops
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
Python Exit For Loop with Example Programs
Python Exit For Loop with Example Programs

Iteration with Step Value

You can specify a step value to skip or increment numbers. For example, for i in range(0, 10, 2) will cover only the even numbers between 0 and 10.

W3Schools provides a illustrative example of using the range function with a loop: for i in range(6): print(i).

Iteration with Descent Order

You can also use the range function with a step value of -1 to iterate in descending order. For instance, for i in range(10, 0, -1) starts at 10 and counts down to 1.

W3 Schools also elaborates on using the 'break', 'continue', and 'pass' statements within for loops, but that delves deeper into exception handling and flow control.

Mastering the for loop in Python is fundamental to efficient and effective coding. By understanding and applying the principles explained here, you're equipped to write robust loops to handle complex iterations. So, the next time you're puzzling over a 'for' loop or struggling to apply the 'range' function, remember to consult W3Schools for a fresh perspective and practical examples.

Now that you're armed with this newfound understanding, it's time to start coding! Get out there and build something meaningful by putting your new 'for' loop skills into practice.