This tutorial will learn about all the different types of python operators and their syntax and use them in real-time with examples.

First, let’s understand what exactly Operators are.

## What are Python Operators?

Python Operators are special symbols used to perform mathematical and logical operations and computations on values or variables.

Values used by python operators are called the operand

## Types of Python Operators

Python programming language supports seven types of operators. Below is the list of python operators.

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

Let us study all the operators one by one.

You can also see the Python Type Casting and Type Conversion.

### Python Arithmetic Operators

Arithmetic operators are used to doing mathematical calculations on values and variables.

Operator | Description | Example |

+ Addition | An addition Operator is used to make the addition of two or more variables or values. | X + Y = 21 + 1 = 2 |

– Subtraction | Subtraction operator is used to subtracting two or more variables or values. | Y – X = 13 – 1 = 1 |

* Multiplication | Multiplication operator is used to multiplying two or more variables or values. | X * Y = 42 * 2 = 4 |

/ Division | Division operator is used to divide two or more variables or values. | X / Y = 24 / 2 = 2 |

% Modulus | The modulus operator is used to find the remainder of two or more variables or values. | X % Y = 110 % 3 = 1 |

** Exponent | The exponent operator is used to find the power of the two or more variables or values. | X ** Y = 642 ** 6 = 64 |

// Floor Division | The Floor Division is used to divide the first operand by the second. | X // Y = 310 // 3 = 3 |

X = 10 Y = 2 Z = 6 # Operations with two values # Addition print(X + Y) #Subtraction print(X – Y) #Multiplication print(X * Y) #Division print(X / Y) #Modulus print(X % Y) #Exponent print(X ** Y) #Floor Division print(X // Y) #Operations with three values # Addition print(X + Y + Z) #Subtraction print(X – Y – Z) #Multiplication print(X * Y * Z) #Division print(X / Y / Z) #Modulus print(X % Y % Z) #Exponent print(X ** Y ** Z) #Floor Division print(X // Y // Z)

**Output:**

12 8 20 5.0 0 100 5 18 2 120 0.8333333333333334 0 10000000000000000000000000000000000000000000000000000000000000000 0

As we can see in the above example how we can use Arithmetic Operators on multiple variables and values

### Python Comparison or Relational Operators

Python Comparison or Relational operators are used to compare the value or variable. It will return True Or False as output based on the condition.

Operator | Description | Example |

> | Greater Then: operator will return True when the left operand is greater than the right. | X > Y2 > 3 |

< | Less Then: operator return True when left operand is less than the right. | X < Y3 < 2 |

== | Equal To: will return True when both operands are equal. | X == Y3 == 3 |

!= | Not Equal To: operator will return True when two operands are not equal. | X != Y3 != 2 |

>= | Greater than or Equal To operator will return True when left operand if greater than right or equal to the right | X >= Y2 >= 3 |

<= | Less Then Equal To Operator will return True when the left operand is less than or equal to the right operand. | X <= Y2 <= 3 |

X = 10 Y = 2 # Greater Than print(X > Y ) #Less Than print(X < Y ) #Equal To print(X == Y) #Not Equal To print(X != Y) #Greater than or Equal To print(X >= Y) #Less Than Equal To: print(X <= Y)

**Output:**

True False False True True False

### Assignment Operators

Python Assignment Operators are used to assign values to variables:

Operator | Example | Same AS |

= | X = 5 | X = 5 |

+= | X += 5 | X = X + 5 |

-= | X -= 5 | X = X – 5 |

*= | X *= 5 | X = X * 5 |

/= | X /= 5 | X = X / 5 |

%= | X %= 5 | X = X % 5 |

//= | X //= 5 | X = X // 5 |

**= | X **= 5 | X = ** 5 |

&= | X &= 5 | X = & 5 |

|= | X |= 5 | X = X | 5 |

^= | X ^= 5 | X = X ^ 5 |

>>= | X >>= 5 | X = X >> 3 |

<<= | X <<= 5 | X = X << 5 |

### Python Logical Operators

Python logical operators are used for the conditional statement.

Operator | Description | Example |

AND | Return True when both statements are true | X < 5 AND X < 10 |

