While loop is a versatile loop in Python used for repeated execution of a block of statements. The number of times this set of statements is executed depends on the condition expression of While Loop. The block of statements of the while loop are executed for the duration the condition is TRUE. As soon as the condition expression evaluates to FALSE, loop terminates. Here are 10 basic While Loop Programs in Python for absolute beginners.
1. Print prime numbers between 1 and 100
This program prints all the prime numbers between 1 and 100. Nested while loops are used in this program.
#initialize variables for loop i=1 p=0 while i<=100: k=2 while k<i: if (i%k==0): p=1 break k=k+1 if (p==0): print(i) p=0 i=i+1
2. Print Factorial of a number entered by user.
In the list of While Loop Programs in Python, this program asks user for a number and prints its factorial.
value=int(input("Enter a number")) fact=1 i=1 while value>=1: fact=fact*value value=value-1 print(fact)
3. Write a While Loop program in Python to print table of multiples of a given number
This program asks user to enter a number and prints its table of first 10 multiples
value=int(input("Enter a number")) i=1 while i<=10: print(str(value)+"X"+str(i)+"="+str(value*i)) i=i+1
4. Print Even Numbers using While Loop in Python
This program accepts a number from user and prints all even numbers in the range of 1 and the user given number
value=int(input("Enter a number")) i=1 while i<=value: if (i%2==0): print(i) i=i+1
5. Numbers divisible by 5 in a given range from 1
This program accepts a number from user and prints all the numbers divisible by 5 starting from 1.
value=int(input("Enter a number")) i=1 while i<=value: if (i%5==0): print(i) i=i+1
6. Count Vowels in a given string using While Loop.
This program counts vowels in a string entered by a user and displays its count.
value=input("Enter a sring") i=0 count=0 while i<len(value): if value[i] in("a","e","i","o","u"): count=count+1 i=i+1 print(count)
7. Check if a string is Palindrome
This program accepts a string and checks if it is a palindrome.
value=input("Enter a sring") i=len(value)-1 j=0 # initalise two strings rev="" act="" #initialise end index of second half end=0 if len(value)%2==0: end=len(value)/2 else: end=len(value)/2-1 #extract first half while j<len(value)/2: act=act+value[j] j=j+1 #extract second half while i>=end: rev=rev+value[i] i=i-1 #compare two halves if act==rev: print(value +" is a palindrome") else: print(value +" is not a palindrome")
8. Print Fibonacci series for a number
This program accepts a number and prints its Fibonacci series
#Read the value value=int(input("How many Fibonacci numbers to print")) # initialize variables anc=0 par=1 i=0 #print print(anc) print(par) cur=par print(cur) #repeat loop to generate next fibonacci number while i<=value: anc=par par=cur cur=anc+par print(cur) i=i+1
9. Print Sum of Squares
This program reads a number and prints the sum of squares using While Loop in Python
#enter a number value=int(input("Enter a number")) # initialize variables i=0 total=0 #repeat loop to print squares of numbers while i<=value: total=total+i*i i=i+1 print(total)
10. Print Reverse of a string
This program accepts a string from user and prints its reverse.
#Read the string value=input("Enter a string") # initialize variables rev="" i=len(value)-1 #repeat loop to print last to first character while i>=0: rev=rev+value[i] i=i-1 print(rev)
Be First to Comment