Python Operator

Python operator is used to performing different types of operations (arithmetic, logical, or types of computation) on variables or values. The variables or values are called operands.

For example

x=10
y=20
add=x+y
Here + is operator which carried arithmetic operation and x and y are called operands.

Python supports the following types of operators

  1. Arithmetic operators
  2. Logical (Boolean) operators
  3. Comparison(relational) operators
  4. Bitwise operators
  5. Assignment operators
  6. Special operators
Arithmetic operators

The arithmetic operator performs basic mathematical operations like addition, subtraction, multiplication, division, and others.

OperatorsDescriptionExample
Addition(+)The addition operator add either side of operandsx+y
Subtraction(-)Subtract right operand from left operandx-y
Multiplication(*)Multiply either side of the operandsx*y
Divison(/)Divide left operand from right operandx/y
Modulus(%)divide left operand from the right and return the remainderx%y
Exponent(**)Perform exponent(power) operation on left operand with power raised by right operandx**y

Example

x=20
y=9
add=x+y
print("Addition",add)
sub=(x-y)
print("Subtraction",sub)
mul=xy
print("Multiplication",mul)
div=(x-y)
print("Division",div)
pow=(x*y)
print("Exponent",pow)
floor=(x//y)
print("FloorDiv",floor)

Output

Addition 29
Subtraction 11
Multiplication 180
Division 11
Exponent 512000000000
FloorDiv 2
Logical Operator

The logical operator is used for performing logical AND, OR, and NOT operations.

OperatorDescriptionExample
ANDTruth;- If both operands are TRUEx and y
ORTrue:- If either of the operands is Truex or y
NOTTrue:- If the operand is Falsenot x

Example

x=True
y=False
result_and=x and y
print("AND",result_and)
result_or= x or y
print("OR",result_or)
result_not=not x
print("NOT",result_not)

Output
AND False
OR True
NOT False
Comparison(relational) operators

These types of operators are uses to comparing either side of the operand values. It returns output True or False.

OperatorDescriptionExample
Equal (==)if the value of both operands is equal then the condition becomes True else Falsex==y
Not Equal (!=)
if the value of both operands is not equal then the condition becomes True else False
x!=y
Greater than (>)If the left operand is greater than the right operand then the condition become Truex>y
Less than (<) If the left operand is less than the right operand then the condition become True x<y
Greater than equal to (>=)If the left operand is greater than and equal to the right operand then the condition become True x>=y
Greater than equal to (<=) If the left operand is less than and equal to the right operand then the condition become True x<=y

Example

equal=(x==y)
notequal=(x!=y)
greater=(x>y)
less=(x=y)
less_equal=(x<=y)
print("Equal to", equal)
print("Not equal to", notequal)
print("Greater than", greater)
print("Less than", less)
print("Less than equal to", greater_equal)
print("Less than equal to", less_equal)

Output
Equal to False
Not equal to True
Greater than True
Less than False
Less than equal to True
Less than equal to False
Bitwise operator

Bitwise operator performs operations bit by bit, First, it converts integer value into binary format and then performs a logical operation on each bit of binary value.

OperatorDescriptionExample
& Bitwise AND x&y
|Bitwise ORx|y
~Bitwise NORx~y
^Bitwise XORx^y
<<Bitwise left shiftx<<y
>>Bitwise right shiftx>>y

Example

x=20
y=15
bitand=x&y
print("bitwise AND", bitand)
bitor=x|y
print("bitwise OR", bitor)
bitnor=~x
print("bitwise NOT", bitnor)
bitxor=x^y
print("bitwise XOR", bitxor)
bitleft=x<>y
print("bitwise Right", bitright)

Output
bitwise AND 4
bitwise OR 31
bitwise NOT -21
bitwise XOR 27
bitwise Left 655360
bitwise Right 0
Assignment operator

Assignment operators are used in assigning a value to the variable but it can be in different ways.

OperatorDescriptionEquivalent
=Assing value to right side operand to left size operandx=10

+= It calculates addition first and then assigns value to the left operand x=x+10
-=It calculates subtraction first and then assigns value to the left operand x=x-10
*= It calculates multiplication first and then assigns value to the left operand x=x*10
/= It calculates division first and then assigns value to the left operand x=x/10
%=It takes modules first and thn assigns value to left operandx=x%10
**=It calculates exponent first and then assigns value to the left operand x=x**10
//=It calculates floor division first and then assigns value to the left operand x=x//10
Special operators

There are also different types of operator called Membership operator and Identity operator.

OperatorDescriptionExample
in(Membership operator)Return True if element found in the specified sequence10 in x

not in(Membership operator)Return True if element not found in the specified sequence10 not in x
is(Identity operator)Return True if operands are identicalx is True
is not(identity operator)Return True if operand are not identicalx is not True

About Mitra N Mishra 35 Articles
Mitra N Mishra is working as a full-stack data scientist.

2 Comments

  1. It’s in point of fact a nice and helpful piece of info. I’m happy that you shared this helpful info with us. Please stay us informed like this. Thank you for sharing.

  2. I’ll right away grab your rss feed as I can not find your e-mail subscription link or newsletter service. Do you’ve any? Please let me know so that I could subscribe. Thanks.

Leave a Reply

Your email address will not be published.


*