5 < 7True
A Boolean expression is one that evaluates to either True or False.
5 < 7True
Given two values a and b, the following are Boolean comparisons:
| Description | Operator | Python |
|---|---|---|
| Less than | \(<\) | a < b |
| Less than or equal | \(\leq\) | a <= b |
| Greater than | \(>\) | a > b |
| Greater than or equal | \(\geq\) | a >= b |
| Equal to | \(=\) | a == b |
That last one can be easy to mix up with assignment.
if CONDITION:
BODY OF CODESometimes we want to do one thing if a condition is true, else we want to do something else if it wasn’t true.
num = np.random.rand()
if num < 0.5:
print('HEADS')
else:
print('TAILS')import numpy as np
arr = np.array([[1, 2],[3, 4],[5 ,6]])
arr[:,1] > 2array([False, True, True])