Python Worksheet for practice
1. Write a Python program to find out what version of Python you are using.
Ans: You can find out what version of Python you are using by typing
`python --version` in the terminal. If you have more than one Python
version installed, you can specify the version number after python. You can
also use the platform library of Python and the python_version() function to
get the version number in a script.
2. Write a Python program that calculates the area of a circle based on the
radius entered by the user.
Ans: PI = 3.14
radius = float(input("Enter the radius of a circle: "))
area = PI * radius * radius
print("Area of a circle = %.2f" % area)
3. Write a Python program that accepts the user's first and last name and prints
them in reverse order with a space between them.
Ans: first_name = input("Enter your first name: ")
last_name = input("Enter your last name: ")
print(last_name + " " + first_name)
4. Write a Python program that accepts a sequence of comma-separated
numbers from the user and generates a list and a tuple of those numbers.
Sample data: 3, 5, 7, 23 Output:
List : ['3', ' 5', ' 7', ' 23']
Tuple : ('3', ' 5', ' 7', ' 23')
Ans: numbers = input("Enter comma-separated numbers: ")
list_of_numbers = numbers.split(",")
tuple_of_numbers = tuple(list_of_numbers)
print("List : ", list_of_numbers)
print("Tuple : ", tuple_of_numbers)
5. Write a Python program to display the first and last colors from the
following list.
color_list = ["Red","Green","White" ,"Black"]
Ans: color_list = ["Red", "Green", "White", "Black"]
print("First color: ", color_list[0])
print("Last color: ", color_list[-1])
6. Write a Python program that will accept the base and height of a triangle
and compute its area.
Ans: base = float(input("Enter the base of the triangle: "))
height = float(input("Enter the height of the triangle: "))
area = 0.5 * base * height
print("Area of the triangle is: ", area)
7. Write a Python program to solve (x + y) * (x + y).
Test Data : x = 4, y = 3
Expected Output : (4 + 3) ^ 2) = 49
Ans: x = 4
y=3
result = (x + y) ** 2
print("Result is: ", result)
8. Write a Python program to compute the future value of a specified principal
amount, rate of interest, and number of years.
principal = float(input("Enter the principal amount: "))
rate = float(input("Enter the rate of interest: "))
years = int(input("Enter the number of years: "))
future_value = principal * ((1 + (rate / 100)) ** years)
print("Future value is: ", round(future_value, 2))
9. The formula for future value with compound interest is FV = P(1 + r/n)^nt.
FV = the future value; P = the principal; r = the
annual interest rate expressed as a decimal; n =
the number of times interest is paid each year; t =
time in years.
Test Data: amt = 10000, int = 3.5, years = 7
Expected Output: 12722.79
principal = 10000
rate = 3.5
years = 7
n=1
future_value = principal * ((1 + ((rate / 100) / n)) ** (n * years))
print("Future value is: ", round(future_value, 2))
10. Write a Python program to calculate the distance between the points (x1,
y1) and (x2, y2).
import math
x1 = float(input("Enter x1: "))
y1 = float(input("Enter y1: "))
x2 = float(input("Enter x2: "))
y2 = float(input("Enter y2: "))
distance = math.sqrt(((x2 - x1) ** 2) + ((y2 - y1) ** 2))
print("Distance is: ", round(distance, 2))
11. Write a Python program to sum the first n positive integers.
n = int(input("Enter a positive integer: "))
sum = 0
for i in range(1, n + 1):
sum += i
print("Sum of the first", n, "positive integers is", sum)
12. Write a Python program to convert height (in feet and inches) to
centimeters.
feet = int(input("Enter height in feet: "))
inches = int(input("Enter height in inches: "))
height_in_cm = ((feet * 12) + inches) * 2.54
print("Height is: ", round(height_in_cm, 2), "cm")
13. Write a Python program to convert the distance (in feet) to inches,
yards, and miles.
feet = float(input("Enter distance in feet: "))
inches = feet * 12
yards = feet / 3
miles = feet / 5280
print("Distance in inches is: ", round(inches, 2))
print("Distance in yards is: ", round(yards, 2))
print("Distance in miles is: ", round(miles, 2))
14. Write a Python program to convert all units of time into seconds.
days = int(input("Enter number of days: "))
hours = int(input("Enter number of hours: "))
minutes = int(input("Enter number of minutes: "))
seconds = int(input("Enter number of seconds: "))
total_seconds = (days * 24 * 60 * 60) + (hours * 60 * 60) + (minutes * 60)
+ seconds
print("The total number of seconds is: ", total_seconds)
15. Write a Python program that converts seconds into days, hours, minutes,
and seconds.
seconds = int(input("Enter the number of seconds: "))
days = seconds // (24 * 3600)
seconds = seconds % (24 * 3600)
hours = seconds // 3600
seconds %= 3600
minutes = seconds // 60
seconds %= 60
print("The equivalent duration is: ", days, "days,", hours, "hours,", minutes,
"minutes,", seconds, "seconds")
16. Write a Python program to calculate sum of digits of a number.
Example: digit 5245 = 5+2+4+5 == 16
def getSum(n):
sum = 0
while (n != 0):
sum = sum + (n % 10)
n = n//10
return sum
n = 5245
print(getSum(n))
17. Write a Python program to concatenate N strings.
def concatenate_strings(*args):
result = ""
for arg in args:
result += arg
return result
print(concatenate_strings("Hello", " ", "World"))
18. Given variables x=30 and y=20, write a Python program to print
"30+20=50".
x = 30
y = 20
print(f"{x}+{y}={x+y}")
19. Write a Python program to swap two variables.
x = 10
y = 20
print(f"Before swapping: x={x}, y={y}")
temp = x
x=y
y = temp
print(f"After swapping: x={x}, y={y}")
20. Write a Python program to print a variable without spaces between values.
Sample value: x =30; Expected output: Value of x is "30"
x = 30
print('Value of x is \"{}\"'.format(x))
EIABC BY : Kassahun T.
2