Skip to content
Tactiverge

School/△ Builder/Week 2

Your first Python program: battery runtime

Your sensor node has a 2000 mAh battery and draws 250 mA. Can six lines of Python tell you when it dies?

About 40 min · 3 goals · Checked by automated tests

1.Why should I care?

A calculator answers one question once. A program answers it every time an input changes: a new battery, a new current, a new sensor. Engineers write small programs like this constantly, because they can be checked, shared and rerun.

  • ◇ Write, run and edit a Python program that prints a calculated result with its unit.
  • ◇ Use variables, arithmetic and print, with comments that state units.
  • ◇ Change an input and rerun the program to answer a design question.

2.Discover

Open the Code Lab and type the battery program from this lesson exactly. Run it. Then change current_ma to 100 and rerun: how long does the node last now? Finally, make it print the runtime in days as well.

Can't use the interactive? Read a description instead

The Code Lab is an in-browser Python editor. The same exercise works on paper: given capacity 2000 mAh and current 250 mA, compute hours = 2000 / 250 = 8.0 and minutes = 480, then repeat with 100 mA (20.0 h).

3.Checkpoint: guided practice

Check what you just discovered before going on. Hints appear after a wrong answer.

Practice 1 · a fresh version every time

A sensor node runs from a 2500 mAh battery and draws a steady 50 mA. A program computes hours = 2500 / 50 and then minutes = hours * 60. What value does minutes hold, in minutes? (Ignore the battery ageing or the voltage dropping.)

min

Practice 2

What does print("3 + 4") show?

4.Apply it

Extend the program: add a variable for a safety margin (use only 80 % of the capacity) and print the runtime with and without the margin, each with its unit. Write one comment explaining why engineers keep a margin.

5.Understand

The program

# Battery runtime for a sensor node
capacity_mah = 2000   # battery capacity in mAh
current_ma = 250      # average current draw in mA

hours = capacity_mah / current_ma
print("Runtime:", hours, "h")
print(f"That is {hours * 60:.0f} minutes")

Output:

Runtime: 8.0 h
That is 480 minutes

What each part does

# starts a comment: Python ignores it, people read it. capacity_mah = 2000 creates a variable: a name that stores a value. / divides. print(...) shows values, separated by spaces. The f"..." is an f-string: anything in { } is worked out and put into the text, and :.0f means 'show with 0 decimal places'.

Units live in names and comments

Python does not know what a milliamp is. It only sees numbers. So engineers put units into names (current_ma) and comments. Here mAh ÷ mA leaves hours, so the variable is called hours. If the units in a calculation don't cancel to what you expect, the formula is wrong.

Explain it another way

A program is a recipe the computer follows exactly, from the top line down. Variables are labelled jars: put a number in, use it later by name.

Worked example 1

The node sleeps at 0.5 mA most of the time. What runtime does the program give for current_ma = 0.5?

hours = 2000 / 0.5 = 4000.0 h, about 4000 / 24 ≈ 167 days. Real batteries also slowly lose charge on their own, so the true figure would be lower: state that assumption next to the result.

Common mix-ups

  • print(2000 / 250) prints 8.
    ✓ It prints 8.0, because / always produces a float in Python 3.
  • The program knows the numbers are in mAh and mA.
    ✓ Python only sees numbers. Units are your job, which is why they go in names and comments.

6.Watch

→ Listen for why people write in languages like Python instead of machine code, and what has to translate a program before the computer can run it.

Why people write programs in languages like Python instead of raw machine code, and what an interpreter or compiler does with them.

Length: see YouTube · captions on YouTube · source

Nothing loads from YouTube until you press play.

The video is optional: everything you need is also in the lesson text and the interactive.

7.Practice on your own

Practice, not a test. Wrong answers get a hint and another try.

Practice 3

What does Python do with # battery capacity in mAh?

Practice 4

Using the same formula, a 3000 mAh battery powers a 25 mA device. How many days is that?

days

Practice 5

Which variable name is best for a current measured in milliamps?

8.Predict

What does Python print for print(2000 / 250)?

Guess first. Being wrong here is how the surprise works.

9.Reflect

10.Go further

Concepts: Variables, types and expressions