NumPy arrays can be helpful for graphing with matplotlib!
NumPy arrays are similar to Python lists, which you saw earlier, but NumPy arrays are often favored for computational purposes due to their performance and certain capabilities they possess that built-in Python lists do not.
Let's take a look at some of the similarities & differences.
Let's try out using some of the array commands along with indexing and slicing.
1) Create a numpy array & print it.
2) Use Slicing & Indexing to print the 3 element of the array.
Coding Assistance:
Click these links if you need help creating the numpy array or on the slicing/indexing.
The command "arange" creates a "street" of mailboxes where the value inside each mailbox follows the requested range.
Scratchpad for Code Testing While Watching the Video
Let's try out using "arange" in a variety of problems to learn about the parameters and which ones can be used as default values.
ARANGE Problems #1 & 2
ARANGE #1
1) Create an array of integers going from 0 to 4 using the np.arange command.
2) Can you code this same array using the default values for start and step?
ARANGE #2
3) Let's code np.arange (10).
4) Now, alter this code to find out what we should include in parenthesis to get the value of 10 included.
5) What additional code should we add to print the length of x?
Understanding code syntax can be very important; but also you must understand when the syntax isn't correct. The process of finding errors in the code and fixing them is called "debugging." See if you can spot the error(s) below.
ARANGE #3 Debugging
ARANGE #3 Practice
Fix the error(s) in this code.
The "linspace" command creates a "street" of mailboxes where the value inside each mailbox follows the requested range.
Watch the Video for an introduction to Linspace Numpy Arrays
LINSPACE- Scratchpad for Code Testing While Watching the Video
Let's try using your knowledge about the linspace command to set up various arrays with the conditions set below.
LINSPACE #1 Practice
1) Change this code to create an array going from 15.3 ending at 101.7 with 14 buckets.
2) What code should you add to print the length of x?
LINSPACE #2 Practice
1) Write the code to create an array from 3.5 to 300.02 with a 99 buckets.
2) Add a line of code to get the 3rd item in the array.
3) Add a line of code to get the last item in the array.