14

Sep# Arithmetic Operators in Python

## Python Arithmetic Operators

**Arithmetic Operators in Python **are one of those **operators in Python** that are used for performing certain mathematical operations like addition, subtraction, multiplication, and division on the numeric values.

In this **Python Tutorial**, we will delve deeper into different **types of Arithmetic Operators in Python, their Syntax, and Usage with proper Example Programs**. For more knowledge on other Python concepts, enroll in our **Python Certification Training** right now!

**Read More: Top 50 Python Interview Questions and Answers**

## Types of Arithmetic Operators in Python

Below are the 7 different types of arithmetic operators that are commonly used in Python with their syntax, usage and example in **Python Editor**:

### 1. Addition Operator (+)

The '+' operator is known as the Addition Operator. It is used to add two specified operands.

#### Syntax

`operand1 + operand2`

#### Example

```
# Addition
result_addition = 5 + 3
print("Addition:", result_addition)
```

#### Output

```
Addition: 8
```

### 2. Subtraction Operator (-)

The '-' operator is known as the Subtraction Operator. It is used to subtract the right operand from the left operand.

#### Syntax

`operand1 - operand2`

#### Example

```
# Subtraction
result_subtraction = 5 - 3
print("Subtraction:", result_subtraction)
```

#### Output

```
Subtraction: 2
```

**Read More: Python Developer Salary**

### 3. Multiplication Operator (*)

The '*' operator is known as Multiplication Operator. It is used to multiply two operands.

#### Syntax

`operand1 * operand2`

#### Example

```
# Multiplication
result_multiplication = 5 * 3
print("Multiplication:", result_multiplication)
```

#### Output

```
Multiplication: 15
```

### 4. Division Operator (/)

The '/' operator is known as the Division Operator. It is used to divide the left operand by the right operand. It will always return a floating point number.

#### Syntax

`operand1 / operand2`

#### Example

```
# Division
result_division = 5 / 3
print("Division:", result_division)
```

#### Output

```
Division: 1.6666666666666667
```

### 5. Floor Division Operator (//)

There is another kind of Division Operator which is known as Floor Division Operator represented by '//'. It is used to divide the left operand by the right operand. The difference is that it returns a rounded down quotient as a result.

#### Syntax

`operand1 // operand2`

#### Example

```
# Floor Division
result_floor_division = 5 // 3
print("Floor Division:", result_floor_division)
```

#### Output

```
Floor Division: 1
```

### 6. Modulus Operator (%)

The '%' operator is known as the Modulus Operator. It is used to divide the left operand by the right operand and return the remainder as the result.

#### Syntax

`operand1 % operand2`

#### Example

```
# Modulus
result_modulus = 5 % 3
print("Modulus:", result_modulus)
```

#### Output

```
Modulus: 2
```

**Read More: 10 Python Developer Skills you must know in 2024**

### 7. Exponentiation Operator (**)

The '**' operator is known as the Exponentiation Operator. It is used to raise the left operand to the power of the right operand.

#### Syntax

`operand1 ** operand2`

#### Example

```
# Exponentiation
result_exponentiation = 5 ** 3
print("Exponentiation:", result_exponentiation)
```

#### Output

```
Exponentiation: 125
```

## Compound Assignment Operators

Operator | Description | Syntax |

Addition Assignment (+=) | Used for adding the values of right operand with the left operand and assigning the result value to the left operand. | a += b |

Subtraction Assignment (-=) | Used for subtracting the value of the right operand from the left operand and assigning the result value to the left operand. | a -= b |

Multiplication Assignment (*=) | Used for multiplying the value of the left operand with the right operand and assigning the result value to the left operand. | a *= b |

Division Assignment (/=) | Used for dividing the value of the left operand by the value of the right operand and assigning the result value to the left operand. | a /= b |

