Operators
Operators combine values, compare values, and build conditions.
Assignment vs equality
Section titled “Assignment vs equality”Use = to store a value. Use == to compare two values. Scripts do not use ===.
target = 10if value == target: print("match")= changes a variable. == asks a question and returns True or False.
Comparisons
Section titled “Comparisons”a == b # equala != b # not equala < b # less thana <= b # less than or equala > b # greater thana >= b # greater than or equalBoolean logic
Section titled “Boolean logic”Use and, or, and not to combine conditions:
if powered and battery > 100: print("ready")
if not blocked: print("clear")Math and division
Section titled “Math and division”a + b # adda - b # subtracta * b # multiplya / b # dividea // b # floor divisiona % b # remainder, also called moduloa ** b # powerUse / 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 # evenvalue % 2 == 1 # oddMembership and None
Section titled “Membership and None”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")See also
Section titled “See also”- Numbers: how the numeric type family works
- Language Reference: bitwise and set operators