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

Python Arithmetic Operators

Last Updated : 01 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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

Python Arithmetic Operators – FAQs

What are the 7 arithmetic operators in Python?

Python has seven basic arithmetic operators used to perform mathematical operations:

  1. Addition (+): Adds two numbers.
result = 3 + 2  # Output: 5
  1. Subtraction (-): Subtracts one number from another.
result = 5 - 2  # Output: 3
  1. Multiplication (*): Multiplies two numbers.
result = 3 * 2  # Output: 6
  1. Division (/): Divides one number by another, returning a float.
result = 6 / 2  # Output: 3.0
  1. Floor Division (//): Divides one number by another, returning an integer.
result = 7 // 2  # Output: 3
  1. Modulus (%): Returns the remainder of a division.
result = 7 % 2  # Output: 1
  1. Exponentiation (**): Raises one number to the power of another.
result = 3 ** 2  # Output: 9

What are the 6 relational operators in Python?

Python has six relational operators used to compare values:

  1. Equal to (==): Checks if two values are equal.
  2. Not equal to (!=): Checks if two values are not equal.
  3. Greater than (>): Checks if the left value is greater than the right value.
  4. Less than (<): Checks if the left value is less than the right value.
  5. Greater than or equal to (>=): Checks if the left value is greater than or equal to the right value.
  6. Less than or equal to (<=): Checks if the left value is less than or equal to the right value.

What is the rule for arithmetic operators in Python?

Arithmetic operators in Python follow the standard mathematical order of operations, also known as BODMAS/BIDMAS rules:

  1. Brackets
  2. Orders (exponentiation, **)
  3. Division and Multiplication (/, *, //, %)
  4. Addition and Subtraction (+, -)
result = 3 + 2 * 2 ** 2 / 2 - 1  # Output: 6.0
# Explanation: 3 + ((2 * (2 ** 2)) / 2) - 1
# 3 + ((2 * 4) / 2) - 1
# 3 + (8 / 2) - 1
# 3 + 4 - 1
# 7 - 1
# 6.0

What is the === in Python?

Python does not have a === operator. In many other programming languages like JavaScript, === is a strict equality operator that checks both the value and the type. In Python, you use == to check for equality and is to check for identity (if two references point to the same object).

What is the arithmetic operator?

An arithmetic operator is a symbol that performs a mathematical operation on one or more operands. The basic arithmetic operators in Python include addition (+), subtraction (-), multiplication (*), division (/), floor division (//), modulus (%), and exponentiation (**). These operators are used to perform calculations and return a numeric result.



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
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
How To Do Math in Python 3 with Operators?
Python3 provides us data types like integer and float along with various operators to perform mathematical calculations for graph plotting, machine learning algorithms, Statistical, data analytical purposes. An operator is a symbol that performs specific operations, which is very useful if one is frequently dealing with numbers. Operator precedence
5 min read
Precedence and Associativity of Operators in Python
In Python, operators have different levels of precedence, which determine the order in which they are evaluated. When multiple operators are present in an expression, the ones with higher precedence are evaluated first. In the case of operators with the same precedence, their associativity comes into play, determining the order of evaluation. Opera
4 min read
Augmented Assignment Operators in Python
An assignment operator is an operator that is used to assign some value to a variable. Like normally in Python, we write "a = 5" to assign value 5 to variable 'a'. Augmented assignment operators have a special role to play in Python programming. It basically combines the functioning of the arithmetic or bitwise operator with the assignment operator
4 min read
Inplace Operators in Python | Set 1 (iadd(), isub(), iconcat()...)
Python in its definition provides methods to perform inplace operations, i.e. doing assignments and computations in a single statement using an operator module. Example x += y is equivalent to x = operator.iadd(x, y) Inplace Operators in PythonBelow are some of the important Inplace operators in Python: iadd()isub()iconcat()imul()itruediv()imod()ia
3 min read
Inplace Operators in Python | Set 2 (ixor(), iand(), ipow(),…)
Inplace Operators in Python | Set 1(iadd(), isub(), iconcat()…) More functions are discussed in this articles. 1. ixor() :- This function is used to assign and xor the current value. This operation does "a^ = b" operation. Assigning is not performed in case of immutable containers, such as strings, numbers and tuples. 2. ipow() :- This function is
3 min read
Inplace vs Standard Operators in Python
Inplace Operators - Set 1, Set 2Normal operators do the simple assigning job. On other hand, Inplace operators behave similarly to normal operators except that they act in a different manner in case of mutable and Immutable targets. The _add_ method, does simple addition, takes two arguments, returns the sum, and stores it in another variable witho
3 min read
Chaining comparison operators in Python
Checking more than two conditions is very common in Programming Languages. Let's say we want to check the below condition: a < b < c The most common syntax to do it is as follows: if a < b and b < c : {...} In Python, there is a better way to write this using the Comparison operator Chaining. The chaining of operators can be written as
5 min read
Increment += and Decrement -= Assignment Operators in Python
If you're familiar with Python, you would have known Increment and Decrement operators ( both pre and post) are not allowed in it. Python is designed to be consistent and readable. One common error by a novice programmer in languages with ++ and -- operators are mixing up the differences (both in precedence and in return value) between pre and post
3 min read
Python Operators
In Python programming, Operators in general are used to perform operations on values and variables. These are standard symbols used for logical and arithmetic operations. In this article, we will look into different types of Python operators.  OPERATORS: These are the special symbols. Eg- + , * , /, etc.OPERAND: It is the value on which the operato
12 min read
Comparison Operators in Python
The Python operators can be used with various data types, including numbers, strings, booleans, and more. In Python, comparison operators are used to compare the values of two operands (elements being compared). When comparing strings, the comparison is based on the alphabetical order of their characters (lexicographic order).Be cautious when compa
5 min read
Python Membership and Identity Operators
There is a large set of Python operators that can be used on different datatypes. Sometimes we need to know if a value belongs to a particular set. This can be done by using the Membership and Identity Operators. In this article, we will learn about Python Membership and Identity Operators. Table of Content Python Membership OperatorsPython IN Oper
7 min read
Python Bitwise Operators
Operators are used to perform operations on values and variables. These are the special symbols that carry out arithmetic and logical computations. The value the operator operates on is known as the Operand.  Python Bitwise OperatorsPython bitwise operators are used to perform bitwise calculations on integers. The integers are first converted into
6 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
6 min read
Python Logical Operators
Python logical operators are used to combine conditional statements, allowing you to perform operations based on multiple conditions. These Python operators, alongside arithmetic operators, are special symbols used to carry out computations on values and variables. In this article, we will discuss logical operators in Python definition and also loo
6 min read
Assignment Operators in Python
The Python Operators are used to perform operations on values and variables. These are the special symbols that carry out arithmetic, logical, and bitwise computations. The value the operator operates on is known as the Operand. Here, we will cover Different Assignment operators in Python. Operators Sign Description SyntaxAssignment Operator = Assi
11 min read
Why Are There No ++ and -- Operators in Python?
Python does not include the ++ and -- operators that are common in languages like C, C++, and Java. This design choice aligns with Python's focus on simplicity, clarity, and reducing potential confusion. In this article, we will see why Python does not include these operators and how you can achieve similar functionality using Pythonic alternatives
3 min read
Arithmetic Operations on Images using OpenCV | Set-1 (Addition and Subtraction)
Arithmetic Operations like Addition, Subtraction, and Bitwise Operations(AND, OR, NOT, XOR) can be applied to the input images. These operations can be helpful in enhancing the properties of the input images. The Image arithmetics are important for analyzing the input image properties. The operated images can be further used as an enhanced input im
3 min read
Arithmetic Operations on Images using OpenCV | Set-2 (Bitwise Operations on Binary Images)
Prerequisite: Arithmetic Operations on Images | Set-1Bitwise operations are used in image manipulation and used for extracting essential parts in the image. In this article, Bitwise operations used are : ANDORXORNOT Also, Bitwise operations helps in image masking. Image creation can be enabled with the help of these operations. These operations can
4 min read
NumPy - Arithmetic operations with array containing string elements
Numpy is a library of Python for array processing written in C and Python. Computations in numpy are much faster than that of traditional data structures in Python like lists, tuples, dictionaries etc. due to vectorized universal functions. Sometimes while dealing with data, we need to perform arithmetic operations but we are unable to do so becaus
2 min read
NumPy - Arithmetic Operations
NumPy is an open-source Python library for performing array computing (matrix operations). It is a wrapper around the library implemented in C and used for performing several trigonometric, algebraic, and statistical operations. NumPy objects can be easily converted to other types of objects like the Pandas data frame and the tensorflow tensor. Pyt
4 min read
Perform Arithmetic Operations in Pandas
Let us see how to perform basic arithmetic operations like addition, subtraction, multiplication, and division on 2 Pandas Series. For all the 4 operations we will follow the basic algorithm : Import the Pandas module. Create 2 Pandas Series objects. Perform the required arithmetic operation using the respective arithmetic operator between the 2 Se
2 min read
Practice Tags :