Python in keyword | Uses and example code

Python in keyword can use two ways, first check if a value is present in a sequence (list, range, string, etc.) and another used in iterate through a sequence in a for a 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 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 and suggestions on this Python keyword tutorial.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

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


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.