Operators in Python

by | May 29, 2021 | Python

Introduction

Operators are the standard symbols used for performing operations on variable and value. For example, to add two values we use the arithmetic operator +, where symbol + signifies the specific meaning of addition.

Types of operators in Python

We have seven types of operators in Python. They are:

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

 

1.      Arithmetic operator

The arithmetic operator includes:

 

Operator symbol Operator name
+ Addition
Subtraction
* Multiplication
/ Division
% Modulus
// Floor division
** Exponentiation

 

Program:

print("Addition: 10 + 2 = ", (10+2))

print("Subtraction: 10 - 2 = ", (10-2))

print("Multiplication: 9 * 4 = ", (9*4))

print("Division: 10 / 2 = ", (10/2))

print("Modulus: 8 % 3 = ", (8%2))

print("Floor division: 25 // 2 = ", (25//2))

print("Exponentiation: 7 ** 2 = ", (7**2))

 

Output:

Arithmetic operator Output

2.      Assignment operator

Assignments operators are used to assign the values to variables in programming. It includes:

 

Operator symbol Description
= Assign value to variable
+= Add right side value to left side and then assign the sum to left side variable.
x += y => x = x+ y
-= Subtract right side value from left side and then assign the value to left variable.
x -= y => x = x – y
*= Multiply right-side value from left side and then assign the value to left variable.
x *= y => x = x * y
/= Divide right-side value from left side and then assign the value to left variable.
x /= y => x = x / y
%= Compute modulus with left and right-side values and then assign the value to left variable.
x %= y => x = x% y
//= Divide right-side value from left side and then assign the floor value to left variable.
x //= y => x = x // y
**= Compute exponents using left and right-side values and assign the result to left variable.
x **= y => x = x**y
&= Compute Bitwise AND on variables and assign to left variable.
x &= y => x = x & y
|= Compute Bitwise OR on variables and assign to left variable.
x |= y => x = x | y
^= Compute Bitwise xOR on variables and assign to left variable.
x ^= y => x = x ^ y
>>= Compute Bitwise right shift on variables and assign to left variable.
x >>= y => x = x >> y
<<= Compute Bitwise left shift on variables and assign to left variable.
x <<= y => x = x << y

 

3.      Comparison operator

Comparison operators are used to compare values and variables and return True or False depending upon the outcome. It includes:

 

Operator symbol Operator name
== Equal
!= Not Equal
Greater than
Less than
>= Greater than or equals
<= Less than or equals

 

Program:

 

print("10>16 ? True:False =>", 10 > 16)

print("10<16 ? True:False =>",10 < 16)

print("8 == 3 ? True:False=>",8 == 3)

print("3 != 4 ? True:False =>",3 != 4)

print("6 >= 9 ? True:False =>",6 >= 9)

print("6 <= 6 ? True:False =>",6 <= 6)

 

Output:

Comparison operators Output

 

4.      Logical operator

Logical operators combine conditional statements and perform Logical And, Logical OR and Logical Not.

 

Operator symbol Operator name
and Logical and
or Logical or
not Logical not

 

Program:

a = False

b = True

print(" a = False ;  b = True")

print("Output of Logical AND ", a and b)

print("Output of Logical OR", a or b)

print("Output of Logical NOT", not a)

Output:

Logical operator Output 

 

5.      Identity operator

Identity operator is used to check whether the objects are same not just only the value but whether they are having same memory location.

 

Operator symbol Description
is If the objects are same, then returns true.
is not If the objects are different, then returns false.

 

Program:

val1 = ["sample1", "sample2"]

val2 = ["sample1", "sample2"]

temp = val1

#val1 is same as temp, hence returns True

print(val1 is temp)

#val1 and val2 are different, hence returns True

print( val1 is not val2)

#val1 and val2 are different, hence returns False

print(val1 is val2)

 

Output:

Identity operator Output

6.      Membership operator

It is used to check whether the sequence is present in the object. Membership operator includes:

 

Operator symbol Description
in If the specified value is present in the given sequence, then returns true.
not in If the specified value is not present in the given sequence, then returns true.

 

Program:

ip_string = 'Program for membership operator'

print('o' in ip_string)

print('Program for membership' in ip_string)  

print('Program membership' not in ip_string)

print('Program for membership' not in ip_string)

 

Output:

Membership operator Output 

7.      Bitwise operator

It is used to compare binary values. The bitwise operator includes:

 

Operator symbol Description
& AND
| OR
^ XOR
~ NOT
<<  Left shift
>>  Signed right shift

 

Program:

x = 34

y = 7

print("x = {0} ; y = {1}".format(x,y)) 

print("Bitwise AND operation: x & y = ", x & y)

print("Bitwise OR operation: x | y = ", x | y) 

print("Bitwise NOT operation: ~x = ",~x)

print("bitwise XOR operation: x ^ y = ",x ^ y) 

print("Bitwise right shift operation: x >> 2 = ", x >> 2)

print("Bitwise left shift operation: x << 2 = ",x << 2)

 

Output:

bitwise operator Output

Operator precedence and Associativity

When more than one operator is present in the expression, we check for the operator precedence to determine which operation to compute first.

 

When more than one operator with same precedence is present in the expression, operator associativity is used to determine in which direction to move (left to right or right to left) for calculation.

 

Operator Description Associativity
() Parenthesis Left-to-right
** Exponent Right-to-left
*/% Multiplication/Division/Modulus Left-to-right
+- Addition/Subtraction Left-to-right
<< >> Bitwise Left/Bitwise Right Left-to-right
< >
<= >=
Less than/ Greater than
Less than or equal/Greater than or equal
Left-to-right
== != Equal to/ Not equal to Left-to-right

 

Program: Operator Precedence

# ** has highes precedence hence it is calculated first, then / and at last + is calculated and output is returned=> 6**2 = 36 ; 36/4 = 9; 5 + 9 = 14

print("5 + 6 ** 2 / 4 = ",5 + 6 ** 2 / 4)

 

Output:

Operator Precedence Output

Program: Operator Associativity

print("Left-to-right associativity: 10 / 10 * 10 = ",10 / 10 * 10) 

print("Right-to-left associativity: 5 ** 2 ** 2 = ", 5 ** 2 ** 2)

Output:

Operator Associativity Output

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.