Basic Arithmetic Operations (Addition, Subtraction, Multiplication, Division)

๐Ÿท๏ธ Numbers and Mathematical Operations / Arithmetic Operators

๐Ÿง  Context Introduction

Numbers are everywhere in programmingโ€”whether you're calculating resource limits, converting units, or summing up log entries. Arithmetic operations are the building blocks of all mathematical work in Python. This guide covers the four fundamental operations: addition, subtraction, multiplication, and division. By the end, you'll be comfortable performing basic calculations and understanding how Python handles numbers.


โš™๏ธ The Four Core Operators

Python uses standard symbols for arithmetic. Here's a quick reference:

  • Addition (+) โ€“ Adds two numbers together.
  • Subtraction (-) โ€“ Subtracts the second number from the first.
  • Multiplication (*) โ€“ Multiplies two numbers.
  • Division (/) โ€“ Divides the first number by the second, always returning a decimal (float).

Example in practice:

  • Addition: 5 + 3 results in 8
  • Subtraction: 10 - 4 results in 6
  • Multiplication: 6 * 7 results in 42
  • Division: 15 / 4 results in 3.75

๐Ÿ“Š Comparison Table: Operation Behavior

Operation Symbol Example Result Type Notes
Addition + 7 + 2 Integer or Float Works with both ints and floats
Subtraction - 9 - 5 Integer or Float Same as addition
Multiplication * 3 * 4 Integer or Float Same as addition
Division / 20 / 6 Always Float Even if numbers divide evenly, result is a decimal

๐Ÿ› ๏ธ Working with Different Number Types

Python automatically handles mixing integers and floats:

  • Integer + Integer โ†’ Integer (e.g., 4 + 5 โ†’ 9)
  • Float + Integer โ†’ Float (e.g., 4.0 + 5 โ†’ 9.0)
  • Integer / Integer โ†’ Always Float (e.g., 10 / 2 โ†’ 5.0)
  • Float / Integer โ†’ Float (e.g., 10.0 / 2 โ†’ 5.0)

Key point: Division always returns a float, even if the result is a whole number. This is different from some other languages.


๐Ÿ•ต๏ธ Common Pitfalls & Tips

  • Division by zero โ€“ Python will raise an error (ZeroDivisionError). Always ensure your divisor is not zero.
  • Order of operations โ€“ Python follows standard math rules: parentheses first, then exponents, then multiplication/division, then addition/subtraction. Use parentheses to make your intent clear.
  • Integer division vs. float division โ€“ If you need an integer result from division, you'll use a different operator (covered in a later guide). For now, remember that / always gives a decimal.

Example with parentheses: (10 + 2) * 3 gives 36, not 10 + (2 * 3) which gives 16.


โœ… Putting It All Together

Here's a simple workflow you can try yourself:

  1. Add two numbers: 15 + 27 โ†’ result is 42
  2. Subtract: 100 - 33 โ†’ result is 67
  3. Multiply: 8 * 9 โ†’ result is 72
  4. Divide: 45 / 6 โ†’ result is 7.5

Pro tip: Use variables to store intermediate results. For example, set a = 10, b = 3, then compute a + b, a - b, a * b, a / b. This makes your calculations reusable and easier to read.


๐Ÿ“š Summary

  • Addition (+) and subtraction (-) work as expected with integers and floats.
  • Multiplication (*) also works seamlessly across types.
  • Division (/) always returns a floatโ€”remember this to avoid surprises.
  • Order of operations matters; use parentheses to control calculation flow.
  • Avoid dividing by zero to prevent errors.

Master these four operations, and you'll have a solid foundation for all the mathematical work you'll do in Python. Next, you can explore more advanced topics like exponentiation, modulo, and integer division.


Arithmetic operators perform mathematical calculations on numeric values in Python.

โž• Example 1: Adding two integers together

This example shows how to add two whole numbers using the + operator.

result = 10 + 5
print(result)

๐Ÿ“ค Output: 15


โž– Example 2: Subtracting one number from another

This example demonstrates subtraction using the - operator with integer values.

result = 20 - 7
print(result)

๐Ÿ“ค Output: 13


โœ–๏ธ Example 3: Multiplying two numbers

This example shows multiplication using the * operator with integer values.

result = 6 * 4
print(result)

๐Ÿ“ค Output: 24


โž— Example 4: Dividing two numbers produces a float

This example demonstrates division using the / operator, which always returns a decimal number.

result = 15 / 4
print(result)

๐Ÿ“ค Output: 3.75


๐Ÿงฎ Example 5: Combining multiple arithmetic operations

This example shows how to combine addition, subtraction, multiplication, and division in a single expression using parentheses for clarity.

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

๐Ÿ“ค Output: 28.0


Comparison Table

Operator Name Example Output
+ Addition 5 + 3 8
- Subtraction 10 - 4 6
* Multiplication 7 * 6 42
/ Division 20 / 3 6.6667

