3 Jan 2020

Python Operators

In our next topic, we shall discuss various Operators that can be used in Python while writing a program. Before we start anything let us try to explain what is an operator?

Python Operators

Operators are some special symbols in Python that carry out arithmetic or logical computation. The value that the operator operates on is called the operand. An operator may take only one operand and some operators may take two operands.

For example: if we write  c=a + b – it means we are trying to add two variables which are a and b and storing the value in another variable c. In this, we are using two symbols = and + which are actually operators – + is used to add two things a and b whereas = is being used to store the value in c. Also, a,b,c all here are treated as operands. In Python, Operators are classified under various categories like –

  1. Arithmetic operator
  2. Comparison (Relational) operator
  3. Boolean(Logical ) operator
  4. Assignment operator
  5. Special operator Like –
    • Identity Operator
    • Membership Operator

The Following tables will describe various operators in different categories.

+Add two operands or unary plusp + q ( where p,q are two variables
Subtract right operand from the left or unary minusp – q
*Multiply two operandsp * y
/Divide left operand by the right one (always results into float)p / y
%Modulus – the remainder of the division of left operand by the rightp % y (remainder of p/y)
//We can find the Quotient even if the exact division is not possible.p // y
**Exponent – left operand raised to the power of rightp**y (p to the power y)


Comparison operators

Comparison operators in Python

Greater than – It checks if a No. is greater than the other No.p > y
Less than – True if the left operand is less than the rightp < y
==Equal to – True if both operands are equalp == y
!=Not equal to – True if operands are not equalp != y
>=Greater than or equal to – True if the left operand is greater than or equal to the rightp >= y
<=Less than or equal to – True if the left operand is less than or equal to the rightp <= y


Logical operators

Logical operators in Python

AndTrue if both the operands are truep and y
orTrue if either of the operands is truep or y
notTrue if an operand is false (complements the operand)not p


Assignment operators

Assignment operators in Python

OperatorExampleEquivalent to
=p = 5p = 5
+=p += 5p = p + 5
-=p -= 5p = p – 5
*=p *= 5p = p * 5
/=p /= 5p = p / 5
%=p %= 5p = p % 5
//=p //= 5p = p // 5
**=p **= 5p = p ** 5
&=p &= 5p = p & 5
|=p |= 5p = p | 5
^=p ^= 5p = p ^ 5
>>=p >>= 5p = p >> 5
<<=p <<= 5p = p << 5


Membership operators

To understand the concept of Membership Operators we are citing the code of a program.

# Python program to illustrate

# not ‘in’ operator

y = 20

list = [10, 20, 30, 40, 50 ]

if ( y not in list ):

   print (“y is NOT present in given list”)


   print (“y is  present in given list”)

Since 20 is present in the List so the output will be – y is present in the given list

Please note: we will be shortly uploading a few more contents on Operators. So keep watching our blog and of course Happy New Year to All of You.