Python supports a variety of arithmetic operators that allow you to perform mathematical calculations on values and variables effortlessly. Whether youโre building a simple calculator or complex data models, understanding these operators is fundamental. ๐งฎ
The basic arithmetic operations include addition, subtraction, multiplication, and division. In Python, these operations follow standard mathematical precedence rules (PEMDAS/BODMAS).
The General Structure of Expressions
Every mathematical expression in Python typically follows this general structure:
Operand Operator Operand [Operator Operand] โฆ
- Operand: Represents the data or variables being operated on (e.g.,
5,x). - Operator: A symbol that represents a specific action (e.g.,
+,*). - Example 1:
x + yโ Here,xandyare operands and+is the operator. - Example 2:
total = 10โ Here,=is the assignment operator (though not arithmetic, it follows the same structure).
In this article, you will explore all the Arithmetic operators provided by Python. ๐
Arithmetic Operators Overview
Basic mathematical calculations are performed on numeric values using these operators.
| Operator | Description | Example |
|---|---|---|
+ | Addition โ | x + y |
- | Subtraction โ | x - y |
-expr | Unary Minus (Negation) ๐ | -x |
* | Multiplication โ๏ธ | x * y |
/ | Division (True Division) โ | x / y |
// | Floor Division (Integer Division) ๐ข | x // y |
% | Modulus (Remainder) ๐ | x % y |
** | Exponentiation (Power) ๐ | x ** y |
Key Points to Remember
- Most of these are binary operators, meaning they work between two operands.
- Precedence:
**is evaluated first, followed by*,/,//, and%(left to right), and finally+and-(left to right). - Parentheses
(): Use them to override the default precedence and make your code more readable! โ
Addition (+) โ
The plus operator is used to add two numbers.
# Adding two integers ๐ฅ
result = 3 + 4
print(result) # Output: 7 ๐
Subtraction (-) โ
The minus operator subtracts the second operand from the first.
# Subtracting numbers ๐ค
result = 13 - 3
print(result) # Output: 10 โจ
Unary Minus (-) ๐
The unary minus negates the value of a single operand, turning a positive number into a negative one or vice versa.
# Negating a value ๐
x = 3
print(-x) # Output: -3 ๐
Multiplication (*) โ๏ธ
The asterisk is used to multiply two numbers.
# Multiplying values ๐ฆ
result = 7 * 4
print(result) # Output: 28 ๐
Division (/) โ
The forward slash performs True Division, which always returns a floating-point number (a decimal).
# Standard division ๐ฐ
result = 24 / 4
print(result) # Output: 6.0 โ๏ธ
Floor Division (//) ๐ข
Also known as Integer Division, it returns the largest possible integer less than or equal to the result. It effectively โchops offโ the decimal part.
# Floating point result for reference โ๏ธ
print(11 / 4) # Output: 2.75
# Floor division result ๐งฑ
print(11 // 4) # Output: 2
# Negative floor division (rounding towards floor) ๐
print(-61 / 4) # Output: -15.25
print(-61 // 4) # Output: -16
Note: For negative numbers, floor division rounds down (away from zero). So
-15.25becomes-16.
Modulus (%) ๐
The percentage symbol returns the remainder of the division. This is extremely useful for checking if a number is even or odd!
# Finding the remainder ๐งฉ
print(24 % 7) # Output: 3 (because 7*3=21, remainder 3)
# Works with floats too! ๐
print(24 % 3.5) # Output: 3.0
Exponentiation (**) ๐
The double asterisk operator calculates the power of a number (e.g., $x^y$).
# 2 to the power of 5 ๐
print(2 ** 5) # Output: 32
# Square root using exponentiation ๐ณ
print(25 ** 0.5) # Output: 5.0
# Right-to-left evaluation for nested powers โก
# This evaluates as 5 ** (2 ** 3) = 5 ** 8
print(5 ** 2 ** 3) # Output: 390625
๐ก Pro Tip: Pythonโs
**operator works similarly to the built-inpow(a, b)function.
Summary and Best Practices
- Always use parentheses when combining multiple operators to avoid logic errors.
- Use
//when you need an integer result and/when you need precision. - The
%operator is your best friend for cyclic logic (like wrapping around an array index).
Hope you enjoyed this guide to Python arithmetic operators! Keep practicing and happy coding! ๐๐