Python Lab Questions
LAB 1
Converting Miles to Kilometers:
miles = float(input("Enter distance in miles: "))
kilometers = miles * 1.60934
print(f"{miles} miles is equal to {kilometers} kilometers")
Convert Temperature from Celsius to Fahrenheit:
celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = (celsius * 9/5) + 32
print(f"{celsius}°C is equal to {fahrenheit}°F")
Find the Distance Traveled by an Object:
speed = float(input("Enter speed (in meters/second): "))
time = float(input("Enter time (in seconds): "))
distance = speed * time
print(f"The object has traveled {distance} meters")
Swapping Values Stored in Two Variables:
x = input("Enter the first value: ")
y = input("Enter the second value: ")
x, y = y, x
print(f"After swapping: x = {x}, y = {y}")
Area of circle and Circumference
radius = float(input("Enter the radius of the circle: "))
pi = 3.14159
area = pi * radius * radius
circumference = 2 * pi * radius
print(f"Area of the circle: {area:.2f}")
print(f"Circumference of the circle: {circumference:.2f}")
LAB 2
Find the Sum of n Natural Numbers:
n = int(input("Enter a number: "))
sum_natural = n * (n + 1) // 2
print(f"The sum of first {n} natural numbers is {sum_natural}")
Find the Sum of Squares of n Natural Numbers:
n = int(input("Enter a number: "))
sum_squares = n * (n + 1) * (2 * n + 1) // 6
print(f"The sum of squares of first {n} natural numbers is {sum_squares}")
Print the Digit at Ones Place of a Given Number:
number = int(input("Enter a number: "))
ones_place = number % 10
print(f"The digit at ones place is {ones_place}")
Calculate the Square of a Number:
number = float(input("Enter a number: "))
square = number * number
print(f"The square of {number} is {square}")
Calculate the Simple Average of Two Numbers:
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
average = (num1 + num2) / 2
print(f"The average of {num1} and {num2} is {average}")
LAB 3
Find the Remainder of a Division:
dividend = float(input("Enter the dividend: "))
divisor = float(input("Enter the divisor: "))
remainder = dividend % divisor
print(f"The remainder when {dividend} is divided by {divisor} is
{remainder}")
Convert Minutes into Hours and Minutes:
total_minutes = int(input("Enter time in minutes: "))
hours = total_minutes // 60
minutes = total_minutes % 60
print(f"{total_minutes} minutes is equal to {hours} hours and {minutes}
minutes")
Calculate the Perimeter of a Rectangle:
length = float(input("Enter the length of the rectangle: "))
width = float(input("Enter the width of the rectangle: "))
perimeter = 2 * (length + width)
print(f"The perimeter of the rectangle is {perimeter}")
Calculate the Product of Two Numbers:
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
product = num1 * num2
print(f"The product of {num1} and {num2} is {product}")
Find the Last Two Digits of a Given Number:
number = int(input("Enter a number: "))
last_two_digits = number % 100
print(f"The last two digits of {number} are {last_two_digits}")
Find the Simple Interest:
principal = float(input("Enter the principal amount: "))
rate = float(input("Enter the rate of interest: "))
time = float(input("Enter the time period in years: "))
interest = (principal * rate * time) / 100
print(f"The simple interest is {interest}")
Convert Hours into Days, Hours, and Minutes:
hours = int(input("Enter time in hours: "))
days = hours // 24
remaining_hours = hours % 24
print(f"{hours} hours is equal to {days} days and{ remaining_hours}
hours")
LAB 4
Find the Largest of Three Numbers:
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
c = float(input("Enter third number: "))
if a >= b and a >= c:
largest = a
elif b >= a and b >= c:
largest = b
else:
largest = c
print(f"The largest number is {largest}")
Check if Two Numbers are Equal:
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
if num1 == num2:
print("Both numbers are equal")
else:
print("The numbers are not equal")
Check if a Number is Positive, Negative, or Zero:
number = float(input("Enter a number: "))
if number > 0:
print("The number is positive")
elif number < 0:
print("The number is negative")
else:
print("The number is zero")
Check Whether the Given Number is Odd or Even:
number = int(input("Enter a number: "))
if number % 2 == 0:
print(f"{number} is even")
else:
print(f"{number} is odd")
Check if a Number is Divisible by Another:
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
if num1 % num2 == 0:
print(f"{num1} is divisible by {num2}")
else:
print(f"{num1} is not divisible by {num2}")
Find the Total and Average of Marks Scored by a Student:
num_subjects = int(input("Enter the number of subjects: "))
mark1 = int(input("Enter mark for subject 1: "))
if num_subjects > 1:
mark2 = int(input("Enter mark for subject 2: "))
if num_subjects > 2:
mark3 = int(input("Enter mark for subject 3: "))
if num_subjects > 3:
mark4 = int(input("Enter mark for subject 4: "))
if num_subjects > 4:
mark5 = int(input("Enter mark for subject 5: "))
if num_subjects == 1:
total_marks = mark1
elif num_subjects == 2:
total_marks = mark1 + mark2
elif num_subjects == 3:
total_marks = mark1 + mark2 + mark3
elif num_subjects == 4:
total_marks = mark1 + mark2 + mark3 + mark4
elif num_subjects == 5:
total_marks = mark1 + mark2 + mark3 + mark4 + mark5
average_marks = total_marks / num_subjects
print(f"Total Marks: {total_marks}")
print(f"Average Marks: {average_marks:.2f}")