capypad
0 day streak
python / beginner
Snippet

Membership Testing with the 'in' Operator

The 'in' operator is used to check if a value exists within a sequence (like a list, tuple, or string). It returns True if the value is found and False otherwise, providing a clean way to perform searches.

snippet.py
python
1
2
3
members = ['user1', 'user2', 'user3']
print('user2' in members)
print('admin' in members)
Breakdown
1
members = ['user1', 'user2', 'user3']
Create a list of registered usernames.
2
print('user2' in members)
Check if 'user2' is present in the list and print the result (True).
3
print('admin' in members)
Check if 'admin' is present in the list and print the result (False).