Skip to content
Programming Problems · Q2

Q.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
Uttarakhand UbseTextbookSubjective· 3mImportance★★★★★est
60% · 18/30 Questions
✓ Free question

Read the names into a tuple, then check membership twice — with your own linear-search function, and with Python's built-in in operator / count() method.

The idea. The task contrasts writing a search with reusing one. The user-defined version is a plain linear search: visit each element, compare with the target, return True on the first match. Python already ships this logic as the membership operator in (and the tuple method count(), which returns how many times a value occurs — nonzero means present).

Program (both approaches on the same data):

def search(names, target):        # (a) user defined function
    for name in names:
        if name == target:
            return True
    return False

n = int(input("How many students? "))
names = ()
for i in range(n):
    names = names + (input("Enter name: "),)

target = input("Enter the name to search: ")

# (a) using the user defined function
if search(names, target):
    print("(a)", target, "is present in the tuple (user defined function)")
else:
    print("(a)", target, "is not present in the tuple (user defined function)")

# (b) using the built-in membership operator 'in' / built-in count()
if target in names:
    print("(b)", target, "is present in the tuple (built-in 'in')")
else:
    print("(b)", target, "is not present in the tuple (built-in 'in')")
print("(b) count() reports", names.count(target), "occurrence(s)")

Sample run:

How many students? 4
Enter name: Aman
Enter name: Bina
Enter name: Chetan
Enter name: Divya
Enter the name to search: Chetan
(a) Chetan is present in the tuple (user defined function)
(b) Chetan is present in the tuple (built-in 'in')
(b) count() reports 1 occurrence(s)

Key lines:

  • search() returns True the moment a match is found and False only after the whole tuple is scanned — the standard linear-search shape, O(n) time.
  • target in names performs exactly the same scan internally, in one readable expression.
  • names.count(target) additionally tells you how many occurrences there are.
Tip

In real code always prefer in — it is shorter, clearer and implemented in C. The user-defined version is for learning how the search actually works.

✓Final answer

Both methods confirm the searched name: (a) the user-defined function scans the tuple element by element and returns True/False;

(b) the built-in membership operator in (backed up by count()) reports the same result directly — in the sample run, Chetan is present.

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.