**Operators in Python**

In Python, we have a set of special symbols that perform various kinds of operations such as logical operations, mathematical operations, and more. These symbols are called Python operators. For every symbol or operator, there is a unique kind of operation. The values on which the operators perform their respective operations are known as operands. In this module, we will learn all about operators that we need to know in order to get started with them.

**Watch this video on ‘Python Operators’:**

**Following is the list of topics that we will cover in this module.**

- Types of Operators in Python
- Arithmetic Operators
- Relational Operators in Python
- Assignment Operators in Python
- Logical Operators in Python
- Membership Operators in Python
- Bitwise Operator in Python
- Identity Operators in Python
- Ternary Operators in Python
- Python Operator Precedence
- Operator Overloading in Python

So, without any further delay, let’s get started.

**Types of Operators in Python**

Depending on the type of operations that the operators perform, they are categorized into the following categories:

- Arithmetic Operators in Python
- Relational Operators in Python
- Assignment Operators in Python
- Logical Operators in Python
- Membership Operators in Python
- Identity Operators in Python
- Bitwise Operators in Python

Now that we know what the different kinds of operators are in Python, let’s discuss each one of them individually, starting with arithmetic operators.

*Become a Professional Python Programmer with this complete Python Training in Singapore!*

**Arithmetic Operators in Python **

Arithmetic operators are used to performing various mathematical operations such as addition, subtraction, etc. The following table contains all the arithmetic operations and their descriptions, along with examples.

Arithmetic Operator |
Operator Name |
Description |
Example |

+ |
Addition | Performs addition | I=40, J=20 >>>I+ J >>>60 |

– |
Subtraction | Performs subtraction | I=40, J=20 >>>I – J >>>20 |

* |
Multiplication | Performs multiplication | I=40, J=20 >>>I * J >>> 800 |

/ |
Division | Performs division | I=30, J=20 >>>I /J >>> 2.5 |

% |
Modulus | Returns the remainder after the division | I=40, J=20 >>>I /J >>> 0 |

** |
Exponent | Performs exponential (power) calculation | I=4, J=20 >>>I /J >>> 204 |

// |
Floor Division | Performs division, removes the decimal value, and returns the quotient value | I=30, J=20 >>>I//J >>> 1 |

**Relational Operators in Python**

They are also known as comparison operators because they compare the values on both sides of the operator and conclude on the relation between the values. After comparison, it returns the Boolean value, i.e., either true or false. The following table contains different types of comparison operators and their descriptions, along with respective examples.

Operator |
Operator Name |
Description |
Example |

== |
Equal to | If values of two operands are equal, then it returns true. | I = 20, J = 20 (I == J) is True |

!= |
Not Equal to | If values of two operands are not equal, then it returns true. | I = 20, J = 20 (I == J) is False |

< |
Less than | If the value of the left operand is less than the value of the right operand, then it returns true. | I = 40, J = 20 (I < J) is False |

> |
Greater than | If the value of the left operand is greater than the value of the right operand, then it returns true. | I= 40, J = 20 (I > J) is True |

<= |
Less than or equal to | If the value of the left operand is less than or equal to the value of the right operand, then it returns true. | I = 40, J = 20 (I <= J) is False |

>= |
Greater than or equal to | If the value of the left operand is greater than or equal to the value of the right operand, then it returns true. | I = 40, J = 20 (I >= J) is True |

<> |
Not equal to (similar to !=) | If values of two operands are not equal, then the condition becomes true. | I=40, J = 20 (I <> J) is True. |

*Go for this in-depth job-oriented Python Training in Hyderabad now!*

**Assignment Operators in Python**

Assignment operators are used to assign values to Python Variables. The assignment is sometimes done directly, and sometimes the operator first performs some kind of mathematical operation and then assigns the value to the operand. The following table contains all types of assignment operators with their descriptions and respective examples.

Operator |
Operator Name |
Description |
Example |

= |
Assignment | It assigns a value from the right-side operand to the left-side operand. | I = 40 It assigns 40 to I |

+= |
Add then assign | It performs addition, and then the result is assigned to the left-hand operand. | I+=J that means I = I + J |

-= |
Subtract then assign | It performs subtraction, and then the result is assigned to the left-hand operand. | I-=J that means I = I – J |

*= |
Multiply the assign | It performs multiplication, and then the result is assigned to the left-hand operand. | I*=J that means I = I * J |

/= |
Divide then assign | It performs division, and then the result is assigned to the left-hand operand. | I/=J that means I = I / J |

%= |
Modulus then assign | It performs modulus, and then the result is assigned to the left-hand operand. | I%=J that means I = I % J |

**= |
Exponent then assign | It performs exponent, and then the result is assigned to the left-hand operand. | I**=J that means I = I ** J |

//= |
Floor division then assign | It performs floor division, and then the result is assigned to the left-hand operand. | I//=J that means I = I // J |

**Logical Operators in Python**

Logical operators are mainly used for conditional statements. There are three types of logical operators, namely, AND, OR, and NOT. The following table contains all logical operators with their descriptions, as well as their respective examples.

