Name | in |
||||
---|---|---|---|---|---|
Examples |
# Check if a given substring is present in a string message = "Now is the winter of our discontent." print("winter" in message) # Prints 'True' # Check if an item is present in a list data = [17, 24, 32, 912, 403] print(17 in data) # Prints 'True' # Check if a key is present in a dictionary elements = {'H': 'hydrogen', 'He': 'helium', 'Li': 'lithium'} print('He' in elements) # Prints 'True' # Note: The "in" operator doesn't check for *values* in dictionaries! print('hydrogen' in elements) # Prints 'False' | ||||
Description |
Returns True if a given value is in a string, list or dictionary, and
False otherwise. The in operator works slightly differently for different types. When used with strings, it checks if a substring is found within another string. When used with lists or tuples, it checks if a given value is found inside the list or tuple. When used with dictionaries, it checks to see if a particular key is present in the dictionary. (Note that the use of the keyword in as an operator is separate from and unrelated to its use in the syntax of for loops and list comprehensions.) |
||||
Syntax | item in object | ||||
Parameters |
| ||||
Related |
list() string Dictionary |
Updated on Tue Feb 27 14:07:12 2024.
If you see any errors or have comments, please let us know.
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License