Now that you understand how to store numerical data using variables holding integers (like 10
, -5
, 0
) and floating-point numbers (like 3.14
, -0.5
, 2.0
), let's explore how to perform calculations with them. Python, much like a calculator, provides a set of arithmetic operators to manipulate these numbers. These operators are the fundamental tools for performing mathematical tasks in your programs.
Python supports the standard arithmetic operations you're familiar with:
+
): Adds two numbers together.
apples = 5
oranges = 10
total_fruit = apples + oranges
print(total_fruit) # Output: 15
price = 9.99
tax = 0.80
total_cost = price + tax
print(total_cost) # Output: 10.79
-
): Subtracts the second number from the first.
budget = 100
spent = 35.50
remaining = budget - spent
print(remaining) # Output: 64.5
*
): Multiplies two numbers.
width = 8
height = 12
area = width * height
print(area) # Output: 96
/
): Divides the first number by the second. It's important to note that standard division in Python always produces a floating-point number, even if the division results in a whole number.
total_distance = 100 # kilometers
time_hours = 2 # hours
speed = total_distance / time_hours
print(speed) # Output: 50.0
items = 10
people = 4
items_per_person = items / people
print(items_per_person) # Output: 2.5
Notice how 100 / 2
resulted in 50.0
, not just 50
. This consistent behavior helps avoid subtle bugs when you expect decimal results.Python provides two more specialized division operators that are particularly useful when working with integers:
//
): Performs division like the standard /
operator, but it discards the fractional part, rounding down to the nearest whole number (integer). This is also known as integer division.
cookies = 10
friends = 3
cookies_per_friend = cookies // friends
print(cookies_per_friend) # Output: 3 (Each friend gets 3 whole cookies)
value = 7
divisor = 2
result = value // divisor
print(result) # Output: 3 (7 divided by 2 is 3.5, rounded down is 3)
# Compare with standard division
print(10 / 3) # Output: 3.3333333333333335
print(10 // 3) # Output: 3
%
): This operator doesn't give you the result of the division, but rather the remainder left over after the division. It's often called the "remainder operator".
total_cookies = 10
friends = 3
leftover_cookies = total_cookies % friends
print(leftover_cookies) # Output: 1 (After giving 3 cookies each, 1 is left)
number = 17
divisor = 5
remainder = number % divisor
print(remainder) # Output: 2 (17 divided by 5 is 3 with a remainder of 2)
The modulo operator is frequently used to check if a number is even or odd. A number is even if number % 2
equals 0
, and odd otherwise.
num = 6
print(num % 2) # Output: 0 (So, 6 is even)
num = 7
print(num % 2) # Output: 1 (So, 7 is odd)
To raise a number to the power of another, you use the double asterisk (**
) operator.
**
): Raises the first number to the power of the second number. xy is written as x ** y
.
base = 2
exponent = 3
power_result = base ** exponent # Equivalent to 2 * 2 * 2
print(power_result) # Output: 8
side = 5
area_square = side ** 2 # Equivalent to 5 * 5 (5 squared)
print(area_square) # Output: 25
When an expression contains multiple operators, Python follows a standard order of operations, similar to mathematics (often remembered by acronyms like PEMDAS/BODMAS: Parentheses/Brackets, Exponents/Orders, Multiplication and Division, Addition and Subtraction).
()
: Operations inside parentheses are performed first.**
: Performed next.*
, Division /
, Floor Division //
, Modulo %
: These have the same precedence and are evaluated from left to right.+
, Subtraction -
: These have the same precedence and are evaluated last, from left to right.Consider this example:
result = 2 + 3 * 4
print(result) # Output: 14 (Multiplication is done before addition: 2 + 12)
If you want the addition to happen first, use parentheses:
result = (2 + 3) * 4
print(result) # Output: 20 (Parentheses force addition first: 5 * 4)
Another example:
calculation = 100 - 20 / 5 * 2 + 3**2
# Step-by-step evaluation:
# 1. Exponentiation: 3**2 becomes 9
# Expression: 100 - 20 / 5 * 2 + 9
# 2. Division/Multiplication (from left to right):
# 20 / 5 becomes 4.0
# Expression: 100 - 4.0 * 2 + 9
# 4.0 * 2 becomes 8.0
# Expression: 100 - 8.0 + 9
# 3. Addition/Subtraction (from left to right):
# 100 - 8.0 becomes 92.0
# Expression: 92.0 + 9
# 92.0 + 9 becomes 101.0
print(calculation) # Output: 101.0
Using parentheses can make complex expressions much clearer, even if they aren't strictly necessary according to precedence rules. When in doubt, use parentheses to ensure the calculation happens in the order you intend.
It's very common in programming to modify a variable based on its current value. For instance, increasing a counter or accumulating a total:
count = 0
count = count + 1 # Increment count by 1
print(count) # Output: 1
total_cost = 50
item_price = 15.50
total_cost = total_cost + item_price # Add item_price to total_cost
print(total_cost) # Output: 65.5
Python provides shorthand operators, called augmented assignment operators, to make this type of operation more concise:
count = 0
count += 1 # Equivalent to count = count + 1
print(count) # Output: 1
score = 100
score -= 10 # Equivalent to score = score - 10
print(score) # Output: 90
multiplier = 5
multiplier *= 2 # Equivalent to multiplier = multiplier * 2
print(multiplier) # Output: 10
total = 50.0
total /= 4 # Equivalent to total = total / 4
print(total) # Output: 12.5
These shorthand operators (+=
, -=
, *=
, /=
, //=
, %=
, **=
) exist for all the arithmetic operators and combine the operation with assignment. They are convenient and often make code easier to read once you are familiar with them.
You now have the tools to perform a wide range of calculations in Python. By combining variables, numeric data types, and these arithmetic operators, you can start writing programs that process numbers effectively. Remember to use parentheses to clarify or control the order of operations when needed.
© 2025 ApX Machine Learning