Understanding the Python "in" Keyword: Check Presence and Iterate Through Sequences
☰Fullscreen
Table of Content:
Definition and Usage
The in keyword has two purposes:
1. The in keyword is used to check if a value is present in a sequence (list, range, string etc.).
languages = ["English", "Bangla", "Hindi"]
if "Bangla" in languages:
print("yes")
Output
yes
2. The in keyword is also used to iterate through a sequence in a for loop:
languages = ["English", "Bangla", "Hindi"] for item in languages: print(item)
Output
English Bangla Hindi
Looping Through a String
Even strings are iterable objects, they contain a sequence of characters. Loop through the letters in the word "Hello"
for x in "Hello": print(x)
Output
H e l l o