Bird
Raised Fist0
Pythonprogramming~20 mins

Creating custom modules in Python - Try It Yourself

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
Creating Custom Modules
📖 Scenario: You are building a small program that uses a custom module to organize your code better. This is like having a toolbox where you keep your tools separate but easy to find.
🎯 Goal: Create a custom module with a function, then import and use that function in your main program.
📋 What You'll Learn
Create a Python module file named mymodule.py with a function inside
Define a function called greet inside mymodule.py that returns a greeting string
Import the greet function from mymodule in the main program
Call the greet function and print its result
💡 Why This Matters
🌍 Real World
In real projects, custom modules help organize code into separate files, making it easier to maintain and reuse parts of your program.
💼 Career
Knowing how to create and use modules is essential for software development jobs, as it improves code structure and teamwork.
Progress0 / 4 steps
1
Create the custom module file
Create a file named mymodule.py and inside it, define a function called greet that returns the string 'Hello from mymodule!'.
Python
Hint

Remember, a function starts with def and returns a value with return.

2
Import the greet function
In a new Python file (main program), write the line to import the greet function from the mymodule module.
Python
Hint

Use the syntax from module_name import function_name.

3
Call the greet function
Call the imported greet function and store its result in a variable named message.
Python
Hint

Call the function by writing its name followed by parentheses.

4
Print the greeting message
Print the variable message to display the greeting from the custom module.
Python
Hint

Use the print() function to show the message on the screen.

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