Recursive Factorial Function

WEBVTT
Kind: captions
Language: en

00:00:00.630 --> 00:00:03.010
What I want to do in this
video is introduce you

00:00:03.010 --> 00:00:07.660
to what I think is one of
the neatest ideas in computer

00:00:07.660 --> 00:00:09.620
science, and that is recursion.

00:00:09.620 --> 00:00:11.984
So the way that we've defined
this factorial function

00:00:11.984 --> 00:00:13.400
in the last few
videos is actually

00:00:13.400 --> 00:00:15.060
an iterative definition.

00:00:15.060 --> 00:00:18.470
We are iterating
through different values

00:00:18.470 --> 00:00:20.250
for this variable
i, and then we're

00:00:20.250 --> 00:00:22.880
essentially taking those values,
adding one, then multiplying

00:00:22.880 --> 00:00:24.542
those times our
original product.

00:00:24.542 --> 00:00:26.500
And when we keep iterating
through all of that,

00:00:26.500 --> 00:00:29.190
our eventual product
has all of the values

00:00:29.190 --> 00:00:32.340
from 1 through the number
multiplied by each other,

00:00:32.340 --> 00:00:34.447
and they're all sitting
in this variable product.

00:00:34.447 --> 00:00:36.530
What I'm going to do here
is rewrite this program.

00:00:36.530 --> 00:00:38.242
And what's cool
about functions--

00:00:38.242 --> 00:00:39.700
I'm going to rewrite
this function.

00:00:39.700 --> 00:00:42.450
What's cool about functions
are this function,

00:00:42.450 --> 00:00:44.860
we just say, look, it returns
a factorial of a number.

00:00:44.860 --> 00:00:47.291
We don't care how this
is actually written.

00:00:47.291 --> 00:00:49.290
And so anything that's
using this function call,

00:00:49.290 --> 00:00:51.581
no matter how I implement
it, as long as I implement it

00:00:51.581 --> 00:00:53.510
correctly, it shouldn't
worry about it.

00:00:53.510 --> 00:00:57.170
So as long as I implement the
guts of this function right,

00:00:57.170 --> 00:00:59.160
even if I do it in a
very different way,

00:00:59.160 --> 00:01:02.240
it shouldn't affect the
behavior of any of the functions

00:01:02.240 --> 00:01:03.210
that call it.

00:01:03.210 --> 00:01:06.200
So let's go ahead and try
to define this function

00:01:06.200 --> 00:01:07.610
recursively.

00:01:07.610 --> 00:01:10.586
And I think you're going to
find this slightly fascinating.

00:01:10.586 --> 00:01:12.460
So I'm going to do it
a little bit different.

00:01:12.460 --> 00:01:15.660
We're going to put some
conditionals in here as well.

00:01:15.660 --> 00:01:18.840
So I'm going to say-- so
it's almost-- in some ways,

00:01:18.840 --> 00:01:20.720
recursion is really
deep and complicated,

00:01:20.720 --> 00:01:23.840
and on some levels, it's way
simpler than anything else.

00:01:23.840 --> 00:01:27.200
So let me just put all
of this stuff down there.

00:01:27.200 --> 00:01:29.520
So I'm going to say, look,
if the number-- you always

00:01:29.520 --> 00:01:30.853
want to think about a base case.

00:01:30.853 --> 00:01:35.340
What's a situation where kind
of the nugget or the smallest

00:01:35.340 --> 00:01:37.730
values we can put into
for a number that'll

00:01:37.730 --> 00:01:39.450
give us a decent response?

00:01:39.450 --> 00:01:45.810
So we'll say, look,
if number is--

00:01:45.810 --> 00:01:49.920
let's say if it's less
than or equal to 1,

00:01:49.920 --> 00:01:53.420
so if it's less than or equal
to 1, let's just return.

00:01:53.420 --> 00:01:54.620
Well, then it's factorial.

00:01:54.620 --> 00:01:56.161
Let's just return
1, especially if we

00:01:56.161 --> 00:01:58.440
want to have the same
behavior as the old function.

00:01:58.440 --> 00:02:00.330
The old function,
regardless of whether we

00:02:00.330 --> 00:02:02.050
put a 1 there, a 0
there, or actually,

00:02:02.050 --> 00:02:04.050
even if we put a negative
1 or negative 2 there,

00:02:04.050 --> 00:02:07.390
it always gave us the
factorial being equal to 1.

00:02:07.390 --> 00:02:09.289
So that's exactly what
we're doing over here.

00:02:09.289 --> 00:02:11.730
We're returning 1 if the
number is less than 1.

00:02:11.730 --> 00:02:14.900
And this also defines
our base case.

00:02:14.900 --> 00:02:16.420
I can even label it here.

00:02:16.420 --> 00:02:20.570
So let me label--
this is our base case.

00:02:20.570 --> 00:02:23.770
And you'll see what I mean
for a second by base case.

00:02:23.770 --> 00:02:25.470
And what I'm about
to do now, this

00:02:25.470 --> 00:02:28.160
is the really exciting
part about recursion.

00:02:28.160 --> 00:02:32.990
If the number is not
less than or equal to 1,

00:02:32.990 --> 00:02:36.680
so I could write
l if-- well, maybe

00:02:36.680 --> 00:02:38.480
I'll just write l, actually.

00:02:38.480 --> 00:02:40.960
So if the number is not
less than or equal to 1,

00:02:40.960 --> 00:02:43.470
I'm going to return
something different.

00:02:43.470 --> 00:02:47.300
And what I'm going to
return is that number

00:02:47.300 --> 00:02:53.160
times the factorial--
times the factorial of one

00:02:53.160 --> 00:02:55.090
less than that number.

00:02:55.090 --> 00:02:57.790
Now, the reason why this
is fascinating and cool

00:02:57.790 --> 00:03:01.120
and all the rest is I
just defined something

00:03:01.120 --> 00:03:02.990
using that something.

00:03:02.990 --> 00:03:05.670
So I just defined a
function called factorial,

00:03:05.670 --> 00:03:08.535
and I defined it using the
definition of the function

00:03:08.535 --> 00:03:09.035
factorial.

00:03:09.035 --> 00:03:10.940
It is referring to itself.

00:03:10.940 --> 00:03:12.815
This is what recursive
functions do.

00:03:12.815 --> 00:03:14.940
But if you think about it--
and I'll diagram it out

00:03:14.940 --> 00:03:16.250
in more detail in
the next video,

00:03:16.250 --> 00:03:18.230
so hopefully it makes a
little bit more sense--

00:03:18.230 --> 00:03:20.230
it should kind of work.

00:03:20.230 --> 00:03:23.850
Because if you put a 1
or a 0 in for a number,

00:03:23.850 --> 00:03:25.020
it'll just return 1.

00:03:25.020 --> 00:03:27.170
What happens if the number is 2?

00:03:27.170 --> 00:03:29.310
Well, if the number
is 2, it'll say, look,

00:03:29.310 --> 00:03:31.643
the number is not less than
our 1, so I'll go over here.

00:03:31.643 --> 00:03:37.540
So it says it'll return 2
times factorial of 2 minus 1,

00:03:37.540 --> 00:03:39.190
or factorial of 1.

00:03:39.190 --> 00:03:42.580
And then it'll call factorial
again, and factorial of 1

00:03:42.580 --> 00:03:44.980
is just 1, so it'll
return 2 times 1.

00:03:44.980 --> 00:03:46.660
So that'll work out for 2.

00:03:46.660 --> 00:03:47.880
Try it for 3.

00:03:47.880 --> 00:03:50.686
If you do a
factorial of 3, it'll

00:03:50.686 --> 00:03:52.060
go down to this
clause over here,

00:03:52.060 --> 00:03:53.768
because 3 is not less
than or equal to 1,

00:03:53.768 --> 00:03:58.340
and it'll return 3 times
the factorial of 3 minus 1,

00:03:58.340 --> 00:03:59.719
which is 2, the factorial of 2.

00:03:59.719 --> 00:04:01.760
Well, we know it already
returns the right answer

00:04:01.760 --> 00:04:04.480
for the factorial of 2, so
it'll return 3 times 2, which

00:04:04.480 --> 00:04:06.379
is 6, which is a factorial of 3.

00:04:06.379 --> 00:04:08.170
So hopefully you're
getting the gist of it.

00:04:08.170 --> 00:04:10.586
Factorial of 4 is going to
work for the exact same reason.

00:04:10.586 --> 00:04:12.990
It's going to boil down to
4 times the factorial of 3.

00:04:12.990 --> 00:04:15.209
We know that it works
for the factorial of 3.

00:04:15.209 --> 00:04:16.750
And just to prove
to you that I'm not

00:04:16.750 --> 00:04:19.410
doing some type of crazy voodoo,
and that this will actually

00:04:19.410 --> 00:04:22.070
work, let's try to
run the program.

00:04:22.070 --> 00:04:26.130
Let's try to run the
program right here.

00:04:26.130 --> 00:04:28.425
This is some stuff that
I was doing beforehand.

00:04:28.425 --> 00:04:30.170
Let me just get rid of that.

00:04:30.170 --> 00:04:31.710
So let's just run the program.

00:04:31.710 --> 00:04:33.470
So all I've done
is I've redefined

00:04:33.470 --> 00:04:39.210
the factorial function, but
I've redefined it recursively.

00:04:39.210 --> 00:04:40.710
I won't have to
change the comments.

00:04:40.710 --> 00:04:45.050
So let me save it, and let's
interpret this program.

00:04:45.050 --> 00:04:48.540
Let's execute it and see if
it does what we need it to do.

00:04:48.540 --> 00:04:50.790
All right.

00:04:50.790 --> 00:04:52.550
So they're prompting
us for something.

00:04:52.550 --> 00:04:55.920
Let's take the
factorial of 5-- 120.

00:04:55.920 --> 00:04:58.179
And so once again,
our factorial program,

00:04:58.179 --> 00:05:00.720
no matter what we put in there,
it gives us the right answer.

00:05:00.720 --> 00:05:03.410
But what's really cool is
it's now doing it recursively.

00:05:03.410 --> 00:05:06.910
When I do factorial of 6,
say, OK, factorial of 6.

00:05:06.910 --> 00:05:08.560
Is 6 less than or equal to 1?

00:05:08.560 --> 00:05:10.620
Well, no it's not, so
it goes to this else

00:05:10.620 --> 00:05:11.990
clause right over here.

00:05:11.990 --> 00:05:15.200
So then it says return 6
times the factorial of 5.

00:05:15.200 --> 00:05:17.150
Then it says, OK,
let me remember

00:05:17.150 --> 00:05:18.420
6 times the factorial of 5.

00:05:18.420 --> 00:05:20.253
Let's figure out what
the factorial of 5 is.

00:05:20.253 --> 00:05:24.410
Well, factorial of 5 is going to
be 5 times the factorial of 4.

00:05:24.410 --> 00:05:26.900
And then that factorial of 4
is 4 times the factorial of 3,

00:05:26.900 --> 00:05:29.160
all the way down to the
factorial of 1, which is 1.

00:05:29.160 --> 00:05:32.440
And so it'll actually return 6
times 5 times 4 times 3 times

00:05:32.440 --> 00:05:35.180
2 times 1.


Diagramming What Happens with a Function Call

WEBVTT
Kind: captions
Language: en

00:00:00.342 --> 00:00:02.050
What I want to do in
this video is really

00:00:02.050 --> 00:00:05.450
think about what happens
when we define a function,

00:00:05.450 --> 00:00:08.420
and actually what happens as
we step through the program

00:00:08.420 --> 00:00:09.830
and we use the function.

00:00:09.830 --> 00:00:11.860
So let's say we
write this program,

00:00:11.860 --> 00:00:15.280
just like we did in the
last video, and we run it.

00:00:15.280 --> 00:00:17.760
So the interpreter will start
at this first line over here.

00:00:17.760 --> 00:00:19.760
It says, oh look, there's
a function definition.

00:00:19.760 --> 00:00:22.590
We're defining the
function factorial.

00:00:22.590 --> 00:00:24.990
What I'm going to do here is
I'm going to make a big box.

00:00:24.990 --> 00:00:27.340
And this big box
is the environment.

00:00:27.340 --> 00:00:30.310
And an environment
for a program is

00:00:30.310 --> 00:00:32.650
all of the different
definitions,

00:00:32.650 --> 00:00:34.480
and variable definitions,
and everything

00:00:34.480 --> 00:00:39.420
that are specific to the context
that the program is running in.

00:00:39.420 --> 00:00:43.680
So immediately, the program will
associate the name factorial

00:00:43.680 --> 00:00:45.910
with this whole function
right over here.

00:00:45.910 --> 00:00:55.480
So right when it says
define factorial,

00:00:55.480 --> 00:01:00.380
it'll actually associate
factorial-- the label

00:01:00.380 --> 00:01:04.150
factorial, you could say it--
it'll associate the label

00:01:04.150 --> 00:01:09.180
factorial with a function that
looks like all of this business

00:01:09.180 --> 00:01:10.450
right over here.

00:01:10.450 --> 00:01:15.020
So you'll pass it a number.

00:01:15.020 --> 00:01:17.960
And I'm not using-- well,
maybe I'll write it like this.

