Concept Flow - Importing specific items
Start
Write import statement
Python loads module
Extract specific items
Use imported items in code
End
This flow shows how Python imports only the specified items from a module and makes them ready to use.
Jump into concepts and practice - no test required
from math import sqrt, pi result = sqrt(16) circle_area = pi * 2 ** 2 print(result, circle_area)
| Step | Action | Evaluation | Result |
|---|---|---|---|
| 1 | Import sqrt and pi from math | sqrt and pi are loaded | sqrt=function, pi=3.141592653589793 |
| 2 | Calculate sqrt(16) | sqrt(16) | 4.0 |
| 3 | Calculate circle_area = pi * 2 ** 2 | 3.141592653589793 * 4 | 12.566370614359172 |
| 4 | Print result and circle_area | print(4.0, 12.566370614359172) | 4.0 12.566370614359172 |
| Variable | Start | After Step 2 | After Step 3 | Final |
|---|---|---|---|---|
| result | undefined | 4.0 | 4.0 | 4.0 |
| circle_area | undefined | undefined | 12.566370614359172 | 12.566370614359172 |
Import specific items using: from module_name import item1, item2 This loads only those items directly, so you can use them without module prefix. Useful to keep code clean and simple.
from math import sqrt do in Python?from module import item imports only the specified item from the module.sqrt function is imported from the math module, not the whole module.sqrt function from the math module. -> Option Bchoice and shuffle functions from the random module?from module import item1, item2 separated by commas.from random import choice, shuffle.from math import sqrt print(sqrt(16))
sqrt function returns the square root of the number, so sqrt(16) returns 4.0.sqrt was imported directly, calling sqrt(16) works without prefix.from os import path
print(os.path.exists('file.txt'))path from os, not the whole os module.os.path.existsos.path.exists, but os is not defined, causing a NameError.os is not imported -> Option Adatetime and timedelta classes from the datetime module but rename timedelta to td for clarity. Which is the correct import statement?as, e.g., timedelta as td.datetime and renames timedelta to td using from datetime import datetime, timedelta as td.