For Loops in Python

WEBVTT
Kind: captions
Language: en

00:00:00.471 --> 00:00:02.220
Now that we know a
little bit about lists,

00:00:02.220 --> 00:00:04.600
I thought I would use this
video to introduce you

00:00:04.600 --> 00:00:06.720
to loops, and in
particular for loops.

00:00:06.720 --> 00:00:09.100
And if we have time, we might
do other types of loops.

00:00:09.100 --> 00:00:10.820
But before we even
do that, I want

00:00:10.820 --> 00:00:15.140
to introduce you to a built-in
Python function called Ranger.

00:00:15.140 --> 00:00:18.090
When I say built-in, it
means it comes with Python.

00:00:18.090 --> 00:00:20.280
And it's really
a way to generate

00:00:20.280 --> 00:00:22.950
lists that have
integers that increment.

00:00:22.950 --> 00:00:28.720
So for example, if I
just say, range of 6,

00:00:28.720 --> 00:00:31.400
it generates a list
from 0 through 5.

00:00:31.400 --> 00:00:35.520
So it's 6 elements up
to, but not including, 6.

00:00:35.520 --> 00:00:39.830
If I wanted to include 6, I
would have to do range of 7.

00:00:39.830 --> 00:00:42.740
And then I go all the way
up to, but not including 7.

00:00:42.740 --> 00:00:44.322
But I get 6 in there.

00:00:44.322 --> 00:00:45.780
If I want to have
1 through 6, if I

00:00:45.780 --> 00:00:50.820
don't want to have that 0 there,
I could do range starting at 1

00:00:50.820 --> 00:00:54.000
and including 1, up to,
but not including 7.

00:00:54.000 --> 00:00:56.260
So the first item
here will be included.

00:00:56.260 --> 00:00:58.810
The last item will
not be included.

00:00:58.810 --> 00:01:00.880
And there we go.

00:01:00.880 --> 00:01:03.640
We get 1, 2, 3, 4, 5, 6.

00:01:03.640 --> 00:01:06.040
And what's cool about these
integrated development

00:01:06.040 --> 00:01:08.720
environments, these IDE's, this
pie scripter that I'm running,

00:01:08.720 --> 00:01:11.919
is that, when I type in a
function that it recognizes,

00:01:11.919 --> 00:01:13.460
it actually will
tell me a little bit

00:01:13.460 --> 00:01:15.870
something about that function,
in case you forget it.

00:01:15.870 --> 00:01:17.270
So I don't know if you
can read this here,

00:01:17.270 --> 00:01:18.955
but it says, range,
start, stop, step.

00:01:22.680 --> 00:01:25.380
So right now, the default is
to increment by 1 each time.

00:01:25.380 --> 00:01:26.796
But it says, hey,
I could tell you

00:01:26.796 --> 00:01:28.710
to increment by a
different number.

00:01:28.710 --> 00:01:30.490
So let's say you have a range.

00:01:30.490 --> 00:01:38.380
Say you start at 0, and
you want to go up to 8.

00:01:38.380 --> 00:01:41.900
And I want to go up
by 2, instead of 1.

00:01:41.900 --> 00:01:45.800
So then I went to 0, 2, 4,
6-- I should have said it more

00:01:45.800 --> 00:01:47.560
precisely-- up to,
but not including 8.

00:01:47.560 --> 00:01:50.220
The upper part of the
range, you don't include it.

00:01:50.220 --> 00:01:53.500
So it goes up to that,
and it increments it by 2.

00:01:53.500 --> 00:01:56.120
So you could do it like that.

00:01:56.120 --> 00:02:01.560
So maybe you want to start at
3, and you want to go up to,

00:02:01.560 --> 00:02:03.410
but not including--
I don't know.

00:02:03.410 --> 00:02:06.910
Let me do up to, but
not including 31.

00:02:06.910 --> 00:02:09.759
And you want to go
up by steps of 3.

00:02:09.759 --> 00:02:10.930
So there you go.

00:02:10.930 --> 00:02:13.930
It went up steps by 3, up
to, but not including 31.

00:02:13.930 --> 00:02:15.826
So it can generate a
whole bunch of lists.

00:02:15.826 --> 00:02:17.200
And the reason
why this is useful

00:02:17.200 --> 00:02:20.722
is because we can use
these inside of loops.

00:02:20.722 --> 00:02:23.180
And so the first type of loop
I'm going to introduce you to

00:02:23.180 --> 00:02:23.940
is a for loop.

00:02:23.940 --> 00:02:25.523
So this was the
interpreter down here.

00:02:25.523 --> 00:02:27.970
Here, I'm going to
actually write a program.

00:02:27.970 --> 00:02:31.030
So this program is really just
to show you a for loop does.

00:02:31.030 --> 00:02:35.660
So I could say, for i-- and i
is this variable that's often

00:02:35.660 --> 00:02:38.160
used in for loops,
in iterative loops.

00:02:38.160 --> 00:02:40.600
And iterative means you keep
doing something over and over

00:02:40.600 --> 00:02:43.640
again-- for i in--
and let me just

00:02:43.640 --> 00:02:45.950
do a simple one-- in
range-- and I'm just

00:02:45.950 --> 00:02:47.900
going to say 5-- range of 5.

00:02:47.900 --> 00:02:51.270
So this will generate
a list, 0, 1, 2, 3, 4.

00:02:51.270 --> 00:02:53.240
So for i in range 5.

00:02:53.240 --> 00:02:55.270
And this colon says,
now we're going

00:02:55.270 --> 00:03:00.380
to have a sub clause of what to
do on each of those iterations.

00:03:00.380 --> 00:03:02.314
Let's just print out what i is.

00:03:02.314 --> 00:03:04.480
And so, before we even see
what this is going to do,

00:03:04.480 --> 00:03:05.620
let's just think about it.

00:03:05.620 --> 00:03:09.510
It's saying, for each time
we go through this loop,

00:03:09.510 --> 00:03:14.576
for each of the things in the
range, make i refer to them.

00:03:14.576 --> 00:03:17.200
And then each of those loops is
going to refer a different one.

00:03:17.200 --> 00:03:18.130
And you want to print it out.

00:03:18.130 --> 00:03:19.255
So let's just try this out.

00:03:19.255 --> 00:03:20.900
So let me save this program.

00:03:20.900 --> 00:03:23.790
And now, let me run it.

00:03:23.790 --> 00:03:24.670
So there you go.

00:03:24.670 --> 00:03:26.760
What it did is it
created a loop.

00:03:26.760 --> 00:03:28.960
So each time it
goes to the loop,

00:03:28.960 --> 00:03:31.580
it assigns i to a different
element in this list.

00:03:31.580 --> 00:03:36.324
As you remember, this
list was 0, 1, 2, 3 and 4.

00:03:36.324 --> 00:03:37.740
Did I say 0, 1,
2, 3, 4, 5 before?

00:03:37.740 --> 00:03:42.200
No, it's 0, 1, 2, 3, 4, up
to, but not including 5.

00:03:42.200 --> 00:03:46.730
And so, each time you
go through the loop,

00:03:46.730 --> 00:03:48.420
it goes through
each of those items.

00:03:48.420 --> 00:03:51.457
And this program really
just says, print out that i.

00:03:51.457 --> 00:03:53.290
So the first time we
go to the loop, i is 0.

00:03:53.290 --> 00:03:54.190
So it prints it out.

00:03:54.190 --> 00:03:55.130
Then it prints out 1.

00:03:55.130 --> 00:03:56.270
Then it prints a 2.

00:03:56.270 --> 00:03:57.150
Then it prints a 3.

00:03:57.150 --> 00:03:58.080
Then it prints a 4.

00:03:58.080 --> 00:04:00.260
So it just keeps going
through it that many times.

00:04:00.260 --> 00:04:04.320
If we said this
was for range 10.

00:04:04.320 --> 00:04:05.870
Now let's run this
program, you'll

00:04:05.870 --> 00:04:08.275
see it'll print 1 through 9.

00:04:11.010 --> 00:04:12.710
I should say 0 through
9, because that's

00:04:12.710 --> 00:04:14.510
the first element in this list.

00:04:14.510 --> 00:04:16.290
But you could do more
interesting things.

00:04:16.290 --> 00:04:22.440
Let's say that you wanted to
take the sum of 0 through 9.

00:04:22.440 --> 00:04:25.250
So what you could do is you
could define another variable

00:04:25.250 --> 00:04:25.980
here, sum.

00:04:25.980 --> 00:04:29.330
So before I do anything, the
sum is just going to be 0.

00:04:29.330 --> 00:04:31.830
And then what you could
say is, each time you go

00:04:31.830 --> 00:04:38.780
through the loop, why
don't you define sum

00:04:38.780 --> 00:04:44.802
to be the previous sum plus what
i is in this part of the loop?

00:04:44.802 --> 00:04:46.510
And just so that we
can keep track of it,

00:04:46.510 --> 00:04:50.425
I then want to print the sum.

00:04:53.150 --> 00:04:54.910
Now let's see what
this thing does.

00:04:54.910 --> 00:04:55.930
So let's try it out.

00:04:55.930 --> 00:05:01.440
So let me save it, and
then let me run it.

00:05:01.440 --> 00:05:03.830
So there you go.

00:05:03.830 --> 00:05:06.990
So the first time we went
to the loop, sum is zero.

00:05:06.990 --> 00:05:08.214
Sum is 0 and i is 0.

00:05:08.214 --> 00:05:09.130
You add them together.

00:05:09.130 --> 00:05:10.320
You print the sum, you get 0.

00:05:10.320 --> 00:05:12.195
The next time we go
through the loop, i is 1.

00:05:12.195 --> 00:05:14.710
We add that to the previous
sum, which was 0, print it,

00:05:14.710 --> 00:05:15.880
you get one.

00:05:15.880 --> 00:05:19.120
Then the next time through the
loop, i is now going to be 2.

00:05:19.120 --> 00:05:22.060
Add that to the previous
sum, 2 plus 1, you get 3.

00:05:22.060 --> 00:05:23.890
Next time through
the loop, i is 3.

00:05:23.890 --> 00:05:26.380
You add that to the
previous sum, you get 6.

00:05:26.380 --> 00:05:31.110
So the sum of all the
numbers, 0 through 9, is 45.

00:05:31.110 --> 00:05:35.761
So just that, that's actually
a kind of a useful program.

00:05:35.761 --> 00:05:37.760
But just to make the point
a little bit clearer,

00:05:37.760 --> 00:05:42.980
let me actually walk you through
this on my little drawing

00:05:42.980 --> 00:05:43.870
tablet.

00:05:43.870 --> 00:05:49.480
So let me close my-- let me
paste my code that I just

00:05:49.480 --> 00:05:50.850
wrote up here.

00:05:50.850 --> 00:05:56.080
And let's just think
about what this is doing.

00:05:56.080 --> 00:05:58.120
So we have a couple
of variables here.

00:05:58.120 --> 00:06:04.560
We start off with this
variable sum that is set to 0.

00:06:04.560 --> 00:06:07.210
And there's two ways
to visualize variables.

00:06:07.210 --> 00:06:09.050
A common way to
visualize it-- and this

00:06:09.050 --> 00:06:11.383
isn't necessarily wrong, this
is sometimes an easier way

00:06:11.383 --> 00:06:13.710
to think about it-- is
to view them as buckets.

00:06:13.710 --> 00:06:15.690
But in Python, the really
correct way-- and you

00:06:15.690 --> 00:06:17.231
saw why that's
important with lists--

00:06:17.231 --> 00:06:19.590
is to view them as
referring to something.

00:06:19.590 --> 00:06:21.990
So if you view them as
buckets, you can say, OK,

00:06:21.990 --> 00:06:25.970
from the get go, the number
in the sum bucket is 0.

00:06:25.970 --> 00:06:29.700
But the reality is, is that
the variable sum is now

00:06:29.700 --> 00:06:34.340
referring to the numeric
literal, the actual number 0,

00:06:34.340 --> 00:06:36.710
sitting in memory someplace.

00:06:36.710 --> 00:06:38.880
Now we enter into the for loop.

00:06:38.880 --> 00:06:42.760
And what the for loop
says is, let's iteratively

00:06:42.760 --> 00:06:46.960
assign i to each of the
elements in the list generated

00:06:46.960 --> 00:06:48.040
by a range of 10.

00:06:48.040 --> 00:06:51.270
So just to be clear, if we
called this right over here,

00:06:51.270 --> 00:06:55.110
range of 10, this will generate
a list that looks like this.

00:06:55.110 --> 00:07:04.970
So it will generate a list 0, 1,
2, 3, 4, 5, 6, 7, 8, and 9, up

00:07:04.970 --> 00:07:06.230
to, but not including 10.

00:07:06.230 --> 00:07:08.960
This is 10 elements
right over here.

00:07:08.960 --> 00:07:10.800
And then we go
into the for loop.

00:07:10.800 --> 00:07:13.030
So the first time we go
through the for loop--

00:07:13.030 --> 00:07:15.470
and let me write it--
so I'll think about it

00:07:15.470 --> 00:07:16.930
two different ways.

00:07:16.930 --> 00:07:21.180
So one, I'll think about
it in the bucket way.

00:07:21.180 --> 00:07:23.900
So we have this variable,
i, that can actually only

00:07:23.900 --> 00:07:25.770
be used inside of this clause.

