Loops In Python

In this part of Learning Python we Cover how to use loops in python
Written by Paayi Tech |10-Apr-2019 | 0 Comments | 226 Views

In last part we have discussed about statement and its syntax and how we can use in our program. Now we will discuss about loops in python.

In programming there can be a scenario when you have to execute the block of code multiple time. This can’t be done by normal sequential programming. For this reason, all programming languages provide the looping structure to run a block of code multiple time. There are multiple types of looping that is offered in python. The name of the loops is following:

  1. For Loop
  2. While Loop
  3. Nested for Loop

We will see all these loops and the variation between them. First, we will see for loop.

 

For Loop:

For loop is a type of looping that starts with a keyword for and take the range of number from where to start and where to end the loop. Consider the following example:

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

#Output:

0

1

2

3

4

5

6

7

8

9

 

As the output show the loop start from 0 and end before 10. The range keyword takes two arguments the starting point of loop and ending point of loop.

 

 

Range with 1 argument:

When we use range with only one argument it assigns the starting point of loop be default zero. The above code can be written with only one range argument:

 

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

 

#Output:

0

1

2

3

4

5

6

7

8

9

 

 

Range with 3 argument:

If we want to set the increment after each iteration than we have to give third argument of range. By default, the increment argument is assigned as one. Following is the code incremented by 2:

 

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

 

#Output:

0

2

4

6

8

 

Similarly, we can do negative increment by passing negative value as 3rd argument.

 

For Loop without Range:

We can use for loop without using range function. This for looping is also known as element extraction. We can do for loop on strings, list, dictionaries and everything that can be iterated. Following is the example of applying for loop on a string:

string = "Python Programming"
 
for e in string:
    print(e)

 

#Output:

P

y

t

h

o

n

 

P

r

o

g

r

a

m

m

i

n

g

 

 

Example Codes:

Check if number is prime or not:

number = 4
 
if number >1:
    for i in range(2,number):
        if number%i==0:
            print(f"{number} is not prime")
            break
        else:
            print(f"{number} is prime")
            break
else:
    print(f"{number} is not prime")

 

 

 

Print all the numbers that are multiple of 5:

for i in range(0,101):
    if i%5==0:
        print(i)

 

The above example start loop from 0 and ends on 100. It will print all the number that are completely divisible by the digit 5 which will result in the printing of all the digits that are multiple of 5.

 

 

Printing Stars:

string = "* "
 
for i in range(10):
    print(string*i)

 

#Output

 

*

* *

* * *

* * * *

* * * * *

* * * * * *

* * * * * * *

* * * * * * * *

* * * * * * * * *

 

 

Nested for Loops:

Nested for loop is another type of for loop. It is basically for loop with in a for loop. For every iteration of outer for loop it will execute the complete inner for loop. Its implementation can be understood by the following code:

for i in range(3):
    print(f"Outer Loop {i}")
    for j in range(3):
        print(f"Inner Loop {j}")
    print()

 

#Output:

Outer Loop 0

Inner Loop 0

Inner Loop 1

Inner Loop 2

 

Outer Loop 1

Inner Loop 0

Inner Loop 1

Inner Loop 2

 

Outer Loop 2

Inner Loop 0

Inner Loop 1

Inner Loop 2

 

 

While Loop:

While loop is like the for loop. The functionality of both the loops are same but the syntax is totally different. We will write the same Check prime code that we have written in for loop with a while loop to know the difference between the syntax of for loop and while loop:

 

number = 3
i=2
if number >1:
    while(i
        if number%i==0:
            print(f"{number} is not prime")
            break
        else:
            print(f"{number} is prime")
            break
        i=i+1
else:
    print(f"{number} is not prime")

 

The above code is the same that we have written in for loop. The only difference is we are using a while loop and at every end of the loop we are incrementing the variable plus 1.

 

Infinite Loops:

while(True):
    print("Run forever")

 

The above loop will iterate for infinite time. It will not stop until the program is closed by the user. Such type of looping is used when the servers are running or some other type of computation like getting the frames from the camera input. For running such type of loop, we must use many types of error exception so that it cannot be stopped from some bad argument and continue looping. These sorts of error exception will be studied in later modules. So, stay tune to learn more exciting aspect of python programming.

 





Login/Sign Up

Comments




Related Posts



© Copyright 2019, All Rights Reserved. paayi.com

This site uses cookies. By continuing to use this site or clicking "I Agree", you agree to the use of cookies. Read our cookies policy and privacy statement for more information.