Engineering Python · Functions & Files
Modules and import
In Engineering Python because labs lean on the standard library — import math or json is how you reuse battle-tested code.
`import` loads a module. Dot access reaches its functions and constants. You will meet `math` and `json` constantly before NumPy and pandas.
- Engineering Python
- Easy level
- 4 concepts
1import and dotted names
`import math` loads the math module. `math.sqrt(9)` calls a function defined there.
Figure. import math loads the math module. math.sqrt(9) calls a function defined there and prints 3.0.
import math
import math
print(math.sqrt(9))What does `import math` do?
- Loads the math module so you can use math.name
- Prints every math function
- Creates a file called math.py always
import loads a module into the session.
2from ... import
`from math import sqrt` binds `sqrt` directly. Use sparingly so readers still know where a name came from.
Figure. from math import sqrt binds sqrt directly. Use sparingly so readers still know where a name came from — math.sqrt keeps the module in the name.
Takeaway
- Idea`from math import sqrt` binds `sqrt` directly. Use sparingly so readers still know where a name came from.
3json for structured text
`json.loads` parses a JSON string into Python lists/dicts; `json.dumps` goes the other way. Labs often exchange small tables as JSON text.
Figure. json.loads parses a JSON string into Python lists/dicts; json.dumps goes the other way. loads('{"n": 3}') then data["n"] is 3.
Takeaway
- Idea`json.loads` parses a JSON string into Python lists/dicts; `json.dumps` goes the other way. Labs often exchange small tables as JSON text.
json.loads
import json
data = json.loads('{"n": 3}')
print(data["n"])4Lab: import math
Import math and print a sqrt the check can see.
Figure. Import math and print math.sqrt(16). The dotted call yields 4.0 — the value the check can see.
Takeaway
- IdeaImport math and print a sqrt the check can see.
Coding lab. Use math.sqrt runs in the app, with checks on your output.
Notes
- In Engineering Python because labs lean on the standard library — import math or json is how you reuse battle-tested code.
- `import math` loads the math module. `math.sqrt(9)` calls a function defined there.
- `from math import sqrt` binds `sqrt` directly. Use sparingly so readers still know where a name came from.
Exam traps & shortcuts
- Run one cell at a time and read stdout before changing more lines.
- Names are labels for values; rebinding a name does not rewrite old prints.
Recap
import loads a module; dotted names call into it; json moves structured text.
- import and dotted names
- `import math` loads the math module. `math.sqrt(9)` calls a function defined there.
- from ... import
- `from math import sqrt` binds `sqrt` directly. Use sparingly so readers still know where a name came from.
- json for structured text
- `json.loads` parses a JSON string into Python lists/dicts; `json.dumps` goes the other way. Labs often exchange small tables as JSON text.
- Lab: import math
- Import math and print a sqrt the check can see.
Practise Modules and import
Reading is free and needs no account. Practice, mocks and progress live in the app.
- A 2-question practice set that ends the chapter
- 1 quick check with worked explanations
- Timed mocks scored with the real marking scheme
- Readiness tracked per topic, kept on your device