00:07:25.770 --> 00:07:28.730
We'll talk more about scope or
where a variable can be used.

00:07:28.730 --> 00:07:31.150
But this i variable is only
good inside of this clause.

00:07:31.150 --> 00:07:32.720
The sum variable, the
way I've defined it,

00:07:32.720 --> 00:07:35.303
I can use it anywhere, outside
of the clause, or inside of it,

00:07:35.303 --> 00:07:37.860
because it was defined
outside of the clause.

00:07:37.860 --> 00:07:38.980
So you have your i.

00:07:38.980 --> 00:07:40.860
So this is if you think
of it as a bucket.

00:07:40.860 --> 00:07:42.670
And so, through the
first iteration,

00:07:42.670 --> 00:07:45.370
i is assigned to this
first item over here.

00:07:45.370 --> 00:07:48.180
So i is going to be 0.

00:07:48.180 --> 00:07:49.952
The better way to
think about it is i

00:07:49.952 --> 00:07:53.690
is actually referring to this
first element, our first time

00:07:53.690 --> 00:07:54.860
through the loop.

00:07:54.860 --> 00:07:57.750
And then we go through the
loop, and it says sum plus i.

00:07:57.750 --> 00:07:58.840
Well, sum is 0.

00:07:58.840 --> 00:07:59.730
And i is 0.

00:07:59.730 --> 00:08:01.380
You add them
together, you get 0.

00:08:01.380 --> 00:08:04.650
So sum is still going to be
0, or still referring to 0.

00:08:04.650 --> 00:08:06.840
And then it prints the sum out.

00:08:06.840 --> 00:08:11.030
So then this line right
over here prints sum.

00:08:11.030 --> 00:08:14.390
It says, OK, the sum
right now is just-- let

00:08:14.390 --> 00:08:16.196
me do it in blue-- it's just 0.

00:08:16.196 --> 00:08:17.760
And it printed it out.

00:08:17.760 --> 00:08:19.489
Then it goes back
to the for loop

00:08:19.489 --> 00:08:21.280
and says, look, are
there any more elements

00:08:21.280 --> 00:08:22.470
that I can assign i to?

00:08:22.470 --> 00:08:23.350
Well, sure there are.

00:08:23.350 --> 00:08:25.350
There's all of these that
we have to go through.

00:08:25.350 --> 00:08:29.270
So then it makes i the next
element in the sequence.

00:08:29.270 --> 00:08:32.522
So now i is going to be the
next element in the sequence.

00:08:32.522 --> 00:08:34.730
Or another way to think
about it is in the i bucket--

00:08:34.730 --> 00:08:36.510
if you want to view
them as buckets--

00:08:36.510 --> 00:08:38.490
i is now set equal to 1.

00:08:38.490 --> 00:08:40.520
And now we run this again.

00:08:40.520 --> 00:08:43.485
And we say, OK, sum is 0 plus i.

00:08:43.485 --> 00:08:44.660
I is 1.

00:08:44.660 --> 00:08:46.965
So sum is now going
to refer to 1.

00:08:50.590 --> 00:08:52.450
Or another way to
think about it is sum

00:08:52.450 --> 00:08:54.840
is now going to refer to 1.

00:08:54.840 --> 00:08:59.160
And when you print
sum, it'll now print 1.

00:08:59.160 --> 00:09:01.720
Then, the for loop
will say, alright,

00:09:01.720 --> 00:09:04.390
are there any more elements
in this list right over here

00:09:04.390 --> 00:09:06.481
to assign i to?

00:09:06.481 --> 00:09:07.355
Well, sure there are.

00:09:07.355 --> 00:09:10.490
There's all of these others,
so let me keep going.

00:09:10.490 --> 00:09:15.730
So i can now be equal
to, or can refer to, 2.

00:09:15.730 --> 00:09:18.220
So if you think of it
as a bucket, i is now 2.

00:09:18.220 --> 00:09:19.980
Or you could say, i
is now referring to 2.

00:09:19.980 --> 00:09:22.225
And you could ignore
these right over here.

00:09:22.225 --> 00:09:23.600
And we go through
the loop again.

00:09:23.600 --> 00:09:24.930
Sum is now 1.

00:09:24.930 --> 00:09:28.010
1 plus i, i is now 2.

00:09:28.010 --> 00:09:29.400
1 plus 2 is 3.

00:09:29.400 --> 00:09:33.530
So sum is now going to be 3
this time through the loop.

00:09:33.530 --> 00:09:37.000
So sum is now referring to 3.

00:09:37.000 --> 00:09:40.260
You print sum, you get 3.

00:09:40.260 --> 00:09:44.240
And you keep going all the way.

00:09:44.240 --> 00:09:45.890
As we go through
the loop, the loop

00:09:45.890 --> 00:09:47.320
will still keep
going, as there's

00:09:47.320 --> 00:09:50.890
more things that i can refer to.

00:09:50.890 --> 00:09:54.480
We can keep incrementing i to
these other values over here.

00:09:54.480 --> 00:09:56.550
And eventually, i
is going to be 9.

00:09:56.550 --> 00:09:57.761
We're going to evaluate this.

00:09:57.761 --> 00:09:59.260
And the for loop
says, hey, is there

00:09:59.260 --> 00:10:01.230
anything left in this
list that i can be?

00:10:01.230 --> 00:10:03.870
And you're like, no, there isn't
anything left in this list.

00:10:03.870 --> 00:10:05.260
And then the loop
is going to be done.

00:10:05.260 --> 00:10:06.343
And we'll break out to it.

00:10:06.343 --> 00:10:07.490
And the program will stop.

00:10:07.490 --> 00:10:11.340
And that's exactly
what we saw happening,

00:10:11.340 --> 00:10:14.061
when we ran the
program over here.


Python Lists

WEBVTT
Kind: captions
Language: en

00:00:00.302 --> 00:00:04.177
In this video, I want to introduce you to the idea of a list,

00:00:04.531 --> 00:00:06.327
of a list in python.

00:00:06.496 --> 00:00:09.154
It is one of the most powerful data structures in Python,

00:00:09.231 --> 00:00:12.116
and it really is just a sequence of a bunch of other stuff.

00:00:12.193 --> 00:00:14.679
So a list in Python would look something like this:

00:00:14.741 --> 00:00:16.986
So this would be a list of integers,

00:00:17.063 --> 00:00:23.594
Maybe 1 , -7, 0, 0, 5 and 10

00:00:23.663 --> 00:00:26.661
And we can have some variable refer to it,

00:00:26.723 --> 00:00:29.505
so that we can refer back to it later on in the program,

00:00:29.551 --> 00:00:32.713
so maybe we say a is equal to all of this.

00:00:32.806 --> 00:00:34.490
And if we run this in the interpreter,

00:00:34.552 --> 00:00:35.898
or if we run this in a program,

00:00:35.929 --> 00:00:38.121
now we can refer to the elements of a,

00:00:38.198 --> 00:00:40.944
and you do that by, so let's say I have a

00:00:41.052 --> 00:00:43.829
(let me write in the same pink color)

00:00:43.859 --> 00:00:47.407
You do that by referring to the index of the element inside of the list.

00:00:47.453 --> 00:00:50.828
So, if I say a and then in brackets, I put a zero here.

00:00:50.828 --> 00:00:54.118
This says the 0th element in a,

00:00:54.179 --> 00:00:55.381
0th elment...

00:00:55.427 --> 00:00:57.090
and the general convention in Python,

00:00:57.090 --> 00:00:59.013
and this is true of most programming languages,

00:00:59.076 --> 00:01:03.938
is what some people would consider the first item in an array, or inside of a list

00:01:03.984 --> 00:01:09.378
you would call it the 0th item, or the index of this item right here, is 0.

00:01:09.440 --> 00:01:11.054
So, if you were to type this in an interpreter,

00:01:11.054 --> 00:01:12.452
or if you were to type it in a program,

00:01:12.514 --> 00:01:16.758
it would evaluate as this item right over here.

00:01:16.819 --> 00:01:19.718
It would be this item, right over there.

00:01:19.795 --> 00:01:21.371
And let me do another example:

00:01:21.402 --> 00:01:23.146
If I were say a of..

00:01:23.192 --> 00:01:23.982
I shouldn't say a of...

00:01:24.044 --> 00:01:26.963
If I were to say the second element...

00:01:27.040 --> 00:01:28.970
in the second element in a

00:01:29.032 --> 00:01:32.485
this would be 0th element, first element, second element.. It would be

00:01:32.485 --> 00:01:35.351
this zero, right over there.

00:01:35.397 --> 00:01:37.777
So, that's how you refer to elements in a list.

00:01:37.884 --> 00:01:40.615
you can maybe already imagine doing interesting things with that.

00:01:40.676 --> 00:01:41.844
But, what is really cool with lists ...

00:01:41.906 --> 00:01:44.606
is that they don't have to all be the same data type.

00:01:44.675 --> 00:01:46.166
They could be all integers,

00:01:46.259 --> 00:01:47.287
or all floats...

00:01:47.333 --> 00:01:48.732
or all strings...

00:01:48.809 --> 00:01:50.192
but you could mix it up.

00:01:50.269 --> 00:01:51.897
So you could... you could...

00:01:51.974 --> 00:01:54.032
Actually, You could take a right over here,

00:01:54.094 --> 00:01:56.889
and you could redefine one of the elements

00:01:56.935 --> 00:01:58.413
so you could say...

00:01:58.521 --> 00:02:00.513
you could say a.....

00:02:00.590 --> 00:02:02.906
you could say let's reset......

00:02:02.990 --> 00:02:04.690
(do it in that same pink color)

00:02:04.813 --> 00:02:08.068
Let's reset the.......the...

