Division (/)
The / operator divides two numbers and always returns a float, even when the division is exact.
Used in 86 lessons of the course, from 00.02.
The / operator is "true" division. It takes two numbers and returns their quotient as a float. That is why 6 / 3 gives 2.0, not 2.
It works this way so the result type doesn't depend on whether the numbers divide evenly. You always know what you get.
If you need an integer result, use // (floor division). Dividing by zero raises ZeroDivisionError.
The course shows another use of the same symbol. In the line prefix / "conda-meta", prefix is a Path object and the slash divides nothing: it joins parts of a path. Python lets each type decide what / means. With numbers it's division; with Path it joins paths.
print(7 / 2) # 3.5
Reach for it when: you want the exact quotient of two numbers, like an average or a percentage. With `Path` objects, use it to join parts of a path.
Examples
Common mistake
total = 10 count = 0 print(total / count)
Why: You can't divide by zero, and Python tells you with an exception.
How to write it: Check the divisor first with if count != 0:, or handle the error with try / except.
Common mistake
items = 7 pairs = items / 2 print(pairs)
Why: You expected an integer (3 pairs), but `/` always returns a float with decimals.
How to write it: Use // to get the whole part: items // 2 gives 3.
