top

Search

Python Tutorial

.

UpGrad

Python Tutorial

Arithmetic Operators in Python

Introduction

Guido Van Rossum developed Python in the year 1991. Python is a computer language used for developing web-based applications. Developers have used Python for several purposes. One of those is the use of arithmetic operators in Python. This range of operators is used for performing mathematical operations on the platform.

Overview

As mentioned earlier, Python can also be used for performing mathematical operations. 

A group of mathematical procedures employed in calculations are referred to as arithmetic operations. Addition, subtraction, multiplication, and division are a few examples of different arithmetic operators in Python available for use.

What are Arithmetic Operators?

Arithmetic Operators are special symbols that are used to carry out calculations.

Imagine you went shopping. You buy clothes, groceries, and many things that you would need for your daily needs. After you come home, you start to look at the bill to know how much you spent and how much discount you earned on the clothes that were on sale.

You use a calculator to add, subtract, multiply, and divide. Yes, you are right; the calculator came to your help. It is not easy to calculate such big numbers without some help. 

Similarly, there are Python arithmetic operators used to calculate values.

Different types of operators in Python are used to compute values, such as:

  • Arithmetic operators

  • Logical operators

  • Assignment operators

  • Bitwise operators, and so on.

Let’s dive deep into the topic of Python math operators.

Let us first understand which different types of arithmetic operators are available in Python

Following are the different types of math operators available for math operations in Python:

  • Addition

  • Subtraction

  • Multiplication

  • Division

  • Modulus

  • Floor Division

  • Exponentiation

Let us understand each of these Python arithmetic operators in detail:

Addition 

Similar to any addition on a simple calculator or an Excel spreadsheet, the addition operator in Python is ‘+.’ It is used to bring out the sum of two or more values.

The code output for the addition operator:

The code:

Var1=20
Var2=10
Sum=var1+var2
Print (“The sum of”, var1, “and”, var2, “is:”, sum)

The code Output:

The sum of 20 and 10 is: 30

Subtraction

The subtraction arithmetic operator in Python is “- .“ This particular operator is to subtract a specific value from a given value.

Let’s look at its code implementation:

Var1=30
Var2=20
Diff=var1-var2
Print (“The difference between”, var1, “and”, var2, “is:”, diff)

The code Output:

The difference between 30 and 20 is: 10

Multiplication

The multiplication operator in Python is “*.”

The multiplication operator is used for getting the product of two or more values.

Let’s look at the multiplication code implementation in Python:

Var1=30
Var2=20
Product=var1*var2
Print (“The product of”, var1, “and”, var2, “is:”, product)

The output of the code:

The product of 30 and 20 is: 600

Division

The Division operator in Python is “/.”

The division operator is used for dividing two or more values. It is used for finding the quotient of a dividing value.

Let’s look at the code implementation for the Division operator:

Var1=50
Var2=30
Quotient=var1/var2
Print (“The quotient of”, var1, “and”, var2, “is:”, quotient)

The code Output:

The quotient of 50 and 30 is: 1.66666666666666667

Modulus

The modulus operator in Python is “%.”

The modulus operator is used to find the remainder when the first operand is divided by the second operand.

Let’s look at the code implementation for the Modulus operator:

Var1=50
Var2=30
Rem=var1&var2
Print (“The remainder of”, var1, “and”, var2, “is:”, rem)

The code Output:

The remainder of 50 and 30 is: 20

Floor Division

The Floor Division operator in Python is “//”

The floor division operator is used to find the floor of the quotient when the first operand is divided by the second.

Let’s look at the code implementation for the Floor - Division operator:

Var1=10
Var2=7
floordiv=var1//var2
Print (“The floor division between”, var1, “and”, car2, “is:”, floordiv)

The code Output:

The floor division between 10 and 7 is: 1

Exponentiation

The exponentiation operator used on Python is “**.”

It is used to raise the power of the first operand to that of the second.

Let us look at the code implementation of the exponentiation operator:

The code implementation:

Var1=10
Var2=7
Exp=var1**var2
Print (“The exponentiation of,” var1, “and,” var2, “is:,” exp)

The code Output:

The exponentiation of 10 and 7 is: 10000000

Operator

Denoted By

Associativity

Example


Exponentiation Operator in Python

**

Right to left

3**2=9

Ignores the decimal value if present

Modulus

%

Left to right

X%y

Gives remainder from dividing a from b

Multiplication

*

Left to right

X*y

Multiplies x with y to get a product

Division

/

Left to right

x/y

Divides x and y to come out with a quotient

Floor Division

//

Left to right

15/2=7

