Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

This notebook introduces the basic arithmetic operations: addition (+), subtraction (−), multiplication (×), and division (÷). Each section provides explanations and Python code examples to help you understand and use these operations effectively.

1. Import Required Libraries

Let’s import any libraries we might need. For basic arithmetic, Python’s built-in operators are sufficient, but we’ll import NumPy for array operations.

# Import NumPy for array operations
import numpy as np

2. Addition Operation (+)

Addition is used to sum two or more numbers. In Python, use the + operator.

# Addition with integers
result_int = 2 + 3
print('2 + 3 =', result_int)

# Addition with floats
result_float = 2.5 + 3.1
print('2.5 + 3.1 =', result_float)

# Addition with NumPy arrays
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
result_arr = arr1 + arr2
print('Array addition:', result_arr)

3. Subtraction Operation (−)

Subtraction finds the difference between two numbers. Use the - operator in Python.

# Subtraction with integers
result_int = 5 - 2
print('5 - 2 =', result_int)

# Subtraction with floats
result_float = 5.5 - 2.2
print('5.5 - 2.2 =', result_float)

# Subtraction with NumPy arrays
arr1 = np.array([10, 20, 30])
arr2 = np.array([1, 2, 3])
result_arr = arr1 - arr2
print('Array subtraction:', result_arr)

4. Multiplication Operation (×)

Multiplication is performed using the * operator. It works with numbers and arrays.

# Multiplication with integers
result_int = 4 * 3
print('4 * 3 =', result_int)

# Multiplication with floats
result_float = 2.5 * 4.0
print('2.5 * 4.0 =', result_float)

# Multiplication with NumPy arrays
arr1 = np.array([2, 4, 6])
arr2 = np.array([3, 5, 7])
result_arr = arr1 * arr2
print('Array multiplication:', result_arr)

5. Division Operation (÷)

Division is performed using the / operator. Integer division uses //. Be careful with division by zero.

# Division with integers (float result)
result_div = 7 / 2
print('7 / 2 =', result_div)

# Integer division
result_int_div = 7 // 2
print('7 // 2 =', result_int_div)

# Division with floats
result_float = 7.5 / 2.5
print('7.5 / 2.5 =', result_float)

# Division with NumPy arrays
arr1 = np.array([8, 16, 32])
arr2 = np.array([2, 4, 8])
result_arr = arr1 / arr2
print('Array division:', result_arr)

# Division by zero example
try:
    print('1 / 0 =', 1 / 0)
except ZeroDivisionError as e:
    print('Division by zero error:', e)

6. Combining Multiple Operations

You can combine multiple arithmetic operations in a single expression. Python follows the standard order of operations (PEMDAS/BODMAS).

# Example combining operations
result = 2 + 3 * 4 - 8 / 2
print('2 + 3 * 4 - 8 / 2 =', result)

# Using parentheses to change precedence
result_paren = (2 + 3) * (4 - 8) / 2
print('(2 + 3) * (4 - 8) / 2 =', result_paren)

This notebook provides a foundation for understanding and using basic arithmetic operations in Python. You can now experiment with your own examples and explore more advanced mathematical functions!