{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Lists and NumPy Arrays\n", "\n", "Before we do can do any data analysis, we must first collect a *data set*. For instance, if we want to get a good idea of the typical temperature in San Diego in December, we might collect a large number of recorded temperatures from the past several years. Naturally, we need some way of storing this collection of numbers. Of course, we could store each recorded temperature in its own variable:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "temp_dec_01_2005 = 65\n", "temp_dec_02_2005 = 68\n", "temp_dec_03_2005 = 63\n", "...\n", "temp_dec_31_2020 = 71" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This gets tedious quickly -- can you imagine trying to compute the average temperature this way? Your code would look something like `(temp_dec_01_2005 + temp_dec_02_2005 + ...) / n_days`, where `n_days` is the number of days in the data set. Instead, we'll use *container* data types -- lists and arrays -- that make it much easier to work with sequential data." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python comes with a data type that is purpose-built for storing sequential data: the *list*. Creating a list in Python is done by surrounding a group of items with square brackets, `[ ]`, and separating each item with a comma `,`, like so:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "temperatures = [65, 68, 63, 71]\n", "temperatures" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Any type of data is allowed inside a list (including other lists), and you can include variables:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = 42\n", "random_stuff = ['oranges', x, True, [1, 2, 3]]\n", "random_stuff" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lists are their own data type:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "type(random_stuff)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python's lists are versatile and easy to use, but code that uses lists can be *slow*. As data scientists, we will be working with sequences of millions, if not billions, of entries -- so speed is of the essence. Therefore, will use another type of collection to store our sequential data: the *array*." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## NumPy Arrays\n", "\n", "Arrays are like lists in that they store sequential data, but they optimized for the types of heavy calculations done in data science. They are blazing fast, and memory-efficient." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Arrays aren't included with Python, however.\n", "Remember that Python wasn't originally designed specifically for data scientists. Instead, it is a *general purpose* language, used by web developers, software engineers, artists, etc. So in order to give Python what it needs -- a way of efficiently working with large sequences of numbers -- a group of scientists independently developed an extension to Python called [NumPy](https://numpy.org/) (short for \"numeric python\")." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{figure} ../images/numpy-logo.svg\n", "---\n", "height: 150px\n", "name: my-figure\n", "---\n", "The *NumPy* logo.\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Tip**\n", "\n", "In case you're curious, it's pronounced \"num-pie\"." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Among other thing, NumPy provides a fast array data type. To use NumPy arrays, we'll need to import NumPy, just as we did with the `math` module in the previous chapter:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{margin}\n", "\n", "Note that while the `math` module is included with Python, *NumPy* is not (it has to be installed separately). But we import them the same way.\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The notation `as np` means that we are giving `numpy` a new, shorter name that will be faster to type. Whenever we want to use a function from the `numpy` package, such as the `sqrt` function, we'll write `np.sqrt` instead of `numpy.sqrt`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{margin}\n", "\n", "You could change `numpy`'s name to whatever you want, for instance: `import numpy as my_favorite_library`. However importing it as `np` is a *de facto* standard in data science. Don't import it as anything else unless you have a good reason, or else you'll confuse other people who read your code.\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's create an array. We do so by calling the `np.array()` function with a list of data:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "temperatures = np.array([65, 68, 63, 71])\n", "temperatures" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note the square brackets! The brackets appear because we are first creating a standard Python *list*. We then pass the list into the `np.array` function as its only argument. In fact, we could create the same array in two steps:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# this is a list\n", "temperatures_list = [65, 68, 63, 71]\n", "\n", "# and this is an array\n", "temperatures_array = np.array(temperatures_list)\n", "temperatures_array" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Warning**\n", "\n", "A common mistake is to write `np.array([temperatures_list])` when attempting to create an array from a list. Do you see what is wrong with this? The square brackets are unnecessary! `temperatures_list` is already a list, so by writing `[temperatures_list]`, we are in fact creating a *list of lists*. The overall result is a two-dimensional array, which is probably not what you wanted. The right way to create an array from a list is to write `np.array(temperatures_list)`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Arrays are their own data type:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "type(temperatures)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The array we've created contains numbers, but arrays can also contain other types of data, like strings or bools. For example, we can make an array of strings:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.array(['this', 'is', 'also', 'fine'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*But* in order to maximize their efficiency, a single array should contain only a single data type. That is, we shouldn't mix ints with strings, for example. In fact, numpy will sometimes do surprising things to make sure that all of an array's elements have the same data type. \n", "Remember what happened when we evaluated expressions that contained both ints and float? The result was always a float. The same thing will happen if we try to make an array containing both ints and floats:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.array([1, 2, 3.0])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you look carefully, you'll see that there is a decimal after each number. Numpy has helpfully converted each of the numbers to floats -- even the ones that were provided as ints.\n", "\n", "The following example may be even more surprising:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.array([1, 2, 3, 'banana'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Numpy has \"helpfully\" converted the ints into strings so that all of the items in the list have the same type: string. Why did it convert the ints to strings instead of the string to an integer? There's no natural way to convert \"banana\" into an integer, but 2 naturally becomes the string `\"2\"`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "```{margin}\n", "No need to be worried about the weird looking \"dtype\" -- that just tells us that the data type it contains are stored as [unicode](https://en.wikipedia.org/wiki/Unicode) strings (`U`) with a maximum possible length of 21 characters (`21`).\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sometimes it is useful to know how many elements are in an array. We can determine this with the `len` function:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "arr = np.array([1, 2, 3])\n", "len(arr)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Array Functions and Methods" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Numpy arrays are not only useful because of their speed and memory efficiency -- they also come with plenty of helpful functions and methods for performing the most common data analysis operations, such as summing all of the entries of an array or computing their mean." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The NumPy functions can be called just like we called the math functions. Once we've imported the `numpy` library (abbreviated as `np`) we can just type `np.` followed by a function name to access the function. For instance, you can use the `np.mean` function to calculate the mean value of a sequence:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "example_array = np.array([1, 1, 2, 3, 3])\n", "np.mean(example_array)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are many functions, such as the `np.diff` function which calculates the difference between each consecutive pair of elements:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.diff(example_array)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Just like strings, arrays also have special *methods* that can perform calculations. A few useful ones are shown below:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "example_array.min()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "example_array.max()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "example_array.sum()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "example_array.mean()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Don't worry, you don't need to memorize all of the different functions/methods (there are a lot!) -- we'll include references when necessary." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What is the difference between a *function* and a *method*? In short, a method is a function that is \"attached\" to a particular instance of a type, such as a particular array. An example of a function call is `np.diff(example_array)`; an example of a method call is `example_array.min()`. Notice that in the method call, the variable name comes first.\n", "\n", "Numpy organizes its operations into function and methods. It isn't always clear why something is a method and not a function, or *vice versa*. In fact, some operations are available *both* as functions and methods. For instance, numpy provides both a function and a method for computing the mean:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# mean, as a function\n", "np.mean(example_array)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# mean, as a method\n", "example_array.mean()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Methods are useful for a variety of reasons, one being that they enable a coding practice called {dterm}`method chaining`. For example, suppose we wish to round each number in an array of floats and then compute their mean. We can use the `.round` method to round each number in the array. Called with the argument of `0`, it will round each number to zero decimal places (that is, to the nearest whole number):" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "arr = np.array([3.14, 2.71, 99.99])\n", "rounded = np.round(arr, 0)\n", "rounded" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then we can compute the mean with the `.mean()` method:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "rounded.mean()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We could have done this all in one line as follows:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "arr.round(0).mean()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This works because the result of each method is returned and used for the next method. That is, `arr.round(0)` returns an array of rounded numbers. This array is never given a name, but it is used for the next method call to `.mean()`.\n", "\n", "Method chaining is sometimes desirable because it concisely captures the \"flow\" of the calculation. We'll see a lot of method chaining in the coming chapters." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example\n", "\n", "Every year, the programming community forum [StackOverflow](http://stackoverflow.com) [surveys](https://insights.stackoverflow.com/survey/2019#overview) its users, asking them such important questions as: what is your salary? and, how many computer monitors are on your desk at home? The results are publicly available. Since many of those who respond are data scientists, we can use the data to get an idea of a typical data scientist's salary." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "remove-cell" ] }, "outputs": [], "source": [ "salaries = np.loadtxt('../../data/salaries.csv')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The variable `salaries` is a NumPy array containing the salaries of every US-based data scientist in the survey. How many were there? We can answer that with `len`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "len(salaries)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What was the mean salary?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "salaries.mean()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Nice. What about the *median* salary?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "raises-exception" ] }, "outputs": [], "source": [ "salaries.median()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Oops. It turns out that there is no method called \"median\" in numpy. There is, however, a `median` function:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.median(salaries)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that the median is about \\$10,000 less than the mean. As a data scientist would point out, the mean is more \"sensitive to outliers\", meaning that a few people who make a very large amount of money can skew the mean. Let's see what the largest salary is:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "salaries.max()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2 million dollars! Remember, though: these salaries are *self-reported*. Presumably no one checked to make sure that they were *accurate*." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Accessing array items" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An array is an *ordered sequence* of items. This means that there is a \"first\" element, a \"second\" element, and so on. Therefore, we can retrieve a particular element by providing its place in the order as a number known as the element's {dterm}`index` in the array. To do so, we write the array's name followed by square brackets. Inside the square brackets, we place the index of the element we wish to retrieve. There's one small catch: Python, like many programming languages but unlike most humans, starts counting from zero.\n", "\n", "For example, let's make a list of three names:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "names = np.array(['Xanthippe', 'Yvonne', 'Zelda'])\n", "names" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To get the first element out of the array, we use the following syntax:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "names[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To get the second, we write:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "names[1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And to get the third, we write:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "names[2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{margin}\n", "This business of starting at zero instead of one may seem strange, but it has its advantages. For a defense of this approach, see the famous essay *Why numbering should start at zero* by the renowned computer science Edsger Dijkstra.\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "How do we retrieve the last element of the list? We could use the list's length, subtracted by one:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "names[len(names) - 1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "But this is a lot of typing! Instead, here's a useful trick: if you use a negative number to retrieve an element, Python starts counting from the *back* of the array. So, for instance, to retrieve the last element we can also write:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "names[-1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The array above has only three things in it, and their indices are 0, 1, and 2. What happens if we try to access the list at an index that doesn't exist, such as 99?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "raises-exception" ] }, "outputs": [], "source": [ "names[99]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It should also be noted that the same syntax works for retrieving elements from Python lists:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lst = ['hi', 'i', 'am', 'a', 'list']\n", "lst[2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Element-wise operations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When working with a data set we often want to perform the same operation on all of the data set's elements at once. For example, given an array of temperatures in Fahrenheit, we might want to convert all of them to Celsius. Because of this, Numpy makes writing code that works with " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "array1 = np.array([1, 2, 3])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To subtract 3 from all of these numbers, we can simply write:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "array1 - 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To multiply each of the numbers by 2, we would write:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "array1 * 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And so on. In practice this means we could do something like convert an entire array of temperatures measured in Fahrenheit to Celsius by writing a single expression:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "temperatures_f = np.array([0.5, 32.0, 71.6, 212.0])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Remember that the formula for converting a measurement in Fahrenheit to Celsius is $C = (F - 32) * (5/9)$. Therefore:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "temperatures_c = (temperatures_f - 32) * (5 / 9)\n", "temperatures_c" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the above example, first `(temperatures_f - 32)` is evaluated and produces an array with 32 subtracted from every temperature. Then `(5 / 9)` is evaluated. Then then every element in the new array is multiplied by 5/9, producing the final output array." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also do element-wise operations between pairs of data from two arrays.\n", "\n", "For this to work, both arrays must have the same size. The arrays are then lined up next to eachother, and the operation is performed between every corresponding pair of elements. This is best demonstrated with some examples:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "array1 = np.array([1, 2, 3])\n", "array2 = np.array([2, 4, 6])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "array1 * array2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "array1 - array2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "array1**array2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Both paired element-wise operations and standalone element-wise operations can be used in the same expression, since we're always producing another array as a result of each expression." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "(array1 * 2) - array2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Watch out for the new errors you might encounter! Let's see what happens if our other array isn't the same size." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "raises-exception" ] }, "outputs": [], "source": [ "array_short = np.array([2,4])\n", "array1 * array_short" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The error message is a little cryptic -- what is this about \"broadcasting\"? Nevertheless, we can kind of understand that there is some issue with the \"shape\" of the two arrays not being compatible.\n", "In fact, this error is telling us that the first array has three elements but the other only has two, so the two arrays couldn't be pushed into the same shape." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Ranges" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Often times it's useful to create an array of consecutive numbers, such as:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.array([0, 1, 2, 3, 4, 5, 6, 7])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Rather than write this array by hand, we can use the `np.arange` function to do it for us:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.arange(8)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that just like indices, ranges will start at zero by default and exclude the last number. So calling `np.arange(12)`, for instance, will create an array with eleven elements whose first entry is 0 and whose last element is 11." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "While we saw an example of the range function being called with one argument, it can be called with one, two, or three arguments:\n", "\n", "- `np.arange(endpoint)` Consecutive integers from 1 to endpoint (exclusive)\n", "- `np.arange(start, endpoint)` Consecutive numbers from start to endpoint (exclusive), increasing by 1 each step.\n", "- `np.arange(start, endpoint, stepsize)` Consecutive numbers from start to endpoint (exclusive), changing by stepsize each step.\n", "\n", "Some example might make this clearer:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.arange(10)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.arange(5.5, 10)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.arange(0, 1, 0.2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.arange(-1, -4)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.arange(-1, -4, -1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The result of `np.arange` is an array like any other, so we can write things like:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.arange(5) + 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Question**:\n", " How would you use `np.arange` to create the array containing the first 6 powers of 2: 1, 2, 4, 8, 16, and 32?\n", "\n", "\n", "
Answer:`2**np.arange(6)`
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "## Summary\n", "\n", "- To get multiple pieces of data in one place, we create a **collection**. If the collection is ordered then it is a **sequence**.\n", "- Each item in a sequence has an **index** -- its position, starting at zero.\n", "- **Lists** are the most basic sequence, and are created by surrounding a group of items with square brackets and separating each item by commas: `[item, item, ...]`\n", "- **Arrays** are a sequence type from the NumPy library, and are created by passing a list into the `np.array` function: `np.array([item, item, ...])` `np.array(my_list)`\n", "- NumPy offers lots of additional functions that can be called on sequences. These can be accessed using `np.function_name(arguments, ...)`.\n", "- An item can be selected from an array by using brackets with the index of the item: `my_array[index]`\n", "- Arrays support **element-wise operations**, such as adding or multiplying all elements by a single number.\n", "- Arrays of the same length support paired element-wise operations between the two arrays, such as adding or multiplying each element in one array with each element in the same position of another array.\n", "- An array of numbers with constant spacing can be easily constructed using `np.arange`\n", "- A range will always *exclude* the endpoint -- so `np.arange(3)` will count `0 1 2`." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.6" } }, "nbformat": 4, "nbformat_minor": 4 }