Master Simple Loops in Python: A Concise Guide

Logan Jul 13, 2026

Loops are fundamental to programming, allowing us to repeat a block of code a specific number of times, or until a certain condition is met. Python, with its simple syntax and readability, offers two main types of loops: 'for' and 'while'. Let's dive into both, exploring their syntax, use-cases, and best practices.

For Loop & While Loop Explained | Python for Beginners
For Loop & While Loop Explained | Python for Beginners

Understanding how loops work is akin to mastering a new language, making your Python code more powerful and efficient.

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

For Loop

The 'for' loop in Python is used to iterate over a sequence (like list, tuple, or string) or other iterable objects. It's ideal when you know how many times the loop should run.

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

Syntax: 'for in : '
- ``: a temporary name used to reference the items in the sequence during each iteration
- ``: the object you're looping over

Iterating over a list

Master Python Loops: The Ultimate Beginner’s Cheat Sheet 🔄🐍
Master Python Loops: The Ultimate Beginner’s Cheat Sheet 🔄🐍

Here's a basic 'for' loop example, iterating over a list of numbers:

numbers = [1, 2, 3, 4, 5]
for i in numbers:
   print(i)

This script will print numbers 1 through 5, each on a new line.

Using 'enumerate()' function

for loop in python with Simple example – allinpython.com
for loop in python with Simple example – allinpython.com

What if you need both the index and value of each item in the sequence? Use the 'enumerate()' function.

numbers = [1, 2, 3, 4, 5]
for i, num in enumerate(numbers):
   print(f"Index: {i}, Value: {num}")

Now, besides the number, you'll also get its index in your output.

While Loop

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

On the other hand, the 'while' loop runs as long as a certain condition remains true. This is useful when you don't know how many times the loop needs to run.

Syntax: 'while : '
- ``: an expression that evaluates to True or False

python Loops for loop and while loop
python Loops for loop and while loop
Python While Loop
Python While Loop
a screenshot of a black background with text that reads, basic for loop 2 loop through a list
a screenshot of a black background with text that reads, basic for loop 2 loop through a list
Loops in Python 💻
Loops in Python 💻
Python While Loop | How to Use While Loops?| Continue | Break ⋆ IpCisco
Python While Loop | How to Use While Loops?| Continue | Break ⋆ IpCisco
Stop Writing Python Loops Like This ❌ (Beginner Mistake)
Stop Writing Python Loops Like This ❌ (Beginner Mistake)
the text reads loops for & while, and an image of a computer screen with code numbers
the text reads loops for & while, and an image of a computer screen with code numbers
Loops Explained for Beginners |Programming Basics
Loops Explained for Beginners |Programming Basics
For Loop, While Loop & Nested Loops Cheat Sheet
For Loop, While Loop & Nested Loops Cheat Sheet
18 Python while Loop Examples and Exercises
18 Python while Loop Examples and Exercises
🐍 Python Loops Explained | For Loop, While Loop, Break, Continue & Pass
🐍 Python Loops Explained | For Loop, While Loop, Break, Continue & Pass
Python Loops Explained for Beginners (Easy Guide)
Python Loops Explained for Beginners (Easy Guide)
Python Loops - Learn one of the most powerful concepts in programming - TechVidvan
Python Loops - Learn one of the most powerful concepts in programming - TechVidvan
If you have slow loops in Python, you can fix it…until you can’t
If you have slow loops in Python, you can fix it…until you can’t
a screen shot of the text looping through tuples in python on a dark background
a screen shot of the text looping through tuples in python on a dark background
For Loop in Python (Complete Guide)
For Loop in Python (Complete Guide)
Loops in Python | For, While, Do While Loops | Python Course | Using Different Types of Python Loops
Loops in Python | For, While, Do While Loops | Python Course | Using Different Types of Python Loops

Basic 'while' loop

Here's a simple 'while' loop example, printing numbers 1 to 5:

i = 1
while i <= 5:
   print(i)
   i += 1

Change the condition or initital/statement lines to modify the output.

Using 'break' and 'continue' statements

Key control statements 'break' and 'continue' help you control the flow of loops.

< lst>i = 0
while True:
   i += 1
   if i == 5:
     break
   elif i % 2 == 0:
     continue
   print(i)

The 'while' loop runs until 'i' equals 5. 'continue' skips even numbers, so only odd numbers are printed.

Understanding and applying these loops is a significant step in your Python journey. With practice, you'll write efficient, clean, and readable code. Happy coding!