CBSE Class 11 Computer Science
Question 20 of 25
List Manipulation — Question 6
Back to all questions 6
Question Question 6
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.
Solution
l = eval(input("Enter list: "))
n = int(input("Enter number to search: "))
if n in l:
print(n, "found at index", l.index(n))
else :
print(n, "not found in list")Output
Enter list: [1, 3, 15, 8, 20]
Enter number to search: 15
15 found at index 2
=====================================
Enter list: [1, 3, 15, 8, 20]
Enter number to search: 25
25 not found in list
l
=
eval
(
input
(
"Enter list: "
))
n
=
int
(
input
(
"Enter number to search: "
))
if
n
in
l
:
print
(
n
,
"found at index"
,
l
.
index
(
n
))
else
:
print
(
n
,
"not found in list"
)
Output
Enter list: [1, 3, 15, 8, 20]
Enter number to search: 15
15 found at index 2
=====================================
Enter list: [1, 3, 15, 8, 20]
Enter number to search: 25
25 not found in list