Python Basic Arithmetic Operators – Comprehensive Guide with Examples
Arithmetic operators are fundamental in any programming language, including Python. They allow you to perform mathematical operations such as addition, subtraction, multiplication, division, and more. Understanding how these operators work and their subtle differences is key to writing effective and error-free code.
1. Addition (+
)
The addition operator +
adds two numbers together.
Example 1:
a = 10
b = 5
result = a + b
print(result) # Output: 15
Explanation: Here, a
and b
are added, resulting in 15.
2. Subtraction (-
)
The subtraction operator -
subtracts the right-hand number from the left-hand number.
Example 2:
a = 10
b = 5
result = a - b
print(result) # Output: 5
Explanation: The value of b
(5) is subtracted from a
(10), giving 5.
3. Multiplication (*
)
The multiplication operator *
multiplies two numbers.
Example 3:
a = 10
b = 5
result = a * b
print(result) # Output: 50
Explanation: The numbers 10 and 5 are multiplied, giving 50.
4. Division (/
)
The division operator /
divides the left-hand number by the right-hand number and returns a floating-point result.
Example 4:
a = 10
b = 3
result = a / b
print(result) # Output: 3.3333333333333335
Important Note: Division always returns a float
, even if the division is exact.
5. Floor Division (//
)
The floor division operator //
divides and returns the largest whole number less than or equal to the result (also called the quotient without the remainder).
Example 5:
a = 10
b = 3
result = a // b
print(result) # Output: 3
Explanation: 10 divided by 3 is 3.333..., but floor division truncates the decimal part and returns 3.
⚠️ Caution:
Floor division returns an integer result if both operands are integers, but if one operand is a float, the result will be a float rounded down.
a = 10.0
b = 3
result = a // b
print(result) # Output: 3.0
6. Modulus (%
)
The modulus operator %
returns the remainder after division of the left operand by the right operand.
Example 6:
a = 10
b = 3
result = a % b
print(result) # Output: 1
Explanation: When 10 is divided by 3, the quotient is 3 and the remainder is 1, which is what the modulus operator returns.
Use Case:
The modulus operator is often used to determine if a number is even or odd:
number = 7
if number % 2 == 0:
print("Even")
else:
print("Odd") # Output: Odd
7. Exponentiation (**
)
The exponentiation operator **
raises the left operand to the power of the right operand.
Example 7:
a = 2
b = 3
result = a ** b
print(result) # Output: 8
Explanation: 2 raised to the power of 3 means 2 × 2 × 2 = 8.
Summary Table
Operator | Description | Example | Output |
---|---|---|---|
+ | Addition | 10 + 5 | 15 |
- | Subtraction | 10 - 5 | 5 |
* | Multiplication | 10 * 5 | 50 |
/ | Division (float result) | 10 / 3 | 3.3333... |
// | Floor Division | 10 // 3 | 3 |
% | Modulus (Remainder) | 10 % 3 | 1 |
** | Exponentiation (Power) | 2 ** 3 | 8 |
Important Tips & Notes
- Division by zero: Be cautious! Dividing by zero causes a
ZeroDivisionError
in Python. Always validate divisor before division. - Order of operations: Python follows standard arithmetic precedence (PEMDAS/BODMAS). Use parentheses
()
to make order explicit. - Data types matter: Operators behave differently on integers, floats, and other numeric types. For example,
/
always returns float, floor division returns integer (or float if one operand is float). - Modulus with negative numbers: The sign of the result follows the divisor (right operand). This is important in some calculations.
Practice Example: Calculate the area and perimeter of a rectangle
length = 7
width = 4
# Area = length * width
area = length * width
# Perimeter = 2 * (length + width)
perimeter = 2 * (length + width)
print("Area:", area) # Output: Area: 28
print("Perimeter:", perimeter) # Output: Perimeter: 22
Explanation: Here, we use multiplication and addition operators to calculate the area and perimeter of a rectangle.
Conclusion
Mastering Python's arithmetic operators is essential for all kinds of programming tasks, from simple calculations to complex algorithms. Remember to understand the subtle differences between division types, modulus, and exponentiation to write efficient and bug-free code. Practice with examples and experiment with different numeric types to gain confidence.
📚 Related Topics:
- ➤ Python Arithmetic Operators
- ➤ Basic String Functions
- ➤ Advanced String Functions
- ➤ Basic List Functions
- ➤ Advanced List Functions : Part-1
- ➤ Advanced List Functions : Part-2
- ➤ Basic Tuple Functions
- ➤ Advanced Tuple Functions
- ➤ Basic Dictionary Functions
- ➤ Advanced Dictionary Functions
- ➤ Conditional Statements : if-elif-else
- ➤ Python 'for' Loop
- ➤ Python 'while' Loop
- ➤ Difference between 'for' loop and 'while' loop
- ➤ Introducing Python Functions
📌 Bookmark this blog or follow for updates!
👍 Liked this post? Share it with friends or leave a comment below!