Checking if a number is not in a list is a common task in Python programming. This tutorial will guide you through the steps of checking if a number is not in a list using Python.
Steps:
1. Define the list of numbers.
1 |
numbers = [1, 2, 3, 4, 5] |
2. Use the not in
operator to check if the number is not in the list.
1 2 3 4 |
if 6 not in numbers: print("6 is not in the list") else: print("6 is in the list") |
This code will check if the number 6 is not in the list numbers
. If it is not in the list, it will print “6 is not in the list”. If it is in the list, it will print “6 is in the list”.
3. Replace the number 6 with the number you want to check.
1 2 3 4 |
if [number] not in numbers: print("[number] is not in the list") else: print("[number] is in the list") |
Replace [number]
with the number you want to check.
Example:
Let’s check if the number 6 and 9 is not in the list numbers
.
1 2 3 4 5 6 7 8 9 10 11 |
numbers = [1, 2, 3, 4, 5] if 6 not in numbers: print("6 is not in the list") else: print("6 is in the list") if 9 not in numbers: print("9 is not in the list") else: print("9 is in the list") |
The output will be:
Output:
1 2 |
6 is not in the list 9 is not in the list |
This output tells us that the number 6 and 9 is not in the list of numbers
.
Conclusion:
Checking if a number is not in a list in Python is easy using the not in
operator. Follow these steps to determine if a number is not in a list.