> ## Documentation Index
> Fetch the complete documentation index at: https://ai.tharung.in/llms.txt
> Use this file to discover all available pages before exploring further.

# Python Modules

## 1. What is a Module?

A **module** is a Python file (`.py`) that contains functions, variables, or classes that can be reused in other Python programs.

Example:

```python theme={null}
# math_utils.py

def add(a, b):
    return a + b

def subtract(a, b):
    return a - b
```

You can import and use it in another file:

```python theme={null}
import math_utils

print(math_utils.add(5, 3))
```

**Output:**

```text theme={null}
8
```

### Advantages of Modules

* Code reusability
* Better organization
* Easier debugging
* Avoid code duplication
* Makes projects easier to maintain

***

# 2. Importing Built-in Modules

Python provides many **built-in modules** that come with the language.

### Example: `math`

```python theme={null}
import math

print(math.sqrt(25))
print(math.pi)
```

**Output**

```text theme={null}
5.0
3.141592653589793
```

Common functions:

| Function            | Description |
| ------------------- | ----------- |
| `math.sqrt(x)`      | Square root |
| `math.pow(x, y)`    | Power       |
| `math.ceil(x)`      | Round up    |
| `math.floor(x)`     | Round down  |
| `math.factorial(x)` | Factorial   |

***

### Example: `random`

```python theme={null}
import random

print(random.randint(1, 10))
```

Generates a random integer between 1 and 10.

***

### Example: `datetime`

```python theme={null}
import datetime

today = datetime.datetime.now()

print(today)
```

Shows the current date and time.

***

# 3. Different Ways to Import Modules

### a) Import entire module

```python theme={null}
import math

print(math.sqrt(16))
```

***

### b) Import specific function

```python theme={null}
from math import sqrt

print(sqrt(16))
```

***

### c) Import multiple functions

```python theme={null}
from math import sqrt, factorial

print(sqrt(36))
print(factorial(5))
```

***

### d) Import with alias

```python theme={null}
import math as m

print(m.pi)
```

Alias (`m`) is a shorter name.

***

### e) Import everything (Not recommended)

```python theme={null}
from math import *

print(sqrt(64))
```

Avoid this in large projects because it can create naming conflicts.

***

# 4. Third-Party Modules

Third-party modules are created by other developers and installed using **pip**.

### Install

```bash theme={null}
pip install requests
```

***

### Example

```python theme={null}
import requests

response = requests.get("https://api.github.com")

print(response.status_code)
```

***

Some popular third-party modules:

| Module       | Purpose             |
| ------------ | ------------------- |
| `requests`   | HTTP requests       |
| `numpy`      | Numerical computing |
| `pandas`     | Data analysis       |
| `matplotlib` | Data visualization  |
| `flask`      | Web development     |

***

# 5. Creating Your Own Module (`utils.py`)

Suppose you have a file named:

```text theme={null}
utils.py
```

Contents:

```python theme={null}
# utils.py

def greet(name):
    return f"Hello, {name}"

def square(num):
    return num * num
```

***

Another file:

```text theme={null}
main.py
```

```python theme={null}
import utils

print(utils.greet("Alice"))
print(utils.square(6))
```

**Output**

```text theme={null}
Hello, Alice
36
```

***

### Import specific function

```python theme={null}
from utils import greet

print(greet("Bob"))
```

***

### Alias

```python theme={null}
import utils as u

print(u.square(9))
```

***
