In computer programming, an operator is basically a symbol that represents a process or an action. These symbols are accommodated from logic and mathematics. An operator has the capability of manipulating certain values or operands. They are very useful and are the backbone of any program. Operators in python are in general used to perform mathematical or logical operations on values and variables. In this article, different types of python operator are discussed in detail along with examples.

**What are Operators in Python?**

Operators are standard symbols in python defined to perform arithmetic and mathematical operations. Below are the list of operators supported in python:

- Arithmetic operators
- Comparison operators
- Logical operators
- Bitwise operators
- Assignment operators
- Special operators

Each operator is explained below in detail with examples.

** ****Arithmetic Operators**

Arithmetic operators are used to perform various mathematical operations like subtraction(-), addition(+), multiplication(*) and division(/).

There are seven arithmetic operators in python, here is a detailed explanation of each arithmetic operator:

: In python, the symbol ‘+’ is used as an additional operator. Two values can be added by using the addition operator.__Addition operator__

**Example:**

value1 = 4 value2 = 3 res = value1 + value2 print (res) Output 7

In python, the symbol ‘-’ is used as a subtraction operator. The second value is subtracted from the first value using the subtraction operator.__Subtraction Operator:__

**Example:**

value1 = 4

value2 = 3

res = value1 – value2

print (res)

Output1

In python, the symbol ‘*’ is used as a multiplication operator. The product of two values can be calculated using the multiplication operator.__Multiplication Operator:__

**Example:**

value1 = 4

value2 = 3

res = value1*value2

print (res)

Output12

In python, symbol ‘/’ is used as a division operator. It calculates the quotient when the first operand is divided by the second operand. It returns the answer as floating point value.__Division Operator:__

**Example:**

value1 = 3

value2 = 2

res = value1/value2

print (res)

Output1.5

In python, symbol ‘%’ is used as a modulus operator. It calculates the remainder left when the first value is divided by the second value.__Modulus Operator:__

**Example:**

value1 = 4

value2 = 3

res = value1%value2

print (res)

Output1

In python, symbol ‘**’ is used as an exponential operator. It is used to calculate the result when the first operand is raised to the power of the second operand.__Exponential Operator:__

**Example:**

value1 = 4

value2 = 3

res = value1**value2

print (res)

Output64

In python, symbol ‘//’ is used as a floor division operator. It calculates the floor of the quotient when the first value is divided by the second value.__Floor Division Operator:__

**Example:**

value1 = 3

value2 = 2

res = value1//value2

print (res)

Output1

**Comparison operators**

Comparison operators are used to comparing two values and find the relation among the values on either side. According to the condition, it returns **True** or **False. **As they are used to find relations between the values, they are also known as relational operators.

There are six comparison operators in python, here is a detailed explanation of each comparison operator:

In python, symbol ‘>’ is used as greater than the operator. It returns True if the value on its left side is greater than the value on its right side, False otherwise.__Greater than operator:__

**Syntax: **a > b

**Example:**

value1 = 3

value2 = 2

print (value1 > value2)

OutputTrue

In python, symbol ‘<’ is used as less than operator. It returns True if the value on its right side is greater than the value on its left side, False otherwise.__Less than operator:__

** ****Syntax: **a < b

** ****Example:**

value1 = 3

value2 = 2

print (value1 < value2)

OutputFalse

In python, symbol ‘>=’ is used as greater than equal to operator. It returns True if the value on its left side is greater than or equal to the value on its right side, False otherwise.__Greater than equal to:__

**Syntax: **a >= b

**Example:**

value1 = 3

value2 = 2

print (value1 >= value2)

OutputTrue

In python, symbol ‘<=’ is used as less than equal to operator. It returns True if the value on its left side is less than or equal to the value on its right side, False otherwise.__Less than equal to:__

**Syntax: **a <= b

** ****Example:**

value1 = 3

value2 = 2

print (value1 <= value2)

OutputFalse

In python, symbol ‘==’ is used as equal to operator. It returns True if the values on its left and right side are equal, False otherwise.__Equal to:__

** ****Syntax: **a == b

** ****Example:**

value1 = 3

value2 = 2

print (value1 == value2)

OutputFalse

In python, symbol ‘!=’ is used as not equal to operator. It returns True if values on both sides are not equal.__Not equal to:__

** **

**Syntax: **a != b

** ****Example:**

value1 = 3

value2 = 2

print (value1 != value2)

OutputTrue

**Logical operators**

Logical operators are used on conditional statements that are either true or false.

