Python Tutorials | Python Arithmetic Operators Explained

Python Arithmetic Operators Explained

Arithmetic operators in Python are used to perform basic mathematical operations such as addition, subtraction, multiplication, and more. These operators work with numbers like integers and floats and return new values based on the operation performed.

Let’s explore each operator with practical examples to understand their purpose and behavior in different situations.

Example 1: Addition (+)

a = 10
b = 5
print(a + b)

The + operator adds two numbers. Output: 15

Example 2: Subtraction (-)

price = 100
discount = 20
print(price - discount)

The - operator subtracts the second value from the first. Output: 80

Example 3: Multiplication (*)

width = 7
height = 6
area = width * height
print(area)

The * operator multiplies two numbers. Output: 42

Example 4: Division (/)

a = 15
b = 2
print(a / b)

The / operator performs division and always returns a float. Output: 7.5

Example 5: Floor Division (//)

x = 17
y = 3
print(x // y)

The // operator performs floor division, which drops the decimal part. Output: 5

Example 6: Modulus (%)

dividend = 20
divisor = 6
print(dividend % divisor)

The % operator returns the remainder of a division. Output: 2

Example 7: Exponentiation (**)

base = 2
power = 5
print(base ** power)

The ** operator raises a number to the power of another. Output: 32

Example 8: Combined Arithmetic

result = (10 + 5) * 2 - 8 / 4
print(result)

Python follows standard order of operations (PEMDAS). Output: 28.0

Example 9: Using Arithmetic with Float and Int

x = 4.5
y = 2
print(x * y)
print(x // y)

Python supports arithmetic between floats and integers. Floor division result will still be a float if one operand is float. Output: 9.0 and 2.0

Example 10: Modulus with Negative Numbers

a = -10
b = 3
print(a % b)

In Python, modulus takes the sign of the divisor. Output: 2


Summary

  • + for addition
  • - for subtraction
  • * for multiplication
  • / for true division (returns float)
  • // for floor division (removes decimal part)
  • % for modulus (returns remainder)
  • ** for exponentiation (power)

Mastering arithmetic operators is essential for any kind of numeric computation in Python — whether you're building a calculator, a billing system, or a data processing tool.