Python 3 Operators

List of operators available in Python 3.

Arithmetic Operators

Operator Name Operation Result Example
+ Addition x + y Sum of x and y
Result
700
- Subtraction x - y Difference of x and y
Result
300
* Multiplication x * y Product of x and y
Result
100000
/ Division x / y Quotient of x and y
Result
2.5
// Floor Division x // y Floored quotient of x and y
Result
2
% Modulus x % y Remainder of x / y
Result
100
** Exponent x ** y x to the power y
Result
250000

Unary Operations for + and -

A unary operation is an operation with one operand.

Operator Meaning Operation Result Example
+ Unary positive +x x unchanged
Result
700
- Unary negative -x x negated
Result
-300

Comparison (Relational) Operators

Comparison operations in Python have the same priority, which is lower than that of any arithmetic, shifting or bitwise operation.

Operator Meaning Operation Result Example
== Equal to x == y True if x is exactly equal to y. Otherwise False.
Result
True
False
!= Not equal to x != y True if x is not equal to y. Otherwise False.
Result
False
True
> Greater than x > y True if x is greater than y. Otherwise False.
Result
False
True
< Less than x < y True if x is less than y. Otherwise False.
Result
True
False
>= Greater than or equal to x >= y True if x is greater than or equal to y. Otherwise False.
Result
True
False
<= Less than or equal to x <= y True if x is less than or equal to y. Otherwise False.
Result
True
False

Logical Operators

Operator Operation Result Example
and x and y True if both x and y are True. Otherwise False.
Result
True
False
or x or y True if either x or y are True. Otherwise False.
Result
True
False
not not x == y or not(x == y) If x is True then return False.
Result
False
True
False

Assignment Operators

Operator Operation Result Example
= z = x + y Assigns the value of its right operand/s to its left operand.
Result
3
+= x += y Same as x = x + y.
Result
3
-= x -= y Same as x = x - y.
Result
-1
*= x *= y Same as x = x * y.
Result
6
/= x /= y Same as x = x / y.
Result
3.0
%= x %= y Same as x = x % y.
Result
2
**= x **= y Same as x = x ** y.
Result
16
//= x //= y Same as x = x // y.
Result
6

Bitwise Operators

The following bitwise operations can be performed on integers. These are sorted in ascending priority.

Operator Operation Result Example
| x | y Bitwise or of x and y
Result
508
^ x ^ y Bitwise exclusive of x and y
Result
316
& x & y Bitwise exclusive of x and y
Result
192
<< x << n x shifted left by n bits
Result
2000
>> x >> n x shifted right by n bits
Result
125
~ ~x The bits of x inverted
Result
-501

The @ Operator

Python also lists the @ symbol as an operator. The @ symbol is used for the Python decorator syntax. A decorator is any callable Python object that is used to modify a function, method or class definition. A decorator is passed the original object being defined and returns a modified object, which is then bound to the name in the definition.

If you're interested in learning more about Python decorators, see the Python wiki.

Ternary (Conditional) Operator

In Python, you can define a conditional expression like this:

The condition (C) is evaluated first. If it returns True, the result is x, otherwise it's y.

Example:

Result
Low