Operators (Cheatsheet)
Operators (Cheatsheet)
6: Logic operators
7: Identity operators
8: Membership operators
Addition
Can be used to add numbers (integers or floats) or join (concatenate) strings.
Subtraction
Can be used mathematically, between integers and floats:
You can also subtract sets to return elements of set_1 that aren’t in set_2 :
Multiplication
Has the typical arithmetic use.
Division
Can be true division, which always results in a float.
Or floor division, which always rounds down to the nearest whole number.
Modulo
Based on Euclidean division (see ref #2 below)
Start with a dividend and a divisor
Used to get the remainder of the division between both operands.
Exponentiation
Raises the first operand to the power of the second operand.
Extra Resources
How do mathematics work in Python
Python’s modulo operator and floor divisio
Python’s divmod function
Comparison operators
Equal
Used to compare two values. They can be numbers, strings, or
anything else.
Not equal
The opposite of == .
Greater than
Less than
Assignment operators
Operator Example Explanation
= var = 2 Makes var equal to the value 2.
+= var += 2 Adds 2 to var.
-= var -= 2 Subtracts 2 from var.
*= var *= 2 Multiplies var by 2.
**= var **= 2 Elevates var to the power of 2.
/= var /= 2 Divides var by 2.
//= var //= 2 Divides var by 2 using floor division.
%= var %= 2 Calculates the remainder of var / 2.
Extra Resources
Conditionals and Booleans
Logic Operators
And
Returns the first value if it evaluates to False , otherwise it returns
the second value.
Statement Result
True and True True
True and False False
False and False False
False and True False
Or
Returns the first value if it evaluates to True , otherwise it returns
the second value.
Statement Result
True or True True
True or False True
False or False False
False or True True
Not
Returns True if the operand evaluates to False , or False if it
evaluates to True .
Statement Result
not True False
not False True
Identity operators
is
Returns True if the operands have the same identity (i.e. are
exactly the same value).
is not
Returns True if the operands don’t have the same identity.
Extra Resources
Logical comparisons in Python: and & or
Membership operators
in
Returns True if an element exists inside an object.
not in
Returns True if an element does not exist inside an object.