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.

OperatorNameDescription
+AdditionUsed to add two numbers.
SubtractionUsed to subtract one number from another.
*MultiplicationUsed to multiply two numbers.
** ExponentialUsed to calculate the power of a number, i.e., A^{b}
/DivisionUsed to divide one number by another.
//Floor divisionUsed to get the floor value of the quotient.
% Modulus Used to find the remainder of the division.

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

OperatorExplanation Description
= Basically this is the actual assignment operator. The rest are derived from it.Assigns the value of the right-hand operand to the left-hand operand.
+= a+=b means a=a+bAdds the left and right operands and assigns the value to the left operand.
-= a-=b means a=a-b Adds the right operand to the left operand and assigns the value to the left operand.
*= a*=b means a=a*b Multiplies the left and right values ​​and assigns them to the left operand.
/= a/=b means a=a/b Assigns the left operand by dividing the left operand by the value of the right operand.
%= a%=b means a=a%b Takes the modulus of the two operands and assigns it to the left operand.
//= a//=b means a=a//b Divides the floor by the left operand and assigns to the left operand.
**= a**=b means a=a**b Computes the index value and assigns the value to the left-hand operand.

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 –

OperatorNameDescription
== EqualChecks if two operands are equal. Returns True if equal and False otherwise.
!= Not equal)Just the opposite of the equals operator. That is, returns True if not equal and False if equal.
> Greater thanUsed to check case between two operands. Returns True if the left-hand operand is greater than the right-hand operand, otherwise returns False.
< Less thanThe opposite of the greater than operator, i.e. Returns False if the left operand is greater than the right operand and True otherwise.
>= Greater than or equal toSimilar to the greater than operator, only here equals will be true.
<= Less than or equal toSimilar to the less than operator, but here equals to true.

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 –

OperatorDescription
andReturns True if both statements are true.
orReturns True if either statement is true.
notShows the opposite of the results obtained. That is, if it is true it is false and if it is false it makes it honest.

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 –

অপারেটরবর্ণনা
isReturns True if the two objects are the same.
is notReturns True if the two objects are not the same.

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 –

অপারেটরনামবর্ণনা
& ANDlogical AND operator; ie will be 1 if all bits (Input Line) are 1.
| ORlogical OR operator; That is, if any one bit (Input Line) is 1, it will be 1.
^ XORlogical XOR operator; That is, if odd number of bits (Input Line) is 1 then only 1 will be.
NOTlogical NOT operator; That is, 0 is 1 and 1 is 0.
<< Binary left shiftTo shift the bits to the left.
>> Binary right shiftTo move the bits to the right.

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 −

অপারেটরবর্ণনা
inReturns True if the element is contained in the specified object.
not inReturns True if the element is not contained in the specified object.

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 🙂

0 0 votes
Article Rating
0
Would love your thoughts, please comment.x
()
x