PYTHON while loop
Iteration (or) Looping constructs
An Iteration or loop statement allows executing repeatedly a statement or group of statements multiple
times till the condition is satisfied.
while loop
The while loop is an entry check control loop.
The while loop has only test condition and also true block part and also may (or) may not else
block part.
In “while” loop, we can execute it even if we are not aware (known) of the number of iterations.
Syntax 1:
while condition/Boolean :
Statements block
Syntax 2:
while condition/Boolean :
Statements block 1
else:
Statements block2
o In the while loop, the condition is any valid Boolean expression returning True or False.
o The else part of while is optional part of while.
o The statements block1 is kept executed till the condition is True.
o If the else part is written, it is executed when the condition is tested False.
Example1
Output
By E. V. VIJAYARAJ M.Sc (Maths) B.Ed., M.C.A., M.Phil., D.C.S.Engg., Page 1
PYTHON while loop
Example2
Output
Example for application
Example for String Extraction
By E. V. VIJAYARAJ M.Sc (Maths) B.Ed., M.C.A., M.Phil., D.C.S.Engg., Page 2
PYTHON while loop
Example for Digit Extraction
Example2 for Digit Extraction
By E. V. VIJAYARAJ M.Sc (Maths) B.Ed., M.C.A., M.Phil., D.C.S.Engg., Page 3
PYTHON while loop
Output
Example1a:
By E. V. VIJAYARAJ M.Sc (Maths) B.Ed., M.C.A., M.Phil., D.C.S.Engg., Page 4
PYTHON while loop
Example for print even numbers from 1 to n
Exercises
By E. V. VIJAYARAJ M.Sc (Maths) B.Ed., M.C.A., M.Phil., D.C.S.Engg., Page 5
PYTHON while loop
1) print n numbers those are not divisible by 3
2) print n numbers those are divisible by 3
3) print n numbers those are divisible both by 3 and 6
4) print n numbers those are divisible either by 3 or 6
5) Check given integer number whether Palindrom or not
6) Sum of given digit : 543217
7) Multiplication table for nth table
8) Check Armstrong number
(eg)153 = 13+53+33
By E. V. VIJAYARAJ M.Sc (Maths) B.Ed., M.C.A., M.Phil., D.C.S.Engg., Page 6