00:01:20.730 --> 00:01:22.470
It's a function.

00:01:22.470 --> 00:01:25.240
And I'm not using the-- you'll
see different notations, lambda

00:01:25.240 --> 00:01:26.740
notation and all
of this fancy stuff

00:01:26.740 --> 00:01:28.573
if you take a formal
computer science class.

00:01:28.573 --> 00:01:30.760
But all I'm just saying
is factorial refers

00:01:30.760 --> 00:01:38.910
to a function that has
the parameters-- that

00:01:38.910 --> 00:01:39.920
only has one parameter.

00:01:39.920 --> 00:01:42.290
It has the parameter number.

00:01:42.290 --> 00:01:49.930
And then given this function
with the parameter number,

00:01:49.930 --> 00:01:51.940
it'll then process this code.

00:01:51.940 --> 00:01:54.590
So let me just copy and
paste it right here.

00:01:54.590 --> 00:01:58.490
And then-- whoops-- I'm
doing it in the wrong layer.

00:01:58.490 --> 00:02:00.040
Let me copy and paste it again.

00:02:00.040 --> 00:02:04.570
So let me try to copy it.

00:02:04.570 --> 00:02:05.780
Let me copy it.

00:02:05.780 --> 00:02:08.620
And then let me paste it.

00:02:08.620 --> 00:02:11.279
So that's what it's
doing right over there.

00:02:11.279 --> 00:02:12.820
And actually, I
should make this box.

00:02:12.820 --> 00:02:14.370
The environment
includes all of this.

00:02:14.370 --> 00:02:15.536
I think you'll bear with me.

00:02:15.536 --> 00:02:18.170
I didn't draw this orange
box big enough the first time

00:02:18.170 --> 00:02:19.560
around.

00:02:19.560 --> 00:02:22.285
So let me do that just so
that no one gets confused.

00:02:22.285 --> 00:02:24.030
Just so that no
one gets confused,

00:02:24.030 --> 00:02:25.970
let me expand the orange box.

00:02:28.620 --> 00:02:31.910
And I erased a little bit.

00:02:31.910 --> 00:02:36.212
One of the parameters is number.

00:02:36.212 --> 00:02:38.420
And then that orange box
goes around the whole thing.

00:02:41.740 --> 00:02:43.950
That's a green color.

00:02:43.950 --> 00:02:45.890
All right, here we go.

00:02:45.890 --> 00:02:48.930
So right when we hit this
first few lines, it's saying,

00:02:48.930 --> 00:02:52.210
look, in our entire
global environment,

00:02:52.210 --> 00:02:53.620
we're defining a function.

00:02:53.620 --> 00:02:55.800
The factorial will now
refer to a function

00:02:55.800 --> 00:02:57.360
that takes the parameter number.

00:02:57.360 --> 00:03:00.460
And then it'll run all of this
code that we've defined here.

00:03:00.460 --> 00:03:03.560
And then it'll return whatever
inside this function world,

00:03:03.560 --> 00:03:06.670
whatever the value of
the variable product is.

00:03:06.670 --> 00:03:08.810
And then it keeps going
line by line by line.

00:03:08.810 --> 00:03:10.750
And then we get to this
line right over here.

00:03:10.750 --> 00:03:14.390
And it says, the variable
user input is equal to--

00:03:14.390 --> 00:03:16.680
and then it calls
the function input.

00:03:16.680 --> 00:03:18.550
And it passes the
function input.

00:03:18.550 --> 00:03:20.841
And I'm starting to use a
little bit of the terminology

00:03:20.841 --> 00:03:22.130
that we've been exposed to.

00:03:22.130 --> 00:03:25.300
It passes the function
input a string.

00:03:25.300 --> 00:03:26.830
So this right here is a string.

00:03:26.830 --> 00:03:29.230
And once again, string
sounds like a fancy word.

00:03:29.230 --> 00:03:31.830
But it literally means
string of characters,

00:03:31.830 --> 00:03:33.940
or this text right
here, the text

00:03:33.940 --> 00:03:35.690
enter a non-negative
integer to take

00:03:35.690 --> 00:03:37.680
the factorial of colon space.

00:03:37.680 --> 00:03:39.880
So even the spaces are
part of the strings.

00:03:39.880 --> 00:03:41.940
And the telltale
sign for a string

00:03:41.940 --> 00:03:44.640
is it's going to be inside
some type of quotation marks,

00:03:44.640 --> 00:03:47.060
either double or
single quotation marks.

00:03:47.060 --> 00:03:49.830
So by passing this to
that input function,

00:03:49.830 --> 00:03:53.110
that tells the input function
what to prompt the user.

00:03:53.110 --> 00:03:55.800
So it prompts the
user with this string.

00:03:55.800 --> 00:03:59.420
The user types in a number
or some type of expression.

00:03:59.420 --> 00:04:03.240
That gets evaluated, and then
gets stored in user input.

00:04:03.240 --> 00:04:06.240
Or I really should say, in
then the Python context,

00:04:06.240 --> 00:04:08.920
user input refers to that value.

00:04:08.920 --> 00:04:10.915
So let's say the user inputs 3.

00:04:13.692 --> 00:04:20.860
Then the variable
user input will

00:04:20.860 --> 00:04:22.860
refer, in this
global environment--

00:04:22.860 --> 00:04:29.760
now factorial refers to
a function-- user input

00:04:29.760 --> 00:04:32.910
refers to whatever
the user typed in.

00:04:32.910 --> 00:04:35.350
I'm going to go with the
specific example of 3.

00:04:35.350 --> 00:04:36.770
Then we go to the next line.

00:04:36.770 --> 00:04:39.270
And it's saying, look, make
factorial of user input.

00:04:39.270 --> 00:04:42.900
Make this entire variable
refer to whatever

00:04:42.900 --> 00:04:47.520
we get when we call factorial
of user input, factorial

00:04:47.520 --> 00:04:49.330
of this variable over here.

00:04:49.330 --> 00:04:50.990
And so this is the
interesting part.

00:04:50.990 --> 00:04:55.890
At this point in the program,
the factorial of user input,

00:04:55.890 --> 00:05:02.140
this part right over here, it
makes a call to this function.

00:05:02.140 --> 00:05:04.900
It makes a call
to this function.

00:05:04.900 --> 00:05:09.010
And it gives it the argument
of whatever user input is.

00:05:09.010 --> 00:05:14.620
Now, user input
is referring to 3.

00:05:14.620 --> 00:05:18.120
So it is going to pass 3
to the function factorial.

00:05:18.120 --> 00:05:19.780
So 3 gets passed here.

00:05:19.780 --> 00:05:22.380
And essentially, within
this function world--

00:05:22.380 --> 00:05:25.110
so now let's say that this
is the context of running

00:05:25.110 --> 00:05:28.790
this function-- so within
this function world--

00:05:28.790 --> 00:05:29.990
let me make it clear.

00:05:29.990 --> 00:05:32.100
So now, within this
function world right

00:05:32.100 --> 00:05:36.830
over here, the
function's environment,

00:05:36.830 --> 00:05:48.877
the variable number within this
function world now refers to 3.

00:05:48.877 --> 00:05:50.460
I now want to be a
little bit careful,

00:05:50.460 --> 00:05:53.870
because we're starting to
touch on the idea of scope

00:05:53.870 --> 00:05:56.750
of a variable.

00:05:56.750 --> 00:05:58.710
The variable number in
this function world,

00:05:58.710 --> 00:06:01.620
it is only referenceable
within the function.

00:06:01.620 --> 00:06:03.910
And we do refer it
within the function.

00:06:03.910 --> 00:06:06.810
It is not a referable
outside of the function.

00:06:06.810 --> 00:06:08.570
So if down here
someplace, I were

00:06:08.570 --> 00:06:11.350
to say print number,
you would get an error,

00:06:11.350 --> 00:06:14.370
because it would be out of
the scope of that variable.

00:06:14.370 --> 00:06:16.930
This variable, it's a
parameter to this function.

00:06:16.930 --> 00:06:18.730
It's only usable
within this function.

00:06:18.730 --> 00:06:20.800
Its scope, the place
where you can refer to it,

00:06:20.800 --> 00:06:23.100
is only in this function.

00:06:23.100 --> 00:06:24.770
So you get that number.

00:06:24.770 --> 00:06:27.380
And then it does the code
that we've looked at before.

00:06:27.380 --> 00:06:28.715
It sets the variable product.

00:06:31.780 --> 00:06:37.790
So the variable product
now refers to the number 1.

00:06:37.790 --> 00:06:39.200
Then it calls all of this code.

00:06:39.200 --> 00:06:40.390
And since we've
talked about scope,

00:06:40.390 --> 00:06:42.181
I'll talk about another
situation of scope.

00:06:42.181 --> 00:06:45.580
This variable i is only
valid within the scope

00:06:45.580 --> 00:06:46.270
of the for loop.

00:06:46.270 --> 00:06:49.480
If down here I said return i,
that would have given an error.

00:06:49.480 --> 00:06:52.300
You can only refer to
it within the for loop.

00:06:52.300 --> 00:06:54.380
And we've gone through
this code multiple times.

00:06:54.380 --> 00:06:57.790
It goes from i equals 0 to
i equals 1 to i equals 2,

00:06:57.790 --> 00:07:00.620
all the way up to i is equal
to 1 less than the number.

00:07:00.620 --> 00:07:02.630
But since we're adding
1 to i every time,

00:07:02.630 --> 00:07:04.780
it's really going from--
this whole expression

00:07:04.780 --> 00:07:08.280
right over here starts at 1,
then 2, then 3, all the way up

00:07:08.280 --> 00:07:09.080
to the number.

00:07:09.080 --> 00:07:11.780
And each time, we're multiplying
it times the original product

00:07:11.780 --> 00:07:13.130
to get a new product.

00:07:13.130 --> 00:07:15.200
So at the end of
the day, after we

00:07:15.200 --> 00:07:21.280
go through this whole
for loop, product

00:07:21.280 --> 00:07:27.260
will contain the
factorial of the number.

00:07:27.260 --> 00:07:28.800
So after you do
this, product will

00:07:28.800 --> 00:07:30.380
keep referring to
a bunch of things.

00:07:30.380 --> 00:07:31.740
It'll refer to 1.

00:07:31.740 --> 00:07:33.200
Then it'll refer to 2.

00:07:33.200 --> 00:07:35.970
Then it'll refer to 6, because
we're going to do 3 times 2.

00:07:35.970 --> 00:07:46.400
So eventually, product
is going to refer to 6.

00:07:46.400 --> 00:07:47.275
And that is returned.

00:07:47.275 --> 00:07:49.110
And when I say that
that is returned,

00:07:49.110 --> 00:07:55.040
that means when you evaluate
this entire thing over here,

00:07:55.040 --> 00:07:59.760
that entire thing
is going to be 6.

00:07:59.760 --> 00:08:03.580
And then once we get
out of that program,

00:08:03.580 --> 00:08:15.230
it let's the interpreter know
that factorial of user input

00:08:15.230 --> 00:08:17.404
should now refer to that 6.

00:08:17.404 --> 00:08:19.570
I know this might be a
little bit confusing with all

00:08:19.570 --> 00:08:20.420
of this diagramming.

00:08:20.420 --> 00:08:22.878
But I really want you to get
the sense of what's happening.

00:08:22.878 --> 00:08:24.520
User input is
referring to something.

00:08:24.520 --> 00:08:27.360
We pass that something
to the factorial program.

00:08:27.360 --> 00:08:28.870
So then we go up here.

00:08:28.870 --> 00:08:30.610
And within the
factorial program,

00:08:30.610 --> 00:08:32.429
now, since we've passed
user input and user

00:08:32.429 --> 00:08:35.429
input was 3, within
the factorial program,

00:08:35.429 --> 00:08:38.200
this number is
going to refer to 3.

00:08:38.200 --> 00:08:40.360
And then we run this
as if number was 3.

00:08:40.360 --> 00:08:42.289
And then we return
product, which

00:08:42.289 --> 00:08:44.550
is going to be the
factorial of number.

00:08:44.550 --> 00:08:46.610
And so this whole
thing evaluates

00:08:46.610 --> 00:08:50.390
to the factorial of whatever was
in here, so the factorial of 3.

00:08:50.390 --> 00:08:53.100
And so factorial of user
input, this variable,

00:08:53.100 --> 00:08:55.990
will now refer to that, because
it's being assigned to that.

00:08:55.990 --> 00:08:57.565
And then we print it.

00:08:57.565 --> 00:08:58.940
And when we print
it, that's what

00:08:58.940 --> 00:09:01.760
actually shows up down
in our interpreter.

00:09:01.760 --> 00:09:03.940
So hopefully that doesn't
confuse you too much.

00:09:03.940 --> 00:09:05.920
In the next video,
we'll discuss how

00:09:05.920 --> 00:09:08.450
we might be able to
do interesting things

00:09:08.450 --> 00:09:10.601
to this function itself.


Defining a Factorial Function

WEBVTT
Kind: captions
Language: en

00:00:00.500 --> 00:00:03.140
In the last few videos, we
wrote a nice little program here