00:02:08.068 --> 00:02:08.645
(I don't know...)

00:02:08.706 --> 00:02:10.824
let's say the fourth element..

00:02:10.896 --> 00:02:12.932
Let's reset the fourth element in a

00:02:12.993 --> 00:02:14.732
to be equal to...

00:02:14.732 --> 00:02:17.979
to be equal to.."this text"

00:02:18.025 --> 00:02:20.256
So literally "this text".

00:02:20.916 --> 00:02:22.777
Literally "this text"

00:02:22.955 --> 00:02:24.071
and so If you did that..

00:02:24.117 --> 00:02:26.343
if you set the fourth element be "this text"

00:02:26.420 --> 00:02:29.049
and then you and your program say print..

00:02:29.049 --> 00:02:31.998
print a..print a

00:02:31.998 --> 00:02:35.376
then a... would look like this.

00:02:35.422 --> 00:02:37.434
The 0th element wouldn't change.

00:02:37.480 --> 00:02:38.396
Still be 1

00:02:38.458 --> 00:02:40.460
The first element will stll be -7

00:02:40.521 --> 00:02:44.078
The...the...so this is the 0 first..this...this...this..

00:02:44.124 --> 00:02:47.077
This two index element would still be 0

00:02:47.138 --> 00:02:48.879
This will be the third.

00:02:48.879 --> 00:02:50.369
So this is 0th index.

00:02:50.415 --> 00:02:52.638
First, second, third, fourth.

00:02:52.715 --> 00:02:54.333
to replace the fourth element.

00:02:54.379 --> 00:02:56.448
So the fourth element will now be "this text"

00:02:56.448 --> 00:03:01.069
It will now be...this....will now be "this text"

00:03:01.315 --> 00:03:04.925
...the second...we wanna try out with Python intrepreter

00:03:04.987 --> 00:03:07.350
and the last element will still be.....

00:03:07.427 --> 00:03:09.649
The last element will still be 10.

00:03:09.726 --> 00:03:12.574
and this will completely valid to do in Python.

00:03:12.590 --> 00:03:13.953
You can mix data type.

00:03:14.015 --> 00:03:17.365
In fact, you could even...if you..after..after you did this,

00:03:17.426 --> 00:03:20.038
you could even say..that..a

00:03:20.146 --> 00:03:23.409
Let's redefine the 0th element of a..

00:03:23.471 --> 00:03:25.587
to be another list.

00:03:25.649 --> 00:03:28.984
To be the list -1, -2.

00:03:29.045 --> 00:03:32.733
and if you did this, then this 0th element over here,

00:03:32.733 --> 00:03:36.133
will in the list, the 0th element will now refer to this thing.

00:03:36.133 --> 00:03:38.000
So the list would now look like this.

00:03:38.000 --> 00:03:41.600
So the first element or the 0th element I should say will be this thing.

00:03:41.600 --> 00:03:43.600
Negative 1, negative 2.

00:03:43.600 --> 00:03:50.554
And then you'd have you're negative seven, zero, zero, this text

00:03:50.554 --> 00:03:53.600
and then you'll have your 10, just like that.

00:03:53.600 --> 00:03:59.933
So you define it this way and literally replaces or the 0th element will now refer to this data structure

00:03:59.933 --> 00:04:04.133
over here and you could maybe already imagine useful ways to use this in programs

00:04:04.133 --> 00:04:06.800
and if you can't don't worry about it. We'll see useful ways.

00:04:06.800 --> 00:04:10.000
Now there is one thing I want to make very clear here

00:04:10.000 --> 00:04:14.267
is that, when I... Let's say that I have... Let me define some new...

00:04:14.267 --> 00:04:19.333
Let's say that I have some list that I have the variable b referred to

00:04:19.333 --> 00:04:25.800
and let's say I make this list: 7, 13, 15.

00:04:25.800 --> 00:04:27.267
So that's my list.

00:04:27.267 --> 00:04:31.600
And let's say that I... the next line in my program or maybe using the python interpetrator.

00:04:31.600 --> 00:04:35.000
I say that c is equal to b.

00:04:35.000 --> 00:04:38.800
And sometimes you might be tempted to do this is, hey I want to have a copy of b.

00:04:38.800 --> 00:04:43.933
And what I wanna make clear is that this point, c and b are actually pointing at the same thing.

00:04:43.933 --> 00:04:47.000
They are referring to the same thing, so if you think about what's happening.

00:04:47.000 --> 00:04:52.667
There are some entity in memory that looks like this: 7, 13, 15.

00:04:52.667 --> 00:04:58.067
And when you execute this, when you execute this first line over here that says

00:04:58.067 --> 00:05:00.333
that look, b is going to definetly refer to that.

00:05:00.333 --> 00:05:05.733
If the variable b refers to this entity and when you say c is equal to b,

00:05:05.733 --> 00:05:11.200
this is actually saying that c is going to refer to the same thing that b is referring to.

00:05:11.200 --> 00:05:17.133
So c is also going to refer to this exact same thing.

00:05:17.133 --> 00:05:21.733
C is not a copy of it, we didn't make a copy of it and make c refer to that.

00:05:21.733 --> 00:05:27.267
And what's relevant here is if we now change c we are also going to change or if we change

00:05:27.267 --> 00:05:30.600
what c is refering to we'll also change what b is referring to.

00:05:30.600 --> 00:05:33.867
And I'll show you that in a second with the interpetator, but let me show you what I'm talking about.

00:05:33.867 --> 00:05:43.067
So this point, I say that c...If I... the next line I say that c, the 0th element in c needs to be equal

00:05:43.067 --> 00:05:44.175
to 1.

00:05:44.175 --> 00:05:49.052
And if I were to tell you then print c you would get.

00:05:49.052 --> 00:05:54.400
You would get 1, 13 and 15.

00:05:54.400 --> 00:05:59.467
But if you were to say print b you would also get 1, 13 and 15.

00:05:59.467 --> 00:06:03.600
And that's because this call right over here or this statement right over here

00:06:03.600 --> 00:06:09.467
it changes what the 0th element in this list refers to.

00:06:09.467 --> 00:06:12.333
So it won't refer to 7 anymore it will refer to 1.

00:06:12.333 --> 00:06:19.400
So now if you say b of 0, this will be equal to 1 and not 7.

00:06:19.400 --> 00:06:24.400
So you might say:" Well Sal okay, I understand that, so if set a variable equal to another variable

00:06:24.400 --> 00:06:28.267
that refers to a list they are actually refering to the same list and if I change one of the items in

00:06:28.267 --> 00:06:31.267
the list that this is referring to is actually changing what this is referring to.

00:06:31.267 --> 00:06:33.467
So how could I copy lists?"

00:06:33.467 --> 00:06:35.000
How could I actually copy it.

00:06:35.000 --> 00:06:42.933
And there you use the notion that can actually and I'll write right over here if you actually want to

00:06:42.933 --> 00:06:45.000
copy things you would do this.

00:06:45.000 --> 00:06:50.533
You would write c is equal to b and this might look a little funny for you

00:06:50.533 --> 00:06:56.267
but you put this colon sign over here and what this does is it tells you copy everything in b

00:06:56.267 --> 00:06:58.933
from the beginning of b all the way to the end of the b.

00:06:58.933 --> 00:07:12.159
So you could write c is equal to b and then you could say 0 all the way up to but not including

00:07:12.159 --> 00:07:13.800
the second element.

00:07:13.800 --> 00:07:23.800
So this right over here, this would create a new list that is the 0th and only the first element.

00:07:23.800 --> 00:07:25.467
It won't include the second element.

00:07:25.467 --> 00:07:30.600
So in this case, what you do... So this one right over here would create a new list that looks like this.

00:07:30.600 --> 00:07:36.933
That creates... That looks like... If we assume we haven't changed b and if we assume this was

00:07:36.933 --> 00:07:42.467
our definition of b, then it will create a new list that looks like this and c will now refer to this

00:07:42.467 --> 00:07:43.800
new list.

00:07:43.800 --> 00:07:46.600
So c will now refer to this new list.

00:07:46.600 --> 00:07:51.533
So it created a copy of the first two elements, 0th element and first, not the seconth element.

00:07:51.533 --> 00:07:54.067
You would have to put 3 here if you wanted the second element,

00:07:54.067 --> 00:07:56.867
so up to but not including the second element.

00:07:56.867 --> 00:07:59.200
If you wanted all of the elements to copy you would do this.

00:07:59.200 --> 00:08:02.267
So this is simply saying from the beginning all the way to the end.

00:08:02.267 --> 00:08:06.000
So it would create... It would create a separate list...

00:08:06.000 --> 00:08:10.867
It would create a separate list that looks just like b, but it is a separate one.

00:08:10.867 --> 00:08:16.533
And then c could refer to that and now if you change c you won't change b.

00:08:16.533 --> 00:08:21.533
So enough of just talking about it, let's actually play with it on our interpetrator.

00:08:21.533 --> 00:08:23.667
Cuz this is the fun stuff right over here.

00:08:23.667 --> 00:08:30.333
So let's say that we have... Let's say that... Let me get rid some of this text that I have here

00:08:30.333 --> 00:08:32.267
So you don't get confused.

00:08:32.267 --> 00:08:34.000
So let's say I define some list.

00:08:34.000 --> 00:08:46.800
A is equal to 1, 2 -- I'll write this way -- 1, 2, negative 7, 9, 11.

00:08:46.800 --> 00:08:49.533
So I've defined and could even verify that.

00:08:49.533 --> 00:08:51.467
Print a, you can see it right there.

00:08:51.467 --> 00:08:53.600
We can change one of the elements in a.

00:08:53.600 --> 00:08:59.600
We can say... Let's change the first element and remember, when we say we have an index 1

00:08:59.600 --> 00:09:01.733
we are not talking about this one here this is the 0th element.

00:09:01.733 --> 00:09:03.467
So we are changing this one right over here.

00:09:03.467 --> 00:09:05.600
So let's change that.

00:09:05.600 --> 00:09:08.933
Let's just for fun... Let's change that to a string.

00:09:08.933 --> 00:09:15.067
So "Sal's String*.

00:09:15.067 --> 00:09:18.533
And now let's see what a looks like. Print a.

00:09:18.533 --> 00:09:24.800
So notice, it took this, this is the index one, this is the first, 0th element, 1st element.

00:09:24.800 --> 00:09:28.600
It changed the first element to "Sal's String" and we see it right there.

00:09:28.600 --> 00:09:30.333
Now let's do something interesting.

00:09:30.333 --> 00:09:32.733
Let's set b equal to a.

00:09:32.733 --> 00:09:38.800
And let's print... Let's print b. So it looks like a.

00:09:38.800 --> 00:09:40.133
And let me do something else.

00:09:40.133 --> 00:09:48.467
Let me... Let c equal to a and then do what I talked about, so let me make c equal to a copy of a.

00:09:48.467 --> 00:09:51.533
So let's print c. Print c.

00:09:51.533 --> 00:09:55.333
So b and c looked the same, but they are two different things.

00:09:55.333 --> 00:10:00.133
C is a copy of a or c is a copy of the thing that a is referring to.

00:10:00.133 --> 00:10:04.533
B is the thing that a is referring to and to see that, let's change the first element

00:10:04.533 --> 00:10:06.467
of both b and c.

00:10:06.467 --> 00:10:08.467
so if you change the first element of b.

00:10:08.467 --> 00:10:14.000
So b of... the 0th element in b. Let's just make that, I don't know. Let's make that equal to...

00:10:14.000 --> 00:10:18.498
Let's make it equal to 0.

00:10:18.498 --> 00:10:22.467
Now if we print b you see the first element is 0.

00:10:22.467 --> 00:10:27.133
And what you'll also see is that if you print a, the first element will be 0 now.

00:10:27.133 --> 00:10:29.800
Cuz they were referring to the same thing.

00:10:29.800 --> 00:10:34.600
So it said, whatever b is referring to make it, the very 0th element, make that equal to 0.

00:10:34.600 --> 00:10:38.267
Well a was referring to the same thing so it also became 0.

00:10:38.267 --> 00:10:42.067
But c is referring to a copy so c should not have changed.

00:10:42.067 --> 00:10:47.333
C did not change. The 0th element here did not change a bit.

00:10:47.333 --> 00:10:51.200
So I'll leave you there. Lists are super powerful, there's a ton of functions you can do

00:10:51.200 --> 00:10:53.533
with lists, In fact, I'll expose you to a few right here.

00:10:53.533 --> 00:10:56.867
So that is a, you can add elements to a.

00:10:56.867 --> 00:11:01.333
So you could say a dot append and you can add something to the end of a.

00:11:01.333 --> 00:11:09.800
So you could add "new element" and if you do that it should add it to the end of a.

00:11:09.800 --> 00:11:11.467
So let's look at a.

00:11:11.467 --> 00:11:12.600
It added that element.

00:11:12.600 --> 00:11:14.867
Well b is referring to the same thing so let's see what b looks like.

00:11:14.867 --> 00:11:16.200
It has a new element.

00:11:16.200 --> 00:11:20.200
C is referring to a copy of the old a, so let's see what that looks like.

00:11:20.200 --> 00:11:23.267
Didn't add it there, cuz it didn't add it to the copy.

00:11:23.267 --> 00:11:26.933
It added only to what a was referring to which is the same thing b is referring to

00:11:26.933 --> 00:11:30.800
and there is a ton of useful functions that we'll explore them as we do more and more

00:11:30.800 --> 00:11:33.467
programs, more more example problems,

00:11:33.467 --> 00:11:38.467
but there is a bunch of useful functions or methods that lists have to be able to manipulate them

00:11:38.467 --> 99:59:59.999
and change them in different ways.


Introduction to Programs Data Types and Variables

WEBVTT
Kind: captions
Language: en

00:00:00.254 --> 00:00:02.170
What I want to do in
this video is really just

00:00:02.170 --> 00:00:05.030
expose you and introduce
you to the idea

00:00:05.030 --> 00:00:07.859
of even what a
computer program is.

00:00:07.859 --> 00:00:09.650
And just in case you
want to follow along--

00:00:09.650 --> 00:00:11.670
and I highly recommend you
do that, because the real way

00:00:11.670 --> 00:00:14.044
to learn computer science is
to really fiddle with things

00:00:14.044 --> 00:00:17.740
yourself-- this is a
Python environment.

00:00:17.740 --> 00:00:20.380
So I'm going to be doing a lot
of the programming in Python.

00:00:20.380 --> 00:00:23.520
And right here, the environment
is called pyscriptor,

00:00:23.520 --> 00:00:27.800
P-Y-S-C-R-I-P-T-O-R. It's free.

00:00:27.800 --> 00:00:29.730
It's an open-source
piece of software.

00:00:29.730 --> 00:00:34.260
And I'm using Python, I believe
I'm using Python 2.6 or 2.7.

00:00:34.260 --> 00:00:36.380
As long as you're
using Python 2,

00:00:36.380 --> 00:00:38.477
your examples will
be the same as mine.

00:00:38.477 --> 00:00:39.560
They'll work the same way.

00:00:39.560 --> 00:00:41.250
If you use Python
3, you're going

00:00:41.250 --> 00:00:43.660
to have to use slightly
different variations every now

00:00:43.660 --> 00:00:45.160
and then to make
it work properly.

00:00:45.160 --> 00:00:48.640
And I'll try to make notes
for those when they occur.

00:00:48.640 --> 00:00:51.099
So let's just start writing
ourselves the computer program.

00:00:51.099 --> 00:00:53.723
And what's cool about this is we
can write our computer program

00:00:53.723 --> 00:00:54.580
right here.

00:00:54.580 --> 00:00:57.720
And really, we're just
editing text in a file.

00:00:57.720 --> 00:00:58.580
That's all it is.

00:00:58.580 --> 00:01:00.030
It's a set of instructions.

00:01:00.030 --> 00:01:02.610
And the computer is just going
to start, for the most part,

00:01:02.610 --> 00:01:04.060
at the top of this file.

00:01:04.060 --> 00:01:06.471
And just go down and
read the instructions.

00:01:06.471 --> 00:01:08.220
Although, you'll see
later on that there's

00:01:08.220 --> 00:01:11.054
ways to tell the computer to
jump around and loop around

00:01:11.054 --> 00:01:13.470
within the instructions, so
it can do things over and over

00:01:13.470 --> 00:01:15.530
again, or skip other things.

00:01:15.530 --> 00:01:18.747
But with that said, let's write
ourselves a simple program.

00:01:18.747 --> 00:01:20.580
And while we do this,
we'll expose ourselves

00:01:20.580 --> 00:01:22.200
to some of the
core concepts that

00:01:22.200 --> 00:01:24.250
exist within a computer program.

00:01:24.250 --> 00:01:26.250
So one of the main--
so let me just

00:01:26.250 --> 00:01:29.510
write a very, very, very
simple computer program.

00:01:29.510 --> 00:01:31.600
So one very simple
computer program

00:01:31.600 --> 00:01:34.140
would literally just
be an expression.

00:01:34.140 --> 00:01:40.990
So three-- So let me just
write, print 3 plus 7.

00:01:40.990 --> 00:01:44.050
So it's, literally, just going
to take 3 plus 7 and print it.

00:01:44.050 --> 00:01:46.640
So it's going to pass it to
the print function, which

00:01:46.640 --> 00:01:47.680
comes with Python.

00:01:47.680 --> 00:01:51.510
And maybe I'll write it
like this, print 3 plus 7.

00:01:51.510 --> 00:01:52.890
And so let's save this file.

00:01:52.890 --> 00:01:55.050
So there's, literally, only,
if you think about it, only one

00:01:55.050 --> 00:01:56.300
command here, at the top line.

00:01:56.300 --> 00:01:57.480
It says print 3 plus 7.

00:01:57.480 --> 00:01:58.980
Actually, let's add
another command,

00:01:58.980 --> 00:02:01.810
just so you can see that it's
going to go from the top down.

00:02:01.810 --> 00:02:07.050
And then let me add another
one, print 2 minus 1.

00:02:07.050 --> 00:02:15.290
And then, let's do, print,
this is a chunk of text.

00:02:15.290 --> 00:02:18.140
And let's see what this
computer programmer, right here,

00:02:18.140 --> 00:02:19.820
is going to do.

00:02:19.820 --> 00:02:21.035
So let me save it.

00:02:23.726 --> 00:02:25.350
I've saved it as the
file, testarea.py.

00:02:28.460 --> 00:02:32.020
The .py signifies that
it is a Python file.

00:02:32.020 --> 00:02:33.630
And now, let me run the program.

00:02:33.630 --> 00:02:35.838
And what's nice about these
development environments,

00:02:35.838 --> 00:02:38.420
these IDE, Integrated
Development Environments,

00:02:38.420 --> 00:02:40.757
is that you can
kind of type and run

00:02:40.757 --> 00:02:42.090
your programs in the same place.

00:02:42.090 --> 00:02:43.506
And it also color
codes your text.

00:02:43.506 --> 00:02:45.272
So you can see what's
a function, what's

00:02:45.272 --> 00:02:46.980
not a function, the
different data types.

00:02:46.980 --> 00:02:49.105
And we'll talk more about
data types in the future.

00:02:49.105 --> 00:02:51.706
But let's just run this
program to see what happens.

00:02:51.706 --> 00:02:52.330
So there we go.

00:02:52.330 --> 00:02:52.920
We ran it.

00:02:52.920 --> 00:02:55.730
So it printed 10,
then it printed 1,

00:02:55.730 --> 00:02:58.200
then it printed, this
is a chunk of text.

00:02:58.200 --> 00:02:59.856
So it did exactly
what we told to do.

00:02:59.856 --> 00:03:00.980
And it did it in the order.

00:03:00.980 --> 00:03:01.970
It started up here.

00:03:01.970 --> 00:03:05.312
It printed-- it evaluated
3 plus 7 as equal to 10,

00:03:05.312 --> 00:03:06.270
and then it printed it.

00:03:06.270 --> 00:03:07.680
It printed 10 here.

00:03:07.680 --> 00:03:10.020
Then it printed 2 minus 1.

00:03:10.020 --> 00:03:11.000
Then it did that there.

00:03:11.000 --> 00:03:13.121
And then it printed,
this is a chunk of text.

00:03:13.121 --> 00:03:14.870
Now, one thing I want
to introduce you to,

00:03:14.870 --> 00:03:17.394
fairly early on, is
the idea of data types.

00:03:17.394 --> 00:03:18.810
So even when you
saw this example,

00:03:18.810 --> 00:03:20.840
you might have had the
gut feeling that look,

00:03:20.840 --> 00:03:23.830
there's something kind of
different about a 3 or a 2,

00:03:23.830 --> 00:03:26.950
or a 1, or a 7, and
this chunk of text.

00:03:26.950 --> 00:03:27.850
This is a number.

00:03:27.850 --> 00:03:31.610
I can kind of just, I feel like
I can just kind of add numbers.

00:03:31.610 --> 00:03:34.320
They're representing
some type of quantity.

00:03:34.320 --> 00:03:36.480
While this, over here,
is representing-- well,

00:03:36.480 --> 00:03:38.410
it's representing
a chunk of text.

00:03:38.410 --> 00:03:40.090
And your intuition
would be right.

00:03:40.090 --> 00:03:42.240
These are different data types.

00:03:42.240 --> 00:03:45.940
The 3's and the 7's and the
1's, these are numeric literals.

00:03:45.940 --> 00:03:48.850
And in this particular
case, they are integers.

00:03:48.850 --> 00:03:51.190
And you can-- this
right over here,

00:03:51.190 --> 00:03:52.936
this is actually
a string, which is

00:03:52.936 --> 00:03:54.810
a word you'll hear a
lot in computer science.

00:03:54.810 --> 00:03:58.720
And it's really referring
to a string of characters.

00:03:58.720 --> 00:04:00.470
And in Python, we
can actually ask

00:04:00.470 --> 00:04:02.630
it, what are the
types of these things.

00:04:02.630 --> 00:04:04.880
So you can pass them
to the function type.

00:04:04.880 --> 00:04:10.310
And so now it should print the
type of 3 plus 7, not just 10.

00:04:10.310 --> 00:04:11.072
So let's try that.

00:04:11.072 --> 00:04:13.030
And I'll just print 2
minus 1, to just show you

00:04:13.030 --> 00:04:13.700
the difference.

00:04:13.700 --> 00:04:17.970
And then I'll print the
type of this chunk of text.

00:04:21.260 --> 00:04:23.110
And so, let's save it.

00:04:23.110 --> 00:04:26.600
I'll just type Control S.
That's a shortcut to save this.

00:04:26.600 --> 00:04:29.760
And then, I'll try
to run this program.

00:04:29.760 --> 00:04:30.959
So there you go.

00:04:30.959 --> 00:04:32.125
It evaluated this statement.

00:04:32.125 --> 00:04:34.990
And to evaluate this, it starts
on the inner parentheses,

00:04:34.990 --> 00:04:37.150
3 plus 7 is 10.

00:04:37.150 --> 00:04:40.400
Then it tries to take the type
of 10, which is a type int,

00:04:40.400 --> 00:04:42.047
and then it prints
that type int.

00:04:42.047 --> 00:04:43.130
And you see it right here.

00:04:43.130 --> 00:04:44.240
It says, type int.

00:04:44.240 --> 00:04:46.600
Int is short for integer.

00:04:46.600 --> 00:04:47.989
Then it says print 2 minus 1.

00:04:47.989 --> 00:04:49.530
It does that on this
line right here.

00:04:49.530 --> 00:04:50.790
It prints 1.

00:04:50.790 --> 00:04:53.610
And then it prints the
type of this whole thing

00:04:53.610 --> 00:04:54.360
right over here.

00:04:54.360 --> 00:04:55.735
So instead of
printing it itself,

00:04:55.735 --> 00:04:56.850
it just printed its type.

00:04:56.850 --> 00:04:59.892
And its type is a string.

00:04:59.892 --> 00:05:01.850
Now, the next thing I
want to introduce you to,

00:05:01.850 --> 00:05:04.660
as we just fiddle our way,
experimenting with programs,

00:05:04.660 --> 00:05:07.080
is the idea of a variable.

00:05:07.080 --> 00:05:08.790
Because one of the
things is, we're

00:05:08.790 --> 00:05:11.730
going to want to store these
things in different places.

00:05:11.730 --> 00:05:13.230
And as we'll learn
in future videos,

00:05:13.230 --> 00:05:14.729
in Python it's more
of like, we want

00:05:14.729 --> 00:05:16.480
to have labels on these things.

00:05:16.480 --> 00:05:17.571
And the labels can change.

00:05:17.571 --> 00:05:19.070
But let's see-- or
we could put them

00:05:19.070 --> 00:05:20.650
in different types of labels.

00:05:20.650 --> 00:05:22.990
So let's write a completely
different program over here,

00:05:22.990 --> 00:05:25.420
and I'll do it using variables.

00:05:25.420 --> 00:05:27.270
So let's-- and what's
cool about Python,

00:05:27.270 --> 00:05:32.720
or some people don't like it,
is you can put any type of data

00:05:32.720 --> 00:05:34.190
in any type of variables.

00:05:34.190 --> 00:05:40.560
So you could say, a
is equal to 3 plus 5.

00:05:40.560 --> 00:05:52.950
And then we can say b is equal
to a times a, minus a, minus 1.

00:05:52.950 --> 00:06:02.860
And then you can say, c
is equal to a times b.

00:06:02.860 --> 00:06:04.290
And then, you could
have something

00:06:04.290 --> 00:06:06.810
like-- I'll put some
space here, just

00:06:06.810 --> 00:06:12.050
to make it look a
little bit cleaner--

00:06:12.050 --> 00:06:13.940
c is equal to a times b.

00:06:13.940 --> 00:06:19.980
And then we can
say, let's print c.

00:06:19.980 --> 00:06:21.536
And so if you want,
you can go ahead

00:06:21.536 --> 00:06:23.660
and try to figure out what
c is going to look like,

00:06:23.660 --> 00:06:25.339
or we can just run this program.

00:06:25.339 --> 00:06:26.630
So let's run the program first.

00:06:26.630 --> 00:06:28.730
And then we can go back
to see that it actually

00:06:28.730 --> 00:06:29.670
did the right thing.

00:06:29.670 --> 00:06:31.610
So I'm just going to
save this program.

00:06:31.610 --> 00:06:34.510
And now I'm going to run it.

00:06:34.510 --> 00:06:36.380
Well, I got 440 for c.

00:06:36.380 --> 00:06:38.270
So let's see if
that makes sense.

00:06:38.270 --> 00:06:40.850
So 3 plus 5 is 8.

00:06:40.850 --> 00:06:43.400
And so, the label
a will refer to 8.

00:06:43.400 --> 00:06:45.330
So any place in the
program, until we redefine

00:06:45.330 --> 00:06:49.460
a, any time you use a, it's
going to say, whoa, a is an 8,

00:06:49.460 --> 00:06:50.980
a is referring to 8.

00:06:50.980 --> 00:06:53.170
So when you go down over
here, and we're defining b,

00:06:53.170 --> 00:06:54.600
it'll say, OK, a times 8.

00:06:54.600 --> 00:06:56.570
It uses order of operations.

00:06:56.570 --> 00:06:59.540
So in order of operations, you
do your multiplication first.

00:06:59.540 --> 00:07:01.735
And so a times-- especially
when you're comparing it

00:07:01.735 --> 00:07:05.490
against subtraction-- so a
times a, that's going to be 64.

00:07:05.490 --> 00:07:07.420
And then we have 64 minus a.

00:07:07.420 --> 00:07:13.820
So 64 minus 8 is
56, minus 1 is 55.

00:07:13.820 --> 00:07:20.770
So b is 55, and then c is going
to be a, which is 8, times 55.

00:07:20.770 --> 00:07:26.020
And 8 times 55 is indeed 440.

00:07:26.020 --> 00:07:27.890
So it all worked out.

00:07:27.890 --> 00:07:31.260
And so maybe you want to--
you've defined these things,

00:07:31.260 --> 00:07:33.760
and you want to see what happens
when you get different a's.

00:07:33.760 --> 00:07:34.801
And you can try that out.

00:07:34.801 --> 00:07:37.630
You can just change what happens
here for the different a's.

00:07:37.630 --> 00:07:41.020
So maybe we'll have
a is equal to-- let's

00:07:41.020 --> 00:07:44.325
make it equal to negative 6.

00:07:44.325 --> 00:07:48.450
Now let's run our program
and see what happens.

00:07:48.450 --> 00:07:50.122
We get negative 246.

00:07:50.122 --> 00:07:51.580
And you can verify
it for yourself.

00:07:51.580 --> 00:07:53.770
You go line-by-line,
and have these variables

00:07:53.770 --> 00:07:56.220
refer to what they're
defined to be referring to,

00:07:56.220 --> 00:07:59.890
and see if you get this
response right over here.

00:07:59.890 --> 00:08:02.300
Now, if programs were
just a bunch of commands

00:08:02.300 --> 00:08:04.680
that you just always
go straight through,

00:08:04.680 --> 00:08:07.306
you wouldn't be able to do
really interesting things.

00:08:07.306 --> 00:08:08.930
And so to do really
interesting things,

00:08:08.930 --> 00:08:10.388
you're going to
start seeing things

00:08:10.388 --> 00:08:12.232
like conditionals and loops.

00:08:12.232 --> 00:08:13.940
And a conditional and
a loop is something

00:08:13.940 --> 00:08:17.930
like-- let's do it like this.

00:08:17.930 --> 00:08:20.730
So I'll just leave that stuff
over there-- and we'll say,

00:08:20.730 --> 00:08:34.880
if a is less than 0,
maybe we will print c.

00:08:34.880 --> 00:08:49.560
And if, or else,
we'll print c minus a.

00:08:49.560 --> 00:08:50.609
So this is interesting.

00:08:50.609 --> 00:08:52.400
And you might already
have a gut for what's

00:08:52.400 --> 00:08:54.100
going to happen here,
but let's save it.

00:08:54.100 --> 00:08:55.475
And it's amazing
how much you can

00:08:55.475 --> 00:08:57.460
get done with just
these conditionals.

00:08:57.460 --> 00:09:00.370
So this is saying, if a
is less than 0, do this.

00:09:00.370 --> 00:09:03.514
Otherwise, if a is not less
than 0, do this over here.

00:09:03.514 --> 00:09:05.680
So notice, we're not just
going to go straight down.

00:09:05.680 --> 00:09:09.150
Depending on whether a
is less than 0 or not,

00:09:09.150 --> 00:09:11.670
it's going to either
execute this line,

00:09:11.670 --> 00:09:13.770
or it's going to
execute this line.

00:09:13.770 --> 00:09:16.020
And the way that
Python, you'd only

00:09:16.020 --> 00:09:20.880
execute this line if a is less
than 0, is it's indented here.

00:09:20.880 --> 00:09:22.910
And the indent, it's
part of this clause.

00:09:22.910 --> 00:09:25.451
And the way it knows that there
are new clauses forming right

00:09:25.451 --> 00:09:27.270
here is this colon
right over here.

00:09:27.270 --> 00:09:30.890
And then the way to know what
to execute if none of this

00:09:30.890 --> 00:09:33.750
happens, or if a is
not less than 0, it's

00:09:33.750 --> 00:09:35.320
within this else clause.

00:09:35.320 --> 00:09:37.560
And if you want to do
something else after this,

00:09:37.560 --> 00:09:40.270
regardless of whether a
is less than 0 or not,

00:09:40.270 --> 00:09:41.700
you just take it
out of the clause

00:09:41.700 --> 00:09:43.840
by getting rid of
the indentations.

00:09:43.840 --> 00:09:46.080
So now we can just print.

00:09:46.080 --> 00:09:50.260
We are done with the program.

00:09:50.260 --> 00:09:52.990
Actually, let's add some
other stuff in this clause.

00:09:52.990 --> 00:09:57.770
So let's print,
a is less than 0.

00:09:57.770 --> 00:10:00.110
So notice, this is not
going to be evaluated.

00:10:00.110 --> 00:10:02.110
We have this inside of a string.

00:10:02.110 --> 00:10:04.080
So it's just going
to print that thing.

00:10:04.080 --> 00:10:13.370
And then over here, we'll say,
print a is not less than 0.

00:10:13.370 --> 00:10:14.930
So this is an
interesting program.

00:10:14.930 --> 00:10:17.250
And let's just run it now.

00:10:17.250 --> 00:10:19.140
All right, let's hope it runs.

00:10:19.140 --> 00:10:20.380
All right, so I saved it.

00:10:20.380 --> 00:10:22.610
And now, let's run the program.

00:10:22.610 --> 00:10:26.210
And it says, it printed
a is less than 0.

00:10:26.210 --> 00:10:29.630
So we could scroll
up a little bit.

00:10:29.630 --> 00:10:32.100
It printed-- so this is,
we ran the program-- it

00:10:32.100 --> 00:10:33.290
printed a is less than 0.

00:10:33.290 --> 00:10:36.800
So it shows that we're
inside of this clause.

00:10:36.800 --> 00:10:38.870
So it printed this,
then it printed

00:10:38.870 --> 00:10:41.380
c, which is negative 246.

00:10:41.380 --> 00:10:43.480
It does not execute
this, because this

00:10:43.480 --> 00:10:46.207
needed to be executed only
if a is not less than 0.

00:10:46.207 --> 00:10:47.790
But then it breaks
out of that clause,

00:10:47.790 --> 00:10:49.248
and it prints this,
no matter what.

00:10:49.248 --> 00:10:51.050
We are done with the program.

00:10:51.050 --> 00:10:53.052
And let's just change
a to try to see

00:10:53.052 --> 00:10:54.760
if we can get this
other clause to print.

00:10:54.760 --> 00:10:56.820
So let's make a greater than 0.

00:10:56.820 --> 00:10:59.940
So let's make a equal to 9.

00:10:59.940 --> 00:11:01.245
And now, let's run the program.

00:11:04.320 --> 00:11:06.150
And so there, a is 9.

00:11:06.150 --> 00:11:08.220
So it says, is a less than 0?

00:11:08.220 --> 00:11:09.514
Well, 9 isn't less than 0.

00:11:09.514 --> 00:11:10.930
So it's not going
to execute this.

00:11:10.930 --> 00:11:12.897
It's going to go
to the else clause.

00:11:12.897 --> 00:11:14.980
And so, it's going to
print, a is not less than 0,

00:11:14.980 --> 00:11:16.200
which it did over here.

00:11:16.200 --> 00:11:20.284
Then it printed c
minus a, which is 630.

00:11:20.284 --> 00:11:21.700
Then it breaks out
of that clause.

00:11:21.700 --> 00:11:24.820
And regardless of whether a is
less than 0 or not, it prints,

00:11:24.820 --> 00:11:27.529
we are done with the program.


Iterative Fibonacci Function Example

WEBVTT
Kind: captions
Language: en

00:00:00.660 --> 00:00:04.970
In the last video I challenged
you to write multiple Fibonacci

00:00:04.970 --> 00:00:07.640
functions, ones that
could calculate the nth

00:00:07.640 --> 00:00:10.170
term of the Fibonacci
sequence either iteratively

00:00:10.170 --> 00:00:11.450
or recursively.

00:00:11.450 --> 00:00:13.431
And I'll do the first
shot right over here.

00:00:13.431 --> 00:00:15.180
And I'll show you over
the next few videos

00:00:15.180 --> 00:00:17.890
that there's really multiple
ways of even doing this just

00:00:17.890 --> 00:00:18.940
iteratively.

00:00:18.940 --> 00:00:22.970
So let's define our
function, Fibonacci.

00:00:22.970 --> 00:00:26.420
And it has a
parameter n, so that

00:00:26.420 --> 00:00:30.960
is what we're going to
pass to the function.

00:00:30.960 --> 00:00:33.870
And we know that by
definition, the first two

00:00:33.870 --> 00:00:37.290
terms of the Fibonacci
sequence are 0 and 1.

00:00:37.290 --> 00:00:40.226
So let's make a
list for ourselves.

00:00:40.226 --> 00:00:41.350
And so this is interesting.

00:00:41.350 --> 00:00:42.933
So this is the first
time we're really

00:00:42.933 --> 00:00:45.140
going to do some
of the actual list

00:00:45.140 --> 00:00:47.320
manipulation in this video.

00:00:47.320 --> 00:00:51.540
So the terms here-- the 0th term
of the Fibonacci sequence is 0.

00:00:51.540 --> 00:00:52.930
And the first 1 is 1.

00:00:52.930 --> 00:00:54.600
This is by definition.

00:00:54.600 --> 00:00:57.920
So we're just going to kind of
hard code that in right there.

00:00:57.920 --> 00:01:00.710
And then what we're
going to do is

00:01:00.710 --> 00:01:04.440
if we're then going to
construct this list to all

00:01:04.440 --> 00:01:07.940
of the terms up to and
including the nth term,

00:01:07.940 --> 00:01:10.214
and then return the nth
term from that list.

00:01:10.214 --> 00:01:12.130
And the reason why I'm
going to do it this way

00:01:12.130 --> 00:01:15.600
is it's able to save up kind of
a memory of all the Fibonacci

00:01:15.600 --> 00:01:17.350
numbers, which is
helpful for computing

00:01:17.350 --> 00:01:19.761
each incremental
Fibonacci number.

00:01:19.761 --> 00:01:20.760
So let's do it this way.

00:01:20.760 --> 00:01:21.860
I'm going to use a while loop.

00:01:21.860 --> 00:01:23.290
You could do it
using a for loop,

00:01:23.290 --> 00:01:25.581
but for me a while loop feels
a little bit more natural

00:01:25.581 --> 00:01:26.130
for this.

00:01:26.130 --> 00:01:28.430
And actually before I even
define the while loop,

00:01:28.430 --> 00:01:33.270
I'm going to set my
variable of iteration.

00:01:33.270 --> 00:01:35.570
I'm going to set
that equal to 0,

00:01:35.570 --> 00:01:38.120
and you're going to see
how this works in a second.

00:01:38.120 --> 00:01:44.290
So I'm going to say while i
is less than or equal to n.

00:01:44.290 --> 00:01:46.880
So i is going to start at
0, and then go up to n.

00:01:46.880 --> 00:01:49.401
And actually frankly,
i shouldn't start at 0

00:01:49.401 --> 00:01:51.900
because we already have the 0th
term here and the first term

00:01:51.900 --> 00:01:52.400
here.

00:01:52.400 --> 00:01:55.090
And we want to construct
the second, third, fourth,

00:01:55.090 --> 00:01:56.530
all the way to the nth term.

00:01:56.530 --> 00:01:59.000
So actually, let's start i at 2.

00:01:59.000 --> 00:02:01.280
So we already have the 0th
term and the first term.

00:02:01.280 --> 00:02:03.080
We then want to construct
the second term.

00:02:03.080 --> 00:02:04.996
So that's why we're going
to start i equals 2,

00:02:04.996 --> 00:02:07.720
all the way up to and
including the nth term.

00:02:07.720 --> 00:02:09.990
So that's why we say
less than or equal to n.

00:02:09.990 --> 00:02:13.770
We're going to keep adding
terms to this sequence.

00:02:13.770 --> 00:02:17.180
So while i is less
than or equal to n,

00:02:17.180 --> 00:02:22.000
I'm going to take this list
right here, and to the end

00:02:22.000 --> 00:02:24.630
of that list-- and this
is a built-in function

00:02:24.630 --> 00:02:28.150
for any list in Python,
and you're learning it now.

00:02:28.150 --> 00:02:30.050
And you can look it
up, and actually my IDE

00:02:30.050 --> 00:02:34.040
tells me how to use it-- I can
add to the end of that list

00:02:34.040 --> 00:02:35.210
another term.

00:02:35.210 --> 00:02:37.770
And that next term I'm going
to add to the end of the list

00:02:37.770 --> 00:02:40.590
is going to be the sum of
the previous two terms.

00:02:40.590 --> 00:02:46.140
So it's going to
be, terms i minus 1.

00:02:46.140 --> 00:02:48.070
So that's the previous term.

00:02:48.070 --> 00:02:51.520
Terms i minus 1 literally
refers to the previous term.

00:02:51.520 --> 00:02:58.100
i minus 1 plus terms i minus 2.

00:02:58.100 --> 00:03:01.620
So this is essentially going
to construct this Fibonacci

00:03:01.620 --> 00:03:04.360
sequence and build
it in this list.

00:03:04.360 --> 00:03:06.680
And then we're going
to increment i.

00:03:06.680 --> 00:03:08.610
i is equal to i plus 1.

00:03:08.610 --> 00:03:12.130
If we never increment
i, then this loop

00:03:12.130 --> 00:03:14.374
will just keep going
on forever and ever.

00:03:14.374 --> 00:03:16.040
But this way it's
going to keep going up

00:03:16.040 --> 00:03:17.499
and up and up until
at some point i

00:03:17.499 --> 00:03:19.290
is not going to be less
than or equal to n,

00:03:19.290 --> 00:03:21.370
and then we're going to
break out of the loop.

00:03:21.370 --> 00:03:25.410
And then when we break
out of the loop--

00:03:25.410 --> 00:03:29.080
let me do it right over--
when we break out of the loop,

00:03:29.080 --> 00:03:34.610
we can then return the nth
Fibonacci term in the sequence.

00:03:34.610 --> 00:03:38.650
So we're going to return terms.

00:03:38.650 --> 00:03:42.770
We're going to return
the nth term in terms.

00:03:42.770 --> 00:03:43.940
So let's see if this works.

00:03:43.940 --> 00:03:46.030
Let's see, does this make sense?

00:03:46.030 --> 00:03:51.540
So I'm going to go all the
way up to the nth term,

00:03:51.540 --> 00:03:56.900
and actually the nth term
here-- yes, this should work.

00:03:56.900 --> 00:03:59.330
Because if this
was Fibonacci of 0,

00:03:59.330 --> 00:04:03.110
I want to return terms of 0,
which is this term right here.

00:04:03.110 --> 00:04:05.560
If it's Fibonacci
of 1, I would want

00:04:05.560 --> 00:04:07.380
to return this term
right over here.

00:04:07.380 --> 00:04:09.880
So the first term in terms.

00:04:09.880 --> 00:04:11.310
Not the 0th, but the first.

00:04:11.310 --> 00:04:13.170
So this feels like
it should work.

00:04:13.170 --> 00:04:14.630
And actually even
before I run it,

00:04:14.630 --> 00:04:16.019
I want you to make
sure you understand

00:04:16.019 --> 00:04:17.410
what I've done
here with the list.

00:04:17.410 --> 00:04:19.701
So I'm going to show you a
little bit an example of how

00:04:19.701 --> 00:04:20.779
these lists work.

00:04:20.779 --> 00:04:27.370
So if I define a list as,
I don't know, 1 comma 2.

00:04:27.370 --> 00:04:28.610
That's my list.

00:04:28.610 --> 00:04:32.260
And if I then say-- it's
doing something weird.

00:04:32.260 --> 00:04:37.030
So I'm going to define
my list as 1 comma 2.

00:04:37.030 --> 00:04:38.880
So if I type a, it's 1 comma 2.

00:04:38.880 --> 00:04:42.290
If I append to a, a dot append.

00:04:42.290 --> 00:04:47.160
So if I append a, let's
say I append a 7 to it.

00:04:47.160 --> 00:04:50.760
Then if I look at a, I
have a 7 at the end of it.

00:04:50.760 --> 00:04:53.990
If I want to refer to
the elements here, a of 0

00:04:53.990 --> 00:04:55.440
should be the first element.

00:04:55.440 --> 00:04:58.804
a, the second element, I just
put the 2 in the brackets

00:04:58.804 --> 00:04:59.970
and it should give me the 7.

00:04:59.970 --> 00:05:02.580
So that's exactly what
I'm doing over here.

00:05:02.580 --> 00:05:04.850
I'm saying terms of i minus 1.

00:05:04.850 --> 00:05:06.992
So we're going to add
a new term over here.

00:05:06.992 --> 00:05:08.450
So this first time
it goes to loop,

00:05:08.450 --> 00:05:09.700
we're going to add a new term.

00:05:09.700 --> 00:05:14.760
And it's going to be the
sum of terms of i minus 1.

00:05:14.760 --> 00:05:19.020
So in that first loop,
i is 2. i minus 1 is 1.

00:05:19.020 --> 00:05:21.910
So terms of 1 plus terms of 0.

00:05:21.910 --> 00:05:25.080
So it's going to be terms of
1 which is 1, plus terms of 0.

00:05:25.080 --> 00:05:27.350
And then it keeps doing
that all the way until we

00:05:27.350 --> 00:05:29.060
construct the nth term.

00:05:29.060 --> 00:05:31.770
Well enough talk, and I'll step
through it a little bit clearer

00:05:31.770 --> 00:05:33.660
in the next video with
a particular example.

00:05:33.660 --> 00:05:36.960
But let's just see if what
we wrote actually works.

00:05:36.960 --> 00:05:39.800
So let me run it.

00:05:39.800 --> 00:05:40.400
All right.

00:05:40.400 --> 00:05:43.162
And let's see if we get the
proper results Fibonacci--

00:05:43.162 --> 00:05:45.620
well, let's just start from
the beginning-- Fibonacci of 0.

00:05:45.620 --> 00:05:47.200
Well, that looks good.

00:05:47.200 --> 00:05:48.690
Let's try Fibonacci of 1.

00:05:48.690 --> 00:05:49.850
That looks good.

00:05:49.850 --> 00:05:50.800
Fibonacci of 2.

00:05:50.800 --> 00:05:52.230
This should also be 1.

00:05:52.230 --> 00:05:53.620
That looks good.

00:05:53.620 --> 00:05:55.721
Fibonacci of 3--
this should be 2 now,

00:05:55.721 --> 00:05:57.470
because we're seeking
the sum of 1 plus 1.

00:05:57.470 --> 00:05:58.710
That works.

00:05:58.710 --> 00:06:00.310
Let's try the Fibonacci of 10.

00:06:00.310 --> 00:06:01.790
Hey, that looks pretty good.

00:06:01.790 --> 00:06:03.720
Let's try the Fibonacci
of something huge.

00:06:03.720 --> 00:06:05.140
Fibonacci of 100.

00:06:05.140 --> 00:06:11.180
Well, this gets a little bit--
I won't go into the-- well,

00:06:11.180 --> 00:06:13.340
this is a very large
number, and I'm not

00:06:13.340 --> 00:06:14.827
going to go into
the whole floating

00:06:14.827 --> 00:06:16.910
kind of the long numbers
and the super large ones.

00:06:16.910 --> 00:06:18.993
But let's try something a
little bit smaller here.

00:06:18.993 --> 00:06:20.500
So let's try Fibonacci of 20.

00:06:20.500 --> 00:06:23.772
So it looks like it works, and
you can verify it for yourself.

00:06:23.772 --> 00:06:25.480
So this is just one
implementation of it,

00:06:25.480 --> 00:06:27.021
and in the next few
videos, I'm going

00:06:27.021 --> 00:06:28.536
to try another way
to implement it.

00:06:28.536 --> 00:06:29.910
Maybe I'll do it
with a for loop,

00:06:29.910 --> 00:06:32.009
or we can also do
it recursively.


Writing a Simple Factorial Program. (Python 2)

WEBVTT
Kind: captions
Language: en

00:00:00.000 --> 00:00:02.021
What I want to do in this video is

00:00:02.021 --> 00:00:04.946
introduce you to a “for" loop.

00:00:04.946 --> 00:00:07.395
And we're going to do this by writing a little program

00:00:07.395 --> 00:00:10.374
that calculates the factorial of a number.

00:00:10.374 --> 00:00:11.838
In case you don't remember

00:00:11.838 --> 00:00:13.835
what a factorial of a number does,

00:00:13.835 --> 00:00:15.846
if I were to tell you what "1 factorial" [is],

00:00:15.846 --> 00:00:18.026
you normally write it

00:00:18.026 --> 00:00:20.685
as 1 and an exclamation mark ( 1! ) like that.

00:00:20.685 --> 00:00:23.698
It literally just means, well, that will just evaluate to 1.

00:00:23.698 --> 00:00:25.533
If you say "2 factorial" ( 2! ),

00:00:25.533 --> 00:00:28.843
that's going to be equal to 2 × 1.

00:00:28.843 --> 00:00:31.846
If you say "3 factorial" ( 3! ),

00:00:31.846 --> 00:00:38.106
that's going to be equal to 3 × 2 × 1,

00:00:38.106 --> 00:00:40.859
and which is going to be equal to 6.

00:00:40.859 --> 00:00:42.980
And so, if you have any number's factorial,

00:00:42.980 --> 00:00:44.026
it's going to be that number

00:00:44.026 --> 00:00:45.380
times one number less than that,

00:00:45.380 --> 00:00:46.553
times one number less than that,

00:00:46.553 --> 00:00:48.556
all the way down to 1.

00:00:48.556 --> 00:00:49.529
So, with that out of the way,

00:00:49.529 --> 00:00:52.374
let's implement our factorial program.

00:00:52.374 --> 00:00:53.989
And the way I'm going to do it is,

00:00:53.989 --> 00:00:55.848
it's going to take an input from the user,

00:00:55.848 --> 00:00:57.108
It's going to prompt the user

00:00:57.108 --> 00:00:59.552
to put a number that they want to take the factorial of

00:00:59.552 --> 00:01:01.858
and then it's going to calculate the factorial.

00:01:01.858 --> 00:01:03.058
And so what I'm going to do is,

00:01:03.058 --> 00:01:06.844
I'm going to use a function that Python provides for us.

00:01:06.844 --> 00:01:09.706
And I'm using Python 2 in this video.

00:01:09.706 --> 00:01:11.671
You'd have to do it slightly different —

00:01:11.671 --> 00:01:12.833
actually, I'll talk about that in a second —

00:01:12.833 --> 00:01:14.379
if you are using Python 3.

00:01:14.379 --> 00:01:15.364
Python 2 is

00:01:15.364 --> 00:01:18.039
what most implementations of Pythoner are built in

00:01:18.039 --> 00:01:19.546
at the time of this video.

00:01:19.546 --> 00:01:21.035
But there is a Python 3 out there

00:01:21.035 --> 00:01:23.386
that's kind of a newer version.

00:01:23.386 --> 00:01:26.999
I'll clarify that a bit in a future video.

00:01:26.999 --> 00:01:30.395
But anyway, we're going to take an input from the user,

00:01:30.395 --> 00:01:32.036
and we're going to prompt them with a message.

00:01:32.036 --> 00:01:33.013
We're going to say,

00:01:33.013 --> 00:01:44.375
"Enter a non-negative integer to take the factorial of:".

00:01:44.375 --> 00:01:45.334
And just so that

00:01:45.334 --> 00:01:47.540
we get to familiarize ourselves with functions and all.

00:01:47.540 --> 00:01:49.545
We're passing an argument to the function.

00:01:49.545 --> 00:01:52.042
And the argument that we are passing to the function —

00:01:52.042 --> 00:01:55.733
to the input function right over here — is a string.

00:01:55.733 --> 00:01:58.025
And "string" sounds like a very complicated thing,

00:01:58.025 --> 00:02:00.020
but it really is just a string of characters.

00:02:00.020 --> 00:02:01.800
You can view it as a bunch of text.

00:02:01.800 --> 00:02:04.036
it'll present this text to the user,

00:02:04.036 --> 00:02:05.708
and it'll give them a little input box.

00:02:05.708 --> 00:02:07.733
And the user will put something into that input box.

00:02:07.733 --> 00:02:10.009
It'll evaluate what is in that input box.

00:02:10.009 --> 00:02:12.010
And then, you could either view it as

00:02:12.010 --> 00:02:14.692
having the variable number refer to it,

00:02:14.692 --> 00:02:16.666
or put it in the variable number,

00:02:16.666 --> 00:02:20.664
depending on how you want to visualize the variable.

00:02:20.664 --> 00:02:24.677
If you're using Python 3,

00:02:24.677 --> 00:02:26.684
you'll want to call "eval"

00:02:26.684 --> 00:02:29.022
on what's returned from this function.

00:02:29.022 --> 00:02:30.667
This is if you're doing Python 3.

00:02:30.667 --> 00:02:32.602
I'm running Python 2 right now.

00:02:32.602 --> 00:02:36.086
And if you want to do it exactly the way I'm doing it,

00:02:36.086 --> 00:02:39.015
I'm using the PyScripter IDE —

00:02:39.015 --> 00:02:41.003
Integrated Development Environment.

00:02:41.003 --> 00:02:43.929
It's a free open-source project on a PC.

00:02:43.929 --> 00:02:46.369
But there're many, many ways that you can edit Python.

00:02:46.369 --> 00:02:49.672
You can really just do it on any text editor — so anyway.

00:02:49.672 --> 00:02:53.669
So, so far, all we've done is we've taken input from the user

00:02:53.669 --> 00:02:56.025
and we're putting it in the variable number.

00:02:56.025 --> 00:02:57.338
What's really cool about Python is

00:02:57.338 --> 00:02:59.343
we can just run this program as is

00:02:59.343 --> 00:03:00.377
and just see what happens.

00:03:00.377 --> 00:03:01.626
So let's do that.

00:03:01.626 --> 00:03:04.159
Save it, and I'm going to run it.

00:03:04.159 --> 00:03:06.826
This little play button runs it in this IDE.

00:03:06.826 --> 00:03:09.648
And just like we said, it prompts the user:

00:03:09.648 --> 00:03:12.067
"Enter a non-negative integer to take the factorial of:".

00:03:12.067 --> 00:03:14.190
So I don't know — let me type in 10.

00:03:14.190 --> 00:03:17.388
And I [click] OK. Nothing happened.

00:03:17.388 --> 00:03:18.510
Well of course nothing happened!

00:03:18.510 --> 00:03:20.832
Because I didn't really tell this to do anything.

00:03:20.832 --> 00:03:23.523
But if we type in "number",

00:03:23.523 --> 00:03:26.013
we'll see that it is now assigned to 10.

00:03:26.013 --> 00:03:28.395
The variable is now referring to the number 10

00:03:28.395 --> 00:03:32.988
So now we can use this number and take the factorial of it.

00:03:32.988 --> 00:03:34.013
So what I'm going to do —

00:03:34.013 --> 00:03:36.006
and we're going to do it iteratively —

00:03:36.006 --> 00:03:37.029
we're going to talk more about

00:03:37.029 --> 00:03:38.987
what that means in the future.

00:03:38.987 --> 00:03:41.867
So I'm going to define another variable called product.

00:03:41.867 --> 00:03:44.073
I'm start product at 1.

00:03:44.073 --> 00:03:45.534
What we're going to do is

00:03:45.534 --> 00:03:49.839
we're going to start from 1 up to whatever number this is,

00:03:49.839 --> 00:03:51.710
and keep multiplying the product

00:03:51.710 --> 00:03:54.701
by each successively larger number.

00:03:54.701 --> 00:03:56.215
So we're going to start —

00:03:56.215 --> 00:03:59.148
So I'm going to set up a for loop here —

00:03:59.148 --> 00:04:01.867
In next video I'm going to really step through

00:04:01.867 --> 00:04:04.010
exactly what a for loop does.

00:04:04.010 --> 00:04:05.215
We're going to — within the for loop —

00:04:05.215 --> 00:04:07.526
we're going to define a variable i.

00:04:07.526 --> 00:04:10.467
This is kind of the classic variable to define in for loops.

00:04:10.467 --> 00:04:11.856
And that variable is going to

00:04:11.856 --> 00:04:14.859
keep incrementing to larger and larger values

00:04:14.859 --> 00:04:16.014
as we go through it.

00:04:16.014 --> 00:04:19.169
So "for i in range" —

00:04:19.169 --> 00:04:21.682
And for loops in Python are a little bit different than

00:04:21.682 --> 00:04:23.334
if you were doing it in Javascript or Java.

00:04:23.334 --> 00:04:25.487
I'll do a future video where we do that.

00:04:25.487 --> 00:04:27.800
But "range of number" —

00:04:27.800 --> 00:04:30.420
And just to understand what range of number does is,

00:04:30.420 --> 00:04:34.000
it provides a list of numbers starting with 0,

00:04:34.000 --> 00:04:36.200
up to 1 less than that number.

00:04:36.200 --> 00:04:38.394
So over here we already defined what number is,

00:04:38.394 --> 00:04:39.385
number is 10.

00:04:39.385 --> 00:04:43.208
So if I type in range of 10 — or let me do range of number.

00:04:43.208 --> 00:04:46.674
It gives me a list starting with 0 all the way to 9 —

00:04:46.674 --> 00:04:48.467
all the way to 1 less than than 10

00:04:48.467 --> 00:04:51.271
So that the same thing as range of 10.

00:04:51.271 --> 00:04:53.348
And what this for loop does is

00:04:53.348 --> 00:04:57.009
it assigns this i to each term of the sequence —

00:04:57.009 --> 00:04:59.392
So it starts with [0] — or each term of this list.

00:04:59.392 --> 00:05:01.845
It starts with [0], and then it'll [do]

00:05:01.845 --> 00:05:05.005
whatever you tell it to do in the for loop with i is equal to [0],

00:05:05.005 --> 00:05:07.665
then it'll assign i to to be 1.

00:05:07.665 --> 00:05:11.226
Sorry. It'll start with i equals 0, then it'll go to i equals 1,

00:05:11.226 --> 00:05:14.482
then i equals 2 then i equals 3 — so on and so forth —

00:05:14.482 --> 00:05:18.810
all the way until you have gone all the way through the list.

00:05:18.810 --> 00:05:24.839
And so what we can do here is —

00:05:24.839 --> 00:05:27.699
within the for loop —

00:05:27.699 --> 00:05:29.985
we can redefine product

00:05:29.985 --> 00:05:33.863
to be equal to the product (the former product) —

00:05:33.863 --> 00:05:39.194
So our new product is going to be our old product times —

00:05:39.194 --> 00:05:44.054
and I'm going to multiply it — times i + 1.

00:05:44.054 --> 00:05:46.400
We're going to step through it carefully in the next video.

00:05:46.400 --> 00:05:48.485
But I want you to think about what it's doing already.

00:05:48.485 --> 00:05:49.678
So right when we start,

00:05:49.678 --> 00:05:53.988
let's say that number ends up being 3.

00:05:53.988 --> 00:05:55.529
So product gets set to 1.

00:05:55.529 --> 00:05:57.634
And we say "for i in range".

00:05:57.634 --> 00:06:02.185
And then, if we do range of 3,

00:06:02.185 --> 00:06:04.538
then i is first going to be 0.

00:06:04.538 --> 00:06:05.691
And so we're going to take...

00:06:05.691 --> 00:06:08.987
our old product — so 1 — times i + 1.

00:06:08.987 --> 00:06:12.067
Well i is going to be 0 so 0 + 1 is 1.

00:06:12.067 --> 00:06:14.715
So it is going to be 1 × 1, [which] is going to be 1.

00:06:14.715 --> 00:06:17.789
Then we're going to assign i to be 1.

00:06:17.789 --> 00:06:19.368
This is what the for loop does.

00:06:19.368 --> 00:06:21.389
It iterates. It keeps incrementing.

00:06:21.389 --> 00:06:22.830
Well it doesn't always have to increment.

00:06:22.830 --> 00:06:24.697
It keeps changing the value of i

00:06:24.697 --> 00:06:26.974
as it goes through this list.

00:06:26.974 --> 00:06:29.190
And so the next time around our product is 1.

00:06:29.190 --> 00:06:34.577
But now i is 1, so 1 + 1 is 2, times 1 is now going to be 2.

00:06:34.577 --> 00:06:37.084
And then it'll go — it'll set i to be 2.

00:06:37.084 --> 00:06:38.687
So it's going to be 2 + 1...

00:06:38.687 --> 00:06:42.523
times what product was from the last iteration which was 2.

00:06:42.523 --> 00:06:46.359
And so it's going to be 2 × 3 which is 6

00:06:46.359 --> 00:06:49.386
And so when we're finally done with this for loop,

00:06:49.386 --> 00:06:51.190
inside of product —

00:06:51.190 --> 00:06:54.390
or I guess we can say product will be referring to —

00:06:54.390 --> 00:06:57.467
the actual factorial of the number inputted.

00:06:57.467 --> 00:07:00.733
So then we can say "print product".

00:07:00.733 --> 00:07:03.856
And in Python — in this case right here —

00:07:03.856 --> 00:07:07.379
the interpreter knows to break out of this for loop

00:07:07.379 --> 00:07:10.053
once it runs out of numbers to assign i to.

00:07:10.053 --> 00:07:12.342
So, that's why it doesn't run forever.

00:07:12.342 --> 00:07:17.325
So, let's see if what we did here actually works.

00:07:17.325 --> 00:07:19.322
Let's see if it actually works.

00:07:19.322 --> 00:07:22.696
So let me — actually — write like this.

00:07:22.696 --> 00:07:25.551
So let's see if this things actually works.

00:07:25.551 --> 00:07:29.667
So I'll save it. And then let me run it.

00:07:29.667 --> 00:07:31.164
And so it's asking me,

00:07:31.164 --> 00:07:33.378
"Enter a non-negative integer to take the factorial of:".

00:07:33.378 --> 00:07:35.622
So let's try it with 3 — Enter.

00:07:35.622 --> 00:07:37.200
It gave me the right answer.

00:07:37.200 --> 00:07:40.266
Let's try it again.

00:07:40.266 --> 00:07:44.230
So, "Enter a non-negative integer to take the factorial of:".

00:07:44.230 --> 00:07:47.067
I don't know, let's try something large: 10.

00:07:47.067 --> 00:07:50.668
It worked. [I] assume that you can verify this for yourself.

00:07:50.668 --> 00:07:52.513
So what we have done here is write a very simple program.

00:07:52.513 --> 00:07:53.641
But, it already does something neat —

00:07:53.641 --> 00:07:55.964
it takes a factorial of an arbitrary number.

00:07:55.964 --> 00:07:56.979
In next video,

00:07:56.979 --> 00:07:59.222
we're going to step through it much more carefully,

00:07:59.222 --> 00:08:02.650
in case this kind of confused you — what this for loop did.

00:08:02.650 --> 00:08:05.364
So, just hold with me for the next video, and watch that.

00:08:05.364 --> 00:08:09.123
That has a little bit of a more careful explanation.


Exercise - Write a Fibonacci Function

WEBVTT
Kind: captions
Language: en

00:00:00.570 --> 00:00:04.990
This right here is a
picture of Fibonacci,

00:00:04.990 --> 00:00:08.080
one of the most famous
mathematicians of all time,

00:00:08.080 --> 00:00:10.910
and he was a mathematician
in medieval Italy.

00:00:10.910 --> 00:00:13.610
And he's most famous for
the Fibonacci numbers.

00:00:19.210 --> 00:00:20.690
And he didn't discover them.

00:00:20.690 --> 00:00:23.106
They were actually discovered
several centuries before him

00:00:23.106 --> 00:00:26.570
in India, but he popularized
them, especially in the West.

00:00:26.570 --> 00:00:28.910
And the Fibonacci
numbers are super simple.

00:00:28.910 --> 00:00:32.610
The first two are
defined as 0 and 1.

00:00:32.610 --> 00:00:35.862
And every number after that is
the sum of the previous two.

00:00:35.862 --> 00:00:37.320
So what I'm
constructing right here

00:00:37.320 --> 00:00:39.764
is really a Fibonacci
sequence of numbers.

00:00:39.764 --> 00:00:41.180
So the next number
in the sequence

00:00:41.180 --> 00:00:44.050
is going to be 0 plus
1, which is 1, Then

00:00:44.050 --> 00:00:46.410
the next number after that
is going to be 1 plus 1,

00:00:46.410 --> 00:00:47.620
which is 2.

00:00:47.620 --> 00:00:50.370
Then the next number
after that 1 plus 2 is 3.

00:00:50.370 --> 00:00:52.860
2 plus 3 is 5.

00:00:52.860 --> 00:00:54.506
3 plus 5 is 8.

00:00:54.506 --> 00:00:57.420
5 plus 8 is 13.

00:00:57.420 --> 00:01:01.510
8 plus 13 is 21.

00:01:01.510 --> 00:01:05.680
13 plus 21 is 34.

00:01:05.680 --> 00:01:07.830
And the Fibonacci
numbers, especially once

00:01:07.830 --> 00:01:09.690
you start getting into
number theories, tons

00:01:09.690 --> 00:01:11.064
of fascinating
things about them.

00:01:11.064 --> 00:01:13.130
But probably the
coolest thing about them

00:01:13.130 --> 00:01:15.900
is as you add more and
more terms to the Fibonacci

00:01:15.900 --> 00:01:19.140
sequence, and you take
the last two terms

00:01:19.140 --> 00:01:21.761
that you've generated, obviously
there is no really last two

00:01:21.761 --> 00:01:22.260
terms.

00:01:22.260 --> 00:01:24.650
You could keep going on
forever, get arbitrarily

00:01:24.650 --> 00:01:26.180
large Fibonacci numbers.

00:01:26.180 --> 00:01:30.400
But say we take these last two
terms over here, 21 and 34.

00:01:30.400 --> 00:01:34.310
If we take the ratio of
these two, 21 over 34,

00:01:34.310 --> 00:01:38.150
this is going to be pretty
close to the golden ratio.

00:01:41.660 --> 00:01:45.242
And I encourage you to look up
the golden ratio on Wikipedia

00:01:45.242 --> 00:01:45.950
and the internet.

00:01:45.950 --> 00:01:48.530
You'll find all sorts of
fascinating and mystical things

00:01:48.530 --> 00:01:50.260
about the golden ratio.

00:01:50.260 --> 00:01:53.020
What's cool about the Fibonacci
numbers, or the Fibonacci

00:01:53.020 --> 00:01:54.920
sequence, this gives
you an approximation

00:01:54.920 --> 00:01:55.753
of the golden ratio.

00:01:55.753 --> 00:01:57.620
You'll get even a
better approximation

00:01:57.620 --> 00:02:00.490
if you add another
term to our sequence.

00:02:00.490 --> 00:02:04.430
So the next term over
here, 21 plus 34 is 55.

00:02:04.430 --> 00:02:13.530
So the ratio of 34 to 55 is
even closer to the golden ratio.

00:02:13.530 --> 00:02:15.200
So one way, if you
wanted to compute

00:02:15.200 --> 00:02:17.990
a really good approximation
for the golden ratio,

00:02:17.990 --> 00:02:20.920
you could really just get
super high Fibonacci numbers

00:02:20.920 --> 00:02:24.510
just adding the previous two
terms to get the next one.

00:02:24.510 --> 00:02:26.590
And you will get a pretty
good approximation,

00:02:26.590 --> 00:02:29.800
when you take the ratio
of the last two terms.

00:02:29.800 --> 00:02:32.290
Now that's what the
Fibonacci numbers are about,

00:02:32.290 --> 00:02:35.094
and now I want to pose
a challenge to you.

00:02:35.094 --> 00:02:36.760
I want you to write,
since we've already

00:02:36.760 --> 00:02:39.460
done some examples
using factorial,

00:02:39.460 --> 00:02:41.730
I want you to write
an implementation

00:02:41.730 --> 00:02:45.900
of a function that generates
the n-th term in the Fibonacci

00:02:45.900 --> 00:02:46.950
sequence.

00:02:46.950 --> 00:02:48.460
So the function
will be like this.

00:02:52.120 --> 00:02:55.860
So if I call your function--
Let me make it a lowercase.

00:02:55.860 --> 00:02:57.400
Let me just give
you some examples.

00:02:57.400 --> 00:03:00.999
So if I take your function,
and I call Fibonacci--

00:03:00.999 --> 00:03:03.540
and you could really implement
this in any language you want,

00:03:03.540 --> 00:03:04.530
although we've been
dealing in Python,

00:03:04.530 --> 00:03:06.770
it might be simplest
to do it in Python--

00:03:06.770 --> 00:03:12.130
if I call fibonacci of
1, what I want this to be

00:03:12.130 --> 00:03:13.464
is the first term.

00:03:13.464 --> 00:03:15.630
And just to make things
clear, and you should always

00:03:15.630 --> 00:03:17.680
clarify this, especially
in computer science,

00:03:17.680 --> 00:03:20.360
because it's not always
clear what the first term is.

00:03:20.360 --> 00:03:22.662
And I'm going to make
it clear right now.

00:03:22.662 --> 00:03:24.870
The first term is not going
to be this one over here.

00:03:24.870 --> 00:03:26.536
I'm going to make it
this one over here.

00:03:26.536 --> 00:03:29.390
I'm going to call
this the 0-th term.

00:03:29.390 --> 00:03:31.310
That's the 0-th
term, and then that

00:03:31.310 --> 00:03:32.980
is going to be the first term.

00:03:32.980 --> 00:03:35.590
This is going to be the second
term, third term, fourth term,

00:03:35.590 --> 00:03:37.170
so on and so forth.

00:03:37.170 --> 00:03:39.360
And so Fibonacci of
1, the first term

00:03:39.360 --> 00:03:40.880
will be this right over here.

00:03:40.880 --> 00:03:44.670
It should return 1.

00:03:44.670 --> 00:03:49.020
So Fibonacci of 0
should return 0.

00:03:49.020 --> 00:03:53.700
Fibonacci of 3 should
return 0, 1, 2, 3.

00:03:53.700 --> 00:03:55.110
It should return 2.

00:03:59.000 --> 00:04:04.700
Fibonacci of 5 should
return 0, 1, 2, 3, 4, 5.

00:04:04.700 --> 00:04:05.885
It should actually return 5.

00:04:05.885 --> 00:04:07.760
And what I want you to
do is write a function

00:04:07.760 --> 00:04:09.790
so we could put in
any argument over here

00:04:09.790 --> 00:04:14.670
and it will return that third
term of the Fibonacci sequence.


Comparing Iterative and Recursive Factorial Functions

WEBVTT
Kind: captions
Language: en

00:00:00.244 --> 00:00:01.660
What I want to do
in this video is

00:00:01.660 --> 00:00:05.625
make clear the distinction
between an iterative,

00:00:05.625 --> 00:00:08.000
or I should say iterative--
I always pronounce it wrong--

00:00:08.000 --> 00:00:13.570
an iterative function definition
and a recursive function

00:00:13.570 --> 00:00:15.890
definition.

00:00:15.890 --> 00:00:18.000
We'll do it really by
just kind of understanding

00:00:18.000 --> 00:00:21.150
where the iteration
is happening over here

00:00:21.150 --> 00:00:24.200
and where the recursion is
happening here on the right.

00:00:24.200 --> 00:00:26.190
So when we start off,
we see the product

00:00:26.190 --> 00:00:28.290
is set to be equal to 1.

00:00:28.290 --> 00:00:30.020
And then we enter our for loop.

00:00:30.020 --> 00:00:35.600
The for loop really is the
meat of this iterative function

00:00:35.600 --> 00:00:36.426
definition.

00:00:36.426 --> 00:00:38.550
And understanding what's
happening in the for loop,

00:00:38.550 --> 00:00:40.170
let me make a little table here.

00:00:40.170 --> 00:00:44.210
So I'm going to make a table
for the value of our variable i,

00:00:44.210 --> 00:00:52.860
and I'll also figure out what
the value of product times

00:00:52.860 --> 00:00:53.740
i plus 1.

00:00:53.740 --> 00:00:56.610
Because every iteration
through this for loop,

00:00:56.610 --> 00:00:59.500
we're going to evaluate this
business right over here.

00:00:59.500 --> 00:01:01.260
And then I'm going
to make a column

00:01:01.260 --> 00:01:03.275
for the new value
of our product.

00:01:07.270 --> 00:01:09.560
So let me underline
these things.

00:01:09.560 --> 00:01:12.190
And then we have the new
value of our product.

00:01:12.190 --> 00:01:15.365
So we learned in many videos
ago that in Python, we

00:01:15.365 --> 00:01:17.820
say for i in range.

00:01:17.820 --> 00:01:23.080
This range part right
over here returns a list,

00:01:23.080 --> 00:01:26.060
and it returns a list of
the number of elements

00:01:26.060 --> 00:01:27.380
that we pass number.

00:01:27.380 --> 00:01:29.276
We pass into it right over here.

00:01:29.276 --> 00:01:31.150
So if we assume-- and
I should have said this

00:01:31.150 --> 00:01:34.140
from the get-go-- let's assume
that we're calling-- just

00:01:34.140 --> 00:01:35.315
give me something specific.

00:01:35.315 --> 00:01:36.690
Let's say that
this is the result

00:01:36.690 --> 00:01:45.320
of a call of factorial of 3.

00:01:45.320 --> 00:01:48.490
So the argument that we
passed a factorial is 3, so

00:01:48.490 --> 00:01:51.640
the variable number
will refer to 3.

00:01:51.640 --> 00:01:53.950
So when you call
range of number,

00:01:53.950 --> 00:01:58.330
it will literally
return a list 0, 1, 2.

00:01:58.330 --> 00:02:00.370
So three elements starting at 0.

00:02:00.370 --> 00:02:02.550
The last element is 3 minus 1.

00:02:02.550 --> 00:02:03.870
It is 2.

00:02:03.870 --> 00:02:06.310
And so each loop
through this for loop,

00:02:06.310 --> 00:02:09.350
i is going to be assigned
to each successive element

00:02:09.350 --> 00:02:09.900
in this list.

00:02:09.900 --> 00:02:12.180
So on the first time
through this for loop,

00:02:12.180 --> 00:02:15.200
i is going to be assigned to 0.

00:02:15.200 --> 00:02:18.390
So our i is going to refer to 0.

00:02:18.390 --> 00:02:22.100
And then product times i plus 1.

00:02:22.100 --> 00:02:23.957
Well, in this
first loop product,

00:02:23.957 --> 00:02:25.540
up here before we
even enter the loop,

00:02:25.540 --> 00:02:28.480
product was defined to be 1.

00:02:28.480 --> 00:02:30.990
So product is going
to be 1, and it's

00:02:30.990 --> 00:02:35.800
1 times-- I don't want
to do it in that color.

00:02:35.800 --> 00:02:41.090
I'll just do it in
magenta-- 1 times

00:02:41.090 --> 00:02:47.205
i, which is 0-- 1
times 0, plus 1.

00:02:50.366 --> 00:02:51.740
And then our new
value or product

00:02:51.740 --> 00:02:53.210
is essentially this evaluated.

00:02:53.210 --> 00:02:54.126
We have it right here.

00:02:54.126 --> 00:02:56.800
Product is equal to
all of this business.

00:02:56.800 --> 00:02:59.084
And so our new value
is 1 times 0 plus 1.

00:02:59.084 --> 00:03:00.250
Well, that's just 1 times 1.

00:03:00.250 --> 00:03:01.990
And that's just going to be 1.

00:03:01.990 --> 00:03:04.754
And that's all we had
inside the for loop clause,

00:03:04.754 --> 00:03:06.170
because that's the
only stuff that

00:03:06.170 --> 00:03:08.540
was indented within
this for loop.

00:03:08.540 --> 00:03:13.300
And so then we go
back up, and we're

00:03:13.300 --> 00:03:15.774
going to iterate through the
next iteration of our loop,

00:03:15.774 --> 00:03:16.690
I guess you could say.

00:03:16.690 --> 00:03:19.175
And now i is going
to be assigned to 1.

00:03:22.600 --> 00:03:24.210
This expression
over here-- we're

00:03:24.210 --> 00:03:26.880
going to take our old product.

00:03:26.880 --> 00:03:30.270
So product is still 1.

00:03:30.270 --> 00:03:37.560
And it's going to be times
i, which is now 1 plus 1.

00:03:40.290 --> 00:03:42.190
And what's this
going to be equal to?

00:03:42.190 --> 00:03:45.490
Well if you evaluate all
of this, you get 1 times 2.

00:03:45.490 --> 00:03:49.960
So now the new value for product
is 2 after our second iteration

00:03:49.960 --> 00:03:53.650
through the loop-- our
second pass through the loop.

00:03:53.650 --> 00:03:57.220
And now it will go back to
the beginning of the for loop,

00:03:57.220 --> 00:04:00.090
and i will be assigned to
the next element in the list.

00:04:00.090 --> 00:04:02.550
It'll now be assigned to 2.

00:04:02.550 --> 00:04:04.130
So i is now 2.

00:04:04.130 --> 00:04:07.250
This thing over here,
it's going to be product.

00:04:07.250 --> 00:04:09.020
Well product is now 2.

00:04:09.020 --> 00:04:13.480
So it's going to be 2 times i.

00:04:13.480 --> 00:04:16.510
Well i is now 2 plus 1.

00:04:19.370 --> 00:04:22.940
And so what does this--
this is 2 times 3, or 6.

00:04:25.560 --> 00:04:27.640
And then it'll go,
and it'll say, OK,

00:04:27.640 --> 00:04:29.900
can we assign i to any
more elements of this?

00:04:29.900 --> 00:04:31.150
No, we've run out of elements.

00:04:31.150 --> 00:04:33.080
So now we break out
of the for loop,

00:04:33.080 --> 00:04:38.740
and we just return the product
or the variable product

00:04:38.740 --> 00:04:39.790
it was referring to.

00:04:39.790 --> 00:04:41.290
And that's what I
should really say.

00:04:41.290 --> 00:04:44.440
We should return the value of
the product is referring to,

00:04:44.440 --> 00:04:46.610
and that value is 6.

00:04:46.610 --> 00:04:50.030
So when you call factorial
of 3, it will return 6.

00:04:50.030 --> 00:05:00.060
So if you were to say factorial
of 3 plus factorial of 3,

00:05:00.060 --> 00:05:02.460
and you were to evaluate
this expression,

00:05:02.460 --> 00:05:05.350
this expression
would evaluate to 6,

00:05:05.350 --> 00:05:08.236
and this expression over
here would evaluate to 6,

00:05:08.236 --> 00:05:10.110
because that's what the
function will return.

00:05:10.110 --> 00:05:14.120
And then you add those up,
and they would evaluate to 12.

00:05:14.120 --> 00:05:15.620
So this is why we
call it iterative.

00:05:15.620 --> 00:05:19.890
We kept iterating through
the same set of instructions.

00:05:19.890 --> 00:05:22.359
Now let's compare the
recursive definition.

00:05:22.359 --> 00:05:24.650
And this one is a little bit
more fun in a lot of ways.

00:05:24.650 --> 00:05:30.100
So once again, we're going
to call factorial of 3.

00:05:30.100 --> 00:05:31.450
So 3 is our argument.

00:05:31.450 --> 00:05:34.520
That's the value that number
will refer to, it'll take on.

00:05:34.520 --> 00:05:36.720
It says if number is
less than or equal to 1.

00:05:36.720 --> 00:05:38.365
Well, 3 is not less
than or equal to 1.

00:05:38.365 --> 00:05:40.240
So we're not going to
do this part over here.

00:05:40.240 --> 00:05:42.010
We're going to do
the else clause.

00:05:42.010 --> 00:05:49.100
So we're going to return number
times factorial of all of this.

00:05:49.100 --> 00:05:55.700
So this is going to evaluate
to number, which is 3--

00:05:55.700 --> 00:06:06.961
that's the argument we passed---
times factorial of number minus

00:06:06.961 --> 00:06:07.460
1.

00:06:07.460 --> 00:06:09.700
Well number minus 1 is
going to evaluate to 2.

00:06:09.700 --> 00:06:12.250
3 minus 1 is 2, So
times factorial of 2.

00:06:12.250 --> 00:06:14.790
Well, that's just another
function called a factorial.

00:06:14.790 --> 00:06:16.440
So we go back, OK, factorial.

00:06:16.440 --> 00:06:18.740
But now the argument
is 2, so number is 2.

00:06:18.740 --> 00:06:19.754
We go here.

00:06:19.754 --> 00:06:21.670
If number is less than
or equal to 1, do this.

00:06:21.670 --> 00:06:22.980
Well, the number isn't
less than or equal to 1.

00:06:22.980 --> 00:06:23.520
It's 2.

00:06:23.520 --> 00:06:25.150
So now we do else.

00:06:25.150 --> 00:06:28.180
So what we now want to
return is the number

00:06:28.180 --> 00:06:29.930
times a factorial
of number minus 1.

00:06:29.930 --> 00:06:34.492
Well, in this situation,
the number is now 2,

00:06:34.492 --> 00:06:35.950
and we're going to
want to multiply

00:06:35.950 --> 00:06:43.620
that times the
factorial of 2 minus 1.

00:06:43.620 --> 00:06:46.692
Well 2 minus 1 is just 1--
times the factorial of 1.

00:06:46.692 --> 00:06:48.400
Well, we just made
another function call,

00:06:48.400 --> 00:06:50.964
so the interpreter has
to kind of remember

00:06:50.964 --> 00:06:52.880
that we've made this
whole series of functions

00:06:52.880 --> 00:06:55.680
calls and has to keep digging
deeper and deeper and deeper.

00:06:55.680 --> 00:07:00.670
So now we've called factorial
of 1, and 1 is the argument.

00:07:00.670 --> 00:07:02.010
Number is referring to 1.

00:07:02.010 --> 00:07:04.119
If number is less than
or equal to 1-- number

00:07:04.119 --> 00:07:05.410
is less than or equal to 1 now.

00:07:05.410 --> 00:07:06.868
This is why we call
it a base case.

00:07:06.868 --> 00:07:08.560
We're kind of going down to it.

00:07:08.560 --> 00:07:12.090
So if the number is less
than or equal to 1, return 1.

00:07:12.090 --> 00:07:14.860
So in this situation, when
we call factorial of 1,

00:07:14.860 --> 00:07:16.840
it literally returns 1.

00:07:16.840 --> 00:07:21.180
And so we now know
that factorial of 2

00:07:21.180 --> 00:07:23.210
evaluates to 2 times 1.

00:07:23.210 --> 00:07:26.030
So this evaluates to 2.

00:07:26.030 --> 00:07:29.070
And now we know that factorial
of 3 evaluates to 3 times

00:07:29.070 --> 00:07:36.180
2, which will evaluate to 6.

00:07:36.180 --> 00:07:37.992
So very different ways
of thinking about it

00:07:37.992 --> 00:07:39.650
but getting you the
exact same result.

00:07:39.650 --> 00:07:41.234
Once again, if you
take factorial of 3

00:07:41.234 --> 00:07:42.816
plus factorial of
3, it doesn't matter

00:07:42.816 --> 00:07:44.140
which way we implement it.

00:07:44.140 --> 00:07:46.661
We'll get 6 plus 6, or 12.