What are different types of Operators in Python

Operator is a symbol that performs certain operations.

Python provides the following set of operations.

1.       Arithmetic Operators

2.       Relational Operators or Comparison Operators

3.       Logical Operators

4.       Bitwise Operators

5.       Assignment Operators

6.      Special Operators

Arithmetic Operators:

+  ===>  Addition Operator

-===>  Subtraction Operator

* ===> Multiplication Operator

/===> Division  Operator

%===> Modulo Operator

// ===> Floor Division Operator

** ===> Exponent Operator or Power Operator

Ex :test.py

a=10

b=2

print(‘a + b=’ a+ b)

print(‘a - b=’ a - b)

print(‘a* b=’ a *b)

print(‘a / b=’ a / b)

print(‘a // b=’ a // b)

print(‘a % b=’ a % b)

print(‘a**b=’ a**b)

Output:

a + b= 12

a – b = 8

a * b = 20

a / b = 5.0

a // b = 5

a % b =0

a**b= 100

 

Note: / operator  always perform floating point arithmetic. Hence it will always returns float value.

But Floor division ( //) can perform both floating point and integral arithmetic. If arguments are  int type  then the result is int type. If one argument is float type then the result is float type.

 

Relational Operators:

>,>=, <,<=

Equality Operators:  ==, !=

Logical Operators :

and, or, not

 For boolean types behaviour:

and===> if boh arguments are True then only result is True

or===> If atleast one argument is True then result is True

no===> complement

True and False ====> False

True or  False ====> True

Not False ===> True

Fornon- boolean types behaviour:

0 means False

Non-zero means True

Empty string is always treated as false

X and y:

===> if x is evaluates to a false return x otherwise return y

Ex:

10 and 20

0 and 20

If first argument is zero then he result is zero otherwise result is y.

 

X or y : ===> if x is evaluates to a True hen result is  x otherwise result is  y

10 or 20 ====>10

0 or 20 ====> 20

 

Bitwise Operators:

We can apply these operators bitwise. These operators are applicable only for int and Boolean types. By mistake if we are trying to apply for any other type then we will get Error.

Bitwise operators are :

&, |,^,~,<<,>>

Operator

Description

&

If both bits are 1 then only result is 1 otherwise result is 0

|

If atleast one bit is 1 then only result is 1 otherwise result is 0

^

If bits are different then only result is 1 otherwise result is 0

~

Bitwise complement operator i.e 1 means 0 and o means 1

<< 

Bitwise Right shift  operator

>> 

Bitwise Left  shift  operator

 

Special Operators:

Python defines the following 2 special operators.

  1. Identity Operators
  2. Membership Operators

 

Identity Operators:  

we can use identity operators for address comparison.  2 identity operators are available.

  1. Is
  2. Is not

r1 is r2 return true if both r1 and r2 are pointing to the same object.

r1 is not  r2 return true if both r1 and r2 are not  pointing to the same object.

Note: we can use is operator address comparison where as  == operator for content comparison.

 

Membership Operators :

We can use Membership Operators to check whether the given object  present in the given collection. ( it may be String, List, Set, Tuple or Dict).

In ---> Returns True if the given object present in the specified Collection.

Not in ---à Returns True if the given object  not present in the specified Collection.