00:00:03.140 --> 00:00:05.750
that asks the user for
some type of an input,

00:00:05.750 --> 00:00:08.580
and then computes the
factorial of that number,

00:00:08.580 --> 00:00:09.970
and then prints it out.

00:00:09.970 --> 00:00:11.200
And that's nice.

00:00:11.200 --> 00:00:12.860
But you could
imagine a world where

00:00:12.860 --> 00:00:15.474
you would want to calculate
the factorial multiple places,

00:00:15.474 --> 00:00:16.890
or in multiple
different programs,

00:00:16.890 --> 00:00:18.764
or in the same program,
you want to calculate

00:00:18.764 --> 00:00:20.600
the factorial multiple times.

00:00:20.600 --> 00:00:22.760
And you don't want to have
to write this code over

00:00:22.760 --> 00:00:26.370
and over again every time you
want to calculate a factorial.

00:00:26.370 --> 00:00:28.090
So what I'm going
to do in this video

00:00:28.090 --> 00:00:31.810
is define a function that
calculates the factorial.

00:00:31.810 --> 00:00:33.920
And then we can use
that function every time

00:00:33.920 --> 00:00:35.550
we want to calculate
that factorial.

00:00:35.550 --> 00:00:37.591
So essentially, what that
function is going to do

00:00:37.591 --> 00:00:40.320
is going to put all of this
code right here in one place.

00:00:40.320 --> 00:00:41.840
And then any other
program that I

00:00:41.840 --> 00:00:45.020
want to calculate the factorial,
I can just recall that code.

00:00:45.020 --> 00:00:46.390
I don't have to rewrite it.

00:00:46.390 --> 00:00:48.660
So let me just show you
what I'm talking about.

00:00:48.660 --> 00:00:51.070
So I'm going to
define a function.

00:00:51.070 --> 00:00:54.132
And the key word in Python--
so it's a special word that

00:00:54.132 --> 00:00:56.090
tells the interpreter
that something special is

00:00:56.090 --> 00:00:58.310
about to happen-- the
key word in Python

00:00:58.310 --> 00:01:00.484
for defining a
function is d-e-f.

00:01:00.484 --> 00:01:01.900
So I'm going to
define a function.

00:01:01.900 --> 00:01:03.950
I'm going to call it
the factorial function.

00:01:03.950 --> 00:01:07.010
And it tends to be a
good idea to name things

00:01:07.010 --> 00:01:08.760
indicative of what
they actually do.

00:01:08.760 --> 00:01:11.030
Sometimes beginning
programmers have

00:01:11.030 --> 00:01:13.510
a habit of naming
things like x425.

00:01:13.510 --> 00:01:15.230
And someone who
comes in later will

00:01:15.230 --> 00:01:18.220
have no clue of what that
variable or that function

00:01:18.220 --> 00:01:18.792
is all about.

00:01:18.792 --> 00:01:20.500
So you definitely want
to somehow name it

00:01:20.500 --> 00:01:22.650
so it gives an idea of
what it's supposed to do.

00:01:22.650 --> 00:01:25.090
My function's called factorial.

00:01:25.090 --> 00:01:28.320
And the user, when
they call factorial,

00:01:28.320 --> 00:01:30.280
they need to tell
factorial what number

00:01:30.280 --> 00:01:32.420
am I going to take
the factorial of.

00:01:32.420 --> 00:01:36.370
So they're going to pass it
an argument called number.

00:01:36.370 --> 00:01:39.064
And if these words look
a little confusing,

00:01:39.064 --> 00:01:40.730
I'll explain them in
detail in a second.

00:01:40.730 --> 00:01:43.390
But you can just-- hopefully
the more you see this,

00:01:43.390 --> 00:01:47.110
the more familiar you'll
get with these ideas.

00:01:47.110 --> 00:01:50.180
And so they're going
to pass its number.

00:01:50.180 --> 00:01:52.360
And I'm just going to
have to return back

00:01:52.360 --> 00:01:53.655
to the calling program.

00:01:53.655 --> 00:01:55.030
And I actually
shouldn't even say

00:01:55.030 --> 00:01:56.446
the user's going
to pass a number.

00:01:56.446 --> 00:01:59.280
The calling program is
going to pass a number.

00:01:59.280 --> 00:02:02.600
And I need to return back
the factorial of that number

00:02:02.600 --> 00:02:03.719
to the calling program.

00:02:03.719 --> 00:02:05.760
So let me write this down
as a comment, actually.

00:02:09.539 --> 00:02:16.400
So returns the factorial
of the argument

00:02:16.400 --> 00:02:18.085
of the argument number.

00:02:18.085 --> 00:02:19.960
And sometimes you'll
hear the word parameter.

00:02:19.960 --> 00:02:21.810
Sometimes you'll hear
the word argument.

00:02:21.810 --> 00:02:24.120
When you're making a
definition of a function,

00:02:24.120 --> 00:02:26.060
this is more formally
called a parameter.

00:02:26.060 --> 00:02:31.460
Number is one of the parameters
to the factorial function.

00:02:31.460 --> 00:02:34.780
The actual number that
someone else passes to it--

00:02:34.780 --> 00:02:36.930
so let's say someone
calls factorial with a 3

00:02:36.930 --> 00:02:40.350
here-- then more formally,
that 3 would be the argument.

00:02:40.350 --> 00:02:44.250
So it returns the factorial
of the argument number.

00:02:44.250 --> 00:02:47.630
And this is the argument
number right over here.

00:02:47.630 --> 00:02:49.220
So I literally
mean-- I don't mean

00:02:49.220 --> 00:02:51.107
this is just arbitrarily
the word number.

00:02:51.107 --> 00:02:53.440
I'm literally talking about
this number right over here.

00:02:53.440 --> 00:02:55.727
Maybe I'll say the
argument number.

00:02:55.727 --> 00:02:57.560
Maybe I'll put it in
quotes to make it clear

00:02:57.560 --> 00:02:59.152
that this is the argument.

00:02:59.152 --> 00:03:00.860
I'm not talking about
any general number.

00:03:00.860 --> 00:03:02.250
And let's define it.

00:03:02.250 --> 00:03:05.460
So we're going to use the
same code essentially.

00:03:05.460 --> 00:03:08.570
Actually, let me
just copy and paste

00:03:08.570 --> 00:03:10.440
a lot of what I took over here.

00:03:10.440 --> 00:03:11.770
So I'm going to cut that out.

00:03:11.770 --> 00:03:13.700
And I'm going to
repaste it over here.

00:03:13.700 --> 00:03:16.080
But I have to be careful
about my indentations,

00:03:16.080 --> 00:03:18.570
because indentations
are how Python

00:03:18.570 --> 00:03:19.930
knows what's part of what.

00:03:19.930 --> 00:03:21.860
It knows how to
interpret things.

00:03:21.860 --> 00:03:24.440
So everything that's part
of this function definition

00:03:24.440 --> 00:03:25.800
has to be indented.

00:03:25.800 --> 00:03:29.410
And we do it by four spaces--
one, two, three, four.

00:03:29.410 --> 00:03:32.140
And this is another
four, two, three, four.

00:03:32.140 --> 00:03:35.070
And then this is part of the
for loop, one, two, three, four.

00:03:35.070 --> 00:03:37.020
And let's think about
what we have so far.

00:03:37.020 --> 00:03:39.680
So it's going to be
passed some number.

00:03:39.680 --> 00:03:43.050
We define this variable
product equals 1.

00:03:43.050 --> 00:03:45.250
And we'll talk more about
scoping of variables.

00:03:45.250 --> 00:03:48.170
But this variable is
only going to be usable

00:03:48.170 --> 00:03:50.437
within this
factorial definition,

00:03:50.437 --> 00:03:52.770
because the first time I
defined it right here is inside

00:03:52.770 --> 00:03:54.200
of this right here.

00:03:54.200 --> 00:03:57.410
So we'll go more into scoping
of variables in the future.

00:03:57.410 --> 00:04:00.270
And then the same logic
we did before, for i

00:04:00.270 --> 00:04:03.360
in range of number-- now
we're not getting number

00:04:03.360 --> 00:04:06.360
using the input function.

00:04:06.360 --> 00:04:08.300
It's being passed
to the function.

00:04:08.300 --> 00:04:11.280
Then product is equal to product
times i plus 1, same logic

00:04:11.280 --> 00:04:12.600
as we had before.

00:04:12.600 --> 00:04:14.587
So after you go
through this for loop--

00:04:14.587 --> 00:04:16.420
after you go through
it-- you're essentially

00:04:16.420 --> 00:04:18.490
going to go through
number times,

00:04:18.490 --> 00:04:21.560
product will have in it
the factorial of number.

00:04:21.560 --> 00:04:24.120
Instead of outputting it
directly, what we want to do

00:04:24.120 --> 00:04:25.380
is return.

00:04:25.380 --> 00:04:27.521
We're going to return it
to the calling program.

00:04:27.521 --> 00:04:29.520
And in the next video,
I'm going to diagram this

00:04:29.520 --> 00:04:31.190
out a little bit
cleaner if this seems

00:04:31.190 --> 00:04:32.850
a little bit confusing to you.

00:04:32.850 --> 00:04:35.050
I'm going to return product.

00:04:35.050 --> 00:04:38.000
So it's essentially the exact
same code as we had before.

00:04:38.000 --> 00:04:40.360
But we've packaged it
inside of a function.

00:04:40.360 --> 00:04:42.510
We've defined a
function, takes it,

00:04:42.510 --> 00:04:44.420
it has a parameter
called a number.

00:04:44.420 --> 00:04:47.316
If you pass factorial-- if
you want a factorial of 3,

00:04:47.316 --> 00:04:48.440
you'd write factorial of 3.

00:04:48.440 --> 00:04:50.450
And 3 would be the
argument, the thing that's

00:04:50.450 --> 00:04:52.550
being put in the place
of the variable number,

00:04:52.550 --> 00:04:54.470
or the thing that
number is referring to.

00:04:54.470 --> 00:04:56.760
You define product
is equal to 1.

00:04:56.760 --> 00:04:59.596
And then, you go number times.

00:04:59.596 --> 00:05:01.220
So for i in range of
number-- and we've

00:05:01.220 --> 00:05:04.180
explained the logic of that in
the previous video-- every time

00:05:04.180 --> 00:05:05.060
you start with 1.

00:05:05.060 --> 00:05:07.695
And then you do 1 times
1 is going to be 1.

00:05:07.695 --> 00:05:09.070
And then product's
going to be 1.

00:05:09.070 --> 00:05:10.890
But then i's going to be 1.

00:05:10.890 --> 00:05:12.000
i starts off at 0.

00:05:12.000 --> 00:05:15.290
So it's going to be 1
times 0 plus 1 gives you 1.

00:05:19.650 --> 00:05:20.700
Then i is 1.

00:05:20.700 --> 00:05:23.900
And so it's going to be 1 times
1 plus 1, which is 1 times 2.

00:05:23.900 --> 00:05:25.339
And so it's going to be 2, then.

00:05:25.339 --> 00:05:26.880
And it'll keep
incrementing that way.

00:05:26.880 --> 00:05:28.920
And we explain that in
detail in the last video.

00:05:28.920 --> 00:05:31.450
And then finally, it's going
to return that product.

00:05:31.450 --> 00:05:34.305
So if we want to have
the same exact behavior

00:05:34.305 --> 00:05:38.150
as before, but now we're using
this function, what we could do

00:05:38.150 --> 00:05:41.552
is-- we could say, we'll
still have the input.

00:05:41.552 --> 00:05:43.010
So this is our
function definition.

00:05:43.010 --> 00:05:44.830
But then, along
our main program,

00:05:44.830 --> 00:05:46.490
we've defined our function.

00:05:46.490 --> 00:05:49.060
And now, we can just
say, look, from the user,

00:05:49.060 --> 00:05:52.410
get a non-negative integer
to take the factorial of.

00:05:52.410 --> 00:05:54.320
And let's put that
into-- let's call

00:05:54.320 --> 00:05:59.320
that a variable
called user input.

00:05:59.320 --> 00:06:01.530
And then what I'm
going to do is I'm

00:06:01.530 --> 00:06:10.322
going to define another variable
called factorial of user input.

00:06:10.322 --> 00:06:12.030
And now, this is going
to be interesting.

00:06:12.030 --> 00:06:14.610
I'm going to call
this function up here.

00:06:14.610 --> 00:06:17.150
So this is going to
be equal to factorial

00:06:17.150 --> 00:06:23.140
of whatever the user had input,
so the factorial of user input,

00:06:23.140 --> 00:06:26.010
the factorial of this
variable right over here,

00:06:26.010 --> 00:06:28.950
the factorial of user input.

00:06:28.950 --> 00:06:31.270
So now, factorial of user
input will be storing--

00:06:31.270 --> 00:06:32.645
or I guess we
should say, it will

00:06:32.645 --> 00:06:34.500
be pointing to
the actual answer.

00:06:34.500 --> 00:06:36.570
And now, we just
have to print it out.

00:06:36.570 --> 00:06:38.390
So now we can print.

00:06:38.390 --> 00:06:43.480
And now we can print
factorial of user input.

