Python in Keyword

❮ Python Keywords


Example

Check if "banana" is present in the list:

fruits = ["apple", "banana", "cherry"]

if "banana" in fruits:
  print("yes")
Try it Yourself »

Definition and Usage

The in keyword has two purposes:

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

The in keyword is also used to iterate through a sequence in a for loop:

Example

Loop through a list and print the items:

fruits = ["apple", "banana", "cherry"]

for x in fruits:
  print(x)
Try it Yourself »

❮ Python Keywords

Copyright 1999-2023 by Refsnes Data. All Rights Reserved.