Kahibaro
Discord Login Register

Arithmetic operations

Arithmetic with Numbers in Python

Arithmetic operations are how you make Python do math. In this chapter we focus only on the basic operations you can perform with numbers.

We’ll use two main numeric types here:

Python can mix them in calculations, but the result may change type (for example, division often gives a float).


The Basic Arithmetic Operators

Python supports the usual mathematical operations:

Let’s look at each one with simple examples.

# Addition
print(2 + 3)       # 5
# Subtraction
print(10 - 4)      # 6
# Multiplication
print(3 * 7)       # 21
# Division (true division)
print(10 / 4)      # 2.5

Notice that / always does “true division” and usually returns a float, even if the division is exact:

print(8 / 2)       # 4.0 (a float, not 4)

Floor Division: `//`

Floor division divides and then rounds down to the nearest whole number (towards negative infinity).

print(10 // 4)     # 2
print(11 // 4)     # 2
print(12 // 4)     # 3

With negative numbers, “round down” means going to the smaller number:

print(-10 / 4)     # -2.5  (normal division)
print(-10 // 4)    # -3    (rounded down from -2.5)

Use // when you want an integer result, such as splitting items into full groups.


Remainder (Modulo): `%`

The modulo operator % returns the remainder after division.

If you divide $a$ by $b$:

$$
a = b \times q + r
$$

then $a % b gives $r$, the remainder.

Examples:

print(10 % 3)      # 1  (3*3 = 9, remainder 1)
print(10 % 4)      # 2  (4*2 = 8, remainder 2)
print(12 % 6)      # 0  (no remainder)

Modulo is very useful for:

number = 7
print(number % 2)         # 1
print(number % 2 == 0)    # False (7 is not even)

With negative numbers, Python’s modulo result is always the same sign as the divisor:

print(-10 % 3)     # 2  (because -10 = 3 * -4 + 2)
print(10 % -3)     # -2

You don’t need to memorize the rule now, just be aware that negative numbers with % can be surprising.


Exponentiation (Powers): `**`

Use ** to raise a number to a power:

Examples:

print(2 ** 3)      # 8   (2 * 2 * 2)
print(5 ** 2)      # 25  (5 squared)
print(9 ** 0.5)    # 3.0 (square root of 9)

You can also use ** with negative exponents or floats:

print(2 ** -1)     # 0.5  (1 / 2)
print(4 ** 1.5)    # 8.0  (4^(3/2) = sqrt(4^3))

For very advanced math, you’ll usually use the math library, but ** covers many basic needs.


Operator Precedence (Order of Operations)

Python follows standard math rules (PEMDAS):

  1. Parentheses
  2. Exponents
  3. Multiplication, Division, Floor division, Modulo
  4. Addition, Subtraction

In practice:

print(2 + 3 * 4)        # 14, not 20
# Because 3 * 4 happens first: 2 + 12
print((2 + 3) * 4)      # 20
# Parentheses force 2 + 3 to happen first

When in doubt, use parentheses to make your intention clear:

result = (10 - 2) / (4 + 4)
print(result)           # 1.0

Mixing `int` and `float` in Calculations

When you mix integers and floats, Python usually converts the result to a float:

print(3 + 2.0)     # 5.0
print(5 * 0.5)     # 2.5
print(8 / 2)       # 4.0

Some operations keep results as int when possible:

print(7 // 2)      # 3  (int)
print(7.0 // 2)    # 3.0 (float, but same numeric value)
print(7 % 2)       # 1

Be aware of this when you expect whole numbers. You can convert types explicitly when needed in other chapters.


Using Arithmetic with Variables

You can store numbers in variables and use them in calculations.

width = 5
height = 3
area = width * height
print(area)        # 15

Variables can also be updated based on their current value:

counter = 0
counter = counter + 1
print(counter)     # 1
counter = counter * 2
print(counter)     # 2

This pattern is very common in loops and other calculations.


Integer Division vs. Real Division

A frequent beginner confusion is between / and //.

Compare:

print(7 / 3)       # 2.3333333333333335
print(7 // 3)      # 2

Use:

Simple Practice Ideas

You can try small arithmetic tasks like:

Experiment in the Python interactive mode:

>>> 35 // 60       # full hours in 35 minutes
>>> 35 % 60        # leftover minutes
>>>
>>> 5 ** 3
>>> (2 + 3) * 4

As you progress, these operations will become the building blocks for more complex programs.

Views: 19

Comments

Please login to add a comment.

Don't have an account? Register now!