summaryrefslogtreecommitdiff
path: root/tutorials/1_06_arrays.md
diff options
context:
space:
mode:
Diffstat (limited to 'tutorials/1_06_arrays.md')
-rw-r--r--tutorials/1_06_arrays.md56
1 files changed, 43 insertions, 13 deletions
diff --git a/tutorials/1_06_arrays.md b/tutorials/1_06_arrays.md
index b16e7fc..76dd1ee 100644
--- a/tutorials/1_06_arrays.md
+++ b/tutorials/1_06_arrays.md
@@ -2,14 +2,15 @@
In computer programming, an array is a structure for storing and retrieving data. We often talk about an array as if it were a grid in space, with each cell storing one element of the data. For instance, if each element of the data were a number, we might visualize a “one-dimensional” array like a list:
-| 1 | 5 | 2 | 0 |
+| 1 | 5 | 2 | 0 |
+| --- | --- | --- | --- |
A two-dimensional array would be like a table:
-| 1 | 5 | 2 | 0 |
-| 8 | 3 | 6 | 1 |
-| 1 | 7 | 2 | 9 |
-
+| 1 | 5 | 2 | 0 |
+| --- | --- | --- | --- |
+| 8 | 3 | 6 | 1 |
+| 1 | 7 | 2 | 9 |
A three-dimensional array would be like a set of tables, perhaps stacked as though they were printed on separate pages.
@@ -17,30 +18,59 @@ A three-dimensional array would be like a set of tables, perhaps stacked as thou
---
-In this tutorial we will be introducing arrays and we will be using the numpy library. Arrays, lists, vectors, matrices, sets - You might've heard of them before, they all store data. In programming, an array is a variable that can hold more than one value at a time. We will be using the Numpy python library to create arrays.
+In this tutorial we will be introducing arrays and we will be using the numpy library. Arrays, lists, vectors, matrices, sets - You might've heard of them before, they all store data. In programming, an array is a variable that can hold more than one value at a time. We will be using the Numpy python library to create arrays. Since we already have installed Numpy previously, we can start using the package.
-Since we already have installed Numpy previously, we can start using the package.
+Before importing our first package, let's as ourselves *what is a package?* A package can be thought of as pre-written python code that we can re-use. This means the for every script that we write in python we need to tell it to use a certain package. We call this importing a package.
-## Import Numpy
+## Importing Numpy
When using packages in python, we need to let it know what package we will be using. This is called importing. To import numpy we need to declare it a the start of a script as follows:
```python
import numpy as np
```
-<code> import </code> calls for a library to use, in our case it is Numpy.
-<code> as </code> gives the library an alias in your script. It's common convention in Python programming to make the code shorter and more readable. We will be using *np* as it's a standard using in many projects.
+- `import` - calls for a library to use, in our case it is Numpy.
+- `as` - gives the library an alias in your script. It's common convention in Python programming to make the code shorter and more readable. We will be using *np* as it's a standard using in many projects.
# Creating arrays
-Now that we have imported the library we can create a one dimensional array or *vector*
+Now that we have imported the library we can create a one dimensional array or *vector* with three elements.
```python
x = np.array([1,2,3])
```
-To create a *matrix* we can nest the arrays to create a two dimensional array.
+To create a *matrix* we can nest the arrays to create a two dimensional array. This is done as follows.
```python
-matrix = np.array([[1,2,3],[4,5,6],[7,8,9]])
+matrix = np.array([[1,2,3],
+ [4,5,6],
+ [7,8,9]])
```
*Note: for every array we nest, we get a new dimension in our data structure.*
+## Numpy array creation functions
+### np.arange
+The `np.arange()` function returns an array with evenly spaced values within a specified range. It is similar to the built-in `range()` function in Python but returns a Numpy array instead of a list. The parameters for this function are the start value (inclusive), the stop value (exclusive), and the step size. If the step size is not provided, it defaults to 1.
+
+```python
+>>> np.arange(4)
+array([0. , 1., 2., 3. ])
+```
+
+In this example, `np.arange(4)` generates an array starting from 0 and ending before 4, with a step size of 1.
+
+### np.linspace
+The `np.linspace()` function returns an array of evenly spaced values over a specified range. Unlike `np.arange()`, which uses a step size to define the spacing between elements, `np.linspace()` uses the number of values you want to generate and calculates the spacing automatically. It accepts three parameters: the start value, the stop value, and the number of samples.
+
+```python
+>>> np.linspace(1., 4., 6)
+array([1. , 1.6, 2.2, 2.8, 3.4, 4. ])
+```
+
+In this example, `np.linspace(1., 4., 6)` generates 6 evenly spaced values between 1. and 4., including both endpoints.
+
+Try this and see what happens:
+
+```python
+x = np.linspace(0,100,101)
+y = np.sin(x)
+```