Python Programs
1) Program to Add Two Numbers
# Take input
num1 = input('Enter first number: ')
num2 = input('Enter second number: ')
# Add two numbers
sum = float(num1) + float(num2)
# Display the sum
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
2) Program to Calculate and print area and perimeter of a rectangle
l=int(input("Enter the length of the rectangle in centimeter: "))
b=int(input("Enter the breadth of the rectangle in centimeter: "))
a=l*b
p=2*(l+b)
print("Area of this rectangle is", a,"Sq.cm." )
print("Perimeter of this rectangle is", p,"cm.")
3) Program to Find the Square Root
num = float(input('Enter a number: '))
num_sqrt = num ** 0.5
print('The square root of %0.3f is %0.3f'%(num ,num_sqrt))
4) Write a program to input name and marks in 5 subjects of a student.
Calculate his percentage assuming maximum marks for each subject as 100.
name=str(input("Enter your name: "))
eng=int(input("Enter your marks in English out of 100: "))
math=int(input("Enter your marks in Math out of 100: "))
sc=int(input("Enter your marks in Science out of 100: "))
sst=int(input("Enter your marks in SST out of 100: "))
hindi=int(input("Enter your marks in Hindi out of 100: "))
total=eng+math+sc+sst+hindi
print("Dear ",name,"Your total marks out of 500 are", total, "and your percentage
is",total/500*100, "%")
5) Program to Calculate the Area of a Triangle using side lengths
area = √(s(s-a)*(s-b)*(s-c))
a = float(input('Enter first side: '))
b = float(input('Enter second side: '))
c = float(input('Enter third side: '))
# calculate the semi-perimeter
s = (a + b + c) / 2
# calculate the area
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('The area of the triangle is %0.2f' %area)
6) Program to Swap Two Variables
x = input('Enter value of x: ')
y = input('Enter value of y: ')
# create a temporary variable and swap the values
temp = x
x=y
y = temp
print('The value of x after swapping: ', x)
print('The value of y after swapping: ',y)
7) Program to Convert Kilometers to Miles
# Taking kilometers input from the user
km = float(input("Enter value in kilometers: "))
# conversion factor
conv_fac = 0.621371
# calculate miles
miles = km * conv_fac
print('%0.2f kilometers is equal to %0.2f miles' %(km,miles))
#Modify the above program to convert miles to kilometers. (km = miles / conv_fac)
8) Program to Convert Celsius To Fahrenheit (fahrenheit = celsius * 1.8 + 32)
celsius = float(input("Enter the temperature in celcius"))
fahrenheit = (celsius * 1.8) + 32
print('%0.1f degree Celsius is equal to %0.1f degree Fahrenheit'
%(celsius,fahrenheit))
# Modify the above program to convert Fahrenheit to celcius. (celsius = (fahrenheit -
32) / 1.8)
9) Program to Check if a Number is Odd or Even
# A number is even if division by 2 gives a remainder of 0.
# If the remainder is 1, it is an odd number.
num = int(input("Enter a number: "))
if (num % 2) == 0:
print("Number is Even")
else:
print("Number is Odd")
10) Program to Check if a Number is Positive, Negative or 0
num = float(input("Enter a number: "))
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
11) Program to Find the Largest Among Three Numbers
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))
if (num1 >= num2) and (num1 >= num3):
largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3
print("The largest number is", largest)
12) Write a program to display the name of the day based on the
number given by the user.
day=int(input("Enter a no. between 1 to 7: "))
if day==1:
print("Its Monday")
elif day==2:
print("Its Tuesday")
elif day==3:
print("Its Wednesday")
elif day==4:
print("Its Thursday")
elif day==5:
print("Its Friday")
elif day==6:
print("Its Saturday")
elif day==7:
print("Its Sunday")
else:
print("Enter a valid number")
13) Print First 10 natural numbers using while loop
i=1
while i <= 10:
print(i)
i += 1
14) Print First 10 natural numbers using for loop
for i in range(1,11,1):
print(i)
15) Print First 10 natural numbers in reverse order using while loop
i = 10
while i >= 1:
print(i)
i -= 1
16) Program to print the table of a given number using while loop
num = int(input("Display multiplication table of? "))
i=1
# Iterate 10 times from i = 1 to 10
while i<=10:
print(num, 'x', i, '=', num*i)
i+=1
17) Program to print the table of a given number using for loop
num = int(input("Display multiplication table of? "))
# Iterate 10 times from i = 1 to 10
for i in range(1, 11):
print(num, 'x', i, '=', num*i)
18) Print First 10 natural numbers in reverse order using for loop
for i in range(10,0,-1):
print(i)
19) Program to Find the Sum of Natural Numbers
num = int(input("Enter a number: "))
if num < 0:
print("Enter a positive number")
else:
sum = 0
# use while loop to iterate until zero
while(num > 0):
sum += num
num -= 1
print("The sum is", sum)
20) Program to find number of digits
print(end="Enter the Number: ")
num = int(input())
tot = 0
while num:
num = int(num/10)
tot = tot+1
if tot>1:
print("\nThere are " +str(tot)+ " digits in this number")
elif tot==1:
print("\nIt is a single digit number")