Skip to content

Python Arithmetic Operators

Mastering Arithmetic Operators in Python

Arithmetic operators in Python are the workhorses of mathematical calculations, allowing you to perform a wide range of operations on numerical values. Whether you’re adding, subtracting, multiplying, dividing, or working with more advanced calculations, understanding and mastering these operators is fundamental to effective Python programming. In this comprehensive guide, we’ll explore the intricacies of arithmetic operators and how they can be applied in various scenarios.

The following table lists the arithmetic operators in Python:

OperatorDescriptionExample
++Adds two operands or unary plusx + yx + y
--Subtracts right operand from the left or unary minusx - yx - y
**Multiplies two operandsx * yx * y
//Divides left operand by right operandx / yx / y
////Floor division operatorx // yx // y
%%Modulus operatorx % yx % y
****Exponentiation operatorx ** yx ** y

Addition

++ (Addition) Operator

The addition operator (++) adds two operands. It can also be used as a unary operator to represent a positive value. The following example demonstrates how to use the addition operator in Python:

operators.py
# Addition operator
x = 10
y = 5
z = x + y
print(z)
operators.py
# Addition operator
x = 10
y = 5
z = x + y
print(z)

Output:

command
C:\Users\Your Name> python operators.py
15
command
C:\Users\Your Name> python operators.py
15

In the above example, we have used the addition operator to add two operands xx and yy and assign the result to the variable zz. The value of zz is then printed to the console.

Subtraction

-- (Subtraction) Operator

The subtraction operator (--) subtracts the right operand from the left operand. It can also be used as a unary operator to represent a negative value. The following example demonstrates how to use the subtraction operator in Python:

operators.py
# Subtraction operator
x = 10
y = 5
z = x - y
print(z)
operators.py
# Subtraction operator
x = 10
y = 5
z = x - y
print(z)

Output:

command
C:\Users\Your Name> python operators.py
5
command
C:\Users\Your Name> python operators.py
5

In the above example, we have used the subtraction operator to subtract the operand yy from the operand xx and assign the result to the variable zz. The value of zz is then printed to the console.

Multiplication

** (Multiplication) Operator

The multiplication operator (**) multiplies two operands. The following example demonstrates how to use the multiplication operator in Python:

operators.py
# Multiplication operator
x = 10
y = 5
z = x * y
print(z)
operators.py
# Multiplication operator
x = 10
y = 5
z = x * y
print(z)

Output:

command
C:\Users\Your Name> python operators.py
50
command
C:\Users\Your Name> python operators.py
50

In the above example, we have used the multiplication operator to multiply the operands xx and yy and assign the result to the variable zz. The value of zz is then printed to the console.

Division

// (Division) Operator

The division operator (//) divides the left operand by the right operand. The following example demonstrates how to use the division operator in Python:

operators.py
# Division operator
x = 10
y = 5
z = x / y
print(z)
operators.py
# Division operator
x = 10
y = 5
z = x / y
print(z)

Output:

command
C:\Users\Your Name> python operators.py
2.0
command
C:\Users\Your Name> python operators.py
2.0

In the above example, we have used the division operator to divide the operand xx by the operand yy and assign the result to the variable zz. The value of zz is then printed to the console.

//// (Floor Division) Operator

The floor division operator (////) divides the left operand by the right operand and returns the integer part of the result. The following example demonstrates how to use the floor division operator in Python:

operators.py
# Floor division operator
x = 10
y = 5
z = x // y
print(z)
operators.py
# Floor division operator
x = 10
y = 5
z = x // y
print(z)

Output:

command
C:\Users\Your Name> python operators.py
2
command
C:\Users\Your Name> python operators.py
2

In the above example, we have used the floor division operator to divide the operand xx by the operand yy and assign the result to the variable zz. The value of zz is then printed to the console.

Modulus

%% (Modulus) Operator

The modulus operator (%%) returns the remainder when the left operand is divided by the right operand. The following example demonstrates how to use the modulus operator in Python:

operators.py
# Modulus operator
x = 10
y = 3
z = x % y
print(z)
operators.py
# Modulus operator
x = 10
y = 3
z = x % y
print(z)

Output:

command
C:\Users\Your Name> python operators.py
1
command
C:\Users\Your Name> python operators.py
1

In the above example, we have used the modulus operator to divide the operand xx by the operand yy and assign the remainder to the variable zz. The value of zz is then printed to the console.

Exponentiation

**** (Exponentiation) Operator

The exponentiation operator (****) raises the left operand to the power of the right operand. The following example demonstrates how to use the exponentiation operator in Python:

operators.py
# Exponentiation operator
x = 10
y = 3
z = x ** y
print(z)
operators.py
# Exponentiation operator
x = 10
y = 3
z = x ** y
print(z)

Output:

command
C:\Users\Your Name> python operators.py
1000
command
C:\Users\Your Name> python operators.py
1000

In the above example, we have used the exponentiation operator to raise the operand xx to the power of the operand yy and assign the result to the variable zz. The value of zz is then printed to the console.

Combining Arithmetic Operators

You can combine multiple arithmetic operators in a single expression. The following example demonstrates how to combine multiple arithmetic operators in Python:

operators.py
# Combining arithmetic operators
x = 10
y = 5
z = x + y * 2
print(z)
operators.py
# Combining arithmetic operators
x = 10
y = 5
z = x + y * 2
print(z)

Output:

command
C:\Users\Your Name> python operators.py
20
command
C:\Users\Your Name> python operators.py
20

In the above example, we have combined the addition operator (++) and the multiplication operator (**) in a single expression. The multiplication operator is evaluated first, and then the addition operator is evaluated. The result of the expression is then assigned to the variable zz, which is then printed to the console.

Conclusion

Arithmetic operators in Python are versatile tools for handling numerical calculations. Whether you’re working with basic arithmetic, order of operations, integer division, exponentiation, or compound assignments, understanding how to leverage these operators is crucial for effective programming.

As you dive deeper into Python programming, experiment with arithmetic operators, explore their applications in real-world scenarios, and use them to solve mathematical problems in your projects. For more hands-on examples and in-depth tutorials, explore our resources on Python Central Hub!

Was this page helpful?

Let us know how we did