Lite / Building Machines That Respond / 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?
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.
Predict
What does Python print for print(2000 / 250)?
- 8
- 8.0
- 2000 / 250
Guess first, then reveal
8.0. In Python 3, / always gives a float (a number with a decimal point), even when the answer is whole. And without quotes, Python works out the sum rather than printing the text.
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.
What the interactive shows (text version)
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).
Watch (optional)
Watch for: 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.
“The First Programming Languages: Crash Course Computer Science #11” by CrashCourse (opens YouTube; streaming video uses a lot of data)
The video is optional. Everything you need is in this lesson's text.
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 minutesWhat 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
Intuitive
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.
Technical
Python is an interpreted language: the interpreter reads each statement, evaluates the expression on the right of =, binds the result to the name on the left, and executes calls like print in order.
Mathematical
Runtime t = Q / I, where Q is charge capacity (mAh) and I is current (mA): 2000 mAh / 250 mA = 8 h. This ideal estimate ignores capacity loss at high current, temperature and the cut-off voltage.
Applied
Wireless sensor nodes are designed around this exact calculation, then made to sleep most of the time: an average of 1 mA instead of 25 mA turns days into months.
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.
Practice
Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.
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.
Reflect
What surprised you most about how literally Python follows your program?
Write your answer on paper or in your own notes. Lite never stores what you write.
Go further
- The Python Tutorial (official)
The official, free introduction to Python, written by its developers.