๐Ÿง  Context Introduction

Numbers are everywhere in programmingโ€”whether you're calculating resource limits, converting units, or summing up log entries. Arithmetic operations are the building blocks of all mathematical work in Python. This guide covers the four fundamental operations: addition, subtraction, multiplication, and division. By the end, you'll be comfortable performing basic calculations and understanding how Python handles numbers.


โš™๏ธ The Four Core Operators

Python uses standard symbols for arithmetic. Here's a quick reference:

  • Addition (+) โ€“ Adds two numbers together.
  • Subtraction (-) โ€“ Subtracts the second number from the first.
  • Multiplication (*) โ€“ Multiplies two numbers.
  • Division (/) โ€“ Divides the first number by the second, always returning a decimal (float).

Example in practice:

  • Addition: 5 + 3 results in 8
  • Subtraction: 10 - 4 results in 6
  • Multiplication: 6 * 7 results in 42
  • Division: 15 / 4 results in 3.75

๐Ÿ“Š Comparison Table: Operation Behavior

Operation Symbol Example Result Type Notes
Addition + 7 + 2 Integer or Float Works with both ints and floats
Subtraction - 9 - 5 Integer or Float Same as addition
Multiplication * 3 * 4 Integer or Float Same as addition
Division / 20 / 6 Always Float Even if numbers divide evenly, result is a decimal

๐Ÿ› ๏ธ Working with Different Number Types

Python automatically handles mixing integers and floats:

  • Integer + Integer โ†’ Integer (e.g., 4 + 5 โ†’ 9)
  • Float + Integer โ†’ Float (e.g., 4.0 + 5 โ†’ 9.0)
  • Integer / Integer โ†’ Always Float (e.g., 10 / 2 โ†’ 5.0)
  • Float / Integer โ†’ Float (e.g., 10.0 / 2 โ†’ 5.0)

Key point: Division always returns a float, even if the result is a whole number. This is different from some other languages.


๐Ÿ•ต๏ธ Common Pitfalls & Tips

  • Division by zero โ€“ Python will raise an error (ZeroDivisionError). Always ensure your divisor is not zero.
  • Order of operations โ€“ Python follows standard math rules: parentheses first, then exponents, then multiplication/division, then addition/subtraction. Use parentheses to make your intent clear.
  • Integer division vs. float division โ€“ If you need an integer result from division, you'll use a different operator (covered in a later guide). For now, remember that / always gives a decimal.

Example with parentheses: (10 + 2) * 3 gives 36, not 10 + (2 * 3) which gives 16.


โœ… Putting It All Together

Here's a simple workflow you can try yourself:

  1. Add two numbers: 15 + 27 โ†’ result is 42
  2. Subtract: 100 - 33 โ†’ result is 67
  3. Multiply: 8 * 9 โ†’ result is 72
  4. Divide: 45 / 6 โ†’ result is 7.5

Pro tip: Use variables to store intermediate results. For example, set a = 10, b = 3, then compute a + b, a - b, a * b, a / b. This makes your calculations reusable and easier to read.


๐Ÿ“š Summary

  • Addition (+) and subtraction (-) work as expected with integers and floats.
  • Multiplication (*) also works seamlessly across types.
  • Division (/) always returns a floatโ€”remember this to avoid surprises.
  • Order of operations matters; use parentheses to control calculation flow.
  • Avoid dividing by zero to prevent errors.

Master these four operations, and you'll have a solid foundation for all the mathematical work you'll do in Python. Next, you can explore more advanced topics like exponentiation, modulo, and integer division.

Interactive Views

You are currently in ๐Ÿ“š All-in-One mode. Use the tabs at the top to switch to ๐Ÿ“– Theory Only or ๐Ÿ’ป Code Only views.

Arithmetic operators perform mathematical calculations on numeric values in Python.

โž• Example 1: Adding two integers together

This example shows how to add two whole numbers using the + operator.

result = 10 + 5
print(result)

๐Ÿ“ค Output: 15


โž– Example 2: Subtracting one number from another

This example demonstrates subtraction using the - operator with integer values.

result = 20 - 7
print(result)

๐Ÿ“ค Output: 13


โœ–๏ธ Example 3: Multiplying two numbers

This example shows multiplication using the * operator with integer values.

result = 6 * 4
print(result)

๐Ÿ“ค Output: 24


โž— Example 4: Dividing two numbers produces a float

This example demonstrates division using the / operator, which always returns a decimal number.

result = 15 / 4
print(result)

๐Ÿ“ค Output: 3.75


๐Ÿงฎ Example 5: Combining multiple arithmetic operations

This example shows how to combine addition, subtraction, multiplication, and division in a single expression using parentheses for clarity.

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

๐Ÿ“ค Output: 28.0


Comparison Table

Operator Name Example Output
+ Addition 5 + 3 8
- Subtraction 10 - 4 6
* Multiplication 7 * 6 42
/ Division 20 / 3 6.6667