00:06:43.480 --> 00:06:45.514
So they look like sentences.

00:06:45.514 --> 00:06:46.930
But these are just
variable names.

00:06:46.930 --> 00:06:48.600
And I'm naming this
way so you really

00:06:48.600 --> 00:06:50.183
know what's inside
of those variables,

00:06:50.183 --> 00:06:52.510
or what those variables
are really pointing to.

00:06:52.510 --> 00:06:56.450
So factorial of user input.

00:06:56.450 --> 00:07:01.811
Now the moment of truth is
always saving the program

00:07:01.811 --> 00:07:03.060
and actually trying to run it.

00:07:03.060 --> 00:07:06.370
So let's try to run it right
now and see what happens.

00:07:06.370 --> 00:07:08.970
Let's see what happens here.

00:07:08.970 --> 00:07:11.360
So at least nothing
broke so far.

00:07:11.360 --> 00:07:13.929
So once again, so
I want to be clear.

00:07:13.929 --> 00:07:14.970
The program started here.

00:07:14.970 --> 00:07:17.070
But up here, all we
did is define this.

00:07:17.070 --> 00:07:19.576
So it's not actually
telling-- it's not

00:07:19.576 --> 00:07:21.200
creating any interaction
with the user.

00:07:21.200 --> 00:07:23.158
It's actually not processing
anything just yet.

00:07:23.158 --> 00:07:24.667
So it just defines
this function.

00:07:24.667 --> 00:07:26.250
Then it goes down
to here and it says,

00:07:26.250 --> 00:07:27.990
OK, look, get some
input from the user.

00:07:27.990 --> 00:07:29.930
And that's what we're
doing right here.

00:07:29.930 --> 00:07:32.340
And after we input
some number here,

00:07:32.340 --> 00:07:34.890
then it's going to go--
it's going to actually call

00:07:34.890 --> 00:07:37.320
this function with that
number that we put.

00:07:37.320 --> 00:07:39.940
The number that we put's going
to go put in the user input.

00:07:39.940 --> 00:07:42.660
And then it's going to call
factorial with user input

00:07:42.660 --> 00:07:43.355
as an argument.

00:07:47.060 --> 00:07:48.940
So let's try it
with the number 3.

00:07:48.940 --> 00:07:52.280
And it did not work.

00:07:52.280 --> 00:07:53.790
Oh, and I see why
it did not work,

00:07:53.790 --> 00:07:56.684
because I had this left over
from the previous program,

00:07:56.684 --> 00:07:58.100
which actually
makes no sense now.

00:07:58.100 --> 00:08:00.050
So let me get rid of that.

00:08:00.050 --> 00:08:02.110
It's a good lesson that
seldom on the first try

00:08:02.110 --> 00:08:04.262
does something work perfectly.

00:08:04.262 --> 00:08:04.970
Let me try again.

00:08:04.970 --> 00:08:06.678
That was just nonsense
that I didn't even

00:08:06.678 --> 00:08:07.730
realize was down there.

00:08:07.730 --> 00:08:09.520
So let me try it again.

00:08:09.520 --> 00:08:10.520
So let me try.

00:08:10.520 --> 00:08:12.940
So a 3 again.

00:08:12.940 --> 00:08:15.380
And it gave me a good answer.

00:08:15.380 --> 00:08:16.790
It gave me the factorial of 3.

00:08:16.790 --> 00:08:20.380
And what's cool now is because
now my Python interpreter is

00:08:20.380 --> 00:08:23.212
assuming that this
definition has been made,

00:08:23.212 --> 00:08:25.670
I can now call the factorial
straight from the interpreter.

00:08:25.670 --> 00:08:27.680
If I had another program, I
could call it in multiple ways.

00:08:27.680 --> 00:08:29.420
And now, you should
hopefully appreciate

00:08:29.420 --> 00:08:31.310
why it's cool that
we made this function

00:08:31.310 --> 00:08:33.250
definition, because
now we can calculate,

00:08:33.250 --> 00:08:35.679
because I've defined
this function factorial,

00:08:35.679 --> 00:08:38.270
I can calculate
the factorial of 4.

00:08:38.270 --> 00:08:39.080
It's 24.

00:08:39.080 --> 00:08:50.560
I could say the factorial of 5
minus the factorial of 3, 114.

00:08:50.560 --> 00:08:52.269
I can take the factorial of 12.

00:08:52.269 --> 00:08:54.560
And you can tell the computer,
even though this is just

00:08:54.560 --> 00:08:56.560
interpreting all of this--
we'll talk more about

00:08:56.560 --> 00:09:00.010
interpreted versus compiled
code-- it's incredibly fast,

00:09:00.010 --> 00:09:02.520
faster than we can
really even fathom.

00:09:02.520 --> 00:09:04.840
So this is a real
power of a function

00:09:04.840 --> 00:09:07.580
is that I don't have to rewrite
the code every time now.

00:09:07.580 --> 00:09:10.510
I can just call it with
different arguments.

00:09:10.510 --> 00:09:11.760
So factorial of 2.

00:09:11.760 --> 00:09:12.850
I can do it every time.

00:09:12.850 --> 00:09:14.690
I don't have to
rerun the program.

00:09:14.690 --> 00:09:16.840
And if I write other
programs that use factorial,

00:09:16.840 --> 00:09:18.720
maybe I do some
things in [INAUDIBLE],

00:09:18.720 --> 00:09:21.510
I can just use
this as a function.

00:09:21.510 --> 00:09:25.670
In fact, I don't
even have to know

00:09:25.670 --> 00:09:27.380
what's going on
inside the function.

00:09:27.380 --> 00:09:33.290
And one of the other
powerful aspects of functions

00:09:33.290 --> 00:09:35.996
is that-- let's say I write
a bunch of programs that

00:09:35.996 --> 00:09:37.870
call-- so I have this
program right here that

00:09:37.870 --> 00:09:38.787
calls factorial.

00:09:38.787 --> 00:09:40.620
But let's say that you
come up with a better

00:09:40.620 --> 00:09:42.790
way of writing this
right over here.

00:09:42.790 --> 00:09:47.400
As long as your program,
that does the same thing

00:09:47.400 --> 00:09:49.930
with different guts, the
end user won't notice.

00:09:49.930 --> 00:09:52.090
So maybe you write a faster
way, or a simpler way,

00:09:52.090 --> 00:09:55.600
of doing this, a way that uses
less memory or less CPU power,

00:09:55.600 --> 00:09:57.559
then you could just
replace this later on.

00:09:57.559 --> 00:09:59.100
And then the
[INAUDIBLE] program that

00:09:59.100 --> 00:10:01.244
calls it, as long
as it still works,

00:10:01.244 --> 00:10:02.410
it'll work for that program.

00:10:02.410 --> 00:10:04.657
And it'll just work
that much better.


Python 3 Not Backwards Compatible with Python 2

WEBVTT
Kind: captions
Language: en

00:00:00.000 --> 00:00:02.385
So the videos that I've been making

00:00:02.385 --> 00:00:03.990
using these Python examples,

00:00:03.990 --> 00:00:08.531
I've been using Python 2,

00:00:08.531 --> 00:00:10.995
which is kind of the standard version of Python

00:00:10.995 --> 00:00:12.546
that most of the implementations are in

00:00:12.546 --> 00:00:14.390
at the time of making this video.

00:00:14.390 --> 00:00:19.692
But there is also a Python 3,

00:00:19.692 --> 00:00:22.374
which is kind of the state of the art version of Python,

00:00:22.374 --> 00:00:25.010
or I guess you could say it's the newest version of Python.

00:00:25.010 --> 00:00:26.990
But it's not backwards compatible.

00:00:26.990 --> 00:00:28.695
When I say backwards compatible,

00:00:28.695 --> 00:00:31.524
it means that if you are running Python 3,

00:00:31.524 --> 00:00:34.846
it will not necessarily be able to interpret everything

00:00:34.846 --> 00:00:36.992
that is written in Python 2.

00:00:36.992 --> 00:00:38.985
So that's what we mean by backwards compatibility:

00:00:38.985 --> 00:00:43.972
You can't take a program that was written in Python 2—

00:00:43.972 --> 00:00:44.990
so let's take a program—

00:00:44.990 --> 00:00:46.834
that's the program right there—

00:00:46.834 --> 00:00:50.015
and let's say it runs fine in Python 2.

00:00:50.015 --> 00:00:51.831
If you take that same program

00:00:51.831 --> 00:00:54.529
and try to run it in a Python 3 interpreter,

00:00:54.529 --> 00:00:55.994
it's not going to work.

00:00:55.994 --> 00:00:56.843
So this means,

00:00:56.843 --> 00:01:03.011
so this is not backwards compatible.

00:01:03.011 --> 00:01:04.225
If this did work,

00:01:04.225 --> 00:01:07.018
then Python 3 would be "backwards compatible".

00:01:07.018 --> 00:01:07.995
And the whole reason—

00:01:07.995 --> 00:01:10.018
well you know this is actually a nice understanding of

00:01:10.018 --> 00:01:11.537
what even backwards compatibility means—

00:01:11.537 --> 00:01:13.025
but the whole point of making this video is

00:01:13.025 --> 00:01:16.543
the program that I ran in the last video

00:01:16.543 --> 00:01:20.554
will work in Python 2,

00:01:20.554 --> 00:01:22.690
but it will not work in Python 3.

00:01:22.690 --> 00:01:25.010
And the reason why it doesn't work in Python 3 is

00:01:25.010 --> 00:01:28.538
Python 3 has a different version...

00:01:28.538 --> 00:01:32.992
...of this input function over here.

00:01:32.992 --> 00:01:39.538
In Python 2, if you use the input function just like I used it—

00:01:39.538 --> 00:01:46.005
I put a message here for the user of what to input

00:01:46.005 --> 00:01:48.552
and when the user inputs something,

00:01:48.552 --> 00:01:54.009
it'll be returned to the program fully evaluated.

00:01:54.009 --> 00:01:56.698
So in this case, if the user enters a number,

00:01:56.698 --> 00:01:58.375
I get the number as a number.

00:01:58.375 --> 00:02:07.190
So I literally get the evaluated expression.

00:02:07.190 --> 00:02:09.395
So like we saw in the last few examples,

00:02:09.395 --> 00:02:11.002
if the user puts in a 4,

00:02:11.002 --> 00:02:13.995
then the number 4 will be put in the variable "number".

00:02:13.995 --> 00:02:17.543
If the user inputs literally 3 + 4,

00:02:17.543 --> 00:02:23.523
then the number 7 will be put into the variable "number".

00:02:23.523 --> 00:02:24.682
In Python 3

00:02:24.682 --> 00:02:27.015
the input function does something slightly different

00:02:27.015 --> 00:02:29.533
and it's enough to break this program

00:02:29.533 --> 00:02:32.013
if you were to try to run it in Python 3.

00:02:32.013 --> 00:02:34.538
In Python 3,

00:02:34.538 --> 00:02:38.987
the input function doesn't return the evaluated expression,

00:02:38.987 --> 00:02:41.005
it returns a string—

00:02:41.005 --> 00:02:42.244
and this is a word you're going to...

00:02:42.244 --> 00:02:43.696
...hear a lot of in computer science,

00:02:43.696 --> 00:02:45.379
and I'll explain it in multiple videos—

00:02:45.379 --> 00:02:54.990
but it returns a string of what the user inputted.

00:02:54.990 --> 00:02:57.987
And you're saying "Sal, what is a string?"

00:02:57.987 --> 00:02:59.369
And this is actually a good introduction:

00:02:59.369 --> 00:03:01.008
A string is a "string of characters".

00:03:01.008 --> 00:03:03.385
So, to—maybe—make the difference,

00:03:03.385 --> 00:03:07.379
if I—if the user inputs—

00:03:07.379 --> 00:03:09.536
right over here, this is coming from the user,

00:03:09.536 --> 00:03:15.700
if the user inputs—in the Python 2 version—2 × 6,

00:03:15.700 --> 00:03:18.369
then what will be stored in the variable "number"

00:03:18.369 --> 00:03:21.385
will be the 2 × 6, so it will be 12,

00:03:21.385 --> 00:03:23.995
it will actually be evaluated.

00:03:23.995 --> 00:03:26.390
In Python 3, if we just did this,

00:03:26.390 --> 00:03:28.995
and the user inputted 2 × 6—

00:03:28.995 --> 00:03:31.385
so once again the user inputs 2 × 6—

00:03:31.385 --> 00:03:33.526
then what's stored in the variable "number",

00:03:33.526 --> 00:03:36.535
what's returned from this function right over here,

00:03:36.535 --> 00:03:39.375
is not going to be 2 × 6 evaluated.

00:03:39.375 --> 00:03:44.990
It is going to be a string of characters that says "2 × 6".

00:03:44.990 --> 00:03:48.021
So this is the string and a string is really—

00:03:48.021 --> 00:03:50.364
you can view it as a string of characters.

00:03:50.364 --> 00:03:53.369
you can kind of view it as some text

00:03:53.369 --> 00:03:56.382
that's not actually interpreted in any way.

00:03:56.382 --> 00:03:58.010
So it will actually return that string of characters,

