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 in python are so important. Assume you have to print “hello world” for 5 times without using loops and with using loops and see the difference.

Loops in Python

In Python, we can use loops to repeatedly execute a block of code. If we want to print a statement 100 times then we can use loops and it will be done within seconds.

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 1000 times then we can’t do this by manual typing. So we go for loops. 

If we want to understand the advantage of loops by code then we can see below codes and see the difference between those two codes. 

#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

NOTE: There is no do-while loop in python.

Types of loops in python

For Loop 

In 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.

Run
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="")
Output:
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.

Run
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
Output:
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
Run
#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

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription