Python in Keyword
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:
s = "Geeks for geeks"
if "for" in s:
print("found")
else:
print("not found")
s = "Geeks for geeks"
if "for" in s:
print("found")
else:
print("not found")
Output
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.
a = ["php", "python", "java"]
if "php" in a:
print(True)
a = ["php", "python", "java"]
if "php" in a:
print(True)
Output
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.
s = "GeeksforGeeks"
for char in s:
if char == 'f':
break
print(char)
s = "GeeksforGeeks"
for char in s:
if char == 'f':
break
print(char)
Output
G e e k s
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.
d = {"Alice": 90, "Bob": 85}
if "Alice" in d:
print("Alice's marks are:", d["Alice"])
d = {"Alice": 90, "Bob": 85}
if "Alice" in d:
print("Alice's marks are:", d["Alice"])
Output
Alice'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.
v = {'a', 'e', 'i', 'o', 'u'}
print('e' in v)
v = {'a', 'e', 'i', 'o', 'u'}
print('e' in v)
Output
True
Explanation: The in operator checks if 'e' is present in the set v. Since 'e' exists in the set, the output will be True.