BASIC OPERATORS IN PYTHON – 1

operators in python - Innovate Yourself
0
0

OVERVIEW

In this blog, you will learn all about the different types of operators in python. Which are:

  • Arithmetic Operators
  • Comparison(Relational) Operators
  • Membership Operators
  • Logical Operators
  • Bitwise Operators
  • Identity Operators
  • Assignment operators

Now let’s understand about them one by one.

ARITHMETIC OPERATORS IN PYTHON

OperatorDescriptionExample
+ AdditionThis will add all the values used with this operator.=>a+b
=>4+5
=>9
– SubtractionThis will subtract the right side value from the left side value used with this operator.=>a-b
=>8-5
=>3
* MultiplicationThis will multiply all the values used with this operator.=>a*b
=>8*5
=>40
/ DivisionThis will divide the left value with the right value with this operator.=>a/b =>8/5
=>1.6
% ModulusThis will divide the left value with the right value and will return the reminder.=>a%b =>8%5
=>3
// Floor divisionFloor Division – The division of operands where the result is the quotient in which the digits after the decimal point are removed.9//2 = 4 and
9//2.0 = 4.0
** ExponentThis will perform the exponential calculation on the operators. =>4**3
=>64

Example

Assume variable a holds 10 and variable b holds 20, then-

OPERATORS IN PYTHON

When you execute the above program arithmetic operators in python, it returns the following result-

arithmetic output OPERATORS IN PYTHON

COMPARISON OPERATORS IN PYTHON

OperatorDescriptionExample
==If both values around the operand are equal, it will return True
otherwise it will return False
=>(a==b)
=> (10==20)
=>False
!=If both values around the operand are not equal, it will return True
otherwise it will return False
=>(a!=b)
=> (10!=20)
=>True
<If the left value is less than the right value, it will return True
otherwise it will return False
=>(a<b)
=> (10<20)
=>True
>If the left value is greater than the right value, it will return True
otherwise it will return False
=>(a>b)
=> (10>20)
=>False
<=If the left value is less than or equal to the right value, it will return True
otherwise it will return False
=>(a<=b)
=> (20<=20)
=>True
>=If the left value is greater than or equal to the right value, it will return True
otherwise it will return False
=>(a>=b)
=> (30>=20) =>True

Example

Assume variable a holds 10 & variable b holds 20, then-

OPERATORS IN PYTHON

When you execute the above program comparison operators in python, it returns the following result-

comparison output OPERATORS IN PYTHON

ASSIGNMENT OPERATORS IN PYTHON

OperatorDescriptionExample
=Assigns values from right side operands to
left side operand
c=a+b
value(a+b) is assigned to c
+=It will add the right value to the left value and will assign the result to the left operand.c+=a is equivalent to the c=c+a
-=It will subtract the right value from the left value and will assign the result to the left operand.c-=a is equivalent to the c=c-a
*=It will multiply the right value to the left value and will assign the result to the left operand.c*=a is equivalent to the c=c*a
/=It will divide the right value from the left value and will assign the result to the left operand.c/=a is equivalent to the c=c/a
%=It will divide the right value from the left value and the remainder value will assign the result to the left operand.c%=a is equivalent to the c=c%a
**=It will perform exponential(power) calculation and will assign the result to the left operand.c**=a is equivalent to the c=c**a
//=It performs floor division on operators and
assign value to the left operand.
c//=a is equivalent to the c=c//a

Example

Assume variable a holds 10 and variable b holds 20, then-

OPERATORS IN PYTHON

When you execute the above program assignment operators in python, it returns the following result-

assignment out OPERATORS IN PYTHON

BITWISE OPERATORS IN PYTHON

Bitwise operators works bit wise and performs operation bit-by-bit. Assume a = 60 and b = 13. Now in binary format they will be as follows-

a = 0011 1100
b = 0000 1101
———————-
a&b = 0000 1100
a|b = 0011 1101
a^b = 0011 0001
~a = 1100 0011

Python’s built-in function bin() can be used to obtain binary representation of an integer
number.

OperatorDescriptionExample
&Operator copies a bit to the result, if it
exists in both operands
=>(a & b)
=>(0011 1100 & 0000 1101)
=>0000 1100
|It copies a bit, if it exists in either
operand.
=>(a | b)
=> (0011 1100 | 0000 1101)
=> 61 (means 0011 1101)
^It copies the bit, if it is set in one operand
but not both.
=>(a ^ b)
=> (0011 1100 ^ 0000 1101)
=> 49 (means 0011 0001)
~It is unary and has the effect of ‘flipping’
bits.
=>(~a )
=> ~(0011 1100)
=> -61 (means 1100 0011 in 2’s
complement form due to a signed
binary number.
<<The left operand’s value is moved left by
the number of bits specified by the right
operand.
a << = 240 (means
1111 0000)
>>The left operand’s value is moved right
by the number of bits specified by the
right operand.
a >> = 15 (means
0000 1111)

Example

OPERATORS IN PYTHON

When you execute the above program bitwise operators in python, it returns the following result-

bitwise output OPERATORS IN PYTHON

LOGICAL OPERATORS IN PYTHON

The following logical operators in python are supported by Python language. Assume variable a has value True and variable b has value False then-

OperatorDescriptionExample
andIf both side of the operator the value is true that it returns True, otherwise False=>(a and b)
=>False.
orIf either side of the operator the value is true that it returns True, otherwise False=>(a or b)
=>True.
notUsed to invert the logical state of its operand.=>not(a and b)
=>True.

MEMBERSHIP OPERATORS IN PYTHON

OperatorDescriptionExample
inIt evaluates the output to true, if it finds a variable
in the specified sequence, otherwise false.
=>6 in [1,2,3,4,5,6]
=>True
not inIt evaluates the output to true, if it doesn’t find a variable
in the specified sequence, otherwise false.
=>6 not in [1,2,3,4,5,6]
=>False

Example

OPERATORS IN PYTHON

When you execute the above program for membership operators in python, it returns the following result-

membership output OPERATORS IN PYTHON

IDENTITY OPERATORS IN PYTHON

OperatorDescriptionExample
isIt evaluates to true if the variables on
either side of the operator point to the
same object and false otherwise.
x is y, here is results
in 1 if id(x) equals
id(y).
is notIt evaluates to false if the variables on
either side of the operator point to the
same object and false otherwise.
x is not y, here is
not results in 1 if id(x)
is not equal to id(y).

Example

OPERATORS IN PYTHON

When you execute the above program identity operators in python, it returns the following result-

identity output OPERATORS IN PYTHON

Check out this video for more clarification:

Time to wrap up now. See you in our next blog, thanks for reading our blog and do leave a comment below to help us improve the content to serve you all of our experience with you. Stay tuned with us for more python programming contents.

Also check out our other playlist Rasa Chatbot, Internet of things, Docker, etc.
Become a member of our social family on youtube here.

Leave a Reply