What Is a Loop in Python?
A loop in Python is a way to repeat code.
This is useful when you want to do the same task more than once. Instead of writing the same line again and again, you can write a loop once and let Python repeat it for you.
Python mainly has two loop types:
forloopswhileloops
This page gives the basic idea of loops. It does not fully teach loop syntax.
Quick example
for number in [1, 2, 3]:
print(number)
This loop prints each number in the list.
The indented line is the code that repeats. It runs once for each value.
Output:
1
2
3
What a loop means
A loop is a way to repeat code.
Here is the main idea:
- You want to perform the same action multiple times
- You write that action once inside a loop
- Python runs it again for each repetition
Each time the loop runs is called an iteration.
For example, if a loop prints three items, that loop has three iterations.
Why loops are useful
Loops are useful because they help you avoid repeated code.
They are commonly used for tasks like:
- working through items in a list
- reading characters in a string
- reading lines from a file
- counting through a range of numbers
Loops also make code easier to update later. If the repeated step needs to change, you usually only change it in one place.
The main loop types in Python
Python has two main loop types.
for loop
A for loop repeats once for each item in a sequence.
That sequence might be:
- a list
- a string
- a
range() - other iterable values
Beginners often start with for loops because they are usually easier to read.
If you want a full lesson, see Python for loops explained.
while loop
A while loop repeats as long as a condition stays True.
This is useful when you do not know exactly how many times the loop should run.
For a full beginner guide, see Python while loops explained.
Simple example of a loop
Here is a small for loop:
for number in [1, 2, 3]:
print(number)
What happens here:
for number in [1, 2, 3]:tells Python to go through the list one item at a timeprint(number)is the repeated action- The indentation shows which line belongs to the loop
Expected output:
1
2
3
If the print(number) line was not indented, it would not be part of the loop. If indentation is confusing, read Python indentation rules and why they matter.
When to use a loop
Use a loop when you need to repeat a step.
Common examples:
- Loop through items in a list
- Repeat until the user enters valid input
- Read lines from a file one by one
- Count through a range of numbers
For example, if you want to process each item in a list, a loop is the natural tool. A good next step is how to loop through a list in Python.
You will also often see loops used with the range() function when counting numbers.
Important beginner idea
There are a few important things to understand early.
- A loop can run many times
- A loop can also run zero times
- Indentation matters because it defines the repeated block
- A
whileloop can become infinite if its condition never becomesFalse
For example, this while loop never stops:
while True:
print("loop running")
This is called an infinite loop.
A while loop usually needs something to change so the condition eventually becomes false.
Also, be careful when changing loop variables or counting values. Small mistakes can cause bugs or make a loop behave in unexpected ways.
What this page should not cover in depth
This page is only a glossary-style definition.
It does not fully teach:
- full
forloop syntax - full
whileloop syntax - nested loops
breakcontinue
If you want to go further, read:
Common mistakes
Beginners often run into these problems:
- Confusing a loop with an
ifstatement - Thinking loops always run forever
- Forgetting that indentation controls which lines repeat
- Using a
whileloop without changing the condition - Not realizing that
forloops usually go through a sequence
If you are not sure what a loop is doing, simple print() statements can help you debug it.
Examples:
print(item)
print(count)
print("loop running")
These help you see:
- whether the loop is running
- which value is being processed
- how a counter is changing
FAQ
What is a loop in simple words?
A loop is code that repeats the same steps multiple times.
What are the two main loops in Python?
Python mainly uses for loops and while loops.
When should I use a for loop?
Use a for loop when you want to go through items like a list, string, or range.
When should I use a while loop?
Use a while loop when code should keep running until a condition changes.
Can a loop run forever?
Yes. A while loop can run forever if the condition never becomes false.
See also
- Python for loops explained
- Python while loops explained
- Python indentation rules and why they matter
- Python break and continue statements
- Python range() function explained
- How to loop through a list in Python
If you are learning loops for the first time, the best next step is to start with for loops, then learn while loops, and then practice looping through lists.