Python Operators

Python Operators are symbols to perform a specific mathematical, relational or logical operation and produce the final result.

Python Operators helps us to perform a lot of operations on Python Variables. These are required for arithmetic operations, to match different variables, assigning value to other variables, and many other operations.


Type of Python variables

Python Operators are categorised into the following groups:

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

In this Python tutorial, we are going to learn all these Python Operators with examples step by step.


Arithmetic operators

The Python Arithmetic operators are used with numeric values to perform common arithmetical operations, such as addition, subtraction, multiplication, etc.

Operator Description Example
+ Addition of two operands. x + y
Subtracts second operand from the first. x − y
* Multiplies both operands. x * y
/ Divides numerator by denominator and gives us the Quotient. y / x
% Modulus Operator: calculate remainder of after an integer division. y % x
** Exponentiation y to x i.e. xy x**y
// Floor Division of x//y rounds the result down the division to the nearest whole number x//y

Assignment operators

The Python assignment operators are used with numeric values to assign a value to a variable. One of the common assignment operators in Python is “=”. It means that the left operand gets set to the value of the assignment expression on the right. Mostly when we declare a variable, we also assign a value to it.

Here is the table which shows all the assignment OPerators supported in Python Programming.

Operator Description Example
= assign it’s value to the other x = y
+= Assign the value after addition x+=y same as x=x+y
-= Assign the value after subtraction x-=y same as x=x-y
*= Assign the value after multiplication x*=y same as x=x*y
/= Assign the value after calculating quotient x/=y same as x=x/y
%= Assign the value after calculating remainder x%=y same as x=x%y
// Floor Division of x//y rounds the result down the division to the nearest whole number x//y

Comparison Operators

The Python Comparison Operators are used to compare two values. The values can be of any type such as numbers, strings, complex numbers, etc. Comparison operators are also known as Relational Operators. Here is the table which shows all Comparison supported in Python programming:

Operator Description Example
== If the values of both operands are equal, then the condition becomes true, and return True otherwise return false. A == B.
!= If values are not equal, then the condition becomes true and return trur otherwise return false. A != B.
> If the value of left operand is greater than the value of right operand, then the condition becomes true. A > B
< If value of left operand is less than the value of right operand, then the condition becomes true and return 1 otherwise return 0(false). A < B
>= Either the value of left operand is greater than or equal to the value of right operand, then the condition becomes true and return 1 otherwise return 0(false). A>=B
<= Either the value of left operand is less than or equal to the value of right operand, then the condition becomes true and return 1 otherwise return 0(false). A<=B

Example of Comparison Operator

We have discussed about the different Comparison operators. Now let’s see a simple example of all Comparison Operators.


Logical Operators

The Python logical operators are used to combine conditional statements. Here is the table showing all logical operators supported by Python:

Operator Description Example
and Logical AND operator.if the both condition are true then it returns true otherwise returns false. xy
or Logical OR Operator. If any of condition is true, then it will return true otherwise returns false. xy
not Logical NOT Operator. it reverses the result such as if the condition is true then it will return false and vice-versa. not(x

Example of Logical Operator

Let’s see a simple example using logical operators.


Identity Operator

Identity operator in python is used to check whether the identity of a variable or object is the same as other variables or objects. Variables can be identical in different ways such as value, datatype, etc.

For example, if a variable has integer type value, and we want to check if the other variable is the same as the first variable. Then we use the identity operator.

Operator Description Example
is If the identity of a variable(datatype) is the same then it will return true, otherwise returns false. x is y
is not If the identity of a variable(datatype) is the same then it will return true, otherwise returns false. x is not y

Example of Identitical Operators

Let’s see a simple example of all the identical operators in Python programming.


Membership Operator

Membership Operator in python is used to check whether a sequence is a member of other variables or lists. In other words, these operators check if a variable is present in the other variable or not.
There is two inbuilt Membership operator in Python Programming:

Operator Description Example
in If the value of a sequence is found in the other sequence then it will return true, otherwise returns false. x in y
not in If the value of a sequence is found in the other sequence then it will return false, otherwise returns true. x is not y

Example of Membership Operators

Let’s see a simple example of all the membership operators in Python programming.


Bitwise Operators

With the help of Python Bitwise Operators, we can easily perform bitwise operations such as converting bit value from 0 to 1, shifting left to right, etc. Here is the table of all the bitwise operators supported in Python programming:

Operator Description Example
& Binary AND Operator copies a bit to the result if it exists in both operands (a & b) (means 0000 1100)
| Binary OR It copies a bit if it exists in either operand. (a | b) = 61 (means 0011 1101)
^ Binary XOR It copies the bit if it is set in one operand but not both. (a ^ b) = 49 (means 0011 0001)
~ Binary Ones Complement It is unary and has the effect of ‘flipping’ bits. (~a ) = -61 (means 1100 0011 in 2’s complement form due to a signed binary number.
<< Binary Left Shift The left operands value is moved left by the number of bits specified by the right operand. a << 2 = 240 (means 1111 0000)
>> Binary Right Shift The left operands value is moved right by the number of bits specified by the right operand. a >> 2 = 15 (means 0000 1111)

Example of Bitwise Operator in Python Programming


Congratulations! Chapter Finished. Do you want to practice more?
Exercises & Assignments
  • No Content Found.
  • Interview FAQs
  • No Content Found.