Welcome to the “Creating Modules” tutorial!
Overview
Modules are Python files (.py
) that group related code. You can import them elsewhere.
Steps
Create a file
mymodule.py
:# mymodule.py def greet(name): return f"Hello, {name}!"
Import and use in another script:
# main.py import mymodule print(mymodule.greet("Alice"))
Use aliases:
from mymodule import greet as hello print(hello("Bob"))
Key Takeaways
- Organize reusable functions and classes into modules.
- Use
import module
orfrom module import name
as needed.
Happy coding!
Comments
Post a Comment