Kodeclik Blog
About
Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.
Popular Classes
Scratch Coding
Minecraft Coding
TinkerCAD
Roblox Studio
Python for Kids
Javascript for Kids
Pre-Algebra
Geometry for Kids
Copyright @ Kodeclik 2024. All rights reserved.
As the name indicates, “not in” is a logical operator in Python that returns true if a given object is not present in an iterable such as string, list, array, or dictionary. It returns False if the object is present.
Consider that we have a list of invitees to a party and we would like to check to see if specific people are invited. Here is a Python program to implement this:
Here are two sample runs:
As you can see 'not in' takes two inputs; in this case, x the user’s input and inviteelist, the iterable that contains the ground truth of who is invited to the party.
The “not in” operator works with many other iterables, e.g., strings. Consider:
The output is:
The “not in” operator works with dictionaries too but note that the “not in” condition is applied to the keys of the dictionary, not to its values.
In the above code, we have a dictionary that represents our shopping cart. It contains the items we have and the number of entries of each item. We also have a grocerychecklist variable called “grocerynotes” which denotes the items we are supposed to purchase. Then a for loop is used to verify the presence of each item in the checklist in our shopping cart. In this manner, we notice that we have forgotten to purchase grapes.
Note that when we use “not in” with the shoppingcart dictionary variable, it is applied to the keys which are the names of vegetables, not the quantities (which are the values).
You can use “not in” with the range operator, like so:
Sample outputs are:
Note that we have to give one higher than 12 as the upper index for the range operator.
As you can see “not in” is a very convenient operator and leads to more readable code. How will you use it in your own programs?
If you liked learning about the not in operator, checkout the Python hasattr() function.
Interested in more things Python? See our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!
Want to learn Python with us? Sign up for 1:1 or small group classes.
inviteelist = ['Jack','Elizabeth','Tommy']
x = input('Enter your name: ')
if (x not in inviteelist):
print("Sorry, you are not invited to the party.")
else:
print("Welcome to the party!")
Enter your name: Timmy
Sorry, you are not invited to the party.
Enter your name: Jack
Welcome to the party!
magic = "Abracadabra"
for x in ['a','b','c','d','e']:
if (x not in magic):
print("Sorry; " + x + " not present.")
Sorry; e not present.
shoppingcart = {'apples': 3, 'bananas': 2, 'oranges': 5}
grocerynotes = ['apples','grapes','oranges','bananas']
for x in grocerynotes:
if (x not in shoppingcart):
print("I forgot to get " + x + "!")
I forgot to get grapes!
x = input ("Input your birth month: ")
if (int(x) not in range(1,13)):
print("Sorry! That’s not a valid month.")
else:
print("That sounds believable.")
Input your birth month: 5
That sounds believable.
Input your birth month: 12
That sounds believable.
Input your birth month: 13
Sorry! That’s not a valid month.