Blog

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.

Operator Meaning Example
+ Add two operands or unary plus p + q ( where p,q are two variables
Subtract right operand from the left or unary minus p – q
* Multiply two operands p * 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 right p % 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 right p**y (p to the power y)

 

Comparison operators

Comparison operators in Python

Operator Meaning Example
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 right p < y
== Equal to – True if both operands are equal p == y
!= Not equal to – True if operands are not equal p != y
>= Greater than or equal to – True if the left operand is greater than or equal to the right p >= y
<= Less than or equal to – True if the left operand is less than or equal to the right p <= y

 

Logical operators

Logical operators in Python

Operator Meaning Example
And True if both the operands are true p and y
or True if either of the operands is true p or y
not True if an operand is false (complements the operand) not p

 

Assignment operators

Assignment operators in Python

Operator Example Equivalent to
= p = 5 p = 5
+= p += 5 p = p + 5
-= p -= 5 p = p – 5
*= p *= 5 p = p * 5
/= p /= 5 p = p / 5
%= p %= 5 p = p % 5
//= p //= 5 p = p // 5
**= p **= 5 p = p ** 5
&= p &= 5 p = p & 5
|= p |= 5 p = p | 5
^= p ^= 5 p = p ^ 5
>>= p >>= 5 p = p >> 5
<<= p <<= 5 p = 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”)

else:

   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.