开始使用免费开始使用

Python 模块化的真实案例

在幻灯片中,我们介绍了用 Python 编写模块化代码的 3 种方式:packagesclassesmethods。作为参考,您可以查看下面我们一起回顾的示例代码。

# 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 语句以加载 numpy package
  • 使用 numpyarray class 来定义 arr
  • 使用 arrsort method 对该 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)
编辑并运行代码