print(2 + 10)
print(2 - 10)
print(2 * 10)
print(2 / 10)
12
-8
20
0.2
We have seen examples of arithmetic operators, and the double equals sign, but let’s cover a wide range of operators in Python.
The Python Operators page from W3 Schools provides an excellent overview. Contents copied below for reference.
Examples (with numbers):
Examples:
Examples (with strings):
Examples (with lists):
Examples (for compound expressions):
The and
operator will return True
if BOTH sides are true.
True
False
False
False
The or
operator will return True
if EITHER side is true.
True
True
True
False
Another way of using the or
operator is within the concept of “truthiness”.
In this context, the or
operator will return the first “truthy” value.
Values that are considered “truthy” are True
, or any other object that is present or non-empty, or non-blank.
Values that are considered “falsy” are False
, None
, empty string (""
), empty list ([]
), etc.
In this case, we see 4
returned because it is the first truthy value.
In practice, we usually use “truthiness” to set a value if it is not null: