Python Programming Assignment
Q1. Write a Python program that will accept the base and height of a triangle and
compute the area. Commented [MW1]: Get the conversation going by
adding comments and using Share (above) to send a link to
Answer: - this doc. It’s free! No subscription or sign-in necessary.
Code: -
# Get user input for base and height
base = float(input('Enter the base of the triangle: '))
height = float(input('Enter the height of the triangle: '))
# Calculate the area
area = (1/2) * base * height
# Display the result
print('Area of triangle = ',area)
Output :-
Enter the base of the triangle: 20
Enter the height of the triangle: 10
Area of triangle = 100.0
Q 4. Write a Python program to calculate the length of a string
Answer: -
# Get user input
string = input("Enter a string: ")
# Calculate the length
length = len(string)
# Display the result
print("The length of the string is", length)
Output: -
Enter a string: hello
The length of the string is 5
Q 5. Write a Python program to find the median among three given numbers.
Answer: -
# function to calculate median
def median(a, b, c):
if a > b:
if b > c:
return b
elif a > c:
return c
else:
return a
else:
if a > c:
return a
elif b > c:
return c
else:
return b
a = int(input("Enter a : "))
b = int(input("Enter b : "))
c = int(input("Enter c : "))
print("The median of a,b,c is ",median(a,b,c))
Output: -
Enter a : 10
Enter b : 5
Enter c : 20
The median of a,b,c is 10