Bird
Raised Fist0

Given a package tools with modules math_ops.py and string_ops.py, which of the following import statements correctly imports the add function from math_ops and the capitalize function from string_ops in a single line?

hard🚀 Application Q9 of Q15
Python - Modules and Code Organization
Given a package tools with modules math_ops.py and string_ops.py, which of the following import statements correctly imports the add function from math_ops and the capitalize function from string_ops in a single line?
Afrom tools.math_ops import add; from tools.string_ops import capitalize
Bfrom tools import add, capitalize
Cfrom tools.math_ops import add, from tools.string_ops import capitalize
Dfrom tools.math_ops import add, capitalize
Step-by-Step Solution
Solution:
  1. Step 1: Understand Python import syntax

    Each import statement can import multiple items from one module, but to import from different modules, separate statements are needed.
  2. Step 2: Analyze options

    from tools.math_ops import add; from tools.string_ops import capitalize uses two separate import statements separated by a semicolon, which is valid Python syntax for multiple statements on one line.
  3. Step 3: Identify incorrect options

    from tools import add, capitalize tries to import functions directly from the package without specifying modules, which is invalid unless explicitly exposed in __init__.py. from tools.math_ops import add, from tools.string_ops import capitalize has a syntax error with a comma separating two import statements. from tools.math_ops import add, capitalize tries to import both functions from the same module incorrectly.
  4. Final Answer:

    from tools.math_ops import add; from tools.string_ops import capitalize -> Option A
  5. Quick Check:

    Separate imports for different modules on one line [OK]
Quick Trick: Use separate imports with semicolon for different modules [OK]
Common Mistakes:
MISTAKES
  • Trying to import multiple functions from different modules in one statement
  • Assuming package exposes all functions directly
  • Using commas to separate multiple import statements

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes