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
0 Comments