Python 模块化的真实案例
在幻灯片中,我们介绍了用 Python 编写模块化代码的 3 种方式:packages、classes 和 methods。作为参考,您可以查看下面我们一起回顾的示例代码。
# Import the pandas PACKAGE
import pandas as pd
# Create some example data
data = {'x': [1, 2, 3, 4],
'y': [20.1, 62.5, 34.8, 42.7]}
# Create a dataframe CLASS object
df = pd.DataFrame(data)
# Use the plot METHOD
df.plot('x', 'y')
在本练习中,您将使用流行的 package numpy 中的一个 class 和一个 method。
本练习是课程的一部分
Python 中的软件工程原理
练习说明
- 完成
import语句以加载numpypackage。 - 使用
numpy的arrayclass 来定义arr。 - 使用
arr的sortmethod 对该numpy数组进行排序。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# import the numpy package
import ____ as np
# create an array class object
arr = np.____([8, 6, 7, 5, 3, 0, 9])
# use the sort method
arr.____()
# print the sorted array
print(arr)