Python Operators
Assignment Comparison
operators operators
Arithmetic 2 3
Logical operators
operators 4
1
Types of Python
Operators
Identity operators 5 Special operators
8
6 7
Membership Bitwise operators
operators
Python Operators
Operators are used to perform operations on variables and
values.
In the example below, we use the + operator to add together
two values:
Python divides the operators in the following groups:
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Identity operators
• Membership operators
• Bitwise operators
Python Arithmetic Operators
Arithmetic operators are used with numeric values to
perform common mathematical operations:
Operator Name Example
+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus x%y
** Exponentiation x ** y
// Floor division x // y
Python Assignment Operators
Assignment operators are used to assign values to
variables:
Operator Example Same As
= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x–3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
//= x //= 3 x = x // 3
**= x **= 3 x = x ** 3
&= x &= 3 x=x&3
|= x |= 3 x=x|3
^= x ^= 3 x=x^3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3
Python Comparison 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 x >= y
to
<= Less than or equal to x <= y
Python Logical Operators
Logical operators are used to combine conditional
statements:
Operator Description Example
Returns True if both
And x < 5 and x < 10
statements are true
Returns True if one of
Or the statements is true
x < 5 or x < 4
Reverse the result,
Not returns False if the result not(x < 5 and x <
is true 10)
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
Returns True if
is both variables are x is y
the same object
Returns True if
is not both variables are x is not y
not the same object
Python Bitwise Operators
Bitwise operators are used to compare (binary) numbers:
Operator Name Description Example
Sets each bit to 1 if both bits
& AND x&y
are 1
Sets each bit to 1 if one of two
| OR x|y
bits is 1
Sets each bit to 1 if only one
^ XOR x^y
of two bits is 1
~ NOT Inverts all the bits ~x
Shift left by pushing zeros in
<< Zero fill left shift from the right and let the x << 2
leftmost bits fall off
Shift right by pushing copies
Signed right of the leftmost bit in from the
>> x >> 2
shift left, and let the rightmost bits
fall off