00:03:58.010 --> 00:03:59.518
then you'd have that string of characters...

00:03:59.518 --> 00:04:00.979
...put here in "number"

00:04:00.979 --> 00:04:03.379
and then the function here "range" would break down

00:04:03.379 --> 00:04:04.979
because it's like I'm trying to take the range of...

00:04:04.979 --> 00:04:07.395
...like a word, not like a number here.

00:04:07.395 --> 00:04:09.390
So thats why it would break in Python 3.

00:04:09.390 --> 00:04:12.985
So if you want that original function to work in Python 3,

00:04:12.985 --> 00:04:15.969
what you have to do is put an—

00:04:15.969 --> 00:04:17.969
so instead of just calling "input" like this,

00:04:17.969 --> 00:04:19.395
you have to—and i will write it below this—

00:04:19.395 --> 00:04:25.054
you'd have to write "eval" and then write "input"—

00:04:25.054 --> 00:04:27.959
and then you write "input"

00:04:27.959 --> 00:04:31.359
and then it quotes all of the stuff I wrote up here,

00:04:31.359 --> 00:04:34.375
colons, close quotes, close parentheses

00:04:34.375 --> 00:04:38.515
and then close the eval() parentheses right over there.

00:04:38.515 --> 00:04:42.379
What eval() does is that it can take a string of characters,

00:04:42.379 --> 00:04:44.385
it can take some text like this.

00:04:44.385 --> 00:04:45.972
And it will evaluate it.

00:04:45.972 --> 00:04:47.529
So what happens is that

00:04:47.529 --> 00:04:51.975
in Python 3 you'll get this string and when you eval it—

00:04:51.975 --> 00:04:56.538
so then you would do an eval() on this—

00:04:56.538 --> 00:04:59.395
so you're doing an eval() on the input()—

00:04:59.395 --> 00:05:00.682
the user puts some input,

00:05:00.682 --> 00:05:03.000
so then you're doing an eval() on what the user inputted,

00:05:03.000 --> 00:05:08.690
and then this will return 12.

00:05:08.690 --> 00:05:11.025
So in Python 2, the input function kind of does it all for you,

00:05:11.025 --> 00:05:12.971
in Python 3 it returns a string

00:05:12.971 --> 00:05:14.995
and then you have to evaluate the string to get 12.

00:05:14.995 --> 00:05:17.371
So if you try to do this in Python 3 and it broke,

00:05:17.371 --> 00:05:18.687
that is the reason why,

00:05:18.687 --> 00:05:22.010
you have to put an eval() around it.


Flowchart for the Factorial Program

WEBVTT
Kind: captions
Language: en

00:00:00.640 --> 00:00:03.294
One thing you'll hear or
you'll see referred to a lot

00:00:03.294 --> 00:00:04.960
is something called
a flow chart, either

00:00:04.960 --> 00:00:06.710
in computer science,
or really when people

00:00:06.710 --> 00:00:09.580
talk about any type of
algorithm or process.

00:00:09.580 --> 00:00:11.680
So what I thought I would
do is for every program

00:00:11.680 --> 00:00:13.290
I write is write a
little simple flow chart.

00:00:13.290 --> 00:00:14.430
Maybe I won't do
it for every one,

00:00:14.430 --> 00:00:15.860
but I'll start with
some of the basic ones,

00:00:15.860 --> 00:00:17.776
just so we understand
that a flow chart really

00:00:17.776 --> 00:00:19.070
isn't anything fancy.

00:00:19.070 --> 00:00:20.960
So in this factorial
program that we

00:00:20.960 --> 00:00:24.469
had been looking at the last
couple of videos, any flow

00:00:24.469 --> 00:00:27.010
chart, you're just going to be
kind of at the starting point,

00:00:27.010 --> 00:00:29.420
which is usually
started, I guess,

00:00:29.420 --> 00:00:32.125
depicted with a circle
or oval of some kind.

00:00:32.125 --> 00:00:33.750
So we start-- you
could kind of view it

00:00:33.750 --> 00:00:36.950
as we're starting at this
line above this assignment

00:00:36.950 --> 00:00:40.890
of the number where we actually
ask for an input from the user.

00:00:40.890 --> 00:00:44.930
And then after that, we ask
for input from the user.

00:00:44.930 --> 00:00:47.470
This line right here,
we are asking for input

00:00:47.470 --> 00:00:50.900
from the user, so
this right over here.

00:00:50.900 --> 00:00:54.450
And we will depict that
by a parallelogram.

00:00:54.450 --> 00:00:56.740
So that is input from the user.

00:00:56.740 --> 00:01:03.561
And then we could just say
number equals user input.

00:01:03.561 --> 00:01:05.269
And you can be a little
bit loosey goosey

00:01:05.269 --> 00:01:06.280
with the terminology here.

00:01:06.280 --> 00:01:08.650
You're really just trying to say
what you're doing at this step.

00:01:08.650 --> 00:01:10.680
The parallelogram tells
us that we are somehow

00:01:10.680 --> 00:01:12.060
interfacing with the user.

00:01:12.060 --> 00:01:14.370
We're just either taking
something from the user,

00:01:14.370 --> 00:01:16.790
or we're outputting
something to the user.

00:01:16.790 --> 00:01:19.040
So here we're saying user
input is equal to number.

00:01:19.040 --> 00:01:22.190
We're assigning number
as the user input.

00:01:22.190 --> 00:01:26.020
Then the next thing we do is
just a straight-up operation.

00:01:26.020 --> 00:01:29.400
We just set product
to be equal to 1.

00:01:29.400 --> 00:01:33.320
So we just set-- and there we
just do that in a rectangle.

00:01:33.320 --> 00:01:35.380
Product equals 1.

00:01:35.380 --> 00:01:40.510
Product is equal to 1.

00:01:40.510 --> 00:01:43.760
And then something interesting
is going to happen.

00:01:43.760 --> 00:01:47.590
We enter into our for
loop, and we start with--

00:01:47.590 --> 00:01:49.090
and what I'm going
to do here is I'm

00:01:49.090 --> 00:01:51.070
going to write it a little bit
different than we wrote it over

00:01:51.070 --> 00:01:51.850
here.

00:01:51.850 --> 00:01:56.060
So what we do is we start
a-- what we're doing

00:01:56.060 --> 00:01:59.160
is we're going to start
our for loop essentially

00:01:59.160 --> 00:02:01.900
with an assignment, where we
assign-- for loop is really

00:02:01.900 --> 00:02:03.810
a bunch of things
happening at the same time.

00:02:03.810 --> 00:02:07.730
It assigns an i to the first
number in the sequence,

00:02:07.730 --> 00:02:09.790
or it tests whether
it can assign i

00:02:09.790 --> 00:02:11.290
to the first number
in the sequence.

00:02:11.290 --> 00:02:13.160
If it can, then it proceeds.

00:02:13.160 --> 00:02:16.280
If it can't, then it
breaks out of the for loop.

00:02:16.280 --> 00:02:18.670
So let me put it this way.

00:02:18.670 --> 00:02:23.280
I will do kind of a test
case right over here.

00:02:23.280 --> 00:02:25.070
So items left in sequence.

00:02:31.315 --> 00:02:32.940
And when I'm referring
to the sequence,

00:02:32.940 --> 00:02:36.160
I'm talking about this sequence
here, the range of our number.

00:02:36.160 --> 00:02:38.710
In the example they gave in the
last video, the number was 3,

00:02:38.710 --> 00:02:40.750
so items left in this sequence.

00:02:40.750 --> 00:02:43.360
I know it's hard to
read right over here.

00:02:43.360 --> 00:02:45.090
If there are-- so
let's say that there

00:02:45.090 --> 00:02:47.240
are items left in sequence.

00:02:47.240 --> 00:02:50.370
So if there are items
left in sequence,

00:02:50.370 --> 00:02:55.170
so we'll just say i is equal to
next item. i is equal to next.

00:02:55.170 --> 00:02:57.600
i is equal to the next item.

00:02:57.600 --> 00:03:00.760
If there are-- well, I'll
just hold off for the case

00:03:00.760 --> 00:03:03.530
where there are no items
left in the sequence.

00:03:03.530 --> 00:03:06.440
But let's say that there are.
i is equal to the next item.

00:03:06.440 --> 00:03:13.335
And then we also define
product to be-- actually,

00:03:13.335 --> 00:03:17.790
I wanted to do these in blue,
just our regular-- so let

00:03:17.790 --> 00:03:19.440
me write this as yes.

00:03:19.440 --> 00:03:21.140
If there are items
in the sequence,

00:03:21.140 --> 00:03:24.470
then i is equal
to the next item.

00:03:28.530 --> 00:03:30.390
That's one operation we do.

00:03:30.390 --> 00:03:33.070
And then we reset product.

00:03:33.070 --> 00:03:36.200
We reassign product
to be equal to what

00:03:36.200 --> 00:03:41.495
the former value of
product was times i plus 1.

00:03:46.830 --> 00:03:52.050
And at this point,
we essentially

00:03:52.050 --> 00:03:54.410
loop back to this
test over here.

00:03:54.410 --> 00:03:56.910
So at this point, we loop back.

00:03:56.910 --> 00:03:58.590
I'll try to write it neatly.

00:03:58.590 --> 00:04:02.400
We loop back to-- let me
do it right over here.

00:04:02.400 --> 00:04:05.470
We loop back to this
point in the program.

00:04:05.470 --> 00:04:09.950
That's why it is called a
loop, because after you perform

00:04:09.950 --> 00:04:12.330
this operation-- this is
embedded in the loop--

00:04:12.330 --> 00:04:13.870
you go back to the
beginning to see

00:04:13.870 --> 00:04:16.470
if there's any more
of the loop to do.

00:04:16.470 --> 00:04:19.190
So you go back to the
beginning before the loop,

00:04:19.190 --> 00:04:21.890
and you say, hey, are there
any items in the sequence?

00:04:21.890 --> 00:04:23.770
And this should be a
question mark over here.

00:04:23.770 --> 00:04:25.270
These kind of
question mark decision

00:04:25.270 --> 00:04:28.400
points you specify
with this diamond.

00:04:28.400 --> 00:04:30.216
If there is another
item in the sequence,

00:04:30.216 --> 00:04:31.590
then i is equal
to the next item,

00:04:31.590 --> 00:04:34.144
and product is equal to what
product was, times i plus 1,

00:04:34.144 --> 00:04:35.060
and We go to the next.

00:04:35.060 --> 00:04:36.725
Are there items left
in the sequence?

00:04:36.725 --> 00:04:38.100
And at some point,
there won't be

00:04:38.100 --> 00:04:40.110
any items left in the sequence.

00:04:40.110 --> 00:04:41.860
So there won't
be-- at some point,

00:04:41.860 --> 00:04:44.181
there won't be any items
left in the sequence.

00:04:44.181 --> 00:04:45.930
And we could go to the
right, or I'll just

00:04:45.930 --> 00:04:48.640
break out of it down here.

00:04:48.640 --> 00:04:50.980
I'll just break that
out of it down here.

00:04:50.980 --> 00:04:53.430
And now we've broken
out of this for loop.

00:04:53.430 --> 00:04:55.190
We've broken out
of this for loop,

00:04:55.190 --> 00:04:57.920
and the next thing we do once
we're done with our for loop

00:04:57.920 --> 00:05:01.000
is we just print the
value of product.

00:05:01.000 --> 00:05:02.800
We just output the
value of product.

00:05:02.800 --> 00:05:04.910
So that is an interaction
with the user.

00:05:04.910 --> 00:05:06.810
So we are literally
just outputting

00:05:06.810 --> 00:05:08.630
the value of product.

00:05:08.630 --> 00:05:15.420
So we output product,
and we are done.

00:05:15.420 --> 00:05:19.410
And I can write end for
the end of our program,

00:05:19.410 --> 00:05:22.050
and the program
will stop running.

00:05:22.050 --> 00:05:22.580
End.

00:05:22.580 --> 00:05:25.194
So this is just a simple flow
chart for this simple program,

00:05:25.194 --> 00:05:27.485
and hopefully, it helps you
appreciate that one-- well,

00:05:27.485 --> 00:05:28.820
one, a little understanding
of the program

00:05:28.820 --> 00:05:31.130
itself if the last two
videos didn't help too much.

00:05:31.130 --> 00:05:32.963
And it also gives you
a simple understanding

00:05:32.963 --> 00:05:35.310
of how to write a flow chart.


Stepping Through the Factorial Program

WEBVTT
Kind: captions
Language: en

00:00:00.660 --> 00:00:03.640
What I want to do in this video
is step through the program

00:00:03.640 --> 00:00:05.700
that we wrote in
the previous video

00:00:05.700 --> 00:00:08.582
to really understand what's
happening, line by line.

00:00:08.582 --> 00:00:10.665
And get a little bit of
an intuitive understanding

00:00:10.665 --> 00:00:12.357
for for loops and variables.

00:00:12.357 --> 00:00:14.190
We're going to do it
for a specific example.

00:00:14.190 --> 00:00:17.730
We're going to do an example
where the user inputs--

00:00:17.730 --> 00:00:19.720
so when the user is
prompted to input a number,

