Python Operators
Operators are used to perform operations on variables and values. These are standard
symbols used for the purpose of logical and arithmetic operations. Python divides the
operators in the following groups:
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Identity operators
Membership operators
Bitwise operators
Arithmetic Operators
Arithmetic operators are used to performing mathematical operations like addition,
subtraction, multiplication, and division.
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
Dept. of AEI Government Engineering College Kozhikode
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
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 to x >= y
<= Less than or equal to x <= y
Dept. of AEI Government Engineering College Kozhikode
Logical Operators
Logical operators are used to combine conditional statements.
Operator Description Example
and Returns True if both statements x < 5 and x < 10
are true
or Returns True if one of the x < 5 or x < 4
statements is true
not Reverse the result, returns False not(x < 5 and x < 10)
if the result is true
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 x is y
are the same object
is not Returns True if both variables x is not y
are not the same object
Membership Operators
Membership operators are used to test if a sequence is presented in an object.
Operator Description Example
in Returns True if a sequence with x in y
the specified value is present in
the object
not in Returns True if a sequence with x not in y
the specified value is not present
in the object
Dept. of AEI Government Engineering College Kozhikode
Bitwise Operators
Bitwise operators are used to compare (binary) numbers.
Operator Name Description
& AND Sets each bit to 1 if both bits are 1
| OR Sets each bit to 1 if one of two bits is 1
^ XOR Sets each bit to 1 if only one of two bits is 1
~ NOT Inverts all the bits
<< Zero fill left shift Shift left by pushing zeros in from the right and let the
leftmost bits fall off
>> Signed right shift Shift right by pushing copies of the leftmost bit in from
the left, and let the rightmost bits fall off
Ternary Operator in Python
Ternary operators are also known as conditional expressions are operators that evaluate
something based on a condition being true or false. It was added to Python in version 2.5.
It simply allows testing a condition in a single line replacing the multiline if-else making the
code compact.
[on_true] if [expression] else [on_false]
Example,
a, b = 10, 20
# Copy value of a in min if a < b else copy b
min = a if a < b else b
print(min)
Dept. of AEI Government Engineering College Kozhikode
Python Operators Precedence
The following table lists all operators from highest precedence to lowest.
Sl.No. Operator & Description
1 **
Exponentiation (raise to the power)
2 ~+-
Complement, unary plus and minus (method names for the last two are +@ and -@)
3 * / % //
Multiply, divide, modulo and floor division
4 +-
Addition and subtraction
5 >><<
Right and left bitwise shift
6 &
Bitwise 'AND'
7 ^|
Bitwise exclusive `OR' and regular `OR'
8 <= <>>=
Comparison operators
9 <> == !=
Equality operators
10 = %= /= //= -= += *= **=
Assignment operators
11 is is not
Identity operators
12 in not in
Membership operators
13 not or and
Logical operators
Dept. of AEI Government Engineering College Kozhikode