A4 Python for engineers · Practice
Problem set 1: numbers and strings
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 eng(x, unit) returning a string like "4.7 kΩ": the value scaled to a number from 1 up to (not including) 1000 with the matching prefix (p, n, µ, m, none, k, M, G), shown with :g, a space, then the prefix and unit. Set s = eng(v, "Ω") for v = 3.3 × 10⁻⁷.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Add 0.1 to a running total n = 10000 times in a loop, starting from 0.0. Set total to the result, err to abs(total - n / 10), and exact to math.fsum of the same n values (which rounds only once).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write hms(seconds) returning a string "h:mm:ss" (hours with no padding, minutes and seconds with two digits) using // and %. Set s = hms(T) for T = 3600.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write sig(x, digits) rounding a positive number to digits significant figures with round and math.log10. Set y = sig(x, d) for x = 98765 and d = 2.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A 16-bit status register packs fields: bits 0 to 3 are the channel, bits 4 to 7 the gain code, and bit 15 an error flag. Write fields(reg) returning (channel, gain, error) using >> and &, with error a bool. Set f = fields(reg) for reg = 1234, and h = f"{reg:04X}".
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
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