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?
✗ Incorrect
Python modules are files with the
.py extension.How do you use functions from a custom module named
tools.py?✗ Incorrect
You use
import tools to access functions inside tools.py.What does the
if __name__ == '__main__': block do?✗ 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?✗ Incorrect
Use
from hello import greet to import just the greet function.What file is needed to make a folder a Python package?
✗ 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.