0
0
Pythonprogramming~10 mins

Importing specific items in Python - Step-by-Step Execution

Choose your learning style9 modes available
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.
Execution Sample
Python
from math import sqrt, pi

result = sqrt(16)
circle_area = pi * 2 ** 2
print(result, circle_area)
This code imports sqrt and pi from math, calculates square root of 16 and area of circle with radius 2, then prints results.
Execution Table
StepActionEvaluationResult
1Import sqrt and pi from mathsqrt and pi are loadedsqrt=function, pi=3.141592653589793
2Calculate sqrt(16)sqrt(16)4.0
3Calculate circle_area = pi * 2 ** 23.141592653589793 * 412.566370614359172
4Print result and circle_areaprint(4.0, 12.566370614359172)4.0 12.566370614359172
💡 All steps complete, program ends after printing results.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
resultundefined4.04.04.0
circle_areaundefinedundefined12.56637061435917212.566370614359172
Key Moments - 2 Insights
Why do we write 'from math import sqrt, pi' instead of 'import math'?
Because 'from math import sqrt, pi' loads only sqrt and pi directly, so we can use them without 'math.' prefix, as shown in step 1 and 2.
What happens if we try to use 'math.sqrt' after importing specific items?
Since we did not import the whole math module, 'math' is not defined. We only have sqrt and pi directly, as shown in the execution table where sqrt is used alone.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'result' after step 2?
Aundefined
B16
C4.0
D2.0
💡 Hint
Check the 'result' value in variable_tracker after step 2.
At which step is the circle_area calculated?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Action' column in execution_table for calculation of circle_area.
If we change 'from math import sqrt, pi' to 'import math', how would the code change?
AWe can use sqrt and pi directly as before.
BWe must use math.sqrt and math.pi to access them.
CThe code will not run at all.
DWe must import sqrt and pi again.
💡 Hint
Recall key_moments about difference between 'import math' and 'from math import ...'.
Concept Snapshot
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.
Full Transcript
This lesson shows how to import specific items from a Python module. Instead of importing the whole module, you write 'from module import item1, item2'. Python loads only those items. Then you can use them directly in your code. For example, 'from math import sqrt, pi' lets you use sqrt() and pi without 'math.' before them. The code calculates the square root of 16 and the area of a circle with radius 2, then prints the results. This method keeps code shorter and clearer. Remember, if you import specific items, the module name itself is not available unless you import it separately.