Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Loops in Python

loops in python

Introduction to loops in python

In programming loops are structure that repeats a sequence of instruction until a specific condition is met. Loops are most important topic to start any programming language. Lets check why loops are so important

Assume you have to print “hello world” for 5 times without using loops and with using loops and see the difference.

Advantages of loops

We can reduce the size of the code and code becomes more readable with loops. If we need to do same operation for 100 times then we can’t do this by manual typing. So we go for loops. 

#without using loops
print(” hello world “)
print(” hello world “)
print(” hello world “)
print(” hello world “)
print(” hello world “)
#using loops
for i in range(5):
    print(” hello world “)
Types of loops in python

Types of loops in python

  • for loop
  • while loop

NOTE: There is no do-while loop in python

For loop 

Python for loop is different from other programming languages.  For loop is used for iterating through string, list, set, tuple, dictionary . For loop is mostly used as iterator in Python.

For loop can be used in two ways , see the below code for understanding.

arr=[“p”,“r”,“e”,“p”,“i”,“n”,“s”,“t”,“a”]

#loop iterating through the indices of the list
for i in range(9):
    print(arr[i],end=“”)

print( ) 

#loop iterating to elements of the list
for i in arr:
    print(i,end=“”)     
prepinsta
prepinsta

Range function in python

Syntax: range(start,end,incrementer)

range function is mostly used in for loops to traverse the set of code for specified numbers of times.

While loop

While loop is a entry controlled looping statement. While loop works on the condition given in the loop. It runs until the given condition is true , once the condition becomes false it exits from the loop. 

To understand more on while loop see the below code.

n=0
#given condition in while loop is false
#so it dont go inside the loop
while(n>10):
    print(n,end=” “

n=0
while(n<5): 
    print(n,)
    n=n+1
0
1
2
3
4

What is loop control statements?

loop control statements are the statements which controls the execution of loops. Types of loop control statements are 

  • break
    used to terminate the loop 
  • continue
    used to escape from the present iteration
  • pass
    pass doesn’t effect the code but it is used when you need to statement but not to execute that
#using break statement
n=0
print(“Output by using break statement :”)
while(n<5): 
    if n==3:
        break
    print(n)
    n+=1
#using continue and pass statement
n=0
print(“Output by using continue statement :”)
while(n<5): 
    if n==3:
        n=n+1
        continue
    else:
        pass
    print(n)
    n=n+1
Output by using break statement :
0
1
2
Output by using continue statement :
0
1
2
4