Python, properly · Chapter 3 of 42

Numbers

By the end of this chapter you can do DabbaDrop's sums in Python: divide with and without decimals, find what is left over, round a result, and add up money so that every paisa is right. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: First steps
  • 4 tasks
  • Python
  • In the all labs plan

The first 2 chapters of Python, properly are free. This one comes with the all labs plan.

Why you need this

Every part of DabbaDrop is a sum: rotis packed into boxes, the average order, GST on an office lunch, the day's cash against the bank statement. Rafiq, who does the accounts, has one rule: the totals must match to the paisa. Last month his spreadsheet was off by a fraction of a rupee and it took him an evening to find out why.

You can already name values. Now you need to know exactly what Python does when it calculates with them, including the one place where computers get simple decimals slightly wrong, and how to avoid it for money.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    boxes.py: rotis into boxes

    The kitchen packs rotis 4 to a box. Start boxes.py with these two lines, exactly:

    rotis = 23

    per_box = 4

    Then print how many boxes are full and how many rotis are left over, so the output is exactly:

    Full boxes: 5

    Loose rotis: 3

    The check also changes the two numbers (for example to 40 rotis, 6 per box) and runs your file again, so work both answers out from the names.

  2. Task 2

    average.py: the average order

    Rafiq wants the average order value. Start average.py with these two lines, exactly:

    sales = 2735

    orders = 12

    Print the sales divided by the number of orders, rounded to 2 decimal places with round, so the output is exactly:

    Average order: 227.92

    The check changes the two numbers and runs your file again.

  3. Task 3

    gst.py: a bill with GST

    An office orders several Veg Thalis, and the bill adds GST. Start gst.py with these three lines, exactly:

    thali_price = 210

    count = 3

    gst_percent = 5

    Print the subtotal (price times count), the GST (that percent of the subtotal) and the total (subtotal plus GST). Round the GST and the total to 2 places with round. The output is exactly:

    Subtotal: 630

    GST: 31.5

    Total: 661.5

    The check changes the three numbers and runs your file again.

  4. Task 4 · interview question

    Interview: money.py, rupees that add up exactly

    The bank sends the day's payments as text. Start money.py with these four lines, exactly:

    upi = "45.10"

    cash = "60.20"

    card = "19.70"

    bank = "125.00"

    Add the three payments exactly, as Decimal values, and compare the sum with the bank's total. The output is exactly:

    Total: 125.00

    Matches bank: True

    The check changes the four values (for example to 0.10, 0.20, 0.30 and 0.60) and runs your file again. Floats get some of these wrong, so the check needs Decimal.

What you will learn

  • Arithmetic: + - / * and the order Python does them in.
  • /, // and %: division with decimals, whole division, the remainder.
  • round(): keeping 2 decimal places, and its surprise.
  • Why 0.1 + 0.2 is not 0.3, and Decimal for money.

More in First steps

Build "Numbers" yourself

DabbaDrop, a Mumbai tiffin startup, runs on Excel and WhatsApp. You are its first Python developer. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Python, properly are free. This one comes with the all labs plan.