1️⃣ while Loop
while loop will execute the statements defined under the while-loop-block, as long as the while-condition remains True.
All statement under while loop should have same indentation.
Lets see the syntax of while loop:
1 2 3 4 5 6 7 8 9 10 11 12 | # -- syntax of while loop --
while condition:
# statements inside while loop
statement1
statement2
statement3
# statement to control condition
control_statement
# statements outside while loop
statement4
statement5
|
As above statement1 to statement3 falls under while loop as they are grouped together with same indentation (4spaces) under the loop. The statement4 won’t fall under the while loop, as it is not part of while loop.
Examples of while loop:
Lets use while loop for printing sequences.
💻 Example 1:
1 2 3 4 5 6 | # -- Lets print the numbers sequentilly from 1 to 5 --
n=1
while n<=5:
print(n)
n+=1
|
✅ Output:
1
2
3
4
5
Lets print the numbers in reverse order from 10 to 5
💻 Example 2:
1 2 3 4 | n=10
while n>=5:
print(n)
n-=1
|
✅ Output:
10
9
8
7
6
5
💻 Example 3: Lets append the numbers sequentilly (1 to 10) to a list
1 2 3 4 5 6 7 8 9 10 | n=1
numbers = []
while n<=10:
numbers.append(n)
print(numbers)
n+=1
print('I am outside of while')
print(n)
|
✅ Output:
[1]
[1, 2]
[1, 2, 3]
[1, 2, 3, 4]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6, 7]
[1, 2, 3, 4, 5, 6, 7, 8]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
I am outside of while
11
💻 Example 4: Guess the number app using while loop, Lets Play !
1 2 3 4 5 6 7 8 9 10 11 12 | # lucky numbers list
lucky_nums = [100, 5000,55,77,88,99]
print("Welcome to Guess the number App!")
input_num = int(input("Enter your lucky number:"))
# membership operator use in while condition
while input_num not in lucky_nums:
print("Better luck next time!!!")
input_num = int(input("Enter your lucky number:"))
print("Congrats !!! You have guessed correct number...")
|
✅ Output:
Welcome to Guess the number App!
Enter your lucky number:44
Better luck next time!!!
Enter your lucky number:1000
Better luck next time!!!
Enter your lucky number:45
Better luck next time!!!
Enter your lucky number:88
Congrats !!! You have guessed correct number...
💻 Example 5: Enter the correct password to login TechnoDexterous.
1 2 3 4 5 6 7 8 9 | correct_pwd = 'hello'
input_pwd = input('Enter your password:')
while input_pwd!=correct_pwd:
print("Sorry, Wrong Password!")
input_pwd = input('Enter your password:')
print("Welcome to TechnoDexterous!")
print("Learn Python from Home")
|
✅ Output:
Enter your password:abcd123
Sorry, Wrong Password!
Enter your password:Hello
Sorry, Wrong Password!
Enter your password:hello
Welcome to TechnoDexterous!
Learn Python from Home
Pattern Printing using while loop:
Lets draw triangle pattern using (*)
💻 Example 1: Triangle
1 2 3 4 5 6 | n=1
while n<=7:
# string multiplication
print('* '*n)
# control statement
n+=1
|
✅ Output:
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
💻 Example 2: Reverse Triangle
1 2 3 4 5 | n=7
while n>=1:
print('* '*n)
# control statement
n-=1
|
✅ Output:
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
Lets draw 7x7 sqare using (*)
💻 Example 3: 7x7 Square
1 2 3 4 | n=1
while n<=7:
print('* '*7)
n+=1
|
✅ Output:
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
In this way, string multiplication with a proper number results triangle and sqare patterns inside while loop. Lets try to draw sqare pattern with core logic that means, we should print the starts one by one without using string operations.
Lets draw 7x7 sqare using nested while loop
💻 Example 3: 7x7 Square
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | '''Lets draw the 7X7 Square using
Nested While loop'''
import time
n=1
j=1
# 1st while: it takes the cursor to next line
while j<=7:
n = 1
# 2nd while: it puts * one by one in each row
while n<=7:
print('* ', end=' ')
# we have kept 0.5 second pause to understand the output well
time.sleep(0.5)
# control inner while
n+=1
print()
# control outer while
j+=1
|
✅ Output:
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
Be prepared with below questions prior to your interview !
Frequently asked Interview Questions
Write a basic while loop using Python and explain.
How do you terminate a while loop?
What is the purpose of a while loop?
What is the difference between a while loop and a for loop?
Can you have multiple conditions in a while loop?
How do you handle infinite loop condition in while?
Can you nest a while loop inside another while loop?
Write a python program to draw a star pattern of triangle and sqaure using a while loop.