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 + 3results in8 - Subtraction:
10 - 4results in6 - Multiplication:
6 * 7results in42 - Division:
15 / 4results in3.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:
- Add two numbers:
15 + 27โ result is42 - Subtract:
100 - 33โ result is67 - Multiply:
8 * 9โ result is72 - Divide:
45 / 6โ result is7.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 + 3results in8 - Subtraction:
10 - 4results in6 - Multiplication:
6 * 7results in42 - Division:
15 / 4results in3.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:
- Add two numbers:
15 + 27โ result is42 - Subtract:
100 - 33โ result is67 - Multiply:
8 * 9โ result is72 - Divide:
45 / 6โ result is7.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 |