Floor Division Assignment (//=) | Used for dividing the value of the left operand by the right operand and assigning the rounded down result value to the left operand. | a //= b |

Modulus Assignment (%=) | Used for computing the modulus of the left operand with the right operand and assigning the result value to the left operand. | a %= b |

Exponentiation Assignment (**=) | Used for raising the left operand to the power of the right operand and assigning the result value to the left operand. | a **= b |

#### Example illustrating compound assignment operators in python

```
# Compound Arithmetic Operators Example
# Variables
a = 10
b = 5
# Addition Assignment (+=)
a += 3
print("Addition Assignment (+=):", a)
# Subtraction Assignment (-=)
a -= 2
print("Subtraction Assignment (-=):", a)
# Multiplication Assignment (*=)
a *= 4
print("Multiplication Assignment (*=):", a)
# Division Assignment (/=)
a /= 2
print("Division Assignment (/=):", a)
# Floor Division Assignment (//=)
a //= 2
print("Floor Division Assignment (//=):", a)
# Modulus Assignment (%=)
a %= 3
print("Modulus Assignment (%=):", a)
# Exponentiation Assignment (**=)
a **= 2
print("Exponentiation Assignment (**=):", a)
# Reset variable a for demonstration
a = 10
# Printing variable a for clarity
print("\nResetting variable a for demonstration:", a)
# Adding variable b to a for demonstration
a += b
print("Addition Assignment (+=) with variable b:", a)
# Subtracting variable b from a for demonstration
a -= b
print("Subtraction Assignment (-=) with variable b:", a)
# Multiplying variable a by b for demonstration
a *= b
print("Multiplication Assignment (*=) with variable b:", a)
# Dividing variable a by b for demonstration
a /= b
print("Division Assignment (/=) with variable b:", a)
# Floor dividing variable a by b for demonstration
a //= b
print("Floor Division Assignment (//=) with variable b:", a)
# Taking modulus of variable a with b for demonstration
a %= b
print("Modulus Assignment (%=) with variable b:", a)
# Exponentiating variable a to the power of b for demonstration
a **= b
print("Exponentiation Assignment (**=) with variable b:", a)
```

#### Explanation:

**Python Compiler**, you can see how the different compound arithmetic operators perform different operations on variables 'a' & 'b' and what happens when the value of 'a' is reset. Their result will be as shown in the below output.

#### Output

```
Addition Assignment (+=): 13
Subtraction Assignment (-=): 11
Multiplication Assignment (*=): 44
Division Assignment (/=): 22.0
Floor Division Assignment (//=): 11.0
Modulus Assignment (%=): 2.0
Exponentiation Assignment (**=): 121.0
Resetting variable a for demonstration: 10
Addition Assignment (+=) with variable b: 15
Subtraction Assignment (-=) with variable b: 10
Multiplication Assignment (*=) with variable b: 50
Division Assignment (/=) with variable b: 10.0
Floor Division Assignment (//=) with variable b: 2.0
Modulus Assignment (%=) with variable b: 0.0
Exponentiation Assignment (**=) with variable b: 100000.0
```

## Order of Operations

**Python**, there is a set of rules that need to be followed in order to avoid ambiguity in our program. These set of rules are commonly termed as the acronyms PEMDAS or BODMAS. They tell in what sequence the mathematical expressions needs to be evaluated. On that basis the calculations are performed further.

### PEMDAS/BODMAS

**PEMDAS**and

**BODMAS**are same, they just have different terminologies. Below is the full form and description of PEMDAS/BODMAS in the correct sequence:

Operation | Description |

Parentheses (Brackets) | Operations within the parentheses are performed first. |

Exponents (Orders) | Then, the operations that involve exponents are performed. |

Multiplication and Division | Multiplication and Division are performed from left to right. |

Addition and Subtraction | Addition and Subtraction are performed from left to right. |

##### Summary

**Python Certification Course**and kick start you python journey with us.

## FAQs

### Q1. What are the arithmetic operators in Python?

### Q2. What are the 7 operators in Python?

### Q3. What is an arithmetic operator?

### Q4. What is the arithmetic expression?

### Q5. What is an arithmetic expression example?

- 2 + 4 -addition
- 4 * (5 - 2) -multiplication and subtraction within parentheses