A4 Python for engineers · Practice
Strings, formatting and parsing values
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 fmt(x, unit) that returns a string with x to three significant figures, a space, then the unit, like fmt(4.567, "V") → "4.57 V". Then set s = fmt(x, "Hz") with x = 4.567.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write eng(x) that returns x scaled to a value from 1 to under 1000, with three significant figures and a prefix letter (p, n, u, m, none, k, M, G), like eng(4700) → "4.7k" and eng(0.0033) → "3.3m". Then set e = eng(v) with v = 0.0033.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write parse(text) that turns a value with an optional prefix letter into a number: parse("4.7k") → 4700, parse("100n") → 1e-07, parse("330") → 330. Then set x = parse(s) for the string s the starter picks.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A parts list has lines like "R1, 4.7, 5%". The starter builds one for you. Set part (the name), value (a float) and tol (a fraction, so 5% is 0.05).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A bench meter answers a query with strings like "+4.71230E+00 VDC": a number in scientific notation, a space, then a unit. Write reading(text) returning (value, unit) with value a float. Then set r = reading(reply) for the reply the starter picks.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Set s to x = 12350 in scientific notation with two digits after the point, like "1.23e-04".
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Set line to the three numbers a = 2, b = 10 and c = 7 joined with commas and no spaces, like "1,10,3".
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Set has_prefix to whether the string s the starter picks ends in a letter.
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