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 | Solve given list containing numbers and arithmetic operators
Given a list containing numbers and arithmetic operators, the task is to solve the list. Example: Input: lst = [2, '+', 22, '+', 55, '+', 4] Output: 83 Input: lst = [12, '+', 8, '-', 5] Output: 15 Below are some ways to achieve the above tasks. Method #1: Using Iteration We can use iteration as the simplest approach to solve the list with importing
3 min read
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
Python program to compute arithmetic operation from String
Given a String with the multiplication of elements, convert to the summation of these multiplications. Input : test_str = '5x10, 9x10, 7x8' Output : 196 Explanation : 50 + 90 + 56 = 196. Input : test_str = '5x10, 9x10' Output : 140 Explanation : 50 + 90 = 140. Method 1 : Using map() + mul + sum() + split() The combination of the above functions can
4 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
Evaluate the Value of an Arithmetic Expression in Reverse Polish Notation in Python
Reverse Polish 'Notation is postfix notation which in terms of mathematical notion signifies operators following operands. Let's take a problem statement to implement RPN Problem Statement: The task is to find the value of the arithmetic expression present in the array using valid operators like +, -, *, /. Each operand may be an integer or another
3 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
Practice Tags :
three90RightbarBannerImg