Give the result of 3 to the power of 2

Addition 

+

Left to right

x+y

Adds x with y

Subtractors

-

Left to right

x-y

Subtracts y from x

Along with the arithmetic operators. There are some other Python arithmetic operators, which are listed below:

Assignment operators in Python

Represented by an “=” sign, the assignment operator is used to assign a specific value to a variable.

For example, x = 15.

Here, ‘x=15’ means the value of x is now 15.

Logical operators in Python

These operators work on logic that is on conditional statements. The below table gives an idea about the various logical operators in Python.

Comparison operators in Python

As the name indicates, comparison operators in Python are used to compares the different values.

Bitwise operators in Python

Bitwise operators are performed on integers. The number or integer is first converted into binary units, and then the calculations are performed on each bit differently.

types of biwise operators in python

Precedence of Arithmetic Operators in Python

Certain operators in Python take precedence over the others which means they are first in order of the operation and then the other operators are used.

The following table lists the operators in Python in order of their precedence – from highest to lowest.

PPT - 4. Python - Basic Operators PowerPoint Presentation, free download -  ID:3674261

Python Arithmetic Operators’ Example:

Let’s try to implement the arithmetic operators in Python

# Taking input  

num1 = input('Enter first number: ')  
num2 = input('Enter second number: ')  

# Addition

sum = float(num1) + float(num2)  

# Subtraction  

min = float(num1) - float(num2)  

# Multiplication  

mul = float(num1) * float(num2)  

#Division  

div = float(num1) / float(num2)  

#Modulus

mod = float(num1) % float(num2)  

#Exponentiation

exp = float(num1) ** float(num2)  

# Floor Division

floordiv = float(num1) // float(num2)  


print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))  
print('The subtraction of {0} and {1} is {2}'.format(num1, num2, min))   
print('The multiplication of {0} and {1} is {2}'.format(num1, num2, mul))   
print('The division of {0} and {1} is {2}'.format(num1, num2, div))
print('The modulus of {0} and {1} is {2}'.format(num1, num2, mod))   
print('The exponentiation of {0} and {1} is {2}'.format(num1, num2, exp))   
print('The floor division between {0} and {1} is {2}'.format(num1, num2,floordiv))  

The Code Output:

Enter first number: 60
Enter second number: 30
The sum of 60 and 30 is 90.0
The subtraction of 60 and 30 is 30.0
The multiplication of 60 and 30 is 1800.0
The division between 60 and 30 is 2.0
The modulus of 60 and 30 is 0.0
The exponentiation of 60 and 30 is 2.2107391972073335e+53
The floor division between 60 and 30 is 2.0

Conclusion

Python has been used across several industries and companies. So, developers and programmers must understand Python and its Operators in depth. 

The computer programming language Python's relevance has been increasing significantly due to the growth in artificial intelligence and information technology. The platform is highly flexible and scalable. Due to its easy portability, Python is becoming one of the popular choices among programmers and developers worldwide. 

Arithmetic operations are the basics of Python's programming language. So, to understand it better, it is important to understand and learn these arithmetic operations.

FAQs

1. What is Python?

Python is a programming language used for developing web-based applications. It was developed by Guido Van Rossum in the year 1991.

2. What are arithmetic Operators in Python?

Arithmetic operators are special symbols used for calculations in Python.

3. What are the different arithmetic operators available in Python?

Addition, Subtraction, Multiplication, Division, Modulus, and Floor Division are some of the arithmetic operators used in Python.

4. What does the addition operator do?

The addition operator is denoted by “+” in Python. It is used to add two or more values.

5. What does the subtraction operator do?

The subtraction operator is denoted by “-” in Python. It is used to subtract a specific value from a specific value.

6. What does the Modulus operator do?

The modulus operator in Python is denoted by “%.” It is used to find the remainder when the first operand is divided by the second operand.

7. What does the Division operator do?

The Division operator in Python is denoted by “/”. It is used for dividing two or more values. It is used for finding the quotient of a dividing value.

8. What does the Multiplication operator do?

The multiplication operator in Python is denoted by “*.” The multiplication operator is used for getting the product of two or more values.

9. What is the average salary of a Python Developer in India?

The average salary of a Python Developer in India ranges between 5.0 to 6.0 LPA.

10. Is Python good for beginners?

Yes, Python has been developed with simple codes and syntax. It’s an easy language, and programming makes it quite useful for beginners. 

11. Can Python be used to run the code on Windows?

Yes, Python’s flexibility and portability can be easily used to run the codes on platforms such as Mac OS X, Windows, Unix, and Linux.

Leave a Reply

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