TME 310 - Computational Physical Modeling
University of Washington Tacoma
Recall that lists are ordered collections of objects:
A) 1
B) False
C) [2, 'three']
Lists can contain mixed data types
Performing operations on lists happens one element at a time
Even though lists can contain mixed data types, not every operation we do will work with every data type
Cell In[2], line 1 x = [1, 2, 3, 4, 5, 6, 7, 8, "nine"] <--- mixed data types ^ SyntaxError: invalid syntax
Like Matplotlib, Numpy is an external library that we can import and use in Python:
Numpy allows us to create n-dimensional arrays.
But for today, just 1-D
Numpy arrays can be created from lists using the numpy.array() function. Numpy will automatically convert data into compatible dtypes if possible.
[1 2 3 4 5] int64
[ 6. 7. 8. 9. 10.] float64
Numpy has built in functions to create arrays:
x: [ 0. 20. 40. 60. 80. 100.]
y: [0. 0. 0. 0.]
z: [1. 1. 1. 1. 1.]
We can access array contents via indexing just like lists:
x: [ 0. 20. 40. 60. 80. 100.] <--- defined on previous slide
np.float64(0.0)
Unlike lists, we can perform operations on entire arrays at once.
y_arr: [ 1 4 9 16 25 36 49 64 81]
