# Python Operators

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.

X = 10
Y = 2
Z = 6

# Operations with two values
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
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.

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:

### Python Logical Operators

Python logical operators are used for the conditional statement.

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.

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.

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.

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.