Python Tutorials: How to use or work with Loops In Python

In this part of Learning Python we Cover how to use loops in python
Written by Paayi Tech |17-Oct-2020 | 0 Comments | 694 Views

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

In programming, there can be a scenario when you have to execute the block of code multiple times. 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 times. There are multiple types of looping that are offered in python. The name of the loops is the 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 a 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 shows, the loop starts from 0 and ends before 10. The range keyword takes two arguments, the starting point of the loop and the ending point of the loop.

 

Range with one argument:

When we use range with only one argument, it assigns the starting point of the loop is 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 three arguments:

If we want to set the increment after each iteration, then we have to give the 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 increments bypassing negative value as a 3rd argument.

 

For Loop without Range:

We can use for loop without using the 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 the 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 multiples of 5:

for i in range(0,101):

    if i%5==0:

        print(i)

The above example starts the loop from 0 and ends on 100. It will print all the numbers that are completely divisible by 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 within a "for" loop. For every iteration of the 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 the loop is like the for a loop. The functionality of both the loops is the 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 an 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 tuned to learn the more exciting aspect of python programming.





Login/Sign Up

Comments




Related Posts



© Copyright 2020, 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.