DEV Community

Nicholus Mush
Nicholus Mush

Posted on

Modules and Packages

"""Lesson 9: Modules and Packages

This lesson introduces code organization with modules and built-in packages for reusable functionality.
"""

Importing a built-in module.

import math

print("Square root of 16:", math.sqrt(16))
print("Pi rounded:", round(math.pi, 3))

Enter fullscreen mode Exit fullscreen mode

Use a specific function from a module.

from datetime import datetime
print("Current date and time:", datetime.now())

# Create a small reusable utility function.

def format_price(amount):
    return f"${amount:,.2f}"

print(format_price(1234.5))

Enter fullscreen mode Exit fullscreen mode

Real-world example:

organizing helpers in separate files
Enter fullscreen mode Exit fullscreen mode

.

You can create your own module and import it by file name.

Top comments (0)