Arithmetic Operator | Operators in Python | Digital Community



Types of Operators in Python

 

 

Python provides us 7 popular types of operators:

 

 

·      Arithmetic Operators

 

·      Relational or Comparison Operators

 

·      Logical Operators

 

·      Assignment Operator


·      Compound Operators

 

·      Identity Operators

 

·      Membership Operators

 

In this post, we are going to discuss Operators in Python but before these, if You are not seen our video on this topic please see it first, then you will understand the concepts very well.






First, we, discuss Arithmetic Operator:

 

      Arithmetic Operators

 

      Special points about + and *

 

      Difference between / and //

 

 

Operators are special symbols that carry out different kinds of computation on values.

 

For example 2+3

 

In the expression 2+3, + is an operator which performs addition of 2 and 3, which are called operands

 

 

o  In Python, we have 7 arithmetic operators and they are as below:

 

 

         +

(Arithmetic Addition)

 

         - 

(Subtraction)

 

         *  

(Arithmetic Multiplication)

 

         /

(Float Division)

 

         %

(Modulo Division)

 

         //

(Floor Division)

 

         **

(Power or Exponentiation)

 

 

o  The Example of 5 Basic Arithmetic Operators :

 

a=10

b=4

print("sum of",a,"and",b,"is",a+b)

print("diff of",a,"and",b,"is",a-b)

print("prod of",a,"and",b,"is",a*b)

print("div of",a,"and",b,"is",a/b)

print("rem of",a,“and",b,"is",a%b)

 

The Output :

 

 



o  Two Special Operators // and ** :

 

 

The operator // in Python is called as floor division.

 

This means it returns the integer part and not the decimal part.

 

For example:  

 

5//2 will be 2 not 2.5

 

 

o  But there are 3 very important points to understand about this operator

o  When used with positive numbers the result is only the integer part of the actual answer i.e. , the decimal part is truncated

 

o  However if one of the operands is negative, the result is floored.

 

o  If both the operands are integers, the result will also, be an integer, otherwise, the result will be float

 

a=10

b=4

print(a//b)


2


a=10.0

b=4

print(a//b)


2.0

 

If both the operands are integers, the result is also an integer. But if any of the operands is float the result is also float.

  

An Important Point

 

o  There is another very important point to remember about the 3 operators /, // and %

 

o  The point is that if the denominator in these operators  is 0 or 0.0, then Python will throw the exception called ZeroDivisionError

 

a=10

b=0

print(a/b)

 

Output:

ZeroDivisionError


a=10

b=0.0

print(a/b)

 

Output:

ZeroDivisionError




The power (**)Operator

 

The power operator i.e. ** performs exponential (power) calculation on operands.

 

For example: 

         a=10

         b=3

         print(a**b)


Output:

         1000


Double Role Of The Operator +



o  The operator + as discussed earlier also ,has 2 roles in Python

 

o  When used with numbers , it performs addition and when used with strings it performs concatenation


For example:

       a=10

       b=5

       print(a+b)

 

Output:


       15

 

a=“Good”

b=“Evening”

print(a+b)


Output:


GoodEvening 

a=“Good”

b=10

print(a+b)

 

Output:


TypeError



Double Role of The Operator *


 

o  The operator * also has 2 roles in Python

 

o  When used with numbers, it performs multiplication and when used with one operand string and another operand int it performs repetition


For example:

a=10

b=5

print(a*b)


50


a="Digital"

b=3

print(a*b)

DigitalDigitalDigital


a=“Digital”

b=“Community”

print(a*b)


Output:

Type Error : Can’t multiply by non int

 




And if you have any doubt regarding this topic then , you can ask your doubt in the comment section , I will respond you as soon as possible .




DIGITAL COMMUNITY

We Make IT Happen.



 

No comments:

Post a Comment

If you have any doubts, let me know.