python / beginner
Snippet
Membership Testing with 'in'
The 'in' operator provides a clean way to check if a value exists within a list or other sequence.
snippet.py
1
2
3
4
allowed_users = ["admin", "editor", "guest"]user = "editor"is_allowed = user in allowed_usersprint(is_allowed)
Breakdown
1
allowed_users = [...]
Creates a list of strings.
2
user = "editor"
Defines the value we want to search for.
3
is_allowed = user in allowed_users
Checks if 'editor' is inside the list; returns True or False.