typestar

Working with numbers in Python

Integer and float arithmetic, and the operators that differ.

minutes = 947
hours = minutes // 60
remainder = minutes % 60
both = divmod(minutes, 60)

price = 19.99
tax = price * 0.0825
rounded = round(price + tax, 2)

big = 2 ** 32
ratio = 7 / 2
floored = 7 // 2

How it works

  1. Splits minutes into hours with floor division // and %.
  2. divmod returns quotient and remainder at once.
  3. Rounds a float result to 2 places with round.
  4. / always gives a float; // keeps integers whole.

Keywords and builtins used here

The run, in numbers

Lines
12
Characters to type
201
Tokens
57
Three-star pace
80 tpm

At the three-star pace of 80 tokens a minute, this run takes about 43 seconds.

Type this snippet

Step 2 of 6 in Variables & types, step 2 of 72 in Language basics.

← Previous Next →