Python While Loop
Python While Loop
Naveen kumar
While Loop
➢ while loop : Executes a group of statements as long as condition is True
➢ Good for indefinite loops (repeat an unknown number of times)
➢ Syntax:
while condition:
statements
➢ Example:
# output
1 2 3 4
Example:
# Add the digits of integer number
n = 1234
temp_n = 0 # initialize the value
while n > 0: # condition
rem = n%10 # reminder of number
temp_n = temp_n + rem # add initialized value + reminder
n = n//10 # divide by 10 to eliminate the last value
print(“Addition of 1234 is ”, temp_n)
Exercise
➢ Write a program to print even numbers between 5 and 20
➢ Write a program to print odd numbers between 5 and 20
➢ Write a program to convert decimal to binary of the given number
➢ Write a program to count number of digits of the given number