00:00:19.720 --> 00:00:21.677
the user will
input the number 3.

00:00:21.677 --> 00:00:23.010
We're just going to assume that.

00:00:23.010 --> 00:00:24.880
And I've picked a
low number so that we

00:00:24.880 --> 00:00:27.070
don't have to keep looping
through this forever.

00:00:27.070 --> 00:00:29.500
So this video can be
of a reasonable length.

00:00:29.500 --> 00:00:32.750
So right when that happens, when
the user inputs the number 3,

00:00:32.750 --> 00:00:36.520
it the assigns the variable
number to be equal to 3.

00:00:36.520 --> 00:00:39.380
And there's two ways that
you can think about this.

00:00:39.380 --> 00:00:41.660
I tend to, what I do in
any programming language,

00:00:41.660 --> 00:00:45.070
when I think of a variable,
I tend to view it as bucket.

00:00:45.070 --> 00:00:48.240
So I have a bucket
called number.

00:00:48.240 --> 00:00:50.950
And in that bucket, when
the user inputs a 3,

00:00:50.950 --> 00:00:54.750
if the user inputs a 3, I'm
sticking a 3 in that bucket.

00:00:54.750 --> 00:00:58.830
Python is a little bit
different than some languages.

00:00:58.830 --> 00:01:02.989
And the more correct way-- and
they're both kind of intuitive,

00:01:02.989 --> 00:01:05.280
I think, if you think about
it-- of viewing of variable

00:01:05.280 --> 00:01:12.090
in Python, is to think of the
variable as referring to the 3.

00:01:12.090 --> 00:01:14.290
And I say that
because, in Python,

00:01:14.290 --> 00:01:18.240
if you change a variable's--
let's say right under this we

00:01:18.240 --> 00:01:19.400
wrote number is equal to 2.

00:01:19.400 --> 00:01:20.510
We didn't do that,
but if we said

00:01:20.510 --> 00:01:22.093
number is equal to
2, what Python does

00:01:22.093 --> 00:01:24.430
is it actually leaves
that 3 there in memory.

00:01:24.430 --> 00:01:30.200
And then it would point
to the number 2 instead.

00:01:30.200 --> 00:01:33.430
It wouldn't obliterate the
3 and just put a 2 there,

00:01:33.430 --> 00:01:34.560
the 3 still exists.

00:01:34.560 --> 00:01:36.180
And that's important
when we start

00:01:36.180 --> 00:01:41.060
doing more complex or nuanced
things with more fancy data

00:01:41.060 --> 00:01:41.850
types.

00:01:41.850 --> 00:01:43.110
But let's rewind all of this.

00:01:43.110 --> 00:01:44.770
For the sake of
this program, you

00:01:44.770 --> 00:01:47.640
can view it either way,
a bucket with a 3 in it.

00:01:47.640 --> 00:01:49.140
Or you could view
it as a label that

00:01:49.140 --> 00:01:54.070
is referring to a 3 someplace
in the computer's memory.

00:01:54.070 --> 00:01:55.950
Now the next thing
that happens is

00:01:55.950 --> 00:01:58.340
that we have another
variable called product.

00:01:58.340 --> 00:02:00.510
And we assign product
to be equal to 1.

00:02:00.510 --> 00:02:03.440
So I'm going to do the
same two representations.

00:02:03.440 --> 00:02:07.430
So we've set up another
bucket here, called product,

00:02:07.430 --> 00:02:10.669
and we are assigning, we are
putting 1 in that bucket.

00:02:10.669 --> 00:02:15.430
Or you could say that
product is a variable.

00:02:15.430 --> 00:02:19.120
It's a label that
is referring to a 1.

00:02:19.120 --> 00:02:20.950
And then we get to our for loop.

00:02:20.950 --> 00:02:22.870
And this is probably
the confusing part.

00:02:22.870 --> 00:02:26.600
So we have for i
in range of number.

00:02:26.600 --> 00:02:29.340
So literally the interpreter
says OK, what's number?

00:02:29.340 --> 00:02:31.100
Well, in this case, number is 3.

00:02:31.100 --> 00:02:38.770
So this is going to be
evaluated as range of 3,

00:02:38.770 --> 00:02:40.970
in our particular example.

00:02:40.970 --> 00:02:41.647
Range of 3.

00:02:41.647 --> 00:02:43.730
And once again, how did
the interpreter know that?

00:02:43.730 --> 00:02:46.070
It's said range of number,
number's a variable,

00:02:46.070 --> 00:02:47.430
look up the number variable.

00:02:47.430 --> 00:02:47.710
OK.

00:02:47.710 --> 00:02:50.043
3 is sitting in it, so I'm
going to put a 3 right there.

00:02:50.043 --> 00:02:51.580
Or look up the number variable.

00:02:51.580 --> 00:02:54.030
The number variable is
pointing to the number 3,

00:02:54.030 --> 00:02:56.640
so I'm just going to put a 3
here and take a range of 3.

00:02:56.640 --> 00:02:58.740
And we saw in the last
video that if you evaluate

00:02:58.740 --> 00:03:05.080
just range of 3, range of 3 is
equal to a sequence of numbers,

00:03:05.080 --> 00:03:07.790
0, 1, 2.

00:03:07.790 --> 00:03:09.540
So it creates a sequence
of three numbers,

00:03:09.540 --> 00:03:13.000
starting at 0, up to
that number minus 1.

00:03:13.000 --> 00:03:15.620
And what the for loop
does, is every time

00:03:15.620 --> 00:03:18.670
we go through the for loop,
it executes this code.

00:03:18.670 --> 00:03:21.250
It executes this code
that's inside the for loop.

00:03:21.250 --> 00:03:24.620
And we know it's inside the for
loop, because we indented it.

00:03:24.620 --> 00:03:27.200
And the most used
convention is Python,

00:03:27.200 --> 00:03:29.540
is to don't use the Tab.

00:03:29.540 --> 00:03:31.290
You'd press Space four times.

00:03:31.290 --> 00:03:33.440
And that tells the
interpreter, anything

00:03:33.440 --> 00:03:36.220
at this level of indention
happens every time

00:03:36.220 --> 00:03:37.570
we go through the for loop.

00:03:37.570 --> 00:03:40.730
And when we start the
for loop, the i variable

00:03:40.730 --> 00:03:42.062
is going to start at 0.

00:03:42.062 --> 00:03:44.020
So the first time we go
through the for loop, i

00:03:44.020 --> 00:03:47.270
is going to be the first
element in this sequence.

00:03:47.270 --> 00:03:49.790
Then, when we go to
the for loop again,

00:03:49.790 --> 00:03:52.052
i is going to be the second
element in the sequence.

00:03:52.052 --> 00:03:54.010
And then when we go
through the for loop again,

00:03:54.010 --> 00:03:56.550
i is going to be the third
element in the sequence.

00:03:56.550 --> 00:03:59.220
And then we go to the
for loop and it'll

00:03:59.220 --> 00:04:01.759
try to assign i to another
element in the sequence

00:04:01.759 --> 00:04:02.550
and say, hey, wait!

00:04:02.550 --> 00:04:04.341
There are no more
elements in the sequence.

00:04:04.341 --> 00:04:06.060
And then it'll break
out of the for loop

00:04:06.060 --> 00:04:09.079
and then continue on with
the rest of the program.

00:04:09.079 --> 00:04:10.620
So given that, let's
think about what

00:04:10.620 --> 00:04:12.560
happens for the
particular example

00:04:12.560 --> 00:04:15.700
where the user inputted a 3.

00:04:15.700 --> 00:04:18.640
So range of number
is going to evaluate

00:04:18.640 --> 00:04:24.420
as-- I wrote up here, 0, 1, 2.

00:04:24.420 --> 00:04:25.430
Let me do it this way.

00:04:25.430 --> 00:04:27.300
Let me write it over here.

00:04:27.300 --> 00:04:32.090
So it's going to be, 0, 1, 2.

00:04:32.090 --> 00:04:34.040
And the first time
through the loop,

00:04:34.040 --> 00:04:36.840
i-- so I'm going to use this
convention right over here.

00:04:36.840 --> 00:04:41.170
The first time through the loop,
i is going to refer to the 0.

00:04:41.170 --> 00:04:43.700
Or if we use this convention
over here, the first time,

00:04:43.700 --> 00:04:45.295
as we go through
the loop, i, you

00:04:45.295 --> 00:04:46.670
could view it as
a bucket that is

00:04:46.670 --> 00:04:49.494
going to be assigned the
first element of range of 3,

00:04:49.494 --> 00:04:50.910
the first element
of the sequence.

00:04:50.910 --> 00:04:53.370
It is going to be 0.

00:04:53.370 --> 00:04:56.080
And then we execute
what's in the loop.

00:04:56.080 --> 00:04:59.750
We reassign, so we have
to reassign product

00:04:59.750 --> 00:05:01.625
to be something new
right over here.

00:05:01.625 --> 00:05:03.500
And what is this something
new the first time

00:05:03.500 --> 00:05:04.970
we go through the for loop?

00:05:04.970 --> 00:05:06.940
i is 0.

00:05:06.940 --> 00:05:09.190
0 plus 1 is 1.

00:05:09.190 --> 00:05:11.220
So the first time we
go through the loop--

00:05:11.220 --> 00:05:13.690
let me do that in a color you
can see-- the first time we

00:05:13.690 --> 00:05:16.410
go through the loop, this
is going to be 1, 0 plus 1.

00:05:16.410 --> 00:05:23.160
Product is 1 this first
time we go through the loop.

00:05:23.160 --> 00:05:25.050
Let me do it in the same colors.

00:05:25.050 --> 00:05:28.822
So i is 0 plus 1, so we get 1.

00:05:28.822 --> 00:05:30.690
1 times 1 is 1.

00:05:30.690 --> 00:05:34.080
So product will be
assigned to be 1.

00:05:34.080 --> 00:05:37.220
So product will be reassigned to
be 1, which really isn't going

00:05:37.220 --> 00:05:39.170
to change anything,
because it's already 1.

00:05:39.170 --> 00:05:40.149
So it was 1 before.

00:05:40.149 --> 00:05:42.190
You multiplied it by 1
and reassigned it to be 1.

00:05:42.190 --> 00:05:45.200
So I could write, cross out 1,
it's now going to be 1 again,

00:05:45.200 --> 00:05:46.559
but I'll just leave it as 1.

00:05:46.559 --> 00:05:48.850
Or we can just say it's going
to be reassigned to be 1.

00:05:48.850 --> 00:05:50.970
So nothing really has
changed that first time

00:05:50.970 --> 00:05:51.940
through the loop.

00:05:51.940 --> 00:05:54.350
But now, interesting
things are going to happen.

00:05:54.350 --> 00:05:56.910
So now we'll go back to
the beginning of the loop.

00:05:56.910 --> 00:06:00.840
And now, i will be the
next element in the loop.

00:06:00.840 --> 00:06:06.160
So now, i-- let me delete
this-- it will be reassigned

00:06:06.160 --> 00:06:08.880
to be the next
element in the loop.

00:06:08.880 --> 00:06:12.800
So now i will be 1.

00:06:12.800 --> 00:06:14.090
That's what the for loop does.

00:06:14.090 --> 00:06:17.360
It makes i each of the items
in the sequence in order,

00:06:17.360 --> 00:06:18.620
going through them.

00:06:18.620 --> 00:06:20.850
And now we execute it again.

00:06:20.850 --> 00:06:23.380
Now we go look at
product times i plus 1.

00:06:23.380 --> 00:06:24.980
Product is still 1.

00:06:24.980 --> 00:06:27.710
i is now 1.

00:06:27.710 --> 00:06:32.030
So 1 plus 1 is
now going to be 2.

00:06:32.030 --> 00:06:33.360
It is now going to be 2.

00:06:33.360 --> 00:06:37.630
So 1 times 2 is going
to be our new product.

00:06:37.630 --> 00:06:40.550
So the new product, it
was 1, but now we're

00:06:40.550 --> 00:06:43.640
multiplying it by 2 and
assigning it again to product.

00:06:43.640 --> 00:06:47.700
So the new product
is now going to be 2.

00:06:47.700 --> 00:06:50.550
So the product
variable is now 2.

00:06:50.550 --> 00:06:53.090
And then now we
try to loop again.

00:06:53.090 --> 00:06:57.230
And now this time, i
will be the next element.

00:06:57.230 --> 00:07:05.040
i is going to be the next
element in the sequence,

00:07:05.040 --> 00:07:07.150
right over here.

00:07:07.150 --> 00:07:09.720
Or we could say i is
going to be reassigned

00:07:09.720 --> 00:07:12.470
to 2, the next element
in the sequence.

00:07:12.470 --> 00:07:14.710
And we try to evaluate it again.

00:07:14.710 --> 00:07:17.090
So now we go into the for loop.

00:07:17.090 --> 00:07:19.840
Product is now 2.

00:07:19.840 --> 00:07:21.630
i is now 2.

00:07:21.630 --> 00:07:23.170
2 plus 1 is 3.

00:07:23.170 --> 00:07:25.610
So this whole expression
over here is 3.

00:07:25.610 --> 00:07:30.400
So we have 2, product is
now 2, times i plus 1.

