Check if a number is in a list

EasyIf-Else Programs · 13 of 64

Write a Python program to check whether a given number is present in a list of numbers.

Advertisement
Input
numbers = [12, 45, 7, 89, 33]; check 89 and 50
Output
89 found at index 3
50 is not in the list

Approach

  1. Use in; use index() to also report the position.

Solution

Python
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.

More if-else programs

← All 64 If-Else Programs

Leave a Comment