Python – Operators

Operators are used to combining two operands into a single operation. The following are the different types of Python operators:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Identity operators
  • Membership operators
  • Bitwise operators

Python Arithmetic operators

To execute arithmetic operations on two operands, arithmetic operators are employed.


Operator Name Description
+ Addition Add two values
Subtraction Subtract one value from another
* Multiplication Multiply two values
/ Division Divide one value by another
** Exponent / Power Returns first operand raised to the power of the second operand
% Modulo Returns remainder of the division operation
// Floor division Returns quotient of the division operation

Python Assignment operators

Assignment operators are used to assign values from the right-hand side expression to the operand on the left-hand side.


Operator Expression Equivalent to
= a = 5 a = 5
+= a += b a = a + b
-= a -= b a = a – b
*= a *= b a = a * b
/= a /= b a = a / b
**= a **= b a = a ** b
%= a %= b a = a % b
//= a //= b a = a // b
&= a &= b a = a & b
|= a |= b a = a | b
^= a ^= b a = a ^ b
>>= a >>= b a = a >> b
<<= a <<= b a = a << b

Python Comparison operators

To compare the values of two operands, comparison operators are employed. When the values match, it returns true, and when they don’t, it returns false.


Operator Description
== Equal
!= Not equal
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

Python Logical operators

Logical operators are used to combining two or more conditions.


Operator Description
and Returns true when all conditions are true
or Returns true when any of the conditions is true
not Returns opposite boolean result

Python Identity operators

Identity operators are used to compare the memory location of two objects.


Operator Description
is Returns true when both variables are the same object
is not Returns true when both variables are not the same object

Python Membership operators

Membership operators are used to check the membership of element(s) in a sequence like lists, tuple etc.


Operator Description Example
in Returns true if a given element is present in the object a in b
not in Returns true if a given element is not present in the object a not in b

Python Bitwise operators

Bitwise operators are used to perform bitwise operations on two operands.


Operator Name Description
& AND Returns 1 if both bits at the same position in both operands are 1, else returns 0
| OR Returns 1 if one of two bits at the same position in both operands is 1, else returns 0
^ XOR Returns 1 if only one of two bits at the same position in both operands is 1, else returns 0
~ NOT Reverse all the bits
>> Right shift The left operand is moved right by the number of bits present in the right operand
<< Left shift The left operand value is moved left by the number of bits present in the right operand

Python Operators Precedence

Operator precedence (order of operations) refers to a set of rules that specify which procedures should be executed first when evaluating a given expression.

For example, multiplication has higher precedence than addition. Thus, the expression 1 + 2 × 3 is interpreted to have the value 1 + (2 × 3) = 7, and not (1 + 2) × 3 = 9. When exponent is used in the expression, it has precedence over both addition and multiplication. Thus 3 + 52 = 28 and 3 × 52 = 75.

The precedence and associativity of Python operators are listed in the table below. Operators are listed in ascending order from top to bottom. Operators with a higher precedence are rated first, followed by operators with a lower precedence. Associativity of the operators dictates the sequence in which operations are done when they have the same precedence.

Except for exponentiation, which has associativity from right to left, operators in the same group have associativity from left to right.


Precedence Operator Description
18 (expressions…) Binding or parenthesized expression
[expressions…] List display
{key: value…} Dictionary display
{expressions…} Set display
17 x[index] Subscription
x[index:index] Slicing
x(arguments…) Call
x.attribute Attribute reference
16 await x Await expression
15 ** Exponentiation
14 +x  -x Unary plus, Unary negation
~ Bitwise NOT
13 *  /  //  % Multiplication, Division, Floor division, Remainder
@ Matrix multiplication
12 +  – Addition, Subtraction
11 <<  >> Bitwise left shift, right shift
10 & Bitwise AND
9 ^ Bitwise XOR
8 | Bitwise OR
7 <  <=  >  >= Less than, Less than or equal, Greater than, and Greater than or equal
is Identity operators
is not
in Membership operators
not in
6 not x Boolean NOT
5 and Boolean AND
4 or Boolean OR
3 if-else Conditional expression
2 lambda Lambda expression
1 = Direct assignment
+=  -=  *=  /=  //=  %=  **= Compound assignment by addition, subtraction, multiplication, division, floor division, remainder, and exponentiation
<<=  >>= Compound assignment by Bitwise left shift, right shift
&=  ^=  |= Compound assignment by Bitwise AND, XOR and OR

Leave a Reply

Your email address will not be published.