For Loop While Loop Python W3schools Tutorial Guide

Logan Jul 13, 2026

Loops are an integral part of programming, allowing us to execute a block of code repeatedly until a specific condition is met. Python offers two types of loops: `for` loop and `while` loop. Both serve unique purposes and have their own syntax. Let's delve into each of them, exploring their syntax, usage, and differences.

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

Before we dive into the details, it's crucial to understand that `for` loops are typically used when we know the number of iterations in advance, while `while` loops are best suited for situations where we don't know how many iterations will occur.

python Loops for loop and while loop
python Loops for loop and while loop

For Loop in Python

The `for` loop is used for iterating over a sequence (e.g., list, tuples, strings) or other iterable objects. It executes a block of code for each item in the sequence until all items have been exhausted.

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

Syntax and Example

The basic syntax of a `for` loop is as follows:

Python Loops Explained (For Loop & While Loop Guide)
Python Loops Explained (For Loop & While Loop Guide)

```python for variable in sequence: # code block ```

Here's a simple example of a `for` loop that prints numbers from 0 to 4:

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

Nested For Loops

Python also supports nested `for` loops, allowing us to iterate over data structures like matrices or lists of lists. Let's print a 3x3 grid as an example:

For Loop vs While Loop in Python β€” Explained Simply 🐍
For Loop vs While Loop in Python β€” Explained Simply 🐍

```python for i in range(3): for j in range(3): print((i, j), end=' ') print() ```

In this nested `for` loop, the outer loop iterates over the rows, and the inner loop iterates over the columns, printing each coordinate on a new line.

While Loop in Python

The `while` loop is used when we need to execute a block of code repeatedly as long as a certain condition is true. This condition is checked at the beginning of each iteration.

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

Syntax and Example

The syntax of a `while` loop is as follows:

Python while Loop: Python Tutorial on while Loop with Examples
Python while Loop: Python Tutorial on while Loop with Examples
Master Python Loops: The Ultimate Beginner’s Cheat Sheet πŸ”„πŸ
Master Python Loops: The Ultimate Beginner’s Cheat Sheet πŸ”„πŸ
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
Coding For Beginners Python - Learn the Basics - Loops
Coding For Beginners Python - Learn the Basics - Loops
For Loop vs While Loop in Python | Key Differences Explained
For Loop vs While Loop in Python | Key Differences Explained
Python Loops Cheat Sheet for Beginners
Python Loops Cheat Sheet for Beginners
18 Python while Loop Examples and Exercises
18 Python while Loop Examples and Exercises
Python Loops Explained for Beginners (Easy Guide)
Python Loops Explained for Beginners (Easy Guide)
For Loop, While Loop & Nested Loops Cheat Sheet
For Loop, While Loop & Nested Loops Cheat Sheet
C++ Loops Explained in 1 Minute πŸ” | for, while, do-while Made Easy πŸ’»βœ¨
C++ Loops Explained in 1 Minute πŸ” | for, while, do-while Made Easy πŸ’»βœ¨
visit site for learning loops in python
visit site for learning loops in python
Python for Loops: The Pythonic Way – Real Python
Python for Loops: The Pythonic Way – Real Python
For Loop & While Loop Explained | Python for Beginners
For Loop & While Loop Explained | Python for Beginners
Codin For Beginners Python - Learn the Basics - Loops
Codin For Beginners Python - Learn the Basics - Loops
While Loop in Python | Syntax, Example
While Loop in Python | Syntax, Example
Python Control Flow for Beginners – If Else, For Loop, While Loop Explained
Python Control Flow for Beginners – If Else, For Loop, While Loop Explained
Python Loops (Beginner Friendly)
Python Loops (Beginner Friendly)
Python While Loop | How to Use While Loops?| Continue | Break ⋆ IpCisco
Python While Loop | How to Use While Loops?| Continue | Break ⋆ IpCisco
Python For Loop Explained 🐍 | Complete Beginner Cheat Sheet
Python For Loop Explained 🐍 | Complete Beginner Cheat Sheet
Python Loops - Learn one of the most powerful concepts in programming - TechVidvan
Python Loops - Learn one of the most powerful concepts in programming - TechVidvan

```python while condition: # code block ```

Here's an example that prints numbers from 0 to 4 using a `while` loop:

```python i = 0 while i < 5: print(i) i += 1 ```

Break, Continue, and Pass in Loops

Python provides control flow statements that allow us to alter the execution order of loops. The `break` statement exits the loop prematurely if a condition is met, while the `continue` statement skips the rest of the current iteration and moves on to the next one. The `pass` statement is a placeholder when a statement is required, but no action needs to be taken.

The choice between using a `for` loop or a `while` loop depends on the specific use case. `For` loops are generally preferred when dealing with iterable sequences, while `while` loops are best for handling unknown numbers of iterations. Understanding when to use each type of loop is essential for writing efficient and maintainable code in Python.

Now that you have a solid understanding of loops in Python, it's time to practice and apply this knowledge to your own projects. Happy coding!