Python is a popular programming language due to how readable and straightforward the syntax and keywords are.
A great example of this is the in and not in operators, which have many uses in Python.
In this post, we'll learn how to use the in and not in operators in Python.
How to use the in and not in operators with lists
A list in Python is a collection of items in a particular order. You can define a list by placing all the items inside square brackets [], separated by commas.
PYTHON
fruits = ["apple", "banana", "cherry"]
Now, we can use the in operator to check if an item is in a list.
PYTHON
fruits = ["apple", "banana", "cherry"]
if"apple"in fruits:
print("Yes, 'apple' is in the fruits list")
BASH
Yes, 'apple' is in the fruits list
Alternatively, we can use the not in operator to check if an item is not in a list.
PYTHON
fruits = ["apple", "banana", "cherry"]
if"pineapple"notin fruits:
print("No, 'pineapple' is not in the fruits list")
BASH
No, 'pineapple' is not in the fruits list
How to use the in and not in operators with strings
Internally, a string in Python is basically a sequence of characters. You can define a string by placing all the characters inside single quotes '' or double quotes "".
PYTHON
sentence = "The quick brown fox jumps over the lazy dog"
Now, we can use the in operator to check if a substring is in a string.
PYTHON
sentence = "The quick brown fox jumps over the lazy dog"if"quick"in sentence:
print("Yes, 'quick' is in the sentence")
BASH
Yes, 'quick' is in the sentence
Likewise, we can use the not in operator to check if a substring is not in a string.
PYTHON
sentence = "The quick brown fox jumps over the lazy dog"if"quickly"notin sentence:
print("No, 'quickly' is not in the sentence")
BASH
No, 'quickly' is not in the sentence
How to use the in and not in operators with dictionaries
In Python, a dictionary is a collection of key-value pairs. You can define a dictionary by placing all the key-value pairs inside curly braces {}, separated by commas.
PYTHON
person = {
"name": "John",
"age": 36,
"country": "Norway"
}
Now, we can use the in operator to check if a key is in a dictionary.
PYTHON
person = {
"name": "John",
"age": 36,
"country": "Norway"
}
if"name"in person:
print("Yes, 'name' is one of the keys in the person dictionary")
BASH
Yes, 'name' is one of the keys in the person dictionary
On the other hand, we can use the not in operator to check if a key is not in a dictionary.
PYTHON
person = {
"name": "John",
"age": 36,
"country": "Norway"
}
if"email"notin person:
print("No, 'email' is not one of the keys in the person dictionary")
BASH
No, 'email' is not one of the keys in the person dictionary
Conclusion
In this post, we learned how to use the in and not in operators in Python.
These operators are useful for checking if an item is in a list, a substring is in a string, or a key is in a dictionary.
Thanks for reading!
To learn more about web development, founding a start-up, and bootstrapping a SaaS, follow me on X!