Skip to content

Commit c6a3826

Browse files
Merge pull request #23 from basnetsoyuj/master
Added 23-modules.md
2 parents 87f14c1 + d4b4302 commit c6a3826

File tree

1 file changed

+220
-0
lines changed

1 file changed

+220
-0
lines changed

23-modules.md

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
# Python Modules
2+
3+
**Video link:**
4+
5+
In this video, we learned why modules are used and how we can import them in our program.
6+
We then learned to create custom modules with the help of examples.
7+
8+
**Programs in the Video**
9+
10+
- [Python Modules](#python-modules-1)
11+
- [Renaming modules](#renaming-modules)
12+
- [Python from...import statement](#python-fromimport-statement)
13+
- [The `dir()` function](#the-dir-function)
14+
- [Custom Modules](#custom-modules)
15+
16+
---
17+
18+
## Python Modules
19+
A module is a file that contains Python code that we can use in our program.
20+
21+
There are several built-in functions like `print()`, `input()` and `sum()` that are readily available to us.
22+
In addition to these functions, Python also has several functions that are defined inside a module which we can use after we import them.
23+
24+
Let's use one such module called `math`:
25+
26+
```python
27+
import math
28+
29+
number = 25
30+
result = math.sqrt(number)
31+
print(result)
32+
33+
print(math.pi)
34+
```
35+
36+
**Output**
37+
```
38+
5.0
39+
3.141592653589793
40+
```
41+
42+
Once we import a module, we can use everything inside it using the dot operator.
43+
44+
We imported `math` using the `import` statement and called its `sqrt()` function that calculates the square root of a number.
45+
46+
Also, the value of the constant `pi` is printed to the screen.
47+
48+
---
49+
50+
## Renaming modules
51+
52+
While importing a module, we can also rename it to a different name as per our needs.
53+
54+
Let's rename the `math` module to `m`:
55+
56+
```python
57+
import math as m
58+
59+
number = 25
60+
result = m.sqrt(number)
61+
print(result)
62+
63+
print(m.pi)
64+
```
65+
66+
**Output**
67+
```
68+
5.0
69+
3.141592653589793
70+
```
71+
72+
We get the same output as before.
73+
74+
---
75+
76+
## Python from...import statement
77+
When we import a module like in our previous examples, everything in the module is available to us.
78+
79+
However, if we only need to import a specific definition like a function or a constant, we can use the `from..import` statement.
80+
81+
```python
82+
from math import sqrt
83+
84+
num = sqrt(64)
85+
print(num)
86+
```
87+
88+
**Output**
89+
90+
```
91+
8.0
92+
```
93+
94+
Here, only the `sqrt` function is imported from the `math` module.
95+
When we use this syntax, instead of `math.sqrt`, we need to directly use the `sqrt` function.
96+
97+
We can also import multiple definitions from the module in a single line using this syntax:
98+
99+
```python
100+
from math import pi, sin, sqrt
101+
102+
value = sin(pi/2)
103+
print(value)
104+
105+
num = sqrt(64)
106+
print(num)
107+
```
108+
109+
**Output**
110+
```
111+
1.0
112+
8.0
113+
```
114+
115+
We can also use the `from...import` statement to import all definitions from a module using `*`:
116+
117+
```python
118+
from math import *
119+
120+
value = sin(pi/2)
121+
print(value)
122+
123+
num = sqrt(64)
124+
print(num)
125+
```
126+
127+
**Output**
128+
```
129+
1.0
130+
8.0
131+
```
132+
133+
Here, asterisk `*` means import everything.
134+
135+
>**Note:** Importing every definition name with the asterisk symbol is a bad programming practice and you should try to avoid it in your code.
136+
137+
---
138+
139+
## The `dir()` function
140+
141+
The `math` module comes with many more handy functions and constants.
142+
143+
We can list out all the things defined inside a module by using the `dir()` function:
144+
145+
```python
146+
import math
147+
148+
print(dir(math))
149+
```
150+
151+
**Output**
152+
153+
```
154+
['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']
155+
```
156+
157+
As you can see, there are many other mathematical, trigonometric and logarithmic functions inside the `math` module.
158+
159+
---
160+
161+
>The `math` module file comes when we install Python that's why we can import it directly. There are many popular modules that are not natively available for us to use.
162+
>For example, `numpy` is a popular package that is used for scientific computing. To use `numpy`, we must first install it.
163+
>
164+
>Similarly, you might have heard about `django`. It's a popular framework used for creating web applications. It's also available only after we install it.
165+
166+
---
167+
168+
## Custom Modules
169+
170+
In Python, we can also create our own custom modules as per our needs. This helps us to keep our code organized.
171+
172+
Let's a module named `calculator` that will contain functions to perform arithmetic operations.
173+
174+
First, create a file named `calculator.py` in the same directory and add the following:
175+
176+
```python
177+
def add(a, b):
178+
return a + b
179+
180+
def subtract(a, b):
181+
return a - b
182+
183+
def multiply(a, b):
184+
return a * b
185+
186+
def divide(a, b):
187+
return a / b
188+
```
189+
190+
This file in itself is a module. Let's import this file from the main file.
191+
192+
In my main file:
193+
194+
```python
195+
import calculator
196+
197+
result1 = calculator.add(2, 3)
198+
print(result1)
199+
200+
result2 = calculator.subtract(6, 3)
201+
print(result2)
202+
203+
result3 = calculator.multiply(10, 3)
204+
print(result3)
205+
206+
result4 = calculator.divide(6, 3)
207+
print(result4)
208+
```
209+
210+
**Output**
211+
```
212+
5
213+
3
214+
30
215+
2.0
216+
```
217+
218+
In our example, we have only used functions and constants from a module. However a module may also contain classes or any other definitions.
219+
220+
Now, we can already sense how useful a module is. It helps us in better code management and improves the reusability of our code.

0 commit comments

Comments
 (0)