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
Recall & Review
beginner
What is a Python module?
A Python module is a file containing Python code, like functions and variables, that you can reuse in other Python programs by importing it.
Click to reveal answer
beginner
How do you create a custom module in Python?
To create a custom module, write your Python code in a file with a <code>.py</code> extension. This file becomes your module which you can import in other scripts.
Click to reveal answer
beginner
How do you import a custom module named <code>mymodule.py</code>?
Use the statement <code>import mymodule</code> in your Python script to use the functions and variables defined in <code>mymodule.py</code>.
Click to reveal answer
intermediate
What is the purpose of the if __name__ == '__main__': block in a module?
It lets you run some code only when the module is run directly, not when it is imported. This is useful for testing your module.
Click to reveal answer
intermediate
How can you organize multiple custom modules into a package?
Put your modules inside a folder with an <code>__init__.py</code> file. This folder becomes a package, letting you import modules using dot notation like <code>package.module</code>.
Click to reveal answer
What file extension should a Python module have?
A.mod
B.txt
C.py
D.exe
✗ Incorrect
Python modules are files with the .py extension.
How do you use functions from a custom module named tools.py?
Arequire tools
Binclude tools
Cload tools
Dimport tools
✗ Incorrect
You use import tools to access functions inside tools.py.
What does the if __name__ == '__main__': block do?
ARuns code only when the module is executed directly
BImports all functions automatically
CDefines a class in the module
DPrevents the module from being imported
✗ Incorrect
This block runs code only if the module is run as the main program.
How do you import a function greet from a module hello.py?
Aimport greet from hello
Bfrom hello import greet
Cinclude greet from hello
Dload greet from hello
✗ Incorrect
Use from hello import greet to import just the greet function.
What file is needed to make a folder a Python package?
A__init__.py
Bmain.py
Cpackage.py
Dsetup.py
✗ Incorrect
The __init__.py file tells Python the folder is a package.
Explain how to create and use a custom module in Python.
Think about writing code in one file and using it in another.
You got /4 concepts.
Describe the role of the if __name__ == '__main__': block in a module.
It controls when some code runs depending on how the module is used.
You got /3 concepts.
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
Step 1: Understand what a module is
A module is a file containing Python code like functions or classes.
Step 2: Identify the purpose of custom modules
Custom modules help organize code and allow reuse in different programs.
Final Answer:
To organize and reuse code easily -> Option D
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
Step 1: Recall Python import syntax
Python uses the keyword import to bring in modules.
Step 2: Match correct syntax
Only import mymodule is valid Python syntax for importing a module.
Final Answer:
import mymodule -> Option A
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
Step 1: Understand the function in math_ops.py
The function add takes two numbers and returns their sum.
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
Step 1: Understand Python import rules
You can import a module or specific functions from it, but not a function as a submodule.
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.
Final Answer:
import utils.greet -> Option B
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
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.
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.
Final Answer:
def count_vowels(text):
vowels = 'aeiouAEIOU'
return sum(1 for char in text if char in vowels) -> Option C
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