00:07:30.400 --> 00:07:32.780
i is two, so i plus 1 is 3.

00:07:32.780 --> 00:07:36.950
So you have 2 times 3
3 is now going to be 6.

00:07:36.950 --> 00:07:41.190
So the new product times
i plus one is now 6.

00:07:41.190 --> 00:07:45.930
So this is now going to-- let me
do that same blue color-- this

00:07:45.930 --> 00:07:50.050
is now going to be 6.

00:07:50.050 --> 00:07:52.379
And now we try to execute
the for loop again.

00:07:52.379 --> 00:07:53.920
And the for loop's
going to say, hey,

00:07:53.920 --> 00:08:00.120
let me reassign i to be the
next item in this sequence.

00:08:00.120 --> 00:08:02.590
It tries to find the next
item in the sequence.

00:08:02.590 --> 00:08:07.060
But it says, wait, there is
no next item in the sequence.

00:08:07.060 --> 00:08:09.670
So we're done
executing our for loop.

00:08:09.670 --> 00:08:12.920
So there's nothing
left in our sequence,

00:08:12.920 --> 00:08:14.410
so we're done with our for loop.

00:08:14.410 --> 00:08:16.150
Now we break out of it.

00:08:16.150 --> 00:08:18.130
And we go to the next
line in the program.

00:08:18.130 --> 00:08:20.046
So the next line in the
program is, literally,

00:08:20.046 --> 00:08:21.570
just print the product.

00:08:21.570 --> 00:08:24.470
And at this point, we just
want to output the product.

00:08:24.470 --> 00:08:27.456
And the variable
product is now 6.

00:08:29.999 --> 00:08:31.790
And actually, one thing
I should have done,

00:08:31.790 --> 00:08:33.350
this entire time
is, I was sticking

00:08:33.350 --> 00:08:35.289
with the bucket representation.

00:08:35.289 --> 00:08:37.419
But the product on
this representation,

00:08:37.419 --> 00:08:39.130
here, it started referring to 1.

00:08:39.130 --> 00:08:40.669
It got reassigned to 1.

00:08:40.669 --> 00:08:42.490
But then it got assigned to 2.

00:08:42.490 --> 00:08:45.130
So then this
reference went to 2.

00:08:45.130 --> 00:08:46.905
And then it got reassigned to 6.

00:08:49.510 --> 00:08:52.660
So if I were to be consistent
with this representation

00:08:52.660 --> 00:08:55.590
over here, every time product
is referring to another number,

00:08:55.590 --> 00:08:58.620
or you can view it as a bucket,
for the sake of this program,

00:08:58.620 --> 00:09:00.289
where it keeps
getting reassigned.

00:09:00.289 --> 00:09:01.830
And what was cool
about this, is when

00:09:01.830 --> 00:09:04.280
we're done with this for
loop, the number that's

00:09:04.280 --> 00:09:06.950
sitting in the bucket product,
the variable product--

00:09:06.950 --> 00:09:09.390
or you could say, the number
that the variable product

00:09:09.390 --> 00:09:12.530
is referring to-- is the
factorial of the number

00:09:12.530 --> 00:09:13.890
that the person inputted.

00:09:13.890 --> 00:09:15.300
It's the factorial of 3.

00:09:15.300 --> 00:09:17.730
And if you remember,
3, the symbol

00:09:17.730 --> 00:09:21.350
for factorial is an exclamation
mark, is equal to 3 times

00:09:21.350 --> 00:09:24.170
2 times 1.


Solving radical equations

WEBVTT
Kind: captions
Language: en

00:00:00.530 --> 00:00:02.170
We're asked to
solve the equation,

00:00:02.170 --> 00:00:07.810
3 plus the principal square root
of 5x plus 6 is equal to 12.

00:00:07.810 --> 00:00:10.250
And so the general strategy
to solve this type of equation

00:00:10.250 --> 00:00:13.925
is to isolate the radical sign
on one side of the equation

00:00:13.925 --> 00:00:15.800
and then you can square
it to essentially get

00:00:15.800 --> 00:00:17.739
the radical sign to go away.

00:00:17.739 --> 00:00:19.280
But you have to be
very careful there

00:00:19.280 --> 00:00:20.822
because when you
square radical signs

00:00:20.822 --> 00:00:22.780
you actually lose the
information that you were

00:00:22.780 --> 00:00:24.230
taking the principal
square root.

00:00:24.230 --> 00:00:26.810
Not the negative square root
or not the plus or minus square

00:00:26.810 --> 00:00:27.420
root.

00:00:27.420 --> 00:00:29.440
You are only taking the
positive square root.

00:00:29.440 --> 00:00:30.940
And so when we get
our final answer,

00:00:30.940 --> 00:00:32.940
we do have to
check and make sure

00:00:32.940 --> 00:00:35.939
that it gels with taking
the principal square root.

00:00:35.939 --> 00:00:36.480
So let's try.

00:00:36.480 --> 00:00:38.330
Let's see what
I'm talking about.

00:00:38.330 --> 00:00:39.190
So the first thing
I want to do is

00:00:39.190 --> 00:00:41.314
I want to isolate this on
one side of the equation.

00:00:41.314 --> 00:00:43.800
And the best way to isolate
that is to get rid of this 3.

00:00:43.800 --> 00:00:45.300
And the best way
to get rid of the 3

00:00:45.300 --> 00:00:47.122
is to subtract 3 from
the left-hand side.

00:00:47.122 --> 00:00:49.080
And of course, if I do
it on the left-hand side

00:00:49.080 --> 00:00:51.070
I also have to do it
on the right-hand side.

00:00:51.070 --> 00:00:52.860
Otherwise, I would
lose the ability

00:00:52.860 --> 00:00:54.880
to say that they're equal.

00:00:54.880 --> 00:00:57.350
And so the left-hand
side right over here

00:00:57.350 --> 00:01:03.000
simplifies to the principal
square root of 5x plus 6.

00:01:03.000 --> 00:01:05.280
And this is equal to 12 minus 3.

00:01:05.280 --> 00:01:07.110
This is equal to 9.

00:01:07.110 --> 00:01:11.640
And now, we can square both
sides of this equation.

00:01:11.640 --> 00:01:15.050
So we could square the principal
square root of 5x plus 6

00:01:15.050 --> 00:01:17.430
and we can square 9.

00:01:17.430 --> 00:01:24.200
When you do this-- when you
square this, you get 5x plus 6.

00:01:24.200 --> 00:01:26.070
If you square the square
root of 5x plus 6,

00:01:26.070 --> 00:01:28.060
you're going to get 5x plus 6.

00:01:28.060 --> 00:01:30.490
And this is where we actually
lost some information

00:01:30.490 --> 00:01:32.240
because we would
have also gotten this

00:01:32.240 --> 00:01:35.906
if we squared the negative
square root of 5x plus 6.

00:01:35.906 --> 00:01:38.530
And so that's why we have to be
careful with the answers we get

00:01:38.530 --> 00:01:41.220
and actually make sure it works
when the original equation

00:01:41.220 --> 00:01:43.640
was the principal square root.

00:01:43.640 --> 00:01:46.090
So we get 5x plus 6
on the left-hand side.

00:01:46.090 --> 00:01:48.950
And on the right-hand
side we get 81.

00:01:48.950 --> 00:01:51.160
And now, this is just a
straight up linear equation.

00:01:51.160 --> 00:01:52.700
We want to isolate the x terms.

00:01:52.700 --> 00:01:54.855
Let's subtract 6
from both sides.

00:01:57.390 --> 00:02:00.990
On the left-hand side, we have
5x and on the right-hand side,

00:02:00.990 --> 00:02:02.990
we have 75.

00:02:02.990 --> 00:02:05.175
And then we can divide
both sides by 5.

00:02:07.880 --> 00:02:14.190
We get x is equal to--
let's see, it's 15, right?

00:02:14.190 --> 00:02:15.945
5 times 10 is 50.

00:02:15.945 --> 00:02:18.880
5 times 5 is 25 gives you 75.

00:02:18.880 --> 00:02:21.700
So we get x is
equal to 15, but we

00:02:21.700 --> 00:02:23.577
need to make sure
that this actually

00:02:23.577 --> 00:02:24.910
works for our original equation.

00:02:24.910 --> 00:02:27.970
Maybe this would
have worked if this

00:02:27.970 --> 00:02:29.370
was the negative square root.

00:02:29.370 --> 00:02:30.400
So we need to make
sure it actually

00:02:30.400 --> 00:02:31.510
works for the
positive square root,

00:02:31.510 --> 00:02:32.760
for the principal square root.

00:02:32.760 --> 00:02:34.930
So let's apply it to
our original equation.

00:02:34.930 --> 00:02:41.160
So we get 3 plus the principal
square root of 5 times 15.

00:02:41.160 --> 00:02:43.912
So 75 plus 6.

00:02:47.160 --> 00:02:49.090
So I just took 5
times 15 over here.

00:02:49.090 --> 00:02:50.320
I put our solution in.

00:02:50.320 --> 00:02:52.060
It should be equal to 12.

00:02:52.060 --> 00:02:55.800
Or we get 3 plus square
root of 75 plus 6

00:02:55.800 --> 00:02:58.480
is 81 needs to be equal to 12.

00:02:58.480 --> 00:03:02.300
And this is the principal
root of 81 so it's positive 9.

00:03:02.300 --> 00:03:05.550
So it's 3 plus 9 needs
to be equal to 12,

00:03:05.550 --> 00:03:07.120
which is absolutely true.

00:03:07.120 --> 00:03:11.030
So we can feel pretty
good about this answer.


Multiplying binomials with radicals

WEBVTT
Kind: captions
Language: en

00:00:00.630 --> 00:00:03.160
We're asked to multiply
and to simplify.

00:00:03.160 --> 00:00:05.790
And we have x squared minus
the principal square root

00:00:05.790 --> 00:00:11.420
of 6 times x squared plus the
principal square root of 2.

00:00:11.420 --> 00:00:13.430
And so we really just
have two binomials,

00:00:13.430 --> 00:00:15.630
two two-term expressions
that we want to multiply,

00:00:15.630 --> 00:00:17.290
and there's multiple
ways to do this.

00:00:17.290 --> 00:00:19.100
I'll show you the
more intuitive way,

00:00:19.100 --> 00:00:19.990
and then I'll show
you the way it's

00:00:19.990 --> 00:00:21.420
taught in some
algebra classes, which

00:00:21.420 --> 00:00:23.070
might be a little bit
faster, but requires

00:00:23.070 --> 00:00:24.278
a little bit of memorization.

00:00:24.278 --> 00:00:26.320
So I'll show you the
intuitive way first.

00:00:26.320 --> 00:00:28.520
So if you have
anything-- so let's say

00:00:28.520 --> 00:00:31.400
I have a times x
plus y-- we know

00:00:31.400 --> 00:00:33.960
from the distributive
property that this

00:00:33.960 --> 00:00:39.230
is the same thing as ax plus ay.

00:00:39.230 --> 00:00:40.980
And so we can do the
same thing over here.

00:00:40.980 --> 00:00:44.810
If you view a as x squared--
as this whole expression

00:00:44.810 --> 00:00:48.400
over here-- x squared minus
the principal square root of 6,

00:00:48.400 --> 00:00:51.219
and you view x plus y
as this thing over here,

00:00:51.219 --> 00:00:52.010
you can distribute.

00:00:55.650 --> 00:00:58.580
We can distribute all
of this onto-- let

00:00:58.580 --> 00:01:02.880
me do it this way-- distribute
this entire term onto this term

00:01:02.880 --> 00:01:04.580
and onto that term.

00:01:04.580 --> 00:01:05.670
So let's do that.

00:01:05.670 --> 00:01:09.470
So we get x squared minus
the principal square root

00:01:09.470 --> 00:01:14.690
of 6 times this term-- I'll do
it in yellow-- times x squared.

00:01:14.690 --> 00:01:17.515
And then we have plus
this thing again.

00:01:17.515 --> 00:01:18.640
We're just distributing it.

00:01:18.640 --> 00:01:19.590
It's just like they say.

00:01:19.590 --> 00:01:20.964
It's sometimes
not that intuitive

00:01:20.964 --> 00:01:22.550
because this is
a big expression,

00:01:22.550 --> 00:01:25.300
but you can treat it just like
you would treat a variable over

00:01:25.300 --> 00:01:25.800
here.

00:01:25.800 --> 00:01:28.840
You're distributing it over
this expression over here.

00:01:28.840 --> 00:01:33.730
And so then we have x squared
minus the principal square root

00:01:33.730 --> 00:01:37.225
of 6 times the principal
square root of 2.

00:01:41.050 --> 00:01:44.290
And now we can do the
distributive property again,

00:01:44.290 --> 00:01:48.760
but what we'll do is we'll
distribute this x squared

00:01:48.760 --> 00:01:51.810
onto each of these terms and
distribute the square root of 2

00:01:51.810 --> 00:01:54.360
onto each of these terms.

00:01:54.360 --> 00:01:55.960
It's the exact
same thing as here,

00:01:55.960 --> 00:01:58.230
it's just you could imagine
writing it like this.

