Lesson explanation
After this lesson, you will be able to:
/, //, and % in practical calculations.Python uses integers for whole numbers and floats for decimal values. The familiar operators +, -, *, and / handle basic arithmetic. Normal division with / returns a float, even when the result is mathematically whole.
// returns the floor-divided result, while % returns the remainder. They are commonly used together for pagination, batching, time conversion, and fixed-size groups.
** means exponentiation. Augmented assignments such as += and -= update an existing value without repeating its name.
Python follows operator precedence: powers first, then multiplication and division, then addition and subtraction. Use parentheses when they make the business rule clearer or change the required order.
Multiplication happens before addition and subtraction.
base_fee = 18
extra_users = 4
credit = 5
total = base_fee + extra_users * 3 - credit
print(total)Output:
25The same values answer three different questions.
records = 47
batch_size = 10
print(records / batch_size)
print(records // batch_size)
print(records % batch_size)Output:
4.7
4
7Augmented assignment changes the current value in place.
processed_rows = 120
processed_rows += 35
processed_rows -= 5
print(processed_rows)Output:
150** raises the left value to the power on the right.
side_length = 6
square_area = side_length ** 2
print(square_area)Output:
36| Goal | Python |
|---|---|
| Normal division | a / b |
| Full groups or units | a // b |
| Leftover amount | a % b |
| Raise to a power | a ** b |
| Increase an existing value | value += amount |
| Force calculation order | (a + b) * c |
/ to return an integer.// with the leftover result from %.^ for exponentiation; in Python it has a different meaning.