How to use Python Arithmetic Operators

# How to use Python Arithmetic Operators

clickHere
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.

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

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.

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

P: Parentheses

E: Exponential

M: Multiplication

D: Division

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.

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.

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 = 55b = 45add = a + bprint ('The sum of a and b is:', add)Copy code`

Output

### 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 = 55b = 45subtract = a - bprint ('The subtraction of a from b is:', subtract)Copy code`

Output

### 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 = 10b = 5product = a * bprint('The multiplication of a and b is:', product)Copy code`

Output

### 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 = 30b = 7division = a / bprint('The division of a and b is:', division)Copy code`

Output

### 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 = 10b = 3mod = a % bprint('The modulus value of a and b is:', mod)Copy code`

Output

### 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 = 2b = 3power = a**bprint('The exponential value of a and b is:', power)Copy code`

Output

### 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 = 30b = 7fd = 30//7print('The floor division between a and b is:', fd)Copy code`

Output

Must Read: Floor Division in Python

## Conclusion

In this article, we have briefly discussed how to use arithmetic operator in python with the help of examples.

Hope, you will like the article.

Keep Learning!!

Keep Sharing!!

Top Trending Article

Interview Questions