Home Loops in Python
Post
Cancel

Loops in Python

Loops are a common tool used in Python to help users iterate through lists, or perform the same operation numerous times without added efforts.

Let’s look at a few examples:

1
2
3
4
5
6
# Loops

for n in [0, 1, 5, 2, -5]:
    # this is inside the loop
    print("The number is", n, "and its squared value is", n**2)
# this is outside the loop
1
2
3
4
5
## The number is 0 and its squared value is 0
## The number is 1 and its squared value is 1
## The number is 5 and its squared value is 25
## The number is 2 and its squared value is 4
## The number is -5 and its squared value is 25
1
2
3
4
## Loop
s = "Python"
for c in s:
    print(c + "!")
1
2
3
4
5
6
## P!
## y!
## t!
## h!
## o!
## n!
1
2
3
# range(10) sets values 0-9, because recall Python starts at 0, not 1
for i in range(10):
    print(i)
1
2
3
4
5
6
7
8
9
10
## 0
## 1
## 2
## 3
## 4
## 5
## 6
## 7
## 8
## 9

This is equivalent to writing:

1
2
for i in range(0,10):
    print(i)
1
2
3
4
5
6
7
8
9
10
## 0
## 1
## 2
## 3
## 4
## 5
## 6
## 7
## 8
## 9

If we wanted it to start at 1 and go to 10 (inclusive), we would write:

1
2
for i in range(1,11):
    print(i)
1
2
3
4
5
6
7
8
9
10
## 1
## 2
## 3
## 4
## 5
## 6
## 7
## 8
## 9
## 10

Other examples:

1
2
3
#(start,end,increments)
for i in range(0,101,10):
    print(i)
1
2
3
4
5
6
7
8
9
10
11
## 0
## 10
## 20
## 30
## 40
## 50
## 60
## 70
## 80
## 90
## 100
1
2
3
4
n = 3
while n > 0:
    print(n)
    n = n - 1
1
2
3
## 3
## 2
## 1
1
print("Smile!")
1
## Smile!
This post is licensed under CC BY 4.0 by the author.