NumPy Arrays
- 02:31
Understand NumPy arrays, including NumPy's compared to lists, and how to create and manipulate NumPy arrays.
Downloads
No associated resources to download.
Glossary
lists NumPy Numpy arrays PythonTranscript
You're already familiar with lists from the previous lessons. NumPy arrays are this new type of object that we're going to talk about and in a lot of ways. And a NumPy array is similar to a list. One difference is that all objects in a NumPy array must be the same type of object. So you can't mix strings and floats, you can't mix floats and integer. All of the objects in a NumPy array must be the same type. You can create a NumPy array by passing a list into the array function. So let's create a new array and call it array one. I'm going to use the array function. So the array function is inside of the NumPy package, and we've created the alias np. So first, I need to point my code toward NumPy. So I'm gonna write np to tell it what package we're getting this code from, and then I'm gonna write the array function. And then inside these parentheses, I'm gonna pass a standard list. So now when I print that array, you can see that the list has been transformed into an array, and you can see that because of the formatting of the output. If you have an object and you're not sure of the type, then you can just use the type function and pass it the object. And that'll tell me that this is a NumPy array. If you try to create a NumPy array with different types of objects, NumPy will automatically convert all of the objects into the same data type. To see that let's create array two. And here I'm passing a list into the array function that contains integer 1, 2, 4, and 5, and then the string 3. When I print that array, you can see because of the quotes on the outside of each object, that NumPy has automatically converted each of these objects into strings. And that means that now you cannot use 1, 2, 4, and 5 for numerical operations. For another example, I'm gonna make array 3.
And here I'm passing a list containing integer 1, 2, 4, and five, and 3 in float format, which means it has that decimal point. So now, if I print array 3, NumPy has converted each of these objects into float format, which you can see because it's added the decimal at the end of each number.