Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Let's create a study log that we will use to track the minutes we will study during the #100DaysOfCode challenge
Learn More
My Notes for Introducing Arrays
## Differences between lists and NumPy Arrays
* An array's size is immutable. You cannot append, insert or remove elements, like you can with a list.
* All of an array's elements must be of the same [data type](https://docs.scipy.org/doc/numpy1.14.0/user/basics.types.html).
* A NumPy array behaves in a Pythonic fashion. You can `len(my_array)` just like you would assume.

0:00
So how did you do with your reflection?

0:02
Now, here's what I came up with.

0:04
So, my first point is, an arrays size is immutable, you can't change it right?

0:09
So you can't append, insert, or

0:11
remove elements like you can with a list and we showed that down here.

0:16
All of an array's elements must be of the same data type.

0:19
I went and did a search for the different data types that are available, and

0:22
I made a little link here.

0:23
I like to use those as bookmarks, and

0:24
we'll actually use this one here just in a bit.

0:26
And then finally, a NumPy array behaves in a Pythonic fashion.

0:31
You can use len(my_array) just like you'd assume.

0:34
Awesome, how did you do?

0:36
That feel good?

0:37
All right, are you ready?

0:38
Let's build out our study log.

0:40
We should track how many minutes we studied for each day.

0:44
Now we know that there are exactly 100 days in the challenge and

0:48
since we can't add or

0:49
remove entries we know that the array that we create will need to be 100 elements.

0:55
Now before when we created our array we did it from an interable, right?

0:59
Remember we did the this np.array and we passed in this list here.

1:03
But in this case, we don't have an interable.

1:06
We don't really have any data, but we do know that we eventually will.

1:11
This is a common enough problem that there is a provided solution.

1:15
What we what basically is 100 element array where each element will

1:20
represent the minutes that we coded each day.

1:23
And we want each of those elements to be initialized with the value of 0.

1:29
Lucky for us, there's a helper method called zeros that does just that.

1:34
So, let's do it, let's create a new array called study_minutes.

1:38
And it's right off of that package, so np.zeros.

1:42
And the first parameter is the shape or size, so we want 100, awesome.

1:48
And if we look at our study minute array,

1:51
we can do that just by putting it right underneath here.

1:54
So study_minutes, and there we go.

1:57
100 elements, a 100 zeros.

1:59
And you'll see that these 0s have this dot here and

2:03
that's showing that these are floating point numbers.

2:06
Now, one thing that we can do with a Jupyter Notebook is to get some additional

2:10
information.

2:11
And we can do that using the whos command.

2:14
WHOS, this magic command, whos.

2:18
If we do that, we can find the study_minutes array and

2:20
we can see that it's 100 elements and it's of type float64.

2:24
That must be with the default that's happening, all right.

2:26
So it takes 64bits or 8 bytes to store each of those.

2:31
But we know something about these values, don't we?

2:34
We know that there are 60 minutes in an hour and that there are 24 hours in a day.

2:40
And therefore, we know that the max value could only be 1440,

2:44
which really isn't too large of a number.

2:49
Let's go ahead and use that link from before and

2:51
see what other data types are available.

2:53
So let's come back up to these notes, I'm gonna go ahead and click this.

2:59
Okay, so these are the different ranges of values that things have and

3:03
we're at float64 right now.

3:06
So double precision float, there is signed bit.

3:09
We don't need all of that, that's a lot of precision.

3:12
We just need minutes up to 1440, not portions of the minute.

3:16
So we don't need a floating point of all.

3:18
So we're probably up in this integer area up here, right?

3:20
So integers, right?

3:22
Those are whole numbers and in fact, you can't study negative minutes in a day,

3:27
so we need an unsigned integer that will be always positive.

3:30
So these go negative, we only need zero forward, right?

3:34
And so we need 1440, so

3:38
this first one is uint that's again unsigned integer of 8 bits.

3:44
We can probably get it here, right, because this 1440's in between here.

3:47
So we can use this uint16, so

3:53
the data types are available as np.the name of the thing afterwards.

3:57
So we can use that, so let's flip back to our example.

4:02
We're down here at our study_minutes and

4:06
let's go ahead an we can put in our data

4:10
type is np.uint16.

4:14
And now remember this was at 800 bytes.

4:19
So it took 800 bytes before so let's go ahead and let's specify the data type.

4:23
Let me go ahead and run this one more time, and

4:24
you'll see the dots are now gone.

4:26
Now if I rerun this who is with a Ctrl+Enter,

4:30
we'll see that it's now 200 bytes.

4:34
Now, this time what we did is probably a little bit of premature optimization.

4:38
We only have 100 entries,

4:40
but I want you to imagine these arrays as enormously large.

4:45
They tend to get that way as you work with larger and larger data sets.

4:48
Constraining that the proper type can save a lot of space, and

4:52
in turn, speed by specifying a data type to meet your needs.

4:56
I've started this challenge already, and on the first day I plugged two and

5:00
a half hours into it.

5:02
Now you only need to do one hour each day, but I was super into the challenge, and

5:06
I hope you will be too.

5:07
So 2.5 hours is 60 plus 60, 120, plus 30, 150, okay.

5:13
So I'm gonna set my minutes for the first day.

5:17
So an array element can be set by its index just like list.

5:21
And just like list, the indices are zero based, right?

