Python Operators

Operators are used to perform operation on variables and values. The following types of operators are supported in Python:

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

Arithmetic Operators

Operator Description Example
+ Addition 2+3 = 5
- Subtraction 10-5 = 5
* Multiplication 2*5 = 10
/ Division 10/2 = 5
% Modulus 11%5 = 1
** Exponent 2**3 = 8 (same as 2*2*2)
// Floor division 15//2 = 7


Assignment Operators

Operator Description Example
= Assign the values from right side operand to the left side operand a = 5
+= Add the values of right operand to the left operand and assigns the result to the left operand a += 5 is equivalent to a = a+5
-= Subtract the values of right operand to the left operand and assigns the result to the left operand a -= 5 is equivalent to a = a-5
*= Multiply the values of right operand to the left operand and assigns the result to the left operand a *= 5 is equivalent to a = a*5
/= Divide the values of right operand to the left operand and assigns the result to the left operand a /= 5 is equivalent to a = a/5
%= Take the modulus value using two operands and assigns the result to the left operand. a %= 5 is equivalent to a = a%5
**= Performs exponential(power) calculation on operators and assign value to the left operand a ** 5 is equivalent to a = a**5
//= Performs floor division calculation on operators and assign value to the left operand a // 5 is equivalent to a = a//5


Comparison Operator

Operator Description Example
== Is equal to a == b is not true
!= Not equal to a == b is true
> Greater than a>b is not true
< Less than a<b is true
>= Greater than or equal to a>=b is not true
<= Less than or equal to a<=b is true


Logical Operator

Operator Description Example
&& Logical AND 5==5 && 7==9 is false
|| Logical OR 5==5 || 7==9 is true
! Logical Not !(7==9) is true


Membership Operator

Operator Description Example
in Evaluates to true if a sequence with the specified value is present in the object a in b
not in Evaluates to true if a sequence with the specified value is not present in the object a not in b


Identity Operator

Operator Description Example
is Evaluates to true if both the variables are the same object a is b
is not Evaluates to true if both the variables are not the same object a is not b


Bitwise Operator

Operator Description Example
& Binary AND 3==5 & 7==9 is false
| Binary OR 3==5 || 7==9 is false
^ Binary XOR 3==5 ^ 7==9 is false
~ Binary Ones Complement (~2) = -2
<< Binary Left Shift (10<<2) = 40
>> Binary Right Shift (10>>2) = 2