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.
Python3
# Python program to demonstrate # in keyword # Create a list animals = [ "dog" , "lion" , "cat" ] # Check if lion in list or not if "lion" in animals: print ( "Yes" ) |
Output:
Yes
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.
Python3
# Python program to demonstrate # in keyword # Create a string s = "w3wiki" # Iterating through the string for i in s: # if f occurs in the string # break the loop if i = = 'f' : break print (i) |
Output:
G
e
e
k
s
Python in Keyword
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.