Python Certification Training for Data Scienc ...
- 60k Enrolled Learners
- Live Class
Loops in python are an efficient method for optimizing your code to execute multiple statements. If a particular code has to be executed multiple times, a user can put it in a loop to perform multiple iterations to get a desired output. It saves a lot of effort and reduces complexity of the code as well. In this blog we will discuss the concept of while loop in python with various examples. Following are the topics discussed in this blog:
A while loop in python is used to iterate over a block of code or statements as long as the test expression is true. In case of a while loop a user does not know beforehand how many iterations are going to take place. Take a look at the syntax of while loop in python.
while (test expression): # statements in the while block
The execution starts and checks whether the test expression is true, when the test expression is true it enters the while loop and executes the statements inside the while loop. As soon as the test expression is false, the execution skips the while loop and moves to the next statements in the program.
To control the flow in the loop, various control statements like break and continue can be used in the while loop. Lets take a look at how we can use these control statements in a while loop.
Break statement in python is used to skip the entire execution of the block in which it is encountered. As soon as a break statement is encountered in a loop, the execution skips the rest of the iterations and moves out of the loop.
i = 1 while i <= 5 : print(i) if i == 4: break i = i+1
Output: 1 2 3 4
As soon as the value of x becomes 4 , the execution will skip the rest of the iterations. To understand how it affects the execution lets take another example with a continue statement.
Continue is used to skip the current iteration in the loop. As soon as continue is encountered in a loop, the current iteration is skipped but the rest of iterations are still executed.
i = 1 while i <=5 : if i == 4: i = i+1 continue else: print(i) i = i+1
Output: 1 2 3 5
As soon as the continue statement is encountered in the loop the current iteration in skipped and the loop executes the remaining iterations.
Conditional statements also have logical conditions as the test expression which is used for decision making in python. To understand the use of conditional statements in a while loop lets take an example.
num =int(input('enter a number')) while num >= 0: if num == 0: print('equal to zero') elif num > 0: print('greater than zero') else: print('enter a valid number') break
This is a simple example where we have used conditional if and else statements in a program. For more complex decision making problems we can use conditional statements in a while loop where a test expression will be declared in the beginning.
An infinite while loop executes infinite times, which means theoretically the execution never stops. This may come as a surprise but it has its own advantages and disadvantages as well.
For instance, if we do not specify a increment operator for the variable in the test expression, the loop will never stop meaning it will execute infinite times.
i = 4 while i > 0: print(' i am an infinite while loop')
This program will run infinite iterations, unless we press ctrl+c or put a control statement in the loop.
If a while loop consists of another while loop we can call it a nested while loop. There is no particular limit for number of loops in a nested while loop. It may go on for as many times a user requires or declares it in the program.
To understand this lets take a look at an example:
i = 1 j = 5 while i < 6: while j > 0: print(i,j) j = j -1 i = i + 1
Output: 1 5 2 4 3 3 4 2 5 1
In this example, we have two variables i and j which are used in different test expressions. It is a classic example of using a nested loop.
Lets take another example using the conditional and control statements as well.
i = 'edureka' j = 1 while j > 0: for x in i: print(j , x) j = j + 1 if x == 'a': break
Output: 1 e 2 d 3 u 4 r 5 e 6 k 7 a
In this article we have discussed the concept of while loop in python with various examples. While loop plays an important when we have a test expression which can be tested in the beginning of the loop. For example- using a while loop for a bank data, where we will only proceed if the test expression is satisfied which in this case can be any statistical value. Python programming language makes it easy to work with fundamental concepts for its ease of access. To master your skills enroll in Edureka’s python certification program and kickstart your learning.
Have any questions? Mention them in the comments section. We will get back to you as soon as possible.