





Please login

Prepinsta Prime
Video courses for company/skill based Preparation

Prepinsta Prime
Purchase mock tests for company/skill building
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.

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.
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.
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
Output by using break statement :
0
1
2
Output by using continue statement :
0
1
2
4
Login/Signup to comment