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 "{ }".
x = True
if x:
print("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?
x = False
if x:
print("X is True")
else:
print("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:
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.")
if statements, along with their associated elif and/or else branches can be nested in one another any number of times.
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-.")
In Python, we can test conditions using a variety of operators that include:
x = 5
if x == 5:
print("Condition is True")
else:
print("Condition is False")
x = 5
y = 10
if x != y:
print("Condition is True")
else:
print("Condition is False")
x = 5
y = 10
if x > y:
print("Condition is True")
else:
print("Condition is False")
x = 5
y = 5
if x >= y:
print("Condition is True")
else:
print("Condition is False")
x = 5
y = 5
if x < y:
print("Condition is True")
else:
print("Condition is False")
x = 5
y = 5
if x <= y:
print("Condition is True")
else:
print("Condition is False")
x = 5
y = 10
if x < y or y > 10:
print("Condition is True")
else:
print("Condition is False")
x = 5
y = 10
if x < y and y > 10:
print("Condition is True")
else:
print("Condition is False")
x = 5
y = 5
if not x < y:
print("Condition is True")
else:
print("Condition is False")
x = True
if not x:
print("Condition is True")
else:
print("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:
x = None
if x is None:
print("Condition is True")
else:
print("Condition is False")
x = None
if x is not None:
print("Condition is True")
else:
print("Condition is False")
You can also check for variables that reference the same object:
x = 'stuff'
y = x
if x is y:
print("Condition is True")
else:
print("Condition is False")