Python Operators Reference with Examples
Python Arithmetic Operators
Arithmetic operators are used with numeric values to perform common mathematical
operations.
Operator Description Example
+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus x%y
** Exponentiation x ** y
// Floor division x // y
x = 15 y = 4 print(x + y) print(x - y) print(x * y) print(x / y) print(x
% y) print(x ** y) print(x // y)
Python Assignment Operators
Assignment operators are used to assign values to variables.
Operator Description Example
= Assign x=5
+= Add and assign x += 3
-= Subtract and assign x -= 3
*= Multiply and assign x *= 3
/= Divide and assign x /= 3
%= Modulus and assign x %= 3
//= Floor division and assign x //= 3
**= Exponent and assign x **= 3
&= Bitwise AND and assign x &= 3
|= Bitwise OR and assign x |= 3
^= Bitwise XOR and assign x ^= 3
>>= Right shift and assign x >>= 3
<<= Left shift and assign x <<= 3
:= Walrus operator x := 3; print(x)
numbers = [1, 2, 3, 4, 5] if (count := len(numbers)) > 3: print(f'List
has {count} elements')
Python Comparison Operators
Comparison operators are used to compare two values and return True or False.
Operator Description Example
== Equal x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater or equal x >= y
<= Less or equal x <= y
x = 5 y = 3 print(x == y) print(x != y) print(x > y) print(x < y)
print(x >= y) print(x <= y) print(1 < x < 10)
Python Logical Operators
Logical operators combine conditional statements.
Operator Description Example
and True if both statements are true x < 5 and x < 10
or True if one statement is true x < 5 or x < 4
not Reverse result not(x < 5 and x < 10)
x = 5 print(x > 0 and x < 10) print(x < 5 or x > 10) print(not(x > 3 and
x < 10))