0% found this document useful (0 votes)
49 views9 pages

Chandigarh University Python Assignment Sem 4

Uploaded by

deepit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views9 pages

Chandigarh University Python Assignment Sem 4

Uploaded by

deepit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Chandigarh

University

Bachelors of
computer
application

Python
Programming
Graded
Assignment
Name:Deepit Chikersal
UID Number:O22BCA11083
Python Programming
Assignment

1. Write a Python program that will accept the base and


height of a triangle and compute the area.

Answer:-

def calculate_triangle_area(base, height):


area = 0.5 * base * height
return area

def main():
try:
base = float(input("Enter the base of the triangle: "))
height = float(input("Enter the height of the triangle: "))

if base <= 0 or height <= 0:


print("Base and height must be positive numbers.")
else:
area = calculate_triangle_area(base, height)
print("The area of the triangle is:", area)
except ValueError:
print("Please enter valid numerical values for base and height.")

if __name__ == "__main__":
main()
2. Write a Python program to create all possible strings by
using 'a', 'e', 'i', 'o', 'u'. Use the characters exactly once.
Answer:-

from itertools import permutations

def generate_strings(characters):
# Generate all permutations of the characters
perms = permutations(characters)

# Join each permutation into a string and add to the result list
result = [''.join(perm) for perm in perms]

return result

def main():
characters = ['a', 'e', 'i', 'o', 'u']
all_strings = generate_strings(characters)

# Print all generated strings


for string in all_strings:
print(string)

if __name__ == "__main__":
main()
3. Write a Python program to sum of two given integers.
However, if the sum is between 15 to 20 it will return 20
Answer:-
def sum_with_condition(a, b):
# Calculate the sum of the two integers
total = a + b

# Check if the sum is between 15 and 20


if 15 <= total <= 20:
return 20
else:
return total

def main():
# Take user input for two integers
try:
num1 = int(input("Enter the first integer: "))
num2 = int(input("Enter the second integer: "))

# Call the function to compute the sum with condition


result = sum_with_condition(num1, num2)

# Print the result


print("The sum is:", result)
except ValueError:
print("Please enter valid integers.")

if __name__ == "__main__":
main()
4. Write a Python program to calculate the length of a string
Answer:-
def calculate_string_length(input_string):
# Use the len() function to calculate the length of the string
length = len(input_string)
return length

def main():
# Take user input for a string
input_string = input("Enter a string: ")

# Call the function to calculate the length of the string


length = calculate_string_length(input_string)

# Print the length of the string


print("Length of the string:", length)

if __name__ == "__main__":
main()
5.Write a Python program to find the median among three
given numbers
def find_median(num1, num2, num3):
# Put the numbers in a list
numbers = [num1, num2, num3]

# Sort the numbers


numbers.sort()

# Find the middle number (median)


median = numbers[1]

return median

def main():
# Take user input for three numbers
try:
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))

# Call the function to find the median


median = find_median(num1, num2, num3)

# Print the median


print("The median is:", median)
except ValueError:
print("Please enter valid numerical values.")

if __name__ == "__main__":
main()

You might also like