Skip to content

Latest commit

 

History

History
196 lines (131 loc) · 4.5 KB

README.md

File metadata and controls

196 lines (131 loc) · 4.5 KB

Chapter 8: Modules

Table of Contents

Introduction to Modules

A module is a file containing Python definitions and statements. Modules allow you to organize your code into separate files, making it easier to manage and reuse. You can import a module into another module or script to use its functions, classes, and variables.

Example: Using a Module

# Save this code in a file named mymodule.py
def greet(name):
    return f"Hello, {name}!"

# Save this code in another file
import mymodule

print(mymodule.greet("Alice"))

Output:

Hello, Alice!

Explanation: The mymodule.py file contains a function greet. We import the mymodule in another file and use the greet function.

Creating Your Own Modules

You can create your own modules by saving Python code in a file with a .py extension. The file name becomes the module name.

Example: Creating a Module

# Save this code in a file named mymodule.py
def factorial(x):
    result = 1

    for i in range(1, x + 1):
        result *= i

    return result

pi = 3.1415

Explanation: The mymodule.py file contains a function factorial and a variable pi. This file can be imported as a module in other scripts.

Importing and Using Modules

You can import a module using the import statement. You can also use from ... import ... to import specific functions or variables from a module.

Example: Importing a Module

# Save this code in a file named main.py
import mymodule

print(mymodule.factorial(5))
print(mymodule.pi)

Output:

120
3.1415

Explanation: We import the mymodule and use its factorial function and pi variable.

Example: Importing Specific Functions or Variables

# Save this code in a file named main.py
from mymodule import factorial, pi

print(factorial(4))
print(pi)

Output:

24
3.1415

Explanation: We import the factorial function and pi variable directly from the mymodule.

Example: Importing All Functions and Variables

# Save this code in a file named main.py
from mymodule import *

print(factorial(6))
print(pi)

Output:

720
3.1415

Explanation: We import all functions and variables from the mymodule using the * wildcard.

Standard Library Modules and Their Usage

Python comes with a standard library of modules that provide various functionalities. You can import and use these modules in your scripts.

Example: Using the math Module

import math

print(math.pi)
print(math.e)
print(math.sqrt(16))
print(math.factorial(5))

Output:

3.141592653589793
2.718281828459045
4.0
120

Explanation: The math module provides mathematical functions and constants. We use the pi and e constants, and the sqrt and factorial functions.

Example: Using the random Module

import random

print(random.randint(1, 10))
print(random.choice(['apple', 'banana', 'cherry']))

Output:

7
banana

Explanation: The random module provides functions for generating random numbers and selecting random items from a list. We use the randint function to generate a random integer and the choice function to select a random item from a list.

Example: Using the datetime Module

import datetime

now = datetime.datetime.now()
print(now)
print(now.strftime("%Y-%m-%d %H:%M:%S"))

Output:

2023-10-05 14:30:00
2023-10-05 14:30:00

Explanation: The datetime module provides functions for working with dates and times. We use the now function to get the current date and time, and the strftime function to format the date and time.

Summary

In this chapter, we covered modules, including creating your own modules, importing and using modules, and standard library modules and their usage.

Tasks

  1. Create a module with a function that calculates the area of a circle.
  2. Create a module with a function that checks if a number is even or odd.
  3. Import and use the math module to calculate the square root of a number.
  4. Import and use the random module to generate a random number between 1 and 100.
  5. Import and use the datetime module to print the current date and time.

Next Chapter: Conclusion and Further Learning