python

Python code to check if a given number exists in a list

The code examples mentioned here can be used to check whether a specific number exists in a list or not. We are using the in keyword for that.

numbers_list = [30, 50, 10, 40, 90, 20]

if 40 in numbers_list:
    print('Number exists in the list')

Output

Number exists in the list

In the code example

  1. Defined a list(numbers_list) that contains multiple numbers in it.
  2. Use the 'in' keyword inside the if condition to check whether the given value(40) exists inside the list.
  3. Printing the message if the given value exists inside the list.

Code Example 2

nums = [1, 3, 5, 7, 9]

if 10 in nums:
  print('Exist')
else:
  print('Not exist')

Output

Not exist
Was this helpful?