Python in Keyword

Example

Check if "banana" exists in the list:

fruits = ["apple", "banana", "cherry"]
if "banana" in fruits:
  print("yes")

Run Example

Definition and Usage

The 'in' keyword has two uses:

The 'in' keyword is used to check if a value exists in a sequence (list, range, string, etc.).

The 'in' keyword is also used to iterate over sequences in for loops:

Example

Traverse the list and print items:

fruits = ["apple", "banana", "cherry"]
for x in fruits:
  print(x)

Run Example