Operator |
Operator Name |
Description |
Example |

and |
Logical AND | When both sides’ conditions are true, the result is true; otherwise false. | 2<1 and 2<3 False |

or |
Logical OR | When at least one condition is true, then result is true; otherwise false. | 2<1 or 2<3 True |

not |
Logical NOT | Reverse the condition | Not (5>4) False |

*We have the perfect professional Python Course in Bangalore for you!*

**Membership Operators in Python**

Membership operators are used to test if a value is available in a sequence or not. It can be any sequence such as (Python String, Python List, Python Set, Python Tuple, and Python Dictionary). There are two types of membership operators, namely, in and not in. The following table contains the description and the respective examples of both membership operators.

Operator |
Description |
Example |

in |
It returns true if it finds a variable in the sequence; otherwise false. | List = [1,2,3,4,5,6,7,8] i=1 if i in List: print(‘i is available in list’) else: print(‘i is not available in list’) Output – i is available in list |

not in |
It returns true if it does not find a variable in the sequence; otherwise false. | List = [1,2,3,4,5,6,7,8] j=10 if j not in List: print (‘j is not available in list’) else: print (‘j is available in list’) Output – j is not available in list |

**Bitwise Operator in Python**

It performs bit-by-bit operations.

For instance, suppose there are two variables,

I= 10 andJ= 20

And their binary values are:

I= 10 = 0000 1010J= 20 = 0001 0100

Now, let us see how the bitwise operator in Python works.

Operator |
Operator Name |
Description |
Example |

& |
Binary AND | If both bits are 1, then 1; otherwise 0. | I & J 0000 0000 |

| |
Binary OR | If one of the bits is 1, then 1; otherwise 0. | I | J 0001 1110 |

^ |
Binary XOR | If both bits are the same, then 0; otherwise 1 | I ^ J 0001 1110 |

~ |
Binary Complement | If the bit is 1 then makes it 0, and if the bit is 0 the makes it 1 | ~I 1111 0101 |

<< |
Binary Left Shift | The left operand is moved left by the number of bits specified by the right operand. | I << 2 240 i.e. 1111 0000 |

>> |
Binary Right Shift | The left operand is moved right by the number of bits specified by the right operand. | I >> 2 15 i.e. 1111 |

**Identity Operators in Python**

Identity operators are used to comparing the memory addresses of two different objects. The two types of identity operators in Python are is and is not. The following table contains the description of these two operators, along with their respective examples.

Operator |
Description |
Example |

is |
It returns true if both operands’ identities are the same; otherwise false. | I = 20 J = 20 if(I is J): print (‘I and J have same identity’) else: print (‘I and J have not same identity’) Output – I and J have same identity |

is not |
It returns true if both operands’ identities are not the same; otherwise false. | I = 20 J = 230 if(I is not J): print (‘I and J have not same identity’) else: print (‘I and J have same identity’) Output – I and J have not same identity |

**Ternary Operators in Python**

Ternary operators, which can also be called conditional expressions evaluate something based on a particular condition being true or false. It simply allows the programmer to test a condition in a single line replacing the multi-line if-else statements, and hence makes the code compact. They are more concise than a traditional if…else statement.

a, b = 10, 20 min = a if a < b else b print(min) The output will be 10

**Python Operator Precedence**

In some expressions, there are more than one operator. To evaluate such expressions, there is a rule of precedence, known as Python Operator Precedence. It guides the order in which these operations are carried out.

The table given below shows the operator precedence in Python in descending order.

Operators |
Meaning |

() | Parentheses |

** | Exponent |

+x, -x, ~x | Unary Plus, Unary Minus, Bitwise NOT |

*, /, //, % | Multiplication, Division, Floor Division, Modulus |

+, – | Addition, Subtraction |

<<, >> | Bitwise Shift Operators |

& | Bitwise AND |

^ | Bitwise XOR |

| | Bitwise OR |

==, !=, >, >=, <, <=, is, is not, in, not in | Comparisons, Identity, Membership Operators |

not | Logical NOT |

and | Logical AND |

or | Logical OR |

**Operator Overloading in Python**

Operator Overloading lets you extend the meaning of operators that are predefined. It provides an expanded definition of the operator. The same operator also behaves in different ways with different data types. Take the example of the + operator. It performs addition when done on numbers, but will concatenate two strings and merge two lists

class Point: def __init__(self, x=0, y=0): self.x = x self.y = y def __str__(self): return "({0},{1})".format(self.x, self.y) def __add__(self, other): x = self.x + other.x y = self.y + other.y return Point(x, y) p1 = Point(1, 2) p2 = Point(2, 3) print(p1+p2) The output will be (3,5)

This brings us to the end of this module in python Tutorial. Here we talked about what Python operators are, types of Python Operators, arithmetic operators in Python, relational operators in Python, assignment operators in Python, logical operators in Python, membership operators in Python, bitwise operators in Python, identity operators in Python. Now, if you are interested in knowing how to implement data science concepts with Python, you can go through this blog on Python Data Science tutorial.

Further, check out our offers for the Python certification course. You can also go through these free python Interview Questions prepared by industry experts.