While Statement

While Loop Statement


The syntax of while loop in Python has the following syntax:

Syntax:

while <condition>:
statements block 1
[else:
statements block2]

In the while loop, the condition is any valid Boolean expression returning True or False. The else part of while is optional part of while. The statements block1 is kept executed till the condition is True. If the else part is written, it is executed when the condition is tested False. Recall while loop belongs to entry check loop type, that is it is not executed even once if the condition is tested False in the beginning.

Example : program to illustrate the use of while loop - to print all numbers from 10 to 15

i=10 # intializing part of the control variable
while (i<=15): # test condition
print (i,end='\t') # statements - block 1
i=i+1 # Updation of the control variable

Output:

10 11 12 13 14 15

Example : program to illustrate the use of while loop - with else part

i=10 # intializing part of the control variable
while (i<=15): # test condition
print (i,end='\t') # statements - block 1
i=i+1 # Updation of the control variable
else:
print ("\nValue of i when the loop exit ",i)

Output: 

10 11 12 13 14 15

Value of i when the loop exit 16





Python : While Statement Program List




Program [1]

Write a program to input any number and to find reverse of that number.


View Solution


Program [2]

Write a program to input any string and count number of uppercase and lowercase letters.


View Solution


Program [3]

Write a program to accept a integer number from user and find its sum of digit.

Sample 1:

Input : 123

Output : Sum of Digit : 6

Sample 2:

Input : 1234

Output : Sum of Digit : 10


View Solution


Program [4]

Write a python program to accept a integer number and count the number of digits in number.

Sample:

Input: 2223

Output: Number of Digits: 4


View Solution


Program [5]

Write a python program to accept a integer number form user and find the largest digit of number.

Sample:

Input : 12341

Output : Largest Digit is : 4


View Solution


Program [6]

Write a python program to accept a integer number form user and find the smallest digit of number.

Sample:

Input : 12341

Output : Smallest Digit is : 1


View Solution


Program [7]

Write a python program to accept an integer number to find out and print the difference between greatest and smallest digits presents in the number .



View Solution


Program [8]

Write a python program to accept an integer number and print the frequency of each digit present in the number .

Sample:

Input : 3255435

Output: 

Digit Frequency

2 1

3 2

4 1

5 3


View Solution


Program [9]

Write a program to input any number and to check whether given number is Armstrong or not.

Example:

Armstrong 153,

1^3+5^3 +3^3 =(1+125+27)=153


View Solution


Program [10]

Write a program to convert decimal number to binary.


View Solution


Program [11]

Write a program to convert binary to decimal.


View Solution


Program [12]

Given a list of integers, write a program to find those which are palindromes. For example, the number 4321234 is a palindrome as it reads the same from left to right and from right to left.


View Solution


Program [13]

Write a complete Python program to do the following :


(i) read an integer X.


(ii) determine the number of digits n in X.


(iii) form an integer Y that has the number of digits n at ten's place and the most significant digit of X at one's place.


(iv) Output Y.


(For example, if X is equal to 2134, then Y should be 42 as there are 4 digits and the most significant number is 2).


View Solution


Program [14]

Write a Python program to input 2 numbers and fin the LCM.


View Solution


Program [15]

Write a Python program to input two number and find the G.C.D


View Solution


CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level NIELIT Study material and Quiz Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Website development Company in Allahabad