0
0
Pythonprogramming~10 mins

__init__ file role in Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create an empty __init__.py file that makes a folder a package.

Python
# Create an empty file named [1] in your folder
Drag options to blanks, or click blank then click option'
Ainit.py
Bpackage.py
C__init__.py
Dmain.py
Attempts:
3 left
💡 Hint
Common Mistakes
Naming the file without underscores like 'init.py'.
Using a different file name like 'package.py'.
2fill in blank
medium

Complete the code to import a module from a package using __init__.py.

Python
from mypackage [1] mymodule
Drag options to blanks, or click blank then click option'
Aimport
Bwith
Cas
Dfrom
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'from' instead of 'import' in this position.
Using 'as' or 'with' which are incorrect here.
3fill in blank
hard

Fix the error in the __init__.py file to expose a function from a submodule.

Python
# __init__.py
from .submodule [1] my_function
Drag options to blanks, or click blank then click option'
Aimport
Bfrom
Cas
Ddef
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'from' instead of 'import' after the dot.
Trying to define the function instead of importing it.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension in __init__.py that maps module names to their lengths.

Python
module_lengths = {name: len(name) for name in [1] if len(name) [2] 5}
Drag options to blanks, or click blank then click option'
A['mod1', 'mod2', 'longmodule']
Brange(5)
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using range(5) instead of a list of module names.
Using '<' instead of '>' for the length condition.
5fill in blank
hard

Fill all three blanks to create a dictionary in __init__.py that maps uppercase module names to their lengths if length is less than 10.

Python
module_info = [1]: [2] for [3] in ['modA', 'moduleB', 'modC'] if len([3]) < 10}
Drag options to blanks, or click blank then click option'
Aname.upper()
Blen(name)
Cname
Dmod
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'mod' instead of the variable name.
Mixing variable names in keys and values.