Bird
Raised Fist0
Pythonprogramming~20 mins

Creating custom modules in Python - Practice Exercises

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
Challenge - 5 Problems
🎖️
Custom Module Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of importing and using a custom module function
Consider you have a file named mymath.py with this content:
def add(a, b):
    return a + b

What will be the output of this code?
import mymath
print(mymath.add(3, 4))
Python
import mymath
print(mymath.add(3, 4))
A7
B34
CTypeError
DNameError
Attempts:
2 left
💡 Hint
Remember that the function add returns the sum of two numbers.
Predict Output
intermediate
2:00remaining
Output when importing a module with a syntax error
Given a file mymodule.py with this content:
def greet(name):
    print(f"Hello, {name}!")

if True
    print("This line has a syntax error")

What happens when you run this code?
import mymodule
mymodule.greet("Alice")
Python
import mymodule
mymodule.greet("Alice")
AHello, Alice!
BSyntaxError
CNameError
DIndentationError
Attempts:
2 left
💡 Hint
Check the if statement syntax in the module.
🔧 Debug
advanced
2:00remaining
Why does this custom module import fail?
You have a file calculator.py with:
def multiply(x, y):
    return x * y

And this code in another file:
from Calculator import multiply
print(multiply(2, 3))

What is the cause of the error when running this code?
Python
from Calculator import multiply
print(multiply(2, 3))
ANameError because multiply is not defined
BTypeError because multiply is not a function
CSyntaxError because import statement is wrong
DModuleNotFoundError because module name is case-sensitive
Attempts:
2 left
💡 Hint
Python module names are case-sensitive and must match the file name exactly.
📝 Syntax
advanced
2:00remaining
Which import statement correctly imports all functions from a custom module?
You have a module tools.py with several functions.
Which option correctly imports all functions so you can call them directly without prefix?
Aimport tools.*
Bfrom tools import all
Cfrom tools import *
Dimport * from tools
Attempts:
2 left
💡 Hint
The syntax for importing all names from a module uses 'from module import *'.
🚀 Application
expert
2:00remaining
What is the output of this custom module usage with __name__ check?
Given a file greetings.py with:
def say_hello():
    print("Hello from greetings")

if __name__ == "__main__":
    say_hello()

And this code in another file:
import greetings
print("Imported greetings module")

What will be printed when running the second file?
Python
import greetings
print("Imported greetings module")
AImported greetings module
B
Hello from greetings
Imported greetings module
CNo output
DNameError
Attempts:
2 left
💡 Hint
Code inside 'if __name__ == "__main__"' runs only when the module is executed directly.

Practice

(1/5)
1. What is the main purpose of creating a custom module in Python?
easy
A. To make the program run faster
B. To store data permanently
C. To create graphical user interfaces
D. To organize and reuse code easily

Solution

  1. Step 1: Understand what a module is

    A module is a file containing Python code like functions or classes.
  2. Step 2: Identify the purpose of custom modules

    Custom modules help organize code and allow reuse in different programs.
  3. Final Answer:

    To organize and reuse code easily -> Option D
  4. Quick Check:

    Custom modules = organize and reuse code [OK]
Hint: Modules group code for reuse and clarity [OK]
Common Mistakes:
  • Thinking modules speed up code execution
  • Confusing modules with data storage
  • Assuming modules create user interfaces
2. Which of the following is the correct way to import a custom module named mymodule?
easy
A. import mymodule
B. include mymodule
C. using mymodule
D. load mymodule

Solution

  1. Step 1: Recall Python import syntax

    Python uses the keyword import to bring in modules.
  2. Step 2: Match correct syntax

    Only import mymodule is valid Python syntax for importing a module.
  3. Final Answer:

    import mymodule -> Option A
  4. Quick Check:

    Import module = import keyword [OK]
Hint: Use 'import' keyword to bring in modules [OK]
Common Mistakes:
  • Using 'include' or 'load' which are not Python keywords
  • Trying 'using' which is from other languages
  • Misspelling 'import'
3. Given a file math_ops.py with this code:
def add(a, b):
    return a + b

What will be the output of this code?
import math_ops
print(math_ops.add(3, 4))
medium
A. 7
B. 34
C. TypeError
D. NameError

Solution

  1. Step 1: Understand the function in math_ops.py

    The function add takes two numbers and returns their sum.
  2. Step 2: Analyze the import and function call

    Importing math_ops allows calling math_ops.add(3, 4), which returns 3 + 4 = 7.
  3. Final Answer:

    7 -> Option A
  4. Quick Check:

    3 + 4 = 7 [OK]
Hint: Imported functions run normally with correct arguments [OK]
Common Mistakes:
  • Confusing string concatenation with addition
  • Expecting errors due to import
  • Forgetting to call function with parentheses
4. You have a module file named utils.py with a function greet(). Which of these import statements will cause an error?
medium
A. import utils
B. import utils.greet
C. from utils import greet
D. from utils import greet as hello

Solution

  1. Step 1: Understand Python import rules

    You can import a module or specific functions from it, but not a function as a submodule.
  2. Step 2: Check each option

    Options B, C, and D are valid. import utils.greet tries to import a function as a module, which causes ImportError.
  3. Final Answer:

    import utils.greet -> Option B
  4. Quick Check:

    Functions are imported, not as submodules [OK]
Hint: Import modules or functions, not functions as modules [OK]
Common Mistakes:
  • Trying to import a function like a module
  • Confusing 'from' and 'import' usage
  • Using invalid aliases
5. You want to create a custom module text_utils.py with a function count_vowels(text) that returns the number of vowels in a string. Which code correctly defines this function?
hard
A. def count_vowels(text): vowels = 'aeiou' count = 0 for char in text: if char in vowels: count += 1 return count
B. def count_vowels(text): vowels = ['a', 'e', 'i', 'o', 'u'] count = 0 for char in text: if char in vowels: count += 1 return count
C. def count_vowels(text): vowels = 'aeiouAEIOU' return sum(1 for char in text if char in vowels)
D. def count_vowels(text): vowels = 'AEIOU' count = 0 for char in text: if char.lower() in vowels: count += 1 return count

Solution

  1. Step 1: Check vowel counting logic

    def count_vowels(text): vowels = 'aeiouAEIOU' return sum(1 for char in text if char in vowels) uses a string with both uppercase and lowercase vowels and counts characters in one line using sum and generator.
  2. Step 2: Compare other options

    def count_vowels(text): vowels = ['a', 'e', 'i', 'o', 'u'] count = 0 for char in text: if char in vowels: count += 1 return count misses uppercase vowels, def count_vowels(text): vowels = 'aeiou' count = 0 for char in text: if char in vowels: count += 1 return count misses uppercase vowels, def count_vowels(text): vowels = 'AEIOU' count = 0 for char in text: if char.lower() in vowels: count += 1 return count incorrectly checks lowercase char in uppercase vowels string.
  3. Final Answer:

    def count_vowels(text): vowels = 'aeiouAEIOU' return sum(1 for char in text if char in vowels) -> Option C
  4. Quick Check:

    Count vowels with case check = def count_vowels(text): vowels = 'aeiouAEIOU' return sum(1 for char in text if char in vowels) [OK]
Hint: Use sum with generator and full vowel set for case [OK]
Common Mistakes:
  • Ignoring uppercase vowels
  • Checking lowercase char in uppercase vowels string
  • Using list instead of string for vowels unnecessarily