There are three logical operators in Python, here is a detailed explanation of each logical operator:

In python, a keyword__Logical AND operator:__**and**is used to perform logical AND operation. It returns True if both the values are true, false otherwise.

If the first expression evaluated is False while performing **and **operator, then the further expressions are not evaluated.

**Example:**

value1 = 3

value2 = 2

if (value1>0 and value2>0):

print (“Both the values are positive”)

else:

print (“Atleast one number is negative”)

OutputBoth the values are positive

In python, keyword__Logical OR operator:__**or**is used to perform logical OR operation. It returns True if any of the two values is true, false otherwise.

If the first expression evaluated is True while performing **or **operator, then the further expressions are not evaluated.

**Example:**

value1 = 3

value2 = -2

if (value1>0 or value2>0):

print (“Either of the value is positive”)

else:

print (“None of the value is greater than zero”)

OutputEither of the value is positive

In python, keyword__Logical NOT operator:__**not**is used to perform logical NOT operation. It works with a single boolean value. It returns True if the value is False and vice versa.

**Example:**

value1 = 3

if not (value1 > 0) :

print (“value1 is less than equal to zero”)

else:

print (“value1 is positive”)

Outputvalue1 is positive

Note that keywords **and, or **and** not **are case sensitive.

**Bitwise operators**

Bitwise operators are used to performing calculations on integers. These operations are performed bit by bit and therefore they are called bitwise operators. The final result is returned in decimal format.

There are six bitwise operators in python, here is a detailed explanation of each bitwise operator:

In python, the symbol ‘&’ is used as Bitwise AND operator. It returns 1 if both the bits on left and right are 1 else returns 0.__Bitwise AND operator:__

** ****Syntax: **a & b

** ****Example:**

value1 = 10

value2 = 4

print (value1 & value2)

Output0

**Explanation:**

1010 is the binary representation of 10

0100 is the binary representation of 4

So 1010 & 0100 == 0000

0 is the decimal representation of 10&4(=0)

__ __

In python, the symbol ‘|’ is used as bitwise or operator. It returns 1 if any of the bits on left or right is 1 else returns 0.__Bitwise OR operator:__

**Syntax: **a | b

** **

**Example:**

value1 = 10

value2 = 4

print (value1 | value2)

Output14

**Explanation:**

1010 is the binary representation of 10

0100 is the binary representation of 4

So 1010 | 0100 == 1110

14 is the decimal representation of 10|4(=14)

In python, the symbol ‘~’ is used as bitwise not operator. It returns one’s complement of the given value__Bitwise NOT operator:__

**Syntax: **a ~ b

** ****Example:**

value1 = 10

print (~value1)

Output11

**Explanation:**

~1010 = ~ (1010 + 1)

= ~ (1011)

Decimal representation of 1011 is 11

In python, symbol ‘^’ is used as xor operator. It returns 1 if both bits are opposite, that is one is 1 other is 0 or vice versa.__Bitwise XOR operator:__

**Syntax: **a ^ b

**Example:**

value1 = 10

value2 = 4

print (value1 ^ value2)

Output14

**Explanation:**

1010 is binary representation of 10

0100 is binary representation of 4

So 1010 ^ 0100 == 1110

14 is the decimal representation of 10^4(=14)

In python, symbol ‘>>’ is used as bitwise right shift operator. It shifts the binary representation of the left operand shifted right by the number of positions equal to the right operand and inserts zero bit on the left.__Bitwise right shift operator:__

**Syntax: **a >> b

**Example:**

value1 = 10

value2 = 1

print (value1 >> value2)

Output5

**Explanation:**

1010 is binary representation of 10

So 10 >> 1 = 0000 0101 = 5

In python, symbol ‘<<’ is used as bitwise left shift operator. It returns the binary representation of the left operand shifted left by the number of positions equal to the right operand and inserts zero bit on the right.__Bitwise left shift operator:__

** ****Syntax: **a << b

** ****Example:**

value1 = 5

value2 = 2

print (value1 << value2)

Output20

**Explanation:**

0000 0101 is binary representation of 5

5 << 2 = 0001 0100 = 20

** ****Assignment operators**

Assignment operators are used to assigning values to the variables. There are thirteen assignment operators, let’s look into each one of these in detail.

It assigns the right-side value of expressions to the left-side value.__Assign operator:__

**Syntax: **a = b + c

**Example: **a = 2

It adds the value of right operand and left operand and assigns results to the left operand.__Add and assign:__

**Syntax: **a += b