OR | Return True when one of the statement is true | X < 5 or X < 5 |

NOT | Return False if the result is not true | not(X < 5 or X < 10) |

**Example:**

X = True Y = False print(X and Y) print(X or Y) print(not Y)

**Output:**

False True True

### Python Bitwise Operators

Same As Logical operators are used to comparing data, and it uses special symbols, but bitwise operators act on operands as binary digits. They operate bit by bit.

For Example, 2 is 10 in binary form, and 111 is for 7.

Operator | Description | Example |

& | Bitwise AND Operator: Return True when both statements are true | X < 5 & X < 10 |

| | Bitwise OR Operator: Return True when one of the statement is true | X < 5 | X < 5 |

~ | Bitwise NOT Operator: Return False if the result is not true | ~X |

^ | Bitwise XOR operators: work with binary values and return one if one of the bits is 0 and the other bit is one, and if both the bits are 0 or 1, then it returns 0. | X ^ Y |

<< | Bitwise Left Shift: Shift left by pushing zeros in from the right and let the leftmost bits fall off. | X << Y |

>> | Bitwise Right Shift: Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off | X >> Y |

Bitwise operators will return Boolean if we use Bitwise operator with other operators, and it will return numeric values if we only use a single bitwise operator. Let take an example of each.

**Example**:

X = 2 Y = 7 # And Operator print(X < 5 & X < 10) # OR Operator print(X < 5 | X < 5) # Not Operator print(X ~Y) # XOR Operator print(X < 5 ^ X < 10)

**Output:**

False False -3 True

Let’s take X as 3, and binary will be 0011, and Y as 8 as 1000.

**Example **:

X = 3 Y= 8 # & Operator print(X & Y) # | Operator print(X | Y) # ~ Operator print(~X) # ^ Operator print(X ^ Y) # >> Operator print(X >> Y) # << Operator print(X << Y)

**Output:**

0 11 -4 11 0 768

### Python Membership Operators

membership operators in python consist “in” and “not in” are used to test whether a value is a member of a sequence, such as a list, tuple, or string.

The “in” operator returns True if the specified value is found in the sequence, and False otherwise. The “not in” operator does the opposite, returning True if the value is not found in the sequence, and False otherwise.

For example, if a value is present in a list, set, and other objects.

Operator | Description | Example |

in | Returns True if a specified value is present in the sequential object | X in Y |

not in | Return True if a specified value is not present in the sequential object. | X not in Y |

let see membership operators in python with example.

X = 6 #Variable with integer value Y = 11 #Variable with integer value Z = [1,2,3,4,5,6,7,9] # Variable with List # in Operator print(X in Z) # not Operator print(Y not in Z)

**Output:**

True True

### Python Identity Operators

identity operator in python are used to compare the objects, not if they are equal, but if they are the same object, with the same memory location.

Operator | Description | Example |

is | Returns True if both variables are the same object | X is Y |

is not | Returns True if both variables are not the same object | X is Not Y |

**Example:** Let’s learn identity operators in python with example.

X = 3 Y = "Python" Z = 21 print(X is Z) print(Z is not X) print(Y is X)

**Output:**

False True False

## FAQs

### What is the ternary operator in Python?

The ternary operator in Python is a shorthand version of an if-else statement. It is represented by the syntax “x if condition else y”, where x is the value returned if the condition is True, and y is the value returned if the condition is False.

### How to overload operators in Python?

Operator overloading in Python allows a programmer to redefine the behavior of built-in operators for custom classes. This can be done by defining special methods such as **add**(), **sub**(), **eq**(), and others.

### What is the use of the ‘not’ operator in Python?

The ‘not’ operator in Python is used to invert the boolean value of a statement or expression, such that True becomes False and vice versa.

### What is the difference between == and is operator in Python?

The == operator checks if two values are equal, while the is operator checks if two variables refer to the same object in memory.

### How are operators used in Python programming?

Operators are used extensively in Python programming to perform a wide variety of operations, from simple arithmetic calculations to complex logical statements. By understanding how operators work and how to use them effectively, Python developers can write more efficient and effective code.