Bird
Raised Fist0
Pythonprogramming~10 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does the statement from math import sqrt do in Python?
easy
A. It imports the entire math module.
B. It imports only the sqrt function from the math module.
C. It imports all functions except sqrt from math.
D. It renames the math module to sqrt.

Solution

  1. Step 1: Understand the import syntax

    The syntax from module import item imports only the specified item from the module.
  2. Step 2: Apply to the given statement

    Here, sqrt function is imported from the math module, not the whole module.
  3. Final Answer:

    It imports only the sqrt function from the math module. -> Option B
  4. Quick Check:

    from module import item = import only that item [OK]
Hint: Remember: 'from module import item' imports just that item [OK]
Common Mistakes:
  • Thinking it imports the whole module
  • Confusing import with renaming
  • Assuming it excludes the named item
2. Which of the following is the correct syntax to import the choice and shuffle functions from the random module?
easy
A. from random import choice, shuffle
B. import random.choice, random.shuffle
C. import choice, shuffle from random
D. from random import (choice shuffle)

Solution

  1. Step 1: Recall correct import syntax for multiple items

    To import multiple items, use from module import item1, item2 separated by commas.
  2. Step 2: Check each option

    from random import choice, shuffle matches the correct syntax: from random import choice, shuffle.
  3. Final Answer:

    from random import choice, shuffle -> Option A
  4. Quick Check:

    Multiple imports use commas inside from-import [OK]
Hint: Use commas to import multiple items from a module [OK]
Common Mistakes:
  • Using 'import module.item' syntax incorrectly
  • Placing 'from' after 'import'
  • Using parentheses without commas
3. What will be the output of this code?
from math import sqrt
print(sqrt(16))
medium
A. sqrt(16)
B. 16
C. NameError
D. 4.0

Solution

  1. Step 1: Understand what sqrt(16) does

    The sqrt function returns the square root of the number, so sqrt(16) returns 4.0.
  2. Step 2: Confirm import allows direct use

    Since sqrt was imported directly, calling sqrt(16) works without prefix.
  3. Final Answer:

    4.0 -> Option D
  4. Quick Check:

    sqrt(16) = 4.0 [OK]
Hint: Direct import lets you call function without module name [OK]
Common Mistakes:
  • Expecting integer 4 instead of float 4.0
  • Forgetting to import sqrt causing NameError
  • Trying to call math.sqrt without importing math
4. Identify the error in this code:
from os import path
print(os.path.exists('file.txt'))
medium
A. NameError because os is not imported
B. AttributeError because path has no exists method
C. SyntaxError in import statement
D. No error, code runs fine

Solution

  1. Step 1: Analyze the import statement

    The code imports only path from os, not the whole os module.
  2. Step 2: Check usage of os.path.exists

    The code tries to use os.path.exists, but os is not defined, causing a NameError.
  3. Final Answer:

    NameError because os is not imported -> Option A
  4. Quick Check:

    Importing item only means module name is undefined [OK]
Hint: Importing item doesn't import module name itself [OK]
Common Mistakes:
  • Assuming module name is available after importing item
  • Confusing AttributeError with NameError
  • Thinking import syntax is wrong
5. You want to import the datetime and timedelta classes from the datetime module but rename timedelta to td for clarity. Which is the correct import statement?
hard
A. from datetime import datetime as dt, timedelta as td
B. import datetime.datetime, datetime.timedelta as td
C. from datetime import datetime, timedelta as td
D. from datetime import datetime, timedelta td

Solution

  1. Step 1: Understand renaming syntax in import

    You can rename an imported item using as, e.g., timedelta as td.
  2. Step 2: Check options for correct syntax

    from datetime import datetime, timedelta as td correctly imports datetime and renames timedelta to td using from datetime import datetime, timedelta as td.
  3. Final Answer:

    from datetime import datetime, timedelta as td -> Option C
  4. Quick Check:

    Use 'as' to rename imported items [OK]
Hint: Use 'as' to rename imported items for clarity [OK]
Common Mistakes:
  • Trying to rename module instead of item
  • Incorrect syntax without commas or 'as'
  • Using import instead of from-import for renaming