Hey Folks, in this module we will learn **Python Operators** and their types with their order of precedence. This is the most important basic concept which is necessary to be known. So let’s start.

## Python operators

**Python operators are used to performing various operations on different sets of variables and values**. These operators once operate any two values at a time.

The use of multiple operators in a single set of equations leads to operator precedence.

Each operator has its own associativity as well as precedence. This order of precedence gives priority to each operator according to its level of precedence.

## Precedence and Associativity

Some important python supported operator precedence and associativity.

ORDER OF PRECEDENCE |
OPERATORS | ASSOCIATIVITY | DESCRIPTION |
---|---|---|---|

1 | () | Left to right | Parentheses |

2 | ** | Right to left | Exponentiation |

3 | ~, +,- | Right to left | Complement, unary plus (positive), and minus (negative) |

4 | *, /, %, // | Left to right | Multiply, divide, modulo, and floor division |

5 | +, – | Left to right | Addition and subtraction |

6 | >>, << | Left to Right | Right and Left bitwise shift |

7 | & | Left to Right | Bitwise ‘AND’ |

8 | ^, | | Left to Right | Bitwise exclusive ‘OR’ and regular ‘OR’ |

9 | < =, <, >, >= | Left to Right | Comparison operators |

10 | <>, ==, != | Left to Right | Equality operators |

11 | =, %=, /=,*=, //=, +=,-=, **= | Right to Left | Assignment operators |

12 | Is, is not | Left to Right | Identity operators |

13 | in, not in | Left to Right | Membership operators |

14 | or not and |
Left to Right Right to Left, Left to Right |
Logical operators |

## Type of Operators

There are various operators used to perform various tasks in less time complexity, These are as follows:

- Arithmetic Operators
- Comparison (Relational) Operators
- Assignment Operators
- Logical Operators
- Identity Operators
- Bitwise Operators
- Membership Operators

### Arithmetic Operators

These operators are basically used to perform different **mathematical operations**.

For Example, we use the ‘+'(addition) operator for adding two or more values.

a = a ‘+’ c

To know arithmetic operators in depth. Let take two integer variables a and b of values

a= 10 and b= 20 of integer data type.

Operator | Description | Example |
---|---|---|

Addition | An Addition operation(+) is used to add two or more values of the same or different data type. | print(a+b) Output: 30 |

Multiplication | This operator, also known as multiplication operator(*), is which is also known as multiplication operator(*) used to multiply two or more values of the same or different data types. | print(a*b) Output: 200 |

Division | This operator, known as Division operator (/) used to divide the value on the left side which is dividend with the value on the right side i.e. divisor. It gives Quotient as a result. | print(b / a) Output: 2 |

Modulus | This operator is used to find the modulus of a given value. Modulus is the remainder that is formed during division. (%) operator helps to find out modulus during the division of the left side value with the value on the right side. | print(b % a) Output: 0 |

Floor Division | This operator is used for giving non-decimal values through division. It is represented as (//). | print(a//b) Output: 0 |

Exponent | This operator is used to perform an Exponential operation. It is represented by (a ** b) where “a” is the base number and “b” is the exponential number. | print(2**3) Output: 8 |

### Comparison (Relational) Operators

This operator is used to perform an operation in which **some conditions are required which need to be satisfied**, then further operation will be executed. It will always be either True(1) or False(0).

There are various conditional operators which are as follows:

Let’s take values X=2 and Y=4 of integer data type.

Operators | Syntax | Description | Result |
---|---|---|---|

> | X > Y | Y is smaller than X | False |

< | X < Y | Y is larger than X | True |

! = | X != Y | X is not equal to Y | True |

== | X == Y | X is equal to Y | False |

> = | X >= Y | Y is smaller or equal to X | False |

< = | X < = Y | Y is larger or equal to X | True |

### Assignment Operators

This is a type of operator used to **assign the value to variables**. This is basically used to override the value of the operand. These are special Signs used for carrying logical, bitwise

Computation. These operators are as follows:

Let take A=5 and B=4 of integer data type.

Operators | Syntax | Description | Result |
---|---|---|---|

+= | a+=b (a=a+b) | This operator is used to perform addition operation then assign value to left side operand | a=9 |

= | c=a+b d=a |
The operator used for assigning right expression to left side operand | c=9 d=5 |

-= | a-=b (a=a-b) | The operator used to perform subtraction operation and then assign value to left operand | a=1 |

*= | a*=c (a=a*c) | An operator used to perform a multiplication operation and then assign value to the left operand | a=20 |

/= | a/=b (a=a/b) | An operator used to perform division operation and then assign value to the left operand | a=1.25 |

//= | a//=b (a=a//b) | The operator used to perform floor division operation and then assign value to left operand | a=1 |

%= | a %=c (a=a % b) | An operator used to perform modulus operation and then assign value to the left operand | a=1 |

**= | a**=b (a= a**b) | The operator used to perform the exponential operation and then assign value to the left operand | a=625 |

### Logical Operators

The operator which used to perform **logical operations** like AND, OR, NOT. Mostly used in different arithmetic operations and logical computations. These are as follows:

Let x=2 and y=3 of integer data type.

Operators | Syntax | Description | Result |
---|---|---|---|

AND | X and Y | The statement will be true when Both X and Y are true | x false |

NOT | NOT Y | Reciprocal of Y, i.e. if Y is true, then the statement is False or vice-versa. | If X=true Then, Not X: True |

OR | X OR Y | The statement will be true when either X or Y is true | x True |

### Identity Operators

This operator is used to identify and **compare the values of two objects**. There are two identity operators, which are:

Operator | Description | Syntax | Example | Output |
---|---|---|---|---|

Is not: | It returns false when both objects are unequal. | A is not B | A=[‘a’,’b’,’d’] B=[‘a’,’b’,’d] C=B print(C is not B) print(A is not B) |
False True(as both objects are different even if they have the same element) |

Is: | It returns True when both objects have the same values. | A is B | A=[‘a’,’b’,’d] B=[‘a’,’b’,’d] C=B print(C is B) print(A is B) |
True(Object B is assigned to object C, therefore both objects are equal) False |

### Bitwise Operators

This operator is used for performing various **logical binary computations**.

These operators are as follows:

Name | Operator | Description |
---|---|---|

OR | | | It set the value 1 when both bits are valued 1 or 0 |

AND | & | It sets a value of 1 when either one of the bit values is 1. |

XOR | ^ | It sets a value of 1 when one of the bit values is 1. |

NOT | ~ | If the value is set as 1, then the output will be 0 or vice -versa. |

Right Shift Bitwise Operator | >> | It will shift binary value from right to left |

Left Shift Bitwise Operator | << | It will shift the binary value from left to right |

### Membership Operators

This operator is used to verify that** a sequence of elements is present in the object**, List, Tuple, or string.

There are two membership operators.

Name | Description | Example | Example |
---|---|---|---|

in | This operator will return true when the element is present in the object. | A=[‘a’,’b’,’d’] print(‘a’ in A) |
True(element ‘a’ is present in object A) |

not in | This operator will return true when the element is not present in the object. | A=[‘a’,’b’,’d’] print(‘c’ not in A) |
True(element ‘c’ is not present in object A) |

In this module, we studied the use of Python operators with their precedence order. Hope you enjoy reading our content. To get a clear vision of the importance of Python please check out How to Become a Python Developer. Happy Learning 🙂