in

KeywordPython 2.0+Beginner

Membership test operator; also used in for loops to iterate over items

Quick Info

Documentation
Official Docs
Python Version
2.0+

Learn by Difficulty

Quick Example

python
# in with lists
fruits = ["apple", "banana", "cherry"]
print("banana" in fruits)
print("grape" in fruits)

# in with strings
text = "Hello, World!"
print("World" in text)
print("xyz" in text)

# in with dictionaries (checks keys)
ages = {"Alice": 30, "Bob": 25}
print("Alice" in ages)
print(30 in ages)  # checks keys, not values

'in' tests membership. For lists it checks values, for strings it checks substrings, for dicts it checks keys.

Try in Playground

Tags

languagesyntaxcoremembershipiterationoperator

Related Items