**Example: **a+=2 i.e. a=a+2

It subtracts the value of the right operand from left operand and assigns result to left operand.__Subtract and assign:__

**Syntax: **a -= b

**Example: **a-=2 i.e. a=a-2

It multiplies the value of right operand and left operand and assigns result to left operand.__Multiply and assign:__

**Syntax: **a *= b

**Example: **a*=2 i.e. a=a*2

It divides the value of left operand with right operand and assigns result to left operand.__Divide and assign:__

**Syntax: **a /= b

**Example: **a/=2 i.e. a=a/2

It takes modulus of the value of left operand with right operand and assigns result to left operand.__Modulus and assign:__

**Syntax: **a %= b

**Example: **a%=2 i.e. a=a%2

It divides the value of left operand with right operand and assigns the floor value to left operand.__Divide(floor) and assign:__

**Syntax: **a //= b

**Example: **a//=2 i.e. a=a//2

It calculates exponent by raising the value of left operand to the power of right operand and assigns the result to left operand.__Exponent and assign:__

**Syntax: **a **= b

**Example: **a**=2 i.e. a=a**2

It performs bitwise AND operation to left and right operands and assigns the result to left operand.__Bitwise AND and assign:__

**Syntax: **a &= b

**Example: **a&=2 i.e. a=a&2

It performs bitwise OR operation to left and right operands and assigns the result to left operand.__Bitwise OR and assign:__

**Syntax: **a |= b

**Example: **a|=2 i.e. a=a|2

It performs bitwise XOR operation to left and right operands and assigns the result to left operand.__Bitwise XOR and assign:__

**Syntax: **a ^= b

**Example: **a^=2 i.e. a=a^2

It performs bitwise left shift operation to left and right operands and assigns the result to left operand.__Bitwise Left Shift and assign:__

**Syntax: **a <<= b

**Example: **a<<=2 i.e. a=a<<2

It performs bitwise right shift operation to left and right operands and assigns the result to left operand.__Bitwise Right Shift and assign:__

**Syntax: **a >>= b

**Example: **a>>=2 i.e. a=a>>2

** **

**Special operators**

Some special operators are offered by python like identity operator and the membership operator, here is the detailed explanation of each special operator:

‘__Identity operators:__**is**’ and ‘**is not**’ are two types of identity operators. They are used to check if two operands are located on the same part of the memory.

**is: **It returns True if both operands are identical

**is not:** It returns True if both operands are not identical

**Example:**

value1 = 5

value2 = 5

print(value1 is value2)

print(value1 is not value2)

OutputTrue

False

‘__Membership operators:__**in**’ and ‘**in not**’ are two types of identity operators. They are used to check if value is in a sequence.

**in: **It returns True if value is present in the sequence

**not in:** It returns True if value is not present in the sequence

### Frequently Asked Questions:

Q.1. What is a Python operator?

Answer: A python operator has the capability of manipulating certain values or operands. They are very useful and are the backbone of any program.

Q.2. What is the operator in Python used for?

Answer: Operators in python are in general used to perform mathematical or logical operations on values and variables. Below is the list of operators supported in python:

- Arithmetic operators
- Comparison operators
- Logical operators
- Bitwise operators
- Assignment operators
- Special operators

Q.3. What are the 3 python logical operators?

Answer:

In python, keyword__Logical AND operator:__**and**is used to perform logical AND It returns True if both the values are true, false otherwise.

If the first expression evaluated is False while performing **and **operator, then the further expressions are not evaluated.

**Example:**

value1 = 3

value2 = 2

print(value1>0 and value2>0)

OutputTrue

In python, keyword__Logical OR operator:__**or**is used to perform logical OR operation. It returns True if any of the two values is true, false otherwise.

If the first expression evaluated is True while performing **or **operator, then the further expressions are not evaluated.

**Example:**

value1 = 3

value2 = -2

print(value1>0 or value2>0)

OutputTrue

In python, keyword__Logical NOT operator:__**not**is used to perform logical NOT operation. It works with a single boolean value. It returns True if the value is False and vice versa.

**Example:**

value1 = 3

if not (value1 > 0) :

print (“value1 is not positive”)

else:

print (“value1 is positive”)

Outputvalue1 is positive

Q.4. What type of operator is == in Python?

Answer: ** **In python, symbol ‘==’ is used as equal to operator. It returns True if the values on its left and right side are equal, False otherwise.

**Example:**

value1 = 3

value2 = 2

print(value1 == value2)

Output:False

## Leave a Reply