In coding, loops are designed to execute a code block repeatedly. In this tutorial, we will learn how to construct a while loop in Python.
In this article, we are discussing while loop in Python. Python's while loop iteration of a code block is executed as long as the given condition (conditional expression) is true.
Syntax of while Loop:
statement while condition: statement |
---|
The given condition (conditional_expression) is evaluated initially in the Python while loop. Then, if the conditional expression gives a boolean value true, the while loop statements are executed. This procedure repeatedly occurs until the conditional expression returns the boolean value false.
The statements of the Python while loop are dictated by indentation.
The code block begins when a statement is indented and ends with unindented statement.
Any non-zero number in Python is interpreted as boolean True. None and zero are interpreted as False.
Find below the code example of while loop in Python for printing numbers from 1 to 10. The code is given below:
num = 1 while num<=10: print(num,end=' ') num+=1 |
---|
Output:
1 2 3 4 5 6 7 8 9 10 |
---|
In same way for printing odd numbers between 1 to 20:
num = 1 while num<=20: print(num,end=' ') num+=2 |
---|
Output:
1 3 5 7 9 11 13 15 17 19 |
---|
For printing even numbers between 1 to 20:
num =2 while num<=20: print(num,end=' ') num+=2 |
---|
Output:
2 4 6 8 10 12 14 16 18 20 |
---|
For printing the numbers divisible by either 5 or 7 between 1 to 50:
num =1 while num<51: if num%5==0 or num%7==0: print(num,end=' ') num+=1 |
---|
Output:
5 7 10 14 15 20 21 25 28 30 35 40 42 45 49 50 |
---|
For printing the sum of the squares of first 10 natural numbers:
#initializing summation and a counter for iteration. s=0 c=1 while c<=10: s = c**2 + s c = c + 1 print("The sum of squares is :", s) |
---|
Output:
The sum of squares is : 385 |
---|
Now, find below the code example of while loop in Python to show that the given number is prime number or not.
num = [34, 51, 17, 2, 76, 71, 99] def prime_number(number): condition = 0 iteration = 2 while iteration<=number/2: if number % iteration ==0: condition = 1 break iteration = iteration + 1 if condition==0: print(f"{number} is a prime number") else: print(f"{number} is not a prime number") for i in num: prime_number(i) |
---|
Output:
34 is not a prime number 51 is not a prime number 17 is a prime number 2 is a prime number 76 is not a prime number 71 is a prime number 99 is not a prime number |
---|
In the following example, we will use while loop for printing the multiplication table of a given number:
num = int(input()) counter = 1 print("The multiplication table of :", num) while counter<=10: ans = num*counter print(num, 'X', counter, '=', ans) counter+=1 #expression to increment the counter |
---|
Output:
The multiplication table of : 67 67 X 1=67 67 X 2=134 67 X 3=201 67 X 4=268 67 X 5=335 67 X 6=402 67 X 7=469 67 X 8=536 67 X 9=603 67 X 10=670 |
---|
Now find the code example below to print the square of numbers between 1 to 10..
number=1 while number<=10: print(number, number**2) number+=1 |
---|
Output:
1 1 2 4 3 9 4 16 5 25 6 36 7 49 8 64 9 81 10 100 |
---|
Now the next is code example to determine that every number in a given list is odd/even number:
list_=[1, 6, 3, 5, 4, 8, 9] index = 0 while index<len(list_): element=list_[index] if element%2==0: print(element, ' It is an even number') else: print(element, ' It is an odd number') index+=1 |
---|
Output:
1 It is an odd number 6 It is an even number 3 It is an odd number 5 It is an odd number 4 It is an even number 8 It is an even number 9 It is an odd number |
---|
Now find next code exemple to determine the length of every word in a given list:
list_= ['Seema', 'Amkur', 'Ram', 'Hanumaan', 'Jia'] index = 0 while index<len(list_): element=list_[index] print(len(element)) index+=1 |
---|
Output:
5 5 3 8 3 |
---|
While Loop Multiple Conditions: We must recruit logical operators to combine two or more expressions specifying conditions into a single while loop. Here Python will analyse all the given expressions of conditions.
Here we will construct a while loop with multiple conditions. We are giving two conditions and a 'and' keyword meaning the Loop will execute the statement until both conditions give Boolean 'True' .
num1=17 num2=-12 while num1>5 and num2<-5: #multiple conditions in a single while loop num1-=2 num2+=3 print(num1, num2) |
---|
Output:
15, -9 13, -6 11, -3 |
---|
Let's look another example with 'or' operator:
num1=17 num2=-12 while num1>5 or num2<-5: #multiple conditions in a single while loop num1-=2 num2+=3 print(num1, num2) |
---|
Output:
15, -9 13, -6 11, -3 9, 0 7, 3 5, 6 |
---|
We can combine multiple logical expressions in the while loop, as shown in following example:
num1=12 num2=14 maximum_value=6 counter=0 #multiple conditions in a single while loop while (counter<num1 or counter<num2) and not counter>=maximum_value: print(f"Number of iterations:{counter}") counter+=1 |
---|
Output:
Number of iteration: 0 Number of iteration: 1 Number of iteration: 2 Number of iteration: 3 Number of iteration: 4 Number of iteration: 5 |
---|
Single Statement While Loop: If our while clause consists of one statement, it may be written on the same line as the while keyword.
Syntax of single line while clause:
#Python program to show how to create a single line while loop counter=1 while counter: print('Python While Loops') |
---|
Loop Control Statements: Now we will discuss the loop control statements in detail. one by one in detail:
1. Continue Statement:
It returns the control of the Python interpreter to the beginning of the loop.
Code:
#initiating the loop for string in "While Loops": if string=="o" or string=="i" or string=="e": continue print('Current Letter:', string) |
---|
Output:
Current Letter: W Current Letter: h Current Letter: l Current Letter: Current Letter: L Current Letter: p
|
---|
2. Break Statement: It stops the execution of the loop, when break statement is reached.
Code:
#initiating the loop for string in "While Loops": if string=="o" : break print('Current Letter:', string) |
---|
Output:
Current Letter: W Current Letter: h Current Letter: i Current Letter: l Current Letter: e Current Letter:
|
---|
3. Pass Statement: Pass statement is used to create empty loops. Pass statement is also employed for classes, functions and empty control statement statements.
Code example1: when result is empty, because condition 1 matches:
#Python program to show how pass statement works a=10 b=20 if (a<b): pass else: print("b<a") |
---|
Output:
Code example2: when condition 2 (else statement) is shown:
#Python program to show how pass statement works a=30 b=20 if (a<b): pass else: print("b<a") |
---|
Output:
b<a |
---|