0
0
Pythonprogramming~5 mins

Package structure and usage in Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a Python package?
A Python package is a folder that contains a special file called __init__.py and other Python files or subfolders. It helps organize code into groups so it is easier to find and use.
Click to reveal answer
beginner
What is the purpose of the __init__.py file in a package?
The __init__.py file tells Python that the folder should be treated as a package. It can be empty or contain code that runs when the package is imported.
Click to reveal answer
beginner
How do you import a module from a package?
Use the dot notation: <code>import package_name.module_name</code> or <code>from package_name import module_name</code>. This lets you use the code inside that module.
Click to reveal answer
intermediate
What is a subpackage in Python?
A subpackage is a package inside another package. It is a folder with its own __init__.py file inside the main package folder.
Click to reveal answer
beginner
Why is organizing code into packages helpful?
It keeps code neat and easy to find, helps avoid name conflicts, and makes it easier to share and reuse code in different projects.
Click to reveal answer
What file must be present in a folder for Python to recognize it as a package?
Asetup.py
Bmain.py
C__init__.py
Dpackage.py
How do you import the module tools from the package utils?
Aimport tools.utils
Bimport utils-tools
Cfrom tools import utils
Dimport utils.tools
What is a subpackage?
AA module inside a package
BA package inside another package
CA file named <code>subpackage.py</code>
DA package without <code>__init__.py</code>
Which statement is true about __init__.py?
AIt can contain code that runs when the package is imported
BIt must always be empty
CIt is only needed for Python 3.12+
DIt is used to run scripts
Why use packages in Python?
ATo organize code and avoid name conflicts
BTo make code run faster
CTo write code without indentation
DTo create executable files
Explain what a Python package is and how it is structured.
Think about folders and special files that group code.
You got /3 concepts.
    Describe how to import a module from a package and why this is useful.
    Consider how you tell Python where to find the code you want.
    You got /4 concepts.