A4 Python for engineers · Practice
Lab: decisions, loops and functions
Problems dealt one at a time, with new numbers every time. The kinds you miss come back more often, and you can keep going as long as you like.
Check
Write led(Vs, Vf, I_max) returning (R, rating): the smallest E12 resistor (from the starter's values) that keeps the current at or below I_max amps, and the smallest of 0.125, 0.25, 0.5 and 1 W that's at least twice its dissipation. Then set R, rating = led(Vs, Vf, I / 1000) for Vs = 5 V, Vf = 1.8 V and I = 20 mA.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
The starter makes temps, a day of room temperatures, one per minute. A thermostat turns the heater on when the temperature is below lo = 19 °C and off when it's above lo + band (1 °C higher), and otherwise leaves it as it was. Starting with the heater off, set on to a list of the heater's state each minute, switches to how many times it changed, and duty to the fraction of minutes it was on.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A current source of Ic = 10 µA charges C = 47 nF until the voltage reaches Vth = 3 V, when it's discharged to zero at once. Simulate 0.5 s in steps of 10 µs. Set n to the number of discharges, period to the average spacing between them in seconds, and ideal to .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write bisect(f, a, b, tol=1e-9), which halves [a, b] until it's narrower than tol times b and returns its midpoint (assume f(a) and f(b) have opposite signs). Use it with a lambda to set f to the frequency, between 1 Hz and 100 MHz, where an RC low-pass with corner fc = 1000 Hz has gain g = 0.5: the gain is .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write sweep(start, stop, n=10, log=False) returning a list of n values from start to stop inclusive: evenly spaced, or with equal ratios between neighbors when log=True. Then set lin = sweep(a, b, n) and geo = sweep(a, b, n, log=True) for a = 10, b = 1000 and n = 11.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A 5 µA current source charges 100 nF from 0 V. How long until the capacitor reaches 3 V?
Write units the way you normally would: 1.5 s and 1500 ms are the same answer.
A kind of problem counts as solid once you've got it right on the first try twice in a row. Back to the lesson · Practice every topic in A4