Python OPERATORS (PYTHON FUNDAMENTALS)

Created with Sketch.

Python OPERATORS

  •  The operator is a symbol that performs certain operations.
  •  Python provides the following set of operators

            1) Arithmetic Operators
            2) Relational Operators OR Comparison Operators
            3) Logical operators
            4) Bitwise operators
            5) Assignment operators
            6) Special operators

1) Arithmetic Operators:

1) +  —> Addition
2) –  —> Subtraction
3) *  —> Multiplication
4) /  —> Division Operator
5) %  —> Modulo Operator
6) //  —> Floor Division Operator
7) **  —> Exponent Operator OR Power Operator


Eg: 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:
Python test.py OR py test.py
a+b = 12
a-b= 8
a*b= 20
a/b= 5.0

a//b= 5
a%b= 0
a**b= 100

 

Eg:

 a = 10.5 
 b=2 
 
 a+b= 12.5 
 a-b= 8.5 
 a*b= 21.0 
 a/b= 5.25 
 a//b= 5.0 
 a%b= 0.5 
 a**b= 110.25 

Eg:
10/2 —> 5.0
10//2 —> 5
10.0/2 —> 5.0
10.0//2 —> 5.0

Note:

  •  / operator always performs floating point arithmetic. Hence it will always return 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 at least one argument is float type then
    the result is float type.

Note:

  •  We can use +,* operators for str type also.
  •  If we want to use + operator for str type then compulsory both arguments should be
    str type only otherwise we will get error.
>>> "durga"+10 
TypeError: must be str, not int 
>>> "durga"+"10" 
'durga10'
  • If we use * operator for str type then compulsory one argument should be int and
    other arguments should be str type
  • 2*”mohan”
    “mohan”*2
    2.5*”mohan” –> TypeError: can’t multiply sequence by non-int of type ‘float
    “mohan”*”mohan” –> TypeError: can’t multiply sequence by non-int of type ‘str
  • + –> String Concatenation Operator
  • –> String Multiplication Operator

Note: For any number x,


x/0 and x%0 always raise “ZeroDivisionError
10/0
10.0/0

2) Relational Operators: >, >=, <, <=

 a=10 
 b=20 
 print("a > b is ",a>b) 
 print("a >= b is ",a>=b) 
 print("a < b is ",a<b) 
 print("a <= b is ",a<=b) 
 
 a > b is False 
 a >= b is False 
 a < b is True 
 a <= b is True

We can apply relational operators for str types also.

Eg 2:

 a="durga" 
 b="durga" 
 print("a > b is ",a>b) 
 print("a >= b is ",a>=b) 
 print("a < b is ",a<b) 
 print("a <= b is ",a<=b) 

8) a > b is False
9) a >= b is True
10) a < b is False
11) a <= b is True

Eg:

 print(True>True) False 
 print(True>=True) True 
 print(10 >True) True 
 print(False > True) False 
 
 print(10>'durga') 
 TypeError: '>' not supported between instances of 'int' and 'str'

Eg:

 a=10 
 b=20 
 if(a>b): 
 print("a is greater than b") 
 else: 
 print("a is not greater than b") 

Output: a is not greater than b

Note: Chaining of relational operators is possible. In the chaining, if all comparisons
returns True then only result is True. If atleast one comparison returns False then the
result is False

1) 10<20 –> True

2) 10<20<30 –> True

3) 10<20<30<40 –> True

4) 10<20<30<40>50 –> False

3) Equality Operators: ==, !=

We can apply these operators for any type even for incompatible types also.

>>> 10==20 
 False 
>>> 10!= 20 
 True 
>>> 10==True 
 False 
>>> False==False 
 True 
>>> "mohan"=="mohan" 
 True 
>>> 10=="mohan" 
 False 

Note: Chaining concept is applicable for equality operators. If atleast one comparison
returns False then the result is False. Otherwise the result is True.

 >>> 10==20==30==40 
 False 
 >>> 10==10==10==10 
 True 

Leave a Reply

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