Skip to content

Python if in statement

  • by

Python if statement uses the in operator, it can see if a particular value is present. Python “if in statement” makes decisions.

Python if in

Simple example code If statements that check for values: if with in. The if statement has the in operator to see whether exampleStr contains the ‘gene’ phrase.

exampleStr = "Python is a general-purpose programming language."

# Look for substring in source string
if 'gene' in exampleStr:
    print("Found! 'gene'.")
else:
    print("Didn't find the substring.")

Output:

Python if in statement

If statement that sees if a list has a certain value

Use the in operator is to see if some list has a particular value

sickDays = [2, 1, 0, 0, 5, 8, 4, 9]

# Check for 9 absence days
if 9 in sickDays:
    print('Ouch, someone was sick 9 days in a row!')
else:
    print("We don't have anyone sick 9 consecutive days.")

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

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 *