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

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

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

#### Example of Comparison Operator

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

``````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:

#### Example of Logical Operator

Let’s see a simple example using logical operators.

``````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``````
``````False
False
False
True
True
True
False
True
True
False``````

### Identity Operator

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

#### Example of Identitical Operators

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

``````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)``````
``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:

#### Example of Membership Operators

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

``````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")``````
``````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:

#### Example of Bitwise Operator in Python Programming

``````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)``````
``````Result  is  0
Result is  14
Result is  14
Result is  -13
Result is  48
Result is  3``````