Python modularity in the wild
In the slides, we covered 3 ways that you can write modular code with Python: packages, classes, and methods. For reference, you can see the example code we reviewed below.
# 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')
In this exercise, you'll utilize a class & a method from the popular package numpy
.
This exercise is part of the course
Software Engineering Principles in Python
Exercise instructions
- Complete the
import
statement to load thenumpy
package. - Use
numpy
'sarray
class to definearr
. - Use
arr
'ssort
method to sort thenumpy
array.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# 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)