00:01:58.230 --> 00:02:04.770
x plus y times a is still
going to be ax plus ay.

00:02:04.770 --> 00:02:07.598
And just to see the pattern, how
this is really the same thing

00:02:07.598 --> 00:02:09.139
as this up here,
we're just switching

00:02:09.139 --> 00:02:10.620
the order of the multiplication.

00:02:10.620 --> 00:02:13.350
You can kind of view it as we're
distributing from the right.

00:02:13.350 --> 00:02:16.590
And so if you do this, you
get x squared times x squared,

00:02:16.590 --> 00:02:21.060
which is x to the fourth,
that's that times that, and then

00:02:21.060 --> 00:02:24.355
minus x squared times the
principal square root of 6.

00:02:28.300 --> 00:02:30.980
And then over here you
have square root of 2 times

00:02:30.980 --> 00:02:36.570
x squared, so plus x squared
times the square root of 2.

00:02:36.570 --> 00:02:39.360
And then you have square root
of 2 times the square root of 6.

00:02:39.360 --> 00:02:41.440
And we have a negative
sign out here.

00:02:41.440 --> 00:02:43.106
Now if you take the
square root of 2--

00:02:43.106 --> 00:02:44.980
let me do this on the
side-- square root of 2

00:02:44.980 --> 00:02:48.680
times the square root of 6, we
know from simplifying radicals

00:02:48.680 --> 00:02:51.780
that this is the exact same
thing as the square root of 2

00:02:51.780 --> 00:02:55.502
times 6, or the principal
square root of 12.

00:02:55.502 --> 00:02:57.460
So the square root of 2
times square root of 6,

00:02:57.460 --> 00:02:58.835
we have a negative
sign out here,

00:02:58.835 --> 00:03:02.290
it becomes minus the
square root of 12.

00:03:02.290 --> 00:03:05.171
And let's see if we can
simplify this at all.

00:03:05.171 --> 00:03:05.670
Let's see.

00:03:05.670 --> 00:03:08.320
You have an x to
the fourth term.

00:03:08.320 --> 00:03:10.731
And then here you
have-- well depending

00:03:10.731 --> 00:03:12.730
on how you want to view
it, you could say, look,

00:03:12.730 --> 00:03:14.160
we have to second degree terms.

00:03:14.160 --> 00:03:15.860
We have something
times x squared,

00:03:15.860 --> 00:03:17.970
and we have something
else times x squared.

00:03:17.970 --> 00:03:19.970
So if you want,
you could simplify

00:03:19.970 --> 00:03:21.820
these two terms over here.

00:03:21.820 --> 00:03:25.436
So I have square
root of 2 x squareds

00:03:25.436 --> 00:03:27.810
and then I'm going to subtract
from that square root of 6

00:03:27.810 --> 00:03:28.960
x squareds.

00:03:28.960 --> 00:03:32.490
So you could view this
as square root of 2

00:03:32.490 --> 00:03:35.510
minus the square root of 6, or
the principal square root of 2

00:03:35.510 --> 00:03:40.260
minus the principal square
root of 6, x squared.

00:03:40.260 --> 00:03:44.010
And then, if you want,
square root of 12,

00:03:44.010 --> 00:03:45.560
you might be able
to simplify that.

00:03:45.560 --> 00:03:48.500
12 is the same
thing as 3 times 4.

00:03:48.500 --> 00:03:52.050
So the square root of 12
is equal to square root

00:03:52.050 --> 00:03:54.712
of 3 times square root of 4.

00:03:54.712 --> 00:03:57.170
And the square root of 4, or
the principal square root of 4

00:03:57.170 --> 00:03:58.761
I should say, is 2.

00:03:58.761 --> 00:04:00.510
So the square root of
12 is the same thing

00:04:00.510 --> 00:04:02.620
as 2 square roots of 3.

00:04:02.620 --> 00:04:04.900
So instead of writing the
principal square root of 12,

00:04:04.900 --> 00:04:08.900
we could write minus 2 times
the principal square root of 3.

00:04:08.900 --> 00:04:13.910
And then out here you have
an x to the fourth plus this.

00:04:13.910 --> 00:04:15.950
And you see, if you
distributed this out,

00:04:15.950 --> 00:04:18.240
if you distribute this x
squared, you get this term,

00:04:18.240 --> 00:04:19.997
negative x squared,
square root of 6,

00:04:19.997 --> 00:04:22.330
and if you distribute it onto
this, you'd get that term.

00:04:22.330 --> 00:04:27.310
So you could debate which
of these two is more simple.

00:04:27.310 --> 00:04:29.080
Now I mentioned
that this way I just

00:04:29.080 --> 00:04:30.580
did the distributive
property twice.

00:04:30.580 --> 00:04:31.820
Nothing new, nothing fancy.

00:04:31.820 --> 00:04:35.380
But in some classes, you will
see something called FOIL.

00:04:35.380 --> 00:04:38.040
And I think we've done
this in previous videos.

00:04:38.040 --> 00:04:39.540
FOIL.

00:04:39.540 --> 00:04:41.500
I'm not a big fan of
it because it's really

00:04:41.500 --> 00:04:44.002
a way to memorize a process
as opposed to understanding

00:04:44.002 --> 00:04:46.460
that this is really just from
the common-sense distributive

00:04:46.460 --> 00:04:47.269
property.

00:04:47.269 --> 00:04:48.810
But all this is is
a way to make sure

00:04:48.810 --> 00:04:50.476
that you're multiplying
everything times

00:04:50.476 --> 00:04:52.570
everything when
you're multiplying

00:04:52.570 --> 00:04:55.290
two binomials times
each other like this.

00:04:55.290 --> 00:05:00.370
And FOIL just says, look,
first multiply the first term.

00:05:00.370 --> 00:05:04.160
So x squared times x
squared is x to the fourth.

00:05:04.160 --> 00:05:06.670
Then multiply the outside.

00:05:06.670 --> 00:05:09.100
So then multiply--
I'll do this in green--

00:05:09.100 --> 00:05:10.360
then multiply the outside.

00:05:10.360 --> 00:05:14.302
So the outside terms are x
squared and square root of 2.

00:05:14.302 --> 00:05:16.010
And so x squared times
square root of 2--

00:05:16.010 --> 00:05:20.210
and they are positive-- so
plus square root of 2 times x

00:05:20.210 --> 00:05:21.040
squared.

00:05:21.040 --> 00:05:23.702
And then multiply the inside.

00:05:23.702 --> 00:05:25.160
And you can see
why I don't like it

00:05:25.160 --> 00:05:26.730
that much is because you
really don't know you're doing.

00:05:26.730 --> 00:05:28.710
You're just applying
an algorithm.

00:05:28.710 --> 00:05:30.809
Then you'll multiply the inside.

00:05:30.809 --> 00:05:32.850
And so negative square
root of 6 times x squared.

00:05:36.020 --> 00:05:40.304
And then you multiply
the last terms.

00:05:40.304 --> 00:05:42.470
So negative square root of
6 times square root of 2,

00:05:42.470 --> 00:05:44.490
that is-- and we
already know that-- that

00:05:44.490 --> 00:05:49.020
is negative square root of
12, which you can also then

00:05:49.020 --> 00:05:51.920
simplify to that expression
right over there.

00:05:51.920 --> 00:05:55.910
So it's fine to use
this, although it's good,

00:05:55.910 --> 00:05:58.930
even if you do use this, to
know where FOIL comes from.

00:05:58.930 --> 00:06:01.640
It really just comes from
using the distributive property

00:06:01.640 --> 00:06:03.190
twice.


Rationalizing denominators of expressions

WEBVTT
Kind: captions
Language: en

00:00:00.952 --> 00:00:05.457
We're asked to rationalize and simplify this expression right over here

00:00:05.457 --> 00:00:08.847
and like many problems there are multiple ways to do this.

00:00:08.847 --> 00:00:15.526
We could simplify a little bit then rationalize, then simplify a little bit more, or we could just rationalize and simplify.

00:00:15.526 --> 00:00:20.782
And just to make sure that you know what they are even talking about rationalize is just a fancy word,

00:00:20.782 --> 00:00:27.516
fancy way of saying we don't want to see any square roots of numbers in the denominator. Thats all it says.

00:00:27.516 --> 00:00:30.627
So try to get these things outside of the denominator.

00:00:30.627 --> 00:00:35.773
So the first thing we can do is just simplify then rationalize, then we can think about other ways to do it.

00:00:35.827 --> 00:00:41.215
So what I'd like to do first is say the principle square root of 8 that can be simplified a little bit because

00:00:41.215 --> 00:00:48.135
8 is the same thing as the square root of 4 times 2 which is the same thing as

00:00:48.135 --> 00:00:53.944
the square root of 4 times the square root of 2. So we can rewrite this entire expression as,

00:00:53.944 --> 00:01:03.501
the numerator is still the same, 16 plus 2X squared. All of that over, we can rewrite this as the square root of 4 times the square root of 2.

00:01:03.501 --> 00:01:14.681
The principle square root of 4 we know is just 2. So the square root of 8 we can rewrite as 2 times the principle square root of two.

00:01:14.681 --> 00:01:21.057
And I've simplified a little bit, I've done no rationalizing just yet, and it looks like there is a little more simplification I can do first.

00:01:21.057 --> 00:01:25.204
Because everything in the numerator and everything in the denominator is divisible by 2.

00:01:25.204 --> 00:01:27.673
So lets divide the numerator by 2.

00:01:27.673 --> 00:01:30.797
So if you divide the numerator by 2, 16 divided by 2,

00:01:30.844 --> 00:01:34.436
or you could view it as multipying the numerator and denominator by one half.

00:01:34.436 --> 00:01:37.965
So 16 times one half is 8.

00:01:37.965 --> 00:01:49.319
2X squared times one half is just X squared. And then 2 times the principle square root of 2 times one half is just the square root of 2.

00:01:49.319 --> 00:01:51.729
It is 1 square roots of 2.

00:01:51.729 --> 00:01:58.306
So this whole thing has simplified to 8 plus X squared, all of that over the square root of 2.

00:01:58.306 --> 00:02:01.930
And now lets rationalize this.

00:02:01.930 --> 00:02:09.268
The best way to get this radical out of the denominator is just multiply the numerator and the denominator by the principle square root of 2.

00:02:09.268 --> 00:02:16.646
So lets do that. So times the principle square root of 2 over the principle square root of 2.

00:02:16.646 --> 00:02:22.116
Now just to show that it works on the denominator what is the principle square root of 2 times the principle square root of 2?

00:02:22.116 --> 00:02:29.687
Well its going to be 2. And in our numerator, we are going to distribute this term onto both terms in this expression,

00:02:29.687 --> 00:02:39.238
so you have 8 times the principle square root of 2 plus the square root of 2 times X squared.

00:02:39.238 --> 00:02:45.010
And we could consider this done, we have simplified the expression, or if you want you could break it up.

00:02:45.010 --> 00:02:58.166
You could say this is the same thing as 8 square roots of 2 over 2, which is 4 square roots of 2, plus the square root of 2 times X squared over 2.

00:02:58.166 --> 00:03:04.020
So depending on your tastes, you might view this as more simple or this as more simple but both are equally valid.

00:03:04.020 --> 00:03:06.240
Now I said there were multiple ways to do this.

00:03:06.240 --> 00:03:10.779
We could have rationalized right from the get go. Let me start with our original problem.

00:03:10.779 --> 00:03:17.697
So our original problem was 16 + 2X squared, all of that over the principle square root of 8.

00:03:17.697 --> 00:03:28.097
We could have rationalized from the get go by multiplying the numerator and the denominator by the principle square root of 8.

00:03:28.097 --> 00:03:30.635
And so in our denominator we'll just get 8.

00:03:30.635 --> 00:03:41.937
And then in our numerator we would get 16 times the principle square root of 8, plus 2 times the principle square root of 8X squared.

00:03:41.937 --> 00:03:45.117
And now we could try and simplify this a little bit more.

00:03:45.117 --> 00:03:57.113
You can say, well, everything in the numerator and denominator is divisible by 2, so the 16 could become an 8 if you divide by 2. The 2 becomes a 1. And this 8 becomes a 4.

00:03:57.113 --> 00:04:13.071
And then you get 8 square roots of 8 plus the square root of 8X squared. All of this over 4.

00:04:13.071 --> 00:04:15.938
And then you say wait, this looks different from what we had over here.

00:04:15.938 --> 00:04:19.436
And the reason is we still haven't simplified this radical.

00:04:19.436 --> 00:04:27.759
We know that we can rewrite the principle square root of 8 as 2 square roots of 2.

00:04:27.759 --> 00:04:33.800
And then we can see again that everything in the numerator and the denominator is also divisible by 2, so lets do that again.

00:04:33.800 --> 00:04:37.871
So if you divide everything in the numerator by 2 you can get rid of this 2 and that 2.

00:04:37.871 --> 00:04:41.574
And everything in the denominator by 2, this will become a 2.

00:04:41.574 --> 00:04:53.639
So then you have 8 square roots of 2 plus the square root of 2X squared, all of that over 2.

00:04:53.639 --> 00:04:57.476
Which is exactly what we had gotten here.