Operators in Python

  • Last updated Apr 25, 2024

In Python, operators are special symbols like +, −, *, /, %, etc, which are used to perform operations on operands (values and variables).

These are the list of operators in Python:

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

Arithmetic operators are used to perform calculations, such as addition, subtraction, multiplication, and division.

The following table shows the lists of arithmetic operators supported by Python:

Operators
Name
Description
Example
+
Addition
Adds two or more values.
a = 4
b = 6
c = a + b
-
Subtraction
Subtracts the right operand from the left operand. a = 10
b = 6
c = a - b
*
Multiplication
Multiplies two values.
a = 2
b = 6
c = a * b
/
Division
Divides the left operand by the right operand.
a = 10
b = 2
c = a / b
%
Modulus
Returns the remainder of the division.
a = 10
b = 2
c = a % b
** Exponentiation
Perform exponential operation. Raises the left operand to the power of the right operand.
a = 10
b = 2
c = a ** b
// Floor Division
Returns the floor value of the division.
a = 51
b = 2
c = a // b
Logical Operators

Logical operators are used to determine the logic between variables(values).

The following table shows the lists of logical operators supported by Python:

Operators
Name
Description
Example
and
Logical AND
Returns true, if and only if both statements are true.
175 > 156 and 253 > 130
or
Logical OR
Returns true, if one of the given statements is true.
147 < 112 or 350> 330
not
Logical NOT
Change the result to the opposite, returns true if the result is false.
not(2 > 5)
Assignment Operators

Assignment operators are used to assign a value to a variable on the left. The = sign is used for assignment operator.

The following table shows the lists of assignment operators supported by Python:

Operator Description Example
=
Assigns a value from the right to a variable on the left
a = 10
+=
Adds the right operand to the left operand and assigns the result to the left operand.
a += b
-=
Subtracts the right operand from the left operand and assigns the result to the left operand.
a -= b
*=
Multiplies the operand on the left by the operand on the right and assigns the result to the left operand.
a *= b
/=
Divides the left operand by the right operand and assigns the result to the left operand.
a /= b
%=
It is used for Modulus and Assignment operation. It calculates the remainder when the left operand is divided by the right operand and assigns the result back to the left operand.
a %= b
//=
Divides the left operand by the right operand and assigns the floor value result to the left operand.
a //= b
**=
Performs an exponential operation with the right operand and assigns the result to the left operand.
a **= b
&=
Performs a bitwise AND operation between the left operand and the right operand and assigns the result back to the left operand.
a &= b
^=
Performs a bitwise XOR operation between the left operand and the right operand and assigns the result back to the left operand.
a ^= b
|=
Performs a bitwise OR operation between the left operand and the right operand and assigns the result back to the left operand.
a |= b
<<=
Performs a bitwise left shift operation on the left operand by the number of positions specified by the right operand and assigns the result back to the left operand.
a <<= b
>>=
Performs a bitwise right shift operation on the left operand by the number of positions specified by the right operand and assigns the result back to the left operand.
a >>= b
Comparison Operators

The Comparison operators are used to compare two values.

The following table shows the lists of comparison operators supported by Python:

Operator Name Description Example
==
Equal
Compares and returns True if the left operand is equal to the right operand.
a == 10
!=
Not equal
Compares and returns True if the left operand is not equal to the right operand.
a != 10
>
Greater than
Compares and returns True if the left operand is greater than the right operand.
a > b
<
Less than
Compares and returns True if the left operand is less than the right operand.
a < b
>=
Greater than or equal to
Compares and returns True if the left operand is greater than or equal to the right operand.
a >= 10
<=
Less than or equal to
Compares and returns True if the left operand is less than or equal to the right operand.
a <= 10
Membership Operator

Membership operators are used used to test the membership in a sequence.

The following table lists the membership operators supported by Python:

Operator
Description
Example
in
Returns True if there is a value in the specified sequence.
a in b
not in
Return True if there is no value in the specified sequence.
a not in b
Identity Operator

Identity operators are used to compare objects if they are the same object and share the same memory location.

The following table lists the identity operators supported by Python:

Operator Description Example
is Returns True if the compared objects are the same object.
a is b
is not Returns True if the compared objects are not the same object.
a is not b
Bitwise Operator

Bitwise operators are used to perform manipulation on individual bits of integer types, long, int, short, byte and char values. Bitwise operations are generally faster than division, multiplication, and sometimes faster than addition.

Here are the lists of bitwise operators supported by Python:

  • Bitwise AND (&): The bitwise & operator is used to perform a bitwise AND operation. It is a binary operator. It is denoted by & symbol. This operator returns 1 if both of the bits are 1, else it gives 0.
  • Example:

    a = 4 (in decimal) = 0100 (in binary)
    b = 7 (in decimal) = 0111 (in binary)
    
    Bitwise AND Operation of 4 and 7
    
      0100
     & 0111
    _______________________________
      0100 = 4 (in decimal)
    
  • Bitwise OR (|): The bitwise | operator performs a bitwise inclusive OR operation. It is denoted by | symbol. This operator returns 1 if either of the bits is 1, else it gives 0.
  • Example:

    a = 4 (in decimal) = 0100 (in binary)
    b = 7 (in decimal) = 0111 (in binary)
    
    Bitwise OR Operation of 4 and 7
    
      0100
    | 0111
    ___________________________________
      0111 = 7 (in decimal)
  • Bitwise XOR (^): The bitwise ^ operator performs a bitwise exclusive OR operation. It is a binary operator. It is denoted by ^ symbol. It gives 1 if corresponding bits are different, else it gives 0.
  • Example:

    a = 4 (in decimal) = 0100 (in binary)
    b = 7 (in decimal) = 0111 (in binary)
    
    Bitwise XOR Operation of 4 and 7
    
      0100
    ^ 0111
    ___________________________________
      0011 = 3 (in decimal)
  • Bitwise Compliment (~): The unary bitwise complement operator ~ inverts a bit pattern; it can be applied to any of the integral types, making every "0" a "1" and every "1" a "0". For example, a byte value of 8 bits whose bit pattern is "00000000" would change to "11111111". It is denoted by ~.
  • Example:

    a = 4 (in decimal) = 0100 (in binary)
    
    Bitwise Compliment Operation of 4
    
    ~ 0100
    ________________________________
      1011 = 11 (in decimal)
  • Signed left shift operator (<< ): The signed left shift operator "<<" shifts a bit pattern to the left by the number of times specified by the right side of the operand. After the left shift, the empty space in the right is filled with 0. Left shifting a number by 1 is equivalent to multiplying it by 2.
  • Example:

    14 (decimal value) = 00001110 (binary value)
    
    (14 << 2) Move 14 by 2 places to the left
    
    00001110
    _____________________________________________________________________________
    00111000 = 56 (in decimal) (After moving 00001110 by 2 places to the left.)
  • Signed right shift operator (>> ): The signed right shift operator ">>" shifts all bits to the right.
  • Example:

    14 (decimal value) = 00001110 (binary value)
    
    (14 >> 2) Move 14 by 2 places to the right
    
    00001110
    _____________________________________________________________________________
    00000011 = 3 (in decimal) (After moving 00001110 by 2 places to the right.)