Python Arithmetic Operators
Last Updated :
09 Jan, 2025
Python operators are fundamental for performing mathematical calculations. Arithmetic operators are symbols used to perform mathematical operations on numerical values. Arithmetic operators include addition (+), subtraction (-), multiplication (*), division (/), and modulus (%).
Operator | Description | Syntax |
---|
+ | Addition: adds two operands | x + y |
---|
– | Subtraction: subtracts two operands | x – y |
---|
* | Multiplication: multiplies two operands | x * y |
---|
/ | Division (float): divides the first operand by the second | x / y |
---|
// | Division (floor): divides the first operand by the second | x // y |
---|
% | Modulus: returns the remainder when the first operand is divided by the second | x % y |
---|
** | Power: Returns first raised to power second | x ** y |
---|
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
In Python programming language Division Operators allow us 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.
There are two types of division operators:
- Float division
- Floor division
Float division
The quotient returned by this operator is always a float number, no matter if two numbers are integers. For example:
Example:
Python
print(5/5)
print(10/2)
print(-10/2)
print(20.0/2)
Integer division( Floor division)
The quotient returned by this operator is dependent on the argument being passed. If any of the numbers is float, it returns output in float. It is also known as Floor division because, if any number is negative, then the output will be floored. For example:
Example:
Python
print(10//3)
print (-5//2)
print (5.0//2)
print (-5.0//2)
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
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 |
Similar Reads
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- + , * , /,
6 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 int
4 min read
Python Arithmetic Operators
Python Arithmetic Operators Python operators are fundamental for performing mathematical calculations. Arithmetic operators are symbols used to perform mathematical operations on numerical values. Arithmetic operators include addition (+), subtraction (-), multiplication (*), division (/), and modulus (%). OperatorDescriptionS
4 min read
Difference between / vs. // operator in Python In Python, both / and // are used for division, but they behave quite differently. Let's dive into what they do and how they differ with simple examples./ Operator (True Division)The / operator performs true division.It always returns a floating-point number (even if the result is a whole number).It
2 min read
Python - Star or Asterisk operator ( * ) The asterisk (*) operator in Python is a versatile tool used in various contexts. It is commonly used for multiplication, unpacking iterables, defining variable-length arguments in functions, and more.Uses of the asterisk ( * ) operator in PythonMultiplicationIn Multiplication, we multiply two numbe
3 min read
What does the Double Star operator mean in Python? The ** (double star)operator in Python is used for exponentiation. It raises the number on the left to the power of the number on the right. For example:2 ** 3 returns 8 (since 2³ = 8)It is one of the Arithmetic Operator (Like +, -, *, **, /, //, %) in Python and is also known as Power Operator.Prec
2 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. There are two types of division operators: Float divisionInteger division( Floor division)When an in
5 min read
Modulo operator (%) in Python Modulo operator (%) in Python gives the remainder when one number is divided by another. Python allows both integers and floats as operands, unlike some other languages. It follows the Euclidean division rule, meaning the remainder always has the same sign as the divisor. It is used in finding even/
4 min read
Python Logical Operators