Question 10 of 14intermediate🔧 ApplyLong Answer5 marks

Write a program to input names of n students and store them in a tuple. Also, input a name from the user and find if this student is present in the tuple or not. We can accomplish these by: (a) writing a user defined function (b) using the built-in function

(a)
Answer

Program using user-defined function

def search_name(student_tuple, search_name): for name in student_tuple: if name == search_name: return True return False

Main program

n = int(input("Enter the number of students: ")) students = tuple() for i in range(n): name = input("Enter student name: ") students = students + (name,)

search = input("Enter name to search: ") if search_name(students, search): print(search, "is present in the tuple") else: print(search, "is not present in the tuple")

(b)
Answer

Program using built-in function

n = int(input("Enter the number of students: ")) students = tuple() for i in range(n): name = input("Enter student name: ") students = students + (name,)

search = input("Enter name to search: ") if search in students: print(search, "is present in the tuple") else: print(search, "is not present in the tuple")

Explanation

The textbook demonstrates creating tuples using tuple() and adding elements through concatenation (Program 10-4). For part (a), a user-defined function iterates through each element to check for a match. For part (b), Python's built-in 'in' operator checks membership directly in the tuple. Both approaches accomplish the same task but differ in implementation.