# 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. x^{y} |
x**y |

// | Floor Division of x//y rounds the result down the division to the nearest whole number | x//y |

1 2 3 4 5 6 7 8 9 |
x = 5 y = 3 print(x+y) # will give 8 print(x-y) # will give 2 print(x*y) # will give 15 print(x/y) # will give 1.6666666666666667 print(x % y) # will give 2 print(x**y) # will give 125 print(x//y) # will give 1 |

### 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.

1 2 3 4 5 6 7 8 |
x = 10 y = 20 print(x == y) # return False print(x != y) # return True print(x < y) # return True print(x > y) # return False print(x <= y) # return True print(x >= y) # return False |

### 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. | x |

or | Logical OR Operator. If any of condition is true, then it will return true otherwise returns false. | x |

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.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
x = 1 y = 2 result1 = (x < y) and (x > y) result2 = (x > y) and (x < y) result3 = (x > y) and (x == y) result4 = (x < y) and (x <= y) result5 = (x < y) or (x > y) result6 = (x > y) or (x < y) result7 = (x > y) or (x == y) result8 = (x < y) or (x <= y) result9 = not((x < y) and (x > y)) result10 = not((x < y) or (x > y)) print(result1) # return False print(result2) # return False print(result3) # return False print(result4) # return True print(result5) # return True print(result6) # return True print(result7) # return False print(result8) # return True print(result9) # return True print(result10) # return False |

1 2 3 4 5 6 7 8 9 10 |
False False False True True True False True True False |

### 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.

1 2 3 4 5 6 7 |
x = 2 y = 3 result1 = x is y result2 = x is not y result3 = type(x) is type(y) result4 = type(x) is not type(y) print(result1, result2, result3, result4) |

1 |
False True True False |

### 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.

1 2 3 4 |
print("Welcome" in "Welcome to Tutorials Class") print("welcome" in "Welcome to Tutorials Class") print("Welcome" not in "Welcome to Tutorials Class") print("welcome" not in "Welcome to Tutorials Class") |

1 2 3 4 |
True False False True |

### 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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
num1 = 12 num2 = 2 result = num1 & num2 print ("Result is ", result) result = num1 | num2 print ("Result is ", result) result = num1 ^ num2 print ("Result is ", result) result = ~num1 print ("Result is ", result) result = num1 << 2 print ("Result is ", result) result = num1 >> 2 print ("Result is ", result) |

1 2 3 4 5 6 |
Result is 0 Result is 14 Result is 14 Result is -13 Result is 48 Result is 3 |

**Congratulations! Chapter Finished. Do you want to practice more?**