Learn the basics of Python 3-Chapter 7: Modules

1.Modules Python Introduction

In the world of programming, we care a lot about making code reusable. In most cases, we write code so that it can be reusable for ourselves. But sometimes we share code that's helpful across a broad range of situations.

In this lesson, we'll explore how to use tools other people have built in Python that are not included automatically for you when you install Python. Python allows us to package code into files or sets of files called modules.

Usually, to use a module in a file, the basic syntax you need at the top of that file is:

from module_name import object_name

One common library that comes as part of the Python Standard Library is datetime. datetime helps you work with dates and times in Python.Let's get started by importing and using the datetime module.

python 复制代码
# Import datetime from datetime below:
from datetime import datetime

# Create a variable current_time and set it equal to datetime.now().
current_time = datetime.now()

# Print out current_time.
print(current_time)
# =>2024-02-05 15:12:29.531529

2.Modules Python Random

Another one of the most commonly used is random which allows you to generate numbers or select items at random. With random, we'll be using more than one piece of the module's functionality, so the import syntax will look like:

import random

We'll work with two common random functions:

random.choice() which takes a list as an argument and returns a number from the list

random.randint() which takes two numbers as arguments and generates a random number between the two numbers you passed in

python 复制代码
# import the random library
import random

# Create a variable random_list and set it equal to an empty list
random_list = []

# Turn the empty list into a list comprehension that uses random.randint() to generate a random integer between 1 and
# 100 (inclusive) for each number in range(101).
random_list = [random.randint(1, 100) for i in range(101)]

# Create a new variable randomer_number and set it equal to random.choice() with random_list as an argument.
# Print randomer_number below:
randomer_number = random.choice(random_list)

# Print randomer_number out to see what number was picked!
print(randomer_number)
# 80(random num)

3.Modules Python Namespaces

Notice that when we want to invoke the randint() function we call random.randint(). This is default behavior where Python offers a namespace for the module. A namespace isolates the functions, classes, and variables defined in the module from the code in the file doing the importing. Your local namespace, meanwhile, is where your code is run.

import pyplot from the module matplotlib with the alias plt.(pycharm:File->Settings->project->python interpreter -> + -> matplotlib->Install Package)

python 复制代码
# Import random below the other import statements. It's best to keep all imports at the top of your file.
from matplotlib import pyplot as plt
import random

# Create a variable numbers_a and set it equal to the range of numbers 1 through 12 (inclusive).
numbers_a = range(1, 13)

# Create a variable numbers_b and set it equal to a random sample of twelve numbers within range(1000).
# Feel free to print numbers_b to see your random sample of numbers.
numbers_b = random.sample(range(1000), 12)
print(numbers_b)
# [146, 374, 399, 117, 478, 462, 144, 335, 690, 239, 627, 675](ramdom num)

# Now let's plot these number sets against each other using plt. Call plt.plot() with your two variables as its
# arguments.
plt.plot(numbers_a, numbers_b)

# Now call plt.show() and run your code! You should see a graph of random numbers displayed. You've used two Python
# modules to accomplish this (random and matplotlib).
plt.show()

4.Modules Python Decimals

Let's say you are writing software that handles monetary transactions. If you used Python's built-in floating-point arithmetic to calculate a sum, it would result in a weirdly formatted number.

python 复制代码
cost_of_gum = 0.10
cost_of_gumdrop = 0.35

cost_of_transaction = cost_of_gum + cost_of_gumdrop
print(cost_of_transaction)
# =>0.44999999999999996

Being familiar with rounding errors in floating-point arithmetic you want to use a data type that performs decimal arithmetic more accurately. You could do the following:

python 复制代码
from decimal import Decimal

cost_of_gum = Decimal('0.10')
cost_of_gumdrop = Decimal('0.35')

cost_of_transaction = cost_of_gum + cost_of_gumdrop
print(cost_of_transaction)
# => 0.45

# Use Decimal to make two_decimal_points only have two decimals points and four_decimal_points to only have four decimal points. Import Decimal below:
from decimal import Decimal

# Fix the floating point math below:
two_decimal_points = Decimal('0.2') + Decimal('0.69')
print(two_decimal_points)
# =>0.89

four_decimal_points = Decimal('0.53') * Decimal('0.65')
print(four_decimal_points)
# =>0.3445

5.Modules Python Files and Scope

You may remember the concept of scope from when you were learning about functions in Python. If a variable is defined inside of a function, it will not be accessible outside of the function.

Scope also applies to classes and to the files you are working within. Tab over to library.py and define a function always_three() with no parameters that returns 3.

python 复制代码
# Add your always_three() function below:
# create library.py
def always_three():
    return 3

Call your always_three() function in script.py. Check out that error message you get in the output terminal and the consequences of file scope. Resolve the error with an import statement at the top of script.py that imports your function from library. Run your code and watch import do its magic!

python 复制代码
# Import library below:
# create script.py
from library import always_three

# Call your function below:
always_three()

6.Modules Python Review

In this lesson, we covered some of the Python Standard Library, but you can explore all the modules that come packaged with every installation of Python at the Python Standard Library documentation.

This is just the beginning. Using a package manager (like conda or pip3), you can install any modules available on the Python Package Index. The sky's the limit!

相关推荐
咖啡の猫4 小时前
Python字典推导式
开发语言·python
曹文杰15190301124 小时前
2025 年大模型背景下应用统计本科 计算机方向 培养方案
python·线性代数·机器学习·学习方法
Wulida0099915 小时前
建筑物表面缺陷检测与识别:基于YOLO11-C3k2-Strip模型的智能检测系统
python
FJW0208145 小时前
Python_work4
开发语言·python
爱笑的眼睛116 小时前
从 Seq2Seq 到 Transformer++:深度解构与自构建现代机器翻译核心组件
java·人工智能·python·ai
yaoh.wang6 小时前
力扣(LeetCode) 88: 合并两个有序数组 - 解法思路
python·程序人生·算法·leetcode·面试·职场和发展·双指针
执笔论英雄6 小时前
【RL】slime创建actor的流程
python
吴佳浩 Alben6 小时前
Python入门指南(四)
开发语言·后端·python
小智RE0-走在路上7 小时前
Python学习笔记(8) --函数的多返回值,不同传参,匿名函数
笔记·python·学习
ZHSH.7 小时前
2026蓝桥杯备赛 | 赛事介绍及python基础(未完)
python·蓝桥杯·数据结构与算法