Bird
Raised Fist0
Pythonprogramming~10 mins

Creating custom modules in Python - Interactive Practice

Choose your learning style10 modes available

Start learning this pattern below

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the custom module named mymodule.

Python
import [1]
Drag options to blanks, or click blank then click option'
Amymodule
Bsys
Cos
Drandom
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to import a module that is not in the same folder or Python path.
Using the wrong module name.
2fill in blank
medium

Complete the code to call the function greet from the mymodule.

Python
mymodule.[1]()
Drag options to blanks, or click blank then click option'
Ahello
Bsay_hello
Cgreet
Dwelcome
Attempts:
3 left
💡 Hint
Common Mistakes
Calling a function name that does not exist in the module.
Forgetting the parentheses to call the function.
3fill in blank
hard

Fix the error in the import statement to correctly import the greet function from mymodule.

Python
from mymodule [1] greet
Drag options to blanks, or click blank then click option'
Aimport
Bimporting
Cimports
Dinclude
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect keywords like importing or include.
Misspelling import.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps each word to its length from the list words, but only for words longer than 3 characters.

Python
{word: [1] for word in words if len(word) [2] 3}
Drag options to blanks, or click blank then click option'
Alen(word)
B>
C<
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong comparison operator like <.
Using the word itself instead of its length.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths, but only for words with length greater than 4.

Python
{ [1]: [2] for word in words if len(word) [3] 4 }
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
C>
Dword.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using word.lower() instead of uppercase.
Using wrong comparison operators like < or ==.

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

  1. Step 1: Understand what a module is

    A module is a file containing Python code like functions or classes.
  2. Step 2: Identify the purpose of custom modules

    Custom modules help organize code and allow reuse in different programs.
  3. Final Answer:

    To organize and reuse code easily -> Option D
  4. 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

  1. Step 1: Recall Python import syntax

    Python uses the keyword import to bring in modules.
  2. Step 2: Match correct syntax

    Only import mymodule is valid Python syntax for importing a module.
  3. Final Answer:

    import mymodule -> Option A
  4. 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

  1. Step 1: Understand the function in math_ops.py

    The function add takes two numbers and returns their sum.
  2. Step 2: Analyze the import and function call

    Importing math_ops allows calling math_ops.add(3, 4), which returns 3 + 4 = 7.
  3. Final Answer:

    7 -> Option A
  4. Quick Check:

    3 + 4 = 7 [OK]
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

  1. Step 1: Understand Python import rules

    You can import a module or specific functions from it, but not a function as a submodule.
  2. 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.
  3. Final Answer:

    import utils.greet -> Option B
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    def count_vowels(text): vowels = 'aeiouAEIOU' return sum(1 for char in text if char in vowels) -> Option C
  4. 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