Skip to main content
← Python Study Guide

Code Scholars Tutoring · Python

1.2 Numeric Types and Division

How it works

The two division operators differ: one always produces a float, the other floors toward negative infinity. Mixing int and float promotes to float.

Choose floor division when counting whole groups and true division when a fraction is meant.

Common error

Floor division of a negative number rounds toward negative infinity, not toward zero.

Worked example

Python

print(7 / 2)
print(7 // 2)
print(-7 // 2)
print(-7 % 2)

Output

3.5
3
-4
1

True division returns a fractional result. Floor division rounds down, so its negative behavior differs from Java integer truncation. The quotient and remainder satisfy a = (a // b)*b + a % b.

Exercises

1.2.1 · Trace

Determine the exact output and explain the state changes.

Python

print(11 // 4)
print(11 % 4)
print(2 ** 3)
Show solution

Output

2
3
8

Floor division gives 2, remainder gives 3, and exponentiation gives 8.

1.2.2 · Write code

Given 135 seconds, print whole minutes and remaining seconds.

Show solution

Use floor division and remainder.

One valid solution

seconds = 135
print(seconds // 60, seconds % 60)

Expected output

2 15
Open Python compiler

Need help with this topic?

Work through questions with a tutor and get help with the steps you find difficult.

Get help with this topic