The in keyword in Python is a powerful operator used for membership testing and iteration. It helps determine whether an element exists within a given sequence, such as a list, tuple, string, set or dictionary.
Example:
Python
s = "Geeks for geeks"
if "for" in s:
print("found")
else:
print("not found")
Explanation: if “for” in s checks if the substring “for” exists in the string s using the in keyword, which performs a membership test.
Purpose of the in keyword
The in keyword in Python serves two primary purposes:
- Membership Testing: To check if a value exists in a sequence such as a list, tuple, set, range, dictionary or string.
- Iteration: To iterate through elements of a sequence in a
for
loop.
Syntax
The in keyword can be used with both if
statements and for
loops.
Using in with if statement:
if element in sequence:
# Execute statement
Using in with for loop:
for element in sequence:
# Execute statement
Examples of in keyword
Let’s explore how the in
keyword functions with different Python data structures .
Example 1: in Keyword with if Statement
In this example, we will check if the string “php” is present in a list of programming languages. If it is, the program will print True.
Python
a = ["php", "python", "java"]
if "php" in a:
print(True)
Explanation: The in
operator checks if the string “php” is present in the list a
. Since “php” exists in the list, the output will be True
.
Example 2: in keyword in a for loop
Here, we will use the in keyword to loop through each character of the string “GeeksforGeeks” and print each character until the character ‘f’ is encountered, at which point the loop will stop.
Python
s = "GeeksforGeeks"
for char in s:
if char == 'f':
break
print(char)
Explanation: The for loop iterates through each character in the strings. The loop prints each character until it encounters ‘f’, at which point it breaks the loop and stops execution.
Example 3: in keyword with dictionaries
In this case, we will check if the key “Alice” exists in a dictionary of student names and marks. If the key is found, we will print Alice’s marks.
Python
d = {"Alice": 90, "Bob": 85}
if "Alice" in d:
print("Alice's marks are:", d["Alice"])
OutputAlice's marks are: 90
Explanation: The in operator checks whether “Alice” is present as a key in dictionary d. Since “Alice” is a key, it prints her marks.
Example 4: in keyword with sets
We will check if the character ‘e’ is present in a set of vowels and print the result as True or False based on its presence.
Python
v = {'a', 'e', 'i', 'o', 'u'}
print('e' in v)
Explanation: The in operator checks if ‘e’ is present in the set v. Since ‘e’ exists in the set, the output will be True.
Similar Reads
is keyword in Python
In programming, a keyword is a âreserved wordâ by the language that conveys special meaning to the interpreter. It may be a command or a parameter. Keywords cannot be used as a variable name in the program snippet. Python language also reserves some of the keywords that convey special meaning. In Py
2 min read
Python Keywords
Keywords in Python are reserved words that have special meanings and serve specific purposes in the language syntax. Python keywords cannot be used as the names of variables, functions, and classes or any other identifier. List of Keywords in PythonTrueFalseNoneandornotisifelseelifforwhilebreakconti
12 min read
Python as Keyword
as keyword in Python plays a important role in simplifying code, making it more readable and avoiding potential naming conflicts. It is mainly used to create aliases for modules, exceptions and file operations. This powerful feature reduces verbosity, helps in naming clarity and can be essential whe
3 min read
Python def Keyword
Python def keyword is used to define a function, it is placed before a function name that is provided by the user to create a user-defined function. In Python, a function is a logical unit of code containing a sequence of statements indented under a name given using the âdefâ keyword. In Python def
6 min read
Python del keyword
The del keyword in Python is used to delete objects like variables, lists, dictionary entries, or slices of a list. Since everything in Python is an object, del helps remove references to these objects and can free up memory del Keyword removes the reference to an object. If that object has no other
2 min read
Keyword Module in Python
Python provides an in-built module keyword that allows you to know about the reserved keywords of python. The keyword module allows you the functionality to know about the reserved words or keywords of Python and to check whether the value of a variable is a reserved word or not. In case you are una
2 min read
Keywords in Python | Set 2
Python Keywords - Introduction Keywords in Python | Set 1 More keywords:16. try : This keyword is used for exception handling, used to catch the errors in the code using the keyword except. Code in "try" block is checked, if there is any type of error, except block is executed. 17. except : As expl
4 min read
Python or Keyword
Python OR is a logical operator keyword. The OR operator returns True if at least one of the operands becomes to be True. Note: In Python "or" operator does not return True or False. The "or" operator in Python returns the first operand if it is True else the second operand.Letâs start with a simple
2 min read
Python False Keyword
False is a boolean value in Python that represents something untrue or a "no" condition. It is one of the two Boolean constants (True and False) and is mostly used in conditions, loops and logical operations. In Python, False is treated as 0 in mathematical operations and as a falsy value in conditi
2 min read
Python Raise Keyword
In this article, we will learn how the Python Raise keyword works with the help of examples and its advantages. Python Raise KeywordPython raise Keyword is used to raise exceptions or errors. The raise keyword raises an error and stops the control flow of the program. It is used to bring up the curr
3 min read