5:25
Remember, the first element is 0, the second is 1 and so on.

5:28
So let's do that, let's set this.

5:31
We'll say study_minutes[0] and we'll set that to our 150 and

5:39
you can retrieve the value also by using the index.

5:44
So we'll say first_day_minutes

5:50
= study_minutes [0].

5:54
And if we take a look at first_day_minutes, see that 150 came back.

6:01
That's interesting, though, isn't it?

6:03
We know that when we put our value of 150, when we put this in here,

6:08
we know that it got coerced or transformed into an unsigned integer 16, right?

6:15
So we know that it is a uint16, but this looks like it's just one 150.

6:19
Why don't we take a deeper look at this?

6:22
So let's say type of first_day_minutes and we'll see what comes back.

6:29
Nice, it's just cleverly wrapped in what is known as a scalar data type.

6:35
Here you see that the scalar data type is uint16 which is what we declared

6:40
each of the elements to be.

6:42
Scalar is a term that comes from mathematics and for

6:44
our purposes right now, it can be thought of as representing a singular value.

6:49
Now, you'll hear the term scalar thrown around for values that

6:52
are a singular element, and the term vector for those that are multiple.

6:56
You'll also hear arrays and vectors used interchangeably.

6:58
Now this naming comes from their common mathematical use cases,

7:02
which we're gonna touch on here in a bit.

7:04
For now though, I want you to just know that the single values pulled out of

7:08
a numpy array are scalar and wrapped in their specific data type object.

7:14
These scalar objects provide the same APIs as arrays.

7:18
This is so that scalar objects can interact with other arrays easily.

7:22
Again, don't fret too much about this,

7:24
we'll get into it as the course progresses.

7:26
But check the teacher's notes for

7:27
more information on scalars, if you're interested.

7:30
Let's go ahead and fill in the second day of the challenge.

7:33
I was able to sneak in some code after I got my kids to bed.

7:36
I've been working on this micro controller project and

7:40
I was able to get some progress on it and the challenge encouraged me to do it.

7:45
So I spent about an hour on it, okay so how would I track that?

7:50
How would I go about doing that?

7:53
So I wanna put 60 minutes into the second day of the challenge,

7:58
hey how about you give that a shot?

8:00
You know, just to make sure this stuff is sinking in.

8:02
So, I'll do a to do here and you do it.

8:04
So TODO, add 60 minutes to the second

8:10
day in the study_minutes array.

8:17
Pause me and give it a go, and then after you get it, unpause me and

8:20
I'll show you how I did it.

8:21
Are you ready, pause me.

8:24
Okay, so I can't tell you how many times this bites me.

8:29
I said second day so I fell like it should be index 2, but it isn't is it?

8:34
It's actually index 1, and I'm gonna set that to 60.

8:41
Now you might be wondering if you can assign multiple values to an array in

8:45
a single line, in a single statement.

8:46
If you remember your collections slicing skills, they work here too.

8:50
So, I've got four more days of data to enter.

8:53
I kept plugging away and let's see, I had 80 minutes,

8:58
on day three, and then I squeezed in another hour of 60.

9:02
And I only got a half hour on day five.

9:04
So it doesn't actually meet the challenge criteria of doing an hour a day.

9:09
But you are allowed one day of wiggle room, so I didn't break the challenge.

9:12
So, I kept going and I made up for it and I got 90 here.

9:15
So, I want to add these into the study minutes array and

9:20
I wanna start at the third day.

9:21
So we'll say study_minutes, we'll start at the third day so again that's 2,

9:26
there are four of them so I wanna go up to the 6th day.

9:31
So the way that slices work remember is they are exclusive it does

9:34
not include that seven.

9:36
So we're gonna say 2 to the 6th,

9:41
so it's not the seventh day, there we go and that should do it.

9:45
That will set it and if we go ahead and

9:48
take a look at it afterwards, we say study_minutes.

9:52
Give that a run, we will see, boom there they are, so we got 150 on our first day,

9:57
60 on the second and here's the values, 80, 60, 30, 90.

10:01
Awesome, if your slicing muscles are out of shape, don't worry.

10:04
We'll exercise them throughout this course, and

10:06
you'll be super fit when we're done.

10:08
But one thing we didn't go over earlier when we ran this Jupyter Notebook

10:13
whos command, is that you'll notice that data type here is ndarray.

10:18
Now this nd, it stands for n dimensional,

10:21
meaning the array can have many dimensions.

10:25
Which is to say that these arrays can be multidimensional,

10:30
which I realize sounds a lot like a science fiction term.

10:33
We'll get to what that means exactly right after a quick break.

10:36
Hey, during that break, why don't you do a little reflection?

10:40
Specifically surrounding what we went over about datatypes and

10:43
choosing the right one and how it can save space.

10:47
I'm going to put my notes in here right above this call,

10:53
right above this call to zero.

10:55
So I'm going to press Esc, I'm gonna get back into command mode.

10:58
I'm gonna press above, then I'm gonna get this into Markdown.

11:04
There's probably a keyboard shortcut for that, share about that.

11:07
So I'm gonna say, About data types.

11:13
And again, just like before, I'll share mine right after the break.

11:16
Take some notes about what we learned here, see you soon.
You need to sign up for Treehouse in order to download course files.
Sign up