6️⃣

6.6 Membership Operators

Membership Operators

You may have noticed that in is a keyword in Python. that's because it's a special type of operator called a membership operator, which can check if something is in another thing (like if an element is in a list). Using the not in operator, you can also check if something is not in another thing. For example:
my_list = [1, 2, 3, "oh no"] if "oh no" in my_list: print("oh no is an element in my_list") else: print("oh no is not an element in my_list") if 4 not in my_list: print("4 is not an element in my_list") else: print("4 is an element in my_list")
View code on GitHub.
Output of the code above
oh no is an element in my_list 4 is not an element in my_list
 
⚖️
Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.