Example of Looping in Python

Ads
 

Example of Looping in Python

In this section we will show you some of the examples of looping in Python programming language.

In this section we will show you some of the examples of looping in Python programming language.

Example of Looping in Python - Learn looping in Python

Let's learn looping in Python with the example code.

What is Loop?

In computer programming, a loop is used to run certain instruction or set of code continually until a specified condition is satisfied. For example you have to print 1 to 100 then you can make a loop and instruct the program to print till 100 and exit the loop once counter increased more than 100.

Looping in programming is used to execute certain code several times very conveniently and it is also easy to debug such code. Here we will show you examples of looping in Python programming language.

Python provides three types of loops to handle the looping.

For loop -
The for loop in Python is used to traverse over a sequence like- list, tuple, string etc.

Syntax of for loop -

for value in values:
        print(value)

In this syntax, values can be a list, tuple or a string and a value contains one value from the values.

For ex -

a_list = [10, 20, 30, 40, 50]

for a in a_list:
        print(a)

Output -
         

10
20
30
40
50

As you can see, output has values from the a_list and it’s printed one by one because we are printing the value after each iteration.
We can perform any operation on a single value from the list like - addition, subtraction etc.

While loop -

With the help of the while loop we can execute a block of instruction unless and until a condition is true.

Syntax of while loop -

while condition:
        instruction(s)

It will get cleared using following example -

num = 1

while num < 5:
        print(num)
        num+=1

As you can see in the above example, we have used a num variable which is one. and the loop will execute continuously unless and until the num goes greater than 5. for that we are incrementing num by one in each iteration. and as soon as the num becomes 5 the loop is going to break because the condition is satisfied.

Output of the above program will look like this -

1
2
3
4

Nested loop -

Nested loop allows a programmer to use one loop which is inside any other for or while loop. That is why nested loops are also being called “loop inside loop”.

Syntax of nested loop -

while condition:            
         while condition:   
                 instruction(s)
instruction(s)

Example of nested loop -

num1 = 1

# Loop 1
while num1 <=2:
        print(num1, 'This is outer loop')
        num2 = 1
      
        # Loop 2
        while num2 <=2:
                print(num2, 'This is inner loop')
                num2+=1
        num1+=1

As you can see the both loop will run until num1 and num2 becomes 2. and the output of the code will look like this -

1 This is outerloop
1 This is innerloop
2 This is innerloop
2 This is outerloop
1 This is innerloop
2 This is innerloop

Here more examples of Python:

Ads

Ads