Skip to content

Operators

Operators combine values, compare values, and build conditions.

Use = to store a value. Use == to compare two values. Scripts do not use ===.

target = 10
if value == target:
print("match")

= changes a variable. == asks a question and returns True or False.

a == b # equal
a != b # not equal
a < b # less than
a <= b # less than or equal
a > b # greater than
a >= b # greater than or equal

Use and, or, and not to combine conditions:

if powered and battery > 100:
print("ready")
if not blocked:
print("clear")
a + b # add
a - b # subtract
a * b # multiply
a / b # divide
a // b # floor division
a % b # remainder, also called modulo
a ** b # power

Use / when you want normal division. Use // when you want the whole-number quotient. Use % when you want the remainder after division.

Modulo is useful for repeating patterns and even/odd checks:

value % 2 == 0 # even
value % 2 == 1 # odd

Use in to check whether a value is inside a list, tuple, set, string, or dict keys. Use is None for the special empty value.

if target in scanned:
print("known")
if result is None:
print("nothing found")