• Home
  • 3.8 Arithmetic Operations

3.8 Arithmetic Operations

View Categories

3.8 Arithmetic Operations

Arithmetic operations are used to perform mathematical calculations on numeric values.

Python provides several arithmetic operators that can be used with integers, floating-point numbers, and complex numbers.

The most commonly used arithmetic operators are:

Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
// Floor Division
% Modulus
** Exponentiation

Some of these operators will be discussed in greater detail later in this chapter.


Example 1: Addition #

main.py

a = 15
b = 10

print(a + b)

Output #

25

Explanation #

  • The + operator adds two numbers.
  • The result is the sum of the operands.

Example 2: Subtraction #

main.py

a = 25
b = 8

print(a - b)

Output #

17

Explanation #

  • The - operator subtracts the second operand from the first.
  • The result is the difference between the two numbers.

Example 3: Multiplication #

main.py

length = 8
width = 5

area = length * width

print(area)

Output #

40

Explanation #

  • The * operator multiplies two numbers.
  • Here, it calculates the area of a rectangle.

Example 4: Division #

main.py

a = 20
b = 4

print(a / b)

Output #

5.0

Explanation #

  • The / operator performs floating-point division.
  • Even when both operands are integers, the result is a float.

Example 5: Mixed Arithmetic #

main.py

a = 15
b = 2.5

print(a + b)
print(a * b)

Output #

17.5
37.5

Explanation #

  • Python allows arithmetic between integers and floating-point numbers.
  • The integer is automatically converted to a float.
  • The result is a floating-point number.

Example 6: Arithmetic Expressions #

main.py

result = 10 + 5 * 2

print(result)

Output #

20

Explanation #

  • Python follows operator precedence.
  • Multiplication is performed before addition.
  • Therefore:
    • 5 * 2 = 10
    • 10 + 10 = 20

Example 7: Using Parentheses #

main.py

result = (10 + 5) * 2

print(result)

Output #

30

Explanation #

  • Parentheses have the highest precedence.
  • The addition is performed first.
  • Then the multiplication is performed.

Example 8: Arithmetic with Variables #

main.py

price = 250
quantity = 4

total = price * quantity

print(total)

Output #

1000

Explanation #

  • Arithmetic operators work with variables as well as literals.
  • Python substitutes the values stored in the variables before performing the calculation.

Example 9: Arithmetic with Complex Numbers #

main.py

a = 3 + 2j
b = 1 + 4j

print(a + b)
print(a - b)

Output #

(4+6j)
(2-2j)

Explanation #

  • Arithmetic operators also work with complex numbers.
  • Python automatically performs the calculations on both the real and imaginary parts.

Example 10: Multiple Operations #

main.py

a = 12
b = 4
c = 3

result = a + b * c - 6

print(result)

Output #

18

Explanation #

  • Multiplication is performed first.
  • The expression becomes:
    • 12 + (4 ร— 3) - 6
    • 12 + 12 - 6
    • 18

Common Mistakes #

1. Assuming Division Returns an Integer #

Incorrect

print(20 / 5)

Incorrect Expectation #

4

Actual Output #

4.0

Reason

The / operator always returns a floating-point result.


2. Ignoring Operator Precedence #

Incorrect

result = 10 + 5 * 2

Incorrect Expectation

30

Actual Result

20

Reason

Multiplication has higher precedence than addition.

Correct

result = (10 + 5) * 2

3. Mixing Strings and Numbers #

Incorrect

print("10" + 5)

Output #

TypeError: can only concatenate str (not "int") to str

Reason

Arithmetic operators require compatible numeric data types.


4. Dividing by Zero #

Incorrect

print(10 / 0)

Output #

ZeroDivisionError: division by zero

Reason

Division by zero is not allowed in Python.


Best Practices #

  • Use descriptive variable names in arithmetic expressions.
  • Use parentheses to improve readability and avoid confusion.
  • Remember that / always returns a floating-point value.
  • Keep arithmetic expressions simple whenever possible.
  • Convert user input to numeric types before performing calculations.

Key Points to Remember #

  • Arithmetic operators perform mathematical calculations.
  • Python supports addition, subtraction, multiplication, division, floor division, modulus, and exponentiation.
  • Arithmetic operators work with integers, floats, and complex numbers.
  • Python follows operator precedence when evaluating expressions.
  • Parentheses can be used to control the order of evaluation.
  • The / operator always produces a floating-point result.
  • Division by zero raises a ZeroDivisionError.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *