How to use the "in" and "not in" Operators in Python
Table of Contents
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.
PYTHONfruits = ["apple", "banana", "cherry"]
Now, we can use the in
operator to check if an item is in a list.
PYTHONfruits = ["apple", "banana", "cherry"]
if "apple" in fruits:
print("Yes, 'apple' is in the fruits list")
BASHYes, 'apple' is in the fruits list
Alternatively, we can use the not in
operator to check if an item is not in a list.
PYTHONfruits = ["apple", "banana", "cherry"]
if "pineapple" not in fruits:
print("No, 'pineapple' is not in the fruits list")
BASHNo, '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 ""
.
PYTHONsentence = "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.
PYTHONsentence = "The quick brown fox jumps over the lazy dog"
if "quick" in sentence:
print("Yes, 'quick' is in the sentence")
BASHYes, 'quick' is in the sentence
Likewise, we can use the not in
operator to check if a substring is not in a string.
PYTHONsentence = "The quick brown fox jumps over the lazy dog"
if "quickly" not in sentence:
print("No, 'quickly' is not in the sentence")
BASHNo, '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.
PYTHONperson = {
"name": "John",
"age": 36,
"country": "Norway"
}
Now, we can use the in
operator to check if a key is in a dictionary.
PYTHONperson = {
"name": "John",
"age": 36,
"country": "Norway"
}
if "name" in person:
print("Yes, 'name' is one of the keys in the person dictionary")
BASHYes, '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.
PYTHONperson = {
"name": "John",
"age": 36,
"country": "Norway"
}
if "email" not in person:
print("No, 'email' is not one of the keys in the person dictionary")
BASHNo, '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!
- Getting Started with TypeScript
- How to Install Node on Windows, macOS and Linux
- How to Serve Static Files with Nginx and Docker
- How to Set Up Cron Jobs in Linux
- How to deploy a .NET app using Docker
- How to deploy a Deno app using Docker
- How to deploy an Express app using Docker
- Using Puppeteer and Jest for End-to-End Testing
- How to Scrape the Web using Node.js and Puppeteer
- Getting Started with Moment.js
- Creating a Twitter bot with Node.js
- Getting Started with React