Introduction to Arrays, Part 2
Introduction
The purpose of this mini-lab is to practice creating and working with
arrays by introducing the NumPy module in Python. The exercises in this
mini-lab will introduce copying arrays, shapes of arrays, iterating over
arrays, and searching through arrays.
Exercises
- Read through the following NumPy Tutorials from W3 Schools, working through the
exercises in each section:
- Create a new program with your solutions to the following:
- Write a statement to create a 1-dimensional array with 10 random integer
values from 0 to 100.
- Print your array elements by using an indexed for loop.
- Print your array elements by using an iterator. (That is, use
the
nditer
function in your for loop.)
- Using either type of for loop, write the statements you would
need to sum the elements of your array and then print the sum.
- Using either type of for loop, write the statements you would need to
find the value of the maximum element in your array. Print this
value.
- Write a statement to create a 2-dimensional array of random
integers between 0 and 3,
with size 5x7 and print your array.
- Use nested for loops to print the elements of your 2-dim array, each
on its own line. (Stop and Think: How many lines
will you be printing?)
- Use an iterator to print the elements of your 2-dim array.
- Write the statement(s) you would need to count how many 2's are
in your 2-dim array. Print the total number of 2's.
- Write the statement(s) you would need to replace all of the 2's
with 5's. (Hints: Try doing this with two different
methods; the first method with nested for loops, an if statement,
and an assignment statement, the
second method with the
numpy.where
function to get the
indices of the elements to be replaced.
Submit
When you are satisfied with your statements, make sure
your name is on your file, and then submit the file you created for this mini-lab via
Kit.