Arithmetic Operators in Python

Arithmetic Operators in Python

10 May 2024
Beginner
369 Views
15 min read

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

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

In Python, the Compound Assignment Operators are the ones that combine the usage of both Arithmetic Operators and Assignment Operators in one single step. Below is the overview of the compound assignment operators with its syntax and usage:
OperatorDescriptionSyntax
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:

In the above example in 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

When we are using arithmetic operations in 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:
OperationDescription
Parentheses (Brackets)Operations within the parentheses are performed first.
Exponents (Orders)Then, the operations that involve exponents are performed.
Multiplication and DivisionMultiplication and Division are performed from left to right.
Addition and SubtractionAddition and Subtraction are performed from left to right.
Summary
Through this article, we learnt about different types of arithmetic operators with examples, compound assignment operators and the PEMDAS/BODMAS rule of calculation. It is important about these operators and their function if you're willing to start your python journey. And to achieve expertise in it, it is always better to look for professional guidance. For that, consider enrolling in our Python Certification Course and kick start you python journey with us.

FAQs

Q1. What are the arithmetic operators in Python?

The arithmetic operators in Python are used for performing various mathematical operations on numeric values such as addition, subtraction, etc.

Q2. What are the 7 operators in Python?

The 7 operators in Python include Arithmetic, Assignment, Comparison, Logical, Bitwise, Identity and Membership Operators.

Q3. What is an arithmetic operator?

An arithmetic operator is a symbol or function in programming that is basically used for performing various mathematical operations on numeric values.

Q4. What is the arithmetic expression?

An arithmetic expression is a representation of a mathematical calculation which include numbers, operators, and sometimes parentheses as well.

Q5. What is an arithmetic expression example?

An arithmetic expression example can be:
  • 2 + 4 -addition
  • 4 * (5 - 2) -multiplication and subtraction within parentheses
Share Article

Live Classes Schedule

Our learn-by-building-project method enables you to build practical/coding experience that sticks. 95% of our learners say they have confidence and remember more when they learn by building real world projects.
Azure Developer Certification Training Jun 29 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details
.NET Microservices Certification Training Jun 29 SAT, SUN
Filling Fast
05:30PM to 07:30PM (IST)
Get Details
React JS Certification Training | Best React Training Course Jun 30 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
ASP.NET Core Certification Training Jun 30 SAT, SUN
Filling Fast
10:00AM to 12:00PM (IST)
Get Details
Full-Stack .NET Developer Certification Training Jun 30 SAT, SUN
Filling Fast
11:00AM to 01:00PM (IST)
Get Details
Advanced Full-Stack .NET Developer Certification Training Jun 30 SAT, SUN
Filling Fast
10:00AM to 12:00PM (IST)
Get Details
Generative AI For Software Developers Jul 14 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details

Can't find convenient schedule? Let us know

About Author
Sakshi Dhameja (Author and Mentor)

She is passionate about different technologies like JavaScript, React, HTML, CSS, Node.js etc. and likes to share knowledge with the developer community. She holds strong learning skills in keeping herself updated with the changing technologies in her area as well as other technologies like Core Java, Python and Cloud.

Accept cookies & close this