Skip to content
Programs · Program 4-3

Q.Write a program to check if a number is present in the list or not. If the number is present, print the position of the number. Print an appropriate message if the number is not present in the list.

Chhattisgarh CgbseTextbookSubjective· 3mImportance★★★★★est
17% · 5/30 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

Test membership first with in, and only then ask for the location with index(). num in myList answers "is it there?"; myList.index(num) answers "where?" — calling index() on an absent value crashes with ValueError, which is exactly why the in check comes first.

The idea. Python gives us two purpose-built tools: the membership operator in (returns True/False) and the list method index() (returns the first index of a value). The safe pattern is check, then locate.

myList = [17, 23, 4, 91, 55, 8]
num = int(input("Enter the number to search: "))

if num in myList:
    print(num, "is present at position", myList.index(num) + 1)
else:
    print(num, "is not present in the list")

Two sample runs:

Enter the number to search: 91
91 is present at position 4
Enter the number to search: 50
50 is not present in the list

Why the key lines work

  • if num in myList: — the in operator scans the list and returns True the moment it finds a match. It is the cleanest way to ask "is this element 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.