0
0
Pythonprogramming~5 mins

Creating custom modules in Python - Quick Revision & Summary

Choose your learning style9 modes available
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
How do you use functions from a custom module named tools.py?
Arequire tools
Binclude tools
Cload tools
Dimport tools
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
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
What file is needed to make a folder a Python package?
A__init__.py
Bmain.py
Cpackage.py
Dsetup.py
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.