Python Practice › If-Else Programs › Program 13
Check if a number is in a list
Write a Python program to check whether a given number is present in a list of numbers.
Input
numbers = [12, 45, 7, 89, 33]; check 89 and 50
Output
89 found at index 3 50 is not in the list
Approach
- Use
in; useindex()to also report the position.
Solution
numbers = [12, 45, 7, 89, 33] for n in [89, 50]: if n in numbers: print(n, "found at index", numbers.index(n)) else: print(n, "is not in the list")Output
89 found at index 3 50 is not in the list
Line by line
Line 5
index() is safe here because the if already confirmed the number is present.
Learn the concept
New to this topic? Read the lesson, then come back.