In the previous post, we learned about variables in Python. An operator is something that operates on a variable or a constant, i.e., performs an operation. We have been familiar with operators for a long time. For example, let’s consider a mathematical statement:

10 + 10 = 20

Here, 10 is the operand and + is the operator that works on the operands. There are several types of operators in Python:

• Arithmetic Operator
• Assignment Operator
• Comparison Operator
• Logical Operator
• Identity Operator
• Bit-wise Operator
• Membership Operator

Let’s take a look at them one by one:

## Arithmetic Operator

As the name suggests, these operators are used to solve mathematical problems.

Now lets see an example for each operator –

```# This program is an example of an arithmetic operator in python

a=14
b=5

print(a-b) # Subtraction
print(a*b) # Multiplication
print(a**b) #exponential
print(a/b) #Division
print(a//b) #Floor division
print(a%b) #Modulus
```

## Assignment operator

Assignment operators are used to assign values to variables.

Note here that many expressions can be written much more concisely by using only the ‘=’ operator.

Now let’s see some examples using this operand –

```# This program is an example of assignment operator in python

a=10
b=5
c=8
d=4
e=6
f=7
g=9
h=6

i=a # = Operator
print (i)

b+=a # += Operator
print(b)

c-=a # -= Operator
print(c)

d*=c # *= Operator
print(d)

e/=d # /= Operator
print(e)

f%=e # %= Operator
print(f)

g//=f # //= Operator
print(g)

h**=g # **= Operator
print(h)
```

## Comparison Operator

Comparison operator is used to compare two values. Note here that the value of the statement created using this operator is True or False. See below for these operators –

Here are some examples using these operators –

```# This program is an example of comparison operator in python

a=10
b=5

print(a==b) # == Operator
print(a!=b) # != Operator
print(a>b) # > Operator
print(a<b) # < Operator
print(a>=b) # >= Operator
print(a<=b) # <= Operator
```

## Logical operator

Logical operators are generally used to simplify one or more conditional statements to arrive at a general conclusion. There are total 3 logical operators –

Now we have seen some examples of logical operators-

```# This program is an example of logical operator in python

a=5
b=10

print(a<b and b>a) # and Operator
print(a>b or b>a) # or Operator
print(not(a>b or b>a)) # not Operator
```

## Identity operator

Earlier we looked at comparison operators; But they are basically working on value. The identity operator is used to check whether an object is the same i.e. belongs to the same memory location.

The number of identity operators is only two –

We have not yet seen an example of this –

```# This program is an example of identity operator in python

a=10
b=10
c=a

print(a is b) # is Operator
print(a is not b) # is not Operator
print(a is c) # is Operator
print(a is not c) # is not Operator
```

## Bit-wise operator

Bitwise operators are used for binary calculations. For those who have a good understanding of Logic Gate, this part will be useful to understand.

Although this episode is not meant to discuss logic gates or binary numbers, let’s discuss a very small amount of gates. As our computer or any electronics device is based on binary numbers, i.e. 0 or 1, commonly used numbers need to be converted to binary. For example 5 in binary is 101 and 10 in binary is 1010. Now if we add 5 and 10 in binary then –

``````1010
+101
-----
1111``````

1111 ie 15

I hope you understand. If you don’t understand then you can check it from here. Now we look at the operators –

Now let us see some examples of these –

```# This program is an example of bitwise operator in python

a=10
b=5

print(a&b) # AND Operator
print(a|b) # OR Operator
print(a^b) # XOR Operator
print(~a) # NOT Operator
print(a<<2) # Binary Left Shift Operator
print(a>>2) # Binary Right Shift Operator
```

Note: Here two cells are shifted left and right with a<<2 and a>>2. value of a is 10 and binary of 10 is 1010; Moving it two places to the left will be 1010 00 i.e. 40. Again moving it to the right will be 00 10 i.e. 2.

## Membership operator

The membership operator is used to check whether a Python object (list, tuple, string, etc.) contains a specific element. Python has two membership operators −

Let us see an example –

```# This program is an example of membership operator in python

a=10
b=5
testList=[1,10,20,30,40,50] # This is a list

print(a in testList) # in Operator
print(b in testList) # in Operator
print(a not in testList) # not in Operator
print(b not in testList) # not in Operator
```

Note: The list has not been discussed so far. In short, List is a Python object that can hold multiple values inside a single variable.

In this episode we learned about various operators in Python. If there is any difficulty in understanding or any question please comment or ask question.

thanks 🙂