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!

相关推荐
DanCheng-studio1 小时前
网安毕业设计简单的方向答疑
python·毕业设计·毕设
轻抚酸~2 小时前
KNN(K近邻算法)-python实现
python·算法·近邻算法
独行soc3 小时前
2025年渗透测试面试题总结-264(题目+回答)
网络·python·安全·web安全·网络安全·渗透测试·安全狮
汤姆yu4 小时前
基于python的外卖配送及数据分析系统
开发语言·python·外卖分析
如何原谅奋力过但无声4 小时前
TensorFlow 1.x常用函数总结(持续更新)
人工智能·python·tensorflow
翔云 OCR API4 小时前
人脸识别API开发者对接代码示例
开发语言·人工智能·python·计算机视觉·ocr
AndrewHZ5 小时前
【图像处理基石】如何在图像中提取出基本形状,比如圆形,椭圆,方形等等?
图像处理·python·算法·计算机视觉·cv·形状提取
温轻舟6 小时前
Python自动办公工具05-Word表中相同内容的单元格自动合并
开发语言·python·word·自动化办公·温轻舟
习习.y7 小时前
python笔记梳理以及一些题目整理
开发语言·笔记·python
撸码猿7 小时前
《Python AI入门》第10章 拥抱AIGC——OpenAI API调用与Prompt工程实战
人工智能·python·aigc