0
0
Pythonprogramming~10 mins

Creating custom modules in Python - Visual Walkthrough

Choose your learning style9 modes available
Concept Flow - Creating custom modules
Write functions/classes in a .py file
Save file as module_name.py
In another file, use import statement
Call functions or use classes from module
Program runs using custom module code
You write code in a file, save it as a module, then import and use it in another file.
Execution Sample
Python
def greet(name):
    return f"Hello, {name}!"

# In another file:
import mymodule
print(mymodule.greet("Alice"))
Defines a greet function in a module and uses it from another file to print a greeting.
Execution Table
StepActionCode LineResult/Output
1Define function greet in mymodule.pydef greet(name): ...Function greet created
2Save file as mymodule.pyFile savedModule ready to import
3Import mymodule in main.pyimport mymoduleModule loaded
4Call greet with 'Alice'mymodule.greet("Alice")Returns 'Hello, Alice!'
5Print the returned greetingprint(mymodule.greet("Alice"))Output: Hello, Alice!
💡 Program ends after printing the greeting
Variable Tracker
VariableStartAfter greet('Alice')Final
nameundefined"Alice"undefined after function ends
return valueundefined"Hello, Alice!""Hello, Alice!"
Key Moments - 3 Insights
Why do we save the code in a separate .py file?
Because the module must be a separate file to import it, as shown in execution_table step 2.
What happens when we import the module?
Python loads the module file so we can use its functions, as seen in step 3.
Why do we use the module name before the function call?
To tell Python which module the function comes from, shown in step 4 calling mymodule.greet.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output at step 5?
AHello, Alice!
Bgreet
Cmymodule
DAlice
💡 Hint
Check the 'Result/Output' column in step 5 of the execution_table.
At which step is the module file saved?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look for the action mentioning saving the file in the execution_table.
If we forget to import the module, what will happen when calling greet?
AThe program prints the greeting anyway
BPython raises an error because greet is undefined
CThe program runs but prints nothing
DThe module imports automatically
💡 Hint
Think about what happens if you call a function without importing its module, related to step 3 and 4.
Concept Snapshot
Create a Python module by saving functions/classes in a .py file.
Import the module in another file using 'import module_name'.
Call functions with 'module_name.function()'.
This helps organize code and reuse it easily.
Full Transcript
Creating custom modules in Python means writing functions or classes in a separate .py file and saving it. This file becomes a module. In another Python file, you import this module using the import statement. Then you can call the functions or use classes from the module by prefixing them with the module name. For example, if you have a greet function in mymodule.py, you import mymodule and call mymodule.greet('Alice'). The program runs by loading the module and executing the function, printing the greeting. This method helps keep code organized and reusable.