Bird
Raised Fist0
Pythonprogramming~5 mins

Importing specific items in Python - Time & Space Complexity

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
Time Complexity: Importing specific items
O(1)
Understanding Time Complexity

When we import specific items in Python, we want to know how this affects the program's speed as it runs.

We ask: How does the time to import grow when we import parts of a module?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.

from math import sqrt, factorial

result1 = sqrt(16)
result2 = factorial(5)
print(result1, result2)

This code imports only two specific functions from the math module and uses them.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Importing the specified functions from the module.
  • How many times: This happens once when the program starts.
How Execution Grows With Input

Importing specific items happens once and does not repeat with input size.

Input Size (n)Approx. Operations
10Constant time to import specified items
100Still constant time, no change
1000Still constant time, no change

Pattern observation: The time to import specific items does not grow with input size; it stays the same.

Final Time Complexity

Time Complexity: O(1)

This means importing specific items takes the same amount of time no matter how big the input is.

Common Mistake

[X] Wrong: "Importing specific items takes longer as the program runs or input grows."

[OK] Correct: Importing happens once at the start, so it does not repeat or grow with input size.

Interview Connect

Understanding how imports work helps you write efficient code and explain program behavior clearly.

Self-Check

"What if we imported an entire large module instead of specific items? How would the time complexity change?"

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