Python Operators
Python divides the operators in the following groups:
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Identity operators
Membership operators
Bitwise operators
Comparison operators are used to compare two values:
Operator Name Example
== Equal x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
Python Identity Operators
Identity operators are used to compare the objects, not if they are equal, but if
they are actually the same object, with the same memory location:
Operator Description Example
is Returns True if both variables are the same object x is y
is not Returns True if both variables are not the same x is not y
object
Python Membership Operators
Operator Description E
in Returns True if a sequence with the specified value is present in x
the object
not in Returns True if a sequence with the specified value is not present x
in the object
Membership operators are used to test if a sequence is presented in an object:
Operator Precedence
Operator precedence describes the order in which operations are performed.
Example
Parentheses has the highest precedence, meaning that expressions inside
parentheses must be evaluated first:
print((6 + 3) - (6 + 3))
Run example »
Example
Multiplication * has higher precedence than addition +, and therefor
multiplications are evaluated before additions:
print(100 + 5 * 3)
Run example »
The precedence order is described in the table below, starting with the highest
precedence at the top:
Operator Description
() Parentheses
** Exponentiation
+x -x ~x Unary plus, unary minus, and bitwise NOT
* / // % Multiplication, division, floor division, and modulu
+ - Addition and subtraction
<< >> Bitwise left and right shifts
& Bitwise AND
^ Bitwise XOR
| Bitwise OR
== != > >= < <= is is not in not Comparisons, identity, and membership operator
in
not Logical NOT
and AND
or OR
If two operators have the same precedence, the expression is evaluated from
left to right.