Python in Keyword

Rumman Ansari   Software Engineer   2023-02-05   350 Share
☰ Table of Contents

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

Python In Keyword