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:
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.")
If statement that sees if a list has a certain value
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)
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.