Working with operators in python


Working with operators in python

In this article we will learn about different types of operators & working with operators in python. An operator is a symbol or a character in programming which takes two operands to perform an action. For example, in the expression "4 + 5" , "+" is an operator It will perform sum of numbers "4" and "5" and returns result "9".

Types of operators in python

  • Arithmetic Operators
  • Comparison Operators
  • Assignment Operators
  • Logical Operators
  • Bitwise Operators
  • Membership Operators
  • Identity Operators

Arithmetic Operators

consider the values a = 10 and b = 21

OperatorDescriptionExampleOutput
+Additionprint (a + b)31
-Subtractionprint (b - a)11
*Multiplicationprint (b * a)210
/Divisionprint (b / a)2
%Modulusprint (b % a)1
**Exponentprint (a ** 2)100
//Floor divisionprint (b // a)2

Comparison Operators

consider the values a = 10 and b = 20. comparison operators always returns a boolean value either True or False

OperatorDescriptionExampleOutput
>Greater thanprint(b > a)True
<Less thanprint(b < a)False
>=Greater than or equalprint(b >= a)True
<=Less than or equalprint(b <= a)False

Assignment Operators

Consider the value of b = 20

OperatorDescriptionExample
=Assigns the valuea = 100
+=Performs addition & assignmentb += 23  equivalent to  b = b + 23
-=Performs subtraction & assignmentb -= 13  equivalent to  b = b - 13
*=Performs multiplication & assignmentb *= 3  equivalent to  b = b * 3
/=Performs division & assignmentb /= 13  equivalent to  b = b / 13
%=Performs modulus operation & assignb %= 3  equivalent to  b = b % 3
//=Performs floor div & assignmentb //= 13  equivalent to  b = b // 13
**=Performs exp calculation & assignmentb **= 3  equivalent to  b = b ** 3

Logical Operators

OperatorDescriptionExample
andReturns boolean value if given conditions are true ⇒ True Otherwise ⇒ Falsea = 100 a > 90 and a < 110  ⇒ True a > 90 and a > 110  ⇒ False
orReturns boolean value At least one of the given conditions are true ⇒ True Otherwise ⇒ Falsea = 100 a > 190 and a < 110  ⇒ True a > 190 and a > 110  ⇒ True
notIt inverts the boolean valuenot True ⇒ False not False ⇒ True

Bitwise Operators

OperatorDescription
&Operator copies a bit to the result if it exists in both operands
|It copies a bit if it exists in either operand.
^It copies the bit if it is set in one operand but not both.
~It is unary and has the effect of 'flipping' bits.
>>The left operands value is moved left by the number of bits specified by the right operand.
<<The left operands value is moved right by the number of bits specified by the right operand.

Membership Operators

OperatorDescriptionExample
inReturns boolean value if it finds the var in given sequence returns truea = 10 a in [1, 2, 10, 20] ⇒ True a in [1, 2, 110, 20] ⇒ False
not inReturns boolean valuea = 10 a not in [1, 2, 10, 20] ⇒ False a not in [1, 2, 110, 20] ⇒ True

Identity Operators

OperatorDescriptionExample
isReturns boolean value True if both variables points to the same memory location in the RAMa = 100; b = 100; c = 101;
a is b ⇒ True a is c ⇒ False
Is notReturns boolean valuea = 100 b = 100 c = 101
a is not b ⇒ False a is not c ⇒ True