How to use Python Arithmetic Operators

How to use Python Arithmetic Operators

4 mins read554 Views Comment
Vikram
Vikram Singh
Assistant Manager - Content
Updated on Oct 13, 2023 14:04 IST

Python arithmetic operators are used to perform mathematical operations between two numeric values. In this article, we will discuss how to use arithmetic operator in python with the help of examples.

2023_01_MicrosoftTeams-image-162.jpg

In any programming language, there are different types of operators that are used to perform mathematical, relational, and logical operations. 

Explore How to Find an Armstrong Number Using Python

In Python, there are mainly eight different types of operators that are generally used to perform different operations:

Must Check: All about Operators in Python

So, without further delay, letโ€™s learn how to use arithmetic operators in Python.

What is an Arithmetic Operator?

An Arithmetic Operator is a mathematical function that takes two input values, performs calculations, and produces a single result as an output. The most common arithmetic operators are addition (+), subtraction (-), multiplication (*), and division (/). In Python, there are eight different types of arithmetic operators.

Name Operator Example
Addition + x + y
Subtraction โ€“ x โ€“ y
Multiplication * x * y
Division / x / y
Modulus % x % y
Exponential ** x ** y
Floor Division // x // y

Must Read: Numbers in Python

Arithmetic operators in Python follow a basic order of precedence known as PEMDAS, which stands for

P: Parentheses

E: Exponential

M: Multiplication

D: Division

A: Addition

S: Subtraction

Note:

  • Parentheses will be evaluated first, then exponential, and subtraction at the end.
  • In case, two operators whose precedence is equal in the expression, then associativity is followed.
    • Associativity Rule: It states all the arithmetic operators (except exponential (**)) follow the left-to-right associativity.

Example: Solve (57 + 13 โ€“ 24/3 * 8)

Here, in this case, the precedence of / and * are equal, but also the 

precedence (/, *) > precedence (+, -) 

=> 24/3 * 8 = 8 * 8 = 64 (since both / and * have the same precedence so follow left to right)

Hence, the next expression becomes: 57 + 13 โ€“ 64

Now, again following left-to-right associativity, we get

70 โ€“ 64 = 6.

Hence, the solution of (57 + 13 โ€“ 24/3 * 8) is 6.

What is Programming What is Python
What is Data Science What is Machine Learning

Until now, we clearly understand arithmetic operators and how to use them to solve numerical problem. Now, we will see how to use them in Python.

Addition

The addition operator (+) in python is used to find the sum of two values.

Example: 55 + 45 = 100

Letโ€™s see how it is implemented in python.

Code

 
# Arithmetic Operator - Addition (+)
a = 55
b = 45
add = a + b
print ('The sum of a and b is:', add)
Copy code

Output

2023_01_image-164.jpg

Subtraction

The subtraction operator (-) in python is used to find the difference between two numeric values.

Example: 55 โ€“ 45 = 10

Letโ€™s see how it is implemented in python.

Code

 
# Arithmetic Operator - Subtraction (-)
a = 55
b = 45
subtract = a - b
print ('The subtraction of a from b is:', subtract)
Copy code

Output

2023_01_image-165.jpg
Recursion Function in Python count() Function in Python
len() Function in Python float() Function in Python
range() Function in Python lambda() Function in Python

Multiplication

The multiplication operator (*) in python is used to find the product between two numeric values.

Example: 10 * 5 = 50

Letโ€™s see how it is implemented in python.

Code

 
# Arithmetic Operator - Multiplication (*)
a = 10
b = 5
product = a * b
print('The multiplication of a and b is:', product)
Copy code

Output

2023_01_image-166.jpg

Division

The division operator (/) in python is used to find the quotient when the first value is divided by the second value.

Example: 30 / 7 = 4.28

Letโ€™s see how it is implemented in python.

Code

 
# Arithmetic Operator - Division (/)
a = 30
b = 7
division = a / b
print('The division of a and b is:', division)
Copy code

Output

2023_01_image-167.jpg

Modulus

The modulus operator (%) in python is similar to the Division operator. But dissimilar to the division operator it returns the remainder when the first value is divided by the second value.

Example: 10 % 3 = 1

Letโ€™s see how it is implemented in python.

Code

 
# Arithmetic Operator - Modulus (%)
a = 10
b = 3
mod = a % b
print('The modulus value of a and b is:', mod)
Copy code

Output

2023_01_image-168.jpg
Programming Online Courses and Certification Python Online Courses and Certifications
Data Science Online Courses and Certifications Machine Learning Online Courses and Certifications

Exponential

The exponential operator (**) in python is used to find the power of first value to the second value.

Answer: 2**3 = (2)3 = 2 *2*2 = 8

Letโ€™s see how it is implemented in python.

Code

 
# Arithmetic Operator - Exponential (**)
a = 2
b = 3
power = a**b
print('The exponential value of a and b is:', power)
Copy code

Output

2023_01_image-169.jpg

Floor Division

The // operator is applied to two numerical values first division will be performed and then the result obtained from the division will be rounded down to the nearest integer.

Example: 30 // 7 = 4

 
# Arithmetic Operator - Floor Division (//)
a = 30
b = 7
fd = 30//7
print('The floor division between a and b is:', fd)
Copy code

Output

2023_01_image-170.jpg

Must Read: Floor Division in Python

Recommended online courses

Best-suited Python courses for you

Learn Python with these high-rated online courses

Free
6 weeks
โ€“ / โ€“
3 months
โ€“ / โ€“
2 weeks
โ€“ / โ€“
16 weeks
โ‚น1.7 K
3 months
โ€“ / โ€“
โ€“ / โ€“
โ‚น4.24 K
2 weeks
โ‚น3 K
3 weeks
โ€“ / โ€“
4 months
About the Author
author-image
Vikram Singh
Assistant Manager - Content

Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio