Python Programs for Practice
1. Python Program to Find Armstrong Number in an Interval
lwr = 100
uppr = 2000
for numb in range(lwr, uppe + 1):
# order of number
odr = len(str(numb))
# initialize sum
sum = 0
tem = numb
while tem > 0:
digit = tem % 10
sum += digit ** odr
tem //= 10
if numb == sum:
print("The Armstrong numbers are: ",numb)
2. Python Program to Find the Largest Among Three Numbers:
def maximum(x, y, z):
if (x >= y) and (x >= z):
largest = x
elif (y >= x) and (y >= z):
largest = y
else:
largest = z
return largest
3. Python Program to Find the Factors of a Number
numb=int(input("enter any number"))
facts=[]
for a in range(1,numb+1):
if numb%a==0:
facts.append(a)
print ("Factors of {} = {}".format(numb,facts))
4. Python Program to Find the Factors of a Number
numb=int(input("enter any number"))
facts=[]
for a in range(1,numb+1):
if numb%a==0:
facts.append(a)
print ("Factors of {} = {}".format(numb,facts))
5. Python Program to Find the Square Root
numb = float(input('Enter a number: '))
numsqrt = numb ** 0.5
print('square root of %0.3f is %0.3f'%(numb ,numsqrt))
6. Python Program to Display the Multiplication Table
numb = int(input(" Enter a number : "))
# using the for loop to generate the multiplication tables
print("Table of: ")
for a in range(1,11):
print(num,'x',a,'=',num*a)
7. Python Program to Check if a Number is Positive, Negative, or 0
n = float (input (“Enter the number: “))
if n > 0:
print (“The number provided is positive”)
elif n == 0:
print (“The number input is zero”)
else:
print (“The number provided is negative”)
8. Python Program to Print all Prime Numbers in an Interval
lwr = 900
upr = 1000
print("Prime numbers between", lwr, "and", upr, "are:")
for numb in range(lwr, upr + 1):
# all prime numbers are greater than 1
if numb > 1:
for a in range(2, numb):
if (numb % a) == 0:
break
else:
print(numb)
9. Python Program to Check Leap Year
year = int (input (“Enter any year that is to be checked for leap year: “))
if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
print (“The given year is a leap year”)
else:
print (“It is not a leap year”)
else:
print (“It is not a leap year”)
else:
print (“It is not a leap year”)
10. Python Program to Swap Two Variables
var1 = input (“Enter the first variable”)
var2 = input (“Enter the second variable”)
temp = var1
var1 = var2
var2 = temp
print (“The value of the first variable after swapping is:”, var1)
print (“The value of the second variable after swapping is:”, var2)