Python Loops

Selin Yazıcıoğlu
4 min readMay 27, 2022

--

Hi everyone! I will explain to loops in this article. I will code on Jupyter Notebook Python. Let’s start!

Loops

Loops in Python are constructs that keep a string of code running over and over. Loops are often used to create clean code. Python has two loops: while and for.

While Loops

Thanks to loops, our programs are constantly
we can make it work. It ensures that the operations are repeated as long as the condition is true.

For example,

count = 1
while count < 6:
print(count)
count +=1 # count + 1
Output:
1
2
3
4
5

If we write as,

count = 1
while count < 2:
print("Hellooo")

then it would go into an infinite loop. That’s why we should write to count +=1

If we want to use “while loops” with decision structures else,

number = int(input("Enter a number between 10 and 20: "))while number > 20 or number < 10:
print("You did not enter a value in the desired value range. ")
number = int(input("Please, enter a number between 10 and 20: "))
else: print("Our value entered in the correct range: ", number)
Output:
Enter a number between 10 and 20: 3
You did not enter a value in the desired value range.
Please, enter a number between 10 and 20: 16
Our value entered in the correct range: 16

number = int(input("Enter a number between 10 and 20: "))
while number > 20 or number < 10:
print("You did not enter a value in the desired value range. ")
number = int(input("Please, enter a number between 10 and 20: "))
else: print("Our value entered in the correct range: ", number)
Output:
Enter a number between 10 and 20: 14
Our value entered in the correct range: 14

For Loops

For is a loop just like while. So just like in the while loop
as well as allowing our programs to run multiple times. However, the for loop is slightly more capable than the while loop. You can easily handle the things that you cannot do with the while loop or that you will have a hard time doing, with the help of the for loop.

range() in Python
It is used to show the numbers in a certain range.

range(start value, end value, change amount)

range(end value) — change amount is increased by 1 1. initial value starts from 0.

Let there be a loop that we want it to go from 4 to 10. “ [4,10)”

The loop includes 4 but does not include 10.

for i in range(4,10):
print(i)
Output:
4
5
6
7
8
9

Another example is,

# Decreased by 1 from 20 to 1.
for x in range(20, 1, -2):
print(x)
Output:
20
18
16
14
12
10
8
6
4
2

Let’s look have at another term is a break,

The main task of the break statement is to terminate a loop.

For example,

for i in range(7,16):
if i > 12:
print("Break")
break;
else:
print(i)
Output:
7
8
9
10
11
12
Break

Continue

Continue mission is to skip everything that comes after it and return to the beginning of the loop.

for i in range(3, 10, 2):
if i == 7:
print("Continue")
continue
print(i)
Output:
3
5
Continue
9

Nested Loops

The best way to describe nested loops is the asterisk.

Let’s draw!

  1. Example
# Double Looprows = 5
for i in range(0, rows):
for j in range(0, i+1):
print("*", end="")
print()
Output:
*
**
***
****
*****
# Single Looprows = 5
for i in range(1, rows +1):
print("*" * i)
Output:
*
**
***
****
*****

2. Example

# Double Looprows = 5
for i in range(rows, 0, -1):
for j in range(0, i):
print("*", end="")
print()
Output:
*****
****
***
**
*

# Single Loop
rows = 5
for i in range(rows, 0, -1):
print("*" * i)
Output:
*****
****
***
**
*

3.Example

# Double Looprows = 5
for i in range(0, rows):
for j in range(0, i+1):
if j == 0:
print(" " *(rows - i) + "*", end ="")
else: print("*", end="")
print()
Output:
*
**
***
****
*****
# Single Looprows = 5
for i in range(1, rows + 1):
print(" " *(rows - i) + "*" *i)
Output:
*
**
***
****
*****

4.Example

# Double Looprows = 5
for i in range(rows, 0, -1):
for j in range(0, i):
if j == 0:
print(" " *(rows - i) + "*", end ="")
else: print("*", end="")
print()
Output:
*****
****
***
**
*
# Single Looprows = 5
for i in range(rows, 0, -1):
print(" " *(rows - i) + "*" *i)
Output:
*****
****
***
**
*

5. Example

rows = 5
for i in range(1, rows + 1):
print(" " *(rows - i) + "*" *i*2)
Output:
**
****
******
********
**********

I hope while you’re reading my article you can enjoy it!

See you on another topic :)

--

--