Table of Contents
hide
While Loop Examples
Example : A Python program to print all the numbers from 1 to 10 using a While loop.
i = 1
while i <= 10:
print(i)
i = i + 1
Output:
1
2
3
4
5
6
7
8
9
10
------------- OR -------------
i = 1
while i <= 10:
print(i, end=" ")
i = i + 1
Output:
1 2 3 4 5 6 7 8 9 10
------------- OR -------------
x=int(input("Enter First Value = "))
y=int(input("Enter Second Value = "))
i = x
while i <= y:
print(i, end=" ")
i = i + 1
Output:
Enter First Value = 15
Enter Second Value = 35
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
Example : A Python program to print all the numbers along with their total from 1 to 10 using a While loop.
i = 1
sum = 0
while i <= 10:
print(i, end=" ")
sum = sum + i
i = i + 1
else:
print("\n")
print("The sum total is = ", sum)
Output:
1 2 3 4 5 6 7 8 9 10
The sum total is = 55
---------------- OR ----------------
i = 1
sum = 0
while i <= 10:
print(i, end=" ")
sum = sum + i
i = i + 1
print("\n")
print("The sum total is = ", sum)
Output:
1 2 3 4 5 6 7 8 9 10
The sum total is = 55
num = int(input("Enter a number: "))
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum = sum + (digit ** 3)
# sum = sum + (digit * digit * digit)
temp = temp//10
if num == sum:
print(num, "is an Armstrong number")
else:
print(num, "is not an Armstrong number")
Output:
Enter a number: 153
153 is an Armstrong number
Example : A Python program to print all the numbers from 1 to 10 except 5 using the Continue statement.
i = 0
while i < 10:
i = i + 1
if i == 5:
continue
print(i, end=" ")
Output:
1 2 3 4 6 7 8 9 10
Example : A Python program to print all the numbers before 5 from 1 to 10 using a Break statement.
i = 1
while i <= 10:
if i == 5:
break
print(i, end=" ")
i = i + 1
Output:
1 2 3 4
Example : A Python program to receive User Inputs after confirmation.
while True:
x = int(input("Enter First nO. = "))
y = int(input("Enter Second nO. = "))
z = x + y
print("The addition result is = ",z)
option = input("To quit, Type N or n otherwise any character = ")
if option == 'n' or option == 'N':
break
Output:
Enter First nO. = 40
Enter Second nO. = 50
The addition result is = 90
To quit, Type N or n otherwise any character = n
For Loop Examples
Example : A Python program to print all the values using For loop.
values = [5, 25, 30, 50, 65]
for x in values:
print(x, end=" ")
Output:
5 25 30 50 65
-------------- OR ---------------
fruits = ["guava", "orange", "apple", "banana", "mango"]
for i in fruits:
print(i, end=" ")
Output:
guava orange apple banana mango
Example : A Python program to find out the Factorial of user-accepted value using a For loop.
num = int(input("Enter a number for factorial : "))
fact = 1
for i in range(1, num + 1):
fact = fact * i
print("Factorial of", num, "is:", fact)
Output:
Enter a number for factorial : 5
Factorial of 5 is: 120
Example : A Python program to find out the Sum Total of user-accepted values using a For loop.
num = int(input("Enter a number for sum : "))
sum = 0
for i in range(1, num + 1):
sum = sum + i
print("The sum of all the values from 1 to", num, "are = ", sum)
Output:
Enter a number for sum : 10
The sum of all the values from 1 to 10 are = 55
------------- OR -------------
num1 = int(input("Enter first number for sum : "))
num2 = int(input("Enter second number for sum : "))
sum = 0
for i in range(num1, num2 + 1):
sum = sum + i
print("The sum of all the values from", num1, "to", num2, "are = ", sum)
Output:
Enter first number for sum : 10
Enter second number for sum : 12
The sum of all the values from 10 to 12 are = 33
Example : A Python program to print all the Fibonacci Series values up to given terms using a For loop.
number = int(input("Enter the number of terms to display series : "))
a = 0
b= 1
#print (a, end=" ")
#print (b, end=" ")
print (a,b, end=" ")
for i in range(1, number-1):
c=a+b
print(c, end=" ")
a=b
b=c
Output:
Enter the number of terms to display series : 10
0 1 1 2 3 5 8 13 21 34
----------- OR -----------
number = int(input("Enter the number upto/below which you want to display series : "))
a = 0
b= 1
#print (a, end=" ")
#print (b, end=" ")
print (a,b, end=" ")
for i in range(1, number-1):
c=a+b
if c>number:
break
else:
print(c, end=" ")
a=b
b=c
Output:
Enter the number upto which you want to display series : 50
0 1 1 2 3 5 8 13 21 34
Example : A Python program prints all the values along with its total using a For loop.
sum = 0;
values = [5, 25, 30, 50, 65]
for x in values:
sum = sum + x
print(x, end=" ")
else:
print("\n")
print(sum, end=" ")
Output:
5 25 30 50 65
175
------------- OR -------------
sum = 0;
values = [5, 25, 30, 50, 65]
for x in values:
sum = sum + x
print(x, end=" ")
print("\n")
print(sum, end=" ")
Output:
5 25 30 50 65
175
Example : A Python program to check the values are Prime or Not using a For loop.
num = int(input("Enter a number: "))
count=0
if num > 1:
for i in range(2, num + 1):
if num % i == 0:
count=count+1
if count == 1:
print(num,"is a Prime Number")
else:
print(num, "is not a Prime Number")
else:
print(num, "is not a prime number")
Output:
Enter a number: 20
20 is not a Prime Number
Example : A Python program to print some values with a Break statement using a For loop.
sum = 0;
values = [5, 25, 30, 50, 65]
for x in values:
if x == 50:
break
print(x, end=" ")
Output:
5 25 30
Example : A Python program to print some values with a Continue statement using a For loop.
sum = 0;
values = [5, 25, 30, 50, 65, 200]
for x in values:
if x == 50:
continue
print(x, end=" ")
Output:
5 25 30 65 200
Example : A Python program to print the number using range() in For loop.
for i in range(10):
print(i, end = ' ')
Output:
0 1 2 3 4 5 6 7 8 9
------------- OR -------------
for i in range(10):
print(i)
Output:
0
1
2
3
4
5
6
7
8
9
------------- OR -------------
for x in range(1,11):
print(x, end=" ")
Output:
1 2 3 4 5 6 7 8 9 10
------------- OR -------------
for i in range(3, 10):
print(i, end = ' ')
Output:
3 4 5 6 7 8 9
------------- OR -------------
for x in range(5, 50, 4):
print(x, end=" ")
Output:
5 9 13 17 21 25 29 33 37 41 45 49
------------- OR -------------
for i in range(15, 3, -1):
print(i, end = ' ')
Output:
15 14 13 12 11 10 9 8 7 6 5 4
------------- OR -------------
for i in range(15, 3, -4):
print(i, end = ' ')
Output:15 11 7
------------- OR -------------
for i in range(1, 5):
for j in range(i):
print(i, end=' ')
print()
Output:
1
2 2
3 3 3
4 4 4 4
------------- OR -------------
for str in 'Welcome':
if str == 'l' or str == 'e' :
continue
print(str, end='')
Output:
Wcom
------------- OR -------------
for str in 'Hello India':
pass
print(str, end='')
Output:
a
NB: 'pass' statements skip all letters except last letter.
Some Important links:
0 Comments