Conditional Statements

At the base of every computer is a series of switches, operating on ones and zeros. These can also be represented as Boolean values. In Python, Boolean values are represented as True and False. These are key words in Python, so they cannot be used for anything except for Boolean values. Boolean values are most commonly used in programming logic. In Python, this takes the form of if statements. An if statement is written as follows:

if condition:
    code block that runs if condition is true

This can also read like "if condition is true, then do this." Also note that the code that is a part of the if statement (also called the body) is indented with a single tab. This is how Python denotes code blocks that belong to particular statements. In many other languages, this is done with curly brackets "{ }".

In [1]:
x = True
if x:
    print("X is True")
X is True

The code above is a basic if statement that exectutes the if body because x is true...But what if x was false?

In [1]:
x = False
if x:
    print("X is True")
else:
    print("X is False")
X is False

If the condition of the if statement is false then the else block of code will run. The else block is not required, but it can be useful in many situations. You can also check for more than just one condition by using the elif keyword:

In [15]:
score = 85
if score >= 90:
    print("Your grade is an A.")
elif score >= 80:
    print("Your grade is an B.")
elif score >= 70:
    print("Your grade is an C.")
elif score >= 60:
    print("Your grade is an D.")
else:
    print("Your grade is an F.")
Your grade is an B.

if statements, along with their associated elif and/or else branches can be nested in one another any number of times.

In [19]:
score = 93
if score >= 90:
    if score > 97:
        print("Your grade is an A+.")
    elif score > 93:
        print("Your grade is an A.")
    else:
        print("Your grade is an A-.")
Your grade is an A-.

In Python, we can test conditions using a variety of operators that include:

  • == is the equality operator that checks if both operands have the same value. Note that this is different than the single equals sign = which is the assignment operator.
  • != checks to see if the values are not equal
  • > checks if the left operand is greater than the right operand
  • >= checks if the left operand is greater than or equal to the right operand
  • < checks if the left operand is less than the right operand
  • <= checks if the left operand is less than or equal to the right operand
  • or checks if either the left or the right operand is true
  • and checks if both of the operands are true
  • not flips the Boolean value of the operand (ex: not False becomes True and vice versa)
  • is checks if the operands refer to the same object (examples below)
In [1]:
x = 5
if x == 5:
    print("Condition is True")
else:
    print("Condition is False")
Condition is True
In [2]:
x = 5
y = 10
if x != y:
    print("Condition is True")
else:
    print("Condition is False")
Condition is True
In [3]:
x = 5
y = 10
if x > y:
    print("Condition is True")
else:
    print("Condition is False")
Condition is False
In [4]:
x = 5
y = 5
if x >= y:
    print("Condition is True")
else:
    print("Condition is False")
Condition is True
In [5]:
x = 5
y = 5
if x <  y:
    print("Condition is True")
else:
    print("Condition is False")
Condition is False
In [6]:
x = 5
y = 5
if x <= y:
    print("Condition is True")
else:
    print("Condition is False")
Condition is True
In [7]:
x = 5
y = 10
if x < y or y > 10:
    print("Condition is True")
else:
    print("Condition is False")
Condition is True
In [8]:
x = 5
y = 10
if x < y and y > 10:
    print("Condition is True")
else:
    print("Condition is False")
Condition is False
In [9]:
x = 5
y = 5
if not x <  y:
    print("Condition is True")
else:
    print("Condition is False")
Condition is True
In [10]:
x = True
if not x:
    print("Condition is True")
else:
    print("Condition is False")
Condition is False

The is operator is not as intuitive as the other Boolean operators. is checks if the operands refer to the same object. It is most commonly used to check if something is None, the Python reserved word to represent an object that has no value. There are cases where you can use == to check for None, but is is the more reliable solution:

In [12]:
x = None
if x is None:
    print("Condition is True")
else:
    print("Condition is False")
Condition is True
In [13]:
x = None
if x is not None:
    print("Condition is True")
else:
    print("Condition is False")
Condition is False

You can also check for variables that reference the same object:

In [14]:
x = 'stuff'
y = x
if x is y:
    print("Condition is True")
else:
    print("Condition is False")
Condition is True