Skip to content

Python in keyword | Uses and example code

  • by

Python in keyword can use in two ways, first check if a value is present in a sequence (list, range, string, etc.) and another used in iterating through a sequence in a for loop.

# Using if statement

if ele in seq:
    statement(s)

# Using for statement

for ele in seq:
    statement(s)

Note: in is a keyword (case-sensitive) in python,

Python in keyword examples

Simple example code program to demonstrate the use in keywords to check whether an element exists in the given sequence like string, list, tuple, etc.

Example 1

Using if statement

fruits = ["Apple", "Orange", "Cherry"]

# Check if Apple in list
if "Apple" in fruits:
    print("Yes 'Apple' in List")

Output:

Python in keyword

Example 2

Using for statement

fruits = ["Apple", "Orange", "Cherry"]

# Print list
for x in fruits:
    print(x)

Output:

Apple
Orange
Cherry

Do comment if you have any doubts or suggestions on this Python keyword tutorial.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *