Sitemap
Mrunal Shrikumar

Mrunal Shrikumar

Loops in Python

7 min readOct 10, 2026

--

Press enter or click to view image in full size

Loops are an important part of programming when the same block of code needs to be executed repeatedly.

Instead of writing the same instructions again and again, a loop allows a block of code to run multiple times based on a sequence or a condition.

In Python, the two main types of loops covered here are:

  • for loop
  • while loop

Both are used for repetition, but they work in different ways. A for loop is commonly used to iterate over a sequence of elements, while a while loop continues executing as long as its condition is satisfied.

What Are Loops?

A loop allows a block of code to be executed repeatedly.

Python provides two main types of loops:

  1. for loop
  2. while loop

The choice between them depends on how the repetition needs to be performed.

A for loop can be used to go through the elements of a sequence, while a while loop repeatedly checks a condition.

Why Are Loops Used?

Loops are used when a block of code needs to be executed multiple times.

For example, if we want to print several numbers, we can use a loop instead of writing a separate print() statement for every number.

Loops are particularly useful when working with:

  • A sequence of numbers
  • Strings
  • Lists
  • Patterns

The for Loop

A for loop is used to iterate over a sequence of elements.

For example:

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

Output:

0
1
2
3
4
5
6
7
8
9

Here, the loop goes through each value produced by range(0, 10) and prints it.

The for loop can also be used with other sequences.

Iterating Through a Sequence

A for loop can iterate through the elements of a sequence.

For example, consider a list:

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

Output:

1
2
3
4
5

The loop takes each element from the list one at a time and executes the code inside the loop.

The same idea can also be applied to a string.

The range( ) Function

The range() function is commonly used with for loops to generate a sequence of numbers.

The general form is:

range(start, stop, step)

The three parts represent:

  • start — where the sequence begins
  • stop — where the sequence ends
  • step — the difference between consecutive values

By default, the step is 1.

For example:

range(0, 10)

produces values from 0 to 9.

The ending value 10 is not included.

We can see the values by converting the range into a list:

list(range(0, 10))

Output:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Using a Step in range( )

The step value determines how the sequence moves.

For example:

list(range(0, 10, 2))

Output:

[0, 2, 4, 6, 8]

Here, the sequence increases by 2.

Another example:

list(range(0, 100, 5))

Output:

[0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95]

The sequence increases by 5.

for Loop with Numbers

A for loop can be used to print a sequence of numbers.

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

Output:

0
1
2
3
4
5
6
7
8
9

The loop starts at 0 and continues until the value before 10.

Printing Even Numbers Using a Loop

The modulo operator % can be used to check whether a number is even.

A number is even when its remainder after division by 2 is 0.

for i in range(0, 10):
if i % 2 == 0:
print(i)

Output:

0
2
4
6
8

The loop checks each number and prints only the values that satisfy the condition.

Printing Odd Numbers Using a Loop

The same idea can be used to print odd numbers.

for i in range(10):
if i % 2 != 0:
print(i)

Output:

1
3
5
7
9

Here, the numbers whose remainder is not 0 when divided by 2 are printed.

Iterating Through Strings

A for loop can also iterate through the characters of a string.

For example:

word = "Python"
for character in word:
print(character)

Output:

P
y
t
h
o
n

The loop goes through the string one character at a time.

Get Mrunal Shrikumar’s stories in your inbox

Join Medium for free to get updates from this writer.

The same idea can be seen with another example:

text = "PWskills"
for character in text:
print(character)

Each character is processed individually by the loop.

Nested for Loops

A loop can be placed inside another loop. This is called a nested loop.

For example:

for i in range(1, 4):
for j in range(1, 4):
print(i * j)

The inner loop executes for each iteration of the outer loop.

Nested loops are useful for examples such as multiplication tables and pattern printing.

Multiplication Table Using Nested Loops

A multiplication table can be created using two for loops.

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

The outer loop selects the number whose table is being generated, while the inner loop goes through the multiplication values.

For example, part of the output would look like:

1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
...
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
...

This is an example of how a nested loop can be used to repeat one sequence of operations inside another.

Pattern Printing

Nested loops can also be used to create patterns.

For example, a right triangle made using * can be created with:

for i in range(4):
for j in range(i + 1):
print("*", end=" ")
print()

Output:

*
* *
* * *
* * * *

The outer loop controls the rows, while the inner loop controls how many * symbols are printed in each row.

A similar approach can be used to create other patterns.

The while Loop

A while loop repeatedly executes a block of code until its condition is no longer satisfied.

The basic structure is:

while condition:
# block of code

The condition is checked before each iteration.

If the condition is True, the block of code executes.

The process continues until the condition becomes False.

while Condition and Repeated Execution

Consider the following example:

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

Output:

1
2
3
4
5
6

Here, the loop begins with i = 1.

The condition is:

i < 7

As long as the condition is True, the loop continues.

After each iteration:

i = i + 1

increases the value of i.

When i becomes 7, the condition becomes False, so the loop ends.

How a while Loop Flows

The basic flow of a while loop can be understood as:

Test condition
|
┌─────┴─────┐
True False
| |
Body of while Exit loop
|
Check again
|
Test condition

The condition is tested repeatedly.

  • If the condition is True, the body of the loop executes.
  • After execution, the condition is checked again.
  • If the condition becomes False, the loop exits.

This repeated checking is what allows a while loop to continue executing a block of code.

Counting with a while Loop

A while loop can also be used for counting.

For example:

count = 5
while count > 0:
print(count)
count = count - 1

Output:

5
4
3
2
1

Here, the value starts at 5 and decreases by 1 after every iteration.

The loop continues while:

count > 0

Once count reaches 0, the condition becomes False and the loop ends.

The while...else Statement

Python also allows an else block to be used with a while loop.

For example:

n = 7
i = 1
while i < n:
print(i)
i = i + 1
else:
print("This will be executed when the while is run successfully without any break")

Output:

1
2
3
4
5
6
This will be executed when the while is run successfully without any break

The else block executes when the while loop finishes normally without being stopped by a break.

The important idea is that the while loop completes its execution, and then the else block runs.

for Loop vs while Loop

Both loops are used for repetition, but their approach is different.

A for loop is used to iterate through a sequence:

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

A while loop continues based on a condition:

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

Both examples produce:

0
1
2
3
4

The difference is in how the repetition is controlled.

Key Takeaways

  • Loops allow a block of code to be executed repeatedly.
  • Python provides two main types of loops: for and while.
  • A for loop can iterate through a sequence of elements.
  • range() can be used to generate a sequence of numbers.
  • The general form of range() is range(start, stop, step).
  • The default step in range() is 1.
  • The for loop can be used with numbers, lists and strings.
  • Nested for loops can be used for multiplication tables and patterns.
  • A while loop repeatedly executes a block while its condition is satisfied.
  • The condition of a while loop is checked repeatedly.
  • A while loop can be used for counting upward or downward.
  • Python also supports while...else, where the else block executes when the loop completes normally without a break.

Loops provide a way to handle repetition in Python through either iteration over a sequence or repeated execution based on a condition. Understanding for and while loops provides the foundation for working with repeated operations and more structured programs.

--

--