Python Types Quiz: Division, Floats, Truthiness and Operators

Test your Python types and operators with 12 questions and three programs on floor division, floating point, truthiness, mutation, conversions and precedence.

  • Course: Python study plan
  • Module: Values, types and operators
  • Kind: Checkpoint — cleared at 70%
  • Reading time: 25 min
  • Runtime: CPython 3.11

Checkpoint — Values, types and operators is the checkpoint that closes the Values, types and operators module: a graded quiz and whole-program exercises, passed at 70%.

Instructions

This checkpoint covers the whole module: arbitrary-precision integers and the floor semantics of // and %, floating point and the three rules for comparing, printing and summing floats, truthiness and what and/or return, names bound to objects and the difference between rebinding and mutation, the explicit conversions and their errors, and operator precedence with the bitwise idioms.

How it works. Twelve questions and three programs. You need 70% on the questions and every program accepted to clear the module. You can retake it as often as you like; your best score counts.

Before you start, make sure you can answer these from memory:

  • What are -7 // 2 and -7 % 2, and which invariant relates them to -7?
  • Why is 0.1 + 0.2 == 0.3 false, and what do you write instead?
  • What does 0 or "x" evaluate to, and what does 3 and 0 evaluate to?
  • After a = [1]; b = a; b += [2], what is a? And after b = b + [3]?
  • Which exception does int("3.0") raise, and what two-step conversion works?
  • What is -2 ** 2, and why?
  • What does (x & (x - 1)) == 0 test, and would it mean the same without the brackets?

The three programs are a digit and bit report that uses integer arithmetic and the base-conversion functions, a temperature table that formats floats to fixed widths and picks labels with chained comparisons, and an exact-change calculator that keeps money in integer cents.

Common questions

What are -7 // 2 and -7 % 2 in Python?

-7 // 2 is -4, because // floors toward negative infinity, and -7 % 2 is 1, because % takes the divisor's sign. Together they satisfy -7 == 2 * (-7 // 2) + (-7 % 2).

What does 0 or "x" evaluate to in Python?

It evaluates to "x": or returns its first operand if that is truthy and its second otherwise, and 0 is falsy. Likewise 3 and 0 is 0, because and returns the first operand if it is falsy and the second otherwise.

After a = [1]; b = a; b += [2], what is a?

a is [1, 2]: b = a binds a second name to the same list, and += extends that list in place. After a further b = b + [3], a is still [1, 2], because + builds a new list and rebinds only b.

Which exception does int("3.0") raise?

ValueError, because int() parses only whole-number text and "3.0" contains a decimal point. Convert in two steps, int(float("3.0")), which parses the float and then truncates it toward zero.

Exercises

Digit and bit report

Read a non-negative integer (it may be very large) and print six facts about it: its number of decimal digits, the sum of its digits, its digital root (sum the digits repeatedly until one digit remains; the root of 0 is 0), its binary form without the 0b prefix, the number of one bits, and the number of bits needed to hold it (bit_length, which is 0 for 0).

Input: one integer. Output: six lines: digits, digit-sum, digital-root, binary, ones, bits, each followed by a space and the value.

12345

prints

digits 5
digit-sum 15
digital-root 6
binary 11000000111001
ones 6
bits 14

Temperature table

Read Celsius temperatures, one per line, until the end of input, and print a table row for each: the Celsius value and the Fahrenheit value (c * 9 / 5 + 32) both right-aligned in 7 characters with one decimal, then two spaces and a label — freezing when c <= 0, cold when 0 < c < 15, warm when 15 <= c < 25, hot otherwise. Use chained comparisons for the bands.

Input: zero or more numbers. Output: one row per number.

-3
22

prints

   -3.0    26.6  freezing
   22.0    71.6  warm

Exact change

A customer pays for an item; compute the change in coins using integer cents throughout. Denominations, in cents: 2000, 1000, 500, 200, 100, 50, 20, 10, 5, 2, 1. Use the greedy method (largest coin first). If the payment is smaller than the price print insufficient.

Input: one line paid price, both with exactly two decimals. Output: change <amount> with two decimals, then one line per denomination used, largest first: <coin with two decimals> x <count>.

20.00 13.45

prints

change 6.55
5.00 x 1
1.00 x 1
0.50 x 1
0.05 x 1

In this module: Values, types and operators

← Operators and precedence · Conditionals — if, elif, else and the shapes that read well →