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!

相关推荐
witAI16 分钟前
**AI仿真人剧制作2025推荐,专业团队与创新技术引领未来**
人工智能·python
♪-Interpretation33 分钟前
第五节:Python的流程控制语句
python
shark22222221 小时前
Python 爬虫实战案例 - 获取社交平台事件热度并进行影响分析
开发语言·爬虫·python
m0_564876842 小时前
提示词工程手册学习
人工智能·python·深度学习·学习
波诺波2 小时前
p1项目system_model.py代码
开发语言·python
静心观复2 小时前
Python 虚拟环境与 pipx 详解
开发语言·python
卷心菜狗2 小时前
Re.从零开始使用Python构建本地大模型网页智慧聊天机器人
开发语言·python·机器人
书到用时方恨少!3 小时前
Python NumPy 使用指南:科学计算的基石
开发语言·python·numpy
L-李俊漩4 小时前
荆华密算 面试题(大模型开发)
python
小陈工4 小时前
Python Web开发入门(十):数据库迁移与版本管理——让数据库变更可控可回滚
前端·数据库·人工智能·python·sql·云原生·架构