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

  1. Read through the following NumPy Tutorials from W3 Schools, working through the exercises in each section:

  2. Create a new program with your solutions to the following:
    1. Write a statement to create a 1-dimensional array with 10 random integer values from 0 to 100.
    2. Print your array elements by using an indexed for loop.
    3. Print your array elements by using an iterator. (That is, use the nditer function in your for loop.)
    4. Using either type of for loop, write the statements you would need to sum the elements of your array and then print the sum.
    5. 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.
    6. Write a statement to create a 2-dimensional array of random integers between 0 and 3, with size 5x7 and print your array.
    7. 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?)
    8. Use an iterator to print the elements of your 2-dim array.
    9. 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.
    10. 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