1. Square Root of a Number import math def find_square_root(number): if number < 0: return "Cannot calculate square root of a negative number" return math.sqrt(number) # Example usage num = 16 print(f"The square root of {num} is {find_square_root(num)}") 2. Area of a Triangle Using Base and Height def area_triangle_base_height(base, height): return 0.5 * base * height # Example usage b = 10 h = 5 print(f"Area of triangle (base={b}, height={h}): {area_triangle_base_height(b, h)}") Using Three Sides (Heron's Formula) import math def area_triangle_heron(a, b, c): s = (a + b + c) / 2 # Semi-perimeter if s <= a or s <= b or s <= c: # Check for valid triangle return "Invalid triangle sides" area = math.sqrt(s * (s - a) * (s - b) * (s - c)) return area # Example usage side1, side2, side3 = 3, 4, 5 print(f"Area of triangle (sides={side1},{side2},{side3}): {area_triangle_heron(side1, side2, side3)}") 3. Student Marks (Dictionary, Total, Average) def calculate_student_marks(student_name, marks): print(f"\n--- Marks for {student_name} ---") for subject, score in marks.items(): print(f"{subject}: {score}") total_marks = sum(marks.values()) num_subjects = len(marks) average_marks = total_marks / num_subjects print(f"Total Marks: {total_marks}") print(f"Average Marks: {average_marks:.2f}") return total_marks, average_marks # Example usage student_scores = { "Math": 85, "Physics": 92, "Chemistry": 78, "Biology": 88, "English": 75 } calculate_student_marks("Alice", student_scores) student_scores_bob = { "Math": 70, "Physics": 65, "Chemistry": 80, "Biology": 75, "English": 90 } calculate_student_marks("Bob", student_scores_bob) 4. Tuple Operations a. Reverse a String in a Tuple def reverse_string_in_tuple(tup_with_string): if not tup_with_string or not isinstance(tup_with_string[0], str): return "Tuple must contain at least one string." original_string = tup_with_string[0] reversed_string = original_string[::-1] return (reversed_string,) + tup_with_string[1:] # Return as new tuple # Example usage my_tuple = ("hello", 123, True) reversed_tuple = reverse_string_in_tuple(my_tuple) print(f"Original tuple: {my_tuple}") print(f"Tuple with reversed string: {reversed_tuple}") my_tuple_single = ("python",) reversed_tuple_single = reverse_string_in_tuple(my_tuple_single) print(f"Original single string tuple: {my_tuple_single}") print(f"Reversed single string tuple: {reversed_tuple_single}") b. Concatenate Two Tuples def concatenate_tuples(tup1, tup2): return tup1 + tup2 # Example usage tuple_a = (1, 2, 3) tuple_b = ('a', 'b', 'c') concatenated_tuple = concatenate_tuples(tuple_a, tuple_b) print(f"Tuple A: {tuple_a}") print(f"Tuple B: {tuple_b}") print(f"Concatenated Tuple: {concatenated_tuple}") 5. Set Operations for Student Sports # Define the sets of students football = {'A', 'B', 'C', 'D'} cricket = {'B', 'D', 'E', 'F'} hockey = {'A', 'C', 'G', 'F'} print(f"Football fans: {football}") print(f"Cricket fans: {cricket}") print(f"Hockey fans: {hockey}") # a. Students who like both Football and Hockey (Intersection) football_and_hockey = football.intersection(hockey) print(f"\na. Students who like both Football and Hockey: {football_and_hockey}") # b. Students who like only Football, only Cricket, and only Hockey only_football = football - (cricket.union(hockey)) only_cricket = cricket - (football.union(hockey)) only_hockey = hockey - (football.union(cricket)) print(f"\nb. Students who like only Football: {only_football}") print(f"Students who like only Cricket: {only_cricket}") print(f"Students who like only Hockey: {only_hockey}")