Jovian
⭐️
Sign In

LOOPS

A loop is a sequence of instructions that is repeated until a certain condition is met.

For Loops

In [33]:
colors = ['red','blue','pink','black','yellow']
for color in colors:
    print(color)

red blue pink black yellow
In [32]:
fruit = 'Watermelon'
for i in fruit:
    print(i)
W a t e r m e l o n

Break Statement

With the break statement we can stop the loop before it has looped through all the items:

In [34]:
colors = ['red','blue','pink','black','yellow'] # will print all colors in list (colors) until color == pink (including pink)
for color in colors:
    print(color)
    if color == 'pink':
        break
red blue pink
In [35]:
colors = ['red','blue','pink','black','yellow'] # will print colors until color == pink (excluding pink)
for color in colors:
    if color == 'pink':
        break
    print(color)
        
red blue

Continue Statement

We can stop the current iteration of the loop with the help of a continue statement, and continue with the next iteration:

In [36]:
colors = ['red','blue','pink','black','yellow'] # will print colors excluding pink
for color in colors:
    if color == 'pink':
        continue
    print(color)
        
red blue black yellow

Range() Function

The range() function is used to generate a sequence of numbers.

In [37]:
for x in range(10): # will display numbers from 0 to 9
    print(x)
0 1 2 3 4 5 6 7 8 9
In [38]:
for x in range (2,10): # will print numbers from 2(start parameter) to 9 
    print(x)
2 3 4 5 6 7 8 9

Increment the sequence with 3 (default is 1):

In [39]:
for x in range(2, 30, 3): # will print numbers from 2 with an increment of 3 upto 30
    print(x)
2 5 8 11 14 17 20 23 26 29

Else in For Loop

In [40]:
for x in range(10):         # will print numbers from 0 to 9 and display a message at the end
  print(x)
else:
  print("Finally the loop is finished!")
0 1 2 3 4 5 6 7 8 9 Finally the loop is finished!

Nested Loops

A nested loop is a loop inside a loop.

The "inner loop" will be executed one time for each iteration of the "outer loop":

In [41]:
fruits = ['apple','orange','grapes']
colors = ['red','orange','green']

for i in fruits :
    for j in colors:
        print(i,j)
apple red apple orange apple green orange red orange orange orange green grapes red grapes orange grapes green

The pass Statement

for loops cannot be empty, but if you for some reason have a for loop with no content, put in the pass statement to avoid getting an error.

In [42]:
x = [1,2,3,4]
for i in x:
    pass

While Loops

With the help of a while loop we can execute a block of code as long as a condition is true.

In [43]:
i = 1
while i < 10:
    print(i)
    i += 1
1 2 3 4 5 6 7 8 9

Note: remember to increment i, or else the loop will continue forever.

The while loop requires relevant variables to be ready, in this example we need to define an indexing variable, i, which we set to 1.

Break Statement

With the break statement we can stop the loop even if the while condition is true:

In [46]:
i = 1    # will print numbers from 1 to 7
while i < 10:
    print(i)
    if i == 7:
        break
    i +=1
1 2 3 4 5 6 7

Continue Statement

With the continue statement we can stop the current iteration, and continue with the next:

In [51]:
i = 0          # will print numbers from 1 to 10 , excluding 7
while i < 10:
    i += 1
    if i == 7:
        continue
    print(i)
1 2 3 4 5 6 8 9 10

Else Statement

In [52]:
i = 1
while i < 10:
    print(i)
    i += 1
else:
    print('i is not less than 10')
1 2 3 4 5 6 7 8 9 i is not less than 10
In [ ]: