Python Arithmetic Operators (original) (raw)
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 |