Introduction - SOLVED
Contents
Introduction - SOLVED#
In this lecture, we will introduce you to the numerical Python library numpy. Numpy is used almost in every field of science and engineering. This library provides the universal standard for working with numerical data in Python, and it also the essential scientific tool.
OUTLINE:
How to create basic arrays
Basic array operations
Indices and slicing.
Iterating over a numpy array
Sorting, adding and removing elements of an array
Broadcasting.
Using a particular axis.
Summary Exercises.
First we import numpy#
import numpy as np # we will be using numpy via the abbreviation np
New lists that allow multi-dimensional indices#
So far we have introduced lists, the equivalent of a list in numpy is a numpy array.
In general, the array is a grid of values containing information about the raw data, such as the location and the interpratation of the data element.
Let’s build our first numpy array:
my_first_numpy = np.array([1, 10, 100, 1000])
Similarly to lists we can assess its elements by calling the indices, namely:
first_element = my_first_numpy[0]
print(f"This is the first element of the above numpy array: {first_element}")
This is the first element of the above numpy array: 1
first_element = my_first_numpy[-1]
print(f"This is the last element of the above numpy array: {first_element}")
This is the last element of the above numpy array: 1000
Exercise 0.1#
In the cell below, create a numpy array with 5 elements, then print its first and last element.
my_shiny_new_array = np.array([3,1,4,5,9])
element_1 = my_shiny_new_array[0]
print(f"This is the first element of the above numpy array: {element_1}")
print(f"This is the last element of the above numpy array: {my_shiny_new_array[-1]}")
This is the first element of the above numpy array: 3
This is the last element of the above numpy array: 9