
![]() |
@wtf | |
(Create your own utility math module) |
||
1
Replies
27
Views
1 Bookmarks
|
![]() |
@wtf | 20 days |
*Creating & Using Custom Modules* *What is a Module?* A module is just a .py file containing functions, variables, or classes you can reuse across other files. *Why Use Modules?* Reuse code Keep things organized Avoid repetition Easier to debug and collaborate *Example: Create a Custom Module* mymath.py python def add(a, b): return a + b def square(x): return x ** 2 main.py python import mymath print(mymath.add(2, 3)) Output: 5 print(mymath.square(4)) Output: 16 *Using from Keyword:* python from mymath import add print(add(10, 5)) *Tip: Where to Store It?* Place the module .py in the same folder as your script Or add the folder path to PYTHONPATH *Mini Project: Make a Utility Module* utils.py python def is_even(n): return n % 2 == 0 def greet(name): return fHello, name! app.py python from utils import is_even, greet print(greet(Deepak)) print(Is 10 even?, is_even(10)) *Make your code modular, readable & scalable!* |
||


