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
- Defined a list(numbers_list) that contains multiple numbers in it.
- Use the 'in' keyword inside the if condition to check whether the given value(40) exists inside the list.
- 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?
Similar Posts
- Check if given value is a number or integer in python
- Check if a list exists in a list of lists Python
- Check if item exists in a list using python
- Check if a substring exists in a list of strings Python
- Check if a directory exists using Python
- Check if a value exists in a dictionary Python
- [Pandas] Check if a column exists in a DataFrame