Featured Article

Master Python Loops: 3 Essential Coding Examples

Cynthia Jul 13, 2026

Python, a high-level, interpreted language known for its simplicity and readability, offers powerful constructs for repetitive tasks through loops. If you're looking to automate repetitive tasks or traverse through data structures, Python loops are your friend. Let's explore three types of loops in Python: for, while, and nested loops.

Python Cheatsheet for Beginners | Learn Python Fast
Python Cheatsheet for Beginners | Learn Python Fast

Before we dive into the loop types, it's crucial to understand that loops in Python allow us to execute a block of code repeatedly. The number of repetitions might be known, or it could be determined during runtime. Now, let's delve into each loop type.

Python Operators Tutorial – Chapter 3 Notes
Python Operators Tutorial – Chapter 3 Notes

For Loops

For loops are often used when we know the range of iterations, like looping through an index or an iterable (like a list, tuple, or string).

while loop in python
while loop in python

Here's the basic syntax of a for loop in Python:

for variable in iterable: # code block

Let's print numbers from 0 to 9 using a for loop:

the instructions for how to use python loops
the instructions for how to use python loops

for i in range(10): print(i)

For Loop with Range

The `range()` function is frequently used with for loops. It generates a sequence of numbers starting from 0 by default, incrementing by 1 for each number in the sequence. The third argument in `range()` is the step, which defaults to 1 if not provided.

Printing even numbers between 2 and 20 using a for loop and `range()` with a step of 2:

Master Python Loops: The Ultimate Beginner’s Cheat Sheet πŸ”„πŸ
Master Python Loops: The Ultimate Beginner’s Cheat Sheet πŸ”„πŸ

for i in range(2, 21, 2): print(i)

For Loop with Other Iterables

For loops can iterate over any sequence or iterable, not just numbers. Here's how you can print elements of a list using a for loop:

my_list = [1, 2, 3, 4, 5] for item in my_list: print(item)

While 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

While loops execute a block of code as long as a specified condition is true. They are ideal for situations where we don't know in advance how many times the loop needs to run.

The syntax for a while loop is as follows:

Python Loops Explained (For Loop & While Loop Guide)
Python Loops Explained (For Loop & While Loop Guide)
Loops in Python πŸ’»
Loops in Python πŸ’»
python aesthetic
python aesthetic
an image of a computer screen with some text on it
an image of a computer screen with some text on it
Python Coding Aesthetic | 3 Elegant Python Oneliners For Clean Code πŸ’»βœ¨
Python Coding Aesthetic | 3 Elegant Python Oneliners For Clean Code πŸ’»βœ¨
Python For Loop Explained 🐍 | Complete Beginner Cheat Sheet
Python For Loop Explained 🐍 | Complete Beginner Cheat Sheet
programming
programming
a computer screen with some text on it and an image of a cat in the background
a computer screen with some text on it and an image of a cat in the background
DAY 4 | Python Loops: for Loop & while Loop
DAY 4 | Python Loops: for Loop & while Loop

while condition: # code block

Here's a simple while loop that prints numbers from 0 to 9:

i = 0 while i < 10: print(i) i += 1

Infinite While Loops

While loops can run indefinitely, acting like an infinite loop until a break statement is encountered. Here's an example of an infinite while loop that keeps asking for user input until the user enters 'q':

while True: user_input = input("Enter something ('q' to quit): ") if user_input == 'q': break print("You entered:", user_input)

Break and Continue in Loops

In addition to the standard loop syntax, Python offers statements like `break` and `continue` to control the flow of loops. The `break` statement is used to exit a loop prematurely, while the `continue` statement skips the current iteration and jumps to the next one.

Combining both loops with these flow control statements can help you create powerful and flexible code, letting you handle various scenarios effortlessly.

Nested Loops

You can use loops inside other loops, creating nested loops. This structure is useful when you need to repeat a block of code, but that block contains another repeatable block. Each loop can run independently or together in controlled iterations.

Here's a simple example of a nested loop that prints the multiplication table up to 5:

for i in range(1, 6): for j in range(1, 6): print(f"{i} x {j} = {i * j}")

As you can see, understanding and effectively using Python's various types of loops is vital for writing efficient and maintainable code. Happy coding!