To create your own Python module, follow these steps:
-
Create a Python file: Create a new file with a
.pyextension. For example, you can name itmy_module.py. -
Define functions or classes: Inside this file, you can define functions, classes, or variables. For example:
# my_module.py def say_hello(): print("Hello from my_module!") def add(a, b): return a + b -
Save the file: Make sure to save the file in a directory where you can access it from your main Python script.
-
Import the module: In your main Python script, you can import your module using the
importstatement:import my_module my_module.say_hello() # Output: Hello from my_module! result = my_module.add(5, 3) print(result) # Output: 8
That's it! You've successfully created and used your own Python module.
