Operators in Python

Malik Fouzan Khan
5 min readMay 19, 2024

Today let us take a dig at types of operators available in python.

Primarily there are 7 operators in python, namely :

Arithmetic operators, Assignment operators, Comparison operators, Logical operators, Bitwise operators, Identity operators, Membership operators.

  1. Arithmetic operators : These are the basic arithmetic operators used to perform mathematical operations in the program.

Addition ( + ) : Used to add two operands(may or may not be numbers).

Subtraction ( — ) : Used to subtract two operands (may or may not be numbers).

Multiplication ( * ) : Multiplies two operands and returns the answer.

Division ( / ) : Divides the operands and returns quotient.

Modulo ( % ) : Divides the operands and returns remainder.

Floor division ( // ) : Performs division and after that rounds off the number to the nearest integer which is less than or equal to the answer.

Exponentiation ( ** ) : Used to find the power of a number.

Examples for Arithmetic operators

2. Assignment operators : These are operators used to assign values directly or assign them after manipulation.

Equal to ( = ) : Assigns the right hand side value to the left hand side variable.

Addition equal to ( += ) : Adds operands then assigns the value.

A += 10 -> A = A + 10

Subtraction equal to ( -= ) : Subtracts operands then assigns the value.

A -= 10 -> A = A -10

Multiply equal to ( *= ) : Multiplies then assigns value.

A *= 10 -> A = A *10

Divide equal to ( /= ) : Divides the operands before assigning.

A /= 10 -> A = A /10

Floor equal to ( //= ) : Performs floor division first then assigns value.

A //= 10 -> A = A //10

Exponent equal to ( **= ) : Performs exponentiation before assigning.

A **= 10 -> A = A **10

Modulo equal to ( %= ) : Performs modulus operation then assigns.

A %= 10 -> A = A %10

Example of Assignment operators

3. Comparison operators : They are also known as ‘Relational operators’. These operators helps us to compare two values (or variables). These are usually used in conditional statements. They return True or False as values ( 1 or 0 ).

Equal to ( == ) : This operators checks whether the values (or variables) are equal or not; if yes returns True or 1 as the value or else False or 0.

Not equal to ( != ) : It is similar to equal to operator but this operator is True if the values are not equal or else True.

Greater than ( > ) : Checks whether left hand value is greater than right hand value.

Lesser than ( < ) : Checks whether left hand value is lesser than right hand value.

Greater than or equal to ( ≥ ) : Returns True even if the value on left hand side is either greater or equal to right hand value.

Lesser than or equal to ( ≤ ) : Returns True even if the value on left hand side is either lesser or equal to right hand value.

Comparison operators in python

4. Logical operators : These operators use logic gates to perform operations and gives output. They consist of logical AND, logical OR and logical NOT.

Logical AND ( and ) : This returns a True value only if both the operands are True. In other words, it searches for the first False; if found returns 0 else returns 1.

Logical OR ( or ) : This returns a True if there is at least one True. This searches for first True; if found returns 1 else returns 0.

Logical NOT ( not ) : Negates the existing state of the expression. If it is True by default then changes it to False and vice versa.

Truth table for Logical operators

5. Bitwise operators : These are very important operators in any programming languages. They perform operations on binary level directly. These operators are very powerful as sometimes complex problems can be solved effectively using these operators. They are;

Bitwise AND ( & ) : Firstly the operands are converted into there respective binary form. Later AND gate operation is performed on each bit of the obtained binary number.

Bitwise OR ( | ) : Firstly the operands are converted into there respective binary form. Later OR gate operation is performed on each bit of the obtained binary number.

Bitwise XOR ( ^) : Firstly the operands are converted into there respective binary form. Later XOR gate operation is performed on each bit of the obtained binary number.

1’s Complement ( ~ ) : Performs 1’s complement to the binary number of the value given, i.e. , replace 0’s with 1’s and 1’s with 0’s in the binary number.

Left-shift ( << ) : Shifts all bits to the left side for the number of times mentioned. New bits created on the right are 0 by default. Each shift is equivalent to the multiplication of the given value by 2.

Right-shift ( >> ) : Shifts all bits to the right side for the number of times mentioned. The bits on Least significant bit (LSB) are discarded. Each right-shift is equivalent to the division of the given value by 2.

Bitwise operators

6. Identity operators : These operators are used to determine the identity of the given variable. These operators check the value and the memory address of the variable or object.

Is operator ( is ) : Returns True if the objects (or variable) refer to the same memory location.

Is not operator ( is not ) : Returns True if the objects (or variable) does not refer to the same memory location.

7. Membership operators : These operators check whether the given item or variable is available in given object.

In operator ( in ) : Returns True if the item is available in the object.

Not in operator ( not in ) : Returns True if the item is not available in the object.

Identity and Membership operators

--

--