The Wayback Machine - https://web.archive.org/web/20240712205253/https://www.geeksforgeeks.org/python-arithmetic-operators/
Open In App

Python Arithmetic Operators

Last Updated : 03 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The Python operators are fundamental for performing mathematical calculations in programming languages like Python, Java, C++, and many others. Arithmetic operators are symbols used to perform mathematical operations on numerical values. In most programming languages, arithmetic operators include addition (+), subtraction (-), multiplication (*), division (/), and modulus (%).

Arithmetic Operators in Python

There are 7 arithmetic operators in Python. The lists are given below:


Operator

Description

Syntax

Addition Operator

+

Addition: adds two operands

x + y

Subtraction Operator

–

Subtraction: subtracts two operands

x – y

Multiplication Operator

*

Multiplication: multiplies two operands

x * y

Division Operator

/

Division (float): divides the first operand by the second

x / y

Floor Division Operator

//

Division (floor): divides the first operand by the second

x // y

Modulus Operator

%

Modulus: returns the remainder when the first operand is divided by the second

x % y

Exponentiation Operator

**

Power (Exponent): Returns first raised to power second

x ** y

Precedence of Arithmetic Operators in Python

Let us see the precedence and associativity of Python Arithmetic operators.

Operator

Description

Associativity

**

Exponentiation Operator

right-to-left

%, *, /, //

Modulos, Multiplication, Division, and Floor Division

left-to-right

+, –

Addition and Subtraction operators

left-to-right

Addition Operator

In Python, + is the addition operator. It is used to add 2 values.

Python
val1 = 2
val2 = 3

# using the addition operator
res = val1 + val2
print(res)

Output: 

5

Subtraction Operator

In Python, – is the subtraction operator. It is used to subtract the second value from the first value.

Python
val1 = 2
val2 = 3

# using the subtraction operator
res = val1 - val2
print(res)

Output:

-1

Multiplication Operator

Python * operator is the multiplication operator. It is used to find the product of 2 values.

Python
val1 = 2
val2 = 3

# using the multiplication operator
res = val1 * val2
print(res)

Output : 

6

Division Operator 

Python // operator is the division operator. It is used to find the quotient when the first operand is divided by the second.

Python
val1 = 3
val2 = 2

# using the division operator
res = val1 / val2
print(res)

Output:

1.5

Floor Division Operator

The // in Python is used to conduct the floor division. It is used to find the floor of the quotient when the first operand is divided by the second.

Python
val1 = 3
val2 = 2

# using the floor division
res = val1 // val2
print(res)

Output:

1

Modulus Operator

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

Python
val1 = 3
val2 = 2

# using the modulus operator
res = val1 % val2
print(res)

Output:

1

Exponentiation Operator

In Python, ** is the exponentiation operator. It is used to raise the first operand to the power of the second. 

Python
val1 = 2
val2 = 3

# using the exponentiation operator
res = val1 ** val2
print(res)

Output:

8


Previous Article
Next Article

Similar Reads

Python | Arithmetic operations in excel file using openpyxl
Prerequisite: Reading & Writing to excel sheet using openpyxlOpenpyxl is a Python library using which one can perform multiple operations on excel files like reading, writing, arithmetic operations and plotting graphs. Let's see how to perform different arithmetic operations using openpyxl. =SUM(cell1:cell2) : Adds all the numbers in a range of
3 min read
Arithmetic operations using OpenCV | Python
Prerequisite: Arithmetic Operations on Images using OpenCV | Basics We can perform different Arithmetic operations on images e.g. Addition, Subtraction, etc. This is possible because images are actually stored as arrays (3 Dimensional for RGB images and 1 dimensional for the grayscale images). Importance of Arithmetic Operations on images: Image Bl
2 min read
How to Perform Arithmetic Across Columns of a MySQL Table Using Python?
Python is a dynamic language, and Python applications can be integrated with database servers. The module used to access a MySQL database from Python is MySQL Connector Python. PyMySQL, MySQLDB and mysqlclient are other Python modules to communicate with a MySQL database server in Python. However, we will use MySQL Connector Python in this article
5 min read
Division Operators in Python
Division Operators allow you to divide two numbers and return a quotient, i.e., the first number or number at the left is divided by the second number or number at the right and returns the quotient. Division Operators in PythonThere are two types of division operators: Float divisionInteger division( Floor division)When an integer is divided, the
5 min read
Python | Operators | Question 1
What is the output of the following code : C/C++ Code print 9//2 (A) 4.5 (B) 4.0 (C) 4 (D) Error Answer: (C)Explanation: The ‘//’ operator in Python returns the integer part of the floating number. Quiz of this QuestionPlease comment below if you find anything wrong in the above post
1 min read
Python | Operators | Question 2
Which function overloads the >> operator? (A) more() (B) gt() (C) ge() (D) None of the above Answer: (D) Explanation: rshift() overloads the >> operatorQuiz of this QuestionPlease comment below if you find anything wrong in the above post
1 min read
Python | Operators | Question 3
Which operator is overloaded by the or() function? (A) || (B) | (C) // (D) / Answer: (B) Explanation: or() function overloads the bitwise OR operatorQuiz of this QuestionPlease comment below if you find anything wrong in the above post
1 min read
Python | Operators | Question 4
What is the output of the following program : C/C++ Code i = 0 while i (A) 0 2 1 3 2 4 (B) 0 1 2 3 4 5 (C) Error (D) 1 0 2 4 3 5 Answer: (C)Explanation: There is no operator ++ in Python Quiz of this QuestionPlease comment below if you find anything wrong in the above post
1 min read
Merging and Updating Dictionary Operators in Python 3.9
Python 3.9 is still in development and scheduled to be released in October this year. On Feb 26, alpha 4 versions have been released by the development team. One of the latest features in Python 3.9 is the merge and update operators. There are various ways in which Dictionaries can be merged by the use of various functions and constructors in Pytho
3 min read
Python 3 - Logical Operators
Logical Operators are used to perform certain logical operations on values and variables. These are the special reserved keywords that carry out some logical computations. The value the operator operates on is known as Operand. In Python, they are used on conditional statements (either True or False), and as a result, they return boolean only (True
4 min read
Practice Tags :
three90RightbarBannerImg