PYTHON PROGRAMMING
LAB
D KISHORE BABU
Assistant Professor
SESHADRI RAO GUDLAVALLERU ENGINEERING COLLEGE
Department Of Mechanical Engineering
Example 1: Add Two Numbers with fixed values
Program
num1 = 1.5
num2 = 6.3
# Add two numbers
sum = num1 + num2
# Display the sum
print ('The sum of 1.5 and 6.3 is’, sum)
output:
Example 2: Add Two Numbers with User Input
Program
# Store input numbers
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 1.5 and 6.3 is’, sum)
output:
Example 3: To find the square root of given number input from
user
Program
#To find the square root of any number
#let take input from user
number=float(input('Enter of any number='))
square_root=number**0.5
print('The sqaure root of given number is:',square_root)
output:
Example 4: To find the area of triangle
Program
# Python Program to find the area of triangle
a = 5
b = 6
c = 7
# Uncomment below to take inputs from the user
# 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)
output:
Example 6: To find roots of quadratic equation
Program
# Python Program to find the roots of quadratic equation
a = 1
b = 5
c = 6
# Find two solutions
sol1 = (-b-(b**2-4*a*c) **0.5)/(2*a)
sol2 = (-b+(b**2-4*a*c) **0.5)/(2*a)
print ('The roots of quadratic equation is:', sol1, sol2)
output:
Example 7: To convert kilometres to miles
Program
# Taking kilometres input from the user
kilometres = float (input ("Enter value in kilometres: "))
# Calculate miles
miles = kilometres * (5/8)
print ('The conversion kilometres to miles is equal to %0.2f'%miles)
output:
Example 8: To convert Celsius to Fahrenheit
Program
#Fahrenheit to celsius conversion
# Take input form user
Fahrenheit = float(input('The temperature in Fahrenheit='))
# Formula for conversion is {F=C*(9/5)+32}
celsius = (Fahrenheit-32)*5/(9)
print('The conversion of celsius to fahrenheit is %0.3f'%celsius)
output:
Example 9: To find the percentage of student marks
Program
#Percentage of student marks
# Take the input from user
input ('Please enter your Roll Number:')
sub1= float (input ('Enter the subject@1 marks:'))
sub2= float(input ('Enter the subject@2 marks:'))
sub3= float(input ('Enter the subject@3 marks:'))
sub4= float(input ('Enter the subject@4 marks:'))
sub5= float(input ('Enter the subject@5 marks:'))
sub6= float(input ('Enter the subject@6 marks:'))
# Percentage calculation formula
Total_marks = sub1+sub2+sub3+sub4+sub5+sub6
Percentage = (Total_marks)/(6)
print('The percentage of given roll number is:',Percentage)
output:
Example 10: To find the distance between two points
Program
output: