The document contains 16 Python programs that perform basic calculations and operations. Program 1 takes a user input message and prints it. Program 2 takes 3 user input numbers and calculates their sum. Program 3 takes length and breadth values and calculates the area of a rectangle. The remaining programs perform calculations like BMI, conversions between units, swapping values, finding averages, and other arithmetic operations.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
32 views
Python Fundamentals Book Programs
The document contains 16 Python programs that perform basic calculations and operations. Program 1 takes a user input message and prints it. Program 2 takes 3 user input numbers and calculates their sum. Program 3 takes length and breadth values and calculates the area of a rectangle. The remaining programs perform calculations like BMI, conversions between units, swapping values, finding averages, and other arithmetic operations.
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5
Python Fundamentals Book programs
Program 1 = Write a program to input a welcome message and print it
INPUT:
message = input(“Enter a welcome message : “)
print(“Hello,”,message)
Program 2 = Program to obtain three numbers and print their sum
INPUT:
num1 = int(input(“Enter number 1 : “))
num2 = int(input(“Enter number 2 : “)) num3 = int(input(“Enter number 3 : “)) Sum = num1 + num2 + num3 print(“Three numbers are : “, num1 , num2 , num3 ) print(“Sum is :”,Sum)
Program 3 = Program to obtain length and breadth of a rectangle and
calculate its area INPUT:
length = float(input("Enter the length of the rectangle: "))
breadth = float(input("Enter the breadth of the rectangle: ")) area = length * breadth print("The area of the rectangle is {area} square units.")
Program 4 = Program to calculate BMI of a person
INPUT:
weight = float(input("Enter your weight in kilograms: "))
height = float(input("Enter your height in meters: ")) bmi = weight / (height ** 2) print("Your Body Mass Index (BMI) is bmi") Program 5 = Write a program to input a number and print its cube. INPUT:
number = float(input("Enter a number: "))
cube = number ** 3 print(“The cube is = “,cube)
Program 6 = Write a program to input a value in kilometres and
convert into miles. INPUT:
km = int(input(“Enter kilometres : “))
miles = km * 0.621371 print( “kilometres = “) print(“Miles = “, miles)
Program 7 = Write a program to input a value in tonnes and convert it
Program 8 = Write a program to input two numbers and swap them.
INPUT:
n1= int( input("Enter number 1:"))
n2 = int( input("Enter number 2 :")) print ("Original numbers :", n1, n2) n1, n2 = n2, n1 print ("After swapping :", n1, n2) Program 9 = Write a program to input three numbers and Swap them as this : Ist number becomes the 2nd number, 2nd number becomes the 3rd number and the 3rd number becomes the first number. INPUT:
x = int (input("Enter number 1 :"))
y = int ( input("Enter number 2:")) z = int ( input(" Enter number 3:")) print("Original numbers:", x, y, z) x, y, z = y, z, x print(""After swapping:, x, y, z)
Program 10 = Write a program to enter a small poem or poem verse
and print it. INPUT:
poem = input("Enter a small poem: ")
print(" The poem you entered") print(poem)
Program 11 = Write a program to obtain temperature in Celsius and
convert it into Fahrenheit. INPUT:
Cels = float (input( "Enter temp in Celsius :") )
print ("Temperature in Celsius is :", Cels) Fahr = Cels * 9/5 +32 print ("Temperature in Fahrenheit is:", Fahr)
Program 12 = Write a program to input three numbers and szwap
them as this : 2nd number gets the value of 1st + 2nd number, 3rd number gets the value of 2nd +3rd nunber. INPUT:
x = int (input ("Enter number 1: "))
y= int (input ("Enter number 2 :")) z = int(input ("Enter number 3:")) print("Original numbers :", x, y, z) x, y, z = x, x+y, x+y+z print ("After swapping :, x, y, z) Program 13 = Write a program to calculate in how many days a work will be completed by three persons A, B and C together. A, B, C take x days, y days and z days respectively to do the job alone. The formula to calculate the number of days if they work together is xyz/(xy + yz+ xz) days where x, y, and z are given as input to the program. INPUT:
x = int(input("Enter value of x: "))
y = int(input("Enter value of y : ")) z = int (input("Enter value of z: ")) print (" Individually, A, B and C take days respectively as :", x, y, z) days = (x*y*z)/ (x*y + y*z + x*z) print(" Together A, B and C take days as :", days)
Program 14 = Write a program to enter two integers and perform all
arithmetic operations on them. INPUT:
x = int (input (""Enter value 1 :"))
Y = int(input ("Enter value 2 :")) summ = x+ y diff = x - y mult = x *y div = x/y mod = x % y print ("Numbers are :", x, y) print("'Sum =",summ) print("Difference = “,diff) print ("Product =", mult) print ("Division =", div) print ("Modulus =", mod) Program 15 =Write a program to swap two numbers using a third variable. INPUT:
x= int (input ("Enter value 1 :"))
y= int(input ("Enter value 2 :")) print( "Original Numbers are:", x, y) t=x x =y y=t print( "After Swapping, Numbers are :", x, y)
Program 16 =Write a program to find average of three numbers.
INPUT:
x = int (input (""Enter value 1 :"))
y= int(input ("Enter value 2 :")) z = int(input ("Enter value 3 :")) print (" Three numbers are:", x, y, z) avg = (x + y+ z) /3 print("Average of given numbers :", avg)