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 Python, keywords are case-sensitive.
Python in Keyword
The ‘in’ keyword in Python returns True if a certain element is present in a Python object, else it will return False. It has two purposes:
- To check if a value is present in a list, tuple, range, string, etc.
- To iterate through a sequence in a for loop.
Python ‘in’ keyword in Expression
The in keyword in Python can be used with if statements as well as with for loops. It has the following syntax:
# Using if statement
if element in sequence:
# Using for statement
for element in sequence:
Python in Keyword Usage
Let us see a few examples to know how the “in” keyword works in Python.
“in” Keyword with Python if Statement
In this example, we will create a Python list of animals. Then we will use the in keyword with the if statement to check the presence of “lion” in animals.
Use of in keyword in for loop in Python
In this example, we will first define a Python String, and use for loop to iterate over each character in that string.