python / beginner
Snippet
Membership Testing with 'in'
The 'in' operator checks if a specific value exists within a sequence, such as a list or a string, and returns a boolean (True or False).
snippet.py
1
2
3
4
fruits = ['apple', 'banana', 'cherry']if 'banana' in fruits:print('Found it!')
Breakdown
1
if 'banana' in fruits:
Checks if the string 'banana' is one of the items in the fruits list.
2
print('Found it!')
This line runs only if the condition is True.