Core Theme: This content introduces CS50, Harvard University's foundational computer science course, emphasizing algorithmic thinking, problem-solving, and the principles of programming through a series of lectures. It aims to empower students to apply these concepts across various fields, not just computer science.
interesting pretty artistically inclined things if you go to this URL at your
things if you go to this URL at your leisure cs50. lart it'll actually
leisure cs50. lart it'll actually redirect you to a Google spreadsheet
redirect you to a Google spreadsheet that we've made in advance and we've
that we've made in advance and we've kind of Shrunk the rows and columns to
kind of Shrunk the rows and columns to resemble a grid of pixels tiny little
resemble a grid of pixels tiny little squares all of which are white by
squares all of which are white by default not unlike this easel here that
default not unlike this easel here that we have a couple of volunteers working
we have a couple of volunteers working away at in fact would you guys like to
away at in fact would you guys like to come forward for a moment and say a
come forward for a moment and say a quick hello before we come back to you
quick hello before we come back to you uh hello my name is Daniel I'm from
uh hello my name is Daniel I'm from Chicago welcome to Daniel and hi
Chicago welcome to Daniel and hi everyone I'm Adam and I'm from chindon
everyone I'm Adam and I'm from chindon Tobago nice well welcome to you both
Tobago nice well welcome to you both thank you you'll see that in their hands
thank you you'll see that in their hands are actually a whole bunch of pixels uh
are actually a whole bunch of pixels uh Post-it notes that we've handed them in
Post-it notes that we've handed them in in advance so if you don't mind we'll
in advance so if you don't mind we'll come back to you in a couple of minutes
come back to you in a couple of minutes and see what they've created if you will
and see what they've created if you will on this grid of white paper much like
on this grid of white paper much like you could create on this Google
you could create on this Google spreadsheet in fact feel free to send us
spreadsheet in fact feel free to send us your Creations if so inclined uh via the
your Creations if so inclined uh via the URL you'll get at cs50. lart now let's
URL you'll get at cs50. lart now let's come back to week zero where we Define
come back to week zero where we Define some of the building blocks for images
some of the building blocks for images we talked about RGB which is just red
we talked about RGB which is just red green blue and it's just one of the
green blue and it's just one of the systems a popular system via which you
systems a popular system via which you can represent any color of the rainbow
can represent any color of the rainbow using some combination of red and green
using some combination of red and green and blue and if any of you are
and blue and if any of you are artistically inclined or have used
artistically inclined or have used Photoshop or similar programs you might
Photoshop or similar programs you might typically have some means of selecting a
typically have some means of selecting a color by some grid like this but down
color by some grid like this but down here notice there's explicit mentions of
here notice there's explicit mentions of the types of color systems in use R GB
the types of color systems in use R GB and in fact here you see 0000 0 and up
and in fact here you see 0000 0 and up here under new you see the color black
here under new you see the color black and that implies that if you have no red
and that implies that if you have no red no green no blue well that indeed would
no green no blue well that indeed would represent by convention the color black
represent by convention the color black by contrast if we play around with
by contrast if we play around with Photoshop or similar program if you have
Photoshop or similar program if you have a lot of red a lot of green and a lot of
a lot of red a lot of green and a lot of blue for instance 255 255 255 like
blue for instance 255 255 255 like really crank it up to the max value you
really crank it up to the max value you can represent with eight bits per week
can represent with eight bits per week zero well then it turns out you get the
zero well then it turns out you get the color white here and we can play with
color white here and we can play with these numbers endlessly for instance if
these numbers endlessly for instance if we use 255 of red but zero green and
we use 255 of red but zero green and zero blue not surprisingly the square at
zero blue not surprisingly the square at the top of the screen Becomes Of course
the top of the screen Becomes Of course red entirely because it's all red and no
red entirely because it's all red and no green no blue if we change it instead to
green no blue if we change it instead to 255 for green but zero for red and blue
255 for green but zero for red and blue of course we get green and then lastly
of course we get green and then lastly if we crank up the blue but leave red
if we crank up the blue but leave red and green as zero we of course get blue
and green as zero we of course get blue but all this while down here highlighted
but all this while down here highlighted is something that maybe some of you have
is something that maybe some of you have seen before like some combination of
seen before like some combination of numbers and letters if any of you have
numbers and letters if any of you have made personal web pages or use programs
made personal web pages or use programs like Photoshop you might have used these
like Photoshop you might have used these so-called color codes so indeed the
so-called color codes so indeed the world has this convention whereby using
world has this convention whereby using six digits or sometimes three you can
six digits or sometimes three you can represent a little more succinctly some
represent a little more succinctly some amount of red green blue and you'll see
amount of red green blue and you'll see here maybe by inference that if RGB is0
here maybe by inference that if RGB is0 0 255 respectively perhaps where we're
0 255 respectively perhaps where we're going with this is that
going with this is that 0000 FF is just an alternative way of
0000 FF is just an alternative way of expressing the exact same idea no red no
expressing the exact same idea no red no green and a lot of blue but why is that
green and a lot of blue but why is that and in fact we'll come full circle here
and in fact we'll come full circle here to introducing something that we could
to introducing something that we could have done in week zero but it doesn't
have done in week zero but it doesn't really solve a problem then but today as
really solve a problem then but today as we focus more on images and on memory
we focus more on images and on memory itself turns out understanding these
itself turns out understanding these patterns is pretty useful so back in
patterns is pretty useful so back in week zero we talked of course about
week zero we talked of course about binary and binary by implying two only
binary and binary by implying two only gives you two digits zero and one you
gives you two digits zero and one you and I as humans almost always use the
and I as humans almost always use the decimal system in normal conversation
decimal system in normal conversation deck meaning 10 so we have 0 through n
deck meaning 10 so we have 0 through n instead if a human like us wants to
instead if a human like us wants to count up as high as 10 or 11 or 12 we
count up as high as 10 or 11 or 12 we don't have a digit per se for 10 11 and
don't have a digit per se for 10 11 and 12 we start reusing digits so it's 1 0 1
12 we start reusing digits so it's 1 0 1 1 1 2 and so forth but in other systems
1 1 2 and so forth but in other systems not binary not decimal but systems
not binary not decimal but systems called heximal hex implying 16 there are
called heximal hex implying 16 there are actually more digits than these which
actually more digits than these which might come as a surprise um it's not
might come as a surprise um it's not pairs of digits like in decimal single
pairs of digits like in decimal single digits and frankly it doesn't really
digits and frankly it doesn't really matter what the digits are because at
matter what the digits are because at the end of the day these are just
the end of the day these are just symbols that you and I immediately
symbols that you and I immediately associate with some notion of math but
associate with some notion of math but just Strokes on the screen that
just Strokes on the screen that represent some have represent some
represent some have represent some actual value so it turns out that by
actual value so it turns out that by convention
convention when you want more than n 10 digits 0
when you want more than n 10 digits 0 through 9 you start using letters of the
through 9 you start using letters of the English alphabet A B C D E and F and you
English alphabet A B C D E and F and you can represent them in lower case it's
can represent them in lower case it's case insensitive so it doesn't really
case insensitive so it doesn't really matter you might see it in upper case or
matter you might see it in upper case or lower case but this is how you can count
lower case but this is how you can count Beyond N9 not using decimal but using
Beyond N9 not using decimal but using indeed something called hexadecimal if
indeed something called hexadecimal if we get really technical this is also
we get really technical this is also known as base 16 and it's the same idea
known as base 16 and it's the same idea as week zero where instead of using base
as week zero where instead of using base 2 for binary base 10 for decimal use 16
2 for binary base 10 for decimal use 16 as the base for heximal and so if we run
as the base for heximal and so if we run through just some simple examples here
through just some simple examples here in the world of heximal your columns are
in the world of heximal your columns are just powers of 16 16 to the 0er 16 to
just powers of 16 16 to the 0er 16 to the 1 16 to the 2 and so forth but in
the 1 16 to the 2 and so forth but in the world of hex we usually at least
the world of hex we usually at least thus far and today we'll see just pairs
thus far and today we'll see just pairs of digits like this so here for instance
of digits like this so here for instance is the ones column and the 16's column
is the ones column and the 16's column if we multiply that out so if you wanted
if we multiply that out so if you wanted to represent the number you and I know
to represent the number you and I know in in uh the real world as zero in
in in uh the real world as zero in heximal it would just be 0 0 if you want
heximal it would just be 0 0 if you want to represent the number one it would be
to represent the number one it would be 01 and from there we get 02 03 04 0 five
01 and from there we get 02 03 04 0 five 06 07 08 09 now things get potentially
06 07 08 09 now things get potentially interesting in decimal it would
interesting in decimal it would obviously become 10 but in heximal it
obviously become 10 but in heximal it just becomes 0 a and then 0 B which is
just becomes 0 a and then 0 B which is to say if I rewind after nine comes in
to say if I rewind after nine comes in heximal if I pronounce it in in decimal
heximal if I pronounce it in in decimal this is how you'd represent 10 this is
this is how you'd represent 10 this is how you'd represent 11 12 13 14 and then
how you'd represent 11 12 13 14 and then lastly in heximal the 16th value is f
lastly in heximal the 16th value is f which is just always going to represent
which is just always going to represent 15 so where to how do we connect this to
15 so where to how do we connect this to some of the past math well once you get
some of the past math well once you get to Zer F in heximal if f is the highest
to Zer F in heximal if f is the highest you can count just like in decimal nine
you can count just like in decimal nine is the highest you can count what comes
is the highest you can count what comes next if this is 15 I claim how do I
next if this is 15 I claim how do I represent 16 in heximal with what
represent 16 in heximal with what pattern of
pattern of symbols what pattern of symbols for
symbols what pattern of symbols for heximal yeah so one Z not 10 even though
heximal yeah so one Z not 10 even though you might read it like that as a typical
you might read it like that as a typical human but one zero because why well even
human but one zero because why well even if this is completely new to you the
if this is completely new to you the whole column system the places are
whole column system the places are exactly the same intuitively so you need
exactly the same intuitively so you need a one in the 16's place and a zero in
a one in the 16's place and a zero in the ones place and we won't count all
the ones place and we won't count all the way up to 255 but we count if we
the way up to 255 but we count if we count a little higher this would be 1 Z
count a little higher this would be 1 Z AKA 16 in decimal this would be 1 one
AKA 16 in decimal this would be 1 one AKA 17 in decimal and then 18 19 20 and
AKA 17 in decimal and then 18 19 20 and so forth dot dot dot and we can count
so forth dot dot dot and we can count all the way up to FF CU if f is the
all the way up to FF CU if f is the biggest digit in hexadecimal FF is
biggest digit in hexadecimal FF is indeed as high as we can count and if
indeed as high as we can count and if each F represents 15 well let's just do
each F represents 15 well let's just do the math like in week zero so 16 * F + 1
the math like in week zero so 16 * F + 1 * f is how all of us learned to do uh
* f is how all of us learned to do uh math in grade school even though not in
math in grade school even though not in heximal that's of course 16 * 15 + 1 *
heximal that's of course 16 * 15 + 1 * 15 multiply that out you get 240 plus 15
15 multiply that out you get 240 plus 15 and air go you can count as high as
and air go you can count as high as 255 using two heximal digits now this is
255 using two heximal digits now this is not the kind of thing where like this is
not the kind of thing where like this is going to be an interesting exercise
going to be an interesting exercise mentally to ever convert in your head
mentally to ever convert in your head generally you'll get used to the fact
generally you'll get used to the fact that after nine comes a and the biggest
that after nine comes a and the biggest digit is f and you'll just start to see
digit is f and you'll just start to see patterns like this in the worlds of
patterns like this in the worlds of Photoshop web pages in a few weeks and
Photoshop web pages in a few weeks and Beyond but like why is heximal useful
Beyond but like why is heximal useful like why are we complicating the world
like why are we complicating the world and adding on top of decimal something
and adding on top of decimal something else well it turns out that a single
else well it turns out that a single decimal digit like f the biggest one for
decimal digit like f the biggest one for instance is 15 and here let me just
instance is 15 and here let me just propose a bit of Mental Math how many
propose a bit of Mental Math how many bits do you need to represent the number
bits do you need to represent the number 15 in binary if you've got the ones
15 in binary if you've got the ones place two's Place fours and so forth how
place two's Place fours and so forth how many bits
many bits total so fewer than five to count this
total so fewer than five to count this highest 15 I think but
highest 15 I think but close someone
close someone else sing in hand yeah so four bits I
else sing in hand yeah so four bits I think suffice because if you want to
think suffice because if you want to count as high as F that is to say 15 I
count as high as F that is to say 15 I think if you have four bits you can do
think if you have four bits you can do that because if over here is the one's
that because if over here is the one's place from week zero for binary this is
place from week zero for binary this is the two's place this is The Four's place
the two's place this is The Four's place this is the e place do out some quick
this is the e place do out some quick math so 8 + 4 is 12 + 2 is 14 + 1 is 15
math so 8 + 4 is 12 + 2 is 14 + 1 is 15 so it turns out that by convenience
so it turns out that by convenience heximal digits can just be represented
heximal digits can just be represented consistently with four bits or fewer but
consistently with four bits or fewer but four and four of course is half of eight
four and four of course is half of eight and eight is like everywhere like eight
and eight is like everywhere like eight bits is a bite which is again just a
bits is a bite which is again just a convention we've seen and so the reason
convention we've seen and so the reason that you see hexad desmal in the world
that you see hexad desmal in the world of Photoshop and eventually web pages is
of Photoshop and eventually web pages is it actually just Maps really nicely to
it actually just Maps really nicely to expressing binary numbers more
expressing binary numbers more succinctly with a fixed number of digits
succinctly with a fixed number of digits so for instance anytime you see 11 one
so for instance anytime you see 11 one one one 1111 in the world as binary you
one one 1111 in the world as binary you know what that's a little tedious to
know what that's a little tedious to both say and write you can represent
both say and write you can represent more succinctly
more succinctly any uh group of four one bits more
any uh group of four one bits more succinctly in heximal as just F so 111
succinctly in heximal as just F so 111 1111 in binary more succinctly and more
1111 in binary more succinctly and more commonly now in the world of Photoshop
commonly now in the world of Photoshop memory images and the like is
memory images and the like is represented more succinctly as FF and
represented more succinctly as FF and that's why because it just Maps really
that's why because it just Maps really nicely to four bits and so we can be a
nicely to four bits and so we can be a little more succinct so any questions on
little more succinct so any questions on heximal which is just another way of
heximal which is just another way of representing information but using the
representing information but using the same grade school approach yeah good
same grade school approach yeah good question if you represent 15 with f it
question if you represent 15 with f it would use four bits so base systems are
would use four bits so base systems are really just a way for us humans on paper
really just a way for us humans on paper or on screens to represent information
or on screens to represent information if F represents the decimal number 15
if F represents the decimal number 15 the computer underneath the hood has to
the computer underneath the hood has to use four bits to represent it so one
use four bits to represent it so one heximal digit by convention always
heximal digit by convention always implies four bits underneath the hood so
implies four bits underneath the hood so therefore if you have two heximal digits
therefore if you have two heximal digits like 0 0 that means eight zero bits
like 0 0 that means eight zero bits underneath the hood like for red or for
underneath the hood like for red or for green if you see FF now we know that's
green if you see FF now we know that's four one bits and another four one Bits
four one bits and another four one Bits And if we do out the math that's 255
And if we do out the math that's 255 that's why in Photoshop
that's why in Photoshop 000000 FF means no red no green and
000000 FF means no red no green and 255 of blue and it's just way more
255 of blue and it's just way more succinct than writing out like what 8
succinct than writing out like what 8 plus 8 plus 8 24 zeros and ones and it's
plus 8 plus 8 24 zeros and ones and it's just cleaner than even using decimal
just cleaner than even using decimal when you're using units of eight which
when you're using units of eight which again computers just use everywhere so
again computers just use everywhere so it's just another system it's not one
it's just another system it's not one you need to dwell on very much but again
you need to dwell on very much but again it's fundamentally no different from
it's fundamentally no different from binary or decimal we're just using a
binary or decimal we're just using a slightly different base no all right
slightly different base no all right well we had this blank canvas here and I
well we had this blank canvas here and I think uh are you two perhaps ready to
think uh are you two perhaps ready to reveal for the world what you've created
reveal for the world what you've created do you want to go ahead and I'll I'll
do you want to go ahead and I'll I'll swivel it around for you all right here
swivel it around for you all right here we go big reveal and today's pixel
we go big reveal and today's pixel art a round of applause if we
art a round of applause if we could very nicely done well thank you
could very nicely done well thank you both if you want to come up after and
both if you want to come up after and tear this off and bring it home you're
tear this off and bring it home you're welcome to and keep the Post-it notes
welcome to and keep the Post-it notes too well thank you to our volunteers
too well thank you to our volunteers there let's now translate this to really
there let's now translate this to really more technical worlds where we're going
more technical worlds where we're going to see and consider it more often
to see and consider it more often because in fact sometimes when you've
because in fact sometimes when you've had error messages over the past few
had error messages over the past few weeks from clang the compiler you might
weeks from clang the compiler you might have even seen evidence of heximal we
have even seen evidence of heximal we didn't call it out it wasn't useful to
didn't call it out it wasn't useful to know at the time but it turns out a lot
know at the time but it turns out a lot of programs use and a lot of code uses
of programs use and a lot of code uses heximal for those reasons of more prec
heximal for those reasons of more prec more representation so for instance
more representation so for instance where else might we see it well here's
where else might we see it well here's that picture we keep pulling up of our
that picture we keep pulling up of our computer's memory and each of these
computer's memory and each of these squares in this grid represents a bite
squares in this grid represents a bite sort of top left to bottom right in the
sort of top left to bottom right in the computer's memory but again just an
computer's memory but again just an artist's representation a few weeks ago
artist's representation a few weeks ago I claimed that each of these bytes can
I claimed that each of these bytes can be numbered of course like this is bite
be numbered of course like this is bite zero at top left then bite one then bite
zero at top left then bite one then bite two then bite two billion if you have
two then bite two billion if you have two gigabytes of memory and so we could
two gigabytes of memory and so we could just number them like this 0 through 15
just number them like this 0 through 15 on up 16 17 18 and so forth but per the
on up 16 17 18 and so forth but per the reasons earlier it's just more common in
reasons earlier it's just more common in computer systems and in software to
computer systems and in software to actually use hexadecimal just to
actually use hexadecimal just to describe the locations of the addresses
describe the locations of the addresses of things in memory so instead a typical
of things in memory so instead a typical programmer or computer scientist would
programmer or computer scientist would call these first 16 bytes 0 through F
call these first 16 bytes 0 through F just because but that's because it's a
just because but that's because it's a predictable number of bits so if we keep
predictable number of bits so if we keep going beyond that you would get not 10
going beyond that you would get not 10 not 11 not 12 but in heximal 1 0 1 1 1 2
not 11 not 12 but in heximal 1 0 1 1 1 2 and so forth all the way down on the
and so forth all the way down on the screen to 1 F and if I you know shrunk
screen to 1 F and if I you know shrunk this down or had a bigger monitor we
this down or had a bigger monitor we would see eventually 255 bytes later
would see eventually 255 bytes later from the start 255 as well but there's a
from the start 255 as well but there's a potential problem here with using
potential problem here with using heximal in this way there's an
heximal in this way there's an ambiguity can anyone imagine like what
ambiguity can anyone imagine like what can go wrong if we use hex to just
can go wrong if we use hex to just simply describe locations in memory like
simply describe locations in memory like this
this yeah yeah yeah like one zero might also
yeah yeah yeah like one zero might also be 10 and you know maybe if you're you
be 10 and you know maybe if you're you know really thorough okay wait a minute
know really thorough okay wait a minute it can't be 10 cuz here's F over here so
it can't be 10 cuz here's F over here so it's obviously not decimal but why
it's obviously not decimal but why create potential confusion especially
create potential confusion especially when you're collaborating building
when you're collaborating building something with someone we want to avoid
something with someone we want to avoid that ambiguity and so the convention
that ambiguity and so the convention humans decided on years ago is that if
humans decided on years ago is that if you want to make clear that a number is
you want to make clear that a number is in hexadecimal just by convention you
in hexadecimal just by convention you prefix all of the digits with Zer X the
prefix all of the digits with Zer X the X is not like another character it's not
X is not like another character it's not like a 17th character it's just a human
like a 17th character it's just a human conven
conven of putting 0x to imply here comes
of putting 0x to imply here comes heximal and now it's unambiguous so now
heximal and now it's unambiguous so now we see 0x10 obviously is not 10 as we
we see 0x10 obviously is not 10 as we know it in decimal but rather it's the
know it in decimal but rather it's the number that comes after a single F so
number that comes after a single F so it's really the number in decimal 16 so
it's really the number in decimal 16 so 0x anytime you see it that's just a
0x anytime you see it that's just a visual cue that what is ahead is
visual cue that what is ahead is actually heximal so let's now start
actually heximal so let's now start playing around with this information so
playing around with this information so here's a super simple line of code from
here's a super simple line of code from like week one where I'm just declaring
like week one where I'm just declaring ing a variable n and I'm defining it to
ing a variable n and I'm defining it to be the value 50 and this is out of
be the value 50 and this is out of context we probably need a main function
context we probably need a main function and all of that but let's just rewind to
and all of that but let's just rewind to week one where we actually saw code like
week one where we actually saw code like this and do something useful with a line
this and do something useful with a line of code like this so let me go over here
of code like this so let me go over here to VSS code and in vs code I'll create a
to VSS code and in vs code I'll create a program called how about addresses since
program called how about addresses since the goal of this uh the goal here is to
the goal of this uh the goal here is to just play around ultimately with a
just play around ultimately with a variable like n and let me go ahead and
variable like n and let me go ahead and do this I'll include how about standard
do this I'll include how about standard i.h I'll do int main void so no command
i.h I'll do int main void so no command line Arguments for now in N gets 50 and
line Arguments for now in N gets 50 and now so that we can do something mildly
now so that we can do something mildly useful with it let's just go use print F
useful with it let's just go use print F and print out with percent I and then a
and print out with percent I and then a new line whatever that value of n is so
new line whatever that value of n is so this is not going to be interesting per
this is not going to be interesting per se it's just week one stuff where I'm
se it's just week one stuff where I'm defining a variable and printing it out
defining a variable and printing it out to the screen so let me go down to my
to the screen so let me go down to my terminal window and do make
terminal window and do make addresses no errors so that's good I'll
addresses no errors so that's good I'll do do/ addresses and of course I should
do do/ addresses and of course I should see the number 50 here now what's going
see the number 50 here now what's going on underneath the hood let's translate
on underneath the hood let's translate now code to really what's going on under
now code to really what's going on under under underneath the hood of the
under underneath the hood of the computer so if this is our grid of
computer so if this is our grid of memory I don't necessarily know as the
memory I don't necessarily know as the programmer and I definitely don't care
programmer and I definitely don't care as the programmer where exactly it's
as the programmer where exactly it's ending up in memory that's the whole
ending up in memory that's the whole point of using Code let the computer
point of using Code let the computer figure this out but at least
figure this out but at least conceptually I know that by declaring a
conceptually I know that by declaring a line of code like that the number 50
line of code like that the number 50 ends up somewhere in the computer's
ends up somewhere in the computer's memory and it's assigned the name n a
memory and it's assigned the name n a symbol n via which I the programmer can
symbol n via which I the programmer can refer to it and I very deliberately used
refer to it and I very deliberately used four of these squares for what
four of these squares for what reason what might be the reason for
reason what might be the reason for using four squares specifically yeah
using four squares specifically yeah yeah so an integer is four bytes at
yeah so an integer is four bytes at least most of the time on Modern systems
least most of the time on Modern systems an integer is four bytes on an older
an integer is four bytes on an older computer it might just use one or maybe
computer it might just use one or maybe even uh two bytes But Here by convention
even uh two bytes But Here by convention we're almost always going to see four
we're almost always going to see four bytes I don't know if it's going to end
bytes I don't know if it's going to end up here it might end up over here but
up here it might end up over here but for now who cares I just know that the
for now who cares I just know that the computer can store the the information
computer can store the the information in this way underneath the hood so let's
in this way underneath the hood so let's now introduce another feature of C that
now introduce another feature of C that we haven't had occasion to use just yet
we haven't had occasion to use just yet that's going to allow us to start poking
that's going to allow us to start poking around the computer's memory For Better
around the computer's memory For Better or For Worse and this is one of those
or For Worse and this is one of those situations where you're about to learn
situations where you're about to learn acquire a a skill a power that can
acquire a a skill a power that can actually come back to bite you because
actually come back to bite you because once you know how to start poking around
once you know how to start poking around a computer's memory you can do very
a computer's memory you can do very powerful things and next week we'll see
powerful things and next week we'll see what you can build in a computer's
what you can build in a computer's memory but you can also screw up pretty
memory but you can also screw up pretty easily and cause more of those
easily and cause more of those segmentation faults that a few of you
segmentation faults that a few of you have already suffered so with that said
have already suffered so with that said let's just stipulate that you know what
let's just stipulate that you know what I don't care necessarily where the 50 is
I don't care necessarily where the 50 is in memory but I know it exists at some
in memory but I know it exists at some address in memory and just so I have an
address in memory and just so I have an easy address to pronounce let's just
easy address to pronounce let's just suppose it lives at ox123 so that's the
suppose it lives at ox123 so that's the address in memory in heximal by
address in memory in heximal by convention and that just happens to be
convention and that just happens to be where it ends up when I write that line
where it ends up when I write that line of code but it turns out C has some
of code but it turns out C has some other operators we can use when we've
other operators we can use when we've seen the asterisk before the star and
seen the asterisk before the star and we've used it for multiplication but
we've used it for multiplication but today we're going to use it for
today we're going to use it for something more powerful and we're also
something more powerful and we're also going to introduce an Amper sand which
going to introduce an Amper sand which allows us to do something as well the
allows us to do something as well the Amper sand operator is going to allow us
Amper sand operator is going to allow us to get the address of a piece of data in
to get the address of a piece of data in memory like by literally putting
memory like by literally putting Ampersand before the name of a variable
Ampersand before the name of a variable C will tell us tell you what address
C will tell us tell you what address that variable lives at maybe it's ox123
that variable lives at maybe it's ox123 maybe it's Ox 456 who knows but that
maybe it's Ox 456 who knows but that will give you back the answer the Star
will give you back the answer the Star does the opposite it's sort of means go
does the opposite it's sort of means go there so using the star otherwise known
there so using the star otherwise known as the D reference operator I can
as the D reference operator I can actually go to a specific address if I
actually go to a specific address if I want and we'll see what this means in
want and we'll see what this means in code so how can I leverage this in some
code so how can I leverage this in some mildly interesting way to start poking
mildly interesting way to start poking around but eventually we'll use this
around but eventually we'll use this primitive to build more interesting
primitive to build more interesting things so let me go back to save vs code
things so let me go back to save vs code here and let me go ahead and do this
here and let me go ahead and do this I'll clear my terminal to start fresh
I'll clear my terminal to start fresh and I'll introduce another format code
and I'll introduce another format code for printf percent p and for now just
for printf percent p and for now just take on faith that this it is percent P
take on faith that this it is percent P because but percent p is going to allow
because but percent p is going to allow me to print the address of a variable if
me to print the address of a variable if I additionally tell C get the address of
I additionally tell C get the address of n so I'm changing percent I to percent p
n so I'm changing percent I to percent p and that's just something you have to do
and that's just something you have to do when printing addresses for now but I
when printing addresses for now but I need to change an ampersand in front of
need to change an ampersand in front of the variable name so I don't print in
the variable name so I don't print in the number 50 I print out something like
the number 50 I print out something like ox123 and it's not going to be as simple
ox123 and it's not going to be as simple as that we'll see on the screen though
as that we'll see on the screen though where it actually ended up in my code
where it actually ended up in my code spaces memory so here we go uh dot uh
spaces memory so here we go uh dot uh down in my terminal make addresses again
down in my terminal make addresses again to recompile and now dot SL addresses
to recompile and now dot SL addresses should reveal not the value of 50 but
should reveal not the value of 50 but the address of 50 and there it is it's
the address of 50 and there it is it's pretty long it's not quite as simple and
pretty long it's not quite as simple and pretty as ox123 but there's the ox
pretty as ox123 but there's the ox meaning here's a hexadecimal address and
meaning here's a hexadecimal address and it's 7f FC c784 a04 C suffice it to say
it's 7f FC c784 a04 C suffice it to say your codes space and even your Macs and
your codes space and even your Macs and PCs nowadays have a lot of memory that's
PCs nowadays have a lot of memory that's why in part this address is so big not
why in part this address is so big not as small as the thing on my slide so
as small as the thing on my slide so this at the moment isn't that useful yet
this at the moment isn't that useful yet but it introduces us to a concept that
but it introduces us to a concept that we'll Now call pointers and pointers are
we'll Now call pointers and pointers are admittedly one of the more challenging
admittedly one of the more challenging aspects of c and if in future life you
aspects of c and if in future life you tell friends that oh I took a class
tell friends that oh I took a class called cs50 and we learned C like you'll
called cs50 and we learned C like you'll probably get kind of a look at people
probably get kind of a look at people like why did you learn C or like oh c
like why did you learn C or like oh c was hard and it's largely because of
was hard and it's largely because of this topic which isn't to say that it's
this topic which isn't to say that it's that hard to wrap your mind around but
that hard to wrap your mind around but it's definitely very different and it's
it's definitely very different and it's not a feature that you can harness in
not a feature that you can harness in higher level languages that we'll see in
higher level languages that we'll see in class two like Python and Java and the
class two like Python and Java and the like C is about as close to the
like C is about as close to the computer's Hardware so to speak that you
computer's Hardware so to speak that you can get before things get actually scary
can get before things get actually scary the so-called Assembly Language we saw
the so-called Assembly Language we saw in week two when I had a link and
in week two when I had a link and compile and assemble and all of that
compile and assemble and all of that like that gets really lowlevel and you
like that gets really lowlevel and you really have to be an expert with the
really have to be an expert with the computer's CPU or brain to understand
computer's CPU or brain to understand that but with C you can actually poke
that but with C you can actually poke around the computer's memory and do
around the computer's memory and do powerful things with that but again with
powerful things with that but again with great power comes responsibility it's
great power comes responsibility it's very easy to break programs by misusing
very easy to break programs by misusing memory or just having a bug that touches
memory or just having a bug that touches memory in some way that you don't intend
memory in some way that you don't intend so pointers at the end of the day are
so pointers at the end of the day are pretty much what we just saw a pointer
pretty much what we just saw a pointer is really just a variable that contains
is really just a variable that contains the address of some value a pointer is a
the address of some value a pointer is a variable that contains the address of
variable that contains the address of some value or more simply it's fine to
some value or more simply it's fine to think of it as an address a pointer is
think of it as an address a pointer is an address of something in the
an address of something in the computer's memory now what might we do
computer's memory now what might we do to uh to uh actualize this well here's
to uh to uh actualize this well here's two lines of code it turns out by using
two lines of code it turns out by using our two new operators today I can
our two new operators today I can declare an INT call it n and assign it a
declare an INT call it n and assign it a value like 50 just like before if I want
value like 50 just like before if I want to store the address of n in a variable
to store the address of n in a variable and not just print it immediately via
and not just print it immediately via printf I can declare a variable for
printf I can declare a variable for instance called P but I could call it
instance called P but I could call it anything I want like any variable but
anything I want like any variable but because it's an address it's not int P
because it's an address it's not int P it has to be int star P so to speak and
it has to be int star P so to speak and the star here on the left hand side of
the star here on the left hand side of the equal sign is just a clue to C that
the equal sign is just a clue to C that means p is going to be a pointer that is
means p is going to be a pointer that is p is going to be the address of what the
p is going to be the address of what the address of an integer now technically
address of an integer now technically it's still an integer itself right
it's still an integer itself right because an address is just a number
because an address is just a number whether it's 1 2 3 or ox123 so this is
whether it's 1 2 3 or ox123 so this is really just a semantic difference so in
really just a semantic difference so in Star p just means that this variable
Star p just means that this variable doesn't contain any old number like 50
doesn't contain any old number like 50 it specifically contains a number that
it specifically contains a number that is the address of something
is the address of something else so how can I now use this well let
else so how can I now use this well let me go back to VSS code and let me
me go back to VSS code and let me propose that we add a line of code like
propose that we add a line of code like that so instead of just directly
that so instead of just directly printing out that value let's go ahead
printing out that value let's go ahead and Define a second variable called uh P
and Define a second variable called uh P that's of type in Star P set it equal to
that's of type in Star P set it equal to Ampersand n and then this time let's not
Ampersand n and then this time let's not just print out Amper sand n let's
just print out Amper sand n let's actually print out the value of P so the
actually print out the value of P so the only two new things here if I zoom in
only two new things here if I zoom in are I've used not only the Amper sand on
are I've used not only the Amper sand on the right to get the address of n I'm
the right to get the address of n I'm now using the star on the Left To Tell C
now using the star on the Left To Tell C that P is still a still a variable as
that P is still a still a variable as always but it's a pointer it is the
always but it's a pointer it is the address of some other value like this
address of some other value like this and I'm still going to print it with the
and I'm still going to print it with the same format code percent P so that
same format code percent P so that doesn't change so let me go ahead and
doesn't change so let me go ahead and zoom out and do make addresses and slash
zoom out and do make addresses and slash addresses and there it is exactly the
addresses and there it is exactly the same thing now in and of itself not that
same thing now in and of itself not that useful yet but the fact that you can now
useful yet but the fact that you can now access the addresses of things in memory
access the addresses of things in memory means that we'll be able to build things
means that we'll be able to build things and construct things and Link things
and construct things and Link things together by knowing where they live so
together by knowing where they live so to speak so any questions on this
to speak so any questions on this technique thus far
technique thus far [Music]
yeah a good question on line six must it be starp and Ampersand and in this case
be starp and Ampersand and in this case yes because what am I doing on the left
yes because what am I doing on the left and I'll get rid of the equal sign for
and I'll get rid of the equal sign for now this would give me a variable called
now this would give me a variable called P that's not an integer per se but
P that's not an integer per se but that's the address of an integer but
that's the address of an integer but without the equal sign I'm not storing
without the equal sign I'm not storing anything in that variable so by adding
anything in that variable so by adding the equal sign and then Ampersand N I am
the equal sign and then Ampersand N I am explicitly figuring out with Ampersand
explicitly figuring out with Ampersand what the address of n is which already
what the address of n is which already exists per line five and tucking it away
exists per line five and tucking it away in this new variable called
in this new variable called P other questions yeah every time you
P other questions yeah every time you run good question every time I run the
run good question every time I run the program it uses up a different piece of
program it uses up a different piece of memory short answer yes computers though
memory short answer yes computers though long story short also have something
long story short also have something called virtual memory so if you run it
called virtual memory so if you run it again and again you might actually see
again and again you might actually see the same addresses on the same Mac or PC
the same addresses on the same Mac or PC or cloud-based server but we'll see in a
or cloud-based server but we'll see in a bit where uh at a high level it's laid
bit where uh at a high level it's laid out but it will always exist at some
out but it will always exist at some address good question yeah some correct
address good question yeah some correct Ampersand n is the address of N and in
Ampersand n is the address of N and in Star p
Star p is a pointer called p and honestly in an
is a pointer called p and honestly in an Ideal World if C were made today and not
Ideal World if C were made today and not decades ago when humans were first
decades ago when humans were first creating languages you know ideally we
creating languages you know ideally we would just have a data type called
would just have a data type called pointer and then this would be a little
pointer and then this would be a little less complicated because it would
less complicated because it would literally be what it says you know the
literally be what it says you know the humans who invented SE didn't do this
humans who invented SE didn't do this but this is the idea so pointer is not a
but this is the idea so pointer is not a legitimate word in the code it is a term
legitimate word in the code it is a term of Art in English but this is really
of Art in English but this is really just the idea but the way you express
just the idea but the way you express pointer as a data type type is a little
pointer as a data type type is a little more cryptic as int star P here but
more cryptic as int star P here but notice in line seven when I print out p
notice in line seven when I print out p i don't use a star I don't use an
i don't use a star I don't use an ampersand y I literally just want to
ampersand y I literally just want to print the value of p and we've been
print the value of p and we've been doing that since week one if you want to
doing that since week one if you want to print a variable just describe the
print a variable just describe the variable by its name no special syntax
variable by its name no special syntax any other questions on this thus
any other questions on this thus far uh what's the advantage of using
far uh what's the advantage of using pointers with pointers we'll see today
pointers with pointers we'll see today some applications of them really the
some applications of them really the idea is going to come to fruition next
idea is going to come to fruition next week when we're going to create what are
week when we're going to create what are called uh data structures in memory
called uh data structures in memory where we can build not just uh for
where we can build not just uh for instance uh one-dimensional data
instance uh one-dimensional data structures like an array we'll see next
structures like an array we'll see next week we can actually create the
week we can actually create the equivalent of two-dimensional data
equivalent of two-dimensional data structures or even threedimensional data
structures or even threedimensional data structures by using these addresses and
structures by using these addresses and sort of linking things together and
sort of linking things together and we'll see the beginnings of that this
we'll see the beginnings of that this week but for now focus at least for now
week but for now focus at least for now on just really the syntax and what these
on just really the syntax and what these building blocks can do for us um does
building blocks can do for us um does the poin have to be does the p uh in
the poin have to be does the p uh in does the pop pointer have to be an point
does the pop pointer have to be an point to an integer short answer no and we'll
to an integer short answer no and we'll come back to this for now for the sake
come back to this for now for the sake of discussion we're only dealing with
of discussion we're only dealing with integers like the number 50 uh you
integers like the number 50 uh you mentioned strings or characters
mentioned strings or characters absolutely we're about to go there soon
absolutely we're about to go there soon so you can use the address of anything
so you can use the address of anything you want in the computer's memory so in
you want in the computer's memory so in fact let's translate this now to just
fact let's translate this now to just the same picture just to help you wrap
the same picture just to help you wrap your minds around what these two lines
your minds around what these two lines of code really fundamentally are doing
of code really fundamentally are doing so if I come back to my grid of memory
so if I come back to my grid of memory here let's plop the number 50 in the
here let's plop the number 50 in the variable n at the bottom right like it
variable n at the bottom right like it was before so this is that first line of
was before so this is that first line of code as before but with the new second
code as before but with the new second line of code as soon as I create P what
line of code as soon as I create P what do I do well first remember that n lives
do I do well first remember that n lives somewhere in the computer's memory
somewhere in the computer's memory usually I don't care precisely where it
usually I don't care precisely where it is but for the sake of discussion let's
is but for the sake of discussion let's suppose it's at ox123 which is easier to
suppose it's at ox123 which is easier to say than where it actually ended up and
say than where it actually ended up and now what is p well p is just another
now what is p well p is just another variable and variables live in memory
variable and variables live in memory too so let me just hypothesize that P
too so let me just hypothesize that P lives up here and it turns out that P
lives up here and it turns out that P once you assign it the value of
once you assign it the value of Ampersand N means that c will take a
Ampersand N means that c will take a look at the variable n realize oh it
look at the variable n realize oh it lives at ox123 and what goes in the
lives at ox123 and what goes in the value of p is literally
value of p is literally ox123 so again it's still an integer
ox123 so again it's still an integer which is confusing but it's technically
which is confusing but it's technically an integer being used as an address and
an integer being used as an address and now just a a prompt here notice that
now just a a prompt here notice that this pointer is pretty darn big it's
this pointer is pretty darn big it's like eight squares what's the
like eight squares what's the implication of that because I did that
implication of that because I did that deliberately
deliberately how big must a pointer apparently be in
how big must a pointer apparently be in most modern systems would you say okay
most modern systems would you say okay good computers today are very big you
good computers today are very big you have gigabytes of RAM in your computer
have gigabytes of RAM in your computer you therefore need big pointers to be
you therefore need big pointers to be able to point at memory that's
able to point at memory that's conceptually pretty far away so to be
conceptually pretty far away so to be clear how many bytes does a pointer
clear how many bytes does a pointer apparently take up well it seems to take
apparently take up well it seems to take up eight in total integers by convention
up eight in total integers by convention nowadays are usually four pointers
nowadays are usually four pointers though nowadays are typically eight in
though nowadays are typically eight in this case so I'm drawing it in a manner
this case so I'm drawing it in a manner consistent with the reality even though
consistent with the reality even though at the end of the day it's not really
at the end of the day it's not really that interesting what values are in here
that interesting what values are in here in fact let's emerge from these weeds I
in fact let's emerge from these weeds I don't really care what else is going on
don't really care what else is going on in my computer's memory at the moment
in my computer's memory at the moment because I've only got those two lines of
because I've only got those two lines of Juicy code defining n and defining P so
Juicy code defining n and defining P so let's hide all of the other squares and
let's hide all of the other squares and honestly I mean it when I say that
honestly I mean it when I say that programmers need to know that a variable
programmers need to know that a variable exists somewhere in memory and needs to
exists somewhere in memory and needs to be able to get that address using like
be able to get that address using like the Ampersand but you're never going to
the Ampersand but you're never going to print F like I did the actual address
print F like I did the actual address like it's not generally interesting
like it's not generally interesting unless you're debugging your code but
unless you're debugging your code but you're not going to like start typing
you're not going to like start typing out crazy Ox numbers in your code to
out crazy Ox numbers in your code to move things around you just need to know
move things around you just need to know that the computer can figure out where
that the computer can figure out where things are so frankly by that logic who
things are so frankly by that logic who cares that it's ox123 right tomorrow it
cares that it's ox123 right tomorrow it could be Ox 456 or something else so one
could be Ox 456 or something else so one of the ways to think of a pointer is
of the ways to think of a pointer is literally as a variable that points at
literally as a variable that points at something else and indeed in this case P
something else and indeed in this case P yeah technically it has an address and
yeah technically it has an address and yeah technically it's ox123 in the story
yeah technically it's ox123 in the story but honestly who cares I just need to
but honestly who cares I just need to know that you using p i can get to the
know that you using p i can get to the value n and so what are these addresses
value n and so what are these addresses and in fact if Carter wouldn't mind
and in fact if Carter wouldn't mind joining me up here for a moment what are
joining me up here for a moment what are these addresses well just like in our
these addresses well just like in our human world we have mailboxes even
human world we have mailboxes even though you might not check it very
though you might not check it very frequently nowadays but to get physical
frequently nowadays but to get physical mail every uh home every business has a
mail every uh home every business has a unique address the uh science and
unique address the uh science and engineering complex is 150 Western
engineering complex is 150 Western Avenue Austin Massachusetts 02134 USA
Avenue Austin Massachusetts 02134 USA and theoretically that uniquely
and theoretically that uniquely identifies that building in the world
identifies that building in the world well here we we have two mailboxes um
well here we we have two mailboxes um over here we have a value n that happens
over here we have a value n that happens to live I'll claim it address ox123 and
to live I'll claim it address ox123 and then over here I claim there's another
then over here I claim there's another address uh called by name p i don't
address uh called by name p i don't actually care where it is even though it
actually care where it is even though it definitely exists somewhere in the
definitely exists somewhere in the computer's memory but if this is p which
computer's memory but if this is p which is a variable and that's n another
is a variable and that's n another variable ideally this mailbox would be
variable ideally this mailbox would be twice as big because of the number of
twice as big because of the number of btes using but Home Depot only had
btes using but Home Depot only had identical size mailboxes but here is p
identical size mailboxes but here is p one variable there is in another
one variable there is in another variable if I open up this mailbox what
variable if I open up this mailbox what should I find inside of it based on our
should I find inside of it based on our story thus far like what value will I
story thus far like what value will I pull out dramatically in just a
pull out dramatically in just a moment yeah I think ox1 123 Now using
moment yeah I think ox1 123 Now using this you can kind of think of this as
this you can kind of think of this as like x marks the spot no pun intended
like x marks the spot no pun intended where I can now like walk around the
where I can now like walk around the computer's memory and find my way to
computer's memory and find my way to that location by sort of following the
that location by sort of following the treasure map or if I want it more
treasure map or if I want it more dramatically uh thanks to our little
dramatically uh thanks to our little Yale foam finger here here you can think
Yale foam finger here here you can think of it more abstractly as p is just
of it more abstractly as p is just pointing at n okay that's not going over
pointing at n okay that's not going over well so let's switch over to the Harvard
well so let's switch over to the Harvard one so p is
pointing so p is pointing at n and so it turns out we will be able to write code
turns out we will be able to write code now that will do the equivalent of me
now that will do the equivalent of me walking over to n but for now Carter if
walking over to n but for now Carter if you want to reveal what's in the mailbox
you want to reveal what's in the mailbox we should see indeed the number 50 so
we should see indeed the number 50 so that's really all that's oh I feel
that's really all that's oh I feel Carter's waiting for Applause so like
Carter's waiting for Applause so like really well nicely done
thank you so that's just like a physical metaphor of what's going on here in one
metaphor of what's going on here in one variable we have an address and that
variable we have an address and that variable by convention is called a
variable by convention is called a pointer in the other variable per week
pointer in the other variable per week one we just have a value like n and you
one we just have a value like n and you can yes follow the map and walk yourself
can yes follow the map and walk yourself to that particular address and we'll see
to that particular address and we'll see how to do that in code but what's really
how to do that in code but what's really interesting is this abstraction that
interesting is this abstraction that pointers literally or really I guess
pointers literally or really I guess figuratively point at some other value
figuratively point at some other value in memory all right questions then on
in memory all right questions then on pointers in this form pointers point to
pointers in this form pointers point to each other can pointers point to each
each other can pointers point to each other so yes there's things called
other so yes there's things called double pointers we're not going to see
double pointers we're not going to see them anytime soon but using star star
them anytime soon but using star star you can express an address of an address
you can express an address of an address um but we won't see that just yet other
um but we won't see that just yet other questions on
questions on pointers yeah in
front our array so to summarize our arrays then pointers so short answer
arrays then pointers so short answer there's a relationship and we'll come
there's a relationship and we'll come back to that in a little bit but arrays
back to that in a little bit but arrays are technically different from pointers
are technically different from pointers but we we're going to be able to blur
but we we're going to be able to blur the lines a little bit by using one like
the lines a little bit by using one like the other but let me come back to that
the other but let me come back to that in just a bit of time all right so if we
in just a bit of time all right so if we have now this mental model if you will
have now this mental model if you will of like what a pointer is in memory I
of like what a pointer is in memory I think we can start to peel back a layer
think we can start to peel back a layer of uh simplification that we've been
of uh simplification that we've been assuming for the past few weeks since
assuming for the past few weeks since week one so a string recall is a
week one so a string recall is a sequence of characters and so if you
sequence of characters and so if you want to create a string that says Hi in
want to create a string that says Hi in all caps and an next exclamation point
all caps and an next exclamation point we do string s equals quote unquote high
we do string s equals quote unquote high and we can hard code it like this or we
and we can hard code it like this or we could use get string but for now just
could use get string but for now just assume that I hardcoded it into my code
assume that I hardcoded it into my code to always say hi in all caps with an
to always say hi in all caps with an exclamation point well what does that
exclamation point well what does that look like in the computer's memory well
look like in the computer's memory well let's stop looking at the entire memory
let's stop looking at the entire memory let's just focus on really what's going
let's just focus on really what's going on once you create a string called s and
on once you create a string called s and store in it hi you know that a couple of
store in it hi you know that a couple of things are happening H and I and the
things are happening H and I and the exclamation point are ending up in the
exclamation point are ending up in the computer's memory we know from week two
computer's memory we know from week two that this thing the so-called null
that this thing the so-called null character n AKA back slz is also being
character n AKA back slz is also being added for you and it's somewhere in
added for you and it's somewhere in memory at the moment I don't really care
memory at the moment I don't really care where I drew it at the bottom right yes
where I drew it at the bottom right yes it has an address but for now it just
it has an address but for now it just ends up somewhere and in fact here's a
ends up somewhere and in fact here's a little visual cue as to how this happens
little visual cue as to how this happens in C anytime you use double quotes to
in C anytime you use double quotes to give you a string you can imagine that
give you a string you can imagine that the double quotes are like a a clue to
the double quotes are like a a clue to not only store Hi exclamation point but
not only store Hi exclamation point but also put the null character there for
also put the null character there for you and this is in contrast to what
you and this is in contrast to what chars if you want individual characters
chars if you want individual characters what syntax did we use
what syntax did we use instead so single quotes single quotes
instead so single quotes single quotes do not add magically a back SL zero they
do not add magically a back SL zero they literally just store one character so
literally just store one character so again strings have always been a little
again strings have always been a little special you get some extra an extra bite
special you get some extra an extra bite for free so that you know where the
for free so that you know where the string ends and functions like stir
string ends and functions like stir compare can then find their way there so
compare can then find their way there so in memory it might indeed look a little
in memory it might indeed look a little like this and if we assume that there's
like this and if we assume that there's going to be somewhere in memory these
going to be somewhere in memory these things are going to be somewhere in
things are going to be somewhere in memory we can address them per week two
memory we can address them per week two by way of the name of the variable so if
by way of the name of the variable so if s is the name of the variable S braet 0
s is the name of the variable S braet 0 is how you would refer to the first
is how you would refer to the first letter s bracket 1 s braet two and if
letter s bracket 1 s braet two and if you really want s bracket 3 would get
you really want s bracket 3 would get you at the uh null character at the very
you at the uh null character at the very end but what is s so technically in this
end but what is s so technically in this line of code here not only is the
line of code here not only is the computer giving you memory for h i
computer giving you memory for h i exclamation point back sl0 we turns out
exclamation point back sl0 we turns out that s itself must take up some amount
that s itself must take up some amount of space right because s is the variable
of space right because s is the variable and every time we' talked about
and every time we' talked about variables thus far I've given you a
variables thus far I've given you a rectangle on the screen in which to
rectangle on the screen in which to store its value so let's assume for the
store its value so let's assume for the sake of discussion that the H is at
sake of discussion that the H is at ox123 and I is at o x124 exclamation
ox123 and I is at o x124 exclamation points at o x125 and the null characters
points at o x125 and the null characters at ox1 126 well what then is s well s is
at ox1 126 well what then is s well s is just going to be some other variable and
just going to be some other variable and I'll draw it somewhat abstractly without
I'll draw it somewhat abstractly without all the other boxes up here and I'll
all the other boxes up here and I'll claim that the name of this variable is
claim that the name of this variable is s but it turns out what is s really how
s but it turns out what is s really how do strings Really Work Well s is a
do strings Really Work Well s is a variable and has been since week one but
variable and has been since week one but when you define it what the computer is
when you define it what the computer is doing for you automatically is when it
doing for you automatically is when it knows you want to store Hi exclamation
knows you want to store Hi exclamation point it puts that somewhere in memory
point it puts that somewhere in memory the computer then figures out for you
the computer then figures out for you what's the address of the very first
what's the address of the very first character and it stores that address and
character and it stores that address and only that address in the variable you
only that address in the variable you created on the left hand side of the
created on the left hand side of the equal sign and that's enough like to
equal sign and that's enough like to represent a string with three letters of
represent a string with three letters of the alph alphabet or punctuation you
the alph alphabet or punctuation you don't need three variables you just need
don't need three variables you just need one you just need to know the beginning
one you just need to know the beginning of the string why why is it sufficient
of the string why why is it sufficient for a variable to Only Store the first
for a variable to Only Store the first byes address and not all of the bytes
addresses exactly because of the design of strings per week two we always null
of strings per week two we always null terminate them so it suffices to only
terminate them so it suffices to only remember the first bites address because
remember the first bites address because from there you can sort of follow the
from there you can sort of follow the breadcrumbs bite after bite after bite
breadcrumbs bite after bite after bite and until you see the new line Sorry the
and until you see the new line Sorry the the null character you know that all of
the null character you know that all of those characters are apparently part of
those characters are apparently part of the same string so this is what's been
the same string so this is what's been going on in the me computer's memory all
going on in the me computer's memory all since week one and in fact if we
since week one and in fact if we abstract this away you can really think
abstract this away you can really think of s as being just this really a pointer
of s as being just this really a pointer to that chunk of memory so in fact what
to that chunk of memory so in fact what do we have here well in the left to
do we have here well in the left to recap on the code here on the left hand
recap on the code here on the left hand side string that's what ensures that
side string that's what ensures that we'll actually be able to store a string
we'll actually be able to store a string in a variable called s we're going to
in a variable called s we're going to have on the uh right hand side though
have on the uh right hand side though the actual value so let me switch back
the actual value so let me switch back to VSS code here and let me change my
to VSS code here and let me change my code to no longer involve integers alone
code to no longer involve integers alone so I'm going to add the uh cs50 Library
so I'm going to add the uh cs50 Library just so that I can use some shortcuts in
just so that I can use some shortcuts in there cs50.h and then in my main
there cs50.h and then in my main function I'm going to go ahead and do
function I'm going to go ahead and do this string s equals quote unquote high
this string s equals quote unquote high in all caps exclamation point and then
in all caps exclamation point and then I'm going to go ahead and print out
I'm going to go ahead and print out using percent S as always back sln the
using percent S as always back sln the value of s so this program at the moment
value of s so this program at the moment not interesting at all it's just week
not interesting at all it's just week one stuff again/ addresses indeed prints
one stuff again/ addresses indeed prints out high but it turns out that now that
out high but it turns out that now that I know this what's really been going on
I know this what's really been going on underneath the hood all this time well
underneath the hood all this time well here's that same line of code that
here's that same line of code that defines the variable called s and it
defines the variable called s and it turns out anyone want to guess what
turns out anyone want to guess what string is actually a synonym
string is actually a synonym for string it turns out is kind of a
for string it turns out is kind of a white lie we've been telling since week
white lie we've been telling since week one there is no such thing as string as
one there is no such thing as string as a keyword in in C it's technically a
a keyword in in C it's technically a cs50 thing
cs50 thing yeah it's a pointer to a character so
yeah it's a pointer to a character so really all this time we've kind of been
really all this time we've kind of been lying to you there is no string quote
lying to you there is no string quote unquote it's actually Char star and if I
unquote it's actually Char star and if I may it dramatically here go the training
may it dramatically here go the training wheels like okay that didn't land very
wheels like okay that didn't land very well so uh what have we been doing well
well so uh what have we been doing well it turns out that string is a much
it turns out that string is a much easier way conceptually to think about
easier way conceptually to think about what a string of characters is like my
what a string of characters is like my God if we had to start in week one by
God if we had to start in week one by having you type char
having you type char like yeah you might get past it but like
like yeah you might get past it but like this is just way too much ugly syntax
this is just way too much ugly syntax not intellectually interesting at all so
not intellectually interesting at all so we abstract it away what a Char star was
we abstract it away what a Char star was in the first week of C by telling you
in the first week of C by telling you it's actually called string now string
it's actually called string now string is a term of art like C programmers
is a term of art like C programmers programmers are in any language we'll
programmers are in any language we'll use the word string to mean a sequence
use the word string to mean a sequence of characters but in C it's not
of characters but in C it's not technically a word unto itself it's
technically a word unto itself it's rather a synonym that we ourselves
rather a synonym that we ourselves created in some form so in fact how did
created in some form so in fact how did we do this well think back to just last
we do this well think back to just last week last week I proposed that it'd be
week last week I proposed that it'd be really nice if we had a person data type
really nice if we had a person data type which the creators of C did not think of
which the creators of C did not think of decades ago but that's okay we can
decades ago but that's okay we can Define it ourselves what did we do here
Define it ourselves what did we do here well using syntax like this recall that
well using syntax like this recall that we defined a person to be what to be
we defined a person to be what to be this structure this structure using the
this structure this structure using the new keyword last week struct means that
new keyword last week struct means that a person is just a name and a number and
a person is just a name and a number and it could have been other things we just
it could have been other things we just kept it simple but how did I associate
kept it simple but how did I associate person with that structure well we
person with that structure well we claimed that it was this value here type
claimed that it was this value here type def which as you might expect defines a
def which as you might expect defines a data type so what did we do as cs50 back
data type so what did we do as cs50 back in week one without telling you well we
in week one without telling you well we could have done something like this like
could have done something like this like int itself is a little cryptic and maybe
int itself is a little cryptic and maybe we should have to keep things even
we should have to keep things even simpler said hey everyone turns out you
simpler said hey everyone turns out you can Define integers in C and if you
can Define integers in C and if you wanted to do this well if you want to
wanted to do this well if you want to create the keyword integer as a data
create the keyword integer as a data type you can just typ def it to int so
type you can just typ def it to int so typed def creates the word on the far
typed def creates the word on the far right integer and U creates a synonym
right integer and U creates a synonym for it in this case called int so what
for it in this case called int so what did we do in week one without telling
did we do in week one without telling you we have a line of code like this in
you we have a line of code like this in the cs-50 library that Associates quote
the cs-50 library that Associates quote unquote string with more cryptically
unquote string with more cryptically charar and this is why in week one
charar and this is why in week one onward anytime you use the cs50 library
onward anytime you use the cs50 library you can write the word string as though
you can write the word string as though it's a real C data type and that's just
it's a real C data type and that's just because we wanted to have this
because we wanted to have this abstraction these training wheels on for
abstraction these training wheels on for the first weeks so we don't have to get
the first weeks so we don't have to get weeds of all this crazy memory stuff we
weeds of all this crazy memory stuff we can sort of talk about strings at a
can sort of talk about strings at a higher level but that's all they are
higher level but that's all they are strings are the address of the first
strings are the address of the first character in that sequence of characters
character in that sequence of characters questions now on any of these details
questions now on any of these details yeah strings
yeah strings liar good question what about the
liar good question what about the strings Library which we have used um
strings Library which we have used um unrelated so it does not define the word
unrelated so it does not define the word string everything in there actually
string everything in there actually relates to char stars and so in fact if
relates to char stars and so in fact if you've used the CS5
you've used the CS5 uh manual uh which is just our
uh manual uh which is just our userfriendly version of the actual
userfriendly version of the actual manual pages for the official language C
manual pages for the official language C you'll see throughout that now if you
you'll see throughout that now if you start poking around or turning off less
start poking around or turning off less comfortable mode you'll actually see
comfortable mode you'll actually see that we've changed any mentions of
that we've changed any mentions of charar in the official documentation for
charar in the official documentation for these first weeks to just string to
these first weeks to just string to simplify it but underneath the hood C
simplify it but underneath the hood C does not know the word string per se as
does not know the word string per se as a keyword but it's absolutely a concept
a keyword but it's absolutely a concept that like every program in the world
that like every program in the world knows about and in fact in other
knows about and in fact in other languages in Python for instance there
languages in Python for instance there will actually be a proper string
will actually be a proper string although it's not going to be called
although it's not going to be called string it's going to be called stir St
string it's going to be called stir St Str for short questions on these strings
Str for short questions on these strings here well let me propose there's one
here well let me propose there's one other feature of this syntax that we can
other feature of this syntax that we can now leverage as follows let me propose
now leverage as follows let me propose that if we go back to the previous
that if we go back to the previous version of my code here wherein let me
version of my code here wherein let me switch back to vs code in just a moment
switch back to vs code in just a moment I'm going to rewind in vs code to the
I'm going to rewind in vs code to the integer version of my code from before
integer version of my code from before and most recently it looked looked like
and most recently it looked looked like this before when we were using integers
this before when we were using integers only and not in fact strings at all let
only and not in fact strings at all let me propose that there's this other
me propose that there's this other feature of C that we can use that
feature of C that we can use that actually allows us to go to an address
actually allows us to go to an address so at the moment let me just rewind and
so at the moment let me just rewind and do make addresses to remind you what
do make addresses to remind you what this program do did when it was using
this program do did when it was using integers alone and there's that address
integers alone and there's that address why because on line seven notice I'm
why because on line seven notice I'm printing out the value of P which is a
printing out the value of P which is a pointer so of course it's going to look
pointer so of course it's going to look like an address but let me zoom out now
like an address but let me zoom out now and make one change and instead of
and make one change and instead of printing out P how can I use today's
printing out P how can I use today's second new operator not the Ampersand
second new operator not the Ampersand but the star to actually go to that
but the star to actually go to that address well what I can actually do on
address well what I can actually do on this line of code is this if I want to
this line of code is this if I want to print out the actual integer 50 that's
print out the actual integer 50 that's in that variable or equivalently at that
in that variable or equivalently at that address I can go to P here and not print
address I can go to P here and not print P literally because that's just an
P literally because that's just an address I can now say star p and star p
address I can now say star p and star p means go there more technically
means go there more technically dreference p that is follow the treasure
dreference p that is follow the treasure map to the actual address and do what
map to the actual address and do what Carter did open the mailbox and print
Carter did open the mailbox and print whatever was in the mailbox which recall
whatever was in the mailbox which recall was the actual number 50 so let me try
was the actual number 50 so let me try this let me recompile the code so make
this let me recompile the code so make addresses okay let me clear my terminal
addresses okay let me clear my terminal window do/ addresses this time I
window do/ addresses this time I shouldn't see the ox anything I should
shouldn't see the ox anything I should see just the number 50 in this case and
see just the number 50 in this case and here to is kind of a unfortunate design
here to is kind of a unfortunate design decision certainly pedagogically I would
decision certainly pedagogically I would say in C if I zoom in on this code star
say in C if I zoom in on this code star is unfortunately being used in two
is unfortunately being used in two different ways in an Ideal World they
different ways in an Ideal World they would have used three different symbols
would have used three different symbols to make this more semantically clear but
to make this more semantically clear but this is what we're stuck with so in line
this is what we're stuck with so in line six when you declare a pointer that is a
six when you declare a pointer that is a variable that stores an address you put
variable that stores an address you put the type of variable that you want to
the type of variable that you want to point at then a star just because and
point at then a star just because and then the name of the variable and on the
then the name of the variable and on the right hand side you actually get the
right hand side you actually get the address of whatever using Ampersand but
address of whatever using Ampersand but when you want to go to an address you
when you want to go to an address you want to dreference a pointer you don't
want to dreference a pointer you don't use int again and we've never done that
use int again and we've never done that right once you declare a variable you
right once you declare a variable you never again mention the data type but in
never again mention the data type but in the world of pointers now if you want to
the world of pointers now if you want to not print out P but go to whatever
not print out P but go to whatever address p is storing you use star P here
address p is storing you use star P here so a good visual indicator would be when
so a good visual indicator would be when you declare a pointer that is make it
you declare a pointer that is make it exist in your program you have to
exist in your program you have to declare the data type with the star but
declare the data type with the star but when you use a pointer you just use the
when you use a pointer you just use the star in an Ideal World this would be a
star in an Ideal World this would be a completely different symbol but again
completely different symbol but again this is what we have questions now on
this is what we have questions now on that syntax
that syntax [Music]
[Music] yeah uh why can't we just do the Amper
yeah uh why can't we just do the Amper sand here are you saying it was still a
sand here are you saying it was still a little quiet so strictly speaking we do
little quiet so strictly speaking we do not need line six so this is really for
not need line six so this is really for pedagogical sake that I am um defining a
pedagogical sake that I am um defining a separate variable p and then printing it
separate variable p and then printing it out at this point though I'm just kind
out at this point though I'm just kind of you know going in circles if you will
of you know going in circles if you will because more simple would have been what
because more simple would have been what I would have done in week one which
I would have done in week one which would be get rid of P alt together get
would be get rid of P alt together get rid of P here and just print out n right
rid of P here and just print out n right but today we're just giving you this new
but today we're just giving you this new building block these this new syntax via
building block these this new syntax via which you can figure out the address of
which you can figure out the address of something and then reverse the process
something and then reverse the process later and actually go to it as
later and actually go to it as well other questions on what we've done
well other questions on what we've done here with these
here with these pointers all right well let's context
pointers all right well let's context switch back to the string now and see
switch back to the string now and see what more we can do with this here in
what more we can do with this here in the case of our strings here let me uh
the case of our strings here let me uh refine this to zoom out let me delete
refine this to zoom out let me delete the integer related code here let me do
the integer related code here let me do string s equals quote unquote high in
string s equals quote unquote high in all caps let me go ahead and for the
all caps let me go ahead and for the moment include cs50.h at the top so that
moment include cs50.h at the top so that indeed I can use the keyword s or string
indeed I can use the keyword s or string rather and let me go ahead now and do
rather and let me go ahead now and do something more than I did last time last
something more than I did last time last time I did print F of percent s back sln
time I did print F of percent s back sln and then I printed out s and again I'll
and then I printed out s and again I'll recompile this just for clarity make
recompile this just for clarity make addresses addresses that just prints out
addresses addresses that just prints out high so that's again week one stuff but
high so that's again week one stuff but now that we have this other bit of
now that we have this other bit of syntax we can do some interesting things
syntax we can do some interesting things too so for instance suppose I want to
too so for instance suppose I want to print out not s itself but what if I
print out not s itself but what if I want to print out the address of s like
want to print out the address of s like at what memory location is s well I can
at what memory location is s well I can change my percent s to percent P which
change my percent s to percent P which now we know P is for pointer so percent
now we know P is for pointer so percent p means print out the value of a pointer
p means print out the value of a pointer that is an address and here I can
that is an address and here I can actually print out s itself but why that
actually print out s itself but why that is we'll see in a moment let me do this
is we'll see in a moment let me do this here go the training wheels string does
here go the training wheels string does not technically EX exist but it does if
not technically EX exist but it does if I'm using the cs-50 library but if I get
I'm using the cs-50 library but if I get rid of the cs50 library as I'm
rid of the cs50 library as I'm metaphorically doing by taking off the
metaphorically doing by taking off the training wheels I can't use the word
training wheels I can't use the word string anymore and in fact let me make
string anymore and in fact let me make this mistake deliberately as you might
this mistake deliberately as you might have accidentally in past weeks here is
have accidentally in past weeks here is the error message I get if I forget the
the error message I get if I forget the cs50 library use of Undeclared
cs50 library use of Undeclared identifier string did you mean standard
identifier string did you mean standard in it's trying to be helpful but it's
in it's trying to be helpful but it's not because I didn't mean standard in so
not because I didn't mean standard in so indeed this is confirmation that c does
indeed this is confirmation that c does not know the word string exists at least
not know the word string exists at least as a keyword exists as a concept but not
as a keyword exists as a concept but not a keyword so I could fix this by adding
a keyword so I could fix this by adding back the cs50 library but that's kind of
back the cs50 library but that's kind of a step backwards educationally is
a step backwards educationally is instead of a step forward what could I
instead of a step forward what could I do instead to fix this now if the
do instead to fix this now if the training wheels are now off
yeah exactly replace string quote unquote with Char star instead so I'm
unquote with Char star instead so I'm going to go ahead and change this to
going to go ahead and change this to char technically you can put the literal
char technically you can put the literal star here the asterisk or you can put it
star here the asterisk or you can put it there or you can put it here by
there or you can put it here by convention is to do what I done from the
convention is to do what I done from the beginning put the star next to the name
beginning put the star next to the name of the variable as opposed to anywhere
of the variable as opposed to anywhere else uh let me go ahead now and or sorry
else uh let me go ahead now and or sorry I meant to add the spaces there you
I meant to add the spaces there you could do this too but this would be the
could do this too but this would be the most normal convention so now let's do
most normal convention so now let's do this make addresses compile is okay now
this make addresses compile is okay now do slash addresses what should I see
do slash addresses what should I see high or something else feel free to just
high or something else feel free to just call it
call it out so still high you say someone
out so still high you say someone else memory location a memory location
else memory location a memory location all right so could be one of the two
all right so could be one of the two options right either I'm going to see
options right either I'm going to see the string or I'm going to see a memory
the string or I'm going to see a memory address though I do in fact see a memory
address though I do in fact see a memory address and this one's quite different
address and this one's quite different from the integer one but does anyone now
from the integer one but does anyone now want to explain why you were correct why
want to explain why you were correct why am I seeing the address down here and
am I seeing the address down here and not hi it's subtle
yeah exactly because I left my percent P there which means hey print F show me a
there which means hey print F show me a pointer but this is where printf is
pointer but this is where printf is smart and has been smart since week zero
smart and has been smart since week zero humans who invented printf decades ago
humans who invented printf decades ago uh wrote code that notices that okay
uh wrote code that notices that okay percent s means to treat the following
percent s means to treat the following value not as just an address per se that
value not as just an address per se that gets printed literally but printed as
gets printed literally but printed as with the mailbox demo is sort of a
with the mailbox demo is sort of a treasure map that leads you to the
treasure map that leads you to the address of a character so simply by
address of a character so simply by changing one character percent P to
changing one character percent P to percent s and if I now do make addresses
percent s and if I now do make addresses again and slash addresses this now is
again and slash addresses this now is identical to week one but hopefully
identical to week one but hopefully makes sense because percent s is just a
makes sense because percent s is just a clue to print f that means go to this
clue to print f that means go to this address in s print out every character
address in s print out every character there and thereafter until you see what
there and thereafter until you see what the null character and then stop
the null character and then stop printing anything more and this is why
printing anything more and this is why hi has printed since week one today we
hi has printed since week one today we can see the address percent P but this
can see the address percent P but this combination of having access to
combination of having access to addresses and the null Terminator is all
addresses and the null Terminator is all the information printf needs to actually
the information printf needs to actually do something more useful by like
do something more useful by like printing the actual
printing the actual strings any questions now on this
strings any questions now on this approach to percent s yeah in
approach to percent s yeah in back oh so why is it traditionally being
back oh so why is it traditionally being used in this way honestly like the word
used in this way honestly like the word string has been around for decades it's
string has been around for decades it's not a keyword you should be able to type
not a keyword you should be able to type in C unless you're using a library like
in C unless you're using a library like cs50 um and so percent s just means
cs50 um and so percent s just means string so even though it doesn't exist
string so even though it doesn't exist as a keyword percent s connotes string
as a keyword percent s connotes string and humans decades ago like today just
and humans decades ago like today just kind of know what that means so they
kind of know what that means so they could have chosen any letter of the
could have chosen any letter of the alphabet but s sort of makes the most
alphabet but s sort of makes the most sense all right well let's in back other
sense all right well let's in back other question good question before let me
question good question before let me zoom in I did not use a star before the
zoom in I did not use a star before the S why well it's subtle here but printf
S why well it's subtle here but printf is was invented years ago to know given
is was invented years ago to know given an address like in the variable s printf
an address like in the variable s printf knows to go there so if we looked at the
knows to go there so if we looked at the source code that some human wrote years
source code that some human wrote years ago for C we would likely see the actual
ago for C we would likely see the actual uh asterisk that you're referring to
uh asterisk that you're referring to printf is taking on the responsibility
printf is taking on the responsibility for going to S if you were to do uh star
for going to S if you were to do uh star s here instead an asterisk and then s
s here instead an asterisk and then s that would now be literally a character
that would now be literally a character because if I say p uh star s that means
because if I say p uh star s that means go to the address in s and all you're
go to the address in s and all you're going to find there is a single
going to find there is a single character what printf wants to know is
character what printf wants to know is not what is the character there what is
not what is the character there what is the address of that character why
the address of that character why because printf needs to walk through the
because printf needs to walk through the rest of those characters looking for the
rest of those characters looking for the final null character and in fact let me
final null character and in fact let me see with a bit more syntax if we can
see with a bit more syntax if we can highlight this a bit more let me do this
highlight this a bit more let me do this in addition to printing s let's try out
in addition to printing s let's try out our syntax in another way let me print
our syntax in another way let me print out with percent s how about uh not s
out with percent s how about uh not s here but let's print out some addresses
here but let's print out some addresses percent s back sln close quote and then
percent s back sln close quote and then let's print out how about this the first
let's print out how about this the first character in the string s would be
character in the string s would be called s bracket Z but how do I get the
called s bracket Z but how do I get the address of the first character in s well
address of the first character in s well I could technically just use today's new
I could technically just use today's new primitive I can just add an ampon that
primitive I can just add an ampon that always gives me the address of some
always gives me the address of some value so when I end this thought and
value so when I end this thought and clear my terminal window and run make
clear my terminal window and run make addresses still compiles when I run
addresses still compiles when I run addresses in just a moment any guesses
addresses in just a moment any guesses as to what I will see line by line this
as to what I will see line by line this will print out two
will print out two things and you don't have to remember
things and you don't have to remember what the actual number was but at a high
what the actual number was but at a high level what will be printed now the same
level what will be printed now the same thing twice why well when I run this
thing twice why well when I run this what I'm printing here and let me zoom
what I'm printing here and let me zoom in at the bottom I ined see two really
in at the bottom I ined see two really long addresses but they're in fact the
long addresses but they're in fact the same why well that's because again if s
same why well that's because again if s is the address of a character as implied
is the address of a character as implied Now by either the cs50 word string or
Now by either the cs50 word string or the actual phrase Char star well then s
the actual phrase Char star well then s is just an address by contrast per week
is just an address by contrast per week two s bracket 0 is a Char always has
two s bracket 0 is a Char always has been a Char a specific Char but if you
been a Char a specific Char but if you want the address of that Char you just
want the address of that Char you just add the Ampersand well it turns out that
add the Ampersand well it turns out that strings per the definition we keep
strings per the definition we keep emphasizing is just the address of the
emphasizing is just the address of the first character in a string so of course
first character in a string so of course if you do this you're going to see the
if you do this you're going to see the exact same thing and if I do this a bit
exact same thing and if I do this a bit more generally you don't want to copy
more generally you don't want to copy paste but this is just for uh
paste but this is just for uh visualization sake let me print out all
visualization sake let me print out all the characters so another another
the characters so another another another and let me change this to print
another and let me change this to print out the address of bracket 1 bracket two
out the address of bracket 1 bracket two and bracket three so all four characters
and bracket three so all four characters Hi exclamation point and the null
Hi exclamation point and the null character notice I'm using percent P for
character notice I'm using percent P for all of them so if I now do make
all of them so if I now do make addresses and do/ addresses now notice
addresses and do/ addresses now notice and this is kind of cool the first two
and this is kind of cool the first two are indeed still the same but what's no
are indeed still the same but what's no noteworthy about the other values on the
screen yeah they're consecutive each of these is just one bite away even if
these is just one bite away even if you're not good at hex yet and there's a
you're not good at hex yet and there's a crazy number of digits here who cares
crazy number of digits here who cares they're all the same except for the last
they're all the same except for the last ones four four and then 5 six seven and
ones four four and then 5 six seven and this confirms what I've been claiming
this confirms what I've been claiming for weeks is that in an array all of the
for weeks is that in an array all of the characters are back to back to back
characters are back to back to back contiguous One Bites away so with just
contiguous One Bites away so with just this Ampersand with just this star like
this Ampersand with just this star like it's actually a pretty cool tool in the
it's actually a pretty cool tool in the toolkit to have because you can start to
toolkit to have because you can start to poke around what's actually going on
poke around what's actually going on inside of the computer's memory and in
inside of the computer's memory and in fact if we do this I can introduce one
fact if we do this I can introduce one other cool trick here if you will let me
other cool trick here if you will let me propose that we can actually now do
propose that we can actually now do arithmetic on pointers and you don't
arithmetic on pointers and you don't have to you'll see a simpler way to do
have to you'll see a simpler way to do this but now that you have perhaps this
this but now that you have perhaps this underlying understanding of where things
underlying understanding of where things are in memory and it's just addresses we
are in memory and it's just addresses we can actually do something kind of neat
can actually do something kind of neat we can do something like this uh let me
we can do something like this uh let me go back to how about uh the string
go back to how about uh the string version of this with high and let me do
version of this with high and let me do this instead let me um clean this up a
this instead let me um clean this up a bit get rid of some of these lines of
bit get rid of some of these lines of code and let me do this let me print out
code and let me do this let me print out percent C percent C percent C let me get
percent C percent C percent C let me get rid of all these Amper Sands we're going
rid of all these Amper Sands we're going to roll back to like week two stuff just
to roll back to like week two stuff just to be clear when I compile and run this
to be clear when I compile and run this version of the program and I'll zoom in
version of the program and I'll zoom in what should get printed on the screen
what should get printed on the screen this is just week two stuff now no
this is just week two stuff now no pointers per se yeah
pointers per se yeah mhm just Hi exclamation point one per
mhm just Hi exclamation point one per line because I have all of these back
line because I have all of these back slash ends so let me do that let me go
slash ends so let me do that let me go down here make addresses enter okay
down here make addresses enter okay pretty good/ addresses and indeed Hi
pretty good/ addresses and indeed Hi exclamation point But now if you're
exclamation point But now if you're getting a little more comfortable and
getting a little more comfortable and it's fine if you're not yet today but
it's fine if you're not yet today but over the coming week or weeks as you get
over the coming week or weeks as you get a little more comfortable with the
a little more comfortable with the equivalence of addresses with our
equivalence of addresses with our definition in the past of arrays and
definition in the past of arrays and strings and all of this you can start to
strings and all of this you can start to play around and I can do this instead if
play around and I can do this instead if I I want to print out the first
I I want to print out the first character in the string I could do like
character in the string I could do like week two s bracket zero like that will
week two s bracket zero like that will always work and you can keep using that
always work and you can keep using that that's not a cs50 thing it's just a
that's not a cs50 thing it's just a convenience in C but I could technically
convenience in C but I could technically print out not s because s is an address
print out not s because s is an address but what would be the syntax I could use
but what would be the syntax I could use to say print out the character at
to say print out the character at s any
s any Instinct how can I say go to the address
Instinct how can I say go to the address in
in s it's one of two possible answers
s it's one of two possible answers today so of our two new uh uh of our two
today so of our two new uh uh of our two new operators today we have the
new operators today we have the Ampersand and the star which one will
Ampersand and the star which one will lead us to what is that an address so
lead us to what is that an address so the star so in fact if I want to print
the star so in fact if I want to print out what is that address zero at the
out what is that address zero at the address S I can just do star s and if
address S I can just do star s and if you really want to get fancy how do you
you really want to get fancy how do you print out the second character that's
print out the second character that's immediately to the right of it so to
immediately to the right of it so to speak well you can go to with the D
speak well you can go to with the D reference operator and do you want to
reference operator and do you want to answer this
answer this one s+ one Argo pointer arithmetic like
one s+ one Argo pointer arithmetic like you can do math simple addition
you can do math simple addition subtraction whatever on pointers if you
subtraction whatever on pointers if you want and you can do this here too so
want and you can do this here too so star you want to pluck this one off too
star you want to pluck this one off too how do I print out the last character
how do I print out the last character the
the third s plus2 right because if you know
third s plus2 right because if you know and understand that like a string is
and understand that like a string is just a sequence of characters every
just a sequence of characters every character is just a bite and these bites
character is just a bite and these bites are back to back to back you can just go
are back to back to back you can just go wherever you want in the computer's
wherever you want in the computer's memory and here I can do make addresses
memory and here I can do make addresses again/ addresses and voila we now have
again/ addresses and voila we now have high exclamation point so we haven't
high exclamation point so we haven't printed out anything new but again just
printed out anything new but again just by using these two new operators the
by using these two new operators the erson and the star you can figure out
erson and the star you can figure out the address of something and you can go
the address of something and you can go to the address of something okay
to the address of something okay question in
question in back indeed it ends up being the exact
back indeed it ends up being the exact same and so I might have used this term
same and so I might have used this term before the Amper sand technique where
before the Amper sand technique where sorry the square bracket technique where
sorry the square bracket technique where you do s bracket 0o s bracket One S
you do s bracket 0o s bracket One S bracket 2 that's actually what we would
bracket 2 that's actually what we would really call syntactic Sugar like it
really call syntactic Sugar like it works and you can use it you should use
works and you can use it you should use it it's nice and simple
it it's nice and simple but the square bracket notation
but the square bracket notation underneath the hood is essentially being
underneath the hood is essentially being converted to this which this is not fun
converted to this which this is not fun right like this is when you want to show
right like this is when you want to show off to your friends like you know how to
off to your friends like you know how to do cool stuff in code but this is not as
do cool stuff in code but this is not as readable as just s bracket Z and one and
readable as just s bracket Z and one and two but that's all that's happening
two but that's all that's happening underneath the hood and so again this is
underneath the hood and so again this is why in cs50 we spend time on some of
why in cs50 we spend time on some of these lower level building blocks
these lower level building blocks because if you assume that indeed your
because if you assume that indeed your computer's memory is just this grid of
computer's memory is just this grid of btes and you have now the code ability
btes and you have now the code ability in code to get an address and go to an
in code to get an address and go to an address you can start doing any anything
address you can start doing any anything you want and you can poke around a
you want and you can poke around a computer's memory at any location and
computer's memory at any location and here in lies the danger like I'm kind of
here in lies the danger like I'm kind of on the honor System right now that if my
on the honor System right now that if my string is high exclamation point it's
string is high exclamation point it's kind of up to me to go to the first bite
kind of up to me to go to the first bite the second and the third but I could get
the second and the third but I could get kind of crazy now and if I want to see
kind of crazy now and if I want to see what's going on in the computer's memory
what's going on in the computer's memory I mean there's nothing stopping me from
I mean there's nothing stopping me from doing like s+ 50 and let's see what's
doing like s+ 50 and let's see what's there so make addresses do/ addresses Hi
there so make addresses do/ addresses Hi and then okay nothing it seems well how
and then okay nothing it seems well how about 5,000 bytes away let's poke around
about 5,000 bytes away let's poke around what's inside of the computer's memory
what's inside of the computer's memory so make addresses again uh make
so make addresses again uh make addresses addresses enter okay still
addresses addresses enter okay still nothing there let's try
nothing there let's try 50,000 all right do make addresses do SL
50,000 all right do make addresses do SL addresses okay there we see it so you've
addresses okay there we see it so you've probably done this some of you by
probably done this some of you by accident because you probably went too
accident because you probably went too far to the left or to the right in an
far to the left or to the right in an array touching memory that you shouldn't
array touching memory that you shouldn't suffice it to say I should not go
suffice it to say I should not go blindly touching 50,000 bytes away cuz
blindly touching 50,000 bytes away cuz who knows what's there and indeed in
who knows what's there and indeed in your computer computer when a program is
your computer computer when a program is running the computer uh segments it into
running the computer uh segments it into different segments of memory and if you
different segments of memory and if you get a little too greedy and you touch
get a little too greedy and you touch another segment of memory that
another segment of memory that technically was not allocated to you by
technically was not allocated to you by Mac OS or Windows or Linux or the
Mac OS or Windows or Linux or the operating system bad things happen and
operating system bad things happen and you get a segmentation fault and that
you get a segmentation fault and that means it's a bug in your code so you can
means it's a bug in your code so you can now do this and this means hackers too
now do this and this means hackers too can do things like this if they can
can do things like this if they can somehow inject code into your C program
somehow inject code into your C program maybe they can poke around the
maybe they can poke around the computer's memory and indeed this is
computer's memory and indeed this is kind of the technique whereby maybe a
kind of the technique whereby maybe a really sophisticated hacker can jump to
really sophisticated hacker can jump to this memory this memory this memory
this memory this memory this memory looking for something like your password
looking for something like your password or your financial information or
or your financial information or anything that's in the program but at
anything that's in the program but at some other address there's nothing
some other address there's nothing stopping an adversary at least right now
stopping an adversary at least right now from poking around if they can execute
from poking around if they can execute code on your computer from doing this
code on your computer from doing this kind of thing so there and again is the
kind of thing so there and again is the power of C but also the danger and
power of C but also the danger and you'll absolutely suffer more segals in
you'll absolutely suffer more segals in the coming days but ultimately the goal
the coming days but ultimately the goal is going to be to help you solve them
is going to be to help you solve them ultimately uh and fix things um but for
ultimately uh and fix things um but for now I think that was that was quite a
now I think that was that was quite a bit so let me propose that we go ahead
bit so let me propose that we go ahead and take our longer break here maybe 10
and take our longer break here maybe 10 minutes and have ourselves some whoopy
minutes and have ourselves some whoopy pies in the transcept we'll be back in
pies in the transcept we'll be back in 10 all right so we're back and to recap
10 all right so we're back and to recap where we left off you now have this new
where we left off you now have this new capability in code to do pointer
capability in code to do pointer arithmetic like treat addresses as
arithmetic like treat addresses as numbers which they really are in heximal
numbers which they really are in heximal or otherwise and like add them together
or otherwise and like add them together and kind of poke around a computer's
and kind of poke around a computer's memory and it was asked during break
memory and it was asked during break actually how we might further harness
actually how we might further harness this in the context of string so I
this in the context of string so I didn't change the code we wrote just
didn't change the code we wrote just before break recall that we last broke
before break recall that we last broke the program by checking out bytes 50,000
the program by checking out bytes 50,000 bytes away but let's not do that and
bytes away but let's not do that and let's actually try printing out not
let's actually try printing out not individual characters like I did per the
individual characters like I did per the percent C but why don't we try printing
percent C but why don't we try printing out strings and substrings if you will
out strings and substrings if you will so let me clear my terminal window let
so let me clear my terminal window let me change all of these percent C's to
me change all of these percent C's to percent s percent s percent s and then
percent s percent s percent s and then let me rewind to what we've been doing
let me rewind to what we've been doing since week one with strings which is
since week one with strings which is just print them out for instance with
just print them out for instance with that first line and the only difference
that first line and the only difference at the moment is that now I took off the
at the moment is that now I took off the training wheels I got rid of cs50.h
training wheels I got rid of cs50.h wherein string is typ Def to char star
wherein string is typ Def to char star for you got rid of that so now on line
for you got rid of that so now on line five I'm declaring S as being a Char
five I'm declaring S as being a Char star which just means the address of a
star which just means the address of a character and print f is smart enough to
character and print f is smart enough to know that the end of a string is
know that the end of a string is wherever that null character is but now
wherever that null character is but now that I can do pointer arithmetic notice
that I can do pointer arithmetic notice that I could do something like this if I
that I could do something like this if I want to print out s i just print out s
want to print out s i just print out s suppose I do s+ one here and s+2 here
suppose I do s+ one here and s+2 here again after changing percent C to
again after changing percent C to percent s any intuition around what this
percent s any intuition around what this code will now print on the screen line
code will now print on the screen line by
by line yeah
line yeah thoughts okay reasonable conjecture
thoughts okay reasonable conjecture maybe the memory address of H that of I
maybe the memory address of H that of I that of exclamation point but other
that of exclamation point but other [Music]
[Music] thoughts yeah I think it's going to do
thoughts yeah I think it's going to do the latter it's going to print high in
the latter it's going to print high in the usual way because honestly line five
the usual way because honestly line five is this rather line six is the same as
is this rather line six is the same as like week one stuff except we took off
like week one stuff except we took off the training wheel of string and we're
the training wheel of string and we're calling it Char star but I think line
calling it Char star but I think line seven is indeed going to print out I and
seven is indeed going to print out I and line eight is just going to print out
line eight is just going to print out because it'll be just the exclamation
because it'll be just the exclamation point prti will still be smart enough to
point prti will still be smart enough to know where each of those substrings a
know where each of those substrings a portions of the string End by the same
portions of the string End by the same logic as always but let me go ahead and
logic as always but let me go ahead and zoom out run make addresses enter
zoom out run make addresses enter compiles okay/ addresses and now indeed
compiles okay/ addresses and now indeed this is all a string is it's a sequence
this is all a string is it's a sequence of characters identified by its first
of characters identified by its first bite if you then start poking around and
bite if you then start poking around and tell printf to print at what's at the
tell printf to print at what's at the next bite or the next next bite it's
next bite or the next next bite it's going to do its same thing printing out
going to do its same thing printing out that character and everything after it
that character and everything after it up until that null character so again
up until that null character so again even though there's like a lot going on
even though there's like a lot going on we've introduced these two new operators
we've introduced these two new operators like there's nothing that's happening
like there's nothing that's happening today that hasn't been happening for
today that hasn't been happening for weeks but hopefully through this week uh
weeks but hopefully through this week uh this week's lecture this week's problem
this week's lecture this week's problem set and Beyond you'll start to realize
set and Beyond you'll start to realize that now you just have more tools via
that now you just have more tools via which to harness those lower level
which to harness those lower level implementation details so last week too
implementation details so last week too recall one other implementation detail I
recall one other implementation detail I claimed that you could not compare two
claimed that you could not compare two strings quite as easily as you could
strings quite as easily as you could compare to uh integers for instance and
compare to uh integers for instance and I told you to use a different function
I told you to use a different function instead that you probably used one or
instead that you probably used one or more times with the past problem set how
more times with the past problem set how are you supposed to compare strings
are you supposed to compare strings apparently yeah so string compare stir
apparently yeah so string compare stir comp that additional function that we
comp that additional function that we said H you just have to use it for now
said H you just have to use it for now but you might have a little intuition
but you might have a little intuition already as to like why we have to use
already as to like why we have to use Stir compare and we can't just use
Stir compare and we can't just use equals equals to compare strings like
equals equals to compare strings like any intuition for this already why was
any intuition for this already why was Stir compare necessary last week
Stir compare necessary last week equ perfect equals equals would compare
equ perfect equals equals would compare literally the two memory addresses
literally the two memory addresses instead of the actual strings character
instead of the actual strings character by character and unless the memory
by character and unless the memory addresses are literally the same so you
addresses are literally the same so you compare the that exact same memory
compare the that exact same memory address two different strings probably
address two different strings probably are not going to be considered equal
are not going to be considered equal even if to us humans they indeed look
even if to us humans they indeed look equal so let's see this let me go ahead
equal so let's see this let me go ahead and close addresses. C and actually
and close addresses. C and actually before I do one last mention one of the
before I do one last mention one of the powerful things about Pointer arithmetic
powerful things about Pointer arithmetic as an aside is that c and really the
as an aside is that c and really the compiler is smart enough to know how
compiler is smart enough to know how many bytes to keep adding and adding and
many bytes to keep adding and adding and by that I mean this right now we got
by that I mean this right now we got lucky because a string is a sequence of
lucky because a string is a sequence of characters and by definition every
characters and by definition every character is is a single bite you can
character is is a single bite you can poke around and do s+ one to get the
poke around and do s+ one to get the next bite S Plus 2 to get the uh the
next bite S Plus 2 to get the uh the third bite however if we weren't dealing
third bite however if we weren't dealing with strings suppose we were dealing
with strings suppose we were dealing with integers that were in an array back
with integers that were in an array back to back to back if you wanted to get at
to back to back if you wanted to get at the next integer you could still do plus
the next integer you could still do plus one or plus two to get at the next or
one or plus two to get at the next or the next next integer you would not
the next next integer you would not start to get into the weeds of doing
start to get into the weeds of doing plus four and then plus eight you don't
plus four and then plus eight you don't have to know or care how big the data
have to know or care how big the data types are in the computer C and the
types are in the computer C and the compiler will figure that out for you
compiler will figure that out for you based on the data type in question so
based on the data type in question so keep that in mind if ever doing this on
keep that in mind if ever doing this on a different data type uh than chars all
a different data type uh than chars all right so let me go ahead and open up a
right so let me go ahead and open up a file that I wrote in advance most of and
file that I wrote in advance most of and let me hide my terminal window and show
let me hide my terminal window and show you this so here is a program called
you this so here is a program called compare. C whose purpose in life is to
compare. C whose purpose in life is to compare two strings I'm back to using
compare two strings I'm back to using the cs50 library because at least for
the cs50 library because at least for now in probably a couple more weeks it
now in probably a couple more weeks it is so much easier to get input from the
is so much easier to get input from the user using cs50's function get in but
user using cs50's function get in but we'll conclude today by taking off those
we'll conclude today by taking off those training wheels as well so you can see
training wheels as well so you can see how you can actually get user input with
how you can actually get user input with nothing cs50 specific so line six and
nothing cs50 specific so line six and seven pretty boring week one stuff get
seven pretty boring week one stuff get an INT called I get an INT called J and
an INT called I get an INT called J and store them in two variables I and J
store them in two variables I and J respectively if I equals equals J print
respectively if I equals equals J print out the same else print out that they're
out the same else print out that they're different let me just stipulate for time
different let me just stipulate for time sake I'm pretty sure this code is
sake I'm pretty sure this code is correct this will get two integers from
correct this will get two integers from the human it will compare them and tell
the human it will compare them and tell me correctly if they're the same or
me correctly if they're the same or different and I'll prove as much by
different and I'll prove as much by running make compare
running make compare slash compare and I'll type in 50 for I
slash compare and I'll type in 50 for I 50 for J and they're the same and now
50 for J and they're the same and now I'll do how about 50 and say 13 and
I'll do how about 50 and say 13 and those are different so let me just
those are different so let me just stipulate this code is indeed correct
stipulate this code is indeed correct would have worked in week one also works
would have worked in week one also works now in week four but let me now change
now in week four but let me now change it to compare not two integers but as I
it to compare not two integers but as I hinted maybe two strings instead so let
hinted maybe two strings instead so let me go ahead and change this line of code
me go ahead and change this line of code to maybe be string s equals get string
to maybe be string s equals get string uh asking the user for s s uh then let's
uh asking the user for s s uh then let's change this second line here to be
change this second line here to be string T just to keep the variable name
string T just to keep the variable name short for now and T is a good uh Choice
short for now and T is a good uh Choice after s for something like this get
after s for something like this get string prompt the human for T and then
string prompt the human for T and then let's change our I and J here to do the
let's change our I and J here to do the wrong thing per the intuition earlier if
wrong thing per the intuition earlier if s equals equals T then print out the
s equals equals T then print out the same else print out that they're
same else print out that they're different now if I want I could take off
different now if I want I could take off at least some of the training wheels I
at least some of the training wheels I could change this to char star I could
could change this to char star I could change this to char star either is fine
change this to char star either is fine I still need the Cs library though
I still need the Cs library though because I'm using get string because
because I'm using get string because it's actually hard as we'll see today to
it's actually hard as we'll see today to get strings manually without using a
get strings manually without using a library but I'll keep it using string
library but I'll keep it using string just for now with the library all right
just for now with the library all right make compare
make compare again compare and now let me go ahead
again compare and now let me go ahead and type in for instance Hi exclamation
and type in for instance Hi exclamation point Enter and Hi exclamation point
point Enter and Hi exclamation point Enter and they're different all they're
Enter and they're different all they're obviously not visually but they are
obviously not visually but they are underneath the hood and you probably do
underneath the hood and you probably do have the intuition for this already
have the intuition for this already whereby what's going on underneath the
whereby what's going on underneath the hood is that we're comparing
hood is that we're comparing accidentally the two memory addresses so
accidentally the two memory addresses so in fact let's go there let's consider
in fact let's go there let's consider the memory and let me zoom out now so I
the memory and let me zoom out now so I can just have more btes to play with so
can just have more btes to play with so the squares are a little smaller than
the squares are a little smaller than before just so we can fit more in them
before just so we can fit more in them and let me propose that when I declare s
and let me propose that when I declare s on what was line six a moment ago it
on what was line six a moment ago it ends up somewhere in memory like the top
ends up somewhere in memory like the top leftand corner of my picture for
leftand corner of my picture for discussion sake and when I uh execute
discussion sake and when I uh execute that same line of code and get string is
that same line of code and get string is called and I type in high exclamation
called and I type in high exclamation point we know from week one that get
point we know from week one that get string puts it somewhere in the
string puts it somewhere in the computer's memory and I'll propose that
computer's memory and I'll propose that it's in like the bottom left hand corner
it's in like the bottom left hand corner of the screen here what happens after
of the screen here what happens after that well I know even though I don't
that well I know even though I don't generally care that Hi exclamation point
generally care that Hi exclamation point in the null character exist at some
in the null character exist at some address like ox123 12 4 125 126 for
address like ox123 12 4 125 126 for discussion sake and what's in s same as
discussion sake and what's in s same as before break 0x1 23 so that's all that's
before break 0x1 23 so that's all that's happening again on line six which is
happening again on line six which is pretty much the same as when we were
pretty much the same as when we were getting an S earlier but notice now with
getting an S earlier but notice now with line seven when I get a second variable
line seven when I get a second variable called T and I call get string again and
called T and I call get string again and by coincidence as the human I type the
by coincidence as the human I type the same thing well what happens here T gets
same thing well what happens here T gets its own chunk of memory maybe at the top
its own chunk of memory maybe at the top right uh that second version of high
right uh that second version of high gets somewhere else in memory you know
gets somewhere else in memory you know the computer could be smart and notice
the computer could be smart and notice it's the same but C doesn't generally do
it's the same but C doesn't generally do that for you it just plops it somewhere
that for you it just plops it somewhere else in memory and maybe it's at address
else in memory and maybe it's at address Ox 456 457 458 459 or wherever but you
Ox 456 457 458 459 or wherever but you can perhaps see where this is going
can perhaps see where this is going already T now of course contains the
already T now of course contains the address of that first bite and so in my
address of that first bite and so in my code on line nine when I compare s and t
code on line nine when I compare s and t for equality suffice it to say they are
for equality suffice it to say they are not equal because of the way the uh
not equal because of the way the uh strings are laid out in the computer's
strings are laid out in the computer's memory it's indeed looks the same the
memory it's indeed looks the same the same values are there but if we abstract
same values are there but if we abstract away further you can really see that s
away further you can really see that s and t are not the same themselves and so
and t are not the same themselves and so how did we fix this or really how did we
how did we fix this or really how did we avoid this last week without spilling
avoid this last week without spilling the beans and going down this Rabbit
the beans and going down this Rabbit Hole explaining like why you have to use
Hole explaining like why you have to use Stir compare well if I go back to my
Stir compare well if I go back to my code here let's do it now the right way
code here let's do it now the right way let me go ahead and include uh a line of
let me go ahead and include uh a line of code that says string compare of s comma
code that says string compare of s comma T both as inputs and then if you recall
T both as inputs and then if you recall what does stir compare return when two
what does stir compare return when two strings are equal there's three possible
strings are equal there's three possible return values so zero so one is for if
return values so zero so one is for if it comes alphabetically or atically
it comes alphabetically or atically first or second but for now I just want
first or second but for now I just want zero if I want to use S compare I do
zero if I want to use S compare I do need
need string.h so string.h does exist that's
string.h so string.h does exist that's not a cs50 thing there's no keyword
not a cs50 thing there's no keyword string as a data type that's a cs50
string as a data type that's a cs50 thing but string.h does exist so I think
thing but string.h does exist so I think now with that change on line 10 if I do
now with that change on line 10 if I do make compare and slash compare and then
make compare and slash compare and then run again uh type again High exclamation
run again uh type again High exclamation point High exclamation point I think now
point High exclamation point I think now they're the same and just as a uh second
they're the same and just as a uh second check high in L caps maybe high in
check high in L caps maybe high in lowercase those are in fact different
lowercase those are in fact different why well stir compare which was written
why well stir compare which was written by some other human decades ago is just
by some other human decades ago is just smart enough to know that it should go
smart enough to know that it should go to S and go to T start comparing them
to S and go to T start comparing them left to right stopping once it hits one
left to right stopping once it hits one or both null characters and return zero
or both null characters and return zero only if everything in s and and T are
only if everything in s and and T are exactly the same are any questions then
exactly the same are any questions then on this
on this here any questions on why we're using
here any questions on why we're using stir compare all right if no yeah oh in
stir compare all right if no yeah oh in the
middle yes so so why does why is it not the case with integers so it turns out
the case with integers so it turns out it's not the case with integers with
it's not the case with integers with floats with bulls with uh doubles with
floats with bulls with uh doubles with Longs like literally every other data
Longs like literally every other data type works correctly strings though are
type works correctly strings though are special they are useful enough in
special they are useful enough in programming and have been for decades
programming and have been for decades that the authors of printf and the
that the authors of printf and the authors of stir compare and bunches of
authors of stir compare and bunches of other functions Sterling for that matter
other functions Sterling for that matter just kind of treat strings special
just kind of treat strings special because they're just useful right we
because they're just useful right we humans interact using language be it
humans interact using language be it English or anything else and so it's
English or anything else and so it's just useful to have into the language C
just useful to have into the language C just sort of uh first class support for
just sort of uh first class support for this notion of strings of human text so
this notion of strings of human text so the short answer is just because like it
the short answer is just because like it just uh is necessar strings are
just uh is necessar strings are different they're implemented with this
different they're implemented with this address and the null character
address and the null character everything else though is just a value
everything else though is just a value but a string again is a white lie it's
but a string again is a white lie it's an address it's not a thing unto itself
an address it's not a thing unto itself good question yeah in front
oh really good question so in my code here in vs code what if I do this
here in vs code what if I do this instead of stir compare and instead of
instead of stir compare and instead of if s equals equals T what if I start
if s equals equals T what if I start playing around using star s and star T
playing around using star s and star T really interesting case to consider
really interesting case to consider let's go back to our sort of deductive
let's go back to our sort of deductive logic here so star the asteris operator
logic here so star the asteris operator today means go there so when I've typed
today means go there so when I've typed in high once and then high again both
in high once and then high again both uppercase for instance what is at the
uppercase for instance what is at the address s literally someone
address s literally someone else what is at the address s yeah uh so
else what is at the address s yeah uh so not quite at the address not so not what
not quite at the address not so not what is the address what is at the address
is the address what is at the address ox123 H and what is at the address Ox
ox123 H and what is at the address Ox 456 H also and so here you're kind of
456 H also and so here you're kind of cheating like you're comparing the first
cheating like you're comparing the first character of both strings but not every
character of both strings but not every other one now you could be really
other one now you could be really pedantic and here again this is like a
pedantic and here again this is like a good use of uh codee but you could do
good use of uh codee but you could do this if that and how about this
this if that and how about this craziness so star
craziness so star s+1 equals equals star t + one and and
s+1 equals equals star t + one and and you could do this for every character
you could do this for every character manually but that's why stir compare
manually but that's why stir compare exists it does all of this for you but
exists it does all of this for you but that's why and that's the intuition so I
that's why and that's the intuition so I would encourage you too anytime there's
would encourage you too anytime there's something kind of weird going on there's
something kind of weird going on there's I realize we might be um straining
I realize we might be um straining credibility now we haven't told you that
credibility now we haven't told you that many white lies and so most everything
many white lies and so most everything that we've seen thus far can explain
that we've seen thus far can explain pretty much all of the behavior up until
pretty much all of the behavior up until now from week one onward in C so let me
now from week one onward in C so let me revert this back to the right way if s
revert this back to the right way if s compare of S&T equals equals z this now
compare of S&T equals equals z this now is the right version of the code and now
is the right version of the code and now here is again where you can play so let
here is again where you can play so let me do this let me um clear my terminal
me do this let me um clear my terminal window just to tidy things up let me get
window just to tidy things up let me get rid of all of this comparison stuff and
rid of all of this comparison stuff and let's just see what's going on as you
let's just see what's going on as you are welcome to in your own code let's
are welcome to in your own code let's print out for instance as we might have
print out for instance as we might have in week one the value of s itself in a
in week one the value of s itself in a new line comma s and then let's just
new line comma s and then let's just print out T just to make sure it
print out T just to make sure it compiles and I'm not doing anything
compiles and I'm not doing anything wrong but this is not going to be that
wrong but this is not going to be that interesting and frankly I don't need
interesting and frankly I don't need string.h anymore because I'm not using
string.h anymore because I'm not using stir compare so make addresses addresses
stir compare so make addresses addresses there's my um oh sorry that's fun okay
there's my um oh sorry that's fun okay not percent T percent s here too ignore
not percent T percent s here too ignore that let's do this again make a oh and
that let's do this again make a oh and that's the wrong program okay dot SL
that's the wrong program okay dot SL let's do make
let's do make compare compare and let's type in high
compare compare and let's type in high again and high again and and now we just
again and high again and and now we just see the two strings I'm not comparing
see the two strings I'm not comparing but now we can kind of play around right
but now we can kind of play around right instead of printing out percent s which
instead of printing out percent s which prints the string how do I print the
prints the string how do I print the address in S I just need to make a
address in S I just need to make a slight change if I want to see not
slight change if I want to see not what's at s but I want to see s the
what's at s but I want to see s the address
yeah perfect so change percent s in both places here to percent P so now printf
places here to percent P so now printf will treat it literally as an dress it's
will treat it literally as an dress it's not going to do any fanciness this with
not going to do any fanciness this with a loop from left to right looking for
a loop from left to right looking for the null character it's just going to
the null character it's just going to print out S&T so let me clear my
print out S&T so let me clear my terminal run make compare whoops let's
terminal run make compare whoops let's do make comparecompare enter type in
do make comparecompare enter type in high type in high again and now you see
high type in high again and now you see oh so this is interesting it's not quite
oh so this is interesting it's not quite as straightforward as the other values
as straightforward as the other values which were slight one bite away they're
which were slight one bite away they're almost the same but this one ends in b0
almost the same but this one ends in b0 this one ends in F0 so they're indeed
this one ends in F0 so they're indeed separated by some number of bytes not
separated by some number of bytes not just one but a few because these strings
just one but a few because these strings are indeed longer all right so once
are indeed longer all right so once you've seen this here how can we now
you've seen this here how can we now maybe leverage this to solve other
maybe leverage this to solve other problems well let me propose that we do
problems well let me propose that we do this let me uh zoom out here let me
this let me uh zoom out here let me close compare and let me open up another
close compare and let me open up another program I wrote part of in advance
program I wrote part of in advance called
called copy.c so copy. C in theory makes a copy
copy.c so copy. C in theory makes a copy of a string how on line eight I'm using
of a string how on line eight I'm using the same thing as before get string
the same thing as before get string storing in a string or char star and
storing in a string or char star and asking the user for it then I'm not
asking the user for it then I'm not asking get string again I'm just making
asking get string again I'm just making a copy super simply with line 10 here
a copy super simply with line 10 here string T equals s now intuitively I
string T equals s now intuitively I think that's how I would copy a variable
think that's how I would copy a variable right that's how we've copied variables
right that's how we've copied variables every week thus far and see but
every week thus far and see but something's going to go wrong in line 12
something's going to go wrong in line 12 in English does someone want to explain
in English does someone want to explain what you think line 12 does don't worry
what you think line 12 does don't worry about finding any bugs or mistakes but
about finding any bugs or mistakes but what does line 12 seem to be doing using
what does line 12 seem to be doing using two upper which which is thanks to the
two upper which which is thanks to the ctype library which I've included the
ctype library which I've included the header file for
header file for yeah yeah right it's kind of like ugly
yeah yeah right it's kind of like ugly syntax but this would seem to be
syntax but this would seem to be capitalizing the first letter of T
capitalizing the first letter of T specifically and just changing it so we
specifically and just changing it so we have t bracket Zer here because we want
have t bracket Zer here because we want to save the change and we're passing to
to save the change and we're passing to two upper the first character here so
two upper the first character here so this is how we did uppercase in the past
this is how we did uppercase in the past and now I print out s and t respectively
and now I print out s and t respectively using percent s so this feels like it
using percent s so this feels like it should work I copied s and stored it in
should work I copied s and stored it in t on line 10 and then I change T and
t on line 10 and then I change T and only t on line 12 but you can perhaps if
only t on line 12 but you can perhaps if you're comfy thus far see where this is
you're comfy thus far see where this is going if I do make
going if I do make copy copy and let me type in lowercase
copy copy and let me type in lowercase Hi exclamation point this time just once
Hi exclamation point this time just once so I'm going to hit enter and watch what
so I'm going to hit enter and watch what we see for the value of s and
we see for the value of s and t huh the new value of SN T at the end
t huh the new value of SN T at the end of my program seems to be what it seems
of my program seems to be what it seems to be
to be the same high is capitalized both times
the same high is capitalized both times so what's the intuition then for this
so what's the intuition then for this why did this just
why did this just happen yeah and
happen yeah and back yeah I assigned S&T the same memory
back yeah I assigned S&T the same memory address so it did copy s into T but C
address so it did copy s into T but C takes this very literally what is s it's
takes this very literally what is s it's an address what is T it's a copy of that
an address what is T it's a copy of that address if you want to copy the whole
address if you want to copy the whole string like a normal human would expect
string like a normal human would expect hey you or someone has to do a lot more
hey you or someone has to do a lot more work you have to go to that address copy
work you have to go to that address copy this character this one this one this
this character this one this one this one and copy it to a new location in
one and copy it to a new location in memory that does not happen
memory that does not happen automatically here for you in C it does
automatically here for you in C it does in some other languages those of you
in some other languages those of you who've programmed in certain higher
who've programmed in certain higher level languages this just works as you
level languages this just works as you would hope and that's one of the
would hope and that's one of the benefits of python and other languages
benefits of python and other languages that we'll soon see but for now it
that we'll soon see but for now it literally takes at face value what this
literally takes at face value what this is copy the address into this address
is copy the address into this address and I'll make that more clear by getting
and I'll make that more clear by getting rid of the string keyword which again is
rid of the string keyword which again is just a type def this is technically an
just a type def this is technically an address here this is technically an
address here this is technically an address here so what's being being
address here so what's being being copied is the value of that address not
copied is the value of that address not all of the characters that might very
all of the characters that might very well follow it so I should make one note
well follow it so I should make one note to here I'm going to start getting more
to here I'm going to start getting more in the habit of trying to avoid
in the habit of trying to avoid segmentation faults because things could
segmentation faults because things could go wrong here for instance online 12
go wrong here for instance online 12 previously I was kind of blindly naively
previously I was kind of blindly naively dangerously assuming that there will be
dangerously assuming that there will be at least one character in s or t that
at least one character in s or t that might not be the case if the user Just
might not be the case if the user Just Hits enter there's no characters to
Hits enter there's no characters to uppercase and so this is reckless of me
uppercase and so this is reckless of me and could theoretically create a sigall
and could theoretically create a sigall so I should probably start to be smarter
so I should probably start to be smarter and say something like this if the
and say something like this if the length of T is greater than zero okay
length of T is greater than zero okay now it's safe to actually capitalize the
now it's safe to actually capitalize the first letter and that will decrease the
first letter and that will decrease the probability now of those segmentation
probability now of those segmentation faults by just not making any
faults by just not making any assumptions about what the human does
assumptions about what the human does almost always the your programs will
almost always the your programs will crash when you've made a mistake yes but
crash when you've made a mistake yes but the user gives you an input that you
the user gives you an input that you yourself did not
yourself did not expect so what does this all look like
expect so what does this all look like in memory well let's go back to the Big
in memory well let's go back to the Big Grid this time focusing on the copying
Grid this time focusing on the copying of values and let's do this here's S as
of values and let's do this here's S as in this new program just declared to be
in this new program just declared to be a Char star uh here is where my
a Char star uh here is where my lowercase High maybe ended up in the
lowercase High maybe ended up in the computer's memory that's probably at
computer's memory that's probably at ox123 12 4 125 whatever something like
ox123 12 4 125 whatever something like that and that's of course what ends up
that and that's of course what ends up in s as a value when I declare T I do
in s as a value when I declare T I do get a second variable called T just like
get a second variable called T just like before but when I copy s into T what
before but when I copy s into T what happens it's really just literally ox1 2
happens it's really just literally ox1 2 3 whatever the value of s is is now also
3 whatever the value of s is is now also the value of T and so if we abstract
the value of T and so if we abstract this away at a high level get rid of all
this away at a high level get rid of all of those extra squares this is what s
of those extra squares this is what s and t now are they're indeed copies but
and t now are they're indeed copies but copies of each other not copies of the
copies of each other not copies of the underlying characters and so if you
underlying characters and so if you follow those arrows um and try to print
follow those arrows um and try to print them both out after capitalizing one or
them both out after capitalizing one or the other you're going to unfortunately
the other you're going to unfortunately end up capitalizing not just one of them
end up capitalizing not just one of them s but both of them s and T because
s but both of them s and T because literally it's the same address any
literally it's the same address any questions then on this
questions then on this visualization
visualization yeah good question is this pass by
yeah good question is this pass by reference we haven't um we have not seen
reference we haven't um we have not seen in detail an example like that um right
in detail an example like that um right now you're copying by value but
now you're copying by value but references will come into play and
references will come into play and remind me in a bit if I haven't used
remind me in a bit if I haven't used that term yet but this is just copying
that term yet but this is just copying things by could have ended poorly value
things by could have ended poorly value other
other questions no all right so with this in
questions no all right so with this in mind like how do we actually copy things
mind like how do we actually copy things properly for this we actually need
properly for this we actually need another building block so today we give
another building block so today we give you two functions one of which is called
you two functions one of which is called Malo one of which is called free and
Malo one of which is called free and these are used all of the time by like
these are used all of the time by like every piece of software you and I use on
every piece of software you and I use on our Macs PCS and phones whether it's
our Macs PCS and phones whether it's written in C or some equivalent other
written in C or some equivalent other language Malo is for memory allocation
language Malo is for memory allocation it's a function that you can use to ask
it's a function that you can use to ask the operating system Mac OS Linux
the operating system Mac OS Linux Windows anything for some number of of
Windows anything for some number of of bytes one bite 100 bytes a gigabyte of
bytes one bite 100 bytes a gigabyte of memory you can ask malog for however
memory you can ask malog for however much memory you want in advance it will
much memory you want in advance it will return to you the address of the first
return to you the address of the first bite of memory that it found free for
bite of memory that it found free for you unlike a string it is not null
you unlike a string it is not null terminated and so the danger with malok
terminated and so the danger with malok is that it's on the honor System if you
is that it's on the honor System if you ask it for one bite or 10 bytes you the
ask it for one bite or 10 bytes you the programmer in like a variable have to
programmer in like a variable have to remember how many byes you requested one
remember how many byes you requested one or 10 or the like strings do that for
or 10 or the like strings do that for you not when we're getting now to this
you not when we're getting now to this low level Mal just going to give you
low level Mal just going to give you some memory and it's up to you to manage
some memory and it's up to you to manage it free does the opposite when you're
it free does the opposite when you're done with some chunk of memory you can
done with some chunk of memory you can free it by passing in that same address
free it by passing in that same address and just hand it back to Mac OS windows
and just hand it back to Mac OS windows or Linux and say I'm done with this you
or Linux and say I'm done with this you can let me use this for something else
can let me use this for something else later um as an aside if your computer
later um as an aside if your computer has ever like frozen or hung like the
has ever like frozen or hung like the whole thing maybe just spontaneously
whole thing maybe just spontaneously reboots yet another reason for a bug
reboots yet another reason for a bug like that might be if you write a
like that might be if you write a program with a bug that keeps Mal
program with a bug that keeps Mal loocking Mal loocking Mal loocking that
loocking Mal loocking Mal loocking that is asking for more and more and more
is asking for more and more and more memory but you make a mistake and you
memory but you make a mistake and you never free it well eventually the
never free it well eventually the computer is going to literally run out
computer is going to literally run out of memory and something's going to go
of memory and something's going to go wrong and that's often when computers
wrong and that's often when computers freeze like they're just out of memory
freeze like they're just out of memory it has the memory there but the program
it has the memory there but the program was trying to use too much of it
was trying to use too much of it endlessly so this too will be a mistake
endlessly so this too will be a mistake that some of us will surely make in the
that some of us will surely make in the coming weeks but hopefully you'll now
coming weeks but hopefully you'll now see the solution so let me go back to uh
see the solution so let me go back to uh vs code here and let me propose that we
vs code here and let me propose that we do the following I'll hide my terminal
do the following I'll hide my terminal window for a moment and I'm going to
window for a moment and I'm going to introduce another header file up here
introduce another header file up here and I promise there's not going to be
and I promise there's not going to be too many more of these but this one is
too many more of these but this one is called standard li. for standard library
called standard li. for standard library and in this file are the Declarations
and in this file are the Declarations the prototypes for malok and free and a
the prototypes for malok and free and a bunch of other stuff as well it lets me
bunch of other stuff as well it lets me now manage my own memory so let's focus
now manage my own memory so let's focus now on line 11 line 11 is where I went
now on line 11 line 11 is where I went wrong before because conceptually I want
wrong before because conceptually I want to copy the whole string but of course
to copy the whole string but of course I'm only copying modestly the individual
I'm only copying modestly the individual address so how do I copy the whole darn
address so how do I copy the whole darn thing well what I need to do is this
thing well what I need to do is this when when I declare T to be the address
when when I declare T to be the address of something in memory why don't I set T
of something in memory why don't I set T to be the address of a free chunk of
to be the address of a free chunk of memory so let me ask the operating
memory so let me ask the operating system give me this many bytes tell me
system give me this many bytes tell me what the address is and I'm going to
what the address is and I'm going to store that in t initially just so I know
store that in t initially just so I know where there's free space for me so how
where there's free space for me so how do I do that well quite simply I call
do I do that well quite simply I call malok and then I pass in the number of
malok and then I pass in the number of bytes that I need now for Hi exclamation
bytes that I need now for Hi exclamation point I think I need three although wait
point I think I need three although wait no I really need four because of the
no I really need four because of the null character but I don't think I
null character but I don't think I should be hardcoding numbers like this
should be hardcoding numbers like this cuz who knows what the human's going to
cuz who knows what the human's going to type in so I can actually use Sterling
type in so I can actually use Sterling of s and then plus one this will ask
of s and then plus one this will ask malok then for however many btes
malok then for however many btes corresponds to the number of characters
corresponds to the number of characters the human typed in plus one for again
the human typed in plus one for again the null character so it's just being
the null character so it's just being smart and defensive rather than choosing
smart and defensive rather than choosing a number myself but now all T is is a
a number myself but now all T is is a pointer if you will to some random chunk
pointer if you will to some random chunk of free space so there's nothing there
of free space so there's nothing there yet or there's you know bits there but
yet or there's you know bits there but who knows what value they are they're
who knows what value they are they're certainly not identical to what the
certainly not identical to what the human type did in I now have to do this
human type did in I now have to do this so how can I copy one string into the
so how can I copy one string into the other well let me do this instead of uh
other well let me do this instead of uh capitalizing something just yet let me
capitalizing something just yet let me do this how about four in I gets zero I
do this how about four in I gets zero I is less than the length of
is less than the length of s uh and then i++ so I'm going to
s uh and then i++ so I'm going to iterate for the whole length of the
iterate for the whole length of the string and in here I'm just going to do
string and in here I'm just going to do this the E character in t should be
this the E character in t should be identical to the E character in s so I'm
identical to the E character in s so I'm just literally copying from right to
just literally copying from right to left each and every character in s and I
left each and every character in s and I can trust that there's enough memory in
can trust that there's enough memory in t why cuz I asked for that many bytes
t why cuz I asked for that many bytes plus one now there's technically a bug
plus one now there's technically a bug here I actually should probably do this
here I actually should probably do this I should do plus one here or if you
I should do plus one here or if you prefer I should do less than or equal to
prefer I should do less than or equal to the Sterling but I think it's a little
the Sterling but I think it's a little clear to do the plus one why do I for
clear to do the plus one why do I for the first time want to go just beyond
the first time want to go just beyond the boundary of s and copy one more
the boundary of s and copy one more bite yeah yeah I need the null character
bite yeah yeah I need the null character like I could technically manually add it
like I could technically manually add it with some additional line of code but I
with some additional line of code but I might as well just copy it because back
might as well just copy it because back sl0 is back sl0 so this time and
sl0 is back sl0 so this time and probably only this time it's reasonable
probably only this time it's reasonable and correct to go just beyond the
and correct to go just beyond the boundary of your string so you copy the
boundary of your string so you copy the null terminating character so that the
null terminating character so that the computer also knows where T ends and now
computer also knows where T ends and now I think what I can do a little more
I think what I can do a little more safely is this let me go down here and
safely is this let me go down here and say t braet 0 equals 2 upper of t uh two
say t braet 0 equals 2 upper of t uh two upper of T bracket Z so same line of
upper of T bracket Z so same line of code as before if I actually want to be
code as before if I actually want to be really safe I should probably do this so
really safe I should probably do this so if the stir L of T is greater than zero
if the stir L of T is greater than zero so there's at least one bite there okay
so there's at least one bite there okay now it's safe to blindly capitalize the
now it's safe to blindly capitalize the first character and I think that now
first character and I think that now puts me in better shape so let me try
puts me in better shape so let me try this now let me uh open up my terminal
this now let me uh open up my terminal make uh copy do slash copy I'm going to
make uh copy do slash copy I'm going to type in Hi exclamation point in all
type in Hi exclamation point in all lowercase crossing my fingers this time
lowercase crossing my fingers this time and now if I zoom in it indeed
and now if I zoom in it indeed capitalized only T and not s in this
capitalized only T and not s in this case so pictorially let me switch over
case so pictorially let me switch over here here is as before the variable s
here here is as before the variable s pointing at high in all lowercase when I
pointing at high in all lowercase when I call malok though that gives me a chunk
call malok though that gives me a chunk of memory that I'm going to store the
of memory that I'm going to store the address in t of so if T is some other
address in t of so if T is some other variable as it is in my code and there's
variable as it is in my code and there's some other available chunk of memory I
some other available chunk of memory I don't know where it is but let's assume
don't know where it is but let's assume as always it's at Ox 456 4557 4558 N9 so
as always it's at Ox 456 4557 4558 N9 so four bytes total what is now happening
four bytes total what is now happening well T is defined as pointing to that
well T is defined as pointing to that because that's what Malo gives us the
because that's what Malo gives us the address of the first bite of the free
address of the first bite of the free memory and now with my for Loop I'm just
memory and now with my for Loop I'm just iterating over it copying the H then the
iterating over it copying the H then the I then the exclamation point and then
I then the exclamation point and then for good measure the back slash Zer
for good measure the back slash Zer instead questions then on this process
instead questions then on this process here a really good question um if I
here a really good question um if I omitted in my code the
omitted in my code the uh plus one and I didn't do less than or
uh plus one and I didn't do less than or equal to so that I'm copying the fourth
equal to so that I'm copying the fourth bite odds are in this program because
bite odds are in this program because it's so short you wouldn't notice that
it's so short you wouldn't notice that there's an actual error but what could
there's an actual error but what could happen is When I Call printf On T if
happen is When I Call printf On T if there's no null bite there it might
there's no null bite there it might print h i exclamation point some random
print h i exclamation point some random value some random value some random
value some random value some random value some random value until it gets
value some random value until it gets lucky and there happens to be a zero
lucky and there happens to be a zero bite a null bite by chance for instance
bite a null bite by chance for instance so if you don't include the back sl0
so if you don't include the back sl0 some way that's going to happen and I
some way that's going to happen and I say some way I could even do this I
say some way I could even do this I could technically just copy the length
could technically just copy the length of the string s and at the very bottom
of the string s and at the very bottom here I could do something like T bracket
here I could do something like T bracket I uh sorry T bracket um
I uh sorry T bracket um Sterling of T I could do this but this
Sterling of T I could do this but this is just not necessary like I could
is just not necessary like I could manually add it at the end of the string
manually add it at the end of the string but again i' claim that it's just
but again i' claim that it's just simpler to borrow that is copy the one
simpler to borrow that is copy the one that's already in s because it's the
that's already in s because it's the same thing at the end of the day good
same thing at the end of the day good question other questions on this copying
question other questions on this copying correctly
correctly [Music]
[Music] now all right is there any room for
now all right is there any room for improvement here well let me propose a
improvement here well let me propose a slight optimization this is kind of a
slight optimization this is kind of a throwback now to week one turns out that
throwback now to week one turns out that arguably my line 13 here wherein I have
arguably my line 13 here wherein I have this for Loop now that I'm doing things
this for Loop now that I'm doing things in Loops again and again and using a
in Loops again and again and using a function like Sterling this is correct
function like Sterling this is correct it will iterate from zero on up to the
it will iterate from zero on up to the length of I a length of uh s plus one
length of I a length of uh s plus one but it's kind of stupid of me to write
but it's kind of stupid of me to write this for Loop in this way why well
this for Loop in this way why well here's my initialization on the left
here's my initialization on the left here's my condition in the middle and in
here's my condition in the middle and in general calling a function inside of
general calling a function inside of your condition is probably not very good
your condition is probably not very good design like why why is it bad for me to
design like why why is it bad for me to be calling a function like Sterling in
be calling a function like Sterling in this condition in the middle of my for
this condition in the middle of my for Loop
yeah yeah you're just calling it again and again for no reason like the length
and again for no reason like the length of s never changes so like why are you
of s never changes so like why are you wasting everyone's Time by calling
wasting everyone's Time by calling Sterling of s again again again again
Sterling of s again again again again just to check this inequality whether I
just to check this inequality whether I is less than that value so it turns out
is less than that value so it turns out if you haven't discovered this already
if you haven't discovered this already there's a slight optimization we can do
there's a slight optimization we can do here that has nothing to do
here that has nothing to do fundamentally with strings or pointers
fundamentally with strings or pointers just with better design I can actually
just with better design I can actually Define two variables at once I could do
Define two variables at once I could do this let me remove this whole condition
this let me remove this whole condition and let me add a comma after I equals 0
and let me add a comma after I equals 0 set n or any variable equal to the stirl
set n or any variable equal to the stirl of s + 1 and then after the semicolon
of s + 1 and then after the semicolon just ask the question while I is less
just ask the question while I is less than n so it's almost the same but
than n so it's almost the same but notice now my condition in the very
notice now my condition in the very middle of this Loop is at least
middle of this Loop is at least comparing two static values n never
comparing two static values n never change sorry one static value n never
change sorry one static value n never changes all that changes is I but I'm
changes all that changes is I but I'm not foolishly calling sterling sterling
not foolishly calling sterling sterling sterling again and again why well how
sterling again and again why well how does Sterling work similar in spirit to
does Sterling work similar in spirit to PR print F Sterling given the name of a
PR print F Sterling given the name of a string looks at the first character and
string looks at the first character and then starts looking through the entire
then starts looking through the entire string looking for the null character
string looking for the null character and we saw this in week two counting up
and we saw this in week two counting up how many characters are there so it's
how many characters are there so it's just a waste of time again and
just a waste of time again and [Music]
again totally if you wanted to use n multiple times you could absolutely take
multiple times you could absolutely take it out of the for Loop put it right
it out of the for Loop put it right after s is defined and reuse n and again
after s is defined and reuse n and again and again absolutely but in General
and again absolutely but in General consider this when designing your for
consider this when designing your for Loops even though modern compilers like
Loops even though modern compilers like cang can actually fix this problem this
cang can actually fix this problem this inefficiency for you good practice would
inefficiency for you good practice would be don't call functions unnecessarily
be don't call functions unnecessarily especially if the answer is always going
especially if the answer is always going to be the same all right so what else
to be the same all right so what else should I perhaps refine here well how
should I perhaps refine here well how about I do one last thing and just
about I do one last thing and just comment on what exactly could go wrong
comment on what exactly could go wrong here well a couple of things well
here well a couple of things well actually this is just silly too like
actually this is just silly too like surely someone before me in the world
surely someone before me in the world has had to copy a string before surely
has had to copy a string before surely there's a function like called stir copy
there's a function like called stir copy maybe like stir compare like stir Ling
maybe like stir compare like stir Ling and indeed there is so let me propose
and indeed there is so let me propose that we actually get rid of this whole
that we actually get rid of this whole for Loop and we actually just call a
for Loop and we actually just call a function called stir copy no o just St
function called stir copy no o just St cpy and pass in the destination which is
cpy and pass in the destination which is T first and then the source that you
T first and then the source that you want to copy into the destination and
want to copy into the destination and that takes the place entirely of that
that takes the place entirely of that whole Loop so again I demonstrated the
whole Loop so again I demonstrated the loop first just to be very pedantic
loop first just to be very pedantic about it but that's wasting time you're
about it but that's wasting time you're wasting time writing lines of code you
wasting time writing lines of code you don't need to stir copy is what you can
don't need to stir copy is what you can use here instead and so this has now
use here instead and so this has now always existed and what more can I do
always existed and what more can I do well as one final point it turns out
well as one final point it turns out that there's actually things that can go
that there's actually things that can go wrong in this code even besides the
wrong in this code even besides the string being too short like if the human
string being too short like if the human Just Hits enter and there are no
Just Hits enter and there are no characters I don't want to blindly
characters I don't want to blindly capitalize the first character that
capitalize the first character that doesn't exist that's why I added that if
doesn't exist that's why I added that if condition but there's other things that
condition but there's other things that can go wrong and we introduced those to
can go wrong and we introduced those to you today it turns out that functions
you today it turns out that functions like get string and functions like Malo
like get string and functions like Malo return potentially a special value and
return potentially a special value and wonderfully confusingly it's also called
wonderfully confusingly it's also called null but with two L's all right so left
null but with two L's all right so left hand and right hand weren't talking so
hand and right hand weren't talking so well like decades ago NL is a back SL
well like decades ago NL is a back SL zero it's a single character as it
zero it's a single character as it always has been for a couple of weeks
always has been for a couple of weeks now
now n is technically a pointer it's an
n is technically a pointer it's an address but it's address zero it's like
address but it's address zero it's like the top left hand corner if you will of
the top left hand corner if you will of your computer's memory that just nothing
your computer's memory that just nothing is ever supposed to go in by convention
is ever supposed to go in by convention so null is a synonym for zero but it's
so null is a synonym for zero but it's specifically an address now why is this
specifically an address now why is this useful well suppose that in my code here
useful well suppose that in my code here something goes wrong with get string
something goes wrong with get string suppose you're being a little crazy and
suppose you're being a little crazy and you type in way too long of a string
you type in way too long of a string it's not just high but it's like an
it's not just high but it's like an entire essay of text and there's not
entire essay of text and there's not enough memory in the computer how does
enough memory in the computer how does get string signal to the programmer whoa
get string signal to the programmer whoa like that's way too big of a string I
like that's way too big of a string I can't fit it in memory well we never
can't fit it in memory well we never told you this but all of this time it
told you this but all of this time it turns out that that get string will
turns out that that get string will return this special value called null if
return this special value called null if something goes wrong so to be really
something goes wrong so to be really careful now you should do something like
careful now you should do something like this if s equals equals literally null
this if s equals equals literally null then you better um exit the program
then you better um exit the program entirely and return like one or two or
entirely and return like one or two or three to signify that something went
three to signify that something went wrong don't uh go any further similarly
wrong don't uh go any further similarly with malok it's possible if you ask for
with malok it's possible if you ask for way too much memory that could fail
way too much memory that could fail especially if you're asking now for
especially if you're asking now for double the memory after the human typed
double the memory after the human typed something in so if T equals equals null
something in so if T equals equals null then you know what but let's also return
then you know what but let's also return one or some other value to just get out
one or some other value to just get out before something crashes or freezes on
before something crashes or freezes on the human as well so honestly I tend not
the human as well so honestly I tend not to do this always in class because the
to do this always in class because the code just gets so bloated and
code just gets so bloated and complicated but you absolutely in
complicated but you absolutely in practice need to start doing this
practice need to start doing this otherwise you will be responsible for
otherwise you will be responsible for the freezes and the crashes and the
the freezes and the crashes and the reboots that users in the real world
reboots that users in the real world might actually encounter otherwise of
might actually encounter otherwise of course if we get to the bottom of this
course if we get to the bottom of this program now I should probably return
program now I should probably return zero explicitly or implicitly to just
zero explicitly or implicitly to just signify that everything is is
signify that everything is is successful but there's one other thing I
successful but there's one other thing I haven't done we introduced malok but
haven't done we introduced malok but what did I claim also
what did I claim also existed so free I'm also being a little
existed so free I'm also being a little reckless now here I am not practicing
reckless now here I am not practicing what I'm preaching I'm asking the
what I'm preaching I'm asking the computer for memory via get string I'm
computer for memory via get string I'm asking the computer for more memory my
asking the computer for more memory my via malok and I'm never technically
via malok and I'm never technically handing it back so really what I should
handing it back so really what I should be doing at the very bottom of my
be doing at the very bottom of my program to is freeing the memory I've
program to is freeing the memory I've asked for so henceforth it is a rule a
asked for so henceforth it is a rule a law if you will in see whenever you
law if you will in see whenever you allocate memory with malok or certain
allocate memory with malok or certain other functions as well you the
other functions as well you the programmer must free it when you're all
programmer must free it when you're all done with it now this is a bit of an
done with it now this is a bit of an overstatement because technically when
overstatement because technically when programs quit they'll free the memory
programs quit they'll free the memory automatically so you're not going to
automatically so you're not going to break someone's Mac or PC because you
break someone's Mac or PC because you necessarily have this bug but for
necessarily have this bug but for programs that are running all the time
programs that are running all the time like someone keeps a chrome their
like someone keeps a chrome their browser open Microsoft Word or the like
browser open Microsoft Word or the like bad things will happen if over time you
bad things will happen if over time you never never never call free and the
never never never call free and the program keeps running so always get into
program keeps running so always get into this habit here you you do not need to
this habit here you you do not need to free memory that comes from get string
free memory that comes from get string because the cs50 library automatically
because the cs50 library automatically frees it for you but you anytime you use
frees it for you but you anytime you use malok henceforth as you did or I did
malok henceforth as you did or I did here you must free that by just passing
here you must free that by just passing in the same address you got back
in the same address you got back questions now on malok and
questions now on malok and free questions
yeah oh really good question so free just so what does free do so free um
just so what does free do so free um just uh lets the computer know that you
just uh lets the computer know that you are done with that chunk of memory which
are done with that chunk of memory which means that if you have a another line of
means that if you have a another line of code elsewhere that same memory might be
code elsewhere that same memory might be reused and can be used again and again
reused and can be used again and again and that's going to be necessary
and that's going to be necessary certainly for any long running program
certainly for any long running program you can't ask for memory constantly
you can't ask for memory constantly you'll eventually run out so you need to
you'll eventually run out so you need to free it in this way other languages as
free it in this way other languages as an aside python you get another
an aside python you get another motivation in a couple of weeks for it
motivation in a couple of weeks for it is going to be Python and certain other
is going to be Python and certain other languages manage all this headache for
languages manage all this headache for you but in C the goal here is to really
you but in C the goal here is to really harness these capabilities ourselves all
harness these capabilities ourselves all right so it turns out like almost
right so it turns out like almost everyone in the room everyone in the
everyone in the room everyone in the room myself included you're going to
room myself included you're going to screw up when it comes to anything
screw up when it comes to anything memory related if you haven't already
memory related if you haven't already segals are in your future but hopefully
segals are in your future but hopefully there's tools via which you can detect
there's tools via which you can detect these things and fix them proactively
these things and fix them proactively and not just use print def uh or debug
and not just use print def uh or debug 50 or rubber duck we actually have
50 or rubber duck we actually have another tool we can equip you with now
another tool we can equip you with now that help you find some mistakes so let
that help you find some mistakes so let me do this let me close copy.c let me
me do this let me close copy.c let me open a program I wrote In Advance called
open a program I wrote In Advance called memory. C that doesn't do anything
memory. C that doesn't do anything really interesting but it's going to
really interesting but it's going to have two bugs in it notice that I've
have two bugs in it notice that I've included standard i.h as always I've
included standard i.h as always I've also included standard li. which is
also included standard li. which is necessary now for anything related to
necessary now for anything related to malok Andor free and the like line six
malok Andor free and the like line six it's a little weird what I've done here
it's a little weird what I've done here but this is like the manual way of
but this is like the manual way of asking for enough memory for an array in
asking for enough memory for an array in week two how do we ask for memory for an
week two how do we ask for memory for an array you very simply say int X3 and
array you very simply say int X3 and that gives you an array called X of size
that gives you an array called X of size three but if you do it manually Now
three but if you do it manually Now using Malo what you have to do is use
using Malo what you have to do is use syntax like this you call malok you ask
syntax like this you call malok you ask for three things times however big an
for three things times however big an INT is now we know it's four so you
INT is now we know it's four so you could literally write 12 here but this
could literally write 12 here but this is more generic so three times the size
is more generic so three times the size of an integer will give you 12
of an integer will give you 12 dynamically and what do Malo return the
dynamically and what do Malo return the address of the first bite you get back
address of the first bite you get back what do I want to put that well I want
what do I want to put that well I want to put it in a variable now the variable
to put it in a variable now the variable can't just be int X because that's a
can't just be int X because that's a number it's not an address per se if I
number it's not an address per se if I want to store this address in a variable
want to store this address in a variable I could call it X I could call it P but
I could call it X I could call it P but in Star X just means that X is now the
in Star X just means that X is now the address of a chunk of memory
address of a chunk of memory specifically a chunk of memory that's
specifically a chunk of memory that's big enough not for one but for three ins
big enough not for one but for three ins in total all right now I'm just sort of
in total all right now I'm just sort of naively putting our old friends 72 73
naively putting our old friends 72 73 and 33 at the first second and third
and 33 at the first second and third locations in memory but perhaps based on
locations in memory but perhaps based on week two or week four I'm clearly
week two or week four I'm clearly screwing up up here in a couple of ways
screwing up up here in a couple of ways someone want to identify at least one
someone want to identify at least one bug what did I do
bug what did I do wrong yeah like this is now you know
wrong yeah like this is now you know amateur uh stuff like I should be zero
amateur uh stuff like I should be zero indexing not one indexing so this has
indexing not one indexing so this has got to be 0 one2 ultimately and other
got to be 0 one2 ultimately and other bugs that are maybe more week four
bugs that are maybe more week four specific other bugs it's more subtle
specific other bugs it's more subtle yeah I'm not freeing the memory right so
yeah I'm not freeing the memory right so I'm not practicing what I'm preaching by
I'm not practicing what I'm preaching by freeing this memory now suppose these
freeing this memory now suppose these are non obvious and honestly after like
are non obvious and honestly after like an hour or two of this like this
an hour or two of this like this shouldn't be obvious yet it will be over
shouldn't be obvious yet it will be over time how could I find these bugs with uh
time how could I find these bugs with uh software as opposed to just staring at
software as opposed to just staring at the thing or asking someone for help
the thing or asking someone for help well let me propose this let me first go
well let me propose this let me first go ahead and run make memory to compile the
ahead and run make memory to compile the program and it seems to work look fine
program and it seems to work look fine there's no syntax errors at least
there's no syntax errors at least dotmemory notice seems to work fine too
dotmemory notice seems to work fine too now this program doesn't do anything
now this program doesn't do anything interesting there's no printf or
interesting there's no printf or anything like that but it didn't crash
anything like that but it didn't crash there's no segmentation fault but that
there's no segmentation fault but that doesn't mean there aren't bugs latent in
doesn't mean there aren't bugs latent in the software and this is true sadly of
the software and this is true sadly of all of today's software like Chrome and
all of today's software like Chrome and Microsoft Word and other programs surely
Microsoft Word and other programs surely have memory related bugs that people at
have memory related bugs that people at Google and Microsoft haven't yet found
Google and Microsoft haven't yet found but there are tools at least to find the
but there are tools at least to find the most obvious of those bugs and we're
most obvious of those bugs and we're going to introduce you now to a program
going to introduce you now to a program called valren so valren it's a fairly
called valren so valren it's a fairly fancy program but we'll use it for very
fancy program but we'll use it for very simple ways we'll look at your code and
simple ways we'll look at your code and find memory errors as it's executing and
find memory errors as it's executing and try to help you understand where they
try to help you understand where they are so let me go back to vs code here
are so let me go back to vs code here memory seems to be fine you know I feel
memory seems to be fine you know I feel like okay I'm going to submit this
like okay I'm going to submit this homework all is good no error messages
homework all is good no error messages that's no longer the case now you need
that's no longer the case now you need to poke a little more at your code to
to poke a little more at your code to see if maybe there's still some bug
see if maybe there's still some bug there so let me do this
there so let me do this valren and then space dotmemory so just
valren and then space dotmemory so just like debug 50 you run it on a program
like debug 50 you run it on a program you already compiled valren I'm going to
you already compiled valren I'm going to run it on a program I already compiled
run it on a program I already compiled let me uh zoom in on my terminal window
let me uh zoom in on my terminal window so we can see more at once and enter all
so we can see more at once and enter all right the output is crazy cryp IC for no
right the output is crazy cryp IC for no good reason there's lots of numbers and
good reason there's lots of numbers and equal signs it's a lot of clutter but
equal signs it's a lot of clutter but there is some juicy information here and
there is some juicy information here and let me start from the top down invalid
let me start from the top down invalid write of size four so write means to
write of size four so write means to change a value read means to like access
change a value read means to like access a value and this is again esoteric like
a value and this is again esoteric like a lot of our error messages are but it
a lot of our error messages are but it looks like uh after a block of size 12
looks like uh after a block of size 12 allock and then there's these weird hex
allock and then there's these weird hex notation there's some mention of malok
notation there's some mention of malok but honestly the juicy part here is
but honestly the juicy part here is memory. C line six that's probably my
memory. C line six that's probably my fault so let's look at line six per that
fault so let's look at line six per that output let me shrink the terminal window
output let me shrink the terminal window look at line six okay 12 is now Germain
look at line six okay 12 is now Germain right if you did the mental math of the
right if you did the mental math of the size of an INT times three 12 is somehow
size of an INT times three 12 is somehow involved here but line six is now uh
involved here but line six is now uh happening next year that's where the
happening next year that's where the memory came from what is this let me
memory came from what is this let me Zoom back in where is there invalid
Zoom back in where is there invalid right of size four like what's perhaps
right of size four like what's perhaps going wrong here invalid right of size
going wrong here invalid right of size four what does that mean it's like a
four what does that mean it's like a very technical way of explaining the bug
very technical way of explaining the bug is actually one line later on line seven
is actually one line later on line seven as we already identified
as we already identified yeah indeed and I I misspoke a moment
yeah indeed and I I misspoke a moment ago the bug actually arises here with
ago the bug actually arises here with line nine so after the allocation of
line nine so after the allocation of memory I'm somehow writing four bytes
memory I'm somehow writing four bytes incorrectly and unfortunately the onus
incorrectly and unfortunately the onus is kind of on you to sort of think
is kind of on you to sort of think through deductively like what could that
through deductively like what could that mean but I'm clearly touching four bytes
mean but I'm clearly touching four bytes of memory in these few lines of code
of memory in these few lines of code that I shouldn't be and hopefully here
that I shouldn't be and hopefully here is the light bulb already went off ear
is the light bulb already went off ear oh I'm not zero indexing okay that must
oh I'm not zero indexing okay that must mean that X bracket 3 as you note is
mean that X bracket 3 as you note is just too far past the chunk of memory so
just too far past the chunk of memory so I'm invalidly writing to four bytes that
I'm invalidly writing to four bytes that I shouldn't be so again it's not super
I shouldn't be so again it's not super obvious this is not super userfriendly
obvious this is not super userfriendly but at least it does give you a clue as
but at least it does give you a clue as to where that bug is so the fix there is
to where that bug is so the fix there is going to be quite simply to change the
going to be quite simply to change the one to a zero the two to a one and the
one to a zero the two to a one and the three to a two that'll fix that but
three to a two that'll fix that but there's still a second error and let me
there's still a second error and let me look at the cryptic output again Heap
look at the cryptic output again Heap summary some stuff there okay this does
summary some stuff there okay this does not sound good down here 12 bytes in one
not sound good down here 12 bytes in one blocks are definitely lost in Lost
blocks are definitely lost in Lost record one of one very Arcane output two
record one of one very Arcane output two but clearly related to line six again
but clearly related to line six again our allocation of memory now here too
our allocation of memory now here too it's not obvious what the solution is
it's not obvious what the solution is but memory is lost AKA this is a memory
but memory is lost AKA this is a memory leak and now the deductions kind of up
leak and now the deductions kind of up to you why what is leak oh wait I didn't
to you why what is leak oh wait I didn't call free and so the second solution
call free and so the second solution here is probably to free x at the very
here is probably to free x at the very end of the program and if you really
end of the program and if you really want to be you should probably check
want to be you should probably check like I proposed earlier if x is null
like I proposed earlier if x is null just get out now while you still can and
just get out now while you still can and don't even touch those other lines of
don't even touch those other lines of code but if you get to the bottom return
code but if you get to the bottom return zero but really the takeaways are I
zero but really the takeaways are I fixed my zero indexing of the array to
fixed my zero indexing of the array to avoid the invalid right of size four and
avoid the invalid right of size four and now I'm freeing the memory that I asked
now I'm freeing the memory that I asked for so there should be no leak lost all
for so there should be no leak lost all right let's try this again make
right let's try this again make memorymemory no visible errors yet but
memorymemory no visible errors yet but let me now increase my terminal window
let me now increase my terminal window again do valren of dotmemory crossing my
again do valren of dotmemory crossing my fingers and now all Heap blocks were
fingers and now all Heap blocks were freed no leaks are possible I don't see
freed no leaks are possible I don't see any invalid rights there's still a crazy
any invalid rights there's still a crazy amount of output but none of it is
amount of output but none of it is erroneous it's not bad now I fixed my
erroneous it's not bad now I fixed my memory bugs and so now my my ta my TF
memory bugs and so now my my ta my TF they're not going to find them either
they're not going to find them either because at least valr has proactively
because at least valr has proactively done that for me questions then on
done that for me questions then on valgren generally it's those two types
valgren generally it's those two types of Errors you might trip over there's
of Errors you might trip over there's not twoo much else in the way of Arcane
not twoo much else in the way of Arcane output questions then on
output questions then on this no all right well what else might
this no all right well what else might be going on so someone alluded to this
be going on so someone alluded to this earlier what happens when you for
earlier what happens when you for instance forget the null Terminator or
instance forget the null Terminator or you generally start poking around memory
you generally start poking around memory that you yourself didn't ask for or uh
that you yourself didn't ask for or uh looking at values you didn't put there
looking at values you didn't put there well let me go ahead and open this code
well let me go ahead and open this code of garbage. C in honor of Oscar the
of garbage. C in honor of Oscar the Grouch here of sorts and here is a
Grouch here of sorts and here is a simple program if I hide my terminal
simple program if I hide my terminal window that just does something kind of
window that just does something kind of arbitrary I first declare an array uh
arbitrary I first declare an array uh called scores uh but I made it crazy big
called scores uh but I made it crazy big like 1024 like that's a lot of uh
like 1024 like that's a lot of uh integers but so be it and then I iterate
integers but so be it and then I iterate over those integers and I print each of
over those integers and I print each of those scores out so I'm using week two
those scores out so I'm using week two syntax here but based on this program
syntax here but based on this program what have I clearly not done that I did
what have I clearly not done that I did do back in week two I've allocated the
do back in week two I've allocated the array I'm printing the array but but
array I'm printing the array but but Bute yeah I didn't initialize any values
Bute yeah I didn't initialize any values for that array back in week two we
for that array back in week two we didn't do 1024 we did like three and I
didn't do 1024 we did like three and I typed in like three test scores or
typed in like three test scores or something like that he here I'm
something like that he here I'm allocating memory even more than that
allocating memory even more than that just because I really want to be
just because I really want to be dramatic with what I'm demonstrating but
dramatic with what I'm demonstrating but I'm not initializing those values to
I'm not initializing those values to anything and so here it turns out in C
anything and so here it turns out in C generally if you do not initialize a
generally if you do not initialize a variable or you do not initialize an
variable or you do not initialize an array with explicit values there are
array with explicit values there are going to be garbage values there so to
going to be garbage values there so to speak remnants of that memory having
speak remnants of that memory having been used before probably by some other
been used before probably by some other function of yours some Library function
function of yours some Library function or something else while your program is
or something else while your program is running not a huge deal with a super
running not a huge deal with a super small program like this but for anything
small program like this but for anything sizable memory is going to be used and
sizable memory is going to be used and unused and used and unused that is
unused and used and unused that is macked and freed again and again there's
macked and freed again and again there's going to be lots of garbage values in
going to be lots of garbage values in the computer's memory by default so if I
the computer's memory by default so if I open my terminal window here uh let me
open my terminal window here uh let me do make garbage uh let me zoom in on my
do make garbage uh let me zoom in on my terminal so we can see the output when I
terminal so we can see the output when I runt garbage theoretically I should see
runt garbage theoretically I should see 1,24 integers but none of which have
1,24 integers but none of which have been initialized now I'm going to get
been initialized now I'm going to get lucky with some of them and it looks
lucky with some of them and it looks like wow okay a lot of them are
like wow okay a lot of them are initialized to zero and C does in some
initialized to zero and C does in some context initialize memory for you to
context initialize memory for you to zero at least at the beginning but not
zero at least at the beginning but not again and again typically but if I start
again and again typically but if I start scrolling backwards in time at this
scrolling backwards in time at this array of size
array of size 1024 where did these values come from so
1024 where did these values come from so just random positive and negative
just random positive and negative numbers interspersed among the zeros
numbers interspersed among the zeros well that's because I'm literally poking
well that's because I'm literally poking around a random 1,24 bytes of the
around a random 1,24 bytes of the computer's memory who knows what's there
computer's memory who knows what's there so the lesson here is that garbage
so the lesson here is that garbage values are indeed this like term of art
values are indeed this like term of art it means that a variable that you might
it means that a variable that you might have uh defined that you might have
have uh defined that you might have declared if you don't give it an
declared if you don't give it an explicit value who knows what's going to
explicit value who knows what's going to be there and the lesson here is just
be there and the lesson here is just don't do that always initialize
don't do that always initialize variables to something either yourself
variables to something either yourself or prompting the human for
or prompting the human for it questions about garbage
it questions about garbage values you'll see them sometimes if you
values you'll see them sometimes if you print things you shouldn't or touch
print things you shouldn't or touch arrays beyond their
arrays beyond their boundaries all right so maybe to make
boundaries all right so maybe to make this a little visual too it turns out
this a little visual too it turns out that a lot of things can go wrong
that a lot of things can go wrong unfortunately with poers and we've seen
unfortunately with poers and we've seen some of them and here's another program
some of them and here's another program that's a little contrived it's very
that's a little contrived it's very simple and it just is about manipulating
simple and it just is about manipulating values it doesn't do anything useful per
values it doesn't do anything useful per se except demonstrate some of today's
se except demonstrate some of today's Concepts so in main here let me propose
Concepts so in main here let me propose that we declare a pointer called X
that we declare a pointer called X that's going to store eventually the
that's going to store eventually the address of an integer apparently here's
address of an integer apparently here's another one called y that's going to
another one called y that's going to store the address of an integer as well
store the address of an integer as well here now I'm allocating enough memory to
here now I'm allocating enough memory to fit one integer now technically it's
fit one integer now technically it's four we know that but size of in just
four we know that but size of in just gives me that answer dynamically so it
gives me that answer dynamically so it will work on all systems
will work on all systems and I'm going to store the address that
and I'm going to store the address that Malo finds for me in X then I go to x
Malo finds for me in X then I go to x and put the number 42 there all right
and put the number 42 there all right why it's the sort of meaning of life the
why it's the sort of meaning of life the universe and everything here but star X
universe and everything here but star X again just means go to that address and
again just means go to that address and put a value there so why I don't know
put a value there so why I don't know but it's just uh correct at this point
but it's just uh correct at this point but what about this line here star y
but what about this line here star y equals 13 unlucky in this case what's
equals 13 unlucky in this case what's bad about this line here star y It's a
bad about this line here star y It's a combination now of today's Primitives
combination now of today's Primitives and that point here
and that point here yeah yeah we didn't ask the computer to
yeah yeah we didn't ask the computer to allocate any space so why was not
allocate any space so why was not initialized with an equal sign at any
initialized with an equal sign at any point to anything and so what is inside
point to anything and so what is inside y so to speak like a garbage value maybe
y so to speak like a garbage value maybe it's zero which isn't bad because at
it's zero which isn't bad because at least it's nice and simple but maybe
least it's nice and simple but maybe it's some crazy large uh positive number
it's some crazy large uh positive number some crazy large negative number either
some crazy large negative number either way odds are if I go to this address or
way odds are if I go to this address or that address randomly with star y bad
that address randomly with star y bad things are going to happen and so let me
things are going to happen and so let me go ahead and propose well let's not do
go ahead and propose well let's not do that let's let's actually do this
that let's let's actually do this instead assign y equal to X and we've
instead assign y equal to X and we've done that before and then I can go to Y
done that before and then I can go to Y now and change what was a 42 to a 13
now and change what was a 42 to a 13 again why this is just for educational
again why this is just for educational sake but for now this does not crash
sake but for now this does not crash because I only dreference y with star y
because I only dreference y with star y after actually giving it a value albe it
after actually giving it a value albe it a duplicate value similar to our copy
a duplicate value similar to our copy example earlier so our friends at
example earlier so our friends at Stanford have put together a wonderful
Stanford have put together a wonderful visual it's about 2 minutes long allow
visual it's about 2 minutes long allow me to dramatically dim the lights if we
me to dramatically dim the lights if we could and play with what happens with
could and play with what happens with memory when you do bad things like
memory when you do bad things like [Music]
[Music] this hey Binky wake up it's time for
this hey Binky wake up it's time for pointer fun what's that learn about
pointer fun what's that learn about pointers Oh goodie well to get started I
pointers Oh goodie well to get started I guess we're going to need a couple
guess we're going to need a couple pointers okay this code allocates two
pointers okay this code allocates two pointers which can point to integers
pointers which can point to integers okay well I see the two pointers but
okay well I see the two pointers but they don't seem to be pointing to
they don't seem to be pointing to anything that's right initially pointers
anything that's right initially pointers don't point to anything the things they
don't point to anything the things they point to are called Pointes and setting
point to are called Pointes and setting them up is a separate step oh right
them up is a separate step oh right right I knew that the Pointes are
right I knew that the Pointes are separate or so how do you allocate a
separate or so how do you allocate a point e okay well this code allocates a
point e okay well this code allocates a new integer point and this part sets X
new integer point and this part sets X to point to it hey that looks better so
to point to it hey that looks better so make it do something okay I'll
make it do something okay I'll dereference the pointer X to store the
dereference the pointer X to store the number 42 into its Point e for this
number 42 into its Point e for this trick I'll need my magic wand of
trick I'll need my magic wand of dereferencing your magic wand of D
dereferencing your magic wand of D referencing uh that that's great this is
referencing uh that that's great this is what the code looks like I'll just set
what the code looks like I'll just set up the number
up the number and hey look there it goes so doing a d
and hey look there it goes so doing a d reference on X follows the arrow to
reference on X follows the arrow to access its Point T in this case to store
access its Point T in this case to store 42 in there hey try using it to store
42 in there hey try using it to store the number 13 through the other pointer
the number 13 through the other pointer why okay I'll just go over here to Y and
why okay I'll just go over here to Y and get the number 13 set up and then take
get the number 13 set up and then take the wand of d referencing and
the wand of d referencing and just oh hey that didn't work say uh
just oh hey that didn't work say uh Binky I don't think de referencing Y is
Binky I don't think de referencing Y is a good idea cuz uh you know setting up
a good idea cuz uh you know setting up the point T is a separate step and uh I
the point T is a separate step and uh I don't think we ever did it good point
don't think we ever did it good point yeah we we allocated the pointer y but
yeah we we allocated the pointer y but we never set it to point to a point
we never set it to point to a point T very observant hey you're looking good
T very observant hey you're looking good there Binky can you fix it so that y
there Binky can you fix it so that y points to the same point as X sure I'll
points to the same point as X sure I'll use my magic wand of pointer assignment
use my magic wand of pointer assignment is that going to be a problem like
is that going to be a problem like before no this doesn't touch the Pointes
before no this doesn't touch the Pointes it just changes one pointer to point to
it just changes one pointer to point to the same thing is another oh I see now y
the same thing is another oh I see now y points to the same place as X so so wait
points to the same place as X so so wait now Y is fixed it has a point e so you
now Y is fixed it has a point e so you can try the wand of D referencing again
can try the wand of D referencing again to send the 13 over uh okay here it
to send the 13 over uh okay here it goes hey look at that now D referencing
goes hey look at that now D referencing works on why and because the pointers
works on why and because the pointers are sharing that one point e they both
are sharing that one point e they both see the 13 yeah sharing uh whatever so
see the 13 yeah sharing uh whatever so are we going to switch places now oh
are we going to switch places now oh look we're out of time but L thanks to
look we're out of time but L thanks to Professor Nick parlante of Stanford for
Professor Nick parlante of Stanford for spending a huge amount of time doing
spending a huge amount of time doing stop motion animation for that but
stop motion animation for that but hopefully now you have a sense of what
hopefully now you have a sense of what two can go wrong when you misuse memory
two can go wrong when you misuse memory in this way but at the end of the day we
in this way but at the end of the day we really only have these four new building
really only have these four new building blocks today like the star operator the
blocks today like the star operator the Ampersand operator malok and free and
Ampersand operator malok and free and really with that and the underlying
really with that and the underlying understanding of what your computer is
understanding of what your computer is doing underneath the hood we have this
doing underneath the hood we have this way now to really manipulate things in
way now to really manipulate things in memory For Better or For Worse and
memory For Better or For Worse and eventually we'll see how we can build
eventually we'll see how we can build things but we can also now use today's
things but we can also now use today's Primitives to better explain some things
Primitives to better explain some things that we've been asking you to take for
that we've been asking you to take for granted over the past several weeks so
granted over the past several weeks so for instance let me propose that we uh
for instance let me propose that we uh do one volunteer up here if we could
do one volunteer up here if we could could we get one volunteer who's you
could we get one volunteer who's you want to come straight up yep right in
want to come straight up yep right in the middle come on you'll have to take a
the middle come on you'll have to take a left right
there all right so we have two empty glasses
all right so we have two empty glasses here and two colors of liquid and we
here and two colors of liquid and we have let me give you the mic if you'd
have let me give you the mic if you'd like to say hello to the
like to say hello to the group hello um I'm Moen uh I'm in the
group hello um I'm Moen uh I'm in the and first year all right welcome well
and first year all right welcome well well welcome here I'm going to go ahead
well welcome here I'm going to go ahead and fill these two glasses with this
and fill these two glasses with this colored liquid um purple here on my
colored liquid um purple here on my right let's fill up a glass
here yeah don't drink uh and now we'll put some orange in here
put some orange in here and what we'd like you to do for the
and what we'd like you to do for the audience if you don't mind is swap the
audience if you don't mind is swap the two values we've got a purple value and
two values we've got a purple value and orange value and I'd like the purple
orange value and I'd like the purple liquid in this glass and the orange
liquid in this glass and the orange liquid in that glass
please can I have another glass oh okay good intuition but for the microphone uh
good intuition but for the microphone uh can I have another glass so you can and
can I have another glass so you can and just in fact I brought one here for you
just in fact I brought one here for you why are you asking for this though cuz
why are you asking for this though cuz if I just pour this into this and it'll
if I just pour this into this and it'll get mixed up right so obviously we need
get mixed up right so obviously we need like a temporary variable if you will so
like a temporary variable if you will so here is your temporary
here is your temporary variable and you want yeah there's
variable and you want yeah there's yeah all right so pouring the value of
yeah all right so pouring the value of the orange glass into this temporary
the orange glass into this temporary variable if you
variable if you will all right and now pouring the value
will all right and now pouring the value of the purple glass into the former
of the purple glass into the former orange
glass and now the temporary value goes back into
now the temporary value goes back into the original purple glass and now I
the original purple glass and now I think we give you a round of applause
think we give you a round of applause for having done that very well
for having done that very well okay thank you all right so so it should
okay thank you all right so so it should go without saying that in the real world
go without saying that in the real world like that's how you do this and in fact
like that's how you do this and in fact in code that's pretty much how you have
in code that's pretty much how you have to do this although ask us some time for
to do this although ask us some time for a super fancy way of doing it without a
a super fancy way of doing it without a temporary variable turns out that is
temporary variable turns out that is possible using bits but for now let's
possible using bits but for now let's suppose that indeed this demonstrates
suppose that indeed this demonstrates what is the reality in code if you want
what is the reality in code if you want to swap two values you need to have have
to swap two values you need to have have something like a temporary variable so
something like a temporary variable so for instance on the screen here is a uh
for instance on the screen here is a uh the beginning of a function called swap
the beginning of a function called swap whose purpose in life is to as you just
whose purpose in life is to as you just did swap two values call it a and b so
did swap two values call it a and b so orange and purple respectively are now
orange and purple respectively are now just a and b and integers to keep things
just a and b and integers to keep things simple well here is the corresponding
simple well here is the corresponding code if I may to what you just enacted
code if I may to what you just enacted as a human you declared a temporary
as a human you declared a temporary variable a called temp in this case
variable a called temp in this case which was like me handing you The Empty
which was like me handing you The Empty Glass and you stored the orange liquid
Glass and you stored the orange liquid in it akaa you then changed the value of
in it akaa you then changed the value of the formerly orange glass to be equal to
the formerly orange glass to be equal to the Purple by pouring one into the other
the Purple by pouring one into the other and then you did the opposite there now
and then you did the opposite there now at the end of this you still have a
at the end of this you still have a temporary variable that's now empty so
temporary variable that's now empty so it's temporary in literally that sense
it's temporary in literally that sense like you just don't need it anymore but
like you just don't need it anymore but it was necessary along the way so I dare
it was necessary along the way so I dare say this code is correct logically like
say this code is correct logically like this will swap two values A and B thanks
this will swap two values A and B thanks to the use of that temporary variable
to the use of that temporary variable unfortunately though if I actually do
unfortunately though if I actually do this in practice let me go over to VSS
this in practice let me go over to VSS code here and open a program I wrote in
code here and open a program I wrote in advance called swap. C which does this
advance called swap. C which does this as follows in here notice I have my
as follows in here notice I have my prototype for a swap function at the
prototype for a swap function at the very top and let me scroll down to the
very top and let me scroll down to the very bottom there is that exact same
very bottom there is that exact same code so I'm uh the same code for
code so I'm uh the same code for swapping two values A and B which I'm
swapping two values A and B which I'm claiming for now is correct now if I go
claiming for now is correct now if I go back up here what is main going to do
back up here what is main going to do for us main is really just meant to be a
for us main is really just meant to be a demonstration of the correctness of your
demonstration of the correctness of your algorithm so here I declare on line
algorithm so here I declare on line seven and eight two variables X and Y
seven and eight two variables X and Y being 1 and two arbitrarily respectively
being 1 and two arbitrarily respectively I then on line 10 just print out what
I then on line 10 just print out what the value of x is and Y is just so I can
the value of x is and Y is just so I can see it on the screen I then call the
see it on the screen I then call the swap function on line 11 and then I
swap function on line 11 and then I literally print the exact same thing
literally print the exact same thing again I print X and Y hopefully it'll
again I print X and Y hopefully it'll obviously be the opposite so I think
obviously be the opposite so I think logically swap is indeed correct let me
logically swap is indeed correct let me do make Swap and then do/ Swap and I
do make Swap and then do/ Swap and I should see X is one y is 2 and then
should see X is one y is 2 and then hopefully X is 2 Y is 1 enter but I
hopefully X is 2 Y is 1 enter but I don't and it did work in the sense that
don't and it did work in the sense that the code compiled the code ran so it's
the code compiled the code ran so it's not like some bug in that sense but
not like some bug in that sense but because I don't quite understand what's
because I don't quite understand what's going on underneath the hood at least as
going on underneath the hood at least as of right now or prior weeks this code
of right now or prior weeks this code here is indeed buggy in some way but
here is indeed buggy in some way but does anyone have an intuition perhaps
does anyone have an intuition perhaps based on today's discussion is to like
based on today's discussion is to like why this code while logically correct
why this code while logically correct clearly works in reality apparently does
clearly works in reality apparently does not work in
not work in C any intuition
yeah perfect and to summarize here's that term of art I prom
that term of art I prom when you call a function and pass in two
when you call a function and pass in two arguments Like A and B you're passing
arguments Like A and B you're passing those arguments by value so copies of
those arguments by value so copies of those values effectively and so when
those values effectively and so when swap is actually called here sorry when
swap is actually called here sorry when you pass in X and Y we call them A and B
you pass in X and Y we call them A and B but that's just a convention we could
but that's just a convention we could call the parameters anything we want
call the parameters anything we want what A and B are are indeed the values
what A and B are are indeed the values of X and Y respectively but copies of
of X and Y respectively but copies of the values so this code here is very
the values so this code here is very successfully in vs code 2 swapping the
successfully in vs code 2 swapping the values of a A and B but as you note
values of a A and B but as you note because I'm passing them in by value
because I'm passing them in by value literally one literally two and not by
literally one literally two and not by another term of art by reference AKA by
another term of art by reference AKA by their addresses swap has no capability
their addresses swap has no capability in C to go to those locations swap the
in C to go to those locations swap the actual locations just like we did
actual locations just like we did successfully in reality but I think we
successfully in reality but I think we really have the syntax already for
really have the syntax already for solving this if we consider that really
solving this if we consider that really this is just an issue of scope and we've
this is just an issue of scope and we've talked a bit about scope in the past
talked a bit about scope in the past whereby scope refers to the context in
whereby scope refers to the context in which a variable lives and generally
which a variable lives and generally I've claimed that a variable exists
I've claimed that a variable exists between the most recent curly braces and
between the most recent curly braces and that's pretty much true for the swap
that's pretty much true for the swap function because a and b i now claim
function because a and b i now claim again exist only in the context of these
again exist only in the context of these curly braces they have no effect on Main
curly braces they have no effect on Main up top which has different variables X
up top which has different variables X and Y but we can consider now what's
and Y but we can consider now what's really going on underneath the hood and
really going on underneath the hood and here's that same picture of memory as
here's that same picture of memory as we've seen in the past if we zoom in and
we've seen in the past if we zoom in and see on these little black chips this is
see on these little black chips this is a bunch of bites of memory if I create a
a bunch of bites of memory if I create a grid out of it just to kind of highlight
grid out of it just to kind of highlight that we can address each of these bites
that we can address each of these bites throw away the plastic circuit board and
throw away the plastic circuit board and focus only on those bites what's going
focus only on those bites what's going on underneath the hood when functions
on underneath the hood when functions are called in C which you've been doing
are called in C which you've been doing for weeks now well this rectangle of
for weeks now well this rectangle of memory if we kind of abstract it away
memory if we kind of abstract it away further is generally broken up into
further is generally broken up into different regions or segments like I
different regions or segments like I called them earlier and different things
called them earlier and different things get put in different parts of the
get put in different parts of the computer's memory and without getting
computer's memory and without getting too into the weeds when you double click
too into the weeds when you double click a program on your macro PC or when you
a program on your macro PC or when you do/ something on a Linux you are loading
do/ something on a Linux you are loading your machine code into the computer's
your machine code into the computer's memory from the computer's hard drive so
memory from the computer's hard drive so all the zeros and ones that compose
all the zeros and ones that compose Microsoft Word or Chrome or whatever are
Microsoft Word or Chrome or whatever are loaded into the computer's memory or RAM
loaded into the computer's memory or RAM and by convention it's put up top in the
and by convention it's put up top in the so-called machine code area and that's
so-called machine code area and that's how the CPU has access to them quickly
how the CPU has access to them quickly at that below that are what are be going
at that below that are what are be going to be our globals so Global variables
to be our globals so Global variables which we haven't used very much in C but
which we haven't used very much in C but you can declare them outside of main at
you can declare them outside of main at the very top of your files if you have
the very top of your files if you have globals they end up up there as well
globals they end up up there as well just FYI and then there's this big chunk
just FYI and then there's this big chunk of memory that we saw valgren mention
of memory that we saw valgren mention indirectly earlier called the Heap and
indirectly earlier called the Heap and it's kind of like Heap literally like
it's kind of like Heap literally like it's a heap of memory that you can use
it's a heap of memory that you can use as you see fit and the Heap is where
as you see fit and the Heap is where malok grabs memory from so initially
malok grabs memory from so initially there's nothing in the Heap it's just a
there's nothing in the Heap it's just a big chunk of free space anytime you call
big chunk of free space anytime you call malok Malo kind of carves out from the
malok Malo kind of carves out from the Heap area more and more bites and Malo
Heap area more and more bites and Malo keeps track of essentially which bites
keeps track of essentially which bites have already been allocated so initially
have already been allocated so initially it looks empty but different bites
it looks empty but different bites squares if you will keep getting
squares if you will keep getting requested again and again as a program
requested again and again as a program runs thanks to functions like malok and
runs thanks to functions like malok and it grows if you will conceptually down
it grows if you will conceptually down so the more and more memory you request
so the more and more memory you request from malok it starts up here but then
from malok it starts up here but then the next chunk you get is down here
the next chunk you get is down here conceptually the next chunk is down here
conceptually the next chunk is down here down here so it kind of fills the
down here so it kind of fills the available space in the computer's
available space in the computer's overall memory but there's this other
overall memory but there's this other chunk of memory called the stack and
chunk of memory called the stack and just like a stack of trays in like
just like a stack of trays in like annenburg or a cafeteria kind of grow
annenburg or a cafeteria kind of grow upward so does the stack of memory and
upward so does the stack of memory and it turns out the stack is where
it turns out the stack is where functions have variables and have
functions have variables and have arguments stored temporarily so whenever
arguments stored temporarily so whenever you call a function and it has variables
you call a function and it has variables inside of it or it has arguments there
inside of it or it has arguments there too this is the chunk of memory and the
too this is the chunk of memory and the computer's overall block of memory that
computer's overall block of memory that are used for functions but anytime you
are used for functions but anytime you call malok it's memory up here right at
call malok it's memory up here right at the end of the day they just had to pick
the end of the day they just had to pick a direction top bottom and technically
a direction top bottom and technically it's an artist rendition you could
it's an artist rendition you could Circle this thing around any orientation
Circle this thing around any orientation you want but you're just using a finite
you want but you're just using a finite amount of memory in this conventional
amount of memory in this conventional way Malo Starts Here functions start
way Malo Starts Here functions start here now you can kind of see where like
here now you can kind of see where like bad things can happen and indeed one of
bad things can happen and indeed one of the other reasons programs computers can
the other reasons programs computers can crash is if you ask for way too much
crash is if you ask for way too much memory from the Heap by calling malok
memory from the Heap by calling malok many many many times or if you call way
many many many times or if you call way too many functions or accidentally per
too many functions or accidentally per last week you recurse infinitely many
last week you recurse infinitely many times you might have a segmentation
times you might have a segmentation fault and that's because you're using
fault and that's because you're using too much stack memory so this is bound
too much stack memory so this is bound to be a problem eventually and the onus
to be a problem eventually and the onus is on the programmer to just minimize
is on the programmer to just minimize the probability of doing that and really
the probability of doing that and really avoid the possibility of doing that by
avoid the possibility of doing that by just checking return values like
just checking return values like checking if malok or get string return
checking if malok or get string return null because you can proactively with
null because you can proactively with conditionals make sure that these two
conditionals make sure that these two things do not Collide by just making
things do not Collide by just making sure that you get back non-null values
sure that you get back non-null values so let's consider the stack in the
so let's consider the stack in the context of Swap and what's really
context of Swap and what's really happening here and Carter if you
happening here and Carter if you wouldn't mind helping me animate the
wouldn't mind helping me animate the screen here when I call the main
screen here when I call the main function of any program it is allocated
function of any program it is allocated a slice of memory called a frame at the
a slice of memory called a frame at the bottom of this stack so if Carter you
bottom of this stack so if Carter you want to go ahead and uh Advance here
want to go ahead and uh Advance here here's like the First Slice of memory
here's like the First Slice of memory that will always be used by main whether
that will always be used by main whether it has commandline arguments or local
it has commandline arguments or local variables it just ends up here in memory
variables it just ends up here in memory suppose now per our swap. C program that
suppose now per our swap. C program that main calls swap well where does the
main calls swap well where does the memory for swap end up like right up
memory for swap end up like right up here so swap had two variable two
here so swap had two variable two arguments uh A and B and it also had a
arguments uh A and B and it also had a temporary variable so all of those end
temporary variable so all of those end up in here in memory and if you want to
up in here in memory and if you want to go ahead and Advance again Carter once
go ahead and Advance again Carter once swap is done executing whether it just
swap is done executing whether it just returns because there's no more lines of
returns because there's no more lines of code or you explicitly return this
code or you explicitly return this memory is just freed up automatically
memory is just freed up automatically you don't call free you don't undo malok
you don't call free you don't undo malok this just all happens automatically and
this just all happens automatically and has been since week one now technically
has been since week one now technically it's still there even though we've
it's still there even though we've removed it from the picture and there's
removed it from the picture and there's your first hint of garbage values right
your first hint of garbage values right like there's still zeros and ones there
like there's still zeros and ones there and they're left in the original the
and they're left in the original the previous configuration and so the reason
previous configuration and so the reason you get random values in the memory is
you get random values in the memory is because even though we hav't drawn swap
because even though we hav't drawn swap here there was stuff there a moment ago
here there was stuff there a moment ago it's going to be there the next time you
it's going to be there the next time you use that same memory now let's go ahead
use that same memory now let's go ahead and step through this a little more
and step through this a little more methodically main has two variables
methodically main has two variables called X and Y one and two so let's
called X and Y one and two so let's advance and represent X is one y is two
advance and represent X is one y is two taking up these two chunks of memory
taking up these two chunks of memory when we call swamp now swamp gets a new
when we call swamp now swamp gets a new slice of memory that then gives us three
slice of memory that then gives us three variables A and B technically the
variables A and B technically the arguments and temp so what happens well
arguments and temp so what happens well because functions automatically pass in
because functions automatically pass in values by value or rather pass in
values by value or rather pass in arguments by value X gets copied into a
arguments by value X gets copied into a y gets copied into B and then once we
y gets copied into B and then once we start executing the algorithm all the
start executing the algorithm all the watered glasses well what happens here
watered glasses well what happens here so if I execute the first line of code
so if I execute the first line of code temp equals a temp gets a copy of a what
temp equals a temp gets a copy of a what happens next a equals B so a takes on a
happens next a equals B so a takes on a copy of B and now we do the final swap
copy of B and now we do the final swap in the glasses b equals temp b gets a
in the glasses b equals temp b gets a copy of temp now we don't have to change
copy of temp now we don't have to change temp because it's essentially empty
temp because it's essentially empty although there's the garbage value one
although there's the garbage value one is always now going to be there until we
is always now going to be there until we reuse that memory the important thing
reuse that memory the important thing though is that a and b have been swapped
though is that a and b have been swapped but what obviously has not been swapped
but what obviously has not been swapped as is Manifest is when swap returns X
as is Manifest is when swap returns X and Y are untouched because copies
and Y are untouched because copies thereof were passed in so we need a
thereof were passed in so we need a solution to this problem and if we
solution to this problem and if we advance one more time if you don't mind
advance one more time if you don't mind let me step over here but then call you
let me step over here but then call you back in a second this code here is
back in a second this code here is logically correct this is what you did
logically correct this is what you did but this is now a detail of C you can't
but this is now a detail of C you can't just swap the things by value because
just swap the things by value because you're only changing it in the scope of
you're only changing it in the scope of the swap function function but I think
the swap function function but I think if we change it to this and add some
if we change it to this and add some annoying syntax we can solve the problem
annoying syntax we can solve the problem just like you can declare variables as
just like you can declare variables as storing addresses you can declare
storing addresses you can declare arguments to functions AKA parameters as
arguments to functions AKA parameters as taking addresses this new version of
taking addresses this new version of swap means that a shall be the address
swap means that a shall be the address of an integer B shall be the address of
of an integer B shall be the address of an integer and now it gets a little
an integer and now it gets a little cryptic here temp is the same because
cryptic here temp is the same because it's just an integer like it was in week
it's just an integer like it was in week one nothing special about temp but if
one nothing special about temp but if you want to get the value at a you do
you want to get the value at a you do star a and that goes to the address
star a and that goes to the address grabs the number one presumably if you
grabs the number one presumably if you want to change the value of a you go to
want to change the value of a you go to that address you follow the treasure map
that address you follow the treasure map to the other mailbox and you set it
to the other mailbox and you set it equal to whatever is at the value of B
equal to whatever is at the value of B you go to B as well last line you go to
you go to B as well last line you go to B now and change it to be whatever the
B now and change it to be whatever the temporary variable was which happened to
temporary variable was which happened to be the same as a so that's where the
be the same as a so that's where the final value gets swapped but here
final value gets swapped but here there's a lot more like crisscrossing
there's a lot more like crisscrossing metaphorically across the stage where
metaphorically across the stage where you're going to all of these different
you're going to all of these different addresses in the swap function to make
addresses in the swap function to make these changes so if we advance now to
these changes so if we advance now to the pictoral version of this here's the
the pictoral version of this here's the same story as before with Maine and X
same story as before with Maine and X and Y are 1 and two respectively when
and Y are 1 and two respectively when swap gets called now notice and I'll do
swap gets called now notice and I'll do it with arrows here a is effectively
it with arrows here a is effectively pointing to X B is effectively pointing
pointing to X B is effectively pointing to Y if we really get into the weeds
to Y if we really get into the weeds these are actually like addresses but
these are actually like addresses but who cares about the specifics it's
who cares about the specifics it's really just the concept here so now what
really just the concept here so now what happens in temp gets star a star a means
happens in temp gets star a star a means start at a and go there Follow the arrow
start at a and go there Follow the arrow if you will sort of shoots in ladder
if you will sort of shoots in ladder Style and then that's one so we put one
Style and then that's one so we put one and 10th all right star a equals star B
and 10th all right star a equals star B so let's do it from right to left star b
so let's do it from right to left star b means Follow the arrow it's two and then
means Follow the arrow it's two and then what do you do Follow the arrow it's now
what do you do Follow the arrow it's now two because you copy one to the other
two because you copy one to the other from right to left and then lastly star
from right to left and then lastly star b gets temp so start at B go to B and
b gets temp so start at B go to B and now store whatever the value is in temp
now store whatever the value is in temp so just by having this basic new syntax
so just by having this basic new syntax of like ampersands and stars and so
of like ampersands and stars and so forth we can actually now go to places
forth we can actually now go to places and circumvent what is otherwise a
and circumvent what is otherwise a feature of C that these variables are
feature of C that these variables are locally scoped but you can still access
locally scoped but you can still access things in other functions as well so
things in other functions as well so thank you so much for helping step
thank you so much for helping step through this so we now have a
through this so we now have a application of this that explains why
application of this that explains why now in this version of the C code this
now in this version of the C code this would actually now work so in fact let
would actually now work so in fact let me go back to my swap code here and let
me go back to my swap code here and let me change the function ever so slightly
me change the function ever so slightly in vs code so let me scroll down leaving
in vs code so let me scroll down leaving main the same and let me change swaps
main the same and let me change swaps prototype to taking in addresses let me
prototype to taking in addresses let me go to a here let me go to a here let me
go to a here let me go to a here let me go to B here and let me go to B here as
go to B here and let me go to B here as well but nothing else changes this
well but nothing else changes this change here in particular is enough of a
change here in particular is enough of a clue to see that means when you call
clue to see that means when you call Swap and pass into values I'm expecting
Swap and pass into values I'm expecting addresses now not integers but now that
addresses now not integers but now that I've made this change I do need to go up
I've made this change I do need to go up to Main and make one change does anyone
to Main and make one change does anyone have the intuition for what now need
have the intuition for what now need change in main so that I pass in X and Y
change in main so that I pass in X and Y by reference that is by address rather
by reference that is by address rather than than by value or copy oh yeah and
than than by value or copy oh yeah and [Music]
[Music] back so close so on the swap line it's
back so close so on the swap line it's not star that I want in front of the X
not star that I want in front of the X and the Y it's
instead what's the other one it's the Amper sand why because if I want to
Amper sand why because if I want to enable swap to go somewhere just like
enable swap to go somewhere just like Carter and I played this game with the
Carter and I played this game with the mailboxes I need to inform swap of the
mailboxes I need to inform swap of the address of X and the address of Y and
address of X and the address of Y and again per the beginning of today's class
again per the beginning of today's class Ampersand is the text via which we do
Ampersand is the text via which we do that so I add an ampersand here to get
that so I add an ampersand here to get the address of X Ampersand here to get
the address of X Ampersand here to get the address of Y and now this code lines
the address of Y and now this code lines up with the picture that Carter just
up with the picture that Carter just helped us walk through and so when I run
helped us walk through and so when I run make swap here I have a mistake oh what
make swap here I have a mistake oh what did I do wrong not intentional but I
did I do wrong not intentional but I guess worth pointing out I screwed up
guess worth pointing out I screwed up here it doesn't like Ampersand
here it doesn't like Ampersand X because of something on line three
X because of something on line three which is way early in the code what did
which is way early in the code what did I screw up yeah in the middle
yeah so this is why we you should not copy paste even though it's necessary
copy paste even though it's necessary for things like function prototypes if I
for things like function prototypes if I change swap at the bottom I need to
change swap at the bottom I need to change its prototype so let me add the
change its prototype so let me add the star there add the star there or just
star there add the star there or just recopy paste it at the top of the file
recopy paste it at the top of the file now let me do make swap again let me now
now let me do make swap again let me now do do/ Swap and I should now see X is
do do/ Swap and I should now see X is one y is 2 and hopefully X is 2 Y is 1
one y is 2 and hopefully X is 2 Y is 1 which I now do so the logic is the same
which I now do so the logic is the same the algorithm is the same all the weak
the algorithm is the same all the weak zero stuff is the same except now and
zero stuff is the same except now and four you just have a bit more
four you just have a bit more expressiveness via which you can tell
expressiveness via which you can tell the computer exactly what you want to
the computer exactly what you want to manipulate and how any questions then on
manipulate and how any questions then on this technique
this technique here no all right well when we fix this
here no all right well when we fix this there's still going to be problems and
there's still going to be problems and just so you've seen some terms of art
just so you've seen some terms of art here this is bad whenever you have two
here this is bad whenever you have two arrows pointing at one another certainly
arrows pointing at one another certainly if you might use and reuse more and more
if you might use and reuse more and more memory and it turns out there's some
memory and it turns out there's some terms of art that might suddenly now
terms of art that might suddenly now make sense especially if you've
make sense especially if you've programmed before bad things Can Happen
programmed before bad things Can Happen by this design but there's really only
by this design but there's really only this kind of design because it's a
this kind of design because it's a finite amount of memory so at some point
finite amount of memory so at some point bad things are going to happen no matter
bad things are going to happen no matter what if a computer runs out of memory so
what if a computer runs out of memory so it's not that this was a poor decision
it's not that this was a poor decision it's just sort of a necessary one given
it's just sort of a necessary one given finite amounts of memory in a computer
finite amounts of memory in a computer but a heap overflow so to speak is when
but a heap overflow so to speak is when you actually overflow the Heap and touch
you actually overflow the Heap and touch memory that you shouldn't up there stack
memory that you shouldn't up there stack Overflow is when you somehow overflow
Overflow is when you somehow overflow the stack and touch memory that you
the stack and touch memory that you shouldn't down there so with that said
shouldn't down there so with that said these are really just problems that can
these are really just problems that can happen and there specific incarnations
happen and there specific incarnations of what are generally called buffer
of what are generally called buffer overflows a buffer like in the YouTube
overflows a buffer like in the YouTube sense is just like a chunk of memory
sense is just like a chunk of memory that in the case of YouTube stores like
that in the case of YouTube stores like the next few seconds or minutes of video
the next few seconds or minutes of video but generally speaking a buffer is just
but generally speaking a buffer is just a chunk of memory that the computer is
a chunk of memory that the computer is using for some purpose be it the stack
using for some purpose be it the stack be it the Heap be it an array in the
be it the Heap be it an array in the computer and so buffer overflows are
computer and so buffer overflows are what happens when you just have logical
what happens when you just have logical bugs in your code but with these
bugs in your code but with these Primitives now in mind we wanted to
Primitives now in mind we wanted to conclude with a final Revelations and
conclude with a final Revelations and that's how some functions like these
that's how some functions like these here work the other thing in the cs50
here work the other thing in the cs50 library besides the type def for quote
library besides the type def for quote unquote string is of course all of these
unquote string is of course all of these functions and we give you these
functions and we give you these functions because honestly in C it is
functions because honestly in C it is hard it's annoying it's painful it's
hard it's annoying it's painful it's difficult to get user input correctly
difficult to get user input correctly it's very easy when you don't know how
it's very easy when you don't know how much how much the humans going to type
much how much the humans going to type to write buggy code when it comes to it
to write buggy code when it comes to it and indeed it's really hard to store it
and indeed it's really hard to store it correctly without accidentally having
correctly without accidentally having some kind of buffer overflow so for
some kind of buffer overflow so for instance let me show you a program here
instance let me show you a program here I'm going to go ahead and write this one
I'm going to go ahead and write this one from scratch so let me go ahead and open
from scratch so let me go ahead and open a file called get. C where I'm going to
a file called get. C where I'm going to go ahead and mimic the idea of getting
go ahead and mimic the idea of getting integers manually without the cs50
integers manually without the cs50 library so I'm going to include standard
library so I'm going to include standard io. only I'm going to Define main as not
io. only I'm going to Define main as not taking any command line arguments and
taking any command line arguments and then I'm going to do something like this
then I'm going to do something like this give me a variable x with no value yet
give me a variable x with no value yet and normally I would do something like
and normally I would do something like get int but let me take that away no
get int but let me take that away no more training wheels for get int either
more training wheels for get int either so let me just Define the int X let me
so let me just Define the int X let me then just print out something like uh a
then just print out something like uh a prompt and I'll just do x c just to make
prompt and I'll just do x c just to make it obvious to the human what we're
it obvious to the human what we're waiting for and now I'm going to use a
waiting for and now I'm going to use a builtin c function to get user input I'm
builtin c function to get user input I'm going to call a function called scan F
going to call a function called scan F which sort of scans the user's keyboard
which sort of scans the user's keyboard for input I'm going to scan it for an
for input I'm going to scan it for an integer so just like print F I'm going
integer so just like print F I'm going to use percent I because I expect an INT
to use percent I because I expect an INT and then I want to tell scanf where to
and then I want to tell scanf where to put the human's integer from the
put the human's integer from the keyboard it is not correct though to say
keyboard it is not correct though to say x because if I say x I run into the same
x because if I say x I run into the same swap problem scanf no function can
swap problem scanf no function can change the value of X unless I pass it
change the value of X unless I pass it not by value but
not by value but by reference so we're back to our
by reference so we're back to our Ampersand friend and now it has like a a
Ampersand friend and now it has like a a treasure map to the actual location of X
treasure map to the actual location of X and can therefore change it and so now
and can therefore change it and so now at the very end of this program let me
at the very end of this program let me do something simple like let's just go
do something simple like let's just go ahead and print out with print F uh the
ahead and print out with print F uh the value of x using percent I as always
value of x using percent I as always plugging in X not Ampersand X this is
plugging in X not Ampersand X this is now week one stuff I want to print the
now week one stuff I want to print the actual integer value of x so the only
actual integer value of x so the only change here is that instead of using get
change here is that instead of using get in I'm now using this new function that
in I'm now using this new function that as of today exists called scanf so let
as of today exists called scanf so let me go ahead and run get make get to
me go ahead and run get make get to create this program doget and let's go
create this program doget and let's go ahead and type in a value for x 50 enter
ahead and type in a value for x 50 enter and it just works so it turns out get in
and it just works so it turns out get in is pretty simple to implement however
is pretty simple to implement however notice what does not work if I type in
notice what does not work if I type in cat for instance cat gets converted to
cat for instance cat gets converted to zero and meanwhile get in recall will
zero and meanwhile get in recall will reprompt the user if a human not type an
reprompt the user if a human not type an actual integer you get automatically
actual integer you get automatically reprompt so that's one of the features
reprompt so that's one of the features we for cs50 added to get in just to make
we for cs50 added to get in just to make your programs more user friendly but
your programs more user friendly but otherwise get in is pretty
otherwise get in is pretty straightforward to re-implement using
straightforward to re-implement using scanf unfortunately that's not true for
scanf unfortunately that's not true for Strings because how do you know when you
Strings because how do you know when you write your code what word the human's
write your code what word the human's going to eventually type in how long
going to eventually type in how long they're greeting like high is if it's uh
they're greeting like high is if it's uh if their name is David or Carter or
if their name is David or Carter or anything else you just don't know in
anything else you just don't know in advance how much memory you need so how
advance how much memory you need so how might we do this in with string well let
might we do this in with string well let me go ahead and declare a string s
me go ahead and declare a string s although you know what there's no cs50
although you know what there's no cs50 Library so we do Char star s today
Library so we do Char star s today instead and that gives me not a string
instead and that gives me not a string per se but a pointer that will Point
per se but a pointer that will Point presumably to a string ideally I would
presumably to a string ideally I would use this get string but again we've
use this get string but again we've taken that training wheel away so now
taken that training wheel away so now that I have a pointer s suppose I prompt
that I have a pointer s suppose I prompt the human for a value for S just like
the human for a value for S just like before let me use scan F now and tell
before let me use scan F now and tell the user that I expect to read a string
the user that I expect to read a string percent s from the keyboard
percent s from the keyboard uh and store it in s now this is subtle
uh and store it in s now this is subtle I don't technically need an Amper sand
I don't technically need an Amper sand here even though I did for an INT and I
here even though I did for an INT and I would for a float and a double and a
would for a float and a double and a long and a bull and a Char why do I not
long and a bull and a Char why do I not need an Amper sand in this story to pass
need an Amper sand in this story to pass by
by reference because s is it's already an
reference because s is it's already an address again strings are just special
address again strings are just special strings now are always addresses so you
strings now are always addresses so you don't need to additionally add an Amper
don't need to additionally add an Amper sand here that's the only subtle
sand here that's the only subtle difference here but now if I go ahead
difference here but now if I go ahead and print out at the very end what the
and print out at the very end what the value of s is using percent S as before
value of s is using percent S as before this program looks like it's almost the
this program looks like it's almost the same as the int version but let's do
same as the int version but let's do make get and okay so this is not good
make get and okay so this is not good all right so it doesn't like an
all right so it doesn't like an initialized value so let me make it
initialized value so let me make it happy I said earlier to always
happy I said earlier to always initialize my variable so let's
initialize my variable so let's initialize it to null so that at least
initialize it to null so that at least something is there that's your good
something is there that's your good default value nowadays now if I do doget
default value nowadays now if I do doget now we're good and let me type in
now we're good and let me type in something like cat
something like cat okay cat is not X well let me try
okay cat is not X well let me try another word maybe it's just cat is
another word maybe it's just cat is wrong dog okay let me try David it just
wrong dog okay let me try David it just doesn't seem to be working moreover it's
doesn't seem to be working moreover it's printing it as a zero what logically
printing it as a zero what logically though is the bug here scanf worked a
though is the bug here scanf worked a moment ago for integers but it's not
moment ago for integers but it's not working for Strings and it seems to be
working for Strings and it seems to be forgetting C A it's forgetting dog it's
forgetting C A it's forgetting dog it's forgetting
forgetting David
David why what's Happening
why what's Happening Here think back to our yellow pictures
Here think back to our yellow pictures of
of memory when I
memory when I yeah it might be reading just the null
yeah it might be reading just the null itself because s is being initialized to
itself because s is being initialized to null and what step have I forgotten from
null and what step have I forgotten from just like a few minutes ago what did I
just like a few minutes ago what did I not actually request of the computer
not actually request of the computer like actual memory to store the C A the
like actual memory to store the C A the dog the d v d right there's nowhere have
dog the d v d right there's nowhere have I asked the computer for some amount of
I asked the computer for some amount of memory and so technically it might be
memory and so technically it might be reading it into some garbage location
reading it into some garbage location and that's really the problem here S is
and that's really the problem here S is initialized to null now and so in fact
initialized to null now and so in fact it is printing zero as null but I'm not
it is printing zero as null but I'm not seeing any of the other letters because
seeing any of the other letters because there was nowhere to put them C A do D
there was nowhere to put them C A do D ID because I didn't ask for three bytes
ID because I didn't ask for three bytes four bytes five bytes 100 bytes there's
four bytes five bytes 100 bytes there's no use of malok there's no use of an
no use of malok there's no use of an array there's no memory allocated for
array there's no memory allocated for anything other than the pointer itself
anything other than the pointer itself and this is where honestly life gets
and this is where honestly life gets hard with scanf like I could solve this
hard with scanf like I could solve this problem in a couple of ways let me go
problem in a couple of ways let me go ahead and do this instead of declaring s
ahead and do this instead of declaring s to be a pointer let me declare s to
to be a pointer let me declare s to actually be a an array of four chars and
actually be a an array of four chars and now let me go ahead and recompile the
now let me go ahead and recompile the code so make get doget and I'll type in
code so make get doget and I'll type in cat now that now works why well I'm
cat now that now works why well I'm allocating an explicit array of size
allocating an explicit array of size four enough for a one two three letters
four enough for a one two three letters plus a null character here's where to
plus a null character here's where to someone's question earlier it turns out
someone's question earlier it turns out then in some context you can treat
then in some context you can treat arrays as though they are Pointers
arrays as though they are Pointers themselves see we'll sort of do the
themselves see we'll sort of do the conversion for you but for now just
conversion for you but for now just assume that s is just an array of size
assume that s is just an array of size four and if you pass it into to scanf
four and if you pass it into to scanf that's like a treasure map that leads to
that's like a treasure map that leads to those four bytes so scanf can now
those four bytes so scanf can now successfully fill it with c a d o but
successfully fill it with c a d o but but let's try this again let's type in
but let's try this again let's type in David and here okay we got lucky but I
David and here okay we got lucky but I technically touched memory that I should
technically touched memory that I should not and in fact if I typed in a long
not and in fact if I typed in a long enough string and I don't think I could
enough string and I don't think I could do it very easily like by without typing
do it very easily like by without typing this thousands or hundreds of times
this thousands or hundreds of times still okay but you'll notice that it's
still okay but you'll notice that it's forgotten the rest of it now right so
forgotten the rest of it now right so somewhere we went beyond the boundary of
somewhere we went beyond the boundary of the array and we just don't have enough
the array and we just don't have enough storage space for that entire thing so
storage space for that entire thing so what do you do in your program if you
what do you do in your program if you don't know how long the person's name or
don't know how long the person's name or the the the animal name is going to be
the the the animal name is going to be what do you do 40 400 4,000 40,000 like
what do you do 40 400 4,000 40,000 like at some point you have to draw a line in
at some point you have to draw a line in the sand and that's why like getting
the sand and that's why like getting user input is so annoying in a language
user input is so annoying in a language like C and that's why get string exists
like C and that's why get string exists what we do if you're curious is we look
what we do if you're curious is we look at the user's input and we take baby
at the user's input and we take baby steps we look at it one character at a
steps we look at it one character at a time and every time we see another
time and every time we see another character we actually call malok again
character we actually call malok again and say no I need more than one bite I
and say no I need more than one bite I need two oh wait they typed in three
need two oh wait they typed in three letters I need three instead of two oh I
letters I need three instead of two oh I need four instead of two and we have
need four instead of two and we have this crazy loop essentially that keeps
this crazy loop essentially that keeps asking for more and more memory but by
asking for more and more memory but by taking baby steps and honestly if you
taking baby steps and honestly if you all had to do that in week one my God
all had to do that in week one my God like we couldn't even write hello world
like we couldn't even write hello world anymore and so that's why these training
anymore and so that's why these training wheels exist at least early on and
wheels exist at least early on and that's why in higher level languages
that's why in higher level languages like in uh python you don't have to do
like in uh python you don't have to do this at all it just works as you'd
this at all it just works as you'd expect so what more can we do well
expect so what more can we do well you'll see in problems set for this
you'll see in problems set for this coming week if I open up an example like
coming week if I open up an example like this phone book. C you'll see that you
this phone book. C you'll see that you can manipulate files now that you have a
can manipulate files now that you have a vocabulary for pointers it's going to be
vocabulary for pointers it's going to be new quickly but here we have an example
new quickly but here we have an example of how I have a program using some
of how I have a program using some familiar libraries here but as I claim
familiar libraries here but as I claim in my comment this saves names and
in my comment this saves names and numbers to a CSV file all of my examples
numbers to a CSV file all of my examples thus far I type in some words I type in
thus far I type in some words I type in some names and some phone numbers and
some names and some phone numbers and they disappear because we only store
they disappear because we only store them in memory but if you want to store
them in memory but if you want to store data in like a CSV file comma separated
data in like a CSV file comma separated values which is like a simple
values which is like a simple spreadsheet like Excel and apple numbers
spreadsheet like Excel and apple numbers and Google Sheets can open you can
and Google Sheets can open you can actually do this yourself so just as a
actually do this yourself so just as a teaser for this week here on line n I'm
teaser for this week here on line n I'm using a new data type not a cs50 thing
using a new data type not a cs50 thing this is a c thing called file but if you
this is a c thing called file but if you want to manipulate files you need to use
want to manipulate files you need to use addresses that is pointers so here is me
addresses that is pointers so here is me creating a variable called file that's
creating a variable called file that's going to point to an actual file on the
going to point to an actual file on the hard drive on the server or your Mac or
hard drive on the server or your Mac or PC fop is going to be a new function
PC fop is going to be a new function you'll use that will open open a file
you'll use that will open open a file and it will return effectively a pointer
and it will return effectively a pointer there to in memory the file name I want
there to in memory the file name I want to open is phonebook .csv and in this
to open is phonebook .csv and in this example it's going to be uh a pen mode
example it's going to be uh a pen mode it will keep allowing me to add more and
it will keep allowing me to add more and more names and numbers to this file
more names and numbers to this file here's some old get string stuff because
here's some old get string stuff because I'm not going to reinvent get string
I'm not going to reinvent get string with scan F but down here is a slightly
with scan F but down here is a slightly new function it's not print F but
new function it's not print F but fprintf and it turns out it's very easy
fprintf and it turns out it's very easy to print things not to the screen but to
to print things not to the screen but to a file with fprintf and it takes an
a file with fprintf and it takes an additional argument instead of starting
additional argument instead of starting with the quoted string you'll have to
with the quoted string you'll have to like say what file you want to write to
like say what file you want to write to and fr print F we'll figure out how to
and fr print F we'll figure out how to get the uh the bits into that file
get the uh the bits into that file passing in something like name comma
passing in something like name comma number so if I run this somewhat quickly
number so if I run this somewhat quickly here let me do this let me pre-create a
here let me do this let me pre-create a file called uh phonebook do CSV and in
file called uh phonebook do CSV and in phone book. CSV I'm going to create a
phone book. CSV I'm going to create a temporary row here name comma number
temporary row here name comma number just so that there's something in this
just so that there's something in this file and now let me go ahead and do this
file and now let me go ahead and do this and split my screen here if I have
and split my screen here if I have phonebook.com
phonebook.com and phone book. C on the left let me
and phone book. C on the left let me compile make phonebook which is the C
compile make phonebook which is the C version phonebook and now I'm prompted
version phonebook and now I'm prompted for a name and a number so I'll type in
for a name and a number so I'll type in David and then for instance + one
David and then for instance + one 949 uh what is it 468 275 o enter oh
949 uh what is it 468 275 o enter oh damn it bug uh pretend that didn't
damn it bug uh pretend that didn't happen I forgot to hit enter in the file
happen I forgot to hit enter in the file so let's do this again if I run the
so let's do this again if I run the program Again David and plus one 94 9
program Again David and plus one 94 9 468 2750 enter it's been saved now to
468 2750 enter it's been saved now to the file and if I close this file and I
the file and if I close this file and I reopen code of phonebook.com
or the like and I've actually created an actual CSV file uh if you're smiling
actual CSV file uh if you're smiling because I keep repeating my phone number
because I keep repeating my phone number out loud I would encourage you to call
out loud I would encourage you to call or text that number sometime it might
or text that number sometime it might very well well be a an Easter egg of
very well well be a an Easter egg of source but via these functions here do
source but via these functions here do we have now the ability to write files
we have now the ability to write files uh input and output and among the goals
uh input and output and among the goals then for this week as we'll see are to
then for this week as we'll see are to actually play with images in the spirit
actually play with images in the spirit of something like Instagram filters or
of something like Instagram filters or the like and we'll introduce you for
the like and we'll introduce you for instance to a file format called bmps
instance to a file format called bmps which to come full circle to the start
which to come full circle to the start of class are just maps of bits but more
of class are just maps of bits but more than just single bits for white and
than just single bits for white and black but rather colorful patterns as
black but rather colorful patterns as well and we'll give you images like this
well and we'll give you images like this of the week's Bridge here across the
of the week's Bridge here across the river at Harvard and you've run
river at Harvard and you've run after writing your own code in C and
after writing your own code in C and understanding how the data stored in the
understanding how the data stored in the computer's memory you'll be able to
computer's memory you'll be able to apply your own Instagram like filters to
apply your own Instagram like filters to make things uh grayscale instead or
make things uh grayscale instead or sepia in this case you can even flip the
sepia in this case you can even flip the bits around so that the thing is a
bits around so that the thing is a mirror image you can blur things further
mirror image you can blur things further or if you really are feeling more
or if you really are feeling more comfortable you can even write code that
comfortable you can even write code that finds the edges of the image and creates
finds the edges of the image and creates works of art like these so all that in
works of art like these so all that in more in problem set four we will see you
more in problem set four we will see you next
all right this is cs50 and this is week five which is going to be our last week
five which is going to be our last week in see uh but what that means is that
in see uh but what that means is that we'll have
we'll have okay
okay but with this week with last week and
but with this week with last week and really all of the weeks prior have you
really all of the weeks prior have you been hopefully acquiring if slowly and
been hopefully acquiring if slowly and with some challenge like some
with some challenge like some fundamental building blocks that are
fundamental building blocks that are still going to underly everything we
still going to underly everything we continue doing in the semester even as
continue doing in the semester even as we transition to so-called higher level
we transition to so-called higher level languages next week indeed we'll
languages next week indeed we'll introduce python a very popular language
introduce python a very popular language that does not have pointers does not
that does not have pointers does not have memory management at this very low
have memory management at this very low level but that's really just because
level but that's really just because someone else wrote the code that will do
someone else wrote the code that will do that for you and it's going to make our
that for you and it's going to make our lives easier because it means when you
lives easier because it means when you want to solve a problem concept up here
want to solve a problem concept up here to just get real work done or build
to just get real work done or build something amazing you don't have to
something amazing you don't have to really get into the same weeds as we
really get into the same weeds as we have been deliberately this week and now
have been deliberately this week and now last but the goal ultimately is that you
last but the goal ultimately is that you better understand and can better harness
better understand and can better harness than all that a computer can do even in
than all that a computer can do even in those higher level languages So today
those higher level languages So today we're going to focus particularly on
we're going to focus particularly on data structures how you might structure
data structures how you might structure your data in memory which really amounts
your data in memory which really amounts to building things digitally stitching
to building things digitally stitching together ideas and Concepts in memory
together ideas and Concepts in memory using this new building block from last
using this new building block from last week which of course are these pointers
week which of course are these pointers pointers allow you to store addresses in
pointers allow you to store addresses in memory like in variables but with those
memory like in variables but with those simple addresses we can sort of leave
simple addresses we can sort of leave these breadcrumbs we can point from here
these breadcrumbs we can point from here to there and we can conceptually stitch
to there and we can conceptually stitch pieces of data together but there's
pieces of data together but there's going to be different ways of doing that
going to be different ways of doing that and today we'll focus first on what's
and today we'll focus first on what's generally known as an abstract data type
generally known as an abstract data type and similar to a type in C more
and similar to a type in C more generally it does actually have some
generally it does actually have some properties in it but the underlying
properties in it but the underlying implementation details of an abstract
implementation details of an abstract data type are ultimately up to you that
data type are ultimately up to you that is to say an abstract data type can
is to say an abstract data type can represent one thing and can do something
represent one thing and can do something but how you implement it allows you some
but how you implement it allows you some discretion underneath the hood so for
discretion underneath the hood so for instance in the world of computer
instance in the world of computer science a q is actually a technical term
science a q is actually a technical term this is a type of data structure that we
this is a type of data structure that we could theoretically build in code in C
could theoretically build in code in C or really any other language but a q has
or really any other language but a q has properties just like cu's in the real
properties just like cu's in the real world for instance if you've ever lined
world for instance if you've ever lined up for something to get food in a uh and
up for something to get food in a uh and get food in a restaurant or go into a
get food in a restaurant or go into a store or wait for the airport to clear
store or wait for the airport to clear you well you've lined up in a queue Q
you well you've lined up in a queue Q being some sort of line but what's
being some sort of line but what's noteworthy about Q's are specific
noteworthy about Q's are specific properties they are first in first out
properties they are first in first out data structures either virtually or in
data structures either virtually or in the human world which is to say the
the human world which is to say the first person in the line should ideally
first person in the line should ideally be served first at the restaurant or the
be served first at the restaurant or the first person in the line should get
first person in the line should get through airport security first by
through airport security first by contrast if it weren't first in first
contrast if it weren't first in first out you can imagine how frustrated you
out you can imagine how frustrated you would be if you have this sort of
would be if you have this sort of inherent unfairness in fact if you've
inherent unfairness in fact if you've ever been in line at a store a
ever been in line at a store a supermarket or the like and all of a
supermarket or the like and all of a sudden they maybe open a new line and
sudden they maybe open a new line and now the person behind you gets to kind
now the person behind you gets to kind of cut ahead and go forward that's
of cut ahead and go forward that's because they've broken the concept of
because they've broken the concept of the cube so it has this inherent
the cube so it has this inherent potential for unfairness unless you
potential for unfairness unless you maintain this first in first out
maintain this first in first out property this would be true too for like
property this would be true too for like a to-do list just for productivity sake
a to-do list just for productivity sake if you're in the habit on paper or
if you're in the habit on paper or virtually making a to-do list ideally
virtually making a to-do list ideally you probably want to go through that
you probably want to go through that list top to bottom so that you actually
list top to bottom so that you actually get the first stuff you thought of done
get the first stuff you thought of done first rather than always focusing on
first rather than always focusing on your most recent thought now within the
your most recent thought now within the world of Q's there's generally two
world of Q's there's generally two operations two functions if you will
operations two functions if you will that any Q would have either in the real
that any Q would have either in the real world or in the virtual NQ is usually
world or in the virtual NQ is usually the technical term to mean adding
the technical term to mean adding something to a q but specifically it
something to a q but specifically it means adding it to the end of the que so
means adding it to the end of the que so that someone isn't cutting in line for
that someone isn't cutting in line for instance to go up front and then DQ is
instance to go up front and then DQ is just the opposite when it's time for the
just the opposite when it's time for the first person in line to be served the
first person in line to be served the time for the first person in line to go
time for the first person in line to go through security they are dced so to
through security they are dced so to speak so technical concept ultimately as
speak so technical concept ultimately as it's implemented in code but it's really
it's implemented in code but it's really just a real world concept and these are
just a real world concept and these are in contrast to another abstract data
in contrast to another abstract data type that we might call a stack and a
type that we might call a stack and a stack much like a stack of trays in the
stack much like a stack of trays in the cafeteria has sort of fundamentally
cafeteria has sort of fundamentally different properties you can still add
different properties you can still add and remove things from them but consider
and remove things from them but consider what happens whenever they clean all the
what happens whenever they clean all the trays in the cafeteria or the dining
trays in the cafeteria or the dining hall they put one of the trays down here
hall they put one of the trays down here and then the next one on top of it and
and then the next one on top of it and then the next one on top of it it it and
then the next one on top of it it it and so forth but of course which tray do you
so forth but of course which tray do you presume presumably take as a user of
presume presumably take as a user of that physical
that physical stack the top one presumably right like
stack the top one presumably right like you're not going to fuss down here and
you're not going to fuss down here and try to pull one out and so that would
try to pull one out and so that would seem to have an opposite property lifo
seem to have an opposite property lifo last in first out is what characterizes
last in first out is what characterizes something like a stack and that just
something like a stack and that just makes sense certainly in the physical
makes sense certainly in the physical world of stacking all of those cafeteria
world of stacking all of those cafeteria trays because it just makes more sense
trays because it just makes more sense to grab the most recently added one the
to grab the most recently added one the last added one first and at least in
last added one first and at least in that context the trays don't necessarily
that context the trays don't necessarily care what order they're using used in
care what order they're using used in but even then you could imagine that
but even then you could imagine that maybe there's some old dirty nasty tray
maybe there's some old dirty nasty tray at the very bottom that like never gets
at the very bottom that like never gets used because you're constantly
used because you're constantly replenishing the stack so there might
replenishing the stack so there might very well be side effects of these kinds
very well be side effects of these kinds of features um you might be familiar
of features um you might be familiar with using Gmail for instance or really
with using Gmail for instance or really any email program what you're looking at
any email program what you're looking at in your inbox is technically a stack at
in your inbox is technically a stack at least if you've left the defaults
least if you've left the defaults configured why you get a new email where
configured why you get a new email where does it end up not like five pages of
does it end up not like five pages of emails later presumably right at the top
emails later presumably right at the top and the next one's right at the top
and the next one's right at the top right at the the top right at the top
right at the the top right at the top and so if you're like me you're guilty
and so if you're like me you're guilty of eventually losing track of some
of eventually losing track of some emails why because you've pushed so many
emails why because you've pushed so many more emails onto the stack that you sort
more emails onto the stack that you sort of lose track of the things you got
of lose track of the things you got earliest last in first out though is
earliest last in first out though is maintained the most recent email you get
maintained the most recent email you get might very well be the first one you
might very well be the first one you reply to but that's not necessarily good
reply to but that's not necessarily good for responsiveness to everyone else out
for responsiveness to everyone else out there uh similarly if you store like all
there uh similarly if you store like all of your sweaters in a stack like this uh
of your sweaters in a stack like this uh the uh like a pile of black ones below
the uh like a pile of black ones below which is a red and then a blue stack
which is a red and then a blue stack might be perfectly fine for keeping
might be perfectly fine for keeping things organized it's sort of the sane
things organized it's sort of the sane way to do it if you just have a shelf in
way to do it if you just have a shelf in your dorm room or home but what's going
your dorm room or home but what's going to be a side effect of using a stack to
to be a side effect of using a stack to store your sweaters if they're these in
store your sweaters if they're these in this way as opposed to a queue
this way as opposed to a queue yeah it's harder to get the red and blue
yeah it's harder to get the red and blue one so presumably you're going to much
one so presumably you're going to much more often wear for instance if you will
more often wear for instance if you will black instead there now the operations
black instead there now the operations for adding things to a stack are similar
for adding things to a stack are similar in spirit but just different vocabulary
in spirit but just different vocabulary you push something on top of a stack um
you push something on top of a stack um even though it's more like in the tray
even though it's more like in the tray world you sort of place it or rest it
world you sort of place it or rest it but pushing means adding something to
but pushing means adding something to the top of the stack and popping means
the top of the stack and popping means some removing something also from the
some removing something also from the top of the stack so it's not a matter of
top of the stack so it's not a matter of enqing at the end and deqing at the
enqing at the end and deqing at the beginning with a stack everything's
beginning with a stack everything's happening on top you're pushing onto the
happening on top you're pushing onto the top and then popping off of the top now
top and then popping off of the top now when it comes to actual code how might
when it comes to actual code how might we Implement something like this well
we Implement something like this well let's just focus on really how you might
let's just focus on really how you might implement the data structure itself and
implement the data structure itself and we won't Implement any functions you
we won't Implement any functions you might implement the notion of a stack
might implement the notion of a stack like this we've seen type def before it
like this we've seen type def before it just means Define a new type of my own
just means Define a new type of my own struct means here comes a structure AKA
struct means here comes a structure AKA a data structure of one or more
a data structure of one or more variables within and let's suppose like
variables within and let's suppose like last time we've had we defined already
last time we've had we defined already like a person data type using a separate
like a person data type using a separate type Def and every person has like a
type Def and every person has like a name and a number or whatever let me
name and a number or whatever let me just stipulate that person exists
just stipulate that person exists already so here you might have to
already so here you might have to implement a stack an array called people
implement a stack an array called people of some capacity maybe it's an an array
of some capacity maybe it's an an array of size 10 or 100 or whatever this is a
of size 10 or 100 or whatever this is a constant here in this context capacity
constant here in this context capacity and every element in that array is a
and every element in that array is a person structure and I now have this to
person structure and I now have this to size now this almost seems like a
size now this almost seems like a synonym for capacity but maybe
synonym for capacity but maybe intuitively anyone want to propose why
intuitively anyone want to propose why I'm apparently maintaining separately
I'm apparently maintaining separately the capacity of the stack but also the
the capacity of the stack but also the size of the stack why these two
size of the stack why these two distinctions
distinctions here yeah
perfect the capacity is how high the stack could be like how much room is
stack could be like how much room is there for those sweatshirts in my closet
there for those sweatshirts in my closet for instance whereas size is just
for instance whereas size is just literally at this moment in time how
literally at this moment in time how many sweatshirts are in the stack it's
many sweatshirts are in the stack it's either capacity or fewer presumably in
either capacity or fewer presumably in total there so what is capacity well we
total there so what is capacity well we could implement this in you know perhaps
could implement this in you know perhaps a familiar way I might just Define a
a familiar way I might just Define a const somewhere else in my code of type
const somewhere else in my code of type int that just defines it to be capacity
int that just defines it to be capacity 50 but what perhaps is going to be the
50 but what perhaps is going to be the downside of implementing a stack in this
downside of implementing a stack in this way of how using an array inside here
way of how using an array inside here like what's a downside now of
like what's a downside now of implementing a stack using an array and
implementing a stack using an array and this size variable
this size variable within what's a caveat here
within what's a caveat here perhaps any hands
yeah okay so it's going to be harder to reach elements that aren't the last one
reach elements that aren't the last one that is the most recently added one so
that is the most recently added one so there could be some sweatshirts so to
there could be some sweatshirts so to speak way down below so we've seen that
speak way down below so we've seen that before too but at some point too a
before too but at some point too a limitation of this design is is what
limitation of this design is is what it's going to be finite right I can
it's going to be finite right I can maximally fit in this example 50
maximally fit in this example 50 sweatshirts or 50 emails or 50 cafeteria
sweatshirts or 50 emails or 50 cafeteria Trace which is fine but at least it's
Trace which is fine but at least it's indeed finite and at least in the
indeed finite and at least in the computer's memory it might be nice to
computer's memory it might be nice to use more and more and more maybe as more
use more and more and more maybe as more things are getting added to the computer
things are getting added to the computer so maybe I make this 500 or heck why
so maybe I make this 500 or heck why don't I make it 5,000 or 50,000 well
don't I make it 5,000 or 50,000 well what's the tradeoff there if I want to
what's the tradeoff there if I want to have enough room to grow seems like I
have enough room to grow seems like I should just crank up the value of
should just crank up the value of capacity endlessly but why might I not
capacity endlessly but why might I not want to change the 50 to 500 or 5,000 or
want to change the 50 to 500 or 5,000 or 50,000 what's the trade-off there
50,000 what's the trade-off there perhaps just intuitively
yeah okay you don't want to touch memory that you're not supposed to be touching
that you're not supposed to be touching and in this case it wouldn't be that
and in this case it wouldn't be that wouldn't be a risk per se unless you
wouldn't be a risk per se unless you indeed overflow the stack but there's a
indeed overflow the stack but there's a related issue in asking for that much
related issue in asking for that much memory what would another downside
be yeah okay exactly so if you've got a capacity of 5,000 but you're only using
capacity of 5,000 but you're only using one of those elements it's it's awkward
one of those elements it's it's awkward to say it non-technically which is just
to say it non-technically which is just to say very very wasteful right that's
to say very very wasteful right that's just bad design it's correct it will
just bad design it's correct it will work for up to 5,000 elements but my
work for up to 5,000 elements but my gosh you're wasting
gosh you're wasting 4,999 extra spots and that's not going
4,999 extra spots and that's not going to end well especially if you're using
to end well especially if you're using more data structures in memory like your
more data structures in memory like your Mac your PC your phone is surely going
Mac your PC your phone is surely going to run out of memory if you ask for that
to run out of memory if you ask for that much so it' be nice if there is a bit
much so it' be nice if there is a bit more dynamism there whether it's a stack
more dynamism there whether it's a stack or a CU both of which might be
or a CU both of which might be implemented a little similarly in spirit
implemented a little similarly in spirit but let's conclude this abstraction by
but let's conclude this abstraction by comparing thanks to a friend of ours
comparing thanks to a friend of ours Professor Shannon Duval of Elon
Professor Shannon Duval of Elon University who kindly put together this
University who kindly put together this graphical animation that's just under 2
graphical animation that's just under 2 minutes long that paints a picture of
minutes long that paints a picture of these two types of abstract data
these two types of abstract data structures and then we'll dive in
structures and then we'll dive in underneath to how we might Implement uh
underneath to how we might Implement uh problems like these if we could dim the
problems like these if we could dim the lights
lights [Music]
dramatically once upon a time there was a guy named Jack when it came to making
a guy named Jack when it came to making friends Jack did not have the Knack so
friends Jack did not have the Knack so Jack went to talk to the most popular
Jack went to talk to the most popular guy he knew he went up to Lou and asked
guy he knew he went up to Lou and asked what do I do Lou saw that his friend was
what do I do Lou saw that his friend was really distressed well Lou began just
really distressed well Lou began just look how you're dressed don't you have
look how you're dressed don't you have any clothes with a different look yeah
any clothes with a different look yeah yes said Jack I sure do come to my house
yes said Jack I sure do come to my house and I'll show them to you so they went
and I'll show them to you so they went off to Jacks and Jack showed Lou the box
off to Jacks and Jack showed Lou the box where he kept all his shirts and his
where he kept all his shirts and his pants and his socks L said I see you
pants and his socks L said I see you have all your clothes in a pile why
have all your clothes in a pile why don't you wear some others once in a
don't you wear some others once in a while Jack said well when I remove
while Jack said well when I remove clothes and socks I wash them and put
clothes and socks I wash them and put them away in the box then comes the next
them away in the box then comes the next morning and up I hop I go to the box and
morning and up I hop I go to the box and get my clothes off the top Lou quickly
get my clothes off the top Lou quickly realized the problem with Jack he kept
realized the problem with Jack he kept clothes CDs and books in a stack when he
clothes CDs and books in a stack when he reached for something to read or to wear
reached for something to read or to wear he chose the top book or underwear then
he chose the top book or underwear then when he was done he would put it right
when he was done he would put it right back back it would go on top of the
back back it would go on top of the stack I know the solution said a
stack I know the solution said a triumphant Lou you need to learn to
triumphant Lou you need to learn to start using a queue Lou took Jack's
start using a queue Lou took Jack's clothes and hung them in a closet and
clothes and hung them in a closet and when he had emptied the box he just
when he had emptied the box he just tossed it then he said now Jack at the
tossed it then he said now Jack at the end of the day put your clothes on the
end of the day put your clothes on the left when you put them away then
left when you put them away then tomorrow morning morning when you see
tomorrow morning morning when you see the sun shine get your clothes from the
the sun shine get your clothes from the right from the end of the line don't you
right from the end of the line don't you see said Lou it will be so nice you'll
see said Lou it will be so nice you'll wear everything once before you wear
wear everything once before you wear something twice and with everything in
something twice and with everything in cues in his closet and shelf Jack
cues in his closet and shelf Jack started to feel quite sure of himself
started to feel quite sure of himself all thanks to Lou and his wonderful
Q all right so sure so that paints a picture of these two abstract data
picture of these two abstract data structures but if we really were to dive
structures but if we really were to dive underneath the hood we could Implement
underneath the hood we could Implement them in a number of different ways but
them in a number of different ways but we really I think need some building
we really I think need some building blocks via which we could solve problems
blocks via which we could solve problems like those and we'll see today to some
like those and we'll see today to some others as well so let's rewind back to
others as well so let's rewind back to week two where we imple we introduced
week two where we imple we introduced you to your very first data structure
you to your very first data structure that is an array and an array of recall
that is an array and an array of recall was just a chunk of memory whereby
was just a chunk of memory whereby elements were stored by Design back to
elements were stored by Design back to back to back it's an array of contiguous
back to back it's an array of contiguous memory specifically so with an array we
memory specifically so with an array we could certainly store not just one thing
could certainly store not just one thing but two or three or even more and so for
but two or three or even more and so for instance if we treat uh my computer's
instance if we treat uh my computer's memory as this sort of abstraction here
memory as this sort of abstraction here and pictured here are three bytes or
and pictured here are three bytes or some uh uh multiplication thereof
some uh uh multiplication thereof suppose we're storing in the computer's
suppose we're storing in the computer's memory an array of size three storing
memory an array of size three storing the digits one two and three well
the digits one two and three well remember that if we zoom out per last
remember that if we zoom out per last week there's other stuff going on in
week there's other stuff going on in memory so even if we want to add another
memory so even if we want to add another number to this array that we didn't
number to this array that we didn't think of when we first started the
think of when we first started the program like the number four ideally we
program like the number four ideally we would put it right here next to it
would put it right here next to it otherwise it's no longer an array so by
otherwise it's no longer an array so by definition if we're using an array it's
definition if we're using an array it's got to end up right there after the
got to end up right there after the three but what else is going on inside
three but what else is going on inside of your computer's memory well assuming
of your computer's memory well assuming your program is of any length and you've
your program is of any length and you've got other variables other functions
got other variables other functions you've been running it for a while
you've been running it for a while there's a lot going on in your memory is
there's a lot going on in your memory is being used and reused so for instance
being used and reused so for instance somewhere in memory might be immediately
somewhere in memory might be immediately adjacent to this like hello comma world
adjacent to this like hello comma world back0 the null character just because
back0 the null character just because maybe you have another variable
maybe you have another variable somewhere in there that is storing that
somewhere in there that is storing that particular string alongside your
particular string alongside your existing array of size three and all of
existing array of size three and all of these Oscar the grouches here really
these Oscar the grouches here really just represent what we called last week
just represent what we called last week garbage values like there's obviously
garbage values like there's obviously bits there because they don't disappear
bits there because they don't disappear they're always going to be inside of the
they're always going to be inside of the computer somehow implemented but we
computer somehow implemented but we don't really know or care what they are
don't really know or care what they are they're the remnants of those bytes
they're the remnants of those bytes having been used for other older
having been used for other older variables previous function calls or the
variables previous function calls or the like but the problem clearly here is
like but the problem clearly here is that okay 1 two 3 is there but the H is
that okay 1 two 3 is there but the H is here and unless I want to start uh
here and unless I want to start uh taking a bite out of my string by
taking a bite out of my string by overriding the H with a four like we
overriding the H with a four like we just can't fit it right there and yet
just can't fit it right there and yet even though there's Oscars all over the
even though there's Oscars all over the place those are indeed garbage values
place those are indeed garbage values and therefore we could use that space
and therefore we could use that space because it's technically unused we just
because it's technically unused we just don't know or care what the values are
don't know or care what the values are so where could I put 1 2 3 4 well my
so where could I put 1 2 3 4 well my gosh like I have all this memory down
gosh like I have all this memory down here that's unused I could certainly
here that's unused I could certainly change those garbage values to be 1 2 3
change those garbage values to be 1 2 3 4 but to do that I might need to do a
4 but to do that I might need to do a bit of work here right it's not just a
bit of work here right it's not just a matter of just saying boom and it
matter of just saying boom and it happens now with c and with code I'd
happens now with c and with code I'd have to do this a little more
have to do this a little more methodically so let me abstract away
methodically so let me abstract away everything else that's a distraction let
everything else that's a distraction let me assume that there is indeed at least
me assume that there is indeed at least four bytes available for four numbers
four bytes available for four numbers just down here though we could have put
just down here though we could have put them in a bunch of different spots
them in a bunch of different spots what's involved now in moving the one
what's involved now in moving the one two three to this new chunk of memory so
two three to this new chunk of memory so we can add the four well I think
we can add the four well I think conceptually we're going to have to copy
conceptually we're going to have to copy the one from old to new copy the two
the one from old to new copy the two from old to new copy the three from old
from old to new copy the three from old to new and then ultimately we can get
to new and then ultimately we can get rid of the old memory those three
rid of the old memory those three original bites could now look like Oscar
original bites could now look like Oscar the Grouch and just be garbage values
the Grouch and just be garbage values for all intents and purposes but now I
for all intents and purposes but now I have room for a fourth bite wherein I
have room for a fourth bite wherein I can put the number four so this is nice
can put the number four so this is nice but what's a downside of this
but what's a downside of this approach what's a downside of solving
approach what's a downside of solving the problem in this way where the
the problem in this way where the problem at hand is just to grow the
problem at hand is just to grow the array so to speak to increase its size
array so to speak to increase its size to fit one or more
to fit one or more numbers seems pretty
numbers seems pretty straightforward but
yeah okay maybe it's out of order but I think that's okay because the order is
think that's okay because the order is just matters that it's relative so so
just matters that it's relative so so long it's it's still contiguous back to
long it's it's still contiguous back to back to back in a different chunk of
back to back in a different chunk of memory I think we're okay there it's not
memory I think we're okay there it's not like I changed it for 4 3 2 one but a
like I changed it for 4 3 2 one but a reasonable hunch
yeah yeah like I don't really plan ahead here like if I have to add another
here like if I have to add another number like five or anything else well I
number like five or anything else well I might have to jump through these hoops
might have to jump through these hoops again maybe I get lucky and maybe
again maybe I get lucky and maybe there's space there but not if I have
there's space there but not if I have other variables and other things going
other variables and other things going on that too might be used at some point
on that too might be used at some point other thoughts yeah slow efficiency slow
other thoughts yeah slow efficiency slow effic in slow efficiency why have
effic in slow efficiency why have to again yeah I mean it's just in it's
to again yeah I mean it's just in it's just inefficient it's sort of bad design
just inefficient it's sort of bad design arguably why because I had to copy all
arguably why because I had to copy all of my original work down here and as you
of my original work down here and as you note if I want to add a fifth number I'm
note if I want to add a fifth number I'm going to have to copy it again and again
going to have to copy it again and again and again and do things end times again
and again and do things end times again and again now maybe that's necessary
and again now maybe that's necessary we'll soon see for sure but it feels
we'll soon see for sure but it feels like this is not going to end well
like this is not going to end well especially if the array isn't of size
especially if the array isn't of size three or four but 300 400 your computer
three or four but 300 400 your computer ends up spending so much time just
ends up spending so much time just spinning its Wheels I mean honestly
spinning its Wheels I mean honestly better might be this like if this is my
better might be this like if this is my same array physically incarnated Now 1
same array physically incarnated Now 1 two 3 it's literally on the edge of the
two 3 it's literally on the edge of the shelf so there's no room for the number
shelf so there's no room for the number four you know maybe where we could take
four you know maybe where we could take this story is well let's just find room
this story is well let's just find room for the four like let's just put the
for the four like let's just put the four for instance over here replacing
four for instance over here replacing some available garbage value some spare
some available garbage value some spare bite over here but now wait a minute
bite over here but now wait a minute I've broken the definition of an array
I've broken the definition of an array right it's I can't have one two three
right it's I can't have one two three and then four over here so maybe there
and then four over here so maybe there maybe there could be a mechanism if I
maybe there could be a mechanism if I put this thing on again where when you
put this thing on again where when you get to the end of the existing elements
get to the end of the existing elements maybe I just somehow digitally point to
maybe I just somehow digitally point to the fourth array and maybe we can kind
the fourth array and maybe we can kind of stitch together all of these
of stitch together all of these different values in memory so that if
different values in memory so that if you follow the arrows so to speak we can
you follow the arrows so to speak we can reconstruct exactly what the order is
reconstruct exactly what the order is even without having to find or make room
even without having to find or make room here or pick up all of these numbers and
here or pick up all of these numbers and move all of them over there so that's
move all of them over there so that's perhaps the direction in which we'll go
perhaps the direction in which we'll go here so let's see how we might get to
here so let's see how we might get to that spot as follows let me go ahead and
that spot as follows let me go ahead and open up say VSS code here let me open up
open up say VSS code here let me open up a program called list. C in my terminal
a program called list. C in my terminal and let me go ahead and whip up a
and let me go ahead and whip up a relatively simple program that just
relatively simple program that just demonstrates what we did back in week
demonstrates what we did back in week two when we introduced arrays as follows
two when we introduced arrays as follows let me include uh standard io. so we can
let me include uh standard io. so we can print stuff out let me do int main void
print stuff out let me do int main void no command line Arguments for now let me
no command line Arguments for now let me give myself a an array uh called list of
give myself a an array uh called list of size three and I'll just hardcode it to
size three and I'll just hardcode it to keep it simple for lecture sake Each of
keep it simple for lecture sake Each of which is going to be an integer and now
which is going to be an integer and now just so we have some specifics to talk
just so we have some specifics to talk about let me put it list bracket zero
about let me put it list bracket zero the number one list bracket one the
the number one list bracket one the number two and list bracket two the
number two and list bracket two the number three so I'm just translating
number three so I'm just translating into codee what we just had uh
into codee what we just had uh pictorially on the screen and also
pictorially on the screen and also physically here with these numbers on
physically here with these numbers on the desk now let's just do something
the desk now let's just do something mildly useful for this how about we do
mildly useful for this how about we do four in I gets zero I is less than three
four in I gets zero I is less than three I ++ let's just print each of these
I ++ let's just print each of these numbers out just to make sure they're
numbers out just to make sure they're indeed in memory as I intended so
indeed in memory as I intended so percent I back sln comma I and then a
percent I back sln comma I and then a semicolon and I think that's it for now
semicolon and I think that's it for now so nothing interesting no problem solved
so nothing interesting no problem solved just yet just a proof of concept so that
just yet just a proof of concept so that now when I clear my terminal and run
now when I clear my terminal and run make
make list no apparent errors at the terminal
list no apparent errors at the terminal and so when I now do do/ list I should
and so when I now do do/ list I should see hopefully from left to right 1 two 3
see hopefully from left to right 1 two 3 but of course if I want to add a fourth
but of course if I want to add a fourth number now there's no mechanism for such
number now there's no mechanism for such certainly in the code that I just wrote
certainly in the code that I just wrote I could go back in here and change this
I could go back in here and change this to a four I could go down here and
to a four I could go down here and change lists bracket 3 equals 4 I could
change lists bracket 3 equals 4 I could just manually change the code recompile
just manually change the code recompile the code but of course that doesn't give
the code but of course that doesn't give me any additional run way for the fifth
me any additional run way for the fifth or sixth number so let me try to take a
or sixth number so let me try to take a different approach drawing some
different approach drawing some inspiration from last week if I want to
inspiration from last week if I want to allocate memory dynamically maybe
allocate memory dynamically maybe because I don't know when I wrote the
because I don't know when I wrote the program how many bytes I want we have
program how many bytes I want we have another function as of last week that
another function as of last week that does not require that you commit in
does not require that you commit in advance to a certain number of bytes via
advance to a certain number of bytes via what function can you just ask the
what function can you just ask the operating system for a chunk of memory
operating system for a chunk of memory okay so Malo to allocate memory now an
okay so Malo to allocate memory now an array is just a chunk of memory and even
array is just a chunk of memory and even though since week two 've been using
though since week two 've been using this syntactic sugar this convenience of
this syntactic sugar this convenience of just using square brackets and indexing
just using square brackets and indexing into it it's just making it easier to
into it it's just making it easier to manipulate a chunk of memory that's
manipulate a chunk of memory that's contiguous all together back to back to
contiguous all together back to back to back so today just like last week we can
back so today just like last week we can take those sort of training wheels off
take those sort of training wheels off and maybe be a little more deliberate in
and maybe be a little more deliberate in how we allocate memory let me go for
how we allocate memory let me go for instance and do this let me delete my
instance and do this let me delete my contents of my main function here go
contents of my main function here go back into Main and let me propose now
back into Main and let me propose now that I declare for instance how about my
that I declare for instance how about my list no longer as an array but as a
list no longer as an array but as a pointer so int star list and I'm going
pointer so int star list and I'm going to go ahead and initialize this to be
to go ahead and initialize this to be how about a chunk of three integers for
how about a chunk of three integers for now so I'm still going to hardcode it
now so I'm still going to hardcode it but I'm taking a step toward more
but I'm taking a step toward more dynamism for now so let me allocate
dynamism for now so let me allocate three times whatever the size is of an
three times whatever the size is of an INT but it's usually going to be four
INT but it's usually going to be four bytes as we know so this is really going
bytes as we know so this is really going to be 3 * 4al 12 but it's a little more
to be 3 * 4al 12 but it's a little more Dynamic and now what can I do down here
Dynamic and now what can I do down here well this is just a chunk of memory
well this is just a chunk of memory so I can do literally list bracket 0 uh
so I can do literally list bracket 0 uh equal 1 list bracket 1 = 2 list bracket
equal 1 list bracket 1 = 2 list bracket 2 equals 3 and voila achieve the exact
2 equals 3 and voila achieve the exact same effect because again an array is
same effect because again an array is just a chunk of contiguous memory but
just a chunk of contiguous memory but malok gives you any old chunk of
malok gives you any old chunk of contiguous memory so you can rather
contiguous memory so you can rather treat one like the other here now if you
treat one like the other here now if you really want to be cool you could do
really want to be cool you could do something like this instead you could
something like this instead you could dreference the address in list and go
dreference the address in list and go there you could go down here and
there you could go down here and dreference list + one and go there but
dreference list + one and go there but honestly no one really writes code like
honestly no one really writes code like this it's just too cryptic it's a little
this it's just too cryptic it's a little too far over the line at least for most
too far over the line at least for most people and so I think the syntactic
people and so I think the syntactic sugar as I keep describing it just the
sugar as I keep describing it just the more user friendly square bracket
more user friendly square bracket notation does the exact same thing
notation does the exact same thing figures out the pointer arithmetic and
figures out the pointer arithmetic and puts each of these integers in the right
puts each of these integers in the right chunks therein now just to be super
chunks therein now just to be super pedantic let me make sure if something
pedantic let me make sure if something went wrong so if list equals equals n
went wrong so if list equals equals n that means that something went wrong
that means that something went wrong like my computer is out of memory which
like my computer is out of memory which we should check for typically so let me
we should check for typically so let me just immediately return one signaling
just immediately return one signaling anything other than zero which means
anything other than zero which means success typically just to get out of
success typically just to get out of this program because something's wrong
this program because something's wrong but now let me propose that I've had a
but now let me propose that I've had a uh well let's do this for in I gets zero
uh well let's do this for in I gets zero I less than three i++ though a better
I less than three i++ though a better design would always be to use a con but
design would always be to use a con but I'm just doing this for demonstration
I'm just doing this for demonstration sake let's print out each of these ins
sake let's print out each of these ins too and just make sure I didn't mess
too and just make sure I didn't mess anything up and let me open my terminal
anything up and let me open my terminal window again let me do make list
window again let me do make list again okay huh implicitly declaring
again okay huh implicitly declaring Library function Malo with type void
Library function Malo with type void star something something implicitly de
star something something implicitly de declaring is the operative words there
declaring is the operative words there what did I mess up
what did I mess up yeah yeah I forgot the header file in
yeah yeah I forgot the header file in which malok is declared I remember now
which malok is declared I remember now okay that's in standard li. and it's
okay that's in standard li. and it's fine to look stuff like that up if you
fine to look stuff like that up if you forget so let me include standard libh
forget so let me include standard libh now let me clear my terminal run make
now let me clear my terminal run make list again okay now we're good /list and
list again okay now we're good /list and now what did I do
now what did I do [Music]
[Music] wrong oh okay not intended but teachable
wrong oh okay not intended but teachable moment what did I do wrong
moment what did I do wrong yeah yeah I'm printing the values of i
yeah yeah I'm printing the values of i instead of what is at location I in the
instead of what is at location I in the array so what I actually meant to do was
array so what I actually meant to do was print this out thank you you so now let
print this out thank you you so now let me recompile make
me recompile make listlist and now okay those are the
listlist and now okay those are the three values I was expecting not the
three values I was expecting not the indices thereof now let me propose that
indices thereof now let me propose that for the sake of discussion that I I
for the sake of discussion that I I regret having only allocated space for
regret having only allocated space for three integers and maybe I really should
three integers and maybe I really should have allocated enough space for four now
have allocated enough space for four now this is not how you would do this in
this is not how you would do this in practice because presumably if you have
practice because presumably if you have a change of thought just go back in and
a change of thought just go back in and correct the code but let me propose that
correct the code but let me propose that somewhere in here is a more complicated
somewhere in here is a more complicated program and time passes dot dot dot
program and time passes dot dot dot there's a lot of other interesting code
there's a lot of other interesting code there but at some point I might want to
there but at some point I might want to give myself more memory so how can I do
give myself more memory so how can I do this well let me just ask the operating
this well let me just ask the operating system now for four new bytes of memory
system now for four new bytes of memory so that we can at least in version one
so that we can at least in version one implement the ID on the board where I
implement the ID on the board where I just copied the three bytes into the new
just copied the three bytes into the new four bytes and then added a fourth value
four bytes and then added a fourth value so I'm going to use malok again and I'm
so I'm going to use malok again and I'm going to say here's a new pointer I'll
going to say here's a new pointer I'll call it temp TMP for short which is
call it temp TMP for short which is quite common when you just need it
quite common when you just need it briefly I'm going to then call malok
briefly I'm going to then call malok again I'm going to say give me four
again I'm going to say give me four integers using size of let me again make
integers using size of let me again make sure so if temp equals equals null
sure so if temp equals equals null something went wrong so let me just
something went wrong so let me just immediately return one and for good
immediately return one and for good measure before I return one one let me
measure before I return one one let me free the original list so that I don't
free the original list so that I don't leak memory so I'm not just immediately
leak memory so I'm not just immediately returning one I'm being a good citizen
returning one I'm being a good citizen and remembering well if this Malo call
and remembering well if this Malo call did succeed and indeed I got as far as
did succeed and indeed I got as far as line 18 but then line 18 failed I should
line 18 but then line 18 failed I should free the memory that I previously macked
free the memory that I previously macked so again that's the rule of thumb if you
so again that's the rule of thumb if you allocate it you should be the one to
allocate it you should be the one to free it even before you're about to quit
free it even before you're about to quit now once I've done that I think I need
now once I've done that I think I need to do what we did pictorially on the
to do what we did pictorially on the screen where I need to copy the One the
screen where I need to copy the One the two the three from the old array into
two the three from the old array into the new so how might I do this well let
the new so how might I do this well let me give myself a loop so for in I gets
me give myself a loop so for in I gets zero I less than three i++ because the
zero I less than three i++ because the size of the original is still the same
size of the original is still the same let me go ahead and treat the new chunk
let me go ahead and treat the new chunk of memory called 10 temp as an array
of memory called 10 temp as an array itself and so I can absolutely use these
itself and so I can absolutely use these square brackets just like before it's
square brackets just like before it's just a chunk of memory I'm treating it
just a chunk of memory I'm treating it like an array and let me add to that
like an array and let me add to that value whatever is at the original list
value whatever is at the original list at location I as well so this again is
at location I as well so this again is just this exercise of copying from right
just this exercise of copying from right uh from old to new step by step the one
uh from old to new step by step the one the two and the three but I still need
the two and the three but I still need one additional step if my goal at hand
one additional step if my goal at hand now is to have ultimately a fourth value
now is to have ultimately a fourth value here well I'm just going to hard coose
here well I'm just going to hard coose this for demonstration sake and I'm
this for demonstration sake and I'm going to go to the very last location of
going to go to the very last location of temp which is of size four which means
temp which is of size four which means the last element in temp is temp
the last element in temp is temp bracket three because it's zero indexed
bracket three because it's zero indexed but there's four total spaces there and
but there's four total spaces there and I'm just going to arbitrarily for the
I'm just going to arbitrarily for the sake of discussion put the number four
sake of discussion put the number four there and that is what happened when we
there and that is what happened when we proposed uh changing the final garbage
proposed uh changing the final garbage value there to that four but now I need
value there to that four but now I need to do what the slide did for us sort of
to do what the slide did for us sort of magically on the screen I should now do
magically on the screen I should now do a couple of final things I should free
a couple of final things I should free the original list which I've not done
the original list which I've not done yet CU I only called free earlier in
yet CU I only called free earlier in cases of error and that was just to be
cases of error and that was just to be safe I can now free the list and now if
safe I can now free the list and now if I want to inform the computer that I
I want to inform the computer that I want list quote unquote my variable
want list quote unquote my variable called list to point at not the old
called list to point at not the old chunk like it originally did but the new
chunk like it originally did but the new chunk I think I can just do this list
chunk I think I can just do this list equals
equals TMP and again that's just saying that if
TMP and again that's just saying that if list is a pointer which it was cuz look
list is a pointer which it was cuz look at the very top line here on line eight
at the very top line here on line eight on line six I declared list to be a
on line six I declared list to be a pointer uh to a chunk of memory temp
pointer uh to a chunk of memory temp meanwhile is a separate pointer to a
meanwhile is a separate pointer to a chunk of memory so down here this line
chunk of memory so down here this line 33 is just a matter of my saying okay
33 is just a matter of my saying okay now henceforth because I've already
now henceforth because I've already freed the old chunk of memory my list
freed the old chunk of memory my list variable should Point not at this chunk
variable should Point not at this chunk of three bytes but this chunk of four
of three bytes but this chunk of four bytes or really 12 in total now uh or
bytes or really 12 in total now uh or rather 16 now because we have four such
rather 16 now because we have four such bytes questions now on this code the
bytes questions now on this code the point of which was quite simply to
point of which was quite simply to demonstrate how we could Implement and
demonstrate how we could Implement and code this idea of Fairly correctly but
code this idea of Fairly correctly but inefficiently allocating a new array of
inefficiently allocating a new array of sufficient size and then populating it
sufficient size and then populating it with a new fourth
with a new fourth value questions on what we've just done
value questions on what we've just done here no
here no yeah
[Music] good question at this point in the story
good question at this point in the story with line 33 do I not have two different
with line 33 do I not have two different variables pointing at the same chunk of
variables pointing at the same chunk of memory short answer yes but here's where
memory short answer yes but here's where the semantics are perhaps compelling
the semantics are perhaps compelling list is the variable that I intend to
list is the variable that I intend to use longer term and keep around in
use longer term and keep around in memory and again assume that there's
memory and again assume that there's even more code going on here that we
even more code going on here that we just didn't write yet so it's useful to
just didn't write yet so it's useful to have that that variable temp was just
have that that variable temp was just kind of a necessary evil because up here
kind of a necessary evil because up here it would not have been correct to do
it would not have been correct to do this it would not have been correct to
this it would not have been correct to say list on line 18 equals the new chunk
say list on line 18 equals the new chunk of memory because this would have
of memory because this would have represented a memory leak if I pre uh
represented a memory leak if I pre uh prematurely change temp to point not at
prematurely change temp to point not at the old chunk but the new chunk at that
the old chunk but the new chunk at that point no one's pointing at the old chunk
point no one's pointing at the old chunk and so I've lost those three bites vren
and so I've lost those three bites vren for instance would yell at you for
for instance would yell at you for having lost as many btes in memory so in
having lost as many btes in memory so in this case here I do leave this as temp
this case here I do leave this as temp yes it's duplicative at this point but
yes it's duplicative at this point but it's it's not a huge deal if it was just
it's it's not a huge deal if it was just meant semantically to be a temporary
meant semantically to be a temporary value but down here at the risk of one
value but down here at the risk of one more line of code I still want to to be
more line of code I still want to to be a good citizen free list and maybe just
a good citizen free list and maybe just for good measure return zero explicitly
for good measure return zero explicitly but notice it's not doing it twice per
but notice it's not doing it twice per se on line 31 what am I freeing the
se on line 31 what am I freeing the original address of list the three
original address of list the three integer version then I change what list
integer version then I change what list points at so it's pointing at a
points at so it's pointing at a completely different chunk of memory
completely different chunk of memory this one of size four So eventually when
this one of size four So eventually when I'm all done using this memory for this
I'm all done using this memory for this demonstration I still need to free list
demonstration I still need to free list but at this point in the story line 40
but at this point in the story line 40 it's pointing at the new chunk of memory
it's pointing at the new chunk of memory which I similarly need to hand back to
which I similarly need to hand back to the operating system by free
yeah when would temp equal null so let me scroll back up slightly this is being
me scroll back up slightly this is being a good citizen and a good programmer
a good citizen and a good programmer whenever it comes to using malok malok
whenever it comes to using malok malok can return null if the computer's out of
can return null if the computer's out of memory so this is maybe a much bigger
memory so this is maybe a much bigger program you've got other things going on
program you've got other things going on in it and so you just don't have enough
in it and so you just don't have enough memory available to be handed malok
memory available to be handed malok needs to signal to you that there's some
needs to signal to you that there's some error and so it will by convention per
error and so it will by convention per the documentation per the manual pages
the documentation per the manual pages return null so this is just me being a
return null so this is just me being a good citizen otherwise here's another
good citizen otherwise here's another error that might cause your program to
error that might cause your program to crash with a segmentation fault if you
crash with a segmentation fault if you get back null but you assume that it's
get back null but you assume that it's good memory uh going to address zero AKA
good memory uh going to address zero AKA null will crash your program
null will crash your program intentionally
intentionally [Music]
yeah correct if I were to change my final line 40 here to be free temp this
final line 40 here to be free temp this would also work as well and here this is
would also work as well and here this is really a matter of design it's a very
really a matter of design it's a very nitpicky thing we could probably debate
nitpicky thing we could probably debate it but because at this point in the
it but because at this point in the story my main variable for remembering
story my main variable for remembering where the list is is called list this is
where the list is is called list this is sort of the more responsible way to do
sort of the more responsible way to do it freeing the list just so that my
it freeing the list just so that my colleagues my ta doesn't sort of wonder
colleagues my ta doesn't sort of wonder why are you freeing temporary ver memory
why are you freeing temporary ver memory that you already freed like it just is a
that you already freed like it just is a semantic thing at this point but good
semantic thing at this point but good Instinct it would also work correct
Instinct it would also work correct maybe just not good design all right so
maybe just not good design all right so it turns out that this gets annoying
it turns out that this gets annoying quickly as it did in the picture of
quickly as it did in the picture of doing all of this duplication and even
doing all of this duplication and even though technically it's necessary to
though technically it's necessary to copy those values if you need a newer
copy those values if you need a newer bigger chunk of memory
bigger chunk of memory there is at least a function in C that
there is at least a function in C that simplifies a lot of this for us and in
simplifies a lot of this for us and in fact let me go ahead and do this instead
fact let me go ahead and do this instead of using malok this second time on line
of using malok this second time on line 18 in addition to the first time I used
18 in addition to the first time I used it on line six I'm actually going to try
it on line six I'm actually going to try and introduce another function called
and introduce another function called realloc which as the name suggests tries
realloc which as the name suggests tries to reallocate memory for you and it
to reallocate memory for you and it works a little differently from malok
works a little differently from malok realloc expects two arguments the first
realloc expects two arguments the first one is what is the chunk of memory that
one is what is the chunk of memory that you want to try to grow or Shrink that
you want to try to grow or Shrink that is reallocate to be a different size and
is reallocate to be a different size and then you specify what size you would
then you specify what size you would want and indeed in this case I want four
want and indeed in this case I want four times size of int and that will now give
times size of int and that will now give me hopefully a new address of a chunk of
me hopefully a new address of a chunk of memory that's big enough to fit all four
memory that's big enough to fit all four numbers but what's wonderful about
numbers but what's wonderful about realloc is that it will handle all of
realloc is that it will handle all of the copying for me so in fact I'm going
the copying for me so in fact I'm going to go down here I'm going to get rid of
to go down here I'm going to get rid of all of this this extra for Loop and what
all of this this extra for Loop and what I'm simply going to do instead is this
I'm simply going to do instead is this once I can
once I can trust after lines 18 through 23 that
trust after lines 18 through 23 that rioc worked and it didn't return null
rioc worked and it didn't return null because I'm out of memory I can just say
because I'm out of memory I can just say okay just immediately remember that the
okay just immediately remember that the new list points at this new chunk of
new list points at this new chunk of memory instead and then I can still now
memory instead and then I can still now do this line but I can tweak the
do this line but I can tweak the semantics here and just say list bracket
semantics here and just say list bracket 3 the new Final a in uh um the new list
3 the new Final a in uh um the new list is for I don't need to free this here I
is for I don't need to free this here I don't need to do this all I need now at
don't need to do this all I need now at the bottom is the final for Loop to just
the bottom is the final for Loop to just print out these values so in short even
print out these values so in short even though that was somewhat quick using
though that was somewhat quick using realloc just moves the entire copying
realloc just moves the entire copying process that I implemented myself a
process that I implemented myself a moment ago using a for Loop it just
moment ago using a for Loop it just moves it to realloc and lets it deal
moves it to realloc and lets it deal with the copying for me it's no more
with the copying for me it's no more efficient but at least means uh I'm
efficient but at least means uh I'm writing less code which is more pleasant
writing less code which is more pleasant and hopefully the people who wrote aloc
and hopefully the people who wrote aloc or realloc are smarter than me and they
or realloc are smarter than me and they just will introduce bugs with lower
just will introduce bugs with lower probability
probability too all right that was a lot any
too all right that was a lot any [Music]
questions good question why do you still need to make list equal temp as I did on
need to make list equal temp as I did on line 24 so ideally I would do this
line 24 so ideally I would do this ideally I would just change this line 18
ideally I would just change this line 18 to be list that is to say call re or
to be list that is to say call re or actually even better ideally I would
actually even better ideally I would just say realloc this list to be of this
just say realloc this list to be of this new size but again things can go wrong
new size but again things can go wrong when allocating memory you need to check
when allocating memory you need to check a return value to see if it was
a return value to see if it was successful or not and so we need to use
successful or not and so we need to use a return value okay so let's not
a return value okay so let's not introduce temp let's just use list but
introduce temp let's just use list but here's where a memory leak might happen
here's where a memory leak might happen in the off chance realloc fails and
in the off chance realloc fails and doesn't have enough memory for your four
doesn't have enough memory for your four bytes therefore it returns by definition
bytes therefore it returns by definition null you can't overwrite the original
null you can't overwrite the original value of list with null
value of list with null to then check it why because now who
to then check it why because now who remembers where the original Three byes
remembers where the original Three byes were if you prematurely change the value
were if you prematurely change the value of list you've lost you've uh leaked
of list you've lost you've uh leaked memory in that sense and so that's why
memory in that sense and so that's why let me undo this change I declare a
let me undo this change I declare a temporary pointer for the sole purpose
temporary pointer for the sole purpose of making sure I can check the return
of making sure I can check the return value and then once it's good now I'll
value and then once it's good now I'll update the value of list so it's sort of
update the value of list so it's sort of doing a Switcheroo by making sure first
doing a Switcheroo by making sure first that you have a new value to swap with
that you have a new value to swap with the old other question
the old other question on this code
yeah indeed realloc automatically frees the previous memory for you and better
the previous memory for you and better yet it's even smarter than that if you
yet it's even smarter than that if you get lucky and they happens to be space
get lucky and they happens to be space right after your existing chunk of
right after your existing chunk of memory so one two three garbage value
memory so one two three garbage value instead of one two three hello world
instead of one two three hello world realloc won't even bother copying things
realloc won't even bother copying things from old to new it will just say okay
from old to new it will just say okay I'm going to now reserve for you more
I'm going to now reserve for you more bites than you originally asked for so
bites than you originally asked for so it doesn't have to waste time doing that
it doesn't have to waste time doing that copying and so in that sense this
copying and so in that sense this version is now not only still correct
version is now not only still correct it's even better designed because we're
it's even better designed because we're not wasting time with that for Loop we
not wasting time with that for Loop we might have to resort to it if there is
might have to resort to it if there is in fact hello world or something else in
in fact hello world or something else in the way but hopefully we'll get lucky
the way but hopefully we'll get lucky and save those steps other questions on
and save those steps other questions on this this manipulation of code
this this manipulation of code here yeah in the
middle what if you want to resize a
what if you want to resize a two-dimensional array so very similar in
two-dimensional array so very similar in Spirit uh whereby you can use the same
Spirit uh whereby you can use the same trickery let me wave my hand at that for
trickery let me wave my hand at that for now just because I think that's going to
now just because I think that's going to sort of significantly increase the
sort of significantly increase the complexity but very same Primitives
complexity but very same Primitives ultimately a two-dimensional array is
ultimately a two-dimensional array is essentially just a doubly long or
essentially just a doubly long or quadratically longer list of memory that
quadratically longer list of memory that using square bracket notation is doing
using square bracket notation is doing some of that mental math for you but
some of that mental math for you but it's fundamentally no different of
it's fundamentally no different of what's going on underneath the
what's going on underneath the hood all right so with that said and
hood all right so with that said and that code under our belt even though
that code under our belt even though that's not going to be something you'll
that's not going to be something you'll frequently need to code yourself let's
frequently need to code yourself let's propose now how we might go about
propose now how we might go about building some actual data structures
building some actual data structures ourselves the new ingredient here being
ourselves the new ingredient here being this reality that if you want to resize
this reality that if you want to resize a chunk of memory so is to make room for
a chunk of memory so is to make room for things we now have that ability memory
things we now have that ability memory addresses and pointers just give us the
addresses and pointers just give us the ability to like Point around at things
ability to like Point around at things and move things around in memory but now
and move things around in memory but now that we have malok and even realloc you
that we have malok and even realloc you can imagine maybe rewinding and you
can imagine maybe rewinding and you could Implement that stack that Q using
could Implement that stack that Q using not an array per se because you have to
not an array per se because you have to commit to an array size in advance but
commit to an array size in advance but if you implement your stack or your que
if you implement your stack or your que using a pointer and then malok and
using a pointer and then malok and realloc and maybe someone else writes
realloc and maybe someone else writes all that code for you perhaps now you
all that code for you perhaps now you can imagine that okay now the stack can
can imagine that okay now the stack can grow or Shrink by using realloc
grow or Shrink by using realloc accordingly you don't have to preact pre
accordingly you don't have to preact pre um preemptively say give me five bytes
um preemptively say give me five bytes or 50 or 500 or 5,000 you can say just
or 50 or 500 or 5,000 you can say just give me one initially and if I need more
give me one initially and if I need more I'll realloc realloc reloc and if you
I'll realloc realloc reloc and if you keep popping things off the stack you
keep popping things off the stack you can realloc in the other direction and
can realloc in the other direction and ask for fewer and fewer bytes and the
ask for fewer and fewer bytes and the operating system can take that memory
operating system can take that memory back as well so we now have this
back as well so we now have this building block let's see what we can do
building block let's see what we can do with it so we've had a few pieces of
with it so we've had a few pieces of syntax in recent weeks all of which
syntax in recent weeks all of which we're going to combine now in just a
we're going to combine now in just a slightly more clever way so struct is
slightly more clever way so struct is this keyword in C that lets us build our
this keyword in C that lets us build our own structure in memory like a
own structure in memory like a collection of two or three or more
collection of two or three or more variables like a person that we've seen
variables like a person that we've seen before the dot operator recall we've
before the dot operator recall we've used when you do have a struct like a a
used when you do have a struct like a a person and you want to go inside of it
person and you want to go inside of it so like uh person. name or person.
so like uh person. name or person. number we did this a few weeks ago now
number we did this a few weeks ago now but the dot Operator just allows you to
but the dot Operator just allows you to go inside of a structure and get the
go inside of a structure and get the individual variables within and then the
individual variables within and then the star operator unfortunately has a lot of
star operator unfortunately has a lot of uses now one was multiplication like my
uses now one was multiplication like my God that was easy back in the day now
God that was easy back in the day now it's used to declare pointers it's also
it's used to declare pointers it's also used to dreference pointers so to make
used to dreference pointers so to make one exist and then go to that address
one exist and then go to that address unfortunately it's the same symbol for
unfortunately it's the same symbol for all of those but it's all related but
all of those but it's all related but with these three symbols it turns out
with these three symbols it turns out you're going to get one last one today
you're going to get one last one today and my God it finally looks like the
and my God it finally looks like the concept it turns out there's a clever
concept it turns out there's a clever way anytime you want to use the dot and
way anytime you want to use the dot and the star together that is to go
the star together that is to go somewhere and go to an address and then
somewhere and go to an address and then look inside of a structure you can
look inside of a structure you can actually literally use an arrow symbol
actually literally use an arrow symbol on your keyboard it's not a single
on your keyboard it's not a single keystroke it's a hyphen and then an open
keystroke it's a hyphen and then an open angle bracket but at least it looks like
angle bracket but at least it looks like an arrow and we'll see indeed in code
an arrow and we'll see indeed in code today the things I was drawing
today the things I was drawing pictorially on the screen last time with
pictorially on the screen last time with yellow arrows you can actually now
yellow arrows you can actually now Express as well in code and so here we
Express as well in code and so here we have our next data structure called a
have our next data structure called a linked list and this is one of the most
linked list and this is one of the most useful powerful Concepts in C it's the
useful powerful Concepts in C it's the kind of thing that you can take for
kind of thing that you can take for granted in Java and Python and higher
granted in Java and Python and higher level languages but today we'll see how
level languages but today we'll see how we or others can actually build these
we or others can actually build these things just using these same Primitives
things just using these same Primitives so a linked list is going to allow us to
so a linked list is going to allow us to actually do what we you know used a foam
actually do what we you know used a foam finger for last week allow us to link
finger for last week allow us to link together for instance these three values
together for instance these three values maybe with that fourth value over there
maybe with that fourth value over there and then if there's a fifth you know
and then if there's a fifth you know maybe this other foam finger points even
maybe this other foam finger points even farther overway to that fifth value the
farther overway to that fifth value the key being that you can stitch together
key being that you can stitch together fancier data structures without having
fancier data structures without having to like pick all of these up and find
to like pick all of these up and find new space you just have to at least
new space you just have to at least connect the dots somehow we just need to
connect the dots somehow we just need to somehow point from one to the other and
somehow point from one to the other and that's going to make things much more
that's going to make things much more efficient it would seem so how do we get
efficient it would seem so how do we get there so here's my computer's memory as
there so here's my computer's memory as always suppose that I'm storing the
always suppose that I'm storing the value one somewhere in there and it's at
value one somewhere in there and it's at ox123 address whatever and I'm storing
ox123 address whatever and I'm storing the number two somewhere else in memory
the number two somewhere else in memory Ox 456 and number three at address Ox
Ox 456 and number three at address Ox 789 this is not an array by definition
789 this is not an array by definition why even though it's the only three
why even though it's the only three things on the screen what makes this not
things on the screen what makes this not an array it's not contiguous so this
an array it's not contiguous so this violates the definition of an array but
violates the definition of an array but you know especially since they're
you know especially since they're sequential it kind of looks to a human
sequential it kind of looks to a human like a list so it would be nice if there
like a list so it would be nice if there were a data type called list and there
were a data type called list and there isn't in C there will be in Python but
isn't in C there will be in Python but you know what if I could somehow like
you know what if I could somehow like Stitch together these three values so I
Stitch together these three values so I can get from one to the next to the next
can get from one to the next to the next then I think we could achieve the idea
then I think we could achieve the idea the concept of a list without this
the concept of a list without this really annoying constraint that they all
really annoying constraint that they all be contiguous as in an array so how do I
be contiguous as in an array so how do I do that well at the end of the day I
do that well at the end of the day I only have memory at my disposal right
only have memory at my disposal right there's no more training wheels to take
there's no more training wheels to take off here this is what we've got
off here this is what we've got underneath the hood of a computer so if
underneath the hood of a computer so if all I have is memory I think the
all I have is memory I think the solution to this problem of stitching
solution to this problem of stitching together those values in a list must be
together those values in a list must be to spend a bit more memory that's
to spend a bit more memory that's literally the only resource we have
literally the only resource we have right now so let me propose that if we
right now so let me propose that if we want to create a list conceptually out
want to create a list conceptually out of three values that are in random
of three values that are in random although pictorially pretty positions in
although pictorially pretty positions in memory let me just add a little bit more
memory let me just add a little bit more memory to the picture so in addition to
memory to the picture so in addition to storing the one I'm going to leave my
storing the one I'm going to leave my space myself some room a little scratch
space myself some room a little scratch pad if you will to use some other bits
pad if you will to use some other bits as well same for the two same for the
as well same for the two same for the three and you can perhaps see where this
three and you can perhaps see where this is going based on last week if I want to
is going based on last week if I want to somehow connect the one to the two any
somehow connect the one to the two any instincts as to what I should write in
instincts as to what I should write in this box here that would lead me
this box here that would lead me effectively from one to the two
effectively from one to the two what could go here
yeah we could store the address of Two And so specifically what would you have
And so specifically what would you have me write
here perfect ideally I would just put in this box another integer one that
this box another integer one that happens to be represented in heximal but
happens to be represented in heximal but that's just a base system it's just a
that's just a base system it's just a human thing for us to look at I'm going
human thing for us to look at I'm going to put the value Ox 456 here so let me
to put the value Ox 456 here so let me go ahead and reveal that Ox 456 goes
go ahead and reveal that Ox 456 goes there you can perhaps see further where
there you can perhaps see further where this is going well if I want to get from
this is going well if I want to get from the two to the three I think I need to
the two to the three I think I need to put below the two the address of the
put below the two the address of the three which gives me Ox 789 now if three
three which gives me Ox 789 now if three is the end of the list I don't want to
is the end of the list I don't want to let it be some garbage value because
let it be some garbage value because that would imply that it who knows where
that would imply that it who knows where it's pointing I need some definitive
it's pointing I need some definitive value and just what would your instincts
value and just what would your instincts be if I want to make clear with some
be if I want to make clear with some special Sentinel value that the buck
special Sentinel value that the buck stops here what do I put what my my
stops here what do I put what my my options be yeah so null not n per se but
options be yeah so null not n per se but n l which was the new keyword we
n l which was the new keyword we introduced last week which just
introduced last week which just represents an empty pointer if you will
represents an empty pointer if you will technically the address o x0 so
technically the address o x0 so literally the zero address and what
literally the zero address and what humans did years ago they just decided
humans did years ago they just decided you know what nothing should ever live
you know what nothing should ever live at address zero in memory we're just
at address zero in memory we're just going to reserve that one special bite
going to reserve that one special bite to be a special signal a sentinel value
to be a special signal a sentinel value such that if you ever see a zero address
such that if you ever see a zero address in a pointer it just means it's it's
in a pointer it just means it's it's invalid it does not exist now now we
invalid it does not exist now now we write that though a little more
write that though a little more pleasantly for the eyes as just n l in
pleasantly for the eyes as just n l in all caps and that's a key word in C as
all caps and that's a key word in C as well but of course last week I claimed
well but of course last week I claimed that who cares where things are in
that who cares where things are in memory and honestly like this quickly
memory and honestly like this quickly gets tedious even worrying about these
gets tedious even worrying about these values so let me abstract this away and
values so let me abstract this away and propose that if we want to remember
propose that if we want to remember where all of these numbers are in memory
where all of these numbers are in memory let's give oursel one final piece of
let's give oursel one final piece of memory that just allows us to start the
memory that just allows us to start the whole process let me allocate on the
whole process let me allocate on the left hand side here not room for a
left hand side here not room for a number like 1 two 3 just room for a
number like 1 two 3 just room for a pointer that henceforth I think I'll
pointer that henceforth I think I'll call list by convention and then store
call list by convention and then store in that one additional pointer a value
in that one additional pointer a value that just kickstarts the whole process
that just kickstarts the whole process this is the sort of treasure map if you
this is the sort of treasure map if you will that you get handed and this has
will that you get handed and this has the address of the very first actual
the address of the very first actual node in memory now technically we could
node in memory now technically we could just start with this but it turns out
just start with this but it turns out we'll see it's just a little cleaner to
we'll see it's just a little cleaner to use a simple single pointer that leads
use a simple single pointer that leads to the things you care about as opposed
to the things you care about as opposed to just starting with the first element
to just starting with the first element why well if you ever want to get rid of
why well if you ever want to get rid of this element it'd be nice if you could
this element it'd be nice if you could at least still hang on to an empty sheet
at least still hang on to an empty sheet of paper that indicates that the list is
of paper that indicates that the list is empty would be one argument for that so
empty would be one argument for that so again who cares about these addresses
again who cares about these addresses now now with the wave of the hand let's
now now with the wave of the hand let's just abstract it away and there are our
just abstract it away and there are our pointers each of those addresses in the
pointers each of those addresses in the rec uh the squares at the bottom are
rec uh the squares at the bottom are simply pointing to the next element in
simply pointing to the next element in the list the jargon to introduce here
the list the jargon to introduce here would be that now that we have these
would be that now that we have these integers 1 2 3 but they're in these like
integers 1 2 3 but they're in these like wrappers if you will these structures
wrappers if you will these structures that have metadata that is additional
that have metadata that is additional data that is related to but not the data
data that is related to but not the data you actually care about this is data
you actually care about this is data this is metadata this thing here
this is metadata this thing here rectangularly we'll call a node n o and
rectangularly we'll call a node n o and it's just a term of art that means it's
it's just a term of art that means it's like a container in code for storing
like a container in code for storing some
some values this then is a linked list and
values this then is a linked list and this then is the sort of graphical
this then is the sort of graphical incarnation of like one node pointing to
incarnation of like one node pointing to the other in this case case they happen
the other in this case case they happen to be by chance and by design of this
to be by chance and by design of this desk contiguous initially but there's no
desk contiguous initially but there's no requirement that they be such the one
requirement that they be such the one could be over there the two over there
could be over there the two over there the three over there I would just need
the three over there I would just need more foam fingers to point at one to the
more foam fingers to point at one to the next questions on this concept of a
next questions on this concept of a linked
linked list yeah and
[Music] back can you say that
again a good question do traditional arrays start with a pointer that's
arrays start with a pointer that's outside of the structure short answer no
outside of the structure short answer no arrays are special in C and certain
arrays are special in C and certain other languages and the name of an array
other languages and the name of an array is technically a symbol if you will that
is technically a symbol if you will that the computer the program knows maps to a
the computer the program knows maps to a specific location in memory it's just a
specific location in memory it's just a label a synonym for a memory address it
label a synonym for a memory address it does not take up space so to be clear
does not take up space so to be clear the name of an array does not take up
the name of an array does not take up space like that extra Square on the left
space like that extra Square on the left but you do need that extra Square on the
but you do need that extra Square on the left when implementing a link list so
left when implementing a link list so that you can determine if the list is of
that you can determine if the list is of size zero there's nothing being pointed
size zero there's nothing being pointed at or size three in this case we're sort
at or size three in this case we're sort of taking on more responsibility
of taking on more responsibility ourselves
yeah how do you point to the next element can you
element can you [Music]
elaborate ah good question if each of these elements is pointing to the next
these elements is pointing to the next how is three point to the others short
how is three point to the others short answer it doesn't at least in this
answer it doesn't at least in this design we have more technically what's
design we have more technically what's called a singly linked list and as the
called a singly linked list and as the arrows imply it only goes in One
arrows imply it only goes in One Direction so if you somehow find incode
Direction so if you somehow find incode maybe a for Loop maybe a while loop
maybe a for Loop maybe a while loop somehow you're sort of encode over here
somehow you're sort of encode over here you have no way in code to go backwards
you have no way in code to go backwards unless we changed this to a doubly link
unless we changed this to a doubly link list where I add another box that lets
list where I add another box that lets me have arrows in both directions or
me have arrows in both directions or maybe I just kind of make it uh uh
maybe I just kind of make it uh uh circular and I connect the three back to
circular and I connect the three back to the one which you can totally do but
the one which you can totally do but that tends to you know make life harder
that tends to you know make life harder because now you have to figure out when
because now you have to figure out when you're stuck in a loop in your data
you're stuck in a loop in your data structure but it's doable as well but as
structure but it's doable as well but as is it's a dead end by Design other
is it's a dead end by Design other questions on this design
here all right well how might we implement this structure in code well
implement this structure in code well let me just connect the dots to
let me just connect the dots to something like we've seen before here
something like we've seen before here like this is how a couple of weeks ago
like this is how a couple of weeks ago we introduced the notion of a a person
we introduced the notion of a a person and we claimed a person might have a
and we claimed a person might have a name and a number last week of course we
name and a number last week of course we took off some of these training wheels
took off some of these training wheels and a string is really technically a
and a string is really technically a Char star in both cases but really
Char star in both cases but really there's no conceptual difference beyond
there's no conceptual difference beyond that but let's use this same Paradigm to
that but let's use this same Paradigm to implement a node as I described it in
implement a node as I described it in that picture so let me get rid of the
that picture so let me get rid of the name and the number because that's
name and the number because that's related only to a person and let me
related only to a person and let me rename this structure for discussion
rename this structure for discussion sake to node that then invites the
sake to node that then invites the question well what needs to go inside of
question well what needs to go inside of a node well minimally an
a node well minimally an integer but this is now where we need to
integer but this is now where we need to think a little harder just conceptually
think a little harder just conceptually even if you have no idea how to type it
even if you have no idea how to type it at the keyboard what else needs to be
at the keyboard what else needs to be part of a node based on these
part of a node based on these rectangular pictures that we've
rectangular pictures that we've drawn what more do we need
drawn what more do we need yeah yeah we need a pointer to another
yeah yeah we need a pointer to another node so if I don't know how to implement
node so if I don't know how to implement this yet you know it could be something
this yet you know it could be something like you know pointer to another node
like you know pointer to another node how do I do that well you know what but
how do I do that well you know what but it turns out you would ideally say this
it turns out you would ideally say this if you know that the next node is itself
if you know that the next node is itself a node by definition well anytime we've
a node by definition well anytime we've needed a pointer we just use the data
needed a pointer we just use the data type and a star and I'm going to
type and a star and I'm going to arbitrarily but I think reasonably call
arbitrarily but I think reasonably call this second square at the bottom of
this second square at the bottom of those rectangles next as the name of my
those rectangles next as the name of my attribute here but node star just
attribute here but node star just connotes that the next variable is going
connotes that the next variable is going to be not a node per se but the address
to be not a node per se but the address of a node and that's exactly what we did
of a node and that's exactly what we did you had me put Ox 456 Ox 789 in that box
you had me put Ox 456 Ox 789 in that box which is the address of another node so
which is the address of another node so the way we would Express this in code
the way we would Express this in code would be node star next but we could
would be node star next but we could call the variable anything we want now
call the variable anything we want now this is a bit of a white lie but we'll
this is a bit of a white lie but we'll fix this right now this code won't
fix this right now this code won't actually compile C takes you pretty
actually compile C takes you pretty literally recall and if you use some
literally recall and if you use some term at the top of your file that you
term at the top of your file that you don't Define until later in your file
don't Define until later in your file you're going to see some error message
you're going to see some error message right we've seen this when I've messed
right we've seen this when I've messed up and forgot to include the function
up and forgot to include the function prototypes at the top of my code this is
prototypes at the top of my code this is related in spirit I seem here on my 1
related in spirit I seem here on my 1 two 3 fourth line of code I'm trying to
two 3 fourth line of code I'm trying to use this new term of art that I invented
use this new term of art that I invented here in my code called node even though
here in my code called node even though it's a CS term as well but nowhere Above
it's a CS term as well but nowhere Above This it would seem did I even Define
This it would seem did I even Define what a node is it's not a data type and
what a node is it's not a data type and C every computer scientist know what a
C every computer scientist know what a node is but it doesn't come for free
node is but it doesn't come for free with the language so I need to do
with the language so I need to do something else like I need this word
something else like I need this word here to come first so that I can use it
here to come first so that I can use it here and so we have this sort of Catch
here and so we have this sort of Catch 22 like how can a structure be
22 like how can a structure be self-referential that is point to
self-referential that is point to another version of itself if the word
another version of itself if the word doesn't yet exist so the solution to
doesn't yet exist so the solution to this in C which we didn't need for a
this in C which we didn't need for a person because there was no notion of
person because there was no notion of listing connecting as a list we need one
listing connecting as a list we need one more keyword here that we didn't need
more keyword here that we didn't need for a person and we reuse that keyword
for a person and we reuse that keyword here so kind of an annoying detail but
here so kind of an annoying detail but if we preemptively call this whole thing
if we preemptively call this whole thing struct node you can now refer to the
struct node you can now refer to the thing on the inside as a struct node
thing on the inside as a struct node star but then you can shorten the name
star but then you can shorten the name of the whole thing from struct node to
of the whole thing from struct node to just node sort of an annoying sequence
just node sort of an annoying sequence of steps but in short anytime you're
of steps but in short anytime you're building a node a linked list in memory
building a node a linked list in memory this is just the Paradigm you use type
this is just the Paradigm you use type def struct the name of the thing you
def struct the name of the thing you want to Define like node you use that
want to Define like node you use that name on the inside if you want to point
name on the inside if you want to point from one to another and then you can
from one to another and then you can shorten it down here to just be called
shorten it down here to just be called node questions then on this code
node questions then on this code here questions on what we just did well
here questions on what we just did well if I rewind just a moment to that final
if I rewind just a moment to that final picture what would be the upside to be
picture what would be the upside to be clear of having jumped through these
clear of having jumped through these hoops and added this complexity if you
hoops and added this complexity if you will what problem did we just solve by
will what problem did we just solve by linking together these three values to
linking together these three values to be clear
be clear [Music]
[Music] yeah making lists that
yeah making lists that are that are not contiguous if you will
are that are not contiguous if you will so making lists that are not contiguous
so making lists that are not contiguous in memory the upside of which is that if
in memory the upside of which is that if I want to add the number four to this
I want to add the number four to this list it looks like I could choose from
list it looks like I could choose from any chunks of available memory on the
any chunks of available memory on the screen I just need to sort of point from
screen I just need to sort of point from the end of the current list to wherever
the end of the current list to wherever that other one is in memory what I don't
that other one is in memory what I don't need to do to be clear is copy the One
need to do to be clear is copy the One the two or the three everything can just
the two or the three everything can just stay put which means TimeWise I can do
stay put which means TimeWise I can do this much more quickly it would seem
this much more quickly it would seem without copying things again and again
without copying things again and again and even without using realloc to let it
and even without using realloc to let it do all of the copying potentially for me
do all of the copying potentially for me all right but as we'll start seeing even
all right but as we'll start seeing even more in the coming weeks every time we
more in the coming weeks every time we benefit and solve some problem we pay a
benefit and solve some problem we pay a price there's a tradeoff what is a
price there's a tradeoff what is a downside as you might perceive now of
downside as you might perceive now of using a linked list instead of an array
using a linked list instead of an array yeah as much memory yeah I mean we use
yeah as much memory yeah I mean we use twice as much memory because now in
twice as much memory because now in addition to storing the integers one two
addition to storing the integers one two three I also need to store a pointer for
three I also need to store a pointer for each of those and honestly even this
each of those and honestly even this picture is a bit of uh simplification
picture is a bit of uh simplification technically in most systems today each
technically in most systems today each int would be four bytes technically
int would be four bytes technically today most pointers though would be
today most pointers though would be eight bytes I just didn't want to draw
eight bytes I just didn't want to draw this weird shape on the board where the
this weird shape on the board where the bottom square is even bigger than the
bottom square is even bigger than the top square but technically we're using
top square but technically we're using even more than twice as much space for
even more than twice as much space for these pointers so there's that trade-off
these pointers so there's that trade-off now thankfully decades after SE was
now thankfully decades after SE was invented memory is generally much
invented memory is generally much cheaper nowadays and so it's okay to
cheaper nowadays and so it's okay to sort of spend more of it if you need to
sort of spend more of it if you need to and it depends on what you want to
and it depends on what you want to optimize for but that's absolutely here
optimize for but that's absolutely here a downside what's another downside of
a downside what's another downside of having transitioned to in a uh link list
you can't index into it now I haven't even tried in code but when you have a
even tried in code but when you have a linked list you can no longer use square
linked list you can no longer use square bracket notation because why well square
bracket notation because why well square bracket notation just assumes the
bracket notation just assumes the contiguousness of memory location zero
contiguousness of memory location zero is here location one is literally one to
is here location one is literally one to the right location two is literally one
the right location two is literally one to the right one to the right these
to the right one to the right these things even though I've drawn it from
things even though I've drawn it from right to left to just keep things pretty
right to left to just keep things pretty there are gaps here and this is just my
there are gaps here and this is just my interpretation of this these gaps could
interpretation of this these gaps could be big they could be narrow they could
be big they could be narrow they could be down here up here they could be
be down here up here they could be anywhere so long as we're linking things
anywhere so long as we're linking things together in this list the computer can't
together in this list the computer can't just use bracket zero bracket 1 bracket
just use bracket zero bracket 1 bracket two anymore because it can't do simple
two anymore because it can't do simple arithmetic and jump to like the middle
arithmetic and jump to like the middle and now here's perhaps the worst price
and now here's perhaps the worst price we've paid if you don't have square
we've paid if you don't have square bracket notation or really you don't
bracket notation or really you don't have
have contiguousness what algorithm did we
contiguousness what algorithm did we just sacrifice for this
just sacrifice for this dynamism if you rewind even back to week
dynamism if you rewind even back to week zero and we gave it a name in week
zero and we gave it a name in week three what algorithm can we not use now
three what algorithm can we not use now if we can't assume that the memory is
if we can't assume that the memory is back to back to back to
back to back to back to back binary search why because binary
back binary search why because binary search just like the phone book back in
search just like the phone book back in the first week requires being able to
the first week requires being able to arithmetically jump right to the middle
arithmetically jump right to the middle right take the total length of it divide
right take the total length of it divide by two and boom you're right there in
by two and boom you're right there in the Middle with some simple arithmetic
the Middle with some simple arithmetic here they might be laid out again with
here they might be laid out again with these big or small gaps there's no
these big or small gaps there's no simple math I can do to just jump
simple math I can do to just jump immediately to the one in the middle and
immediately to the one in the middle and in fact again if this TV were bigger the
in fact again if this TV were bigger the two could technically be in memory be
two could technically be in memory be way down here or even way over here the
way down here or even way over here the foam finger could be pointing in any
foam finger could be pointing in any number of directions depending on where
number of directions depending on where malok put the thing there's just no way
malok put the thing there's just no way to do binary search and so it would seem
to do binary search and so it would seem that we've paid another price indeed in
that we've paid another price indeed in terms of it performance we're now
terms of it performance we're now talking about linear time again so
talking about linear time again so that's a regression now that's also a
that's a regression now that's also a lot things like feels like a good time
lot things like feels like a good time for some muffins and fruit out in the
for some muffins and fruit out in the lobby and when we come back we'll try to
lobby and when we come back we'll try to solve the problem we just created so see
solve the problem we just created so see you in 10 so we are back and let's see
you in 10 so we are back and let's see if we can't now take some of these
if we can't now take some of these higher level concepts of like stitching
higher level concepts of like stitching together these nodes in memory and
together these nodes in memory and translate it to some actual code but
translate it to some actual code but we'll do it step by step first before I
we'll do it step by step first before I actually start writing it in vs code so
actually start writing it in vs code so if Carter you wouldn't mind helping me
if Carter you wouldn't mind helping me step through with some visuals let me
step through with some visuals let me propose that line by line we solve some
propose that line by line we solve some of the problems that we've just created
of the problems that we've just created for ourselves in building this thing in
for ourselves in building this thing in memory so let's go ahead and first
memory so let's go ahead and first consider how we could build a length
consider how we could build a length list containing the numbers indeed one
list containing the numbers indeed one then two then three and let's translate
then two then three and let's translate each of those steps to code and then
each of those steps to code and then we'll put it all together into something
we'll put it all together into something that actually runs so how about first
that actually runs so how about first step here will just be this to declare a
step here will just be this to declare a pointer called list that's initially has
pointer called list that's initially has no value at least at this point in the
no value at least at this point in the story list is the name of the variable
story list is the name of the variable node star just means that this is
node star just means that this is essentially going to be our little
essentially going to be our little square over here that points to the
square over here that points to the beginning of the list of course it's
beginning of the list of course it's ideal If It ultimately has a value
ideal If It ultimately has a value because when we initially I'll call this
because when we initially I'll call this line of code it just gives us indeed
line of code it just gives us indeed that square over here on the left but
that square over here on the left but it's got a garbage value because there's
it's got a garbage value because there's no equal sign on the other side there so
no equal sign on the other side there so let's propose that we do one more step
let's propose that we do one more step here and actually initialize it to null
here and actually initialize it to null so that if only we know that it's not
so that if only we know that it's not garbage it at least has some known value
garbage it at least has some known value and null is a good way of signifying
and null is a good way of signifying that at this point in the story The List
that at this point in the story The List is empty indeed null indicates there's
is empty indeed null indicates there's no nodes in the list so that picture
no nodes in the list so that picture would now look like this whereby let's
would now look like this whereby let's just draw instead of writing null
just draw instead of writing null everywhere I'll just leave the squares
everywhere I'll just leave the squares blank when it's not a garbage value per
blank when it's not a garbage value per se it's literally Ox Z or null all right
se it's literally Ox Z or null all right so that's it for building a link list of
so that's it for building a link list of size zero like we're sort of done then
size zero like we're sort of done then but we want to now add a one and then a
but we want to now add a one and then a two then a three so next step here might
two then a three so next step here might be this if I want to allocate the first
be this if I want to allocate the first of my rectangles on our previous picture
of my rectangles on our previous picture I'm going to call malok and I'm going to
I'm going to call malok and I'm going to ask for enough memory to fit a whole
ask for enough memory to fit a whole node now technically I think that's
node now technically I think that's going to be like four bytes for the int
going to be like four bytes for the int and eight bytes for the pointer even
and eight bytes for the pointer even though I did not draw it to scale on the
though I did not draw it to scale on the board so that's technically going to be
board so that's technically going to be what 12 bytes but again size of node
what 12 bytes but again size of node just figures out how many bytes I
just figures out how many bytes I actually need dynamically that's going
actually need dynamically that's going to return to me the address of that
to return to me the address of that chunk of memory which apparently I'm
chunk of memory which apparently I'm going to store inside of a temporary
going to store inside of a temporary variable called n for short for node but
variable called n for short for node but let's see what this does pictorially so
let's see what this does pictorially so when this line of code is executed I
when this line of code is executed I first get on the left that variable n
first get on the left that variable n it's got a garbage value by default
it's got a garbage value by default because I haven't executed the whole
because I haven't executed the whole thing from right to left meanwhile on
thing from right to left meanwhile on the right hand side of the expression
the right hand side of the expression I've got now a node somewhere in memory
I've got now a node somewhere in memory it happened to be free here this is
it happened to be free here this is where malok put it for me but it does
where malok put it for me but it does have two garbage values initially but
have two garbage values initially but because it's a node per my type def
because it's a node per my type def earlier every node I proposed is going
earlier every node I proposed is going to have a number and a next pointer so
to have a number and a next pointer so we can see those labeled here but
we can see those labeled here but they've got two garbage values initially
they've got two garbage values initially but all I care about initially is that
but all I care about initially is that ultimately n is pointing at that chunk
ultimately n is pointing at that chunk of code so initially if we could back up
of code so initially if we could back up two
two steps we have two steps so we have initi
steps we have two steps so we have initi one step forward we have this line of
one step forward we have this line of code gives us this variable here which
code gives us this variable here which has garbage when this side of the
has garbage when this side of the expression is executed that allocates
expression is executed that allocates the memory and then when we copy from
the memory and then when we copy from right to left the address of that chunk
right to left the address of that chunk of memory that's what gives us
of memory that's what gives us conceptually this arrow and the garbage
conceptually this arrow and the garbage goes away because it's a valid pointer
goes away because it's a valid pointer now of course there's still two garbage
now of course there's still two garbage values there because we haven't set this
values there because we haven't set this node to store a number like the number
node to store a number like the number one so let's go ahead and execute one
one so let's go ahead and execute one other line of code like this which while
other line of code like this which while cryptic looking is just an application
cryptic looking is just an application of ideas we've seen in week four and
of ideas we've seen in week four and prior star N means to start at this
prior star N means to start at this variable and go there Follow the arrow
variable and go there Follow the arrow is what the star or the D reference
is what the star or the D reference operator does for us and then the dot
operator does for us and then the dot operator recall when we first introduce
operator recall when we first introduce structs like for a person struct allows
structs like for a person struct allows us to go at the number field or the next
us to go at the number field or the next field so if I do star n and then in
field so if I do star n and then in parentheses to make sure order of
parentheses to make sure order of operations is preserved do number and
operations is preserved do number and then assign it the actual number one
then assign it the actual number one which puts the one in the top of that
which puts the one in the top of that rectangle now admittedly this syntax is
rectangle now admittedly this syntax is not very user friendly it's annoying to
not very user friendly it's annoying to remember you have to the parentheses so
remember you have to the parentheses so there's another Syntax for this whenever
there's another Syntax for this whenever you're doing two things like this in
you're doing two things like this in code dereferencing a pointer that is
code dereferencing a pointer that is going to an address and then further
going to an address and then further using the dot notation to go inside of
using the dot notation to go inside of the structure you find that wonderfully
the structure you find that wonderfully C gives us this syntax whereby you can
C gives us this syntax whereby you can just change the star and the parentheses
just change the star and the parentheses and the dot to just be an arrow and
and the dot to just be an arrow and again it's not a single character on
again it's not a single character on your keyboard it's a hyphen and then an
your keyboard it's a hyphen and then an open angle bracket but I kind of like
open angle bracket but I kind of like the semantics of this because this code
the semantics of this because this code now pretty much matches the picture n
now pretty much matches the picture n arrow leads you to the value that you
arrow leads you to the value that you want to access or ultimately change in
want to access or ultimately change in this way there's one step though we've
this way there's one step though we've forgotten of course which is that we
forgotten of course which is that we can't leave this garbage value here
can't leave this garbage value here because the garbage value is some
because the garbage value is some unknown value that effectively is
unknown value that effectively is pointing who knows where and we don't
pointing who knows where and we don't want to accidentally misinterpret that
want to accidentally misinterpret that garbage value as being a valid address
garbage value as being a valid address and risk going there so of course what
and risk going there so of course what value should we put here instead our old
value should we put here instead our old friend null just to signify that this is
friend null just to signify that this is indeed the end of the list and we could
indeed the end of the list and we could do that with a line of code like this
do that with a line of code like this and again we'll canote as much by just
and again we'll canote as much by just leaving that empty box blank so now we
leaving that empty box blank so now we have a list of size one let's go ahead
have a list of size one let's go ahead and add the second number to it as with
and add the second number to it as with these lines here list equals n allows us
these lines here list equals n allows us to remember that indeed we have this
to remember that indeed we have this list here so if we can step one step
list here so if we can step one step forward here's what the picture now
forward here's what the picture now looks like and technically let's go one
looks like and technically let's go one step further here this is now really
step further here this is now really what's going on in memory once my list
what's going on in memory once my list of size exists my main variable called
of size exists my main variable called list is pointing at exactly that first
list is pointing at exactly that first node at this point in the story I don't
node at this point in the story I don't need to know or care about the temporary
need to know or care about the temporary variable that I called n even though it
variable that I called n even though it might very well still be there but
might very well still be there but indeed this now represents that link
indeed this now represents that link list let's now indeed add the number two
list let's now indeed add the number two so with the same line of code as before
so with the same line of code as before I'm going to allocate another node size
I'm going to allocate another node size of node ideally I would be checking for
of node ideally I would be checking for null here but we're doing the juicy
null here but we're doing the juicy Parts only on the slides let's now go
Parts only on the slides let's now go ahead and depict that so what happens
ahead and depict that so what happens with this this brings back our n pointer
with this this brings back our n pointer which might have been there the whole
which might have been there the whole time but we're doing this step by step
time but we're doing this step by step it's a garbage value though because we
it's a garbage value though because we haven't yet copied from right to left
haven't yet copied from right to left Malo of course gives us a second chunk
Malo of course gives us a second chunk of memory which maybe ends up there with
of memory which maybe ends up there with two garbage values by default I've
two garbage values by default I've omitted the labels now just because
omitted the labels now just because they're still going to be number and
they're still going to be number and next respectively once we copy from
next respectively once we copy from right to left the garbage value indeed
right to left the garbage value indeed becomes an arrow Oscar disappears
becomes an arrow Oscar disappears because it's now indeed a valid pointer
because it's now indeed a valid pointer pointing here now the values themselves
pointing here now the values themselves number and next are invalid garbage
number and next are invalid garbage values so here is where we can now start
values so here is where we can now start using our new syntax like the arrow
using our new syntax like the arrow notation or the star and the dot if you
notation or the star and the dot if you prefer and we can change the value of n
prefer and we can change the value of n Follow the arrow to number and that
Follow the arrow to number and that becomes two similarly we can do this
becomes two similarly we can do this again and set n arrow next so start at n
again and set n arrow next so start at n Follow the arrow access the next field
Follow the arrow access the next field and set that equal to null now we're not
and set that equal to null now we're not quite done yet because we haven't
quite done yet because we haven't actually linked things together so
actually linked things together so here's now where things get interesting
here's now where things get interesting how do I combine these two well let me
how do I combine these two well let me me propose this let me propose on our
me propose this let me propose on our next line here we actually update for
next line here we actually update for Now list equal to n that is to say
Now list equal to n that is to say whatever address this is whatever it's
whatever address this is whatever it's pointing at change list to be the same
pointing at change list to be the same address that is point at the same thing
address that is point at the same thing so if n is pointing here let's change
so if n is pointing here let's change list to point here and go ahead and do
list to point here and go ahead and do that Carter if you could I don't like
that Carter if you could I don't like this can you go one further step this is
this can you go one further step this is bad what is wrong about my sequence of
bad what is wrong about my sequence of operations here where I updated list to
operations here where I updated list to point my new
point my new node
node yeah yeah we lost the pointer to the
yeah yeah we lost the pointer to the other node so I don't even care about
other node so I don't even care about the ordering 21 or one two the bigger
the ordering 21 or one two the bigger problem now as the lack of arrows over
problem now as the lack of arrows over there suggests is that I have a memory
there suggests is that I have a memory leak I have orphaned my original node in
leak I have orphaned my original node in the sense that nothing is pointing at it
the sense that nothing is pointing at it anymore now absolutely I could fix this
anymore now absolutely I could fix this by adding some temporary variables I
by adding some temporary variables I could add it to the mix but at this
could add it to the mix but at this point in the story I have not done any
point in the story I have not done any such uh recollection thereof so let me
such uh recollection thereof so let me back this up and let's go forward in the
back this up and let's go forward in the slides this is where we left off a
slides this is where we left off a moment ago I think I need to take into
moment ago I think I need to take into account order of operations and I'm
account order of operations and I'm going to keep this simple I'm not going
going to keep this simple I'm not going to care about the order of the numbers
to care about the order of the numbers for now I'm fine with a list that is two
for now I'm fine with a list that is two and then one so with that said let me go
and then one so with that said let me go ahead and update I think this box here
ahead and update I think this box here to point at my original node so let's
to point at my original node so let's see how we can do this in code okay n
see how we can do this in code okay n arrow next so n arrow next should equal
arrow next so n arrow next should equal the current list and this is a little
the current list and this is a little weird again but recall what list is list
weird again but recall what list is list is this pointer here that just contains
is this pointer here that just contains the address of the original address of
the address of the original address of the list or equivalently it contains
the list or equivalently it contains this Arrow whatever it's pointing at so
this Arrow whatever it's pointing at so what this means in this line of code n
what this means in this line of code n bracket next means start at n Follow the
bracket next means start at n Follow the arrow access the next pointer and set it
arrow access the next pointer and set it equal to whatever list equals so if list
equal to whatever list equals so if list is pointing here then next should point
is pointing here then next should point there as well this I think is safe
there as well this I think is safe because now we have redundancy now we've
because now we have redundancy now we've got two pointers pointing at the
got two pointers pointing at the original list and now I think we can do
original list and now I think we can do another step whereby we update list to
another step whereby we update list to equal n same line of code before that
equal n same line of code before that got us into trouble but I'm doing it
got us into trouble but I'm doing it second now instead of first when I
second now instead of first when I execute list equals n this now sets list
execute list equals n this now sets list equal to the same thing that n equals
equal to the same thing that n equals and so now I have successfully inserted
and so now I have successfully inserted my new node containing two into the list
my new node containing two into the list and in fact if we advance one more we
and in fact if we advance one more we can just clear up the Clutter assume
can just clear up the Clutter assume that the temporary variable is gone from
that the temporary variable is gone from the story now we have a linked list
the story now we have a linked list where admittedly ordering is wrong it's
where admittedly ordering is wrong it's 21 instead of one two but at least it's
21 instead of one two but at least it's linked correctly and I didn't orphan or
linked correctly and I didn't orphan or leak any
leak any memory questions on this sequence of
memory questions on this sequence of steps
steps here yeah in
here yeah in [Music]
back yeah spot on so this would fall under that category of a stack if you
under that category of a stack if you will although I've not called it that by
will although I've not called it that by name because I just pushed the number
name because I just pushed the number two onto this data structure if you will
two onto this data structure if you will and indeed it ended up at the beginning
and indeed it ended up at the beginning of the list instead of the end and so
of the list instead of the end and so here's where we see a distinction
here's where we see a distinction between an abstract data structure which
between an abstract data structure which is where we began a stack is a thing
is where we began a stack is a thing like the pile of sweaters that just has
like the pile of sweaters that just has push and pop properties and lifo access
push and pop properties and lifo access like uh last in first out how do you
like uh last in first out how do you implement something like that in memory
implement something like that in memory well it would seem that you could
well it would seem that you could implement the notion of a stack here not
implement the notion of a stack here not for sweaters but for numbers using a
for sweaters but for numbers using a linked list so long as you implement
linked list so long as you implement insertion AKA pushing by prepending new
insertion AKA pushing by prepending new values to the list by prepending again
values to the list by prepending again and again and if Carter you don't mind
and again and if Carter you don't mind hitting the keyboard one more time if I
hitting the keyboard one more time if I wanted to add the number three now you
wanted to add the number three now you would could imagine prepending it to the
would could imagine prepending it to the list why well honestly especially as
list why well honestly especially as this list gets longer and longer I kind
this list gets longer and longer I kind of like the appeal of prepending these
of like the appeal of prepending these elements why because even if this list
elements why because even if this list gets crazy long and way way out here you
gets crazy long and way way out here you didn't notice me following all of the
didn't notice me following all of the arrows earlier to do the insert if I
arrows earlier to do the insert if I want to insert a fourth number a fifth
want to insert a fourth number a fifth number a sixth number all I have to do
number a sixth number all I have to do is like insert it here if you will point
is like insert it here if you will point it at the original uh start of the list
it at the original uh start of the list then update this pointer and done and I
then update this pointer and done and I would say that's like two steps give or
would say that's like two steps give or take it's not going to be end steps as
take it's not going to be end steps as it would be if I had to upend the new
it would be if I had to upend the new nodes to the end of the list now of
nodes to the end of the list now of course we've sacrificed ordering of
course we've sacrificed ordering of these numbers they're literally in the
these numbers they're literally in the opposite order or whatever order they
opposite order or whatever order they were inserted in but that might very
were inserted in but that might very well be okay depending on the goal at
well be okay depending on the goal at hand all right thank you to Carter for
hand all right thank you to Carter for stepping through this what if now we
stepping through this what if now we wanted to translate this oh sure thank
you it's all for you none for me in this example so here we have perhaps a way of
example so here we have perhaps a way of translating this now to some actual code
translating this now to some actual code and this will be the last of like the
and this will be the last of like the sort of intense code here just to give
sort of intense code here just to give you a sense of how we can translate this
you a sense of how we can translate this idea now to actual step so this is list.
idea now to actual step so this is list. C and VSS code here let me go ahead and
C and VSS code here let me go ahead and make a couple of changes up top let me
make a couple of changes up top let me go ahead and how about uh declaring a
go ahead and how about uh declaring a node using typ def uh struct node using
node using typ def uh struct node using our new framing as before I'm going to
our new framing as before I'm going to give every node a number as I proposed
give every node a number as I proposed and every node a pointer to the next
and every node a pointer to the next element which is going to be implemented
element which is going to be implemented just as before and I'm going to simplify
just as before and I'm going to simplify the whole name as just node so all of
the whole name as just node so all of that is is the exact same type depth
that is is the exact same type depth that we proposed earlier now let me go
that we proposed earlier now let me go ahead and get rid of all of this code
ahead and get rid of all of this code which we wrote earlier and recall that
which we wrote earlier and recall that this was the most recent version that
this was the most recent version that was not a linked list this was just in
was not a linked list this was just in Array that we allocated and then
Array that we allocated and then reallocated so this is sort of the old
reallocated so this is sort of the old way of doing things but it was
way of doing things but it was inefficient because we might have to
inefficient because we might have to lean on a for Loop or lean on realloc to
lean on a for Loop or lean on realloc to copy everything around we're now going
copy everything around we're now going to reimplement the notion of a list as
to reimplement the notion of a list as an actual linked list not as an array so
an actual linked list not as an array so my main function now might do something
my main function now might do something like this and I'm going to really just
like this and I'm going to really just copy the lines of code that we just
copy the lines of code that we just stepped through on the board so let me
stepped through on the board so let me give myself a uh special variable called
give myself a uh special variable called list that's going to be initialized to
list that's going to be initialized to null and this is just my pointer the
null and this is just my pointer the square on the left hand side of the
square on the left hand side of the screen that represents the start of the
screen that represents the start of the list and if it's null it means the list
list and if it's null it means the list is empty so done I'm done implementing a
is empty so done I'm done implementing a linked list of size zero well now how do
linked list of size zero well now how do I want to run this code well let me
I want to run this code well let me propose for the sake of discussion that
propose for the sake of discussion that this version of the program will take
this version of the program will take command line arguments so I want to be
command line arguments so I want to be able to do something like this I want to
able to do something like this I want to run this program ultimately and type in
run this program ultimately and type in three command line arguments like this 1
three command line arguments like this 1 2 3 and I want my program in a couple
2 3 and I want my program in a couple minutes to allocate one two three nodes
minutes to allocate one two three nodes and Stitch them together just like the
and Stitch them together just like the visualization on the board uh I could
visualization on the board uh I could use get int but it's just going to be
use get int but it's just going to be faster if we use command line arguments
faster if we use command line arguments so again I'm just borrowing some
so again I'm just borrowing some Concepts from week two but none of
Concepts from week two but none of that's possible yet until I change my
that's possible yet until I change my code here so let's do this in argc uh
code here so let's do this in argc uh string uh argv but you know what we know
string uh argv but you know what we know that strings are not actually a thing
that strings are not actually a thing anymore so I can change my command line
anymore so I can change my command line argument definition to be what it really
argument definition to be what it really is it's really charar but it's the exact
is it's really charar but it's the exact same thing as in week two just strings
same thing as in week two just strings are no more at least without the
are no more at least without the training wheels on anymore like last
training wheels on anymore like last week and now let me do this uh for in I
week and now let me do this uh for in I equal 1 uh I is less than ARG C i++ so
equal 1 uh I is less than ARG C i++ so what I'm doing with this Loop is I just
what I'm doing with this Loop is I just want to iterate over the command line
want to iterate over the command line argument so I have one number at a time
argument so I have one number at a time from The Prompt um what else do I want
from The Prompt um what else do I want to do here uh well let's go ahead and
to do here uh well let's go ahead and how about do this um let's get
how about do this um let's get a number so in number equals arv braet I
a number so in number equals arv braet I so a couple of notes Here one I'm
so a couple of notes Here one I'm starting my for loop at one instead of
starting my for loop at one instead of zero but I'm going up to RC RC is
zero but I'm going up to RC RC is argument count how many words are at the
argument count how many words are at the prompt why am I starting at one instead
prompt why am I starting at one instead of zero though given my
of zero though given my goal why am I starting at one
yeah yeah so the first value in RV is is actually the name of the program that's
actually the name of the program that's obviously not a number so I want the
obviously not a number so I want the second value so I'm going to start
second value so I'm going to start iterating over those command line
iterating over those command line arguments at I equals 1 so that's all I
arguments at I equals 1 so that's all I just want to get the actual numbers at
just want to get the actual numbers at the prompt um unfortunately argv bracket
the prompt um unfortunately argv bracket I is a string AKA Char star that is not
I is a string AKA Char star that is not an INT so this line of code won't work
an INT so this line of code won't work but can anyone think back to like week
but can anyone think back to like week two where we had a function for
two where we had a function for converting strings to
converting strings to integers anyone yeah so a to I is a
integers anyone yeah so a to I is a function that converts asky to an
function that converts asky to an integer assuming what you give it as an
integer assuming what you give it as an argument looks like a number like one or
argument looks like a number like one or two or three so let me fix this let me
two or three so let me fix this let me actually do the conversion if I were
actually do the conversion if I were really being careful I would error check
really being careful I would error check this make sure that there's no digits
this make sure that there's no digits just like you might have in problem set
just like you might have in problem set two but for today's purposes I'm just
two but for today's purposes I'm just going to assume the honor System that
going to assume the honor System that the user me is going to run the program
the user me is going to run the program correctly all right so now that I have a
correctly all right so now that I have a variable containing the number from the
variable containing the number from the command line let's just allocate a node
command line let's just allocate a node for it so let me do node star n just
for it so let me do node star n just like we did in the visualization and
like we did in the visualization and let's malok enough space for the size of
let's malok enough space for the size of one such node here I now need to just be
one such node here I now need to just be super safe so if n equals equals null
super safe so if n equals equals null like if I'm out of memory you know what
like if I'm out of memory you know what let me go ahead and just immediately
let me go ahead and just immediately return one here otherwise if that's not
return one here otherwise if that's not the case let me go ahead and update the
the case let me go ahead and update the number field of this new node which it
number field of this new node which it line 24 does exist because it did not
line 24 does exist because it did not return null so I did not exit early with
return null so I did not exit early with return and let me just store whatever
return and let me just store whatever number that human typed in first so the
number that human typed in first so the return value of a to I which per line 17
return value of a to I which per line 17 is in my variable called number and then
is in my variable called number and then let me go ahead and just prepend this to
let me go ahead and just prepend this to the list let me go ahead and say that um
the list let me go ahead and say that um this next field first has a known value
this next field first has a known value null just so that we get rid of that
null just so that we get rid of that second garbage value and let me go ahead
second garbage value and let me go ahead and now prepend it to the list so if I
and now prepend it to the list so if I want to prepend it that means this new
want to prepend it that means this new node must have a next field field that
node must have a next field field that points to the current beginning of the
points to the current beginning of the list and again the goal here is to preen
list and again the goal here is to preen Preen Preen so whatever the current list
Preen Preen so whatever the current list is let's change it so that this new node
is let's change it so that this new node points to that existing list and now
points to that existing list and now step two as before was to update the
step two as before was to update the actual list to point at this node so
actual list to point at this node so recall in red on the screen before I
recall in red on the screen before I screwed up originally and I only did
screwed up originally and I only did this line by moving the pointer too
this line by moving the pointer too early if you will but I fixed that once
early if you will but I fixed that once Carter helped me rewind and we got rid
Carter helped me rewind and we got rid of the red line which indicated error
of the red line which indicated error and I just do n arrow next to change the
and I just do n arrow next to change the next field of this new node to point to
next field of this new node to point to the existing list so I'm not orphaning
the existing list so I'm not orphaning anything all right at this point in the
anything all right at this point in the story I think my code is
story I think my code is correct not batting very well though
correct not batting very well though today but I think my code is correct but
today but I think my code is correct but the program doesn't do anything
the program doesn't do anything interesting so it would be nice to kind
interesting so it would be nice to kind of now iterate over this link list in
of now iterate over this link list in memory whatever its order is and print
memory whatever its order is and print things out well how do we do that well
things out well how do we do that well it turns out if you want to iterate over
it turns out if you want to iterate over a linked List the general Paradigm is to
a linked List the general Paradigm is to do something like this to define a
do something like this to define a temporary variable I could call it temp
temporary variable I could call it temp but another convention that you might as
but another convention that you might as well see is called pointer PTR for short
well see is called pointer PTR for short but you can call it anything you want
but you can call it anything you want and you can have a temporary variable
and you can have a temporary variable first point at the first node in the
first point at the first node in the list and then in some kind of loop like
list and then in some kind of loop like a while loop you point it at the second
a while loop you point it at the second node in the list and then you keep
node in the list and then you keep iterating you point it at the last node
iterating you point it at the last node in the list and then eventually you
in the list and then eventually you iterate too far effectively pointing at
iterate too far effectively pointing at null at which point your while loop can
null at which point your while loop can presumably terminate so how do I
presumably terminate so how do I Implement that idea of allocating a
Implement that idea of allocating a temporary pointer that just points at
temporary pointer that just points at each node in the list and lets me print
each node in the list and lets me print out ultimately each of those numbers
out ultimately each of those numbers well let's go back to my code here and
well let's go back to my code here and let me do this let me go ahead and
let me do this let me go ahead and declare this temporary pointer which is
declare this temporary pointer which is going to be a node star also why because
going to be a node star also why because it's the address of a node the first the
it's the address of a node the first the second the third and I'm going to set
second the third and I'm going to set that equal to whatever the beginning of
that equal to whatever the beginning of the list is so that is going to be
the list is so that is going to be equivalent to this version of the
equivalent to this version of the picture here where pointer is just
picture here where pointer is just temporarily pointing at the first node
temporarily pointing at the first node in the list it's not pointing at list
in the list it's not pointing at list per se it's pointing at the first node
per se it's pointing at the first node in the list which list is also pointing
in the list which list is also pointing at itself all right once I've done this
at itself all right once I've done this I think I can translate this to code
I think I can translate this to code that's a little new but it's
that's a little new but it's conceptually familiar perhaps now while
conceptually familiar perhaps now while that pointer does not equal null so
that pointer does not equal null so while I have a valid pointer like my
while I have a valid pointer like my finger or that arrow is pointing at an
finger or that arrow is pointing at an actual node in memory well let me go
actual node in memory well let me go ahead and print it out so let me print
ahead and print it out so let me print out with percent I back sln whatever is
out with percent I back sln whatever is in the current node at the number field
in the current node at the number field within and again this is going to have
within and again this is going to have the effect hopefully of first printing
the effect hopefully of first printing the three and I think I just need to Now
the three and I think I just need to Now update the pointer so that on the next
update the pointer so that on the next iteration it's pointing at the next
iteration it's pointing at the next value so if this is where the story is
value so if this is where the story is how do I update pointer to point at the
how do I update pointer to point at the second element of the list well I want
second element of the list well I want pointer to point at the two and I want
pointer to point at the two and I want pointer to eventually point at the three
pointer to eventually point at the three well how do I do that well the way in
well how do I do that well the way in code I can follow these arrows is as
code I can follow these arrows is as follows if I currently have pointer
follows if I currently have pointer pointing at this node but I want to
pointing at this node but I want to point it at the next node I can borrow
point it at the next node I can borrow this pointer here so whatever this
this pointer here so whatever this address is in the first node aka the
address is in the first node aka the next field I can copy that into pointer
next field I can copy that into pointer because then pointer will point at
because then pointer will point at whatever this is pointing at by just
whatever this is pointing at by just setting one equal to the other so once
setting one equal to the other so once I've done that the picture will
I've done that the picture will become this and how do I translate that
become this and how do I translate that to code it while new syntax is
to code it while new syntax is surprisingly straightforward all I need
surprisingly straightforward all I need do is say pointer after printing it
do is say pointer after printing it equals whatever pointer currently is but
equals whatever pointer currently is but grab its next field
grab its next field instead and this is a very common
instead and this is a very common Paradigm when iterating over a link list
Paradigm when iterating over a link list and you're using some temporary variable
and you're using some temporary variable like pointer you can simply set pointer
like pointer you can simply set pointer equal to pointer next and what that
equal to pointer next and what that means here is as follows if this is
means here is as follows if this is pointer pointing from here down to here
pointer pointing from here down to here pointer next is Follow the arrow grab
pointer next is Follow the arrow grab the next field so if you set pointer
the next field so if you set pointer equal to this thing that's the same
equal to this thing that's the same thing as pointing this at this same box
thing as pointing this at this same box and indeed if I advance to the next
and indeed if I advance to the next slide even though the arrows are
slide even though the arrows are technically pointing at different parts
technically pointing at different parts of the rectangles that's just for
of the rectangles that's just for graphic sake pointer is now pointing at
graphic sake pointer is now pointing at the second node and when I do this again
the second node and when I do this again on my next iteration it points at this
on my next iteration it points at this and then this last step notice when I
and then this last step notice when I keep doing pointer equals pointer next
keep doing pointer equals pointer next this will become eventually this value
this will become eventually this value but what's this value in this link list
but what's this value in this link list it's null technically so this Arrow will
it's null technically so this Arrow will eventually take on this value when I set
eventually take on this value when I set pointer equal to pointer next and at
pointer equal to pointer next and at that point PTR my temporary pointer is
that point PTR my temporary pointer is going to be null so it might as well
going to be null so it might as well look like this pictorially and what does
look like this pictorially and what does that mean for my Loop once pointer is
that mean for my Loop once pointer is null because you've walked off the end
null because you've walked off the end of the length list what's going to be
of the length list what's going to be true of this Loop here started in line
true of this Loop here started in line 32 any OB observations
32 any OB observations here what's going to be true what will
here what's going to be true what will happen now as soon as we hit the end of
happen now as soon as we hit the end of the list yeah
the list yeah sorry the loop is going to break out why
sorry the loop is going to break out why because line 32 which is constantly
because line 32 which is constantly asking well pointer does not equal null
asking well pointer does not equal null well if pointer finally equals null
well if pointer finally equals null three steps later the four the while
three steps later the four the while loop is now done and so what I can do at
loop is now done and so what I can do at the end of this program once I've
the end of this program once I've printed out those values well first
printed out those values well first let's go ahead and open my terminal
let's go ahead and open my terminal window let's make list okay a compile
window let's make list okay a compile do/ list and let me try the same values
do/ list and let me try the same values one and two and three that's going to
one and two and three that's going to again allocate one node two node three
again allocate one node two node three nodes by prepending prepending
nodes by prepending prepending prepending each of those values and it's
prepending each of those values and it's then going to iterate over them from
then going to iterate over them from left to right and so when I hit enter
left to right and so when I hit enter now what should I see on the screen if
now what should I see on the screen if my code is
correct what will I see feel fre to just call it
call it out 321 because I've prepended
out 321 because I've prepended presumably and here we go I indeed see
presumably and here we go I indeed see 321 so the list is is backwards but all
321 so the list is is backwards but all of the elements are there now
of the elements are there now technically if I ran valgrind on this
technically if I ran valgrind on this valgren would not be happy because I
valgren would not be happy because I have never freed any of my memory so I
have never freed any of my memory so I should probably now have a second Loop
should probably now have a second Loop here that does something like this let
here that does something like this let me again set pointer equal to list I
me again set pointer equal to list I don't need to redeclare it because I've
don't need to redeclare it because I've already created this thing on line 31 I
already created this thing on line 31 I just want to reset it to be the
just want to reset it to be the beginning of the list again and now I
beginning of the list again and now I can do the same kind of thing while PTR
can do the same kind of thing while PTR not equals null go ahead and do this
not equals null go ahead and do this well I don't want to just do free
well I don't want to just do free pointer and then do pointer gets pointer
pointer and then do pointer gets pointer next y my goal is to free all of my
next y my goal is to free all of my memory but I think this is going to get
memory but I think this is going to get me in trouble pointer equals list just
me in trouble pointer equals list just gives me a temporary pointer that points
gives me a temporary pointer that points at the three and then eventually the two
at the three and then eventually the two and then the one how well while pointer
and then the one how well while pointer not equal null I'm freeing the pointer
not equal null I'm freeing the pointer so this is like saying to Malo free that
so this is like saying to Malo free that node free that node free that node but
node free that node free that node but what's the problem with what I've just
what's the problem with what I've just done
done here this code is technically Bugg
here this code is technically Bugg [Music]
[Music] yeah exactly after you call free on
yeah exactly after you call free on pointer You Are by social contract with
pointer You Are by social contract with c not allowed to touch pointer anymore
c not allowed to touch pointer anymore it is invalid now it's still going to be
it is invalid now it's still going to be a number it's still going to be a
a number it's still going to be a pattern of bits but it's invalid and
pattern of bits but it's invalid and you'll very often get a segmentation
you'll very often get a segmentation fault if you tempt fate in that way so I
fault if you tempt fate in that way so I can't free the pointer and then use it
can't free the pointer and then use it literally the next line the solution
literally the next line the solution here kind of like our swapping of the
here kind of like our swapping of the liquids last time was to maybe just have
liquids last time was to maybe just have a temporary variable so I can do a
a temporary variable so I can do a Switcheroo and so a common way to solve
Switcheroo and so a common way to solve this problem to get the order of
this problem to get the order of operations right would be to do
operations right would be to do something like this give yourself a
something like this give yourself a temporary pointer like node star next
temporary pointer like node star next set it equal to the place you want to go
set it equal to the place you want to go next so one step ahead now you can free
next so one step ahead now you can free pointer and then you can update pointer
pointer and then you can update pointer to be that next value so essentially you
to be that next value so essentially you need need like two hands now you create
need need like two hands now you create on line 41 another pointer that if this
on line 41 another pointer that if this is pointing at the first Noe the three
is pointing at the first Noe the three your new pointer is pointing at the two
your new pointer is pointing at the two temporarily so now you can tell malok
temporarily so now you can tell malok via free release this memory but I
via free release this memory but I haven't forgotten where I want to go
haven't forgotten where I want to go next and so I can now continue on so a
next and so I can now continue on so a common Paradigm for just iterating over
common Paradigm for just iterating over these nodes and then freeing them a
these nodes and then freeing them a couple of observations strictly speaking
couple of observations strictly speaking I could have Consolidated this I don't
I could have Consolidated this I don't need two Loops to print the nodes and
need two Loops to print the nodes and then free the nodes I could do that all
then free the nodes I could do that all at once but let's assume that there's
at once but let's assume that there's other stuff of interest in my program
other stuff of interest in my program and I don't want to just immediately
and I don't want to just immediately free it there's one other bug that I
free it there's one other bug that I should probably address here there is
should probably address here there is still a potential memory leak up here
still a potential memory leak up here and this one is super subtle the valind
and this one is super subtle the valind would help you find it notice that in
would help you find it notice that in this Loop here when I'm calling malok
this Loop here when I'm calling malok this line of code is fine if the first
this line of code is fine if the first line of malok fails and returns null
line of malok fails and returns null because I immediately return and I'm
because I immediately return and I'm done but what if the second call but not
done but what if the second call but not the first or the third call but not the
the first or the third call but not the first or second fail this line of code
first or second fail this line of code has me returning immediately you really
has me returning immediately you really need to to do some garbage collection so
need to to do some garbage collection so to speak whereby you really need to go
to speak whereby you really need to go in and free any nodes that you did
in and free any nodes that you did allocate successfully earlier honestly
allocate successfully earlier honestly that's going to be a pin in the neck we
that's going to be a pin in the neck we won't do that here but probably what I'd
won't do that here but probably what I'd want to do is write a function called
want to do is write a function called free list or something like that and
free list or something like that and call that function to free any nodes I
call that function to free any nodes I had previously created so it's not quite
had previously created so it's not quite at the finish line but the building
at the finish line but the building blocks are indeed here questions on this
blocks are indeed here questions on this code and I think it's safe for me to
code and I think it's safe for me to promise that it won't escalate further
promise that it won't escalate further from
from that questions on
that questions on [Music]
[Music] this no well let me show you one
this no well let me show you one alternative that you might prefer and
alternative that you might prefer and I'm pretty sure this isn't an escalation
I'm pretty sure this isn't an escalation it's just an alternative formulation
it's just an alternative formulation another way you can iterate over nodes
another way you can iterate over nodes in a list could be this instead of a
in a list could be this instead of a while loop for instance let me actually
while loop for instance let me actually show you one other piece of syntax here
show you one other piece of syntax here you could technically use a for Loop you
you could technically use a for Loop you could give yourself a node pointer here
could give yourself a node pointer here that initialized is initialized to the
that initialized is initialized to the list you can then check in your for Loop
list you can then check in your for Loop that it's not equal to null and then you
that it's not equal to null and then you can do your update as usual like this
can do your update as usual like this either of these are equivalent even
either of these are equivalent even though this one I suspect looks scarier
though this one I suspect looks scarier it's doing the exact same thing in one
it's doing the exact same thing in one line instead of two but there's no
line instead of two but there's no reason we can't use four Loops instead
reason we can't use four Loops instead of while Loops to achieve the same idea
of while Loops to achieve the same idea but I'll leave these two as
but I'll leave these two as demonstrations of one approach or the
demonstrations of one approach or the other but that's just like in week one
other but that's just like in week one four Loops while Loops whatever looks
four Loops while Loops whatever looks simpler to you even though admittedly
simpler to you even though admittedly neither of these probably looks super
neither of these probably looks super clean all right so let's take the back
clean all right so let's take the back to things more conceptual here up until
to things more conceptual here up until now we've been inserting elements into
now we've been inserting elements into this link list by prepending them let's
this link list by prepending them let's consider what the running time then is
consider what the running time then is of these operations so if I've got a
of these operations so if I've got a link list of size three or size N More
link list of size three or size N More generally time has passed and I've added
generally time has passed and I've added a lot of things to it what's going to be
a lot of things to it what's going to be the running time for instance of
the running time for instance of searching a linked list for some value
searching a linked list for some value and I'll tell you already it's not login
and I'll tell you already it's not login because again binary search is off the
because again binary search is off the table as per before break so what might
table as per before break so what might the running time be of searching a
the running time be of searching a linked list for some value like two or
linked list for some value like two or three or 1 or
three or 1 or 50 what might the running time
50 what might the running time be o of I heard it over here o of n and
be o of I heard it over here o of n and y who was that oh in the middle here why
y who was that oh in the middle here why o of
[Music] n exactly you're going to have to go
n exactly you're going to have to go through every item in the list starting
through every item in the list starting from the left from the beginning which
from the left from the beginning which is how we've been drawing things and
is how we've been drawing things and connecting the dots and in the worst
connecting the dots and in the worst case the element might very well be at
case the element might very well be at the very end so it's going to be Big O
the very end so it's going to be Big O of n what about insertion how many steps
of n what about insertion how many steps in terms of Big O notation has it been
in terms of Big O notation has it been taking me to insert elements into the
taking me to insert elements into the link list using this prepended
design yeah so it's technically constant time Big O of one and again one is just
time Big O of one and again one is just representative of any constant it could
representative of any constant it could technically be two steps or three steps
technically be two steps or three steps or even 10 steps or 100 steps but if
or even 10 steps or 100 steps but if it's always finite and fixed then indeed
it's always finite and fixed then indeed you can say it's in Big O of one now why
you can say it's in Big O of one now why is that well over again no matter how
is that well over again no matter how long this list gets so long as there's
long this list gets so long as there's memory available for me I can just kind
memory available for me I can just kind of uh create a little splice at the
of uh create a little splice at the beginning of the list to put in the new
beginning of the list to put in the new node update the original list and I'm on
node update the original list and I'm on my way and it keeps getting longer even
my way and it keeps getting longer even though it might not be spread out in
though it might not be spread out in memory so big O of one is possible with
memory so big O of one is possible with these link lists if I indeed Preen
these link lists if I indeed Preen things of course if I Preen things
things of course if I Preen things everything's going to get out of order
everything's going to get out of order potentially and we're going to have
potentially and we're going to have maybe the stack property instead of a q
maybe the stack property instead of a q property so we might want to do things
property so we might want to do things slightly differently so instead of doing
slightly differently so instead of doing this
this whereby we kept prepending prepending
whereby we kept prepending prepending prepending suppose we append to the end
prepending suppose we append to the end of the list instead so if we now insert
of the list instead so if we now insert the one the two and the three as we
the one the two and the three as we might want to for a q to maintain that
might want to for a q to maintain that fairness property we might start with an
fairness property we might start with an empty list we might add the one we might
empty list we might add the one we might append the two append the three and so
append the two append the three and so it just is sort of laid out differently
it just is sort of laid out differently in memory and again if I can come to you
in memory and again if I can come to you in the middle what's the running time of
in the middle what's the running time of search again when the link list uses
search again when the link list uses this append
this append implementation yeah still Big O of n
implementation yeah still Big O of n because in the worst case you're going
because in the worst case you're going to have to go through the whole list
to have to go through the whole list just to find it and notice it doesn't
just to find it and notice it doesn't matter if you have an intuition now that
matter if you have an intuition now that the bigger numbers might very well be at
the bigger numbers might very well be at the end you have no way to jump to the
the end you have no way to jump to the end you have no way to jump to the
end you have no way to jump to the middle or do anything resembling binary
middle or do anything resembling binary search every search has to start from
search every search has to start from the left and follow the arrows again and
the left and follow the arrows again and again all right so I don't think we've
again all right so I don't think we've done any better there and in fact what
done any better there and in fact what is insertions running time now in Big O
is insertions running time now in Big O when we're appending to the list in this
when we're appending to the list in this way as we might to implement a Q instead
way as we might to implement a Q instead of a stack what's the running time of
of a stack what's the running time of inserting a new
inserting a new value Big O
value Big O of so not Big O of one in this case but
of so not Big O of one in this case but Big O of n because if I'm appending by
Big O of n because if I'm appending by definition I have to start here and
definition I have to start here and Traverse the whole thing looking for the
Traverse the whole thing looking for the end now this is a bit of an
end now this is a bit of an overstatement right you could obviously
overstatement right you could obviously optimize this slightly by maybe adding
optimize this slightly by maybe adding another variable that always points to
another variable that always points to the last element sort of a cheat sheet a
the last element sort of a cheat sheet a shortcut that gets you all the way to to
shortcut that gets you all the way to to the end that's totally fine it's not
the end that's totally fine it's not doesn't really fit the traditional
doesn't really fit the traditional definition of a singly linked list but
definition of a singly linked list but there's absolutely smart engineering
there's absolutely smart engineering solutions to these kinds of problems but
solutions to these kinds of problems but as designed it would indeed be Big O of
as designed it would indeed be Big O of end to insert to if you've got to go all
end to insert to if you've got to go all the way to the end and you're not using
the way to the end and you're not using a little extra memory to get yourself
a little extra memory to get yourself there quickly well what if we want to
there quickly well what if we want to take things one last step and not just a
take things one last step and not just a pen blindly because even though I
pen blindly because even though I inserted one two three if I inserted
inserted one two three if I inserted them in random order they would end up
them in random order they would end up in random order what if you want to
in random order what if you want to maintain a sorted list from smallest to
maintain a sorted list from smallest to largest well then you might want to
largest well then you might want to insert numbers like this starting from
insert numbers like this starting from an empty list we might have a two then
an empty list we might have a two then we might try inserting a one but we want
we might try inserting a one but we want to keep it sorted so now we're going to
to keep it sorted so now we're going to prepend in our code but then you might
prepend in our code but then you might want to insert a four so you would
want to insert a four so you would append the four because you're probably
append the four because you're probably going to look for the right spot to
going to look for the right spot to insert it then we're going to insert a
insert it then we're going to insert a three and this one's getting a little
three and this one's getting a little annoying because now you have to like
annoying because now you have to like iterate over the list look for the right
iterate over the list look for the right spot and then do a little smarter of a
spot and then do a little smarter of a splice but it's possible but you don't
splice but it's possible but you don't want to Orphan the four for instance and
want to Orphan the four for instance and then ultimately we get back to this
then ultimately we get back to this question what would the performance be
question what would the performance be of your linked list if you're trying to
of your linked list if you're trying to maintain sorted order well search I
maintain sorted order well search I think is going to be Big O of n for the
think is going to be Big O of n for the same reasons as before what about
same reasons as before what about insertion big go of what for inserting
insertion big go of what for inserting into a sorted linked
into a sorted linked list yeah in the worst
list yeah in the worst case yeah it's still Big O ofen so it's
case yeah it's still Big O ofen so it's no worse than but it's not really any
no worse than but it's not really any better than a pending but we gain the
better than a pending but we gain the additional property of maintaining a
additional property of maintaining a sorted list which might very well be
sorted list which might very well be useful if you're sorting your contacts
useful if you're sorting your contacts in your phone or something like that
in your phone or something like that where it just maintain makes sense to
where it just maintain makes sense to maintain sorted order now in the code
maintain sorted order now in the code for online today if you take a look at
for online today if you take a look at some of the final versions of code like
some of the final versions of code like list 6. C and list 5.c as we'll post on
list 6. C and list 5.c as we'll post on the website you can actually see code
the website you can actually see code that will solve all three of these
that will solve all three of these problems the Preen version that we wrote
problems the Preen version that we wrote live the appen version which we talk
live the appen version which we talk through as well as this sorted order one
through as well as this sorted order one but I think I'll avoid showing it live
but I think I'll avoid showing it live just because I do think that starts to
just because I do think that starts to escalate quickly but I think we have
escalate quickly but I think we have enough of a building block if we're
enough of a building block if we're comfortable with prepending to at least
comfortable with prepending to at least solve some real world problems with
solve some real world problems with these link
these link lists questions then on link list which
lists questions then on link list which we'll now leave behind on their own but
we'll now leave behind on their own but now use this technique to solve fancier
now use this technique to solve fancier problems but much less
problems but much less code questions on linked list all right
code questions on linked list all right so to C recap we've kind of taken a s
so to C recap we've kind of taken a s side step with link list like we have
side step with link list like we have this dynamism now where we can grow and
this dynamism now where we can grow and Shrink our chunks of memory without over
Shrink our chunks of memory without over allocating or accidentally
allocating or accidentally underallocation as in the world of an
underallocation as in the world of an array we don't have to worry about
array we don't have to worry about copying values endlessly because once
copying values endlessly because once you allocate the node it can just stay
you allocate the node it can just stay wherever it is in memory and you can
wherever it is in memory and you can just maintain uh you can just Stitch it
just maintain uh you can just Stitch it together somehow but unfortunately we've
together somehow but unfortunately we've sacrificed what we started the class
sacrificed what we started the class with in week zero which was like binary
with in week zero which was like binary search divide and conquer which was like
search divide and conquer which was like gave us that log and running time which
gave us that log and running time which was really compelling if you think back
was really compelling if you think back to the demonstrations and the the
to the demonstrations and the the visuals can we get the best of both
visuals can we get the best of both worlds can we get the sort of uh speed
worlds can we get the sort of uh speed of binary search something logarithmic
of binary search something logarithmic but the dynamism of something like a
but the dynamism of something like a link list well we can actually I think
link list well we can actually I think if we start to think not in a single
if we start to think not in a single Dimension just the x-axis if you will
Dimension just the x-axis if you will but two Dimensions such that our data
but two Dimensions such that our data structures can maybe now have width and
structures can maybe now have width and height if you will and so a tree is
height if you will and so a tree is perhaps the right term here much like a
perhaps the right term here much like a family tree if you have sort of your
family tree if you have sort of your elders up here in the tree and then the
elders up here in the tree and then the branches below them for their children
branches below them for their children and grandchildren and the like that's
and grandchildren and the like that's actually what a computer scientist means
actually what a computer scientist means when they talk about trees not a tree
when they talk about trees not a tree that grows up like this but really one
that grows up like this but really one that typically is depicted growing down
that typically is depicted growing down although this is just an artist's
although this is just an artist's depiction no matter what but there are
depiction no matter what but there are certain types of trees in the world
certain types of trees in the world called binary search trees that are
called binary search trees that are structured on paper and in visually like
structured on paper and in visually like a uh family tree but they have a special
a uh family tree but they have a special property that lends themselves to
property that lends themselves to exactly that feature binary search so
exactly that feature binary search so for instance here is an array back from
for instance here is an array back from week two and I've sorted a whole bunch
week two and I've sorted a whole bunch of numbers here in from 1 to seven we
of numbers here in from 1 to seven we know we can do binary search on this
know we can do binary search on this structure if it's implemented as an
structure if it's implemented as an array but what feature do arrays to be
array but what feature do arrays to be clear not have that link lists
clear not have that link lists do today's kind of a seesaw like what
do today's kind of a seesaw like what did we just gain by adding link list
did we just gain by adding link list that arrays do not
that arrays do not allow
allow yeah
yeah yeah you can insert more elements
yeah you can insert more elements without having to copy or moving
without having to copy or moving everything else around like right now in
everything else around like right now in this single Dimension if these values to
this single Dimension if these values to the left and or right are already used
the left and or right are already used then you have to move everything and
then you have to move everything and that's where we started today's story so
that's where we started today's story so arrays kind of paint you into a corner
arrays kind of paint you into a corner because you have to by definition decide
because you have to by definition decide in advance how big they are well
in advance how big they are well couldn't we have some kind of array that
couldn't we have some kind of array that can still grow but still is contiguous
can still grow but still is contiguous so we can do binary search in some way
so we can do binary search in some way well yes if we sort of rethink how we
well yes if we sort of rethink how we Implement binary search Let Me propose
Implement binary search Let Me propose that this I've chosen these seven
that this I've chosen these seven elements in the array much like the
elements in the array much like the lockers from uh week two to be ordered
lockers from uh week two to be ordered from smallest to largest I've
from smallest to largest I've highlighted now in yellow the middle
highlighted now in yellow the middle elements here and if we were telling the
elements here and if we were telling the story of week two going left or going
story of week two going left or going right let me highlight in red the middle
right let me highlight in red the middle elements of the left half and the right
elements of the left half and the right half and then let me further highlight
half and then let me further highlight in green the other elements in between
in green the other elements in between those and there's a pattern here as you
those and there's a pattern here as you might notice whereby there's one yellow
might notice whereby there's one yellow in the middle and then there's the two
in the middle and then there's the two red and the four green there's kind of
red and the four green there's kind of an implicit structure there if you will
an implicit structure there if you will and what if I do start to think in two
and what if I do start to think in two dimensions and instead of laying out an
dimensions and instead of laying out an array of lockers like this on the x-axis
array of lockers like this on the x-axis only what if I kind of like Slide the
only what if I kind of like Slide the four up and pull the uh the the one the
four up and pull the uh the the one the three the five down and kind of draw
three the five down and kind of draw this in two Dimensions instead well let
this in two Dimensions instead well let me do that as by separating these things
me do that as by separating these things like this such that now let me propose
like this such that now let me propose that each of these squares maybe it
that each of these squares maybe it doesn't have to be contiguous it can be
doesn't have to be contiguous it can be anywhere in the computer's memory but I
anywhere in the computer's memory but I can't have these crazy gaps among them
can't have these crazy gaps among them how could I perhaps keep these things
how could I perhaps keep these things connected conceptually what should I add
connected conceptually what should I add to the picture if you
will yeah branches say again so branches metaphorically here and more technically
metaphorically here and more technically in the language of C maybe just some
in the language of C maybe just some arrows some pointers so I won't bother
arrows some pointers so I won't bother drawing things as rectangles constantly
drawing things as rectangles constantly let me propose that we're now just
let me propose that we're now just abstracting away what a node is but let
abstracting away what a node is but let me claim that each of these squares now
me claim that each of these squares now is a node and a node might have a number
is a node and a node might have a number but it might also have a pointer heck
but it might also have a pointer heck maybe even two or more pointers and let
maybe even two or more pointers and let me draw those now I don't care about
me draw those now I don't care about addresses like ox1 2 3 4 5 6 7 8 n
addresses like ox1 2 3 4 5 6 7 8 n anymore let's just draw our pointers
anymore let's just draw our pointers with arrows but now let me propose that
with arrows but now let me propose that we could very well think about this as a
we could very well think about this as a tree storing what was previous
tree storing what was previous previously array data but now each of
previously array data but now each of these nodes can be anywhere in memory
these nodes can be anywhere in memory and moreover even though I've kind of
and moreover even though I've kind of painted myself into a corner visually on
painted myself into a corner visually on the screen so long as there's more
the screen so long as there's more memory in the computer I could put the
memory in the computer I could put the number zero over here I could put the
number zero over here I could put the number eight over here and I'm if I'm
number eight over here and I'm if I'm smart I could probably if I want to
smart I could probably if I want to insert other numbers like 2.5 or 1.5 or
insert other numbers like 2.5 or 1.5 or values in between you know I bet we
values in between you know I bet we could kind of make room by swiveling
could kind of make room by swiveling things around and just kind of hanging
things around and just kind of hanging things off of these branches slightly
things off of these branches slightly differently and so what does this gain
differently and so what does this gain me well if I instead start to model my
me well if I instead start to model my data not single dimensionally but in two
data not single dimensionally but in two dimensions and I connect those nodes
dimensions and I connect those nodes with these pointers what can I now do I
with these pointers what can I now do I think I just gave myself back binary
think I just gave myself back binary search why suppose I'm searching for the
search why suppose I'm searching for the number five how do I find it Well just
number five how do I find it Well just like in a family tree where you might
like in a family tree where you might visually start reading from top to
visually start reading from top to bottom I'm always going to start from
bottom I'm always going to start from the so-called root of a binary search
the so-called root of a binary search tree this is just like the list pointer
tree this is just like the list pointer that kicks off the whole link list
that kicks off the whole link list process
process this is the so-called route all right
this is the so-called route all right here I am at the number four I want to
here I am at the number four I want to find the number five what decision can I
find the number five what decision can I make when I see that I'm currently at
make when I see that I'm currently at the number four just like the phone book
the number four just like the phone book from week zero where is five
from week zero where is five not it's not to the left and if I were
not it's not to the left and if I were had you know built a little mobile here
had you know built a little mobile here or something we could very dramatically
or something we could very dramatically snip off this Branch this is this is
snip off this Branch this is this is like very lowbudget animation these
like very lowbudget animation these nodes could like fall to the ground and
nodes could like fall to the ground and we're left with half of essentially a
we're left with half of essentially a tree but what do I now know it's
tree but what do I now know it's obviously the five to the right so let
obviously the five to the right so let me go to the right six is obviously not
me go to the right six is obviously not the one I'm looking for but what do I
the one I'm looking for but what do I now know about the five well five is
now know about the five well five is less than the sixth so I can sort of
less than the sixth so I can sort of snip this off here because I know it's
snip this off here because I know it's not going to be down there and I can
not going to be down there and I can follow the remaining Arrow here and
follow the remaining Arrow here and voila I just found it and now without
voila I just found it and now without getting into the weeds of the math I've
getting into the weeds of the math I've got here what Seven Elements that's
got here what Seven Elements that's roughly eight if I round up and if I do
roughly eight if I round up and if I do some log base two I actually 1 two three
some log base two I actually 1 two three is the key detail here the height of
is the key detail here the height of this tree
this tree is three because I took a list of size
is three because I took a list of size seven and IED it and IED it in order to
seven and IED it and IED it in order to let it dangle in these two Dimensions
let it dangle in these two Dimensions plus or minus one for rounding sake so
plus or minus one for rounding sake so what do I get back I now have binary
what do I get back I now have binary search but it's not like H the middle of
search but it's not like H the middle of the middle of the middle I now follow
the middle of the middle I now follow these arrows in one of two directions so
these arrows in one of two directions so each of these nodes now has an INT and
each of these nodes now has an INT and maybe a left pointer and a right pointer
maybe a left pointer and a right pointer but you can call them anything you want
but you can call them anything you want and so I've gotten back binary search
and so I've gotten back binary search and dynamism because if you want to add
and dynamism because if you want to add zero or eight or 9 or 10 we can just
zero or eight or 9 or 10 we can just dangle them at the bottom of the binary
dangle them at the bottom of the binary search tree so what would this look like
search tree so what would this look like in code but we won't actually implement
in code but we won't actually implement it line by line well here was previously
it line by line well here was previously our definition of a node for a link list
our definition of a node for a link list which was onedimensional if you will
which was onedimensional if you will even though it might bounce up and down
even though it might bounce up and down on the screen it was still just a line
on the screen it was still just a line if you will well let me get rid of the
if you will well let me get rid of the single pointer in the linked list let me
single pointer in the linked list let me make a little bit of room here in this
make a little bit of room here in this type death and let me propose that we
type death and let me propose that we just add two pointers each of which is a
just add two pointers each of which is a struct node star one will be called left
struct node star one will be called left by convention one will be called right
by convention one will be called right by convention and so long as someone not
by convention and so long as someone not me not today not in class writes the
me not today not in class writes the code that stitches together this data
code that stitches together this data structure too handling both the left
structure too handling both the left child and the right child so to speak I
child and the right child so to speak I think we can indeed stitch together that
think we can indeed stitch together that two-dimensional structure and moreover
two-dimensional structure and moreover once you have this in memory you can
once you have this in memory you can translate pretty elegantly to code
translate pretty elegantly to code binary search itself using a principle
binary search itself using a principle we talked about recently too here is for
we talked about recently too here is for instance a function that I'll write by
instance a function that I'll write by just clicking through steps called
just clicking through steps called search whose purpose in life is to
search whose purpose in life is to return a Boolean true or false the
return a Boolean true or false the number I'm looking for is in the tree
number I'm looking for is in the tree this search function therefore takes two
this search function therefore takes two arguments the number I'm looking for
arguments the number I'm looking for called number and then a pointer to the
called number and then a pointer to the tree the so-called root of the tree now
tree the so-called root of the tree now how can I Implement binary search in
how can I Implement binary search in code will recall our brief discussion of
code will recall our brief discussion of recursion it turns out recursion is a
recursion it turns out recursion is a beautiful technique and honestly more
beautiful technique and honestly more obvious technique when you have two
obvious technique when you have two dimensional structures which finally
dimensional structures which finally after five plus weeks we now do here's
after five plus weeks we now do here's maybe my first line of code here if the
maybe my first line of code here if the tree is null then obviously return false
tree is null then obviously return false you've handed me an empty tree there's
you've handed me an empty tree there's nothing going on obviously the number
nothing going on obviously the number you're looking for is not going to be
you're looking for is not going to be here so that's my like safe base case to
here so that's my like safe base case to make sure I don't screw up and recurse
make sure I don't screw up and recurse infinitely well what else might be the
infinitely well what else might be the case well if the number I'm looking for
case well if the number I'm looking for is less than the tree's own number and
is less than the tree's own number and now recall that trees a node star so
now recall that trees a node star so even though I'm calling it a tree it's
even though I'm calling it a tree it's really the current node that's been
really the current node that's been passed in so if the number I'm looking
passed in so if the number I'm looking for is less than the current nodes
for is less than the current nodes number then I must know that the number
number then I must know that the number I'm looking for is to the left so to
I'm looking for is to the left so to speak so how can I solve that well this
speak so how can I solve that well this is where the magic of recursion just
is where the magic of recursion just return whatever the answer is to calling
return whatever the answer is to calling search again but on a sub tree if you
search again but on a sub tree if you will this is the sort of equivalent of
will this is the sort of equivalent of snipping off half of the tree pass in
snipping off half of the tree pass in the left sub tree if you will with the
the left sub tree if you will with the same number else if the number you're
same number else if the number you're looking for isn't less than the current
looking for isn't less than the current nodes number but greater than snip off
nodes number but greater than snip off the other subtree instead and just
the other subtree instead and just return whatever search says it finds in
return whatever search says it finds in the right subtree here and then there's
the right subtree here and then there's a fourth and final case what else could
a fourth and final case what else could be true
be true logically
logically [Music]
[Music] yeah perfect if the number you're
yeah perfect if the number you're looking for equals equals the number in
looking for equals equals the number in this node then I'm just going to return
this node then I'm just going to return true and you might recall from our
true and you might recall from our recurring discussions of design I don't
recurring discussions of design I don't strictly need to ask that explicitly
strictly need to ask that explicitly either there's no node it's to the left
either there's no node it's to the left it's to the right or you found it so I
it's to the right or you found it so I can just Whittle that down as usual to
can just Whittle that down as usual to an else and this now returns my true so
an else and this now returns my true so here too this is where recursion once
here too this is where recursion once you get comfy with it sort of gets
you get comfy with it sort of gets pretty elegant and cool in the sense
pretty elegant and cool in the sense that wow even though there's a lot of
that wow even though there's a lot of lines here I mean there's only a few
lines here I mean there's only a few interesting lines a lot of it's like
interesting lines a lot of it's like Curly braces at that which strictly
Curly braces at that which strictly speaking I could get rid of and so
speaking I could get rid of and so recurs lends itself to Elegance when it
recurs lends itself to Elegance when it comes to traversing these
comes to traversing these two-dimensional data structures as well
two-dimensional data structures as well so that is in code how you might
so that is in code how you might Implement something like
Implement something like search questions then on these
search questions then on these trees we have dynamism we can insert
trees we have dynamism we can insert more nodes to them they're faster
more nodes to them they're faster because we get B search back but but but
because we get B search back but but but there's got to be a price paid any
there's got to be a price paid any downsides or question or
downsides or question or downside okay let me come back to that
downside okay let me come back to that in just one sec downside though what
in just one sec downside though what price of we paid for this dynamism and
price of we paid for this dynamism and for this binary
for this binary searchability even though I've
searchability even though I've abstracted it away in the
abstracted it away in the picture say
picture say again we're using a lot of memory right
again we're using a lot of memory right I'm kind of misleading you now because
I'm kind of misleading you now because I'm just drawing these little squares
I'm just drawing these little squares with the simple numbers but there's
with the simple numbers but there's actually three things in there a four
actually three things in there a four byte integer an 8 by left pointer a 8 by
byte integer an 8 by left pointer a 8 by right pointer so we're already up to 16
right pointer so we're already up to 16 20 bytes now to store individual ins
20 bytes now to store individual ins that's probably okay though if memory is
that's probably okay though if memory is relatively cheap and voluminous as it
relatively cheap and voluminous as it nowadays is but these are the kinds of
nowadays is but these are the kinds of trade-offs and here too you see a hint
trade-offs and here too you see a hint of why some people still do like and use
of why some people still do like and use C and in fact it's so omnipresent
C and in fact it's so omnipresent because when you have C you can really
because when you have C you can really fine-tune how much memory is being used
fine-tune how much memory is being used for Better or For Worse under the hood
for Better or For Worse under the hood as we transition soon to python these
as we transition soon to python these decisions get made for you and you have
decisions get made for you and you have much much less control about how many me
much much less control about how many me how much memory is being used by your
how much memory is being used by your program because someone else made those
program because someone else made those designed decisions for you
designed decisions for you question
question is it bad if we don't know the parent
is it bad if we don't know the parent node uh not necessarily there's no
node uh not necessarily there's no reason why you need to have pointers in
reason why you need to have pointers in both directions however that can lend
both directions however that can lend itself to efficiency by spending more
itself to efficiency by spending more space and having arrows go up too you
space and having arrows go up too you can actually save more time when
can actually save more time when searching the tree in other context this
searching the tree in other context this though would be the canonical way the
though would be the canonical way the typical way to implement it um but
typical way to implement it um but absolutely just like a doubly link list
absolutely just like a doubly link list that could help you solve other problems
that could help you solve other problems too all right so turns out I'm kind of
too all right so turns out I'm kind of overselling binary search trees there
overselling binary search trees there are perversions of them so to speak
are perversions of them so to speak whereby they won't actually behave as
whereby they won't actually behave as advertised for instance here's a a good
advertised for instance here's a a good situation suppose you've got an empty
situation suppose you've got an empty tree initially and you insert the number
tree initially and you insert the number two well it's got to go somewhere so it
two well it's got to go somewhere so it might as well become the root of this
might as well become the root of this binary search tree and let's assume that
binary search tree and let's assume that someone wrote the code to do this now
someone wrote the code to do this now you want to insert the number one and
you want to insert the number one and you want to maintain the searchability
you want to maintain the searchability of this tree well it's important to note
of this tree well it's important to note that binary search tree is different
that binary search tree is different from tree if you just got a tree in
from tree if you just got a tree in memory there is no social contract with
memory there is no social contract with where the numbers need to go they can be
where the numbers need to go they can be completely random all over the place
completely random all over the place binary search tree means that you can do
binary search tree means that you can do binary search means that any node here
binary search means that any node here is going to be greater than every node
is going to be greater than every node here and less than every node here and
here and less than every node here and that's a definition it's a recursive
that's a definition it's a recursive structural definition that must be true
structural definition that must be true to be a binary search tree or BST so if
to be a binary search tree or BST so if we maintain that property ourselves let
we maintain that property ourselves let me insert two let me insert one one
me insert two let me insert one one belongs there by that definition let me
belongs there by that definition let me insert three three belongs there by that
insert three three belongs there by that definition but I kind of got lucky in
definition but I kind of got lucky in that I in the story inserted two and
that I in the story inserted two and then one and then three let me propose a
then one and then three let me propose a sort of perversion of the algorithm
sort of perversion of the algorithm whereby we just get unlucky let me
whereby we just get unlucky let me propose that we insert in uh insert one
propose that we insert in uh insert one first and then we insert two well where
first and then we insert two well where does two go well logically it goes to
does two go well logically it goes to the right because it's larger all right
the right because it's larger all right now the user inserts three where does it
now the user inserts three where does it go okay it goes there logically and how
go okay it goes there logically and how does this story un fold the user inserts
does this story un fold the user inserts four five six it's wonderfully sorted in
four five six it's wonderfully sorted in advance by luck but this is a perversion
advance by luck but this is a perversion of the structure in what
of the structure in what sense it's still technically a binary
sense it's still technically a binary search tree but what does it look more
search tree but what does it look more like it really is devolving if you will
like it really is devolving if you will into a linked list and so if you the
into a linked list and so if you the programmer don't Implement a binary
programmer don't Implement a binary search tree with some kind of repairs
search tree with some kind of repairs going on such that as soon as something
going on such that as soon as something gets whoa a little too long in stringy I
gets whoa a little too long in stringy I think I can fix this it's going to be an
think I can fix this it's going to be an annoying line number of lines of code
annoying line number of lines of code which we're not going to write here or
which we're not going to write here or or in a pet but we could kind of pivot
or in a pet but we could kind of pivot this thing right and we could just
this thing right and we could just rejigger things so that the two becomes
rejigger things so that the two becomes the new root the one becomes the left
the new root the one becomes the left child the three becomes the right child
child the three becomes the right child but that's what like two three plus
but that's what like two three plus lines of code it's possible it's doable
lines of code it's possible it's doable but it's it's extra work it's extra code
but it's it's extra work it's extra code so unless you write that code though and
so unless you write that code though and maintain balance of these trees just
maintain balance of these trees just because it's a binary search tree does
because it's a binary search tree does not mean its height is going to be log
not mean its height is going to be log base 2 of n the height could be log it
base 2 of n the height could be log it could be n in which case you don't get
could be n in which case you don't get those properties so when it comes to
those properties so when it comes to looking up in a balanced binary search
looking up in a balanced binary search tree yes it's log in but if it's
tree yes it's log in but if it's unbalanced if you don't add that
unbalanced if you don't add that additional logic and those repairs so to
additional logic and those repairs so to speak you could it could devolve into
speak you could it could devolve into Big O of N and this is a whole category
Big O of N and this is a whole category of algorithms and fanciness that you
of algorithms and fanciness that you would explore in a higher level course
would explore in a higher level course on algorithms and data structures
on algorithms and data structures there's lots of way to do that sort of
there's lots of way to do that sort of fixing that I'm alluding to in the
fixing that I'm alluding to in the picture there on the screen
picture there on the screen screen all right a few other data
screen all right a few other data structures if you will toward an end of
structures if you will toward an end of a sort of computer science Holy Grail so
a sort of computer science Holy Grail so log in is repeatedly a really good place
log in is repeatedly a really good place to end up we started in week zero when
to end up we started in week zero when we got log in we lost it this earlier
we got log in we lost it this earlier today by introducing link list but we
today by introducing link list but we just got it back albeit at the price of
just got it back albeit at the price of spending more space but the Holy Grail
spending more space but the Holy Grail so to speak when it comes to algorithms
so to speak when it comes to algorithms would not be Big O of n certainly
would not be Big O of n certainly definitely not n squar like our mer like
definitely not n squar like our mer like our bubble sorts and selection sorts and
our bubble sorts and selection sorts and not even Big O of Logan what's better
not even Big O of Logan what's better than all of
than all of those big O of one constant time right
those big O of one constant time right that's the Holy Grail because if we
that's the Holy Grail because if we could store huge amounts of data but
could store huge amounts of data but find it instantly in one step or two
find it instantly in one step or two steps or heck even 10 or 20 steps but
steps or heck even 10 or 20 steps but independent of the size of the data
independent of the size of the data structure that's pretty powerful I mean
structure that's pretty powerful I mean that's the secret sauce of the Googles
that's the secret sauce of the Googles and the twitters of the world trying to
and the twitters of the world trying to get back results really really fast well
get back results really really fast well it turns out another abstract data type
it turns out another abstract data type or abstract data structure might be
or abstract data structure might be something called a dictionary just like
something called a dictionary just like the maram Webster Oxford English
the maram Webster Oxford English dictionaries that you might know which
dictionaries that you might know which associate say words with definitions
associate say words with definitions well you can think of a dictionary
well you can think of a dictionary really abstractly as this like two
really abstractly as this like two columns maybe on a spreadsheet of sorts
columns maybe on a spreadsheet of sorts where the left column represents
where the left column represents something and the right column
something and the right column represents something else like the word
represents something else like the word is on the left and its definition is on
is on the left and its definition is on the right and that's almost literally
the right and that's almost literally what a dictionary is on paper you've got
what a dictionary is on paper you've got all the words and all the definitions
all the words and all the definitions right next to it but more generally in
right next to it but more generally in Computing a dictionary really just has
Computing a dictionary really just has not words and definitions per se but key
not words and definitions per se but key value pairs this is a term of Art and
value pairs this is a term of Art and we're going to see this again and again
we're going to see this again and again especially as we transition to web
especially as we transition to web programming keys and values key is what
programming keys and values key is what you use to look for something the value
you use to look for something the value is what you find ultimately via that key
is what you find ultimately via that key so that's the generic term there we've
so that's the generic term there we've seen key value pairs really in the past
seen key value pairs really in the past in week zero we talked about your
in week zero we talked about your contacts in your iPhone or Android phone
contacts in your iPhone or Android phone uh being an app that has a whole bunch
uh being an app that has a whole bunch of contexts presumably alphabetized by
of contexts presumably alphabetized by first name or last name or the like well
first name or last name or the like well one one of those contact cards
one one of those contact cards ultimately has someone's number for
ultimately has someone's number for instance like John Harvard in this case
instance like John Harvard in this case so in that type of application the keys
so in that type of application the keys is the name like John Harvard that you
is the name like John Harvard that you use to find information and the value is
use to find information and the value is the number that you find there or if
the number that you find there or if there's more information like where he
there's more information like where he lives and uh email address and the like
lives and uh email address and the like the whole contact card could be the
the whole contact card could be the value thereof the key is what you use to
value thereof the key is what you use to look up John Harvard now back in week
look up John Harvard now back in week zero oh and rather the corresponding
zero oh and rather the corresponding table then if we draw this in two
table then if we draw this in two columns wouldn't be word and definition
columns wouldn't be word and definition or key value generically it would be
or key value generically it would be name and number for instance so we're
name and number for instance so we're just slapping some new terminology on
just slapping some new terminology on this old contact problem well this is
this old contact problem well this is the picture we drew way back in week
the picture we drew way back in week zero whereby I claimed that log of n was
zero whereby I claimed that log of n was really really good and indeed it was and
really really good and indeed it was and has been since but the Holy Grail would
has been since but the Holy Grail would indeed be something more like this in
indeed be something more like this in this dashed Green Line constant time and
this dashed Green Line constant time and maybe not literally one step but a fixed
maybe not literally one step but a fixed number of steps that even as the problem
number of steps that even as the problem gets huge and you go way way out on the
gets huge and you go way way out on the right of the X AIS the problem does not
right of the X AIS the problem does not depend on the side the uh the uh the
depend on the side the uh the uh the time to solve the problem does not
time to solve the problem does not depend at all on the size of the problem
depend at all on the size of the problem itself you can have a thousand contacts
itself you can have a thousand contacts or 100,000 contacts constant time means
or 100,000 contacts constant time means it takes the same number of steps no
it takes the same number of steps no matter what well how can we get to that
matter what well how can we get to that point well there's a couple of final
point well there's a couple of final building blocks today and there's one
building blocks today and there's one called hashing and this is something
called hashing and this is something that will recur a few times but for now
that will recur a few times but for now hashing is all about taking as input
hashing is all about taking as input some value
some value and outputting a simpler version thereof
and outputting a simpler version thereof so for instance here's a a gratuitously
so for instance here's a a gratuitously large deck of cards which are all the
large deck of cards which are all the more visible as a result and in a deck
more visible as a result and in a deck of cards typically you've got like what
of cards typically you've got like what 52 cards plus maybe the Jokers and
52 cards plus maybe the Jokers and whatnot and each of those cards has a
whatnot and each of those cards has a number of sorts and a suit on it and
number of sorts and a suit on it and here are literally four buckets on the
here are literally four buckets on the stage and how might I go about sorting
stage and how might I go about sorting these cards not just by number but also
these cards not just by number but also by suit well you could certainly like
by suit well you could certainly like spread them all out and sort of make a
spread them all out and sort of make a mess of things and just kind of reason
mess of things and just kind of reason your way through it and get everything
your way through it and get everything in order according to suit and corded by
in order according to suit and corded by number but most of us even if you don't
number but most of us even if you don't have four buckets at home probably are
have four buckets at home probably are going to do something a little more
going to do something a little more intuitive feels like an optimization
intuitive feels like an optimization where if I find like the nine of Hearts
where if I find like the nine of Hearts I'm going to put that into the hearts
I'm going to put that into the hearts bucket the King of Spades I'm going to
bucket the King of Spades I'm going to put that into the Spades bucket the jack
put that into the Spades bucket the jack of diamonds over here and I'll do this
of diamonds over here and I'll do this with the Queen of Diamonds and uh the
with the Queen of Diamonds and uh the Ace of clubs here and the three here and
Ace of clubs here and the three here and the 10 here and even though it's still
the 10 here and even though it's still going to be 52 steps why am I and maybe
going to be 52 steps why am I and maybe at home like why would you perhaps do
at home like why would you perhaps do this step
this step first what's the value of bucketization
reduces the probability of errors or the like and what I'm doing here to give it
like and what I'm doing here to give it a technical term is that I'm hashing the
a technical term is that I'm hashing the values I'm taking as input a card like
values I'm taking as input a card like this and I'm reducing it more simply
this and I'm reducing it more simply from a larger domain to a much smaller
from a larger domain to a much smaller range if you will so here's a domain of
range if you will so here's a domain of like 52 possibilities I want to map that
like 52 possibilities I want to map that to a range of four possible outcomes the
to a range of four possible outcomes the Diamonds the clubs the carts or the
Diamonds the clubs the carts or the Spades here and by doing that I'm just
Spades here and by doing that I'm just shrinking the size of the problem so
shrinking the size of the problem so hashing does that it's like literally an
hashing does that it's like literally an F ofx type Arrangement whereby you pass
F ofx type Arrangement whereby you pass something in and you get back a simpler
something in and you get back a simpler known value well a hash function more
known value well a hash function more technically is the algorithm or even the
technically is the algorithm or even the math or even the code that implements
math or even the code that implements that idea converting something bigger to
that idea converting something bigger to something smaller to this indeed finite
something smaller to this indeed finite range of values and it turns out that
range of values and it turns out that hash tables are a wonderful application
hash tables are a wonderful application of arrays and length lists to try to
of arrays and length lists to try to Leverage The Best of Both Worlds the
Leverage The Best of Both Worlds the goal being theoretically to achieve that
goal being theoretically to achieve that Holy Grail of constant time and that's
Holy Grail of constant time and that's going to be a bit of an overstatement
going to be a bit of an overstatement because you're not always going to
because you're not always going to achieve it exactly but at least we can
achieve it exactly but at least we can get a little closer there too so with
get a little closer there too so with hash tables you have something that
hash tables you have something that looks like this this is just an array
looks like this this is just an array this is an artist rendition of drawing
this is an artist rendition of drawing it vertically instead of horizontally
it vertically instead of horizontally but that's just a a detail graphically
but that's just a a detail graphically and this array for instance maybe uh is
and this array for instance maybe uh is of size 26 and where am I going with
of size 26 and where am I going with this well how does Apple how does Google
this well how does Apple how does Google Store your contexts alphabetically in
Store your contexts alphabetically in your phone and search for things quickly
your phone and search for things quickly well they might they probably
well they might they probably alphabetize at least in English
alphabetize at least in English according to a through z or if we
according to a through z or if we convert that to numbers it's like what
convert that to numbers it's like what 65 through whatever or really 0 through
65 through whatever or really 0 through 25 suffices if we're using an array of
25 suffices if we're using an array of size 26 we start counting at zero and we
size 26 we start counting at zero and we count up to 25 but let's abstract that
count up to 25 but let's abstract that away as just letters of the alphabet so
away as just letters of the alphabet so maybe what Google and apple are doing in
maybe what Google and apple are doing in your phone is storing all of the A's up
your phone is storing all of the A's up there all of the Z's down there and
there all of the Z's down there and everything else in between and so this
everything else in between and so this works pretty well if you start adding
works pretty well if you start adding your friends and your family so for
your friends and your family so for instance and I'll get rid of the letter
instance and I'll get rid of the letter so it's to not distract uh alvus might
so it's to not distract uh alvus might go in that first spot because a you
go in that first spot because a you subtract the 65 maps to zero so we put
subtract the 65 maps to zero so we put him in the first bucket the a bucket uh
him in the first bucket the a bucket uh maybe Zacharias ends up all the way at
maybe Zacharias ends up all the way at the end there and then in the middle
the end there and then in the middle might here be Hermione and if we do this
might here be Hermione and if we do this dot dot dot you keep adding all of your
dot dot dot you keep adding all of your classmates you might get a uh contact
classmates you might get a uh contact database that has all of this data here
database that has all of this data here in now each of these nodes they're drawn
in now each of these nodes they're drawn differently because this is just another
differently because this is just another artist rendition these rectangles these
artist rendition these rectangles these long rectangles represent a contact card
long rectangles represent a contact card like John Harvard's that's got the name
like John Harvard's that's got the name maybe email definitely phone number and
maybe email definitely phone number and things like that so this seems great why
things like that so this seems great why how can I find Albus well I go to the a
how can I find Albus well I go to the a bucket how do I find Zacharias I go to
bucket how do I find Zacharias I go to the Z bucket how do I find Hermione I go
the Z bucket how do I find Hermione I go to the H bucket but but but I've done
to the H bucket but but but I've done this very deliberately what problem will
this very deliberately what problem will arise eventually assuming you have
arise eventually assuming you have enough classmates
yeah there'll be too many people too many contacts for all of the available
many contacts for all of the available spaces in the array there's still some
spaces in the array there's still some room here but I'm pretty sure if I think
room here but I'm pretty sure if I think back to this particular class uh we've
back to this particular class uh we've got not hermy but also Harry who's also
got not hermy but also Harry who's also an H Hagrid who's also an H so where do
an H Hagrid who's also an H so where do I put them I could just put them
I put them I could just put them arbitrarily in any of the open spots but
arbitrarily in any of the open spots but then you lose the immediacy of jumping
then you lose the immediacy of jumping right to the H right to the a right to
right to the H right to the a right to the Z but now that we have link lists we
the Z but now that we have link lists we can kind of combine these ideas right
can kind of combine these ideas right use an array to get to the first letter
use an array to get to the first letter of the name you care about and then if
of the name you care about and then if you have a collision so to speak whereby
you have a collision so to speak whereby someone's already there you don't do
someone's already there you don't do something stupid like put Harry down
something stupid like put Harry down here just because it's available or
here just because it's available or maybe Hagrid down here just because it's
maybe Hagrid down here just because it's available because then you're losing the
available because then you're losing the immediacy of the lookup why don't you
immediacy of the lookup why don't you just kind of stitch them together in a
just kind of stitch them together in a linked list now what does this mean this
linked list now what does this mean this means for most of the characters here
means for most of the characters here you have constant time lookup you look
you have constant time lookup you look up alvas boom you're done Zacharias boom
up alvas boom you're done Zacharias boom you're done okay Harry Hermione Hagrid
you're done okay Harry Hermione Hagrid it might be one two or three steps so
it might be one two or three steps so that's actually devolving into something
that's actually devolving into something linear but here we make a distinction
linear but here we make a distinction today between theoretical running times
today between theoretical running times which we keep talking about and honestly
which we keep talking about and honestly a clock on the wall running times that
a clock on the wall running times that actual humans care about this is way
actual humans care about this is way faster than a linked list because you
faster than a linked list because you don't have to search every name it's
don't have to search every name it's even faster than a an array because you
even faster than a an array because you don't need to do binary search
don't need to do binary search you can literally for most of the names
you can literally for most of the names find them in constant time one step and
find them in constant time one step and again it's not theoretically constant
again it's not theoretically constant because these if you only befriend
because these if you only befriend people who have H names it's going to be
people who have H names it's going to be a crazy long link list anyway so again
a crazy long link list anyway so again it really kind of depends on what the
it really kind of depends on what the nature of the data is here but this is
nature of the data is here but this is pretty close to constant time and in
pretty close to constant time and in fact how could we get even closer how
fact how could we get even closer how could we reduce the probability of
could we reduce the probability of collisions for the H's or any other
collisions for the H's or any other letters how could we avoid putting too
letters how could we avoid putting too many H names together
L okay yeah so we could add another dimension if you will but let's not add
dimension if you will but let's not add a third dimension per se but let's
a third dimension per se but let's indeed look at not just the first letter
indeed look at not just the first letter of everyone's name but the first and the
of everyone's name but the first and the second and in fact let's see if that
second and in fact let's see if that gets us a little uh further along so let
gets us a little uh further along so let me go ahead and propose if you go
me go ahead and propose if you go through the whole Harry Potter Universe
through the whole Harry Potter Universe there's actually a lot of collisions if
there's actually a lot of collisions if we keep going and so we've got the L's
we keep going and so we've got the L's here the RS the S's and so forth well
here the RS the S's and so forth well let's clean this up here Hermione
let's clean this up here Hermione originally went to the H location but
originally went to the H location but let's decrease the probability of
let's decrease the probability of collisions there and everywhere else
collisions there and everywhere else instead of putting hermion Harry and
instead of putting hermion Harry and Hagrid Al together let's go ahead and do
Hagrid Al together let's go ahead and do this instead instead of labeling these
this instead instead of labeling these buckets A through Z let's just give
buckets A through Z let's just give ourselves more buckets so in fact this
ourselves more buckets so in fact this might be H well instead of H maybe this
might be H well instead of H maybe this should be ha and then this should be HB
should be ha and then this should be HB HC HD h e HF now some of those are a
HC HD h e HF now some of those are a little nonsensical cuz I can't think of
little nonsensical cuz I can't think of names that match most of those but it's
names that match most of those but it's deterministic at least we know the
deterministic at least we know the bucket will be there which is important
bucket will be there which is important even if it's empty and now we can put
even if it's empty and now we can put Hermione here we can put Harry here but
Hermione here we can put Harry here but uhoh we didn't do this perfectly well
uhoh we didn't do this perfectly well Hagrid still Collide so let me come back
Hagrid still Collide so let me come back to you how can we reduce the probability
to you how can we reduce the probability of Harry and Hagrid
of Harry and Hagrid colliding yeah so we could look at the
colliding yeah so we could look at the third letter okay so let me try that
third letter okay so let me try that instead of ha let's look at haa ha ha C
instead of ha let's look at haa ha ha C dot dot dot haq dot dot dot h h e q h e
dot dot dot haq dot dot dot h h e q h e r hes and so forth and now I think those
r hes and so forth and now I think those names and probably all the others we saw
names and probably all the others we saw are now much more cleanly distributed
are now much more cleanly distributed there's much lower probability of
there's much lower probability of collisions unless two people have like
collisions unless two people have like almost the same names or one is like a
almost the same names or one is like a prefix of the other but but but even
prefix of the other but but but even though we're now closer than ever to
though we're now closer than ever to constant time because the odds that we
constant time because the odds that we hit a collision and have to devolve to a
hit a collision and have to devolve to a link list or much lower what's the
link list or much lower what's the downside that's not completely obvious
downside that's not completely obvious from how I've depicted this on
from how I've depicted this on screen what's the price I'm paying here
screen what's the price I'm paying here yeah this is a huge amount of memory the
yeah this is a huge amount of memory the number of cells here in the array is now
number of cells here in the array is now what 26 * 26 * 26 for the first the
what 26 * 26 * 26 for the first the second and the third possible characters
second and the third possible characters all combinatorically combined here
all combinatorically combined here that's a lot I didn't even draw them I
that's a lot I didn't even draw them I have the dot dot dot to evoke that
have the dot dot dot to evoke that instead that's a huge amount of memory
instead that's a huge amount of memory this is a very sparse data set now and
this is a very sparse data set now and odds are you're going to waste so much
odds are you're going to waste so much memory even for the names like Hae ha
memory even for the names like Hae ha like HQ like I can't even think of names
like HQ like I can't even think of names so many of those buckets are going to be
so many of those buckets are going to be empty not to mention the AAA and the zzz
empty not to mention the AAA and the zzz and everything else in between so it's a
and everything else in between so it's a trade-off and it might be too expensive
trade-off and it might be too expensive a trade-off and so you might have to
a trade-off and so you might have to tolerate something like the collisions
tolerate something like the collisions we had earlier whereby even though they
we had earlier whereby even though they might very well happen at least you uh
might very well happen at least you uh are decreasing the probability by
are decreasing the probability by perhaps having more buckets like this
perhaps having more buckets like this and in fact if I rewind now to where we
and in fact if I rewind now to where we might have gone with this here here's
might have gone with this here here's how we might represent these nodes in
how we might represent these nodes in the tree previously in the past we've
the tree previously in the past we've had a person who had a string name and a
had a person who had a string name and a string number AKA now Char star and so
string number AKA now Char star and so here now might be how in this hash table
here now might be how in this hash table we represent someone's name and number
we represent someone's name and number as well as a pointer to the next element
as well as a pointer to the next element in the list we let me rewind just to the
in the list we let me rewind just to the picture here we keep drawing different
picture here we keep drawing different shapes because again these are
shapes because again these are abstractions who really cares if they're
abstractions who really cares if they're to scale now we've got enough room for
to scale now we've got enough room for the person's name not pictured on the
the person's name not pictured on the screen is herm's number that's somewhere
screen is herm's number that's somewhere in this rectangle but yes pictured here
in this rectangle but yes pictured here in this little square is a pointer to
in this little square is a pointer to the next node in the list so by storing
the next node in the list so by storing name and number maybe her address maybe
name and number maybe her address maybe her mailing address whatever in addition
her mailing address whatever in addition to a pointer allows each of these nodes
to a pointer allows each of these nodes to be connectable just like the nodes in
to be connectable just like the nodes in a linked list but where they're starting
a linked list but where they're starting is in an array so the array gets us like
is in an array so the array gets us like 126 of or gets us um Narrows the problem
126 of or gets us um Narrows the problem from size 26 to one gets us to the link
from size 26 to one gets us to the link list in question hopefully it's a single
list in question hopefully it's a single person or perhaps it has more than that
person or perhaps it has more than that meanwhile what is the hash table itself
meanwhile what is the hash table itself the hash table the whole thing is
the hash table the whole thing is literally just an array I've hardcoded
literally just an array I've hardcoded the simplest version of size 26 but what
the simplest version of size 26 but what do each of those boxes in the vertical
do each of those boxes in the vertical array represent just a pointer to
array represent just a pointer to potentially a node a node in a length
potentially a node a node in a length list and if there's no one there if
list and if there's no one there if there's no one in location y or X or the
there's no one in location y or X or the like in that Universe well it's just a
like in that Universe well it's just a null pointer signifying there's no uh
null pointer signifying there's no uh one there but if there is it's going to
one there but if there is it's going to be a pointer to a valid node from which
be a pointer to a valid node from which we can get to any of the others as well
we can get to any of the others as well and that so-called hash function just
and that so-called hash function just like the one I did with the greeting
like the one I did with the greeting cards well it's just an a black box if
cards well it's just an a black box if you will but implemented somewhere in
you will but implemented somewhere in code like in C and so if you pass in
code like in C and so if you pass in Albus what is the hash value of Albus
Albus what is the hash value of Albus well in the first version of the story
well in the first version of the story with 26 buckets it should be a zero if
with 26 buckets it should be a zero if you pass in Zacharias it should be 25
you pass in Zacharias it should be 25 and so just as my cards were being
and so just as my cards were being hashed to one of 1 2 3 four values now
hashed to one of 1 2 3 four values now these names are being hashed to one of
these names are being hashed to one of 26 possibilities or 26 time 26 or 26 to
26 possibilities or 26 time 26 or 26 to the 3 power if you have more and more
the 3 power if you have more and more granularity than
granularity than that questions on this implementation
that questions on this implementation now of this idea of a hash
table if uh if you say that again with the
[Music] null oh a good question so if there's so
null oh a good question so if there's so much spareness there's all of these
much spareness there's all of these empty cells in the array couldn't you
empty cells in the array couldn't you just go in and free them or delete them
just go in and free them or delete them or just kind of shrink the array and not
or just kind of shrink the array and not have a a AA and aab and a a only have
have a a AA and aab and a a only have the prefixes two or three characters
the prefixes two or three characters that you need you absolutely could do
that you need you absolutely could do that but now what you you lose is the
that but now what you you lose is the arithmetic benefit of being able to map
arithmetic benefit of being able to map each letter to a number if you start uh
each letter to a number if you start uh freeing up unused space you don't know
freeing up unused space you don't know that Zacharias is necessarily at
that Zacharias is necessarily at location 25 Albus is still going to be
location 25 Albus is still going to be at location zero but if you've deleted
at location zero but if you've deleted some of the elements in the middle
some of the elements in the middle Zacharias could be at 24 if you've
Zacharias could be at 24 if you've deleted one 23 if you've deleted another
deleted one 23 if you've deleted another and so you don't have that arithmetic
and so you don't have that arithmetic immediacy that you need in order to
immediacy that you need in order to index into the array with constant time
index into the array with constant time and the same is going to be true if it's
and the same is going to be true if it's uh two letters or three letters you need
uh two letters or three letters you need to be able to trust that you can do some
to be able to trust that you can do some quick math and jump to the right index
quick math and jump to the right index in constant time and that's again the
in constant time and that's again the appeal of these arrays so when it comes
appeal of these arrays so when it comes to the running time of a hash table
to the running time of a hash table inserting values into it searching for
inserting values into it searching for values therein at the end of the day it
values therein at the end of the day it technically is Big O of n because in the
technically is Big O of n because in the craziest case you might have a huge
craziest case you might have a huge fancy hash table but everyone in the
fancy hash table but everyone in the universe has a name starting with h and
universe has a name starting with h and then it just evolves into a really long
then it just evolves into a really long link list just like a binary search tree
link list just like a binary search tree could do the same but if you choose a
could do the same but if you choose a smarter hash function maybe you mitigate
smarter hash function maybe you mitigate that and you don't rely only on the
that and you don't rely only on the first letter but on the second or the
first letter but on the second or the third as well or some other combination
third as well or some other combination of that input and make your hash
of that input and make your hash function smarter odds are if you get a
function smarter odds are if you get a good hash function you want it get it to
good hash function you want it get it to be more of like order of n divided by K
be more of like order of n divided by K where K means constant mathematically
where K means constant mathematically and so K is the number of buckets so
and so K is the number of buckets so ideally you want like a uniform
ideally you want like a uniform distribution you want like this many
distribution you want like this many people here this many people here you
people here this many people here you don't want there to be some or no people
don't want there to be some or no people you want a uniform statistical
you want a uniform statistical distribution and maybe you get that from
distribution and maybe you get that from Human names maybe you don't but that's
Human names maybe you don't but that's kind of the challenge of a hash function
kind of the challenge of a hash function of course Big O of n over K is not a
of course Big O of n over K is not a thing because we always throw away
thing because we always throw away constants like K so it's still Big O of
constants like K so it's still Big O of n but again the distinction today is
n but again the distinction today is that okay yes academically you learned
that okay yes academically you learned in cs50 that sure it's Big O ofin but my
in cs50 that sure it's Big O ofin but my God it's 26 times faster if you do the
God it's 26 times faster if you do the hash function well and you spread
hash function well and you spread everyone out over the hash table and
everyone out over the hash table and that's the appeal of these kinds of
that's the appeal of these kinds of structures
structures and we've got one more for you if I may
and we've got one more for you if I may something now known as a try so it turns
something now known as a try so it turns out that a try is even cooler if you
out that a try is even cooler if you like this kind of thing in that it does
like this kind of thing in that it does not devolve into Big O of n it is truly
not devolve into Big O of n it is truly constant time but there's going to be a
constant time but there's going to be a price there's going to be a gotcha a try
price there's going to be a gotcha a try is sort of a fancier tree and it's short
is sort of a fancier tree and it's short for retrieval but pronounced try for
for retrieval but pronounced try for weird historical reasons but a try is a
weird historical reasons but a try is a tree each of whose nodes is an array
tree each of whose nodes is an array right so this is all like crazy mashups
right so this is all like crazy mashups now people started inventing data
now people started inventing data structures just by combining different
structures just by combining different ones unfortunately a lot of the good
ones unfortunately a lot of the good ideas are taken but you just have
ideas are taken but you just have benefits from certain aspects of those
benefits from certain aspects of those data structures and combining them
data structures and combining them ideally gives you the best of both
ideally gives you the best of both worlds so to speak so here might be the
worlds so to speak so here might be the root of a try it's literally a big node
root of a try it's literally a big node a big rectangle but it's actually an
a big rectangle but it's actually an array so there's like 26 locations in
array so there's like 26 locations in this picture here and here's how you use
this picture here and here's how you use a try for instance to store names just
a try for instance to store names just like the hash table it you treat each of
like the hash table it you treat each of the elements of that array in that node
the elements of that array in that node as like a letter of the alphabet so a
as like a letter of the alphabet so a through z or 0 through 25 and if you
through z or 0 through 25 and if you want to store someone's name in here you
want to store someone's name in here you do so as follows if you want to store
do so as follows if you want to store like uh an H you index into the H
like uh an H you index into the H location and if you want to store the
location and if you want to store the second letter of someone's name like an
second letter of someone's name like an a well you add another node below it and
a well you add another node below it and such one is connected to the other and
such one is connected to the other and you then identify the a in that array
you then identify the a in that array and then you go on and maybe put a g if
and then you go on and maybe put a g if the goal is to store spoiler now Hagrid
the goal is to store spoiler now Hagrid in this data structure and then the r
in this data structure and then the r and the I and then the D but when you
and the I and then the D but when you get to the D the end of the name you
get to the D the end of the name you have to somehow flag that this is the
have to somehow flag that this is the end of a name that we've embedded into
end of a name that we've embedded into this data structure so whereas all of
this data structure so whereas all of these are called out in white just to
these are called out in white just to make obvious what we're connecting to
make obvious what we're connecting to what green has to be like a bull that's
what green has to be like a bull that's true that just indicates like the buck
true that just indicates like the buck stops here like d is the last letter in
stops here like d is the last letter in someone's actual name and what's kind of
someone's actual name and what's kind of cool now about a try is that we can
cool now about a try is that we can repeat this for other names as well so
repeat this for other names as well so for instance here is where we might put
for instance here is where we might put Harry as well and notice they share a
Harry as well and notice they share a common prefix ha for Hagrid ha for Harry
common prefix ha for Hagrid ha for Harry so we're reusing some of these nodes
so we're reusing some of these nodes some of these arrays we can even slip
some of these arrays we can even slip Hermione in here too borrowing only the
Hermione in here too borrowing only the H but she gets the H then the E then r r
H but she gets the H then the E then r r m i o n e and so forth and we Mark at
m i o n e and so forth and we Mark at the end of her name too that she's in
the end of her name too that she's in there now what's the takeaway here well
there now what's the takeaway here well what is the running time of a try how
what is the running time of a try how many steps does it take to find someone
many steps does it take to find someone in this data structure and let me zoom
in this data structure and let me zoom out so that it sort of suddenly becomes
out so that it sort of suddenly becomes a massive data structure with even more
a massive data structure with even more in it uh maybe it looks sorry no I'll
in it uh maybe it looks sorry no I'll keep it on this one maybe it looks a
keep it on this one maybe it looks a little something like this with just
little something like this with just these three names but how many steps
these three names but how many steps does it take to find Hagrid or Harry or
does it take to find Hagrid or Harry or Hermione no matter how many names are in
Hermione no matter how many names are in this data structure there's three at the
this data structure there's three at the moment but it takes what h a g r i d so
moment but it takes what h a g r i d so six steps to find Hagrid uh h a r r y
six steps to find Hagrid uh h a r r y five steps to find Harry h e r m i o n e
five steps to find Harry h e r m i o n e eight steps to find Hermione but notice
eight steps to find Hermione but notice that those steps are only dependent on
that those steps are only dependent on what the lengths of the human's names
what the lengths of the human's names and let's assume that no one's going to
and let's assume that no one's going to have a infinitely long name it's going
have a infinitely long name it's going to max out at what like eight no maybe
to max out at what like eight no maybe eight 18 maybe 20 30 there's actually
eight 18 maybe 20 30 there's actually some pretty long human names out there
some pretty long human names out there but it's going to be finite you know
but it's going to be finite you know it's abounded whereas most contexts n
it's abounded whereas most contexts n could grow forever so what's compelling
could grow forever so what's compelling here is if you assume that the longest
here is if you assume that the longest name is I don't know 50 for the sake of
name is I don't know 50 for the sake of a theme here then you know that finding
a theme here then you know that finding anyone in this data structure will take
anyone in this data structure will take you no more than 50 steps 50 is thus a
you no more than 50 steps 50 is thus a constant which means you have big O of
constant which means you have big O of one running time it doesn't matter if
one running time it doesn't matter if there's a million people in this phone
there's a million people in this phone book or a billion people in this phone
book or a billion people in this phone book that's going to definitely add more
book that's going to definitely add more nodes to it but it's still going to take
nodes to it but it's still going to take you h a r uh sorry h a g r i d six steps
you h a r uh sorry h a g r i d six steps to find Hagrid h a r r y five steps to
to find Hagrid h a r r y five steps to find Harry even if there's a billion
find Harry even if there's a billion other people in that data structure so
other people in that data structure so now we actually do seem to have constant
now we actually do seem to have constant time if you assume that there's going to
time if you assume that there's going to be a bound on the length of the name why
be a bound on the length of the name why don't we use tries for everything then
don't we use tries for everything then what's the price we're paying for this
what's the price we're paying for this data structure even though we've
data structure even though we've represented just three characters here
represented just three characters here yeah it's a lot of memory yeah and you
yeah it's a lot of memory yeah and you can see it even with these three names
can see it even with these three names most of the squares on the screen are
most of the squares on the screen are empty like bites and bits that are there
empty like bites and bits that are there and are allocated and they need to be
and are allocated and they need to be there because you need to be able to do
there because you need to be able to do that arithmetic thing of this being zero
that arithmetic thing of this being zero this being 25 so you can jump from boom
this being 25 so you can jump from boom boom boom boom based on each of the
boom boom boom based on each of the letters but it's a hugely sparse data
letters but it's a hugely sparse data structure which means it takes up a
structure which means it takes up a crazy amount of memory now maybe that's
crazy amount of memory now maybe that's tolerable especially for short names but
tolerable especially for short names but that's going to be the trade-off as well
that's going to be the trade-off as well and this is such a tension in Computing
and this is such a tension in Computing almost any time you want to improve time
almost any time you want to improve time you want to speed up the efficiency the
you want to speed up the efficiency the speed of your algorithm you're going to
speed of your algorithm you're going to spend space if by contrast you want to
spend space if by contrast you want to decrease the amount of space you might
decrease the amount of space you might very well have to increase the running
very well have to increase the running time it is indeed this seesaw back and
time it is indeed this seesaw back and forth and you your colleagues your
forth and you your colleagues your company need to decide what resource is
company need to decide what resource is the most precious heck it might be much
the most precious heck it might be much harder to code one of these data
harder to code one of these data structures than another you're a human
structures than another you're a human your time is valuable do you really want
your time is valuable do you really want to spend hours implementing a try when
to spend hours implementing a try when you know hey in 30 minutes I can bang
you know hey in 30 minutes I can bang out an array nowadays or a linked list
out an array nowadays or a linked list even there too development time is going
even there too development time is going to be yet another resource and why
to be yet another resource and why sometimes there's good code or bad code
sometimes there's good code or bad code it depends on what you're prioritizing
it depends on what you're prioritizing so what do each of these nodes look like
so what do each of these nodes look like in a try well we can keep calling it a
in a try well we can keep calling it a node this is a very generic term for
node this is a very generic term for just a container in these data
just a container in these data structures in this story though let me
structures in this story though let me claim that everyone has a number like a
claim that everyone has a number like a phone number a string akhr star every
phone number a string akhr star every every node has 26 children or
every node has 26 children or technically an array of size 26 that can
technically an array of size 26 that can point to more of these nodes notice that
point to more of these nodes notice that I don't need to store the name of
I don't need to store the name of someone in a try because it's implicit
someone in a try because it's implicit in the path that you take to find them
in the path that you take to find them so that's a minor optimization but it
so that's a minor optimization but it saves us some space but this would be
saves us some space but this would be just a different data structure we could
just a different data structure we could use to actually solve this problem as
use to actually solve this problem as well albeit at a very expensive cost and
well albeit at a very expensive cost and what do we need our variable to be that
what do we need our variable to be that stores the try just like four we just
stores the try just like four we just need a single pointer that hangs on to
need a single pointer that hangs on to the root of this structure that's null
the root of this structure that's null if it's empty or non-null if it's
if it's empty or non-null if it's actually pointing at
actually pointing at something any questions then on
something any questions then on tries and if it's feeling like a lot the
tries and if it's feeling like a lot the fire the the fire hydrant it is we
fire the the fire hydrant it is we started with arrays then link list then
started with arrays then link list then tries but questions on how we've just
tries but questions on how we've just assembled from these basic building
assembled from these basic building blocks
blocks [Music]
[Music] yeah a good question why is this not uh
yeah a good question why is this not uh size 26 uh it's just like with the try
size 26 uh it's just like with the try just like with the link list before it
just like with the link list before it just tends to be en code convenient to
just tends to be en code convenient to have a separate additional pointer
have a separate additional pointer that's small that just points to the
that's small that just points to the beginning of the data structure because
beginning of the data structure because that way it can be null thereby clearly
that way it can be null thereby clearly indicating there are no nodes the whole
indicating there are no nodes the whole structure is empty if you allocated one
structure is empty if you allocated one of those nodes you absolutely could but
of those nodes you absolutely could but then you'd be just wasting space even if
then you'd be just wasting space even if it's empty and it creates an ambiguity
it's empty and it creates an ambiguity so just having a single pointer linked
so just having a single pointer linked to the beginnings of all of these things
to the beginnings of all of these things is a good thing other questions now on
is a good thing other questions now on tries or trees or hash tables or
tries or trees or hash tables or arrays so what problems might arise well
arrays so what problems might arise well here's a counter example what names are
here's a counter example what names are manifest in this try
out what do you see Danielle and Danielle so presumably
see Danielle and Danielle so presumably if these are two uh names here one of
if these are two uh names here one of which is a prefix of another notice that
which is a prefix of another notice that the data structure still works and I
the data structure still works and I chose you know a friend's name and then
chose you know a friend's name and then appended a couple of more characters to
appended a couple of more characters to it that's also a name because we have
it that's also a name because we have here d a n i e l and the green
here d a n i e l and the green technically is implemented as a bull or
technically is implemented as a bull or something like that that indicates a
something like that that indicates a word stops here but we don't want to
word stops here but we don't want to preclude storing Danielle as well who's
preclude storing Danielle as well who's a super string if you will of Daniel and
a super string if you will of Daniel and so that's okay too so long as the
so that's okay too so long as the structure allows for the pointers to
structure allows for the pointers to keep going so even that works out okay
keep going so even that works out okay whereas it might not have otherwise and
whereas it might not have otherwise and in terms of the running time just to be
in terms of the running time just to be clear at the end of the day tries do
clear at the end of the day tries do give you actual constant time for
give you actual constant time for insertions lookups deletions and the
insertions lookups deletions and the like because it's dependent only on the
like because it's dependent only on the length of the input the key if you will
length of the input the key if you will and not on how many other people are in
and not on how many other people are in your phone or a dress book and now
your phone or a dress book and now thought we'd conclude with a visual if
thought we'd conclude with a visual if you've gotten out into the square anyone
you've gotten out into the square anyone recognize this okay okay sweet green a
recognize this okay okay sweet green a local salad place what are we looking at
local salad place what are we looking at here and what's its connection to today
here and what's its connection to today um you're about to become all the
um you're about to become all the geekier in the real world cuz you will
geekier in the real world cuz you will start to see data structures
start to see data structures everywhere what is this or how does this
everywhere what is this or how does this work maybe in salad form who's been to
work maybe in salad form who's been to Sweet
Sweet green okay either of you so how does
green okay either of you so how does this
this [Music]
work okay good so if you order a salad for someone named L when it's ready they
for someone named L when it's ready they put it in the l section here and so this
put it in the l section here and so this is kind of a set of key value pairs
is kind of a set of key value pairs right if L is the first letter of
right if L is the first letter of someone's name the value hopefully is
someone's name the value hopefully is the salad and so what you kind of have
the salad and so what you kind of have here is a dictionary key value pairs
here is a dictionary key value pairs where it's not words and definitions
where it's not words and definitions it's names and salads and you can think
it's names and salads and you can think of this too as kind of a hash table why
of this too as kind of a hash table why even though it actually doesn't fit on
even though it actually doesn't fit on one long shelf cu the store is only so
one long shelf cu the store is only so big this is really an array and
big this is really an array and apparently a is missing or maybe it's
apparently a is missing or maybe it's around around the corner but this array
around around the corner but this array just happens to wrap onto multiple lines
just happens to wrap onto multiple lines but it's still conceptually a single
but it's still conceptually a single Dimension but suppose two people have
Dimension but suppose two people have the name L what do they do
typically yeah so maybe they they well if they run put that much effort into it
if they run put that much effort into it they might look at the second letter and
they might look at the second letter and then the third letter odds are this is
then the third letter odds are this is not that interesting a problem to solve
not that interesting a problem to solve optimally in that way but they probably
optimally in that way but they probably do start stacking the salads on top of
do start stacking the salads on top of each other maybe scooching it over just
each other maybe scooching it over just a little bit and so what do you have
a little bit and so what do you have there well now you start to view the
there well now you start to view the lens through like cs50 glasses like okay
lens through like cs50 glasses like okay you have an array and then you have like
you have an array and then you have like these link lists that are sort of
these link lists that are sort of growing here but even then you run into
growing here but even then you run into a problem why because it's not really a
a problem why because it's not really a link list because at some point you're
link list because at some point you're going to hit the boundary here so it's
going to hit the boundary here so it's kind of like an array of arrays because
kind of like an array of arrays because you can only fit what like three or four
you can only fit what like three or four salads here and so long story short we
salads here and so long story short we started today deliberately talking about
started today deliberately talking about real world things like stacks and cues
real world things like stacks and cues and even though it did escalate quickly
and even though it did escalate quickly into binary search trees and hash tables
into binary search trees and hash tables and tries even those things are
and tries even those things are everywhere even though they don't call
everywhere even though they don't call them as such these are just solutions to
them as such these are just solutions to problems and now with this final week of
problems and now with this final week of SE under your belt you have all the more
SE under your belt you have all the more of a technical toolkit via which to
of a technical toolkit via which to implement these things and code next
implement these things and code next week we'll be able to trust that someone
week we'll be able to trust that someone else solved all these problems we'll
else solved all these problems we'll introduce Python and lines of code like
introduce Python and lines of code like this will finally become lines of code
this will finally become lines of code like that so that's the promise ahead
like that so that's the promise ahead and we'll see you next
and we'll see you next [Applause]
[Applause] [Music]
[Applause] [Music]
all right this is cs50 and this is week six wherein we finally transitioned from
six wherein we finally transitioned from scratch to see to now Python and indeed
scratch to see to now Python and indeed this is going to be somewhat of a unique
this is going to be somewhat of a unique experience in that just like a few weeks
experience in that just like a few weeks past perhaps for the first time and now
past perhaps for the first time and now uh today you're going to learn a new
uh today you're going to learn a new language but the goal isn't just to
language but the goal isn't just to throw another fire hose of content and
throw another fire hose of content and syntax and whatnot at you but rather to
syntax and whatnot at you but rather to really equip you all to actually teach
really equip you all to actually teach yourself new languages in the future and
yourself new languages in the future and so indeed what we'll do today what we'll
so indeed what we'll do today what we'll do this coming week is sort of prepare
do this coming week is sort of prepare you to stand on your own and once python
you to stand on your own and once python is p a and the world has moved on to
is p a and the world has moved on to some other language in some number of
some other language in some number of years you'll be well equipped to figure
years you'll be well equipped to figure out how to wrap your mind around some
out how to wrap your mind around some new syntax some new language and solve
new syntax some new language and solve problems as well now you recall in week
problems as well now you recall in week zero this is where we started just
zero this is where we started just saying hello to the world and that
saying hello to the world and that quickly escalated just a week later in C
quickly escalated just a week later in C to see be something much much more
to see be something much much more cryptic and if you've still sort of
cryptic and if you've still sort of struggled with some of the syntax find
struggled with some of the syntax find yourself checking your notes or your
yourself checking your notes or your previous code like that's totally normal
previous code like that's totally normal and that's one of the reasons why there
and that's one of the reasons why there are languages besides C out there among
are languages besides C out there among them this language called python humans
them this language called python humans over the decades have realized gee that
over the decades have realized gee that wasn't necessarily the best designed
wasn't necessarily the best designed decision or humans have realized wow you
decision or humans have realized wow you know what now that computers have gotten
know what now that computers have gotten faster with more memory and more faster
faster with more memory and more faster CPUs we can actually do more with our
CPUs we can actually do more with our programming languages so just as human
programming languages so just as human languages evolve so do actual
languages evolve so do actual programming languages and even within a
programming languages and even within a programming language there's typically
programming language there's typically different versions We for instance have
different versions We for instance have been using version C11 of C which was
been using version C11 of C which was updated in 2011 um but python itself
updated in 2011 um but python itself continues to evolve and it's now up to
continues to evolve and it's now up to version three plus and so there too
version three plus and so there too these things will evolve in the coming
these things will evolve in the coming days thankfully what you're about to see
days thankfully what you're about to see is hello world for the third time but
is hello world for the third time but it's going to be literally this none of
it's going to be literally this none of the crazy syntax above or below fewer
the crazy syntax above or below fewer semicolons if any fewer curly braces and
semicolons if any fewer curly braces and really a lot of the distractions get out
really a lot of the distractions get out of the way so to get there let's
of the way so to get there let's consider exactly how we've been
consider exactly how we've been programming up until now so you write a
programming up until now so you write a program in C and you've got hopefully no
program in C and you've got hopefully no syntax error so you're ready to build it
syntax error so you're ready to build it that is compil it and so you've run make
that is compil it and so you've run make and then you've run the program like/
and then you've run the program like/ hello or if you think back to week two
hello or if you think back to week two where we took a peak underneath the hood
where we took a peak underneath the hood of what make is doing it's really
of what make is doing it's really running the actual compiler something
running the actual compiler something called clang maybe with some command
called clang maybe with some command line arguments creating a program called
line arguments creating a program called hello and then you could do do/ hello so
hello and then you could do do/ hello so today you're going to start doing
today you're going to start doing something similar in spirit but fewer
something similar in spirit but fewer steps no longer will you have to compile
steps no longer will you have to compile your code and then run it and then maybe
your code and then run it and then maybe fix or change it and then compile your
fix or change it and then compile your code and run it and then repeat repeat
code and run it and then repeat repeat the process of running your code is
the process of running your code is going to be distilled into just a single
going to be distilled into just a single step and the way to think of this for
step and the way to think of this for now is that where a c is frequently used
now is that where a c is frequently used as indeed a compiled language whereby
as indeed a compiled language whereby you convert it first to zeros and ones
you convert it first to zeros and ones Python's going to let you speed things
Python's going to let you speed things up whereby you the human programmer
up whereby you the human programmer don't have to compile it you're just
don't have to compile it you're just going to run what's called an
going to run what's called an interpreter which by Design is named the
interpreter which by Design is named the exact same thing as the language itself
exact same thing as the language itself and by running this program installed in
and by running this program installed in VSS code or eventually on your own Max
VSS code or eventually on your own Max or PCS this is just going to tell your
or PCS this is just going to tell your computer to interpret this code and
computer to interpret this code and figure out how to get down to that lower
figure out how to get down to that lower level of zeros and ones but you don't
level of zeros and ones but you don't have to compile the code yourself
have to compile the code yourself anymore so with that said let's consider
anymore so with that said let's consider what the code is going to look like side
what the code is going to look like side by side in fact let's look back at some
by side in fact let's look back at some scratch blocks just like we did with C
scratch blocks just like we did with C in week one and do some side by sides
in week one and do some side by sides because even though some of the syntax
because even though some of the syntax this week and Beyond's going to be
this week and Beyond's going to be different like the ideas are truly going
different like the ideas are truly going to be the same there's not all that much
to be the same there's not all that much intellectually new just yet so whereas
intellectually new just yet so whereas in week zero we might have said hello to
in week zero we might have said hello to the world with this purple puzzle piece
the world with this purple puzzle piece today of course uh or rather in week one
today of course uh or rather in week one it looked like this in C but today
it looked like this in C but today moving forward it's going to quite
moving forward it's going to quite simply look like this instead and if we
simply look like this instead and if we go back and forth for just a moment here
go back and forth for just a moment here again is the version in C noticing the
again is the version in C noticing the very seike characteristics and just at a
very seike characteristics and just at a glance here in Python I claim it's now
glance here in Python I claim it's now this what do you apparently need not
this what do you apparently need not worry about
worry about anymore what's gone so semicolon is gone
anymore what's gone so semicolon is gone and indeed you don't need those to
and indeed you don't need those to finish most of your thoughts anymore
finish most of your thoughts anymore anything else so the back sln is absent
anything else so the back sln is absent and that's kind of curious because we're
and that's kind of curious because we're still going to get a new line but we'll
still going to get a new line but we'll see that it's become the default and
see that it's become the default and this one's a little more subtle but now
this one's a little more subtle but now the function is called print instead of
the function is called print instead of print F so it's a little more familiar
print F so it's a little more familiar in that sense all right so when it comes
in that sense all right so when it comes to using libraries that is code that
to using libraries that is code that other people have written in the past
other people have written in the past we've done things like hash include
we've done things like hash include cs50.h to use cs50's own header file or
cs50.h to use cs50's own header file or standard IO or standard lib or string or
standard IO or standard lib or string or any number of other header files you
any number of other header files you have all used well moving forward we're
have all used well moving forward we're going to give you for this first week a
going to give you for this first week a similar cs-50 Library just very
similar cs-50 Library just very short-term uh training wheels that will
short-term uh training wheels that will quickly take off because in reality it's
quickly take off because in reality it's a lot easier to do things in python as
a lot easier to do things in python as we'll see but the Syntax for this now is
we'll see but the Syntax for this now is going to be to import the cs50 library
going to be to import the cs50 library in this way and when we have now this
in this way and when we have now this ability we can actually start writing
ability we can actually start writing some code right away in fact let me
some code right away in fact let me switch over to vs code here and just as
switch over to vs code here and just as in the past I'll create a new file but
in the past I'll create a new file but instead of creating something called C
instead of creating something called C I'm going to go ahead and create my
I'm going to go ahead and create my first program called hello.py using Code
first program called hello.py using Code space hello.py that of course gives me
space hello.py that of course gives me this new tab and let me actually quite
this new tab and let me actually quite simply do what I proposed print quote
simply do what I proposed print quote unquote hello world without the back
unquote hello world without the back slash without the semicolon without the
slash without the semicolon without the F in print and now let me go down to my
F in print and now let me go down to my terminal window and I don't have to
terminal window and I don't have to compile it I don't have to do dot slash
compile it I don't have to do dot slash i instead run a program called python
i instead run a program called python whose purpose in life is now to
whose purpose in life is now to interpret my code top to bottom left to
interpret my code top to bottom left to right and if I run python of hello.py
right and if I run python of hello.py crossing my fingers as always voila now
crossing my fingers as always voila now I have printed out hello world so we
I have printed out hello world so we seem to have gotten the new line for
seem to have gotten the new line for free in this sense where it's
free in this sense where it's automatically happening the dollar sign
automatically happening the dollar sign isn't weirdly on the same line like it
isn't weirdly on the same line like it want was in week one but that's just a a
want was in week one but that's just a a minor detail here if we switch back to
minor detail here if we switch back to now some other capabilities well indeed
now some other capabilities well indeed with the cs50 library you can also not
with the cs50 library you can also not just import the library itself but
just import the library itself but specific functions and you'll see that
specific functions and you'll see that temporarily we're going to give you a
temporarily we're going to give you a helper function called get string just
helper function called get string just like in C that just makes it work
like in C that just makes it work exactly the same way as in C and we'll
exactly the same way as in C and we'll see a couple of other functions that
see a couple of other functions that will just make life easier initially but
will just make life easier initially but quickly will we take those training
quickly will we take those training wheels off so that nothing is indeed
wheels off so that nothing is indeed cs50 specific all right well how about
cs50 specific all right well how about functions more generally in Python let's
functions more generally in Python let's do a whirlwind tour if you will much
do a whirlwind tour if you will much like we did in that first week of C
like we did in that first week of C comparing one to the other so back in
comparing one to the other so back in our world of scratch one of the first
our world of scratch one of the first programs we wrote was this one here
programs we wrote was this one here whereby we asked the human their name we
whereby we asked the human their name we then used the return value that was sort
then used the return value that was sort of automatically stored in this answer
of automatically stored in this answer variable as a second argument to join so
variable as a second argument to join so that we could say hello David or hello
that we could say hello David or hello Carter so this was back in week one uh
Carter so this was back in week one uh week zero in week one we converted it to
week zero in week one we converted it to this and here's a perfect example of
this and here's a perfect example of things like escalating quickly and again
things like escalating quickly and again this is why we start in scratch there's
this is why we start in scratch there's just so much distraction here to achieve
just so much distraction here to achieve the same idea but even today we're going
the same idea but even today we're going to chip away at some of that syntax so
to chip away at some of that syntax so in C we had to declare the argument as a
in C we had to declare the argument as a we had to declare the variable as a
we had to declare the variable as a string here we of course had the
string here we of course had the semicolon and more well in Python the
semicolon and more well in Python the comparable code now is going to look
comparable code now is going to look more simply like this so semicolon is
more simply like this so semicolon is again gone on both lines for that matter
again gone on both lines for that matter so that's good what else appears to have
so that's good what else appears to have changed or disappeared yeah type of
changed or disappeared yeah type of variable yeah so I didn't have to
variable yeah so I didn't have to specifically say that answer is now a
specifically say that answer is now a string and indeed python is is
string and indeed python is is dynamically typed and in fact it will
dynamically typed and in fact it will infer from Context exactly what it is
infer from Context exactly what it is you are storing in that variable other
you are storing in that variable other details that seem a little bit
different little bit different what else jumps out at you here I'll go back this
jumps out at you here I'll go back this was the C version and maybe Focus now on
was the C version and maybe Focus now on the second line because we've rather
the second line because we've rather exhausted the first here's now the
exhausted the first here's now the python version what's different here
yeah yeah there's no percent s anymore there's no second argument at the moment
there's no second argument at the moment per se to print now it is still a little
per se to print now it is still a little weird it's as though I've like deployed
weird it's as though I've like deployed some addition here arithmetically but
some addition here arithmetically but that's not the case some of you have
that's not the case some of you have programmed before and plus some of you
programmed before and plus some of you might know means what in this
might know means what in this context so to combine or more
context so to combine or more technically anyone know the buzz word
technically anyone know the buzz word yeah
yeah to concatenate so to concatenate is like
to concatenate so to concatenate is like the fancy way of what scratch calls
the fancy way of what scratch calls joining which is to take one string on
joining which is to take one string on the left one string on the right and to
the left one string on the right and to join them together to glue them together
join them together to glue them together if you will so this is not addition it
if you will so this is not addition it would be if it were numbers involved
would be if it were numbers involved instead but because we've got a string
instead but because we've got a string hello comma and another string
hello comma and another string implicitly in this variable based on
implicitly in this variable based on what the human typed in in response to
what the human typed in in response to this get string function that's going to
this get string function that's going to concatenate hello comma space and then
concatenate hello comma space and then David or Carter or whatever the human
David or Carter or whatever the human has typed in but turns out there's going
has typed in but turns out there's going to be different ways to do this in
to be different ways to do this in Python and we'll show you a few
Python and we'll show you a few different ones and here too try not to
different ones and here too try not to get too hung up on or frustrated by like
get too hung up on or frustrated by like all of the different ways you can solve
all of the different ways you can solve problems odds are you're going to be
problems odds are you're going to be picking up tips and techniques for years
picking up tips and techniques for years to come if you continue programming so
to come if you continue programming so let's just give you a few of the
let's just give you a few of the possible ways so here's a second way you
possible ways so here's a second way you could print out hello comma David or
could print out hello comma David or hello comma Carter but what has changed
hello comma Carter but what has changed in the previous version I used
in the previous version I used concatenation explicitly and the space
concatenation explicitly and the space here is important grammatically just so
here is important grammatically just so we get that in the final phrase
we get that in the final phrase now I'm proposing to get rid of that
now I'm proposing to get rid of that space to add a comma outside of the
space to add a comma outside of the double quotes as well but if you think
double quotes as well but if you think back to C this probably just means that
back to C this probably just means that print similar in spirit to print F can
print similar in spirit to print F can take not just one argument but even two
take not just one argument but even two and in fact because of this comma in the
and in fact because of this comma in the middle that's outside of the double
middle that's outside of the double quotes it's hello comma and then it will
quotes it's hello comma and then it will be automatically concatenated with even
be automatically concatenated with even without using the plus to whatever the
without using the plus to whatever the value of answer is and by default just
value of answer is and by default just for grammatical prettiness the print fun
for grammatical prettiness the print fun function always gives you a space for
function always gives you a space for free in between each of the multiple
free in between each of the multiple arguments you pass in we'll see how you
arguments you pass in we'll see how you can override that down the line but for
can override that down the line but for now that's just another way to do it now
now that's just another way to do it now perhaps the better if slightly cryptic
perhaps the better if slightly cryptic way to do this or just the increasingly
way to do this or just the increasingly common way is probably this third
common way is probably this third version which looks a little weird too
version which looks a little weird too and probably the weirdness jumps out
and probably the weirdness jumps out we've automatic we've suddenly
we've automatic we've suddenly introduced these like Curly braces which
introduced these like Curly braces which I promised were mostly gone and they are
I promised were mostly gone and they are but inside of this string here I've done
but inside of this string here I've done a curly brace which might mean what just
a curly brace which might mean what just intuitively and here is sort of an
intuitively and here is sort of an example of how you learn a new language
example of how you learn a new language just kind of infer from Context how
just kind of infer from Context how python probably
python probably works what might this mean yeah able to
works what might this mean yeah able to tell that this is not one of the actual
tell that this is not one of the actual stat
stat inside yeah so this is an indication
inside yeah so this is an indication because the curly braces because this is
because the curly braces because this is the way python was designed that we want
the way python was designed that we want to plug in the value of answer not
to plug in the value of answer not literally a ANS w r and the fancy word
literally a ANS w r and the fancy word here is that the answer variable will be
here is that the answer variable will be interpolated that is substituted with
interpolated that is substituted with its actual value but but but and this is
its actual value but but but and this is actually weird looking and this was
actually weird looking and this was introduced a few years ago to python
introduced a few years ago to python what else did I have to change to make
what else did I have to change to make these curly braces work
these curly braces work apparently
apparently yeah yeah there's this weird F and so
yeah yeah there's this weird F and so it's sort of like part of print F but
it's sort of like part of print F but now it's inside the curly it's inside
now it's inside the curly it's inside the parenthesis there this is just the
the parenthesis there this is just the way python designed this so a few years
way python designed this so a few years ago when they introduced what are called
ago when they introduced what are called format strings or F strings you
format strings or F strings you literally prefix your quoted string with
literally prefix your quoted string with the uh letter F and then you can use
the uh letter F and then you can use trickery like this like putting curly
trickery like this like putting curly braces so that the value will be
braces so that the value will be substituted automatically if you forget
substituted automatically if you forget the F you're going to literally see
the F you're going to literally see hello comma curly brace answer close
hello comma curly brace answer close curly brace if you add the F it's indeed
curly brace if you add the F it's indeed interpolated the value is plugged in all
interpolated the value is plugged in all right questions on how we can just say
right questions on how we can just say hello to the world via python in this
hello to the world via python in this case
case yeah if you do this without the without
yeah if you do this without the without the F if you omit the F you will
the F if you omit the F you will literally see c h e l l o comma curly
literally see c h e l l o comma curly brace a NS w r close curly brace so in
brace a NS w r close curly brace so in fact let's do this let me go back to vs
fact let's do this let me go back to vs code here quickly I've still got my file
code here quickly I've still got my file called hello.py open and let me go ahead
called hello.py open and let me go ahead and change this ever so slightly so I'm
and change this ever so slightly so I'm going to go ahead and uh let's say from
going to go ahead and uh let's say from cs50 import get string and that's just
cs50 import get string and that's just the new syntax I propose using to import
the new syntax I propose using to import a function from someone else's Library
a function from someone else's Library I'm going to now go ahead and ask the
I'm going to now go ahead and ask the question uh let's go ahead and use get
question uh let's go ahead and use get string storing the result and answer so
string storing the result and answer so get string quote unquote what's your
get string quote unquote what's your name question mark and then on this line
name question mark and then on this line I'm going to deliberately make a mistake
I'm going to deliberately make a mistake here exactly to your question let me
here exactly to your question let me just say hello comma answer and just
just say hello comma answer and just this now even though answer is a
this now even though answer is a variable Python's not going to be so
variable Python's not going to be so presumptuous as to just plug in the
presumptuous as to just plug in the value of a variable called answer what
value of a variable called answer what it's going to do of course is if I type
it's going to do of course is if I type in my name whoops I typed too fast let
in my name whoops I typed too fast let me go ahead and rerun that again if I
me go ahead and rerun that again if I run python of hello.py type in my name
run python of hello.py type in my name and hit enter I get hello comma answer
and hit enter I get hello comma answer well let me do one better let me apply
well let me do one better let me apply these curly braces as before let me
these curly braces as before let me rerun python of hello.py What's Your
rerun python of hello.py What's Your Name daav ID and here's again the answer
Name daav ID and here's again the answer to your question now we get literally
to your question now we get literally the curly braces so the fix here
the curly braces so the fix here ultimately is just going to be to add
ultimately is just going to be to add the F there rerun my program again with
the F there rerun my program again with daav ID and now hello comma David so
daav ID and now hello comma David so this is admittedly a little more cryptic
this is admittedly a little more cryptic than the ones with the Plus the comma
than the ones with the Plus the comma but this is just increasingly common why
but this is just increasingly common why because you can read it left to right
because you can read it left to right it's nice and convenient it's less
it's nice and convenient it's less cryptic than the percent s's so it's
cryptic than the percent s's so it's sort of a new and improved version if
sort of a new and improved version if you will of printf in C based on Decades
you will of printf in C based on Decades of experience of programmers doing
of experience of programmers doing things like this questions on printing
things like this questions on printing in this
in this way we're now on our way to programming
way we're now on our way to programming in
in Python anything all right well what more
Python anything all right well what more can we do with this language here well
can we do with this language here well let me propose that we consider that we
let me propose that we consider that we have for
have for instance a few other features that we
instance a few other features that we can add to the mix as well
can add to the mix as well namely let's say some data types as well
namely let's say some data types as well so let me flip over here to um back to
so let me flip over here to um back to the slides and there's different data
the slides and there's different data types in python as we'll soon see but
types in python as we'll soon see but they're not as explicit as we already
they're not as explicit as we already saw by using a string from get string
saw by using a string from get string you don't have to explicitly State what
you don't have to explicitly State what it is but you solve recall and see all
it is but you solve recall and see all of these various data types and then in
of these various data types and then in python kind of nicely enough this list
python kind of nicely enough this list is about to get shorter and so here is
is about to get shorter and so here is our list in C here is an abbreviated
our list in C here is an abbreviated list in Python so we're still going to
list in Python so we're still going to have strings but they're going to be
have strings but they're going to be more succinctly called stirs now St Str
more succinctly called stirs now St Str we're still going to have in for
we're still going to have in for integers we're still going to have
integers we're still going to have floats for floating Point values we're
floats for floating Point values we're even going to have bulls for true and
even going to have bulls for true and false but what's missing now from the
false but what's missing now from the list is long and floats and why is that
list is long and floats and why is that or rather long and double well recall
or rather long and double well recall that in C those used more bits well in
that in C those used more bits well in Python the smaller data types previously
Python the smaller data types previously int and Float themselves just use more
int and Float themselves just use more bits for you and so you don't need to
bits for you and so you don't need to distinguish between small and large you
distinguish between small and large you just use one data type and the language
just use one data type and the language gives you a bigger range than before it
gives you a bigger range than before it turns out though there's going to be
turns out though there's going to be some other features as well of python
some other features as well of python these data types one of which will be
these data types one of which will be called range another of which will be
called range another of which will be list So Gone will be arrays we'll
list So Gone will be arrays we'll actually use something literally called
actually use something literally called a list tle sort of like XY pairs for
a list tle sort of like XY pairs for coordinates and things like that uh dict
coordinates and things like that uh dict for dictionaries so we have built-in
for dictionaries so we have built-in capabilities for storing keys and values
capabilities for storing keys and values we'll see and even a set sort of
we'll see and even a set sort of mathematically a set is like a
mathematically a set is like a collection of values but it
collection of values but it automatically gets rid of duplicates for
automatically gets rid of duplicates for you so all of these things we could
you so all of these things we could absolutely Implement in C if we wanted
absolutely Implement in C if we wanted and indeed in problem set five you've
and indeed in problem set five you've been implementing your very own spell
been implementing your very own spell checker using some form of hashtable
checker using some form of hashtable well it turns out that in Python you can
well it turns out that in Python you can solve those same problems but perhaps a
solve those same problems but perhaps a little more readily in fact let me go
little more readily in fact let me go back over here to vs code and let me
back over here to vs code and let me propose that I do the following let me
propose that I do the following let me go ahead and create a file called
go ahead and create a file called dictionary. let me propose that I try to
dictionary. let me propose that I try to implement say problem set five our spell
implement say problem set five our spell checker in Python instead of c and
checker in Python instead of c and Achieve ultimately the same kind of
Achieve ultimately the same kind of behavior uh whereby I'll be able to
behavior uh whereby I'll be able to spell check a whole bunch of words so
spell check a whole bunch of words so this is jumping the gun a little bit
this is jumping the gun a little bit because you're about to see syntax will
because you're about to see syntax will revisit over the course of today but for
revisit over the course of today but for now I've got a new file called
now I've got a new file called dictionary. py and let me begin to
dictionary. py and let me begin to create uh some placehold ERS for
create uh some placehold ERS for functions we'll see in just a bit that
functions we'll see in just a bit that in Python you can define a function
in Python you can define a function called check and that check function can
called check and that check function can take a word as it's input and I'll come
take a word as it's input and I'll come back to this in just a moment in Python
back to this in just a moment in Python I can define a second function like load
I can define a second function like load which itself will take a whole
which itself will take a whole dictionary just like in problem set five
dictionary just like in problem set five and I'll go ahead and come back to the
and I'll go ahead and come back to the implementation of this meanwhile we
implementation of this meanwhile we might similarly Implement a function
might similarly Implement a function called size which takes no arguments but
called size which takes no arguments but ultimately is going to return the size
ultimately is going to return the size of my dictionary of words and then last
of my dictionary of words and then last lastly for consistency with problem set
lastly for consistency with problem set five we might Define an unload function
five we might Define an unload function whose purpose in life is to free any
whose purpose in life is to free any memory that you've been using just to
memory that you've been using just to give it back to the computer now odds
give it back to the computer now odds are whether you're still working on
are whether you're still working on speller or half finished speller like
speller or half finished speller like you wrote a decent amount of lines of
you wrote a decent amount of lines of code and indeed it's been by design a
code and indeed it's been by design a challenge but one of the reasons for
challenge but one of the reasons for these higher level languages like python
these higher level languages like python is that you can stand on the shoulders
is that you can stand on the shoulders of programmers before you and solve very
of programmers before you and solve very common problems much more quickly so
common problems much more quickly so that you can f us on building your new
that you can f us on building your new app or your web application or your own
app or your web application or your own project to solve problems of interest to
project to solve problems of interest to you so at the risk of crushing some
you so at the risk of crushing some Spirits let me propose that in Python if
Spirits let me propose that in Python if you want a dictionary for something like
you want a dictionary for something like a spell checker well that's fine go
a spell checker well that's fine go ahead and give yourself a variable like
ahead and give yourself a variable like words to store all of those words and
words to store all of those words and just assign it equal to a dictionary or
just assign it equal to a dictionary or dict for short in Python that will give
dict for short in Python that will give you a hashtable now it turns out in
you a hashtable now it turns out in speller recall you don't need to worry
speller recall you don't need to worry about words and definition conditions
about words and definition conditions it's just about spellchecking the words
it's just about spellchecking the words so strictly speaking we don't need keys
so strictly speaking we don't need keys and values we just need keys so I'm
and values we just need keys so I'm going to save myself a few more key
going to save myself a few more key strokes by just saying that technically
strokes by just saying that technically in Python using a set suffices again a
in Python using a set suffices again a set is just a collection of values with
set is just a collection of values with no duplicates but they don't necessarily
no duplicates but they don't necessarily have uh keys and values it's just one or
have uh keys and values it's just one or the other but now that I have on line
the other but now that I have on line one I claim the equivalent in Python of
one I claim the equivalent in Python of a hash table I can actually do something
a hash table I can actually do something like this here's how I might implement
like this here's how I might implement the check function in Python if the word
the check function in Python if the word passed into this function is in my
passed into this function is in my variable called words well return true
variable called words well return true else go ahead and return false done wait
else go ahead and return false done wait you're thinking if anything at all maybe
you're thinking if anything at all maybe we want to handle lowercase instead of
we want to handle lowercase instead of just uppercase and lowercase well you
just uppercase and lowercase well you know what in Python if you want to force
know what in Python if you want to force a whole word to lowercase you don't have
a whole word to lowercase you don't have to iterate over it with a loop you don't
to iterate over it with a loop you don't have to use any of that ctype functions
have to use any of that ctype functions or anything just say word. lower and
or anything just say word. lower and that will convert the whole thing to
that will convert the whole thing to lowercase for parody with the dictionary
lowercase for parody with the dictionary all right how about something like the
all right how about something like the load function in Python well in Python
load function in Python well in Python you can open files just like in C for
you can open files just like in C for instance in Python I might do open the
instance in Python I might do open the dictionary argument in read mode just
dictionary argument in read mode just like fopen in Python I might do
like fopen in Python I might do something like this for each line in
something like this for each line in that file let me go ahead and add to my
that file let me go ahead and add to my words variable that line and then let me
words variable that line and then let me go ahead and close that file and I think
go ahead and close that file and I think I'm done I'm just going to go ahead and
I'm done I'm just going to go ahead and return true just because I think think
return true just because I think think I'm already done now here too I could
I'm already done now here too I could nitpick a little bit technically if I'm
nitpick a little bit technically if I'm reading in every line from the file
reading in every line from the file every line in the dictionary ends with
every line in the dictionary ends with technically a back sln but there's an
technically a back sln but there's an easy way to get rid of that uh just like
easy way to get rid of that uh just like you might in see with an alternative
you might in see with an alternative syntax what I'm actually going to do is
syntax what I'm actually going to do is this let me grab from the current line
this let me grab from the current line the current word by stripping off with
the current word by stripping off with reverse strip R strip a function will
reverse strip R strip a function will again see that just gets rid of the
again see that just gets rid of the trailing new line the back sln at the
trailing new line the back sln at the end of that line and what I really want
end of that line and what I really want to do then is add this word to that
to do then is add this word to that dictionary meanwhile if I want to figure
dictionary meanwhile if I want to figure out what the size is of my dictionary
out what the size is of my dictionary well and see you're probably writing
well and see you're probably writing code to iterate over all of those lines
code to iterate over all of those lines and you're just going to uh count them
and you're just going to uh count them up using a variable not so in Python you
up using a variable not so in Python you can just return the length of those
can just return the length of those words and better still in Python you
words and better still in Python you don't have to manage your own memory no
don't have to manage your own memory no more Malo no more free no more manual
more Malo no more free no more manual thinking about memory the language just
thinking about memory the language just deals with all of that for you so you
deals with all of that for you so you know what it suffices for me to just
know what it suffices for me to just return true and claim that unloading is
return true and claim that unloading is done for me and that's it again whether
done for me and that's it again whether you're in the middle of or already
you're in the middle of or already finished this might perhaps suggest some
finished this might perhaps suggest some frustration but also Enlightenment in
frustration but also Enlightenment in this in that this is why higher level
this in that this is why higher level languages exist you can build on top of
languages exist you can build on top of the same principles the same ideas with
the same principles the same ideas with which you've been dealing struggling
which you've been dealing struggling even this past week but you can now
even this past week but you can now express yourself all the more succinctly
express yourself all the more succinctly like this one line implements a hash
like this one line implements a hash table for you and all of this now now
table for you and all of this now now just uses that hash table in a simpler
just uses that hash table in a simpler way any questions now on this keeping in
way any questions now on this keeping in mind that the point nonetheless of
mind that the point nonetheless of speller and P said 5 is to understand
speller and P said 5 is to understand what's really going on underneath the
what's really going on underneath the hood and better still to notice this
hood and better still to notice this this might seem all rather amazing but
this might seem all rather amazing but let me go ahead and do this I've
let me go ahead and do this I've actually got a couple of versions of
actually got a couple of versions of speller written here and I've got a
speller written here and I've got a version written in C that I won't show
version written in C that I won't show the source code for but I'm going to go
the source code for but I'm going to go ahead and make that version of speller
ahead and make that version of speller in C and I'm going to go ahead here and
in C and I'm going to go ahead here and let's say split my window here for just
let's say split my window here for just a moment and I'm going to go into a
a moment and I'm going to go into a python version of spell really that I
python version of spell really that I just wrote and on the left hand side
just wrote and on the left hand side here let me go ahead and run speller the
here let me go ahead and run speller the version I compiled in C using a big text
version I compiled in C using a big text like uh the Sherlock Holmes text which
like uh the Sherlock Holmes text which is a whole lot of words in it and on the
is a whole lot of words in it and on the right hand side let me run python of
right hand side let me run python of spell. Pi which is a separate file I
spell. Pi which is a separate file I wrote in advance just like we give you
wrote in advance just like we give you speller.c and I'll similarly run this on
speller.c and I'll similarly run this on the Sherlock Holmes text and I'm going
the Sherlock Holmes text and I'm going to do my best to hit enter on the left
to do my best to hit enter on the left and the right of my screen at the same
and the right of my screen at the same time but we should see hopefully the
time but we should see hopefully the same list of misspelled words and the
same list of misspelled words and the timings thereof so here we go on the
timings thereof so here we go on the right here we go on the
right here we go on the left all right sort of a race to see
left all right sort of a race to see which one wins here C is on the left
which one wins here C is on the left python is on the right okay
python is on the right okay interesting hopefully Python's close
interesting hopefully Python's close behind note that some of the is internet
behind note that some of the is internet delay and so it might not necessarily be
delay and so it might not necessarily be a crazy number of seconds but the system
a crazy number of seconds but the system is indeed using if we measure at a low
is indeed using if we measure at a low level how much time the CPU spent
level how much time the CPU spent executing my code C took a total of 1.64
executing my code C took a total of 1.64 seconds that was pretty fast even though
seconds that was pretty fast even though it took a moment more for all of the
it took a moment more for all of the btes to come over the Internet the
btes to come over the Internet the python version though took what 2.44
python version though took what 2.44 seconds so what might an inference be I
seconds so what might an inference be I mean one maybe I'm just better at
mean one maybe I'm just better at programming in c than I am in Python
programming in c than I am in Python which is probably not true
which is probably not true but what else might you infer from this
example should we maybe give up on python stick with
python stick with C no so where what might be going on
C no so where what might be going on here like why is the python version that
here like why is the python version that I claim is correct and I think the
I claim is correct and I think the numbers all line up just not the
numbers all line up just not the times where's the trade-off here well
times where's the trade-off here well here again is sort of this design
here again is sort of this design tradeoff
yeah yeah exactly in order to save the human programmer time there's a lot more
human programmer time there's a lot more features built into python more
features built into python more functions more automatic management of
functions more automatic management of memory and so forth and you have to pay
memory and so forth and you have to pay a price like someone else's code is
a price like someone else's code is doing all of that work for you but if
doing all of that work for you but if they've written some number of lines of
they've written some number of lines of code those are just more lines of code
code those are just more lines of code that need to be executed for you whereas
that need to be executed for you whereas here the computer is at the risk of
here the computer is at the risk of oversimplifying only running my lines of
oversimplifying only running my lines of code so there's just less overhead and
code so there's just less overhead and so this is a Perpetual trade-off
so this is a Perpetual trade-off typically when using a more userfriendly
typically when using a more userfriendly and more modern language one of the
and more modern language one of the prices you might pay is performance now
prices you might pay is performance now there's a lot of smart computer
there's a lot of smart computer scientists in the world though trying to
scientists in the world though trying to push back on those same tradeoffs and so
push back on those same tradeoffs and so these interpreters like the command I
these interpreters like the command I wrote python technically can especially
wrote python technically can especially if you run a program again and again
if you run a program again and again they can actually sort of secretly
they can actually sort of secretly behind the scenes compile your code for
behind the scenes compile your code for you down to zeros and ones and then the
you down to zeros and ones and then the second the third the fourth time you run
second the third the fourth time you run that program it might very well be
that program it might very well be faster so this is a bit of a head fake
faster so this is a bit of a head fake here in that I'm running them once and
here in that I'm running them once and only once but we could get benefit over
only once but we could get benefit over time if we kept running the python
time if we kept running the python version again and again and perhaps
version again and again and perhaps fine-tune the performance but in general
fine-tune the performance but in general there's going to be this trade-off now
there's going to be this trade-off now would you rather spend the 60 seconds I
would you rather spend the 60 seconds I wrote implementing a spell checker or
wrote implementing a spell checker or the 6 hours 16 hours you might be or
the 6 hours 16 hours you might be or have spent implementing the same and C
have spent implementing the same and C you know probably not for productivity
you know probably not for productivity sake this is why we have these
sake this is why we have these additional languages just for fun let me
additional languages just for fun let me flip over to another screen here and
flip over to another screen here and open up a version of python that's
open up a version of python that's actually on on my in just a second on my
actually on on my in just a second on my own uh Mac instead of the cloud so that
own uh Mac instead of the cloud so that I can actually do something with
I can actually do something with Graphics so here I just have a black and
Graphics so here I just have a black and white terminal window on my very own Mac
white terminal window on my very own Mac and I've pre-installed python just like
and I've pre-installed python just like we've done so for VSS code in the cloud
we've done so for VSS code in the cloud for you uh notice that I've got this uh
for you uh notice that I've got this uh photo of uh perhaps one of your favorite
photo of uh perhaps one of your favorite TV shows here with the cast of The
TV shows here with the cast of The Office notice all of the faces in this
Office notice all of the faces in this image here and let me propose that we
image here and let me propose that we try to find one face in the crowd sort
try to find one face in the crowd sort of CSI style whereby we want to find
of CSI style whereby we want to find perhaps the stranton Strangler so to
perhaps the stranton Strangler so to speak and so here is an example of this
speak and so here is an example of this this guy's face now how do we go about
this guy's face now how do we go about finding this specific face in the crowd
finding this specific face in the crowd well our human eyes obviously can pluck
well our human eyes obviously can pluck him out especially if you're familiar
him out especially if you're familiar with the show but let me go ahead and do
with the show but let me go ahead and do this instead let me go ahead and propose
this instead let me go ahead and propose that we run code that I already wrote in
that we run code that I already wrote in advance here this is a Python program
advance here this is a Python program with more lines of code that we won't
with more lines of code that we won't dwell on for today but it's meant to
dwell on for today but it's meant to motivate what we can do from a a pillow
motivate what we can do from a a pillow uh Library implying a python image
uh Library implying a python image Library I want to import some type of
Library I want to import some type of information called some type of some
information called some type of some feature called image so that I can
feature called image so that I can manipulate images not unlike our own
manipulate images not unlike our own problem set 4 and this is kind of
problem set 4 and this is kind of powerful you in Python you can just
powerful you in Python you can just import face recognition as a library
import face recognition as a library that someone else wrote from there I'm
that someone else wrote from there I'm going to create a variable called image
going to create a variable called image I'm going to use this face recognitions
I'm going to use this face recognitions libraries load image file function it's
libraries load image file function it's a little verbose but it's similar in
a little verbose but it's similar in Spirit to F open and I'm going to open
Spirit to F open and I'm going to open office. jpeg I'm going to then declare a
office. jpeg I'm going to then declare a second variable called face locations
second variable called face locations plural because what I'm expecting to get
plural because what I'm expecting to get back per the documentation for this
back per the documentation for this library is a list of all of the faces
library is a list of all of the faces locations that are detected all right
locations that are detected all right then I'm going to iterate over each of
then I'm going to iterate over each of those uh faces using a for Loop that
those uh faces using a for Loop that we'll see in more detail I'm going to
we'll see in more detail I'm going to then infer what the top right bottom and
then infer what the top right bottom and left Corners are of that face and then
left Corners are of that face and then what I'm going to do here is show that
what I'm going to do here is show that face alone if I've detected the face in
face alone if I've detected the face in question so let me go ahead here and run
question so let me go ahead here and run detect. and we'll see not just the one
detect. and we'll see not just the one face we're looking for but if I run
face we're looking for but if I run python of detect. piy it's going to do
python of detect. piy it's going to do all of the analysis I'll see a big
all of the analysis I'll see a big opening here now of all of the faces
opening here now of all of the faces that were detected in this here program
that were detected in this here program okay some better than others I guess if
okay some better than others I guess if you zoom in on catching someone typical
you zoom in on catching someone typical Angela if you now want to Now find that
Angela if you now want to Now find that one face I think we need to train the
one face I think we need to train the software a bit more so let me actually
software a bit more so let me actually open up a second program called
open up a second program called recognize that's got more going on but
recognize that's got more going on but let me with a wave of a hand point out
let me with a wave of a hand point out that I'm now loading not only the
that I'm now loading not only the office. JPEG but also toby. JPEG to sort
office. JPEG but also toby. JPEG to sort of train the algorithm to find that
of train the algorithm to find that specific face and so now if I run this
specific face and so now if I run this second version recognize. with python of
second version recognize. with python of recognize. py hold my breath for just a
recognize. py hold my breath for just a moment it's an analyzing presumably all
moment it's an analyzing presumably all of the faces you see the same original
of the faces you see the same original photo but do you see one such face
photo but do you see one such face highlighted here this adversion of the
highlighted here this adversion of the code found Toby highlighted him with
code found Toby highlighted him with this green and voila we have face
this green and voila we have face recognition so for better for worse this
recognition so for better for worse this is what's happening increasingly
is what's happening increasingly societally nowadays and honestly even
societally nowadays and honestly even though I didn't write the code live
though I didn't write the code live because it's a good dozen or more lines
because it's a good dozen or more lines of code it's not terribly many and
of code it's not terribly many and literally all the authorities all we
literally all the authorities all we have to do is import face recognition
have to do is import face recognition and voila you have access like these
and voila you have access like these techn IES are here already but let's
techn IES are here already but let's consider for just a moment how did we
consider for just a moment how did we find Toby like how might that Library
find Toby like how might that Library even though we're not going to look at
even though we're not going to look at its implementation details how does it
its implementation details how does it find Toby and distinguish him from all
find Toby and distinguish him from all of these other faces in the crowd what
of these other faces in the crowd what might it be doing
might it be doing intuitively think back even to pet 4
intuitively think back even to pet 4 like what you yourselves have access to
like what you yourselves have access to data wise
yeah you know pixels in one area area and a lot
and a lot of
of that it's a lot of a lot of
simar yeah exactly and to summarize for for camera here we have trained the
for camera here we have trained the software if you will by giving it a
software if you will by giving it a photo of Toby's face by so by looking
photo of Toby's face by so by looking for the same or really similar pixels
for the same or really similar pixels especially if it's a slightly different
especially if it's a slightly different image of Toby we can perhaps identify
image of Toby we can perhaps identify him in the crowd and what really is a
him in the crowd and what really is a human face well at the end of the day
human face well at the end of the day the computer only knows it as a pattern
the computer only knows it as a pattern of bits or really at a higher level a
of bits or really at a higher level a pattern of pixels so maybe a human face
pattern of pixels so maybe a human face is perhaps best defined in general as
is perhaps best defined in general as like two eyes and a nose and a mouth
like two eyes and a nose and a mouth that even though all of us look similar
that even though all of us look similar structurally odds are the measurement
structurally odds are the measurement between the eyes and the nose and the
between the eyes and the nose and the width of the mouth the skin tone and all
width of the mouth the skin tone and all of these other physical characteristics
of these other physical characteristics are patterns that software could perhaps
are patterns that software could perhaps detect and then look sort of
detect and then look sort of statistically through the image looking
statistically through the image looking for the closest possible match to these
for the closest possible match to these various measurement shapes colors and
various measurement shapes colors and sizes and the like and indeed that might
sizes and the like and indeed that might might be the intuition but what's
might be the intuition but what's powerful here again is just how easy and
powerful here again is just how easy and readily available this technology now is
readily available this technology now is all right so with that said let's
all right so with that said let's propose to consider what more we can do
propose to consider what more we can do with python itself get back to the
with python itself get back to the fundamentals so that you yourselves can
fundamentals so that you yourselves can start to implement something along those
start to implement something along those same lines so besides having access to
same lines so besides having access to things like a get string function um the
things like a get string function um the cs50 library provides a few other things
cs50 library provides a few other things as well namely in C we had these but in
as well namely in C we had these but in Python we're going to have fewer in
Python we're going to have fewer in Python our library short term is going
Python our library short term is going to give you not only get string but also
to give you not only get string but also get in and get float why it's actually
get in and get float why it's actually just kind of annoying as we'll student
just kind of annoying as we'll student see to get back an integer or a float
see to get back an integer or a float from a user and just make sure that it's
from a user and just make sure that it's an INT and a float and not like a word
an INT and a float and not like a word like cat or dog or some string that's
like cat or dog or some string that's not actually a number well we can import
not actually a number well we can import not just the specific function get
not just the specific function get string but we can actually import all of
string but we can actually import all of these functions one at a time like this
these functions one at a time like this as we'll soon see or you can even in
as we'll soon see or you can even in Python import specific functions from a
Python import specific functions from a file one of you asked a while back when
file one of you asked a while back when you import when you include something
you import when you include something like cs50.h or standard i.h you're
like cs50.h or standard i.h you're actually getting all of the code in that
actually getting all of the code in that file which potentially can add bulk to
file which potentially can add bulk to your own program or time in this case
your own program or time in this case when you import specific functions from
when you import specific functions from python you can be a little more narrowly
python you can be a little more narrowly uh precise as to what it is you want to
uh precise as to what it is you want to have access to all right so with that
have access to all right so with that said let's go ahead and see what
said let's go ahead and see what conditionals look like in Python so in
conditionals look like in Python so in the left hand side again here we'll see
the left hand side again here we'll see scrp
scrp and here for instance was just kind of a
and here for instance was just kind of a contrived example asking if x is less
contrived example asking if x is less than y then say x is less than y in C it
than y then say x is less than y in C it looked like this in Python now it's
looked like this in Python now it's going to look like this instead and
going to look like this instead and here's before in C and here's after and
here's before in C and here's after and just to call out a few of the obvious
just to call out a few of the obvious differences what has changed in Python
differences what has changed in Python for conditionals it would
seem sort of what's the difference yeah yeah so there's no more curly
yeah yeah so there's no more curly braces and indeed you don't use those
braces and indeed you don't use those what appears to be taking their place if
what appears to be taking their place if you might
you might infer what seems to have taken their
infer what seems to have taken their place what do you think so the colon at
place what do you think so the colon at the start of this line here but also
the start of this line here but also even more important now is this
even more important now is this indentation below it so some of you and
indentation below it so some of you and we know this from Office hours have a
we know this from Office hours have a habit of like uh indenting everything on
habit of like uh indenting everything on the left right and it's just kind of
the left right and it's just kind of this crazy mess to look at frustrating
this crazy mess to look at frustrating for you surely but C and clang is pretty
for you surely but C and clang is pretty tolerant when it comes to things like
tolerant when it comes to things like Whit space in a program python uh-uh
Whit space in a program python uh-uh they realized years ago that let's help
they realized years ago that let's help humans help themselves and just require
humans help themselves and just require standard indentation so four spaces
standard indentation so four spaces would be the norm here but because it's
would be the norm here but because it's indented below that colon that indeed
indented below that colon that indeed indicates that this now is part of that
indicates that this now is part of that condition something else has gone
condition something else has gone missing versus C in this conditional
missing versus C in this conditional what else is a little
what else is a little simplified yeah so no more parentheses
simplified yeah so no more parentheses you can still use them especially when
you can still use them especially when you need to logically to do order of
you need to logically to do order of operations like in math but in this case
operations like in math but in this case if you just want to ask a simple
if you just want to ask a simple question like if x less than y you can
question like if x less than y you can just do it like that how about when you
just do it like that how about when you have an if else well this is almost the
have an if else well this is almost the same here with these same changes in C
same here with these same changes in C this looked like this and it's starting
this looked like this and it's starting to get a bit bulky at least if we use
to get a bit bulky at least if we use our curly braces in this way in Python
our curly braces in this way in Python we can tighten things up further even
we can tighten things up further even though strictly speaking in C you don't
though strictly speaking in C you don't always need the curly braces but here
always need the curly braces but here gone are the parentheses again gone are
gone are the parentheses again gone are the curly braces indentation is
the curly braces indentation is consistent and we've just added another
consistent and we've just added another keyword else with a colon but no more
keyword else with a colon but no more semicolons as well how about something
semicolons as well how about something larger like this and if else if else
larger like this and if else if else this one's a little curious but in C it
this one's a little curious but in C it looked like this if else if else in
looked like this if else if else in Python it now looks like this and
Python it now looks like this and there's perhaps one curiosity here that
there's perhaps one curiosity here that honestly all these years later I still
honestly all these years later I still can't remember how to spell it half the
can't remember how to spell it half the time what's weird about
time what's weird about this what do you spot as
this what do you spot as different uh yeah over
here yeah instead of else if it's l if why apparently El space if was just too
why apparently El space if was just too many keystrokes for humans to type so
many keystrokes for humans to type so they condensed it into this way probably
they condensed it into this way probably means it's a little more distinguishable
means it's a little more distinguishable too for the computer between the if and
too for the computer between the if and the else to but just something to
the else to but just something to remember now it's indeed L if and not
remember now it's indeed L if and not else if all right so what about
else if all right so what about variables in Python I've used a couple
variables in Python I've used a couple of them already but let's let's um
of them already but let's let's um distill exactly how you define uh and
distill exactly how you define uh and declare these things as well so in
declare these things as well so in scratch if we wanted to create a
scratch if we wanted to create a variable called counter and set it equal
variable called counter and set it equal initially to zero we would do something
initially to zero we would do something like this specify that it's an INT use
like this specify that it's an INT use the assignment op operator and the
the assignment op operator and the thought with a semicolon in Python it's
thought with a semicolon in Python it's just simpler you name the variable you
just simpler you name the variable you use the assignment operator as before
use the assignment operator as before you set it equal to some value and
you set it equal to some value and that's it you don't mention the type you
that's it you don't mention the type you don't mention the semicolon or anything
don't mention the semicolon or anything more what if you want to change a
more what if you want to change a variable like the counter by one that is
variable like the counter by one that is incremented by one you have a few
incremented by one you have a few different ways here in C we saw syntax
different ways here in C we saw syntax like this where you can say counter
like this where you can say counter equals counter plus one which again
equals counter plus one which again feels like illogical how can counter
feels like illogical how can counter equal counter plus one but again we read
equal counter plus one but again we read this code really right to left updating
this code really right to left updating its value by one um in Python it's
its value by one um in Python it's almost the same you just get rid of the
almost the same you just get rid of the semicolon so that logic is there but
semicolon so that logic is there but recall in C we could do something
recall in C we could do something slightly different that we can also do
slightly different that we can also do in Python in Python you can also more
in Python in Python you can also more succinctly do this plus equals and then
succinctly do this plus equals and then whatever number you want to add or you
whatever number you want to add or you can even change it to subtract if you
can even change it to subtract if you prefer sadly gone is something you've
prefer sadly gone is something you've probably typed a whole lot what was the
probably typed a whole lot what was the other way you can add
other way you can add one Plus+ is no more sadly in Python
one Plus+ is no more sadly in Python just too many ways to do the same thing
just too many ways to do the same thing so they got rid of it in favor of just
so they got rid of it in favor of just this syntax here so keep that in mind as
this syntax here so keep that in mind as well what about loops when you want to
well what about loops when you want to do something in Python again and again
do something in Python again and again well in scratch in week zero here's how
well in scratch in week zero here's how we meowed three times specifically in C
we meowed three times specifically in C we had a couple of ways of doing this
we had a couple of ways of doing this this was like the more mechanical
this was like the more mechanical approach where you create a variable
approach where you create a variable called I you set it equal to zero you
called I you set it equal to zero you then do while I is less than three the
then do while I is less than three the following and then you yourself
following and then you yourself increment I again and again mechanical
increment I again and again mechanical in the sense that like you have to
in the sense that like you have to implement all of these gears and make
implement all of these gears and make them turn yourself but this was a
them turn yourself but this was a correct way to do that in Python we can
correct way to do that in Python we can still achieve the same idea but we don't
still achieve the same idea but we don't need the int keyword we don't need any
need the int keyword we don't need any of the semicolons we don't need the
of the semicolons we don't need the parentheses we don't need the curly
parentheses we don't need the curly braces we can't use the Plus+ so maybe
braces we can't use the Plus+ so maybe that's a minor step backwards if you're
that's a minor step backwards if you're a fan but otherwise the code the logic
a fan but otherwise the code the logic is exactly the same but there's other
is exactly the same but there's other ways to achieve this same idea recall
ways to achieve this same idea recall that in C we could also do this you
that in C we could also do this you could use a for Loop which it does
could use a for Loop which it does exactly the same thing both are correct
exactly the same thing both are correct both are arguably welld designed it's
both are arguably welld designed it's kind of to each their own when it comes
kind of to each their own when it comes to choosing between these in Python
to choosing between these in Python though we're going to have to think
though we're going to have to think through how to do this so you don't do
through how to do this so you don't do the same for loop as in C the closest I
the same for loop as in C the closest I could come up with is this where you say
could come up with is this where you say four I or whatever variable you want to
four I or whatever variable you want to do the counting in literally the
do the counting in literally the preposition and then you use square
preposition and then you use square brackets here and we've used square
brackets here and we've used square brackets before in the context of like
brackets before in the context of like arrays and things like that and the 012
arrays and things like that and the 012 looks like an array in some sense even
looks like an array in some sense even though we've also seen arrays with curly
though we've also seen arrays with curly braces but these square brackets for now
braces but these square brackets for now denote a list python does not have
denote a list python does not have arrays an array is that contiguous chunk
arrays an array is that contiguous chunk of memory back to back to back that you
of memory back to back to back that you have to resize somehow by moving things
have to resize somehow by moving things around in memory as per two weeks ago in
around in memory as per two weeks ago in Python though you can just create a list
Python though you can just create a list like this using square brackets and
like this using square brackets and better still as we'll see you can add or
better still as we'll see you can add or even remove things from that list down
even remove things from that list down the road um this though is not going to
the road um this though is not going to be very welld designed this will work
be very welld designed this will work this will iterate in Python three
this will iterate in Python three times but what might rub you the wrong
times but what might rub you the wrong way about this design even if you've
way about this design even if you've never seen python before how does this
never seen python before how does this example not end well
yeah yeah like if you're making a large list you have to type out each one of
list you have to type out each one of these numbers like comma three comma
these numbers like comma three comma four comma 5 comma dot dot do 50 comma
four comma 5 comma dot dot do 50 comma dot dot dot 500 like surely that's not
dot dot dot 500 like surely that's not the best solution to have all of these
the best solution to have all of these numbers on the code on the screen
numbers on the code on the screen wrapping endlessly on the screen so in
wrapping endlessly on the screen so in Python another way to do this would be
Python another way to do this would be to use a function called range which
to use a function called range which technically is a data type unto itself
technically is a data type unto itself and this returns to you as many values
and this returns to you as many values as you ask for it range takes some other
as you ask for it range takes some other arguments as well but the simplest use
arguments as well but the simplest use case here is if you want back the
case here is if you want back the numbers 0 1 and two a total of three
numbers 0 1 and two a total of three values you say hey python please give me
values you say hey python please give me a range of three values and by default
a range of three values and by default they start at zero on up but this is
they start at zero on up but this is more efficient than it would be to
more efficient than it would be to hardcode the entire list at once and the
hardcode the entire list at once and the best metaphor I could up with is
best metaphor I could up with is something like this like here for
something like this like here for instance is a deck of cards this is sort
instance is a deck of cards this is sort of normal human size and there's
of normal human size and there's presumably 52 cards here so writing out
presumably 52 cards here so writing out 0 through 51 on code would be a little
0 through 51 on code would be a little ridiculous for the reasons you know it
ridiculous for the reasons you know it would just be very unwieldy and ugly and
would just be very unwieldy and ugly and wrapping and all of that it would be the
wrapping and all of that it would be the phys it would be the virtual equivalent
phys it would be the virtual equivalent of me like handing you all of these
of me like handing you all of these cards at once to just deal with and
cards at once to just deal with and right you know they're not that big but
right you know they're not that big but like it's a lot of cards to hold on to
like it's a lot of cards to hold on to it requires a lot of memory or physical
it requires a lot of memory or physical storage if you will what range does
storage if you will what range does metaphorically is if you ask me for
metaphorically is if you ask me for three cards I hand you them one at a
three cards I hand you them one at a time like this so that at any point in
time like this so that at any point in time you only have one number in the
time you only have one number in the computer's memory until you're handed
computer's memory until you're handed the next the alternative the previous
the next the alternative the previous version would be to hand me all three
version would be to hand me all three cards at once or all 52 cards at once
cards at once or all 52 cards at once but in this case range is just way more
but in this case range is just way more efficient you can do range of a thousand
efficient you can do range of a thousand that's not going to give you a list of a
that's not going to give you a list of a thousand values all at once it's going
thousand values all at once it's going to give you a thousand values one at a
to give you a thousand values one at a time reducing memory sign signicantly in
time reducing memory sign signicantly in the computer itself all right so besides
the computer itself all right so besides this what about doing something forever
this what about doing something forever in scratch well we could do this
in scratch well we could do this literally with a forever block which
literally with a forever block which didn't quite exist in C in C we had had
didn't quite exist in C in C we had had to hack it together by saying while true
to hack it together by saying while true because true is by definition TR always
because true is by definition TR always true so this just in uh deliberately
true so this just in uh deliberately induces an infinite Loop for us in
induces an infinite Loop for us in Python the logic's going to be almost
Python the logic's going to be almost the same and infinite Loops in Python
the same and infinite Loops in Python tend to actually be even more common
tend to actually be even more common because you can always break out of them
because you can always break out of them as you couldn't see in Python it looks
as you couldn't see in Python it looks like this and this is slightly more
like this and this is slightly more subtle but gone are the curly braces
subtle but gone are the curly braces gone or the parentheses but ever so
gone or the parentheses but ever so slight difference
slight difference toal capital T for true and it's going
toal capital T for true and it's going to be a capital f for false stupid
to be a capital f for false stupid little differences eventually you're
little differences eventually you're going to mistype one or the other but
going to mistype one or the other but these are the kinds of things to keep an
these are the kinds of things to keep an eye out and to start recognizing in your
eye out and to start recognizing in your mind's eye when you read code questions
mind's eye when you read code questions now on any of these building
now on any of these building blocks
blocks yeah
I in the for Loop was I re uh it was set to zero on the first iteration then one
to zero on the first iteration then one on the next then two on the third and
on the next then two on the third and the same thing for range it just doesn't
the same thing for range it just doesn't use up as much memory all at once other
use up as much memory all at once other questions now on any of these building
questions now on any of these building blocks of
blocks of python no all right well let's go ahead
python no all right well let's go ahead and build something a little more than
and build something a little more than hello let me propose that over here we
hello let me propose that over here we Implement maybe the the simplest of
Implement maybe the the simplest of calculators here so let me go back to vs
calculators here so let me go back to vs code here open my terminal uh window and
code here open my terminal uh window and open up say a file called
open up say a file called calculator. and in calculator. we'll
calculator. and in calculator. we'll have an opportunity to explore some of
have an opportunity to explore some of these building blocks but we'll allow
these building blocks but we'll allow things to escalate pretty quickly to
things to escalate pretty quickly to more interesting examples so that we can
more interesting examples so that we can do the same thing ultimately as well and
do the same thing ultimately as well and in fact let me go ahead and do this
in fact let me go ahead and do this moreover I've brought some code with me
moreover I've brought some code with me in advance uh for instance something
in advance uh for instance something called calculator 0. C from the first
called calculator 0. C from the first week of C
week of C and let me go ahead and split my window
and let me go ahead and split my window here in fact so that I can now do
here in fact so that I can now do something like uh this let me move this
something like uh this let me move this over here here calculator. Pi so now I
over here here calculator. Pi so now I have on the left of my screen
have on the left of my screen calculator. C or calculator z.c because
calculator. C or calculator z.c because that's the first version I made and
that's the first version I made and calculator. Pi on the right let me go
calculator. Pi on the right let me go ahead and Implement really the same idea
ahead and Implement really the same idea here so on the right hand side the
here so on the right hand side the analog of including cs50.h would be from
analog of including cs50.h would be from cs50 import get int if I want to indeed
cs50 import get int if I want to indeed use this function now I'm going to go
use this function now I'm going to go ahead and give myself a variable X
ahead and give myself a variable X without defining its type I'm going to
without defining its type I'm going to use this get int function and I'm going
use this get int function and I'm going to prompt the user for X just like in C
to prompt the user for X just like in C I'm then going to go ahead and prompt
I'm then going to go ahead and prompt the user for another int like y here
the user for another int like y here just like in C and at the very end I'm
just like in C and at the very end I'm going to go ahead and do print X Plus Y
going to go ahead and do print X Plus Y and that's it now granted I have some
and that's it now granted I have some comments in my C version of the code
comments in my C version of the code just to remind you of what each line is
just to remind you of what each line is doing but I've still distilled this into
doing but I've still distilled this into like six lines or really four if I get
like six lines or really four if I get rid of the blank line so it's already
rid of the blank line so it's already perhaps a bit tighter here here but
perhaps a bit tighter here here but there's also it's tighter because
there's also it's tighter because something really important historically
something really important historically is missing what did I seem to Omit
is missing what did I seem to Omit altogether that we haven't really
altogether that we haven't really highlighted yet
highlighted yet yeah yeah the main function is gone and
yeah yeah the main function is gone and in fact maybe you took for granted that
in fact maybe you took for granted that it just worked a moment ago when I wrote
it just worked a moment ago when I wrote hello but I didn't have a main function
hello but I didn't have a main function in hello either and this too is a
in hello either and this too is a feature of python and a lot of other
feature of python and a lot of other languages as well instead of having to
languages as well instead of having to adhere to these long-standing Traditions
adhere to these long-standing Traditions if you just want to write code and get
if you just want to write code and get something done fine just write code and
something done fine just write code and get something done without necessarily
get something done without necessarily all of the same boiler plate so whatever
all of the same boiler plate so whatever is in your python file left indented if
is in your python file left indented if you will by default is just going to be
you will by default is just going to be the code that The Interpreter runs top
the code that The Interpreter runs top to bottom left to right well let me go
to bottom left to right well let me go ahead now and run code like this let me
ahead now and run code like this let me go ahead and open that back up my
go ahead and open that back up my terminal window run python of
terminal window run python of calculator. piy and I'll do X is one y
calculator. piy and I'll do X is one y is 2 and as you might expect it gives me
is 2 and as you might expect it gives me three slight aesthetic bug I put my
three slight aesthetic bug I put my space in the wrong place here so that's
space in the wrong place here so that's a new mistake let me fix that
a new mistake let me fix that aesthetically let me rerun python of
aesthetically let me rerun python of calculator. Pi type in one type in two
calculator. Pi type in one type in two and voila there is now my same version
and voila there is now my same version again but let me propose now that we get
again but let me propose now that we get rid of this training wheel we don't want
rid of this training wheel we don't want to keep taking one step forward and then
to keep taking one step forward and then two steps back by adding these training
two steps back by adding these training wheels so let me instead do this in my
wheels so let me instead do this in my version of calculator. suppose that we
version of calculator. suppose that we take away already the training wheel
take away already the training wheel that is the cs50 library here and let me
that is the cs50 library here and let me instead then use just Python's built-in
instead then use just Python's built-in function called input which literally
function called input which literally does just that it gets input from the
does just that it gets input from the user and it stores it as before in X and
user and it stores it as before in X and Y so this is not cs50 specific this is
Y so this is not cs50 specific this is real world Python Programming well let
real world Python Programming well let me go ahead and run again python of
me go ahead and run again python of calculator. Pi and of course if x is one
calculator. Pi and of course if x is one and Y is 2 X + Y should of course still
and Y is 2 X + Y should of course still be three
be three hm it's apparently 12 according to
hm it's apparently 12 according to python until cs50's Library gets
python until cs50's Library gets involved but does anyone want to
involved but does anyone want to infer what's just went wrong
yeah exactly the input function by Design always returns a string of text
Design always returns a string of text after all that's what the human typed in
after all that's what the human typed in and even though yes I type the number
and even though yes I type the number keys on the keyboard it's still coming
keys on the keyboard it's still coming back is all text now maybe we should use
back is all text now maybe we should use like a get in function well that doesn't
like a get in function well that doesn't exist in Python all you can do is get
exist in Python all you can do is get textual input a string from the user but
textual input a string from the user but we can convert one to the other and so a
we can convert one to the other and so a fix for this so that we don't accident
fix for this so that we don't accident Al concatenate that is join X+ y
Al concatenate that is join X+ y together would be to do something like
together would be to do something like this let me go back to my python code
this let me go back to my python code here and whereas in C we could
here and whereas in C we could previously do type casting we could
previously do type casting we could convert one type to another that
convert one type to another that generally wasn't the case when you were
generally wasn't the case when you were doing something complex like a string to
doing something complex like a string to an INT you could do a Char to an INT and
an INT you could do a Char to an INT and vice versa but for a string recall there
vice versa but for a string recall there was a special function in the ctype
was a special function in the ctype library called a to I like ask e to
library called a to I like ask e to integer that's uh that closest analog
integer that's uh that closest analog here and in fact the way to do this in
here and in fact the way to do this in Python would be to use a function called
Python would be to use a function called int which indeed is the name of the data
int which indeed is the name of the data type 2 even though I have not yet had to
type 2 even though I have not yet had to type it and I can convert the output of
type it and I can convert the output of the input function automatically from a
the input function automatically from a string immediately to an INT and now if
string immediately to an INT and now if I go back to my terminal window rerun
I go back to my terminal window rerun python of calculator. piy with one and
python of calculator. piy with one and two for X and Y now I'm back in business
two for X and Y now I'm back in business so that then is for instance what the
so that then is for instance what the cs50 library does if temporarily this
cs50 library does if temporarily this week is it just deals with the
week is it just deals with the conversion for you and in fact bad
conversion for you and in fact bad things could happen if I type the wrong
things could happen if I type the wrong thing like dog or cat instead of a
thing like dog or cat instead of a number but we'll cross that bridge in
number but we'll cross that bridge in just a moment as well all right what if
just a moment as well all right what if we do something slightly different now
we do something slightly different now with our calculator instead of just
with our calculator instead of just addition let me go ahead and do how
addition let me go ahead and do how about uh div instead of addition let's
about uh div instead of addition let's do division instead so Z equals x / y
do division instead so Z equals x / y thereby giving me a third variable Z let
thereby giving me a third variable Z let me go ahead and run python of
me go ahead and run python of calculator. piy again I'll type in one
calculator. piy again I'll type in one I'll type in three this time and what
I'll type in three this time and what prog what problem do you think we're
prog what problem do you think we're about to
about to see or is it gone what happened when I
see or is it gone what happened when I did this in C albeit with some slightly
did this in C albeit with some slightly more cryptic syntax when I divided one
more cryptic syntax when I divided one number like one one divided by
number like one one divided by three anyone recall
three anyone recall yeah yeah so it would round down to the
yeah yeah so it would round down to the nearest integer whereby you experience
nearest integer whereby you experience trunc so if you take an integer like one
trunc so if you take an integer like one you divide it by another integer like
you divide it by another integer like three that technically should be
three that technically should be 0.33333 infinitely long but in uh C
0.33333 infinitely long but in uh C recall you truncate the value if you
recall you truncate the value if you divide an INT by an INT you get back an
divide an INT by an INT you get back an INT which means you get only the integer
INT which means you get only the integer part which was the zero now python
part which was the zero now python actually handles this for us and avoids
actually handles this for us and avoids the truncation but it leaves us still
the truncation but it leaves us still with one other problem here which is
with one other problem here which is going to be for instance not necessarily
going to be for instance not necessarily visible at a GL
visible at a GL this looks correct this has solved the
this looks correct this has solved the problem in C so truncation does not
problem in C so truncation does not happen the integers are automatically
happen the integers are automatically converted to a float a floating point
converted to a float a floating point value but what other problem did we trip
value but what other problem did we trip over back
over back in week uh
one what else got a little dicey when dealing with simple
dealing with simple arithmetic anyone recall well the syntax
arithmetic anyone recall well the syntax in Python's a little different but let
in Python's a little different but let me go ahead and do this it turns out in
me go ahead and do this it turns out in Python if you want to see more
Python if you want to see more significant digits than what I'm seeing
significant digits than what I'm seeing here by the default which is a dozen or
here by the default which is a dozen or so let me go ahead and print out Z as
so let me go ahead and print out Z as follows let me first print out a format
follows let me first print out a format string because I want to format Z in an
string because I want to format Z in an interesting way and notice this would
interesting way and notice this would have no effect on the difference this is
have no effect on the difference this is just a format string that for no
just a format string that for no compelling reason at the moment is
compelling reason at the moment is interpolating z in those curly braces
interpolating z in those curly braces using an F string or format string if I
using an F string or format string if I run this again with one and three we'll
run this again with one and three we'll see indeed the exact same thing but when
see indeed the exact same thing but when you use an F string you indeed have the
you use an F string you indeed have the ability to format that string more
ability to format that string more precisely just like with percent F in
precisely just like with percent F in Python you could start to fine-tune how
Python you could start to fine-tune how many significant digits you see in uh p
many significant digits you see in uh p in C rather in Python you can do the
in C rather in Python you can do the same but the syntax is a little
same but the syntax is a little different if you want the computer to
different if you want the computer to interpolate Z and show you 50
interpolate Z and show you 50 significant digits that is 50 numbers
significant digits that is 50 numbers after the decimal point syntax is
after the decimal point syntax is similar to C but it's a little different
similar to C but it's a little different you literally put a colon after the
you literally put a colon after the variable's name 50 means show me the
variable's name 50 means show me the decimal point and then 50 digits to the
decimal point and then 50 digits to the right and the F just indicates please
right and the F just indicates please treat this as a floating point value so
treat this as a floating point value so now if I rerun python of calculator. Pi
now if I rerun python of calculator. Pi divide 1 by 3 unfortunately python has
divide 1 by 3 unfortunately python has not solved all of the world's problems
not solved all of the world's problems for us this again was an example of
for us this again was an example of floating point in Precision so that
floating point in Precision so that problem is still latent so just because
problem is still latent so just because the world has advanced doesn't
the world has advanced doesn't necessarily mean that all of our
necessarily mean that all of our problems from C have gone away there are
problems from C have gone away there are solutions using third-party libraries
solutions using third-party libraries for scientific calculations and the like
for scientific calculations and the like but out of the box floating point in
but out of the box floating point in Precision is still an issue meanwhile
Precision is still an issue meanwhile there was one other problem in C that we
there was one other problem in C that we ran into involving numbers and that was
ran into involving numbers and that was this integer overflow recall that an
this integer overflow recall that an integer in C only took up what like 32
integer in C only took up what like 32 bits typically which meant you could
bits typically which meant you could count as high as four billion or maybe
count as high as four billion or maybe if you're doing positive and negatives
if you're doing positive and negatives as high as two billion after which weird
as high as two billion after which weird things would happen the number would go
things would happen the number would go to zero or negative or just it would
to zero or negative or just it would overflow or wrap back around well
overflow or wrap back around well wonderfully in Python they did it at
wonderfully in Python they did it at least address this whereby you can count
least address this whereby you can count as high as you want and python will just
as high as you want and python will just use more and more and more and more bits
use more and more and more and more bits and bites to store really big numbers so
and bites to store really big numbers so integer overflow is not a thing with
integer overflow is not a thing with that said python is limited to how many
that said python is limited to how many digits it will show you on the screen at
digits it will show you on the screen at once as a string but mathematically your
once as a string but mathematically your math will be correct now so we've taken
math will be correct now so we've taken a couple steps forward One Step sideways
a couple steps forward One Step sideways but indeed we've solved some of our
but indeed we've solved some of our problems here all right questions now
problems here all right questions now now on any of these examples thus
now on any of these examples thus far
far question all right well how about uh how
question all right well how about uh how about another problem that we
about another problem that we encountered in C let's revisit it here
encountered in C let's revisit it here in python as well so let me go ahead and
in python as well so let me go ahead and on the left hand side here let me open
on the left hand side here let me open up a file called say compare let's see
up a file called say compare let's see uh how about a file called compare 3. C
uh how about a file called compare 3. C on the left and let me go ahead and
on the left and let me go ahead and create a new file on the right called
create a new file on the right called compare. Pi because recall that bad
compare. Pi because recall that bad things happened when we needed to
things happened when we needed to compare two values in C so on the left
compare two values in C so on the left here is a reminder of what we once did
here is a reminder of what we once did in C whereby if we want to compare
in C whereby if we want to compare values we can get an INT in C stored in
values we can get an INT in C stored in x a get int in C stored in y we then
x a get int in C stored in y we then have our familiar conditional logic here
have our familiar conditional logic here just printing out if x is less than y or
just printing out if x is less than y or not well we can certainly do the same
not well we can certainly do the same thing ultimately in Python by using some
thing ultimately in Python by using some fairly familiar syntax and let's just
fairly familiar syntax and let's just demonstrate this one quickly let me go
demonstrate this one quickly let me go over here too I'll do from cs50 import
over here too I'll do from cs50 import uh get int even though I could do this
uh get int even though I could do this instead with the input function itself x
instead with the input function itself x equals get int and I'll prompt the user
equals get int and I'll prompt the user for that y equals get int and I'll
for that y equals get int and I'll prompt the user for that after that
prompt the user for that after that recall that I can say without
recall that I can say without parentheses if x is less than y then
parentheses if x is less than y then print out without the F uh X is less
print out without the F uh X is less than y then I can go ahead and say else
than y then I can go ahead and say else if x is greater than y i can print out
if x is greater than y i can print out uh quote unquote X is greater than y if
uh quote unquote X is greater than y if you'd like to interject now what did I
you'd like to interject now what did I screw
screw up anyone yeah L if right so L if L if x
up anyone yeah L if right so L if L if x is greater than y else this part's the
is greater than y else this part's the same print X is equal to Y so there's
same print X is equal to Y so there's not all that much new there's no New
not all that much new there's no New Logic going on here but at least
Logic going on here but at least syntactically it's a little cleaner
syntactically it's a little cleaner indeed this program is only 11 lines
indeed this program is only 11 lines long albeit without any comments let me
long albeit without any comments let me go ahead and run python of compare. Pi
go ahead and run python of compare. Pi let's see is 1 less than two indeed
let's see is 1 less than two indeed let's run it again is 2 less than 1 no
let's run it again is 2 less than 1 no it's greater than and let's lastly type
it's greater than and let's lastly type in one and one twice X is equal to Y so
in one and one twice X is equal to Y so we've got a pretty side by side onetoone
we've got a pretty side by side onetoone conversion here let's do something a
conversion here let's do something a little more interesting then and see how
little more interesting then and see how about I open instead something where we
about I open instead something where we actually compared for a purpose so if I
actually compared for a purpose so if I open up from last uh from earlier in the
open up from last uh from earlier in the course how about uh agree do C which
course how about uh agree do C which prompt the user to agree to something or
prompt the user to agree to something or not and let me code up a new version
not and let me code up a new version here called agree. pi and I'll do this
here called agree. pi and I'll do this on the right hand side with agree. pi
on the right hand side with agree. pi but on gree do c on the left notice that
but on gree do c on the left notice that this is how we did this sort of yes no
this is how we did this sort of yes no thing in C we compared c a character
thing in C we compared c a character equal to single quotes y or equal to
equal to single quotes y or equal to single quotes little Y and then the same
single quotes little Y and then the same thing for n now in Python this one's
thing for n now in Python this one's actually going to be a little bit
actually going to be a little bit different here let me go ahead and in
different here let me go ahead and in the python version of this let me do
the python version of this let me do something like this uh we'll use get
something like this uh we'll use get string uh actually no we'll just use
string uh actually no we'll just use input in this case so let's do uh s
input in this case so let's do uh s equals input and we'll ask the user the
equals input and we'll ask the user the same thing do you agree question mark
same thing do you agree question mark then let's go ahead and say if s equals
then let's go ahead and say if s equals equals how about uh
equals how about uh y huh how do I do this well a few things
y huh how do I do this well a few things turns out I'm going to do this s equals
turns out I'm going to do this s equals equals little y then I'm going to go
equals little y then I'm going to go ahead and print out a agreed and L if s
ahead and print out a agreed and L if s equals equals capital N or S equals
equals equals capital N or S equals equals lowercase n I'm going to go ahead
equals lowercase n I'm going to go ahead and print out not agreed and I claimed
and print out not agreed and I claimed for the moment that this is identical
for the moment that this is identical now to the program on the right the
now to the program on the right the program on the left in C but what's
program on the left in C but what's different so we're still doing the same
different so we're still doing the same kind of logic these equal equals for
kind of logic these equal equals for comparing for equality but notice that
comparing for equality but notice that nicely enough python got rid of the two
nicely enough python got rid of the two vertical bars and it's just literally
vertical bars and it's just literally the word or if you recall seeing
the word or if you recall seeing Ampersand Ampersand to express a logical
Ampersand Ampersand to express a logical and in C you can just write literally
and in C you can just write literally the word and and so here's a hint of why
the word and and so here's a hint of why python tends to be pretty popular people
python tends to be pretty popular people just like that it's a little closer to
just like that it's a little closer to English there's a little less of the
English there's a little less of the cryptic syntax here now this is correct
cryptic syntax here now this is correct as this code will now work but I've also
as this code will now work but I've also used double quotes instead of single
used double quotes instead of single quotes and I also omitted a few minutes
quotes and I also omitted a few minutes ago for my list of data types in Python
ago for my list of data types in Python the word Char in Python there are no
the word Char in Python there are no chars there are no individual characters
chars there are no individual characters if you want to manipulate individual
if you want to manipulate individual character you use a string that is to
character you use a string that is to say a stir of size one now in Python you
say a stir of size one now in Python you can use single quotes or double quotes
can use single quotes or double quotes I'm deliberately using double quotes
I'm deliberately using double quotes everywhere just for consistency with how
everywhere just for consistency with how we treat strings in C it's pretty common
we treat strings in C it's pretty common though to use single quotes instead if
though to use single quotes instead if only because on most keyboards you don't
only because on most keyboards you don't have to hold the shift key anymore I
have to hold the shift key anymore I mean humans have really started to
mean humans have really started to optimize just how quickly they want to
optimize just how quickly they want to be able to code so using a single quote
be able to code so using a single quote tends to be pretty popular in Python and
tends to be pretty popular in Python and other languages as well they are
other languages as well they are fundamentally the same uh single or
fundamentally the same uh single or double unlike in C where they have
double unlike in C where they have meaning so this is correct I claim and
meaning so this is correct I claim and in fact let me run this real quick I'll
in fact let me run this real quick I'll open up my terminal window here let me
open up my terminal window here let me get rid of the version and see run
get rid of the version and see run python of agree. piy and I'll type in y
python of agree. piy and I'll type in y okay I'll run it again and type in
okay I'll run it again and type in little Y and I'll stipulate it's going
little Y and I'll stipulate it's going to work for no as well but this isn't
to work for no as well but this isn't necessarily the only way we can do this
necessarily the only way we can do this there are other ways to implement the
there are other ways to implement the same idea and in fact I can go about
same idea and in fact I can go about doing this this instead let me go back
doing this this instead let me go back up to my code here and we saw a hint of
up to my code here and we saw a hint of this earlier we know that lists exist in
this earlier we know that lists exist in Python and you can create them just by
Python and you can create them just by using square brackets so what if I
using square brackets so what if I simplify the code a little bit and just
simplify the code a little bit and just say if s is in the following list of
say if s is in the following list of values capital y or lowercase y it's not
values capital y or lowercase y it's not all that different logically but it's a
all that different logically but it's a little tighter it's a little more
little tighter it's a little more compact so L ifs is in capital N or
compact so L ifs is in capital N or lowercase n i can express that same idea
lowercase n i can express that same idea too so here again it's just getting a
too so here again it's just getting a little more pleasant to write code
little more pleasant to write code there's less like hitting of the
there's less like hitting of the keyboard you can express yourself a
keyboard you can express yourself a little more succinctly and using the
little more succinctly and using the keyword in Python will figure out how to
keyword in Python will figure out how to search the entire list for whatever the
search the entire list for whatever the value of s is and if it finds it it will
value of s is and if it finds it it will return true automatically else it will
return true automatically else it will return false so if I run agree. Pi again
return false so if I run agree. Pi again and type in capital y or lowercase y
and type in capital y or lowercase y that still now works well I can typen
that still now works well I can typen tighten this up further if I want to add
tighten this up further if I want to add more features well what if I want to
more features well what if I want to support not just why Big Y and little y
support not just why Big Y and little y but how about yes or yes or in case the
but how about yes or yes or in case the user's yelling or you know someone who
user's yelling or you know someone who doesn't really isn't good with caps lock
doesn't really isn't good with caps lock types in yes wait a minute but it could
types in yes wait a minute but it could be weird like do we want to support this
be weird like do we want to support this or this I mean this this just gets
or this I mean this this just gets really tedious quickly combinatorically
really tedious quickly combinatorically if you consider all of these possible
if you consider all of these possible permutations what would be smarter than
permutations what would be smarter than doing something like this if you want to
doing something like this if you want to just be able to tolerate yes in any form
just be able to tolerate yes in any form of capitalization like logically what
of capitalization like logically what would be nice maybe whatever theut is
would be nice maybe whatever theut is you justf over to all lower and then
you justf over to all lower and then exactly super common Paradigm why don't
exactly super common Paradigm why don't we just force the users's input to all
we just force the users's input to all lowercase or all uppercase doesn't
lowercase or all uppercase doesn't matter so long as we're self-consistent
matter so long as we're self-consistent and just compare against all uppercase
and just compare against all uppercase or all lowercase and that will get rid
or all lowercase and that will get rid of all of the possible permutations
of all of the possible permutations otherwise now in C we might have done
otherwise now in C we might have done something like this we might have
something like this we might have simplified this whole list and just said
simplified this whole list and just said let's say uh we'll do how about
let's say uh we'll do how about lowercase so y or yes and we'll just
lowercase so y or yes and we'll just leave it at that but we need to force
leave it at that but we need to force now s to lowercase well in C we would
now s to lowercase well in C we would have used the ctype library we would
have used the ctype library we would have done like two lower and called that
have done like two lower and called that function passing it in although not
function passing it in although not really cuz in C type those operate on
really cuz in C type those operate on individual characters or chars not whole
individual characters or chars not whole strings we have actually didn't see a
strings we have actually didn't see a function that could convert the whole
function that could convert the whole string in C to lowercase but in Python
string in C to lowercase but in Python we're going to benefit from some other
we're going to benefit from some other feature as well it turns out that python
feature as well it turns out that python supports what's called objectoriented
supports what's called objectoriented programming and we're only going to
programming and we're only going to scratch the surface of this in cs50 but
scratch the surface of this in cs50 but if you take a higher level course in
if you take a higher level course in programming or CS you explore this as a
programming or CS you explore this as a different Paradigm up until now in C
different Paradigm up until now in C we've been focusing on what's called
we've been focusing on what's called really procedural programming you write
really procedural programming you write procedures you write functions top to
procedures you write functions top to bottom uh left to right and when you
bottom uh left to right and when you want to change some value we were in the
want to change some value we were in the habit of using a procedure that is a
habit of using a procedure that is a function you would pass something like a
function you would pass something like a variable into a function like to Upper
variable into a function like to Upper or to lower and it would do its thing
or to lower and it would do its thing and hand you back a value well it turns
and hand you back a value well it turns out that it would be nicer programming
out that it would be nicer programming wise if some data types just had
wise if some data types just had built-in functionality like why do we
built-in functionality like why do we have our variables over here and all of
have our variables over here and all of our helper functions like two upper and
our helper functions like two upper and two lower over here such that we
two lower over here such that we constantly have to pass one into the
constantly have to pass one into the other it would be nice to sort of bake
other it would be nice to sort of bake into our data types buil-in
into our data types buil-in functionality so that you can change
functionality so that you can change variables using their own default be uh
variables using their own default be uh buil-in functionality and so
buil-in functionality and so object-oriented programming otherwise
object-oriented programming otherwise known as oop is a technique whereby
known as oop is a technique whereby certain types of values like a string
certain types of values like a string AKA stir not only have properties inside
AKA stir not only have properties inside of them attributes just like a struct in
of them attributes just like a struct in C your data can also have functions
C your data can also have functions built into them as well so whereas in C
built into them as well so whereas in C which is not objectoriented you have
which is not objectoriented you have strs and strs can only store data like a
strs and strs can only store data like a name and a number when implementing a
name and a number when implementing a person in Python you can for instance
person in Python you can for instance have not just a structure otherwise
have not just a structure otherwise known as a class storing a name and a
known as a class storing a name and a number you can have a function like call
number you can have a function like call that person or email that person or
that person or email that person or actual verbs or actions associated with
actual verbs or actions associated with that piece of data now in the context of
that piece of data now in the context of strings it turns out that strings come
strings it turns out that strings come with a lot of useful functionality and
with a lot of useful functionality and in fact this URL here which is in docs.
in fact this URL here which is in docs. python.org which is the official
python.org which is the official documentation for python you'll see a
documentation for python you'll see a whole list of methods that is functions
whole list of methods that is functions that come with strings that you can
that come with strings that you can actually use to modify their values and
actually use to modify their values and what I mean by this is the following if
what I mean by this is the following if we go through the documentation poke
we go through the documentation poke around it turns out that strings come
around it turns out that strings come with a function called Lower and if you
with a function called Lower and if you want to use that function you just have
want to use that function you just have to use slightly different syntax than in
to use slightly different syntax than in C you do not do to lower and you do not
C you do not do to lower and you do not say as I just did lower because this
say as I just did lower because this function is built into s itself and just
function is built into s itself and just like in C when you want to go inside of
like in C when you want to go inside of a variable like a structure and access a
a variable like a structure and access a piece of data inside of it like name or
piece of data inside of it like name or number when you also have functions
number when you also have functions built into data types AKA methods a
built into data types AKA methods a method is just a function that is built
method is just a function that is built into a piece of data you can do s do
into a piece of data you can do s do lower open pen closed pen in this case
lower open pen closed pen in this case and I can do this down here as well if
and I can do this down here as well if if s do lower in quote unquote uh n or
if s do lower in quote unquote uh n or no the whole thing I can force this
no the whole thing I can force this whole thing to lowercase so the only
whole thing to lowercase so the only difference here now is in
difference here now is in object-oriented programming instead of
object-oriented programming instead of constantly passing a value into a
constantly passing a value into a function you just access a function
function you just access a function that's inside of the value it just works
that's inside of the value it just works because of how the language itself is
because of how the language itself is defined and the only way you know that
defined and the only way you know that these functions exist is the
these functions exist is the documentation a Class A book a website
documentation a Class A book a website or the like questions now on this
technique all right I claim this is correct now even though you've never
correct now even though you've never programmed most of you in Python before
programmed most of you in Python before not super well-designed there's an
not super well-designed there's an subtle inefficiency now on lines three
subtle inefficiency now on lines three and five together what's dumb about how
and five together what's dumb about how I've used lower might you
I've used lower might you think
yeah yeah if you're going to use the same function twice and ask the same
same function twice and ask the same question expecting the same answer why
question expecting the same answer why are you calling the function itself
are you calling the function itself twice maybe we should just store the
twice maybe we should just store the result in a variable so we could do this
result in a variable so we could do this in a couple of different ways we for
in a couple of different ways we for instance could go up here and create
instance could go up here and create another variable called T and set that
another variable called T and set that equal to s. lower and then we could just
equal to s. lower and then we could just change this to be T here but honestly I
change this to be T here but honestly I don't think we technically need another
don't think we technically need another variable Al together here I could just
variable Al together here I could just do something like this let's change the
do something like this let's change the value of s to be the lowercase version
value of s to be the lowercase version thereof and so now I can quite simply
thereof and so now I can quite simply refer to S again and again like this
refer to S again and again like this this reusing that same value now to be
this reusing that same value now to be sure I have now just lost the user's
sure I have now just lost the user's original input and if I care about that
original input and if I care about that if they typed in all caps I have no idea
if they typed in all caps I have no idea anymore so maybe I do want to use a
anymore so maybe I do want to use a separate variable altogether but a
separate variable altogether but a takeaway here too is that strings in
takeaway here too is that strings in Python are technically what we'll call
Python are technically what we'll call immutable that is they cannot be changed
immutable that is they cannot be changed this was not true in C once we gave you
this was not true in C once we gave you arrays in week two or memory in week
arrays in week two or memory in week four you could go to town on a string
four you could go to town on a string and change any of the characters you
and change any of the characters you want upper casing lower casing changing
want upper casing lower casing changing it shortening it and so forth but in
it shortening it and so forth but in this case uh this returns a copy of s
this case uh this returns a copy of s forced to lowercase it doesn't change
forced to lowercase it doesn't change the original string that is the memory
the original string that is the memory the bytes in the computer's memory when
the bytes in the computer's memory when you assign it back to S you're
you assign it back to S you're essentially forgetting about the old
essentially forgetting about the old version of s but because python does
version of s but because python does memory management for you there's no
memory management for you there's no maloc there's no free python
maloc there's no free python automatically frees up the original
automatically frees up the original bytes like yees and hands them back to
bytes like yees and hands them back to the operating system for you all right
the operating system for you all right questions now on this
questions now on this technique questions on
technique questions on this in general I'll call out the python
this in general I'll call out the python documentation will start to be your
documentation will start to be your friend because in class we'll only
friend because in class we'll only scratch the surface with some of these
scratch the surface with some of these things but in docs. python.org for
things but in docs. python.org for instance there's a whole reference of
instance there's a whole reference of all of the built-in functions that come
all of the built-in functions that come with the language as well as for
with the language as well as for instance those with the string all right
instance those with the string all right well let me go ahead and before we take
well let me go ahead and before we take a break let's go ahead and create
a break let's go ahead and create something a little familiar to based on
something a little familiar to based on our week here in C let me propose that
our week here in C let me propose that we revisit those examples in involving
we revisit those examples in involving some meow so for instance when we had
some meow so for instance when we had our cat meow back in the first week and
our cat meow back in the first week and then second in C we did something that
then second in C we did something that was a little stupid at first whereby we
was a little stupid at first whereby we created a file as I'll do here this time
created a file as I'll do here this time called meow. p and if I want a cat to
called meow. p and if I want a cat to meow three times I could run it once
meow three times I could run it once like this little copy paste and now
like this little copy paste and now python of meow. py and I'm done now
python of meow. py and I'm done now we've visited this example like two
we've visited this example like two times at least now in scratch it and see
times at least now in scratch it and see it's correct I'll stipulate but what's
it's correct I'll stipulate but what's obviously poorly
obviously poorly designed what's the fault here
designed what's the fault here yeah it should just be a loop right like
yeah it should just be a loop right like why type it three times literally
why type it three times literally copying and pasting is almost always a
copying and pasting is almost always a bad thing except in C when you have the
bad thing except in C when you have the function prototypes that you need to
function prototypes that you need to borrow but in this case this is just
borrow but in this case this is just inefficient so what could we do better
inefficient so what could we do better here in Python well in Python we could
here in Python well in Python we could probably change this in a few different
probably change this in a few different ways we could borrow some of the syntax
ways we could borrow some of the syntax we proposed in slide form earlier like
we proposed in slide form earlier like give me a variable called I set it to
give me a variable called I set it to zero no semicolon while I is less than
zero no semicolon while I is less than three if I want to do this three times I
three if I want to do this three times I can go ahead and print out meow and then
can go ahead and print out meow and then I can do i+ equals one and I think this
I can do i+ equals one and I think this would do the trick python of meow. and
would do the trick python of meow. and we're back in business already well if I
we're back in business already well if I wanted to change this to a for Loop well
wanted to change this to a for Loop well in Python it would be a little tighter
in Python it would be a little tighter but this would not be the best approach
but this would not be the best approach so for I in uh
so for I in uh 012 I could just do print meow like this
012 I could just do print meow like this and that too would get the job done but
and that too would get the job done but to my to our discussion earlier this
to my to our discussion earlier this would get stupid pretty quickly if you
would get stupid pretty quickly if you had to keep enumerating all of these
had to keep enumerating all of these values like what did we introduce
values like what did we introduce instead the the range function exactly
instead the the range function exactly so that hands me back way more
so that hands me back way more efficiently just the values I want
efficiently just the values I want indeed one at a time so even this if I
indeed one at a time so even this if I run it a third a third or fourth time
run it a third a third or fourth time we've got the same result but now let's
we've got the same result but now let's transition to where we went with this
transition to where we went with this back in the day how can we start to
back in the day how can we start to modularize this like just like it would
modularize this like just like it would be nice I claimed if MIT had given us a
be nice I claimed if MIT had given us a meow function wouldn't it be nice if
meow function wouldn't it be nice if like python had given us a meow function
like python had given us a meow function maybe less compelling in Python but how
maybe less compelling in Python but how can I build my own function well I did
can I build my own function well I did this briefly with the spell checker
this briefly with the spell checker earlier but let me go ahead and propose
earlier but let me go ahead and propose that we could Implement now our own
that we could Implement now our own version of this in python as follows let
version of this in python as follows let me go ahead and start fresh here and use
me go ahead and start fresh here and use the keyword defa so this did not exist
the keyword defa so this did not exist in C you had the return value the
in C you had the return value the function name the arguments in Python
function name the arguments in Python you literally say defa to define a
you literally say defa to define a function you give it a name like meow
function you give it a name like meow and now now I'm going to go ahead and in
and now now I'm going to go ahead and in this function just print out meow and
this function just print out meow and this lets me change it to anything else
this lets me change it to anything else I want in the future but for now it's an
I want in the future but for now it's an abstraction and in fact I can uh move it
abstraction and in fact I can uh move it out of sight out of mind just going to
out of sight out of mind just going to hit enter a bunch of times to pretend
hit enter a bunch of times to pretend like now it exists but I don't care how
like now it exists but I don't care how it is implemented and up here now I can
it is implemented and up here now I can do something like this 4 I in range of
do something like this 4 I in range of three let me go ahead and not print meow
three let me go ahead and not print meow anymore let me just call meow and
anymore let me just call meow and tightening up my code further but I
tightening up my code further but I think let's see python of meow. py this
think let's see python of meow. py this is I think going to be the first time it
is I think going to be the first time it does not work
does not work correctly okay so here we have sadly our
correctly okay so here we have sadly our first python error and let's see the
first python error and let's see the syntax is going to be different from C
syntax is going to be different from C or clangs output traceback is like the
or clangs output traceback is like the term of art here this is like a trace
term of art here this is like a trace back of all of the lines of code that
back of all of the lines of code that were just executed or really functions
were just executed or really functions you called the file name is
you called the file name is uninteresting this is like my codes
uninteresting this is like my codes space specifically but the file name is
space specifically but the file name is important here meow. py uh line two is
important here meow. py uh line two is is the issue okay I didn't get very far
is the issue okay I didn't get very far before I screwed up and then there's a
before I screwed up and then there's a name error and you'll see in Python
name error and you'll see in Python there's typically these capitalized uh
there's typically these capitalized uh keywords that hint at what the issue is
keywords that hint at what the issue is it's something related to names of
it's something related to names of variables meow is not defined all right
variables meow is not defined all right you're programming python for the first
you're programming python for the first time you've screwed up you're following
time you've screwed up you're following some online tutorial you're seeing this
some online tutorial you're seeing this reason through it like why might meow
reason through it like why might meow not be defined what can weer infer about
not be defined what can weer infer about python how to troubleshoot
python how to troubleshoot logically is it me ised after maybe is
logically is it me ised after maybe is it because meow is defined after you
it because meow is defined after you know as smart as python seems to be Visa
know as smart as python seems to be Visa VC they have some similar design
VC they have some similar design characteristics so let's try that so let
characteristics so let's try that so let me scroll all the way back down to where
me scroll all the way back down to where I move this earlier let me uh get rid of
I move this earlier let me uh get rid of it way down there I'll copy it to my
it way down there I'll copy it to my clipboard and let me just kind of hack
clipboard and let me just kind of hack something together let me just put it up
something together let me just put it up here and let's see if this works so now
here and let's see if this works so now let me clear my terminal run python of
let me clear my terminal run python of meow. okay we're back in business so
meow. okay we're back in business so that was actually really good intuition
that was actually really good intuition good debugging technique to sort of
good debugging technique to sort of reason through it now this is kind of
reason through it now this is kind of contradicting what I claimed back in
contradicting what I claimed back in week one which was that you know the
week one which was that you know the main part of your program ideally should
main part of your program ideally should just be at the top of the file like
just be at the top of the file like don't make me look for it it's not a
don't make me look for it it's not a huge deal with like a four-line program
huge deal with like a four-line program but if you've got 40 lines 400 lines you
but if you've got 40 lines 400 lines you don't want like the juice juicy part of
don't want like the juice juicy part of your program to be way down here and all
your program to be way down here and all of these functions way up here so it
of these functions way up here so it would be nice maybe if we actually have
would be nice maybe if we actually have a main function and so it actually turns
a main function and so it actually turns out to be a convention in Python to
out to be a convention in Python to define a main function it's not a
define a main function it's not a special function that's automatically
special function that's automatically called like in C but humans realized you
called like in C but humans realized you know what that was a pretty useful
know what that was a pretty useful feature let me Define a function called
feature let me Define a function called main let me indent these lines
main let me indent these lines underneath it let me practice what I'm
underneath it let me practice what I'm preaching which is put the main code at
preaching which is put the main code at the top of the file and wonderfully in
the top of the file and wonderfully in Python now you do not need prototypes
Python now you do not need prototypes there's none of that hackish copying and
there's none of that hackish copying and pasting of the return type the name and
pasting of the return type the name and the arguments to a function like we
the arguments to a function like we needed in C this is now
needed in C this is now okay instead except for one Minor Detail
okay instead except for one Minor Detail let me go ahead and run python of
let me go ahead and run python of meow. hopefully now I've solve this
meow. hopefully now I've solve this problem by having a main function but
problem by having a main function but now nothing has happened all right even
now nothing has happened all right even if you've never programmed in Python
if you've never programmed in Python before What might explain this
before What might explain this behavior and how do I
behavior and how do I fix again when you're off in the real
fix again when you're off in the real world learning some new language all you
world learning some new language all you have is deductive logic to debug yeah I
have is deductive logic to debug yeah I remember
right so the solution to be clear in C was that we had to put the Prototype up
was that we had to put the Prototype up here otherwise we'd get an error message
here otherwise we'd get an error message in this case I'm actually not getting an
in this case I'm actually not getting an error message and indeed I'll claim that
error message and indeed I'll claim that you don't need the prototypes in Python
you don't need the prototypes in Python just not necessary because that was
just not necessary because that was annoying if nothing
annoying if nothing else but what else might explain yeah
else but what else might explain yeah I'm
I'm back yeah maybe you have to call Main
back yeah maybe you have to call Main itself if main is not some some special
itself if main is not some some special status in Python maybe just because it
status in Python maybe just because it exists isn't enough and indeed if you
exists isn't enough and indeed if you want to call Maine the new convention is
want to call Maine the new convention is actually going to be as the very last
actually going to be as the very last line of your program typically to
line of your program typically to literally call Main it's a little stupid
literally call Main it's a little stupid stupid looking but you know they made a
stupid looking but you know they made a design decision and this is how now we
design decision and this is how now we work around it python of meowy now we're
work around it python of meowy now we're back in business but now logically why
back in business but now logically why does this work the way it does well in
does this work the way it does well in this case top to bottom line one is
this case top to bottom line one is telling python to define a fun fun
telling python to define a fun fun called Main and then Define it as
called Main and then Define it as follows lines two and three but it's not
follows lines two and three but it's not calling main yet line six is telling
calling main yet line six is telling python how to define a function called
python how to define a function called meow but it's not calling these lines
meow but it's not calling these lines yet now line 10 you're telling python
yet now line 10 you're telling python call Main and at that point python has
call Main and at that point python has been trained if you will to know what
been trained if you will to know what main is on line one to know what meow is
main is on line one to know what meow is on line six and so it's now perfectly
on line six and so it's now perfectly okay for Maine to be above meow because
okay for Maine to be above meow because you never called them yet you defined
you never called them yet you defined defined and then you called and that's
defined and then you called and that's the logic behind this any questions now
the logic behind this any questions now on the structure of this technique
on the structure of this technique here now let's do one more then recall
here now let's do one more then recall that the last thing we did in scratch
that the last thing we did in scratch and in Python uh scratch and in C was to
and in Python uh scratch and in C was to actually
actually parameterize uh these same function so
parameterize uh these same function so suppose that you don't want Maine to be
suppose that you don't want Maine to be responsible for the loop here you
responsible for the loop here you instead want to very simply do something
instead want to very simply do something like meow three times and be done with
like meow three times and be done with it well in Python it's going to be
it well in Python it's going to be similar in spirit toy but again we don't
similar in spirit toy but again we don't need to keep mentioning data types if
need to keep mentioning data types if you want me now to take some argument
you want me now to take some argument like a number n you can just specify n
like a number n you can just specify n as the name of that argument or you
as the name of that argument or you could call it anything else of course
could call it anything else of course that you want you don't have to specify
that you want you don't have to specify int or anything else in your code now
int or anything else in your code now inside of meow you can do something like
inside of meow you can do something like for I in let's say I definitely now
for I in let's say I definitely now can't do this because like that would be
can't do this because like that would be weird to start the list and end it with
weird to start the list and end it with n so if I can come back over here what's
n so if I can come back over here what's the solution how can I do something n
the solution how can I do something n times yeah using range so range is nice
times yeah using range so range is nice cuz I can pass in now this variable n
cuz I can pass in now this variable n and now I can meow whoops now I can
and now I can meow whoops now I can print out quote unquote meow so it's
print out quote unquote meow so it's almost the same as in scratch almost the
almost the same as in scratch almost the same as in C but it's a little simpler
same as in C but it's a little simpler and if now I run meow. P I'll have the
and if now I run meow. P I'll have the ability now to do this here as well all
ability now to do this here as well all right questions on any of this right now
right questions on any of this right now we're sort of like taking the stroll
we're sort of like taking the stroll through week one we're going to
through week one we're going to momentarily escalate things to look not
momentarily escalate things to look not only at some of these basic but also
only at some of these basic but also other features like we saw with face
other features like we saw with face recognition with the speller or the like
recognition with the speller or the like um because of how many of us are here we
um because of how many of us are here we have a huge amount of candy out in the
have a huge amount of candy out in the lobby so why don't we go ahead and take
lobby so why don't we go ahead and take a 10-minute break and we come back we'll
a 10-minute break and we come back we'll do even fancier more powerful things
do even fancier more powerful things with python in 10 all right so we are
with python in 10 all right so we are back among our goals now are to
back among our goals now are to introduce a few more building blocks so
introduce a few more building blocks so that we can solve more interesting
that we can solve more interesting problems at the end much like those that
problems at the end much like those that we began with you recall from a few
we began with you recall from a few weeks ago we played with this sort of
weeks ago we played with this sort of two-dimensional Super Mario World and we
two-dimensional Super Mario World and we tried to print a vertical column of like
tried to print a vertical column of like three or more bricks well let me propose
three or more bricks well let me propose that we use this as an opportunity to
that we use this as an opportunity to now Tinker with some of Python's more uh
now Tinker with some of Python's more uh useful more userfriendly functionality
useful more userfriendly functionality as well so let me code a file called
as well so let me code a file called mario. py and let's just print out like
mario. py and let's just print out like that the equivalent of that vertical
that the equivalent of that vertical column so it's of height three each one
column so it's of height three each one is a hash so let's do for I in range of
is a hash so let's do for I in range of three initially and let's just print out
three initially and let's just print out a single hash and I think now python of
a single hash and I think now python of mario. py voila we're in business
mario. py voila we're in business printing out just that same pyramid
printing out just that same pyramid there or just that same column there
there or just that same column there what if though we want to print a column
what if though we want to print a column of like some variable height where the
of like some variable height where the user tells us how tall they want it to
user tells us how tall they want it to be well let me go up here for instance
be well let me go up here for instance and instead how about we'll use um let's
and instead how about we'll use um let's do this how about uh from cs50
do this how about uh from cs50 import how about the get in function as
import how about the get in function as before so it will deal with making sure
before so it will deal with making sure the user gives us an integer and now in
the user gives us an integer and now in the past whenever we wanted to get a
the past whenever we wanted to get a number from a user we've actually
number from a user we've actually followed a certain Paradigm in fact if I
followed a certain Paradigm in fact if I open up here for instance uh how about
open up here for instance uh how about Mario in how about Mario 1. C from a
Mario in how about Mario 1. C from a while back you might recall that we had
while back you might recall that we had code like this and we specifically use
code like this and we specifically use the do while loop and see whenever we
the do while loop and see whenever we want to like get something from the user
want to like get something from the user maybe again and again and again until
maybe again and again and again until they cooperate at which point we finally
they cooperate at which point we finally break out of the loop so it turns out
break out of the loop so it turns out python does have while Loops does have
python does have while Loops does have four Loops does not have do while loops
four Loops does not have do while loops and yet pretty much anytime you've
and yet pretty much anytime you've gotten user input you've probably used
gotten user input you've probably used this Paradigm so it turns out that the
this Paradigm so it turns out that the python equivalent of this is to do
python equivalent of this is to do similar in spirit but using only a while
similar in spirit but using only a while loop and a common Paradigm in python as
loop and a common Paradigm in python as I alluded earlier is to actually
I alluded earlier is to actually deliberately induce an infinite Loop
deliberately induce an infinite Loop while true capital T and then do what
while true capital T and then do what you want to do like get an INT from the
you want to do like get an INT from the user and prompt them for the height for
user and prompt them for the height for instance in question and then if you're
instance in question and then if you're sure that the user has given you what
sure that the user has given you what you want like n is greater than zero
you want like n is greater than zero which is what I want in this case cuz I
which is what I want in this case cuz I want a positive integer otherwise
want a positive integer otherwise there's nothing to print you literally
there's nothing to print you literally just break out of the loop and so we
just break out of the loop and so we could actually use this technique in C
could actually use this technique in C it's just not really done in C you could
it's just not really done in C you could absolutely in C have done a while true
absolutely in C have done a while true loop with the parentheses lowercase true
loop with the parentheses lowercase true you could break out of it and so forth
you could break out of it and so forth but in Python this is like the python
but in Python this is like the python way and this is actually a term of art
way and this is actually a term of art this way in Python is pythonic like this
this way in Python is pythonic like this is the way everyone does it quote
is the way everyone does it quote unquote doesn't mean you have to but
unquote doesn't mean you have to but that's sort of the way like the cool
that's sort of the way like the cool python programmers would Implement an
python programmers would Implement an idea like this trying to do something
idea like this trying to do something again and again and again until the user
again and again and again until the user actually cooperates but all we've done
actually cooperates but all we've done is take away the do while loop but still
is take away the do while loop but still logically we can implement the same idea
logically we can implement the same idea now below this let me go ahead and just
now below this let me go ahead and just print out for I in range of n this time
print out for I in range of n this time because I want it to be variable and not
because I want it to be variable and not three I can go ahead and print out the
three I can go ahead and print out the hash let me go ahead and get rid of the
hash let me go ahead and get rid of the C version here open my terminal window
C version here open my terminal window and I'll run again python of mario. py
and I'll run again python of mario. py I'll type in three and I get back those
I'll type in three and I get back those three hashes but if I instead type in
three hashes but if I instead type in four I now get four hashes instead so
four I now get four hashes instead so the takeaway here is quite simply that
the takeaway here is quite simply that this would be the way for instance to
this would be the way for instance to actually get back a value in Python that
actually get back a value in Python that is consistent with some parameter like
is consistent with some parameter like greater than zero how about this let's
greater than zero how about this let's actually uh practice what we preached a
actually uh practice what we preached a moment ago with our meowing examples and
moment ago with our meowing examples and kind of factoring all this out let me go
kind of factoring all this out let me go ahead and Define a main function as
ahead and Define a main function as before let me go ahead and assume for
before let me go ahead and assume for the moment that a get height function
the moment that a get height function exists which is not a thing in Python
exists which is not a thing in Python I'm going to invent it in just a moment
I'm going to invent it in just a moment and now I'm going to go ahead and do
and now I'm going to go ahead and do something like this for I in the range
something like this for I in the range of that height well let's go ahead and
of that height well let's go ahead and print out those hashes so I'm assuming
print out those hashes so I'm assuming that get height exists let me go ahead
that get height exists let me go ahead and Implement that abstraction so Define
and Implement that abstraction so Define a function now called get height it's
a function now called get height it's not going to take any arguments in this
not going to take any arguments in this design while true I can go ahead and do
design while true I can go ahead and do the same thing as before assign a
the same thing as before assign a variable n the return value of get int
variable n the return value of get int prompting the user for that height and
prompting the user for that height and then if n is greater than zero I can go
then if n is greater than zero I can go ahead and break but if I break here I
ahead and break but if I break here I logically just like can see end up
logically just like can see end up executing below the loop in question but
executing below the loop in question but there's nothing there but if I want get
there's nothing there but if I want get height to return the height what should
height to return the height what should I type here on line 14
logically what do I want to return to be clear yeah so I actually want to return
clear yeah so I actually want to return n and here's another curiosity of python
n and here's another curiosity of python Visa BC there doesn't seem to be an
Visa BC there doesn't seem to be an issue of scope anymore right in C it was
issue of scope anymore right in C it was super important to not only declare your
super important to not only declare your variables with the data types you also
variables with the data types you also had to be mindful of like where they
had to be mindful of like where they exist inside of those curly braces in
exist inside of those curly braces in Python it turns out you can be a little
Python it turns out you can be a little looser with things for better for worse
looser with things for better for worse and so on line 11 if I create a variable
and so on line 11 if I create a variable called n it exists on line 11 12 and
called n it exists on line 11 12 and even 13
even 13 outside of the while loop so to be clear
outside of the while loop so to be clear in C with a while loop we would have
in C with a while loop we would have ordinarily had not the colon we would
ordinarily had not the colon we would have had the curly brace like here and
have had the curly brace like here and over here and a week ago I would have
over here and a week ago I would have claimed that in C N does not exist
claimed that in C N does not exist outside of the while loop by nature of
outside of the while loop by nature of those curly braces even though the curly
those curly braces even though the curly braces are gone python actually allows
braces are gone python actually allows you to use a variable anytime after you
you to use a variable anytime after you have assigned it a value so slightly
have assigned it a value so slightly more powerful as such how however I can
more powerful as such how however I can tighten this up a little bit logically
tighten this up a little bit logically and this is true in C I don't really
and this is true in C I don't really need to break out of the loop by using
need to break out of the loop by using break recall that or know that I can
break recall that or know that I can actually once I'm ready to go I can just
actually once I'm ready to go I can just return the value I care about even
return the value I care about even inside of the loop and that will have
inside of the loop and that will have the side effect of breaking me out of
the side effect of breaking me out of the loop and also breaking me out of and
the loop and also breaking me out of and returning from the entire function so
returning from the entire function so nothing too new here in terms of C
nothing too new here in terms of C versus python except for this issue of
versus python except for this issue of scope and I indeed returned n at the
scope and I indeed returned n at the bottom there just to make clear that n
bottom there just to make clear that n would still exist so either of those are
would still exist so either of those are correct now I just have a Python program
correct now I just have a Python program that I think is going to allow me to
that I think is going to allow me to implement this same Mario idea so let's
implement this same Mario idea so let's run python of mario. and okay so nothing
run python of mario. and okay so nothing happened uh python of mario.
happened uh python of mario. py what did I do wrong yeah I have to
py what did I do wrong yeah I have to call Main so at the bottom of my code I
call Main so at the bottom of my code I have to call Main here and this is a
have to call Main here and this is a stylistic detail that's been subtle um
stylistic detail that's been subtle um generally speaking when when you are
generally speaking when when you are writing in Python um there's not a cs50
writing in Python um there's not a cs50 style guide per se there's actually a
style guide per se there's actually a python style guide that most people
python style guide that most people adhere to um it's and in this case
adhere to um it's and in this case double blank lines between functions is
double blank lines between functions is the norm I'm doing that deliberately
the norm I'm doing that deliberately although uh it might otherwise not be
although uh it might otherwise not be obvious but now that I've called main on
obvious but now that I've called main on line 16 let's run mario. once more aha
line 16 let's run mario. once more aha now we get there now we see it type in
now we get there now we see it type in three and I'm back in business printing
three and I'm back in business printing out the values there
out the values there yeah
sure why do I need the if condition at all why can't I just return n here as by
all why can't I just return n here as by doing return n or if I really want to be
doing return n or if I really want to be succinct I could technically just do
succinct I could technically just do this the only reason I added the if
this the only reason I added the if condition is because if the user types
condition is because if the user types in negative 1 -2 I wanted to prompt them
in negative 1 -2 I wanted to prompt them again and again that's all but that
again and again that's all but that would be totally acceptable too if you
would be totally acceptable too if you were okay with that result instead well
were okay with that result instead well let me do one other thing here to point
let me do one other thing here to point out why we are using get in so
out why we are using get in so frequently this new training wheel all
frequently this new training wheel all be it temporarily so let me go back to
be it temporarily so let me go back to the way it was a moment ago and let me
the way it was a moment ago and let me propose now to take away get int I
propose now to take away get int I claimed earlier that if you're not using
claimed earlier that if you're not using get int you can just use the input
get int you can just use the input function itself from
function itself from python but that always returns a string
python but that always returns a string or a stir and so recall that you have to
or a stir and so recall that you have to pass the output of the input function to
pass the output of the input function to an INT either on the same line or if you
an INT either on the same line or if you prefer on another line instead but it
prefer on another line instead but it turns out what I didn't do was show you
turns out what I didn't do was show you what happens if you uh don't cooperate
what happens if you uh don't cooperate with the user uh with the program so if
with the user uh with the program so if I run python of mario. now works great
I run python of mario. now works great even without the get int function and I
even without the get int function and I can do it with four still works great
can do it with four still works great but let me clear my terminal and be
but let me clear my terminal and be difficult now as the user and type in C
difficult now as the user and type in C for the height instead enter now we see
for the height instead enter now we see one of those tracebacks again this one
one of those tracebacks again this one is different this isn't a name error but
is different this isn't a name error but apparently a value error and if I kind
apparently a value error and if I kind of ignore the stuff I don't understand I
of ignore the stuff I don't understand I can see invalid literal for INT with
can see invalid literal for INT with base 10 cat that's a super cryptic way
base 10 cat that's a super cryptic way of saying that c a is not a number in
of saying that c a is not a number in decimal notation and so I would seem to
decimal notation and so I would seem to have to somehow handle this case and if
have to somehow handle this case and if you want to be more Curious you'll see
you want to be more Curious you'll see that this is indeed a trace back and um
that this is indeed a trace back and um C tends to do this too or the debugger
C tends to do this too or the debugger would do this for you too you can see
would do this for you too you can see all of the functions that have been
all of the functions that have been called to get you to this point so
called to get you to this point so apparently my problem is initially in
apparently my problem is initially in line 14 but line 14 if I keep scrolling
line 14 but line 14 if I keep scrolling is uninteresting it's main but line 14
is uninteresting it's main but line 14 leads me to execute line two which is
leads me to execute line two which is indeed in main that leads me to execute
indeed in main that leads me to execute line nine which is in get height and
line nine which is in get height and okay here's the issue so the closest
okay here's the issue so the closest line number to the error message is the
line number to the error message is the one that probably reveals the most line
one that probably reveals the most line nine is where my issue is so I can't
nine is where my issue is so I can't just blindly ask the user for input and
just blindly ask the user for input and then convert it to an INT if they're not
then convert it to an INT if they're not going to give me an INT now how do we
going to give me an INT now how do we deal with this well back in problem set
deal with this well back in problem set two you might recall validating that the
two you might recall validating that the user typed in a number and using a for
user typed in a number and using a for Loop and the like well it turns out
Loop and the like well it turns out there's a better way to do this in
there's a better way to do this in Python and the are kind of there if you
Python and the are kind of there if you want to try to convert something for a
want to try to convert something for a number to a number that might not
number to a number that might not actually be a number turns out Python
actually be a number turns out Python and certain other languages literally
and certain other languages literally have a keyword called try and if only
have a keyword called try and if only this existed for the past few weeks I
this existed for the past few weeks I know but like you can try to do the
know but like you can try to do the following with your code what do I want
following with your code what do I want to try to do well I want to try to
to try to do well I want to try to execute those few lines except if
execute those few lines except if there's an error so I can say except if
there's an error so I can say except if there's a value error specifically the
there's a value error specifically the one I screwed up and created a moment
one I screwed up and created a moment ago and if there is a value error I can
ago and if there is a value error I can print out an informative message to the
print out an informative message to the user like not an integer or anything
user like not an integer or anything else and what's happening here now is
else and what's happening here now is literally this operative word try the
literally this operative word try the pro python is going to try to get input
pro python is going to try to get input and try to convert it to an in and it's
and try to convert it to an in and it's going to try to check if it's greater
going to try to check if it's greater than zero and then try to return it all
than zero and then try to return it all why all of three of those lines are
why all of three of those lines are inside of indented underneath the tri
inside of indented underneath the tri block except if something goes wrong
block except if something goes wrong specifically a value error happens then
specifically a value error happens then it prints this but it doesn't return
it prints this but it doesn't return anything and because I'm in a loop that
anything and because I'm in a loop that means it's going to do it again and
means it's going to do it again and again and again until the human actually
again and again until the human actually cooperates and gives me an actual number
cooperates and gives me an actual number and so this too is what the world would
and so this too is what the world would call pythonic in Python you don't
call pythonic in Python you don't necessarily rigorously try to validate
necessarily rigorously try to validate the users's input make sure they haven't
the users's input make sure they haven't screwed up you honestly take a more
screwed up you honestly take a more laxidasical approach and just try to do
laxidasical approach and just try to do something but catch an error if it
something but catch an error if it happens so catch is also a term of art
happens so catch is also a term of art even though it's not a keyword here
even though it's not a keyword here except if something happens you handle
except if something happens you handle it so you try and you handle it it's
it so you try and you handle it it's sort of best effort programming if you
sort of best effort programming if you will but this is baked into the mindset
will but this is baked into the mindset of the Python uh programming community
of the Python uh programming community so now if I do python of mario. py and I
so now if I do python of mario. py and I cooperate works great as before try and
cooperate works great as before try and succeed three Works four works if though
succeed three Works four works if though I try and fail by typing in cat it
I try and fail by typing in cat it doesn't crash per se it doesn't show me
doesn't crash per se it doesn't show me an error it shows me something more user
an error it shows me something more user friendly like not an integer and then I
friendly like not an integer and then I can try again with dog not an integer I
can try again with dog not an integer I can try again with five and now it works
can try again with five and now it works so we won't generally have you write
so we won't generally have you write much in the way of these try except
much in the way of these try except blocks only because they get a little
blocks only because they get a little sophisticated quickly but that is to
sophisticated quickly but that is to reveal what the get int function is
reveal what the get int function is doing this is why we give you the
doing this is why we give you the training wheels so that when you want to
training wheels so that when you want to get an INT you don't have to jump
get an INT you don't have to jump through all these annoying Hoops to do
through all these annoying Hoops to do so but that's all the library is really
so but that's all the library is really doing for you is just try and accept you
doing for you is just try and accept you won't be left with any training wheels
won't be left with any training wheels ultimately questions now on getting
ultimately questions now on getting inputs and trying in this way
yeah Tri block it say that oh you could you put the condition outside of the tri
you put the condition outside of the tri block short answer yes and in fact I
block short answer yes and in fact I struggled with this last night when
struggled with this last night when tweaking this example to show the
tweaking this example to show the simplest version I will disclaim that
simplest version I will disclaim that really I should only be trying literally
really I should only be trying literally to do the The Fragile part and then down
to do the The Fragile part and then down here I should be really doing what
here I should be really doing what you're proposing which is do the
you're proposing which is do the condition out here the problem is though
condition out here the problem is though that logically this gets messy quickly
that logically this gets messy quickly right because except if there's a value
right because except if there's a value error I want to print out not an integer
error I want to print out not an integer I can't compare n against zero then
I can't compare n against zero then because n doesn't exist because there
because n doesn't exist because there was an error so it turns out and I'll
was an error so it turns out and I'll show you this this is now the advanced
show you this this is now the advanced version of python there's actually an
version of python there's actually an else keyword you can use in Python that
else keyword you can use in Python that does not accompany if or L if it
does not accompany if or L if it accompanies try and accept which I think
accompanies try and accept which I think is weirdly confusing a different word
is weirdly confusing a different word would have been better but if you really
would have been better but if you really prefer I could have done this instead
prefer I could have done this instead dead and this is one of these design
dead and this is one of these design things where like reasonable people will
things where like reasonable people will disagree generally speaking you should
disagree generally speaking you should only try to do the one line that might
only try to do the one line that might very well fail but honestly this looks
very well fail but honestly this looks kind of stupid now it's just
kind of stupid now it's just unnecessarily complicated and so my own
unnecessarily complicated and so my own preference was actually the original
preference was actually the original which was yeah I'm trying a few extra
which was yeah I'm trying a few extra lines that really aren't going to fail
lines that really aren't going to fail mathematically but it's just tighter
mathematically but it's just tighter it's cleaner this way and here's again
it's cleaner this way and here's again the sort of like you know arguments
the sort of like you know arguments you'll start to make yourself as you get
you'll start to make yourself as you get more comfortable with programming you'll
more comfortable with programming you'll have an opinion you'll disagree with
have an opinion you'll disagree with someone and so long as you can back your
someone and so long as you can back your argument up pretty
argument up pretty probably all right so how about we now
probably all right so how about we now take away some piece of magic that's
take away some piece of magic that's been here for a while let me go ahead
been here for a while let me go ahead and uh Delete all of this here and let
and uh Delete all of this here and let me propose that we revisit uh not that
me propose that we revisit uh not that vertical column and the exceptions that
vertical column and the exceptions that might result from getting input but
might result from getting input but these like horizontal question marks
these like horizontal question marks that we saw a while ago so I want all of
that we saw a while ago so I want all of those question marks on the same line
those question marks on the same line and yet I worry we're about to see a
and yet I worry we're about to see a challenge here because print up until
challenge here because print up until now has been putting new lines
now has been putting new lines everywhere automatically even without
everywhere automatically even without those backslash NS well let me propose
those backslash NS well let me propose that we do this for I in the range of
that we do this for I in the range of four if I want four question marks let
four if I want four question marks let me just print four question marks
me just print four question marks unfortunately I don't think this is
unfortunately I don't think this is correct yet let me run python of mario.
correct yet let me run python of mario. and of course this gives me a column
and of course this gives me a column instead of the row of question marks
instead of the row of question marks that I want so how do we do this well it
that I want so how do we do this well it turns out if you read the documentation
turns out if you read the documentation for the print function it turns out that
for the print function it turns out that print not surprisingly perhaps takes a
print not surprisingly perhaps takes a lot of different arguments as well and
lot of different arguments as well and in fact if you go to the documentation
in fact if you go to the documentation for it you'll see that it takes not just
for it you'll see that it takes not just positional arguments that is from left
positional arguments that is from left to right separated by commas turns out
to right separated by commas turns out python has supports a fancier feature
python has supports a fancier feature with arguments where you can pass the
with arguments where you can pass the names of arguments to functions too so
names of arguments to functions too so what do I mean by this if I go back to
what do I mean by this if I go back to vs code here and I've read the
vs code here and I've read the documentation it turns out that yes as
documentation it turns out that yes as before you can pass multiple arguments
before you can pass multiple arguments to python like this like hello comma
to python like this like hello comma David comma me that will just
David comma me that will just automatically concatenate all three of
automatically concatenate all three of those positional arguments together
those positional arguments together they're positional in the sense that
they're positional in the sense that they literally flow from left to right
they literally flow from left to right separated by commas but if you don't
separated by commas but if you don't want to just pass in values like that
want to just pass in values like that you want to actually print out as I did
you want to actually print out as I did before a question mark but you want to
before a question mark but you want to override the default behavior of print
override the default behavior of print by changing the line ending you can
by changing the line ending you can actually do this you can use the name of
actually do this you can use the name of an argument that you know exists from
an argument that you know exists from the documentation set it equal to some
the documentation set it equal to some alternative value and in fact even
alternative value and in fact even though this looks cryptic this is how I
though this looks cryptic this is how I would override the end of each line to
would override the end of each line to be quote unquote that is nothing because
be quote unquote that is nothing because if you read the documentation the
if you read the documentation the default value for this end argument does
default value for this end argument does someone want to guess is is back sln so
someone want to guess is is back sln so if you read the documentation you'll see
if you read the documentation you'll see that back sln is the implied default for
that back sln is the implied default for this end argument and so if you want to
this end argument and so if you want to change it you just say end equals
change it you just say end equals something else and so here I can change
something else and so here I can change change it to nothing and now rerun
change it to nothing and now rerun python of mario. and now they're all on
python of mario. and now they're all on the same line now looks a little stupid
the same line now looks a little stupid cuz I made that sort of week one mistake
cuz I made that sort of week one mistake where I still need to move the cursor to
where I still need to move the cursor to the next line that's just a different
the next line that's just a different problem I'm just going to go over here
problem I'm just going to go over here and print nothing I don't even need to
and print nothing I don't even need to print back sln because if print
print back sln because if print automatically gives you a backslash n
automatically gives you a backslash n just call print with nothing and you'll
just call print with nothing and you'll get that for free so let me rerun python
get that for free so let me rerun python of mario. Pi and now it looks a little
of mario. Pi and now it looks a little prettier at the prompt and to be super
prettier at the prompt and to be super clear as to what's going on suppose I
clear as to what's going on suppose I want to sort of make an exclamation here
want to sort of make an exclamation here I could change the back sln default to
I could change the back sln default to like an exclamation point Just for kicks
like an exclamation point Just for kicks and if I run python of mario. py again
and if I run python of mario. py again now I get this sort of you know
now I get this sort of you know exclamation with question marks and
exclamation with question marks and exclamation points as well so that's all
exclamation points as well so that's all that's going on here and this is what's
that's going on here and this is what's called a named argument it literally has
called a named argument it literally has a name that you can specify when calling
a name that you can specify when calling it in and it's different from positional
it in and it's different from positional in that you're literally using the
in that you're literally using the name let me propose something else
name let me propose something else though and this is why people kind of
though and this is why people kind of like python there's just kind of cool
like python there's just kind of cool ways to do things that's kind of a you
ways to do things that's kind of a you know ver it's a three line verbose way
know ver it's a three line verbose way of printing out four question marks you
of printing out four question marks you know I could certainly take the you know
know I could certainly take the you know shortcut and just do this but that's not
shortcut and just do this but that's not really that interesting for anyone
really that interesting for anyone especially if I want to do it a variable
especially if I want to do it a variable number of times but python does let you
number of times but python does let you do this if you want to uh multiply a
do this if you want to uh multiply a character some number of times not only
character some number of times not only can you use Plus for concatenation you
can you use Plus for concatenation you can use star or an asterisk for
can use star or an asterisk for multiplication if you will that is
multiplication if you will that is concatenation again and again and again
concatenation again and again and again so if I just print out quote unquote
so if I just print out quote unquote question mark Time 4 that's actually
question mark Time 4 that's actually going to be the tightest way the most
going to be the tightest way the most distinct way I can print four question
distinct way I can print four question marks instead and if I don't use four I
marks instead and if I don't use four I use n where I get n from the user bang
use n where I get n from the user bang like now I've gotten rid of the four
like now I've gotten rid of the four Loop entirely and I'm using the the star
Loop entirely and I'm using the the star operator to manipulate it instead and to
operator to manipulate it instead and to be super clear here in so far as python
be super clear here in so far as python does not have milock or free or memory
does not have milock or free or memory management that you have to do guess
management that you have to do guess what python also doesn't
have anything on your minds the past couple of
couple of weeks doesn't have pointers yes so
weeks doesn't have pointers yes so python does not have pointers which just
python does not have pointers which just means that all of that happens for you
means that all of that happens for you automatically underneath the hood Again
automatically underneath the hood Again by way of code that someone else wrote
by way of code that someone else wrote how about one more throwback with Mario
how about one more throwback with Mario we've talked about in week one this sort
we've talked about in week one this sort of two-dimensional structure where it's
of two-dimensional structure where it's like I claim like 3x3 a grid of bricks
like I claim like 3x3 a grid of bricks if you will well how can we do this in
if you will well how can we do this in Python we can do this in a couple of
Python we can do this in a couple of ways now let me go back to my mario. py
ways now let me go back to my mario. py and let me do something like for I in
and let me do something like for I in range of we'll just do three even though
range of we'll just do three even though I know now I could use get int or I
I know now I could use get int or I could use input and int and if I want to
could use input and int and if I want to do something two-dimensionally just like
do something two-dimensionally just like in C you can Nest your for Loop so maybe
in C you can Nest your for Loop so maybe I could do 4J in range of three and then
I could do 4J in range of three and then in here I could print out a uh hash
in here I could print out a uh hash symbol and then let's see if that gives
symbol and then let's see if that gives me nine total so if I've got a nested
me nine total so if I've got a nested Loop like this python of mario.
Loop like this python of mario. hopefully gives me a grid no it gave me
hopefully gives me a grid no it gave me a column of
a column of nine why
nine why logically even though I've got my row
logically even though I've got my row and my
and my columns
columns yeah yeah the line ending so in my row I
yeah yeah the line ending so in my row I can't let print just keep adding new
can't let print just keep adding new line adding new lines so I just have to
line adding new lines so I just have to override this here and let me not screw
override this here and let me not screw up like before let me print one at the
up like before let me print one at the end of the whole row just to move the
end of the whole row just to move the cursor down and I think now together now
cursor down and I think now together now we've got our 3x3 of course we could
we've got our 3x3 of course we could tighten this up further like if I don't
tighten this up further like if I don't like the nested loop I probably could go
like the nested loop I probably could go in here and just print out for instance
in here and just print out for instance a uh a brick times three or I could
a uh a brick times three or I could change the three to a variable if I've
change the three to a variable if I've gotten it from the user so I can tighten
gotten it from the user so I can tighten this up further so again just different
this up further so again just different ways to solve the same problem and again
ways to solve the same problem and again sort of evidence of why a lot of people
sort of evidence of why a lot of people like python there's just some more
like python there's just some more pleasant ways to solve problems without
pleasant ways to solve problems without getting into the weeds constantly of
getting into the weeds constantly of doing things like like with um uh for
doing things like like with um uh for loops and wild Loops endlessly all right
loops and wild Loops endlessly all right well how about some other building
well how about some other building blocks lists are going to be so
blocks lists are going to be so incredibly useful in Python just as
incredibly useful in Python just as arrays were in C but arrays are annoying
arrays were in C but arrays are annoying because you have to manage the memory
because you have to manage the memory yourself you have to know in advance how
yourself you have to know in advance how big they are or you have to use pointers
big they are or you have to use pointers and malok or realloc to resize them like
and malok or realloc to resize them like oh my God like the past two weeks have
oh my God like the past two weeks have been painful in that sense but python
been painful in that sense but python does this all for free for you in fact
does this all for free for you in fact there's a whole bunch of functions that
there's a whole bunch of functions that come with python that involve lists and
come with python that involve lists and they'll ow us ultimately um to do things
they'll ow us ultimately um to do things again and again and again uh with uh
again and again and again uh with uh within the same data structure and for
within the same data structure and for instance we'll be able to get the length
instance we'll be able to get the length of a list you don't have to remember it
of a list you don't have to remember it yourself in a variable you can just ask
yourself in a variable you can just ask python how many elements are in this
python how many elements are in this list and with this I think we can solve
list and with this I think we can solve some some old problems too so let me go
some some old problems too so let me go back here to vs code let me close Mario
back here to vs code let me close Mario and give us a new program called scores.
and give us a new program called scores. piy and rather than show the C and the
piy and rather than show the C and the python now let's just focus on Python
python now let's just focus on Python and in scores. C way back when we just
and in scores. C way back when we just averaged like three test scores or
averaged like three test scores or something like that 72 73 and 33 a few
something like that 72 73 and 33 a few weeks ago so if I want to create a list
weeks ago so if I want to create a list in this python version of 72 73 33 I
in this python version of 72 73 33 I just use my square bracket notation C
just use my square bracket notation C let you use curly braces if you know the
let you use curly braces if you know the values in advance but Python's just this
values in advance but Python's just this and now if I want to compute the average
and now if I want to compute the average in pi in C recall I did something with a
in pi in C recall I did something with a loop I added all the values together I
loop I added all the values together I then divided by the total number values
then divided by the total number values just like you would in grade school and
just like you would in grade school and that gave me the average well python
that gave me the average well python comes with a lot of super handy
comes with a lot of super handy functions not just length but others as
functions not just length but others as well and so in fact if you want to
well and so in fact if you want to compute the average you can take the sum
compute the average you can take the sum of all of those scores and divide it by
of all of those scores and divide it by the length of all of those scores so
the length of all of those scores so python comes with length comes with sum
python comes with length comes with sum you can just pass in a whole list of any
you can just pass in a whole list of any size and let it deal with that problem
size and let it deal with that problem for you so if I want to now print out
for you so if I want to now print out this average I can print out average
this average I can print out average colon and then I'll plug in my average
colon and then I'll plug in my average uh string for variable for interpolation
uh string for variable for interpolation let me make this an F string so that it
let me make this an F string so that it gets formatted and let me just run
gets formatted and let me just run python of scores. Pi and there's my
python of scores. Pi and there's my average it's sort of rounding weird
average it's sort of rounding weird because we're still vulnerable to some
because we're still vulnerable to some floating point in Precision but at least
floating point in Precision but at least I didn't need loops and I didn't have to
I didn't need loops and I didn't have to write all this darn code just to do
write all this darn code just to do something that you know Excel and Google
something that you know Excel and Google spreadsheets can just do like that well
spreadsheets can just do like that well python is closer to those kinds of tools
python is closer to those kinds of tools but more powerful and that you can
but more powerful and that you can manipulate the data yourself how about
manipulate the data yourself how about though if I want to um get a bunch of
though if I want to um get a bunch of scores manually from the user and then
scores manually from the user and then sum them together well let's combine a
sum them together well let's combine a few ideas here how about this first let
few ideas here how about this first let me go ahead and uh import um the cs50 LI
me go ahead and uh import um the cs50 LI get in function from the cs50 library
get in function from the cs50 library just so we don't have to deal with try
just so we don't have to deal with try and accept or all of that and let me go
and accept or all of that and let me go ahead and give myself an empty list and
ahead and give myself an empty list and this is powerful in py in C there's
this is powerful in py in C there's really there's no point to an empty
really there's no point to an empty array because if you create an empty
array because if you create an empty array with square bracket notation like
array with square bracket notation like it's not useful for anything but in
it's not useful for anything but in Python you can create it empty because
Python you can create it empty because python will grow and shrink the list for
python will grow and shrink the list for you automatically as you add things to
you automatically as you add things to it so if I want to get three scores from
it so if I want to get three scores from the user I could do something like this
the user I could do something like this for I in range of three and then I can
for I in range of three and then I can grab a variable called score or anything
grab a variable called score or anything I could call get int prompt the human
I could call get int prompt the human for the score that they want to type in
for the score that they want to type in and then once they do I can do this
and then once they do I can do this thinking back to our objectoriented
thinking back to our objectoriented programming capability now I could do
programming capability now I could do scores dot a pen
scores dot a pen and I Canen that score to it and you
and I Canen that score to it and you would only know this from having read
would only know this from having read the documentation heard it in class in a
the documentation heard it in class in a book or whatnot but it turns out that
book or whatnot but it turns out that just like strings have functions like
just like strings have functions like lower built into them lists have
lower built into them lists have functions like append built into them
functions like append built into them that just literally appends to the end
that just literally appends to the end of the list for you and python will grow
of the list for you and python will grow or Shrink it as needed no more Malo or C
or Shrink it as needed no more Malo or C or Realo or the like so this just
or Realo or the like so this just appends to the scores array the scores
appends to the scores array the scores list that score and then again and again
list that score and then again and again and again so the array starts at
and again so the array starts at sorry the list starts at size zero then
sorry the list starts at size zero then grows to one then two then three without
grows to one then two then three without you having to do anything else and so
you having to do anything else and so now down here I can compute an average
now down here I can compute an average with the sum of those scores divided by
with the sum of those scores divided by the length of the total number of scores
the length of the total number of scores and to be clear length is the total
and to be clear length is the total number of elements in the list doesn't
number of elements in the list doesn't matter how big the values themselves are
matter how big the values themselves are now I can go ahead and print out an FST
now I can go ahead and print out an FST string uh with something like average
string uh with something like average colon average and curly braces and and
colon average and curly braces and and if I run python of scores. piy I'll type
if I run python of scores. piy I'll type in just for the sake of discussion the
in just for the sake of discussion the three values I still get the same answer
three values I still get the same answer but that would have been painful to do
but that would have been painful to do in see unless you committ it in advance
in see unless you committ it in advance to a fixed size array which we already
to a fixed size array which we already decided weeks ago was annoying or uh you
decided weeks ago was annoying or uh you uh grew it dynamically using malok or
uh grew it dynamically using malok or realloc or the like all right what else
realloc or the like all right what else can I do well there's some nice things
can I do well there's some nice things you might as well know exist um instead
you might as well know exist um instead of scores. aen you can do slight
of scores. aen you can do slight fanciness like this like if you want to
fanciness like this like if you want to append something to a list you can
append something to a list you can actually do plus equals and then put
actually do plus equals and then put that thing in a a temporary list of its
that thing in a a temporary list of its own and just use what is essentially
own and just use what is essentially concatenation but not concatenation of
concatenation but not concatenation of strings but concatination of lists so
strings but concatination of lists so this new line six appends to the scores
this new line six appends to the scores list this tiny little list I'm
list this tiny little list I'm temporarily creating with just the
temporarily creating with just the current new score so just another piece
current new score so just another piece of syntax that's worth seeing that
of syntax that's worth seeing that allows you to do something like that as
allows you to do something like that as well all right well how about we go back
well all right well how about we go back to strings for a moment and all these
to strings for a moment and all these examples as always are on the course's
examples as always are on the course's website afterward suppose we want to do
website afterward suppose we want to do something like converting characters to
something like converting characters to uppercase well to be clear I could do
uppercase well to be clear I could do something like this let me create a
something like this let me create a program called uppercase dop let me
program called uppercase dop let me prompt the user for a before string as
prompt the user for a before string as by using the input function or get
by using the input function or get string which is almost the same and I'll
string which is almost the same and I'll prompt the user for a string
prompt the user for a string beforeand then let me go ahead and print
beforeand then let me go ahead and print out uh how about the keyword after
out uh how about the keyword after and then end the new line with nothing
and then end the new line with nothing just so that I can see before on one
just so that I can see before on one line and after on the next line and then
line and after on the next line and then let me do this and here's where python
let me do this and here's where python gets pleasant too with loops for C in
gets pleasant too with loops for C in before print c. uper n equals quote
before print c. uper n equals quote unquote and then I'll print this here
unquote and then I'll print this here all right that was fast but let's try to
all right that was fast but let's try to infer what's going on so line one just
infer what's going on so line one just gets input from the user stores it in a
gets input from the user stores it in a variable called before line two
variable called before line two literally just prints after but doesn't
literally just prints after but doesn't move the new line to uh the cursor to
move the new line to uh the cursor to the next line what it then does is this
the next line what it then does is this and in C this was a little more annoying
and in C this was a little more annoying you needed a for loop with I you needed
you needed a for loop with I you needed array in uh notation with the square
array in uh notation with the square brackets but python if you say four
brackets but python if you say four variable in string so for c for
variable in string so for c for character in string Python's going to
character in string Python's going to automatically assign C to the first word
automatically assign C to the first word letter that the user types in then on
letter that the user types in then on the next iteration the second letter the
the next iteration the second letter the third letter and the fourth so you don't
third letter and the fourth so you don't need any square bracket notation you
need any square bracket notation you just you see and python will do it for
just you see and python will do it for you and just hand you back one at a time
you and just hand you back one at a time each of the letters that the user has
each of the letters that the user has typed in so if I go back over here and I
typed in so if I go back over here and I run for instance python of uppercase
run for instance python of uppercase dopy and I'll type in how about uh David
dopy and I'll type in how about uh David in all lowercase and hit enter you'll
in all lowercase and hit enter you'll now see that it's all uppercase instead
now see that it's all uppercase instead by iterating over it indeed one
by iterating over it indeed one character at a time but we already know
character at a time but we already know thanks to object-oriented programming
thanks to object-oriented programming strings themselves have the
strings themselves have the functionality built in to not just
functionality built in to not just uppercase Single Character
uppercase Single Character but the whole string so honestly this
but the whole string so honestly this was a bit of a silly exercise I don't
was a bit of a silly exercise I don't need to use a loop anymore like in see
need to use a loop anymore like in see and so some of the habits you've only
and so some of the habits you've only just developed in recent weeks it's time
just developed in recent weeks it's time to start breaking them when they're not
to start breaking them when they're not necessary I can create a variable called
necessary I can create a variable called after set it equal to before do uper
after set it equal to before do uper which indeed exists just like lower
which indeed exists just like lower exists and then what I can go ahead and
exists and then what I can go ahead and print out is for instance uh let's get
print out is for instance uh let's get rid of this print line here and do it at
rid of this print line here and do it at the end after and print the value of
the end after and print the value of that variable so now if I rerun
that variable so now if I rerun uppercase Pi type in David and all
uppercase Pi type in David and all lowercase I can just uppercase the whole
lowercase I can just uppercase the whole thing all at once because again in C in
thing all at once because again in C in Python you don't have to operate on
Python you don't have to operate on characters individually questions on any
characters individually questions on any of these tricks up until
of these tricks up until now now all right how about a few other
now now all right how about a few other techniques that we saw and C that we'll
techniques that we saw and C that we'll bring back now in Python so it turns out
bring back now in Python so it turns out in Python there are other librar you can
in Python there are other librar you can use two that unlock even more
use two that unlock even more functionality so in C if you wanted
functionality so in C if you wanted command line arguments you just change
command line arguments you just change the proo the signature for main to be
the proo the signature for main to be void instead of void to be int argc
void instead of void to be int argc comma string argv Open brackets for an
comma string argv Open brackets for an array or Char star eventually well it
array or Char star eventually well it turns out in Python that if you want to
turns out in Python that if you want to access command line arguments it's a
access command line arguments it's a little simpler but they're tucked away
little simpler but they're tucked away in a library otherwise known as a module
in a library otherwise known as a module called CIS the CIS or system module now
called CIS the CIS or system module now this is similar in spirit to the cs50
this is similar in spirit to the cs50 library and that it's got a bunch of
library and that it's got a bunch of functionality built in but this one
functionality built in but this one comes with python itself so if I want to
comes with python itself so if I want to create a program like greet py in VSS
create a program like greet py in VSS code here let me go ahead and do this
code here let me go ahead and do this from the CIS Library let's import argv
from the CIS Library let's import argv and that's just a thing that exists it's
and that's just a thing that exists it's not built into main because there is no
not built into main because there is no main per se anymore so it's tucked away
main per se anymore so it's tucked away in that library and now I can do
in that library and now I can do something like this if the length of
something like this if the length of argv equals equals 2 well let's go ahead
argv equals equals 2 well let's go ahead and print out something friendly like
and print out something friendly like hello comma AR V bracket 1 and then
hello comma AR V bracket 1 and then close quotes else if the length of RV is
close quotes else if the length of RV is not equal to two let's just go ahead and
not equal to two let's just go ahead and print out hello world now at a glance
print out hello world now at a glance this might look a little cryptic but
this might look a little cryptic but it's identical to what we did a few
it's identical to what we did a few weeks ago when I run this python of
weeks ago when I run this python of greet with no arguments it just says
greet with no arguments it just says hello world but if I instead add a
hello world but if I instead add a command line argument like my first name
command line argument like my first name and hit enter now the length of arv is
and hit enter now the length of arv is no longer one it's going to be two and
no longer one it's going to be two and so it prints out hello David instead so
so it prints out hello David instead so the takeaway here is that whereas in C
the takeaway here is that whereas in C argv technically contained the name of
argv technically contained the name of your program like hello or dog greet and
your program like hello or dog greet and then everything the human typed Python's
then everything the human typed Python's a little different in that because we're
a little different in that because we're using The Interpreter in this way
using The Interpreter in this way Technically when you run python of greet
Technically when you run python of greet py the length of arv is only one it
py the length of arv is only one it contains only greet so the name of the
contains only greet so the name of the file it does not unnecessarily contain
file it does not unnecessarily contain python itself because what's the point
python itself because what's the point of that being there omnes it does
of that being there omnes it does contain the number of words that the
contain the number of words that the human typed after python itself so argv
human typed after python itself so argv is length one here argv is length two
is length one here argv is length two here and that's why when it did equal to
here and that's why when it did equal to I saw hello David instead of the default
I saw hello David instead of the default hello world so same ability to access
hello world so same ability to access command line arguments add these kinds
command line arguments add these kinds of inputs to your functions but you have
of inputs to your functions but you have to unlock it by way of using Arvy uh
to unlock it by way of using Arvy uh instead in this way if you want to see
instead in this way if you want to see all of the words you could do something
all of the words you could do something like this uh just as if we combine ideas
like this uh just as if we combine ideas here for I in range of how about length
here for I in range of how about length of
of arv then I can do this print argv
arv then I can do this print argv bracket I all right a little cryptic but
bracket I all right a little cryptic but line three is just a for Loop iterating
line three is just a for Loop iterating over the range of length of argv so if
over the range of length of argv so if the human types in two words the length
the human types in two words the length of argv will be two so this is just a
of argv will be two so this is just a way of saying iterate over all of the
way of saying iterate over all of the words in arv printing them one at a time
words in arv printing them one at a time so python of greet dopy enter just
so python of greet dopy enter just prints out the name of the program
prints out the name of the program python of greet dopy with David prints
python of greet dopy with David prints out greet dopy and then David I can keep
out greet dopy and then David I can keep running it though with more words and
running it though with more words and they'll each get printed one at a time
they'll each get printed one at a time but what's nice too about Python and
but what's nice too about Python and this is the point of this exercise
this is the point of this exercise honestly this looks pretty cryptic this
honestly this looks pretty cryptic this is not very pleasant to look at if you
is not very pleasant to look at if you just want to iterate over every word in
just want to iterate over every word in a list which argv is watch what I can do
a list which argv is watch what I can do I can do for ARG or any variable name in
I can do for ARG or any variable name in ARG V let me just now print out that
ARG V let me just now print out that argument I could keep calling it I but I
argument I could keep calling it I but I seems weird when it's not a number so
seems weird when it's not a number so I'm changing to AR as a word instead if
I'm changing to AR as a word instead if I now do python of greet py it does this
I now do python of greet py it does this if I do python of greet David it does
if I do python of greet David it does that again David ma it does that again
that again David ma it does that again so this is again why Python's just very
so this is again why Python's just very appealing you want to do something this
appealing you want to do something this many times iterate over a list just say
many times iterate over a list just say it and it reads a little more like
it and it reads a little more like English and there's even other fanciness
English and there's even other fanciness too if I may it's a little stupid that I
too if I may it's a little stupid that I keep seeing the name of the program
keep seeing the name of the program greet dopy so I it'd be nice if I could
greet dopy so I it'd be nice if I could remove that python also supports what
remove that python also supports what are called slices of arrays sorry slices
are called slices of arrays sorry slices of lists even though I get the
of lists even though I get the terminology confused if Arvy is a list
terminology confused if Arvy is a list then it's going to print out everything
then it's going to print out everything in it but if I want a slice of it that
in it but if I want a slice of it that starts at location one all the way to
starts at location one all the way to the end you can use this funky syntax in
the end you can use this funky syntax in between the square brackets which we've
between the square brackets which we've not seen yet that's going to start at
not seen yet that's going to start at item one and go all the way to the end
item one and go all the way to the end and so this is a nice clever way of
and so this is a nice clever way of slicing off if you will the very first
slicing off if you will the very first element because now when I run greet doy
element because now when I run greet doy David men I should only see David and
David men I should only see David and men if I only want one element I could
men if I only want one element I could do one to two if I want all of them I
do one to two if I want all of them I could do zero onward I could give myself
could do zero onward I could give myself just two of one of them in this way so
just two of one of them in this way so you can play with the start value and
you can play with the start value and the end value in this way to sort of
the end value in this way to sort of slice and dice these lists in different
slice and dice these lists in different ways that would have been a pain in see
ways that would have been a pain in see just because we didn't really have the
just because we didn't really have the built-in support for manipulating arrays
built-in support for manipulating arrays as cleanly as this all right just so
as cleanly as this all right just so you've seen it too though this one is
you've seen it too though this one is less uh exciting to see live if I go
less uh exciting to see live if I go ahead and create a quick program here it
ahead and create a quick program here it turns out there's something else in the
turns out there's something else in the CIS Library the ability to exit programs
CIS Library the ability to exit programs either exiting with status code one or
either exiting with status code one or zero as we've been doing anytime
zero as we've been doing anytime something goes right or wrong so for
something goes right or wrong so for instance Let Me Whip up a quick program
instance Let Me Whip up a quick program that just says if the length of cy.
that just says if the length of cy. argv uh does not equal two then let's y
argv uh does not equal two then let's y at the user and say you're missing a
at the user and say you're missing a command line argument otherwise command
command line argument otherwise command line argument and let's then return cy.
line argument and let's then return cy. exit one else let's go ahead and
exit one else let's go ahead and logically just say print a formatted
logically just say print a formatted string that says hello as before cy.
string that says hello as before cy. arv1 now things look different all of a
arv1 now things look different all of a sudden but I'm doing something
sudden but I'm doing something deliberately first let's see what this
deliberately first let's see what this does so on line one I'm importing not
does so on line one I'm importing not argv specifically I'm importing the
argv specifically I'm importing the whole CIS library and we'll see why in
whole CIS library and we'll see why in second well it turns out that this Arvy
second well it turns out that this Arvy the CIS library has not only the Arvy
the CIS library has not only the Arvy list it also has a function called exit
list it also has a function called exit which I'd like to be able to use as well
which I'd like to be able to use as well so it turns out that if you import a
so it turns out that if you import a whole library in this way that's fine
whole library in this way that's fine but you have to refer to the things
but you have to refer to the things inside of it by using that same
inside of it by using that same library's name and a DOT to sort of
library's name and a DOT to sort of namespace it so to speak so here I'm
namespace it so to speak so here I'm just saying if the user types in does
just saying if the user types in does not type in two words yell at them with
not type in two words yell at them with missing command line argument and then
missing command line argument and then exit with one just like in C when you do
exit with one just like in C when you do exit one just means something went wrong
exit one just means something went wrong otherwise print out hello to this and
otherwise print out hello to this and this is starting to look cryptic but
this is starting to look cryptic but it's just a combination of ideas the
it's just a combination of ideas the curly braces means interpolate this
curly braces means interpolate this value plug it in here cy. Arvy is just
value plug it in here cy. Arvy is just the verbose way of saying go into the
the verbose way of saying go into the CIS library and get the argv variable
CIS library and get the argv variable therein and bracket one of course just
therein and bracket one of course just like arrays and C is just the second
like arrays and C is just the second element at the prompt so when I run this
element at the prompt so when I run this version now python of exit. py with no
version now python of exit. py with no arguments I get yelled at in this way if
arguments I get yelled at in this way if however I type in two arguments total
however I type in two arguments total the name of the file and my own name now
the name of the file and my own name now I get greeted with hello David and it's
I get greeted with hello David and it's the same idea before this was a very
the same idea before this was a very low-level technique but same thing here
low-level technique but same thing here if you do Echo dollar sign question mark
if you do Echo dollar sign question mark enter you'll see the exit code of your
enter you'll see the exit code of your program so if I do this incorrectly
program so if I do this incorrectly again let me rerun it without my name
again let me rerun it without my name enter I get yelled at but if I do Echo
enter I get yelled at but if I do Echo dollar sign question mark there's the
dollar sign question mark there's the secret one that's returned again just to
secret one that's returned again just to show You parody with C in this case
show You parody with C in this case questions now on any of these techniques
here all right how about something that's a little more powerful too we
that's a little more powerful too we spent so much time in week zero and one
spent so much time in week zero and one doing searching and then eventually
doing searching and then eventually sorting in week three well it turns out
sorting in week three well it turns out python can help with some of this too
python can help with some of this too let me go ahead and create a program
let me go ahead and create a program called names. py that's just going to be
called names. py that's just going to be an opportunity to maybe search over a
an opportunity to maybe search over a whole bunch of names let me go ahead and
whole bunch of names let me go ahead and import CIS and then just so I have
import CIS and then just so I have access to exit and let me go ahead and
access to exit and let me go ahead and create a variable called names that's
create a variable called names that's going to be a list with a whole bunch of
going to be a list with a whole bunch of names uh how about Here Charlie and Fred
names uh how about Here Charlie and Fred and George and Jenny and Percy and
and George and Jenny and Percy and lastly Ron so a whole bunch of names
lastly Ron so a whole bunch of names here and you know it'd be a little
here and you know it'd be a little Annoying to implement code that iterates
Annoying to implement code that iterates over that from left to right and see
over that from left to right and see searching for one of those names in fact
searching for one of those names in fact what name well let's go ahead and ask
what name well let's go ahead and ask the user to input the name that they
the user to input the name that they want to search for so that we can tell
want to search for so that we can tell them if the name is there or not and we
them if the name is there or not and we could do this similar to C in Python
could do this similar to C in Python doing something like this so for n in
doing something like this so for n in names where n is just a variable to
names where n is just a variable to iterate over each name if how about the
iterate over each name if how about the name I'm looking for equals the current
name I'm looking for equals the current name in the list AKA n let's print out
name in the list AKA n let's print out something friendly like found and then
something friendly like found and then let's do cy. Exit 0 to indicate that we
let's do cy. Exit 0 to indicate that we found whoever that is otherwise if we
found whoever that is otherwise if we get all the way to the bottom here
get all the way to the bottom here outside of this Loop let's just print
outside of this Loop let's just print not found because if we haven't exited
not found because if we haven't exited yet and then let's just exit with one
yet and then let's just exit with one just to be clear I can continue
just to be clear I can continue importing all of CIS or I could do from
importing all of CIS or I could do from CIS import exit and then I could get rid
CIS import exit and then I could get rid of CIS dot everywhere else but you know
of CIS dot everywhere else but you know sometimes it's helpful to know exactly
sometimes it's helpful to know exactly where functions came from so this two is
where functions came from so this two is just a matter of style in this case all
just a matter of style in this case all right so let's go ahead and run this
right so let's go ahead and run this python of names. piy and let's look for
python of names. piy and let's look for like Ron all the way at the end you know
like Ron all the way at the end you know all right he's found and let's search
all right he's found and let's search for someone outside of the family here
for someone outside of the family here like herion not found okay so it seems
like herion not found okay so it seems to be working in this way but I've
to be working in this way but I've essentially implemented what
essentially implemented what algorithm what algorithm would this seem
algorithm what algorithm would this seem to be per lines seven and eight and nine
to be per lines seven and eight and nine and
and 10 yeah so it's just linear search it's
10 yeah so it's just linear search it's a loop even though the syntax is a
a loop even though the syntax is a little more succinct today and it's just
little more succinct today and it's just iterating over the whole thing well
iterating over the whole thing well honestly we've seen an even more tur way
honestly we've seen an even more tur way to do this in Python and this again is
to do this in Python and this again is what makes it a more pleasant language
what makes it a more pleasant language sometimes why don't I just do this
sometimes why don't I just do this instead in of iterating one at a time
instead in of iterating one at a time why don't I just say this let me go
why don't I just say this let me go ahead and change my condition to just be
ahead and change my condition to just be How about if the name we're looking for
How about if the name we're looking for is in the names list we're done we found
is in the names list we're done we found it use the in preposition that we've
it use the in preposition that we've seen a couple of times now that itself
seen a couple of times now that itself asks the question is something in
asks the question is something in something else and python will take care
something else and python will take care of linear search for us and it's going
of linear search for us and it's going to work exactly the same if I do python
to work exactly the same if I do python of names. search for run it's still
of names. search for run it's still going to find him and it's still going
going to find him and it's still going to do it linearly in this case but I
to do it linearly in this case but I don't have to write all of the lower
don't have to write all of the lower level code myself in this
level code myself in this case questions now on any of this the
case questions now on any of this the code's just getting shorter and
code's just getting shorter and shorter now what about uh let's see what
shorter now what about uh let's see what else might we have here how about this
else might we have here how about this it turns out let's go ahead and
it turns out let's go ahead and Implement that phone book that we
Implement that phone book that we started metaphorically with in the
started metaphorically with in the beginning of the course let's code up a
beginning of the course let's code up a program called phonebook dopy and in
program called phonebook dopy and in this case let's go ahead and let's
this case let's go ahead and let's create a dictionary this time recall
create a dictionary this time recall that a dictionary is a little something
that a dictionary is a little something that implements something like this like
that implements something like this like a two column table that's got keys and
a two column table that's got keys and values words and definitions names and
values words and definitions names and numbers and let's focus on the last of
numbers and let's focus on the last of those names and numbers in this case
those names and numbers in this case well I claimed earlier that python has
well I claimed earlier that python has built-in support for dictionaries dict
built-in support for dictionaries dict objects that you can create with one
objects that you can create with one line I didn't need it for speller
line I didn't need it for speller because a set is sufficient when you
because a set is sufficient when you only want one of the keys or the values
only want one of the keys or the values not both but now I want some names and
not both but now I want some names and numbers so it turns out in pyth python
numbers so it turns out in pyth python you can create an empty dictionary by
you can create an empty dictionary by saying dict open parenthesis close and
saying dict open parenthesis close and that just gives you essentially a chart
that just gives you essentially a chart that looks like this with nothing in it
that looks like this with nothing in it or there's more succinct syntax you can
or there's more succinct syntax you can alternatively do uh this with two curly
alternatively do uh this with two curly braces instead and in fact I've been
braces instead and in fact I've been using a shortcut all this time when I
using a shortcut all this time when I had a list earlier where my
had a list earlier where my variable uh was called scores and I did
variable uh was called scores and I did this that was actually the shorthand
this that was actually the shorthand version of this hey python give me an
version of this hey python give me an empty list so there's different Syntax
empty list so there's different Syntax for achieving the same goal in this case
for achieving the same goal in this case if I want a dictionary for people I can
if I want a dictionary for people I can either do this or more commonly just two
either do this or more commonly just two curly braces like that all right well
curly braces like that all right well what do I want to put in this well let
what do I want to put in this well let me actually put some things in this and
me actually put some things in this and I'm going to just move my Clos curly
I'm going to just move my Clos curly brace to a new line if I want to
brace to a new line if I want to implement this idea of keys and values
implement this idea of keys and values the way you do this in Python is key
the way you do this in Python is key colon value comma key colon value so
colon value comma key colon value so you'd implement it more in code so for
you'd implement it more in code so for instance if I want Carter to be the
instance if I want Carter to be the first key in my phone book and I want
first key in my phone book and I want his number to be+ one 617495 1000 I can
his number to be+ one 617495 1000 I can put that as the corresponding value the
put that as the corresponding value the colon is in between both are strings or
colon is in between both are strings or stirs so I've quoted both deliberately
stirs so I've quoted both deliberately if I want to add myself I can put a
if I want to add myself I can put a comma and then just to keep things
comma and then just to keep things pretty I'm moving the cursor to the next
pretty I'm moving the cursor to the next line but that's not strictly required
line but that's not strictly required aesthetically it's just good style and
aesthetically it's just good style and here I might do+ 1 949 468 uh
here I might do+ 1 949 468 uh 2750 and now I have a dictionary that
2750 and now I have a dictionary that essentially has two rows here David uh
essentially has two rows here David uh Carter and his number and David and his
Carter and his number and David and his number as well and if I kept adding to
number as well and if I kept adding to this this call this chart would just get
this this call this chart would just get longer and longer suppose I want to
longer and longer suppose I want to search for one of our numbers well let's
search for one of our numbers well let's prompt the user for the name for whose
prompt the user for the name for whose number you want to search by getting
number you want to search by getting string or you know what we don't need
string or you know what we don't need the cs50 library let's just use input
the cs50 library let's just use input and prompt the user for a name and now
and prompt the user for a name and now we can use this super syntax and just
we can use this super syntax and just say if name in people print the format
say if name in people print the format added string number colon and here we
added string number colon and here we can do this people bracket name okay so
can do this people bracket name okay so this is getting kind of cool kind of
this is getting kind of cool kind of quickly kind of confusingly so let me
quickly kind of confusingly so let me run this python of phone
run this python of phone book. let's type in Carter and indeed I
book. let's type in Carter and indeed I see his number let's run it again with
see his number let's run it again with David and I see my number here so what's
David and I see my number here so what's going on well it turns out that a
going on well it turns out that a dictionary is very similar in spirit to
dictionary is very similar in spirit to a list it's actually very similar in
a list it's actually very similar in spirit to an array in C but instead of
spirit to an array in C but instead of being limited to keys that are numbers
being limited to keys that are numbers like bracket 0o bracket 1 bracket two
like bracket 0o bracket 1 bracket two you can actually use words and that's
you can actually use words and that's all I'm doing here on line eight if I
all I'm doing here on line eight if I want to check for the name Carter which
want to check for the name Carter which is currently in this variable called
is currently in this variable called name I can index into my people
name I can index into my people dictionary using not a number but using
dictionary using not a number but using literally a string the name Carter or
literally a string the name Carter or David or anything else to make this
David or anything else to make this clear too notice that I'm at the moment
clear too notice that I'm at the moment you using this format string which is
you using this format string which is adding some undue complexity but I could
adding some undue complexity but I could clarify this perhaps further as this I
clarify this perhaps further as this I could give myself another variable
could give myself another variable called number set it equal to the people
called number set it equal to the people dictionary indexing into it using the
dictionary indexing into it using the current name and now I can shorten this
current name and now I can shorten this to make it clear that all I'm doing is
to make it clear that all I'm doing is printing the value of that and in fact I
printing the value of that and in fact I can do this even more cryptically if I
can do this even more cryptically if I this would be weird to do but if I only
this would be weird to do but if I only ever want to show David's phone number
ever want to show David's phone number and never Carter's I can literally quote
and never Carter's I can literally quote unquote index into the people dictionary
unquote index into the people dictionary because now when I run this even if I
because now when I run this even if I type Carter I'm going to get back my
type Carter I'm going to get back my number instead but that's all that's
number instead but that's all that's happening if I undo that because that's
happening if I undo that because that's now a bug but I index into it using the
now a bug but I index into it using the value of name dictionaries are just so
value of name dictionaries are just so wonderfully convenient because now you
wonderfully convenient because now you can associate anything with anything
can associate anything with anything else but not using numbers but entire
else but not using numbers but entire keywords instead so here's how if in
keywords instead so here's how if in spell we gave you not just words but
spell we gave you not just words but hundreds of thousands of definitions as
hundreds of thousands of definitions as well you could essentially store them as
well you could essentially store them as this and then when the human wants to
this and then when the human wants to look up a definition in a proper
look up a definition in a proper dictionary not just for spellchecking
dictionary not just for spellchecking you could index into the dictionary
you could index into the dictionary using square brackets and get back the
using square brackets and get back the definition in English as well
definition in English as well questions on this
yeah location a really good question so how
location a really good question so how to summarize how is python finding that
to summarize how is python finding that name within that dictionary this is
name within that dictionary this is where honestly speller in pet 5 is what
where honestly speller in pet 5 is what Python's all about so you have struggled
Python's all about so you have struggled are struggling with implementing your
are struggling with implementing your own spell checker and implementing your
own spell checker and implementing your own hash table and recall that per last
own hash table and recall that per last week the goal of a hash table is to
week the goal of a hash table is to ideally get constant time access not
ideally get constant time access not something linear which is slow and even
something linear which is slow and even better than something uh uh logarithmic
better than something uh uh logarithmic like log base 2 of n so Python and the
like log base 2 of n so Python and the really smart people who invented it they
really smart people who invented it they have written the code that does its best
have written the code that does its best to give you constant time searches of
to give you constant time searches of dictionaries and they're not always
dictionaries and they're not always going to succeed just as you and your
going to succeed just as you and your own problem set probably going to have
own problem set probably going to have some collisions once in a while and
some collisions once in a while and start to have chains of length lists of
start to have chains of length lists of words but this is where again you defer
words but this is where again you defer to someone else someone smarter than you
to someone else someone smarter than you someone with more time than you to solve
someone with more time than you to solve these problems for you and if you read
these problems for you and if you read Python's documentation you'll see that
Python's documentation you'll see that it doesn't guarantee constant time but
it doesn't guarantee constant time but it's going to ideally optimize the data
it's going to ideally optimize the data structure for you to get as fast as
structure for you to get as fast as possible and of all of the data
possible and of all of the data structures um like a dictionary a
structures um like a dictionary a hashtable is really like the Swiss army
hashtable is really like the Swiss army knife of computing because it just lets
knife of computing because it just lets you associate something with something
you associate something with something else and even though we keep focusing on
else and even though we keep focusing on names and numbers that's a really
names and numbers that's a really powerful thing because it's more
powerful thing because it's more powerful than lists and arrays which are
powerful than lists and arrays which are only numbers and something else now you
only numbers and something else now you can have any sorts of relationships
can have any sorts of relationships instead all right let me show a few
instead all right let me show a few other examples before we culminate with
other examples before we culminate with some more powerful techniques in pythons
some more powerful techniques in pythons thanks to libraries how about this
thanks to libraries how about this problem we encountered in week four
problem we encountered in week four which was this let me code up a program
which was this let me code up a program called again compare. Pi here but this
called again compare. Pi here but this time compare to Strings and not numbers
time compare to Strings and not numbers so let me for instance do uh get one
so let me for instance do uh get one string from the user called s just for
string from the user called s just for the sake of discussion let me get
the sake of discussion let me get another string from the user uh called T
another string from the user uh called T so that we can actually do some
so that we can actually do some comparison here and if s equals equals T
comparison here and if s equals equals T let's go ahead and print out that
let's go ahead and print out that they're the same else let's go ahead and
they're the same else let's go ahead and print out that they're different so this
print out that they're different so this is very similar to what we did in week
is very similar to what we did in week four but in week four recall we did this
four but in week four recall we did this spef specifically because we had
spef specifically because we had encountered a problem for instance if I
encountered a problem for instance if I run whoops
run whoops uh if I
run what's going on uh input T come
on oh the okay wow okay long day all right if I run the proper command python
right if I run the proper command python of compare. py then let's go ahead and
of compare. py then let's go ahead and type in something like cat in all
type in something like cat in all lowercase cats in all lowercase and
lowercase cats in all lowercase and they're the same uh if though I do this
they're the same uh if though I do this again with dog and dog they're the same
again with dog and dog they're the same and of course cat and dog they're
and of course cat and dog they're different but does anyone recall from
different but does anyone recall from two weeks ago when I typed in my name
two weeks ago when I typed in my name twice both identically capitalized what
twice both identically capitalized what did it say that they were in
did it say that they were in fact different and why was that like why
fact different and why was that like why were two strings in C different even
were two strings in C different even though I typed literally the same
though I typed literally the same thing two different places in memory so
thing two different places in memory so each string might look the same
each string might look the same aesthetically but of course was stored
aesthetically but of course was stored elsewhere in memory and yet python
elsewhere in memory and yet python appears to be using the equality
appears to be using the equality operator equals equals like you and I
operator equals equals like you and I would expect as humans actually
would expect as humans actually comparing for us Char by Char in each of
comparing for us Char by Char in each of those strings for actual equality so
those strings for actual equality so this is a feature of python in that it's
this is a feature of python in that it's just easier to do and why well this
just easier to do and why well this derives from the reality that in Python
derives from the reality that in Python there are no pointers anymore there's no
there are no pointers anymore there's no underlying memory management it's not up
underlying memory management it's not up to you now to worry about those lower
to you now to worry about those lower level details the language itself takes
level details the language itself takes care of that for you and so similarly if
care of that for you and so similarly if I do this and don't ask the user for two
I do this and don't ask the user for two strings but just one and then I do
strings but just one and then I do something like this how about give
something like this how about give myself a second variable T set it equal
myself a second variable T set it equal to s.
to s. capitalize which note is not the same as
capitalize which note is not the same as upper capitalize by Design per Python's
upper capitalize by Design per Python's documentation will only capitalize the
documentation will only capitalize the first letter for you I can now print out
first letter for you I can now print out say two F strings here what the value of
say two F strings here what the value of s is and then let me print out with
s is and then let me print out with another F string what the value of t is
another F string what the value of t is and recall that in C this was a problem
and recall that in C this was a problem because if you capitalize s and store it
because if you capitalize s and store it in t we accidentally capitalized both s
in t we accidentally capitalized both s and t but in this case in Python when I
and t but in this case in Python when I actually run this and type in cat in all
actually run this and type in cat in all lowercase the original s is unchanged
lowercase the original s is unchanged because when I use capitalize on line
because when I use capitalize on line three this is indeed capitalizing s but
three this is indeed capitalizing s but it's returning a copy of the result it
it's returning a copy of the result it cannot change S itself because again for
cannot change S itself because again for that technical term s is IM mutable
that technical term s is IM mutable strings once they exist cannot be
strings once they exist cannot be changed themselves but you can return
changed themselves but you can return copies and modified mutated copies of
copies and modified mutated copies of those same strings so in short all of
those same strings so in short all of those headaches we encountered in week
those headaches we encountered in week four are now solved really in the way
four are now solved really in the way you might expect and here's another one
you might expect and here's another one that we dwell on in week four with the
that we dwell on in week four with the colored uh liquid in glasses let me code
colored uh liquid in glasses let me code up a program called swap. pi and in
up a program called swap. pi and in swap. pi let me set x equal to 1 y equal
swap. pi let me set x equal to 1 y equal to 2 and then let me just print out an F
to 2 and then let me just print out an F string here so how about X X is this
string here so how about X X is this comma Y is that and then let me do that
comma Y is that and then let me do that twice just for the sake of demonstration
twice just for the sake of demonstration and in here recall that we had to create
and in here recall that we had to create a swap function but then we had to pass
a swap function but then we had to pass it in by reference with the Amper sand
it in by reference with the Amper sand and like oh my God like that was kind of
and like oh my God like that was kind of peak complexity and see well if you want
peak complexity and see well if you want to swap X and Y in Python you could do X
to swap X and Y in Python you could do X comma yal y comma X and now python of
comma yal y comma X and now python of swap whoops python of
swap whoops python of swap. and there we go all of that's
swap. and there we go all of that's handled for you sort of like a shell
handled for you sort of like a shell game without even a temporary variable
game without even a temporary variable in mind so what more can we do here how
in mind so what more can we do here how about a few final building blocks and
about a few final building blocks and these related now to files from that
these related now to files from that week four suppose that I want to save
week four suppose that I want to save some names and numbers in like a a CSV
some names and numbers in like a a CSV file comma separated values which is
file comma separated values which is like a very lightweight spreadsheet well
like a very lightweight spreadsheet well first let me create a uh phonebook CSV
first let me create a uh phonebook CSV file that just has name comma number as
file that just has name comma number as like the first row there but after after
like the first row there but after after that I'm going to go ahead now and code
that I'm going to go ahead now and code up a phonebook dopy program that
up a phonebook dopy program that actually allows me to add things to this
actually allows me to add things to this phone book so let me split my screen
phone book so let me split my screen here so that we can see the old and the
here so that we can see the old and the new and down here in my code for phone
new and down here in my code for phone book. in this new and improved version
book. in this new and improved version I'm going to actually import a whole
I'm going to actually import a whole other Library this one called CSV and
other Library this one called CSV and here too especially for people in data
here too especially for people in data science and the like really like being
science and the like really like being in a manipulate files and data that
in a manipulate files and data that might very well be stored in
might very well be stored in spreadsheets or csvs comma separated
spreadsheets or csvs comma separated values which we we saw briefly in week
values which we we saw briefly in week four in phonebook then it suffices to
four in phonebook then it suffices to just import CSV after reading the
just import CSV after reading the documentation therefore because this is
documentation therefore because this is going to give me functionality in code
going to give me functionality in code related to CSV files so here's how I
related to CSV files so here's how I might open a file in Python I literally
might open a file in Python I literally call open it's not fop now it's just
call open it's not fop now it's just open and I open this file called
open and I open this file called phonebook.com
phonebook.com going to open it in a pend mode not
going to open it in a pend mode not right where it would change the whole
right where it would change the whole thing I want to append new line at a
thing I want to append new line at a time uh after this I want to get maybe
time uh after this I want to get maybe how about a name from the user so let's
how about a name from the user so let's prompt the user for some input for their
prompt the user for some input for their name and then let's prompt the user for
name and then let's prompt the user for a number as well using input prompting
a number as well using input prompting for number all right and now this is a
for number all right and now this is a little cryptic and you'd only know this
little cryptic and you'd only know this from the documentation but if you want
from the documentation but if you want to write line rows to a CSV file that
to write line rows to a CSV file that you can then view in Excel or the like
you can then view in Excel or the like you can do this give me a variable
you can do this give me a variable called writer but I could call it
called writer but I could call it anything I want let me use a CSV do
anything I want let me use a CSV do writer function that comes with this CSV
writer function that comes with this CSV Library passing in the file this is like
Library passing in the file this is like saying hey python treat this open file
saying hey python treat this open file as a CSV file so that things are
as a CSV file so that things are separated with commas and nicely
separated with commas and nicely formatted in rows and columns now I'm
formatted in rows and columns now I'm going to do this use that writer to
going to do this use that writer to write a row well what do I want to write
write a row well what do I want to write I want to write a short list namely the
I want to write a short list namely the current name and the current number to
current name and the current number to that file but I don't want to use FR
that file but I don't want to use FR printf and percent s and all of that
printf and percent s and all of that stuff that we might have had in the past
stuff that we might have had in the past and now I just want to close the file
and now I just want to close the file let me reopen my ter teral let me run
let me reopen my ter teral let me run python of phone book. and let me type in
python of phone book. and let me type in how about David and then +1
how about David and then +1 949468 2750 and hold crossing my fingers
949468 2750 and hold crossing my fingers watching the actual CSV at top left my
watching the actual CSV at top left my code has just added me to the file and
code has just added me to the file and if I were to run it again for instance
if I were to run it again for instance with Carter in plus1 617495 1000
with Carter in plus1 617495 1000 crossing my fingers again we've updated
crossing my fingers again we've updated the file and it turns out there's code
the file and it turns out there's code now via which I can even read that file
now via which I can even read that file but I can first tighten this up just so
but I can first tighten this up just so you've seen it it turns out into python
you've seen it it turns out into python it's so common to open files and close
it's so common to open files and close them you know humans make mistakes and
them you know humans make mistakes and they often forget to close files which
they often forget to close files which might then end up using more memory than
might then end up using more memory than you intend so you can alternatively do
you intend so you can alternatively do this in Python so that you don't have to
this in Python so that you don't have to worry about closing files you can use
worry about closing files you can use this keyword instead you can say with
this keyword instead you can say with the opening of this file as a variable
the opening of this file as a variable called file do all of the following
called file do all of the following underneath so I'm indenting most of my
underneath so I'm indenting most of my code I'm using this new python specific
code I'm using this new python specific keyword called wi and this is just a
keyword called wi and this is just a matter of saying with the following
matter of saying with the following opening of the file do those next four
opening of the file do those next four lines of code and then automatically
lines of code and then automatically close it for me at the end of the
close it for me at the end of the indentation it's a minor optimization
indentation it's a minor optimization but this again is sort of the pythonic
but this again is sort of the pythonic way to do things instead how else might
way to do things instead how else might I do this too well it turns out that the
I do this too well it turns out that the code I've written here on line n
code I've written here on line n especially is a little fragile right if
especially is a little fragile right if any human opens the spreadsheet the CSV
any human opens the spreadsheet the CSV file in Excel Google spreadsheets Apple
file in Excel Google spreadsheets Apple numbers and maybe like moves the columns
numbers and maybe like moves the columns around just because maybe they're
around just because maybe they're fussing they SA it and they don't
fussing they SA it and they don't realize they've now changed my
realize they've now changed my assumptions I don't want to necessarily
assumptions I don't want to necessarily write name and number always in that
write name and number always in that order CU what if someone screws up and
order CU what if someone screws up and flips those two columns by literally
flips those two columns by literally dragging and dropping so it turns out
dragging and dropping so it turns out that instead of using a list here we can
that instead of using a list here we can use another feature of this Library as
use another feature of this Library as follows instead of using a writer
follows instead of using a writer there's something called a dictionary
there's something called a dictionary writer or dict writer that takes the
writer or dict writer that takes the same argument as input the file that's
same argument as input the file that's opened but now the one difference here
opened but now the one difference here is that you need to tell this dictionary
is that you need to tell this dictionary writer
writer that your field names are name and
that your field names are name and number and let me close the CSV here
number and let me close the CSV here name and number are the names of the
name and number are the names of the fields The Columns in this CSV file and
fields The Columns in this CSV file and when it comes time to write a new row
when it comes time to write a new row the syntax here is going to be a little
the syntax here is going to be a little uglier but it's just a dictionary the
uglier but it's just a dictionary the name I want to write to the dictionary
name I want to write to the dictionary is going to be whatever name the human
is going to be whatever name the human typed in the number that I want to write
typed in the number that I want to write to the the CSV file is going to be
to the the CSV file is going to be whatever the number the human typed in
whatever the number the human typed in but what's different next about this
but what's different next about this code is by simply using a dictionary
code is by simply using a dictionary writer here instead of the generic
writer here instead of the generic writer now the columns can be in this
writer now the columns can be in this order or this order or any order and the
order or this order or any order and the dictionary writer is going to figure out
dictionary writer is going to figure out based on the first line of text in that
based on the first line of text in that CSV where to put name where to put
CSV where to put name where to put number so if you flip them no big deal
number so if you flip them no big deal it's going to notice oh wait the columns
it's going to notice oh wait the columns changed and it's going to insert the
changed and it's going to insert the columns correctly so just again another
columns correctly so just again another more powerful feature that lets you
more powerful feature that lets you focus
focus on lets you focus on real work as
on lets you focus on real work as opposed to actually uh getting tied up
opposed to actually uh getting tied up in the weeds of writing code like this
in the weeds of writing code like this otherwise questions on this one as well
otherwise questions on this one as well but what we will do now is come full
but what we will do now is come full circle to some of the more uh
circle to some of the more uh sophisticated examples with which we
sophisticated examples with which we began and I'm going to go back over to
began and I'm going to go back over to my own Mac laptop here where I've got my
my own Mac laptop here where I've got my own terminal window up and running and I
own terminal window up and running and I was just going to introduce a couple of
was just going to introduce a couple of final libraries that really speak to
final libraries that really speak to just how powerful python can can be and
just how powerful python can can be and how quickly you can get up and running
how quickly you can get up and running to be fair can't necessarily do all of
to be fair can't necessarily do all of these things in the cloud like in code
these things in the cloud like in code spaces because you need access to your
spaces because you need access to your own speakers or microphone or the like
own speakers or microphone or the like so that's why I'm doing it on my own Mac
so that's why I'm doing it on my own Mac here but let me go ahead and open up a
here but let me go ahead and open up a program called
program called speech. and I'm not using VSS code here
speech. and I'm not using VSS code here I'm using a program called VI that's
I'm using a program called VI that's entirely terminal window based but it's
entirely terminal window based but it's going to allow me for instance to import
going to allow me for instance to import the python text to speech version three
the python text to speech version three Library I'm going to give myself a
Library I'm going to give myself a variable called engine that's going to
variable called engine that's going to be set equal to the python text to
be set equal to the python text to speech 3 libraries init method which is
speech 3 libraries init method which is just going to initialize this library
just going to initialize this library that relates to text to speech I'm going
that relates to text to speech I'm going to then use the engines say function to
to then use the engines say function to say something like how about hello comma
say something like how about hello comma world and then as my last line I'm going
world and then as my last line I'm going to say engine. runand wait capitalized
to say engine. runand wait capitalized as such to tell my program now to run
as such to tell my program now to run that speech and wait until it's done all
that speech and wait until it's done all right I'm going to save this file I'm
right I'm going to save this file I'm going to run python of speech. py and
going to run python of speech. py and I'm going to cross my fingers as always
I'm going to cross my fingers as always and hello world all right so now I have
and hello world all right so now I have a program that's actually synthesizing
a program that's actually synthesizing speech using a library like this how can
speech using a library like this how can I now modify this to be a little more
I now modify this to be a little more interesting well how about this let me
interesting well how about this let me go ahead and prompt the user for their
go ahead and prompt the user for their name like we've done several times here
name like we've done several times here using Python's built-in name function
using Python's built-in name function and now let me go ahead and use a format
and now let me go ahead and use a format string in conjunction with this Library
string in conjunction with this Library interpolating the value of name there
interpolating the value of name there and at least if my name is somewhat
and at least if my name is somewhat phonetically pronounceable let's go
phonetically pronounceable let's go ahead and run python ofp
ahead and run python ofp speech. type in my name and hello David
speech. type in my name and hello David okay it's a sort of weird choice of
okay it's a sort of weird choice of inflection but we're starting to
inflection but we're starting to synthesize voice not unlike Siri or
synthesize voice not unlike Siri or Google assistant or Alexa or the like
Google assistant or Alexa or the like now we can maybe do something a little
now we can maybe do something a little more advanced too in addition to
more advanced too in addition to synthesizing speech in this way we could
synthesizing speech in this way we could synthesize for instance uh an actual
synthesize for instance uh an actual graphic let me go ahead now and do
graphic let me go ahead now and do something like this let me create a
something like this let me create a program called
program called qr. I'm going to go ahead and import a
qr. I'm going to go ahead and import a library called OS which gives you access
library called OS which gives you access to operating system related
to operating system related functionality in Python I'm going to
functionality in Python I'm going to import a library I've pre-installed
import a library I've pre-installed called QR code which is a
called QR code which is a two-dimensional barcode that you might
two-dimensional barcode that you might have seen in the real world I'm going to
have seen in the real world I'm going to go ahead and create an image variable
go ahead and create an image variable using this QR code library's make
using this QR code library's make function which per its documentation
function which per its documentation takes a URL like one of cs50's own
takes a URL like one of cs50's own videos so we'll do this with YouTube uh
videos so we'll do this with YouTube uh /xv f
zj5 p g g0 so hopefully that's the right
p g g0 so hopefully that's the right lecture and now we've got image. saave
lecture and now we've got image. saave which is going to allow me to create a
which is going to allow me to create a file called qr. ping think back now on
file called qr. ping think back now on problem set 4 and how painful it was to
problem set 4 and how painful it was to save files we'll just use the save
save files we'll just use the save function now in Python and save this as
function now in Python and save this as a ping file portable Network graphic and
a ping file portable Network graphic and then lastly let's just go ahead and open
then lastly let's just go ahead and open uh the with the command open qr. ping on
uh the with the command open qr. ping on my Mac so that hopefully this just
my Mac so that hopefully this just automatically opens all right I'm going
automatically opens all right I'm going to go ahead and just double check my
to go ahead and just double check my syntax here so that I haven't made any
syntax here so that I haven't made any mistakes I'm going to go ahead and run
mistakes I'm going to go ahead and run python of qr. enter that opens up this
python of qr. enter that opens up this let me go ahead and zoom in if you've
let me go ahead and zoom in if you've got a phone handy and you'd like to scan
got a phone handy and you'd like to scan this code
this code here whether in person or
here whether in person or online I apologize you won't appreciate
online I apologize you won't appreciate it amazing okay and lastly let me go
it amazing okay and lastly let me go back into our speech example here uh
back into our speech example here uh create a final ending here on our final
create a final ending here on our final moments and how about we just say
moments and how about we just say something like this was cs50 like this
something like this was cs50 like this let's go ahead here fix my
let's go ahead here fix my capitalization just for tidiness get rid
capitalization just for tidiness get rid of the name and now with our final
of the name and now with our final flourish and your introduction to python
flourish and your introduction to python equipped here we go this was cs50 all
equipped here we go this was cs50 all right we'll see you next
right we'll see you next [Applause]
all right this is cs50 and this is already week seven and this is the week
already week seven and this is the week where we'll continue where we left off
where we'll continue where we left off with python introducing you to a bit
with python introducing you to a bit more syntax and capabilities of the
more syntax and capabilities of the language so you can solve like
language so you can solve like interesting problems but a lot of those
interesting problems but a lot of those problems increasingly are now going to
problems increasingly are now going to involve data in some form after all if
involve data in some form after all if you think of most any website or mobile
you think of most any website or mobile app uh or uh process nowadays that
app uh or uh process nowadays that involves solving problems it almost
involves solving problems it almost always involves some amount of data and
always involves some amount of data and often data at scale lots and lots of
often data at scale lots and lots of data and so what we're going to see
data and so what we're going to see first today is that yes you can use
first today is that yes you can use Python to solve all the problems past
Python to solve all the problems past that we've seen and also some data
that we've seen and also some data specific ones but sometimes it's just
specific ones but sometimes it's just going to be annoying it's going to be a
going to be annoying it's going to be a little painful it's just going to be
little painful it's just going to be more work than you might like to just
more work than you might like to just get to some answer and so today we'll
get to some answer and so today we'll too introduce you to a new language
too introduce you to a new language called SQL structured query language and
called SQL structured query language and this is a language that wur is actually
this is a language that wur is actually much smaller relatively speaking than C
much smaller relatively speaking than C and python it sort of does less but it
and python it sort of does less but it does it really well and it's a language
does it really well and it's a language for querying databases storing data in
for querying databases storing data in it updating it inserting it deleting it
it updating it inserting it deleting it and so much more and it's the kind of
and so much more and it's the kind of Technology that's used nowadays in
Technology that's used nowadays in indeed web apps and mobile apps data
indeed web apps and mobile apps data science analytics and and so much more
science analytics and and so much more it's really good at storing lots and
it's really good at storing lots and lots of data now this is yet another
lots of data now this is yet another language and believe it or not next week
language and believe it or not next week we'll introduce you to three more
we'll introduce you to three more languages HTML and CSS which are not
languages HTML and CSS which are not technically programming languages
technically programming languages they're all about Aesthetics and markup
they're all about Aesthetics and markup of information but also JavaScript which
of information but also JavaScript which is in fact a programming language but
is in fact a programming language but the goals here in cs50 really are going
the goals here in cs50 really are going to be to empower you to program more
to be to empower you to program more generally and indeed when you're out
generally and indeed when you're out there in the real world some years from
there in the real world some years from now invariably there's going to be some
now invariably there's going to be some new other popular language out there and
new other popular language out there and hopefully in this week and next week and
hopefully in this week and next week and Beyond among the goals is not just to
Beyond among the goals is not just to teach you these languages specifically
teach you these languages specifically but again like how to teach yourself the
but again like how to teach yourself the future languages that we've not even
future languages that we've not even heard about just yet so with that said
heard about just yet so with that said let's begin with a survey of sorts if
let's begin with a survey of sorts if you go to this URL on your phone or
you go to this URL on your phone or laptop cs50. l/f favorites a very simple
laptop cs50. l/f favorites a very simple Google form awaits you that's just going
Google form awaits you that's just going to ask you a couple of multiple choice
to ask you a couple of multiple choice questions so go to cs50.
questions so go to cs50. l/f favorites and that should lead you
l/f favorites and that should lead you to a Google form that looks a little
to a Google form that looks a little something like this asking you first as
something like this asking you first as of now in week seven what is your
of now in week seven what is your favorite language among those options
favorite language among those options here and then further down one more
here and then further down one more question if you think back on problem
question if you think back on problem sets 0 through six what was if any your
sets 0 through six what was if any your favorite uh problem set problem be it in
favorite uh problem set problem be it in scratch or C or python so answer those
scratch or C or python so answer those two
two questions and in a moment I'll flip over
questions and in a moment I'll flip over to my screen here where you'll see and
to my screen here where you'll see and anyone who's used Google forms knows the
anyone who's used Google forms knows the spreadsheet that's collecting now this
spreadsheet that's collecting now this data um Microsoft Office 365 can do the
data um Microsoft Office 365 can do the same if you use one of those forms and
same if you use one of those forms and what you see here now is a spreadsheet
what you see here now is a spreadsheet in Google Sheets enumerating all of the
in Google Sheets enumerating all of the audience's questions language is in
audience's questions language is in column B problem is in column C and each
column B problem is in column C and each row represents one student who has
row represents one student who has responded uh a few of you were super
responded uh a few of you were super eager for class today at 8:33 a.m.
eager for class today at 8:33 a.m. eastern time 10:32 11:10 okay so now
eastern time 10:32 11:10 okay so now we're getting into the actual class time
we're getting into the actual class time here and if I scroll down we'll probably
here and if I scroll down we'll probably see few dozen couple hundred Answers by
see few dozen couple hundred Answers by now and yeah so we're getting a whole
now and yeah so we're getting a whole lot of answers here and I'm seeing some
lot of answers here and I'm seeing some patterns emerge but but it's not
patterns emerge but but it's not necessarily obvious to the human eyes
necessarily obvious to the human eyes what those patterns are now of course
what those patterns are now of course you can use Google spreadsheets you can
you can use Google spreadsheets you can like highlight the data and you can
like highlight the data and you can create charts magically out of it but
create charts magically out of it but you can only do what Google lets you do
you can only do what Google lets you do with the data and same thing for
with the data and same thing for Microsoft Excel or apple numbers but
Microsoft Excel or apple numbers but wouldn't it be nice to just be able to
wouldn't it be nice to just be able to manipulate the raw data relatively
manipulate the raw data relatively simple though it is to just answer
simple though it is to just answer questions about the data Maybe long-term
questions about the data Maybe long-term create your own charts customize it just
create your own charts customize it just the way you want rather than be holding
the way you want rather than be holding to like software that's off the shelf
to like software that's off the shelf like this well how could we go about
like this well how could we go about doing this well let me propose that we
doing this well let me propose that we treat this data set now as what we're
treat this data set now as what we're going to call for now a flat file
going to call for now a flat file database we'll see today that there's
database we'll see today that there's fancier databases but the simplest
fancier databases but the simplest database in the world is really just
database in the world is really just like a CSV file and we saw that a couple
like a CSV file and we saw that a couple of weeks ago in C we wrote a bit of C
of weeks ago in C we wrote a bit of C code that used F printf to write data to
code that used F printf to write data to a file using commas as the separator we
a file using commas as the separator we didn't really do much more with csvs at
didn't really do much more with csvs at the time though because it's really
the time though because it's really annoying painful timec consuming not fun
annoying painful timec consuming not fun to use see for something like that
to use see for something like that because of malok and memory and all that
because of malok and memory and all that stuff but with python it's going to be
stuff but with python it's going to be much easier and so anytime you have
much easier and so anytime you have access to some data set where you can
access to some data set where you can just like download it to your own Mac or
just like download it to your own Mac or PC or your Cloud environment it's sort
PC or your Cloud environment it's sort of a candidate for now writing code to
of a candidate for now writing code to do something with the data Maybe analyze
do something with the data Maybe analyze it right away if it's been human inputed
it right away if it's been human inputed manually maybe you have to clean it up
manually maybe you have to clean it up by doing a lot of find and replace but
by doing a lot of find and replace but not with your keyboard but rather with
not with your keyboard but rather with code and so let me go ahead and do this
code and so let me go ahead and do this let me go back to my uh Google sheet
let me go back to my uh Google sheet here that has all of the data that's
here that has all of the data that's come in now and let me go ahead and
come in now and let me go ahead and download this via uh the file menu
download this via uh the file menu here and let's see download and you can
here and let's see download and you can see a whole bunch of options of most
see a whole bunch of options of most formats might be familiar but today
formats might be familiar but today we'll focus just on this one comma
we'll focus just on this one comma separated values or CSV that's going to
separated values or CSV that's going to go ahead and download it on my Mac here
go ahead and download it on my Mac here into my own downloads folder and now I'm
into my own downloads folder and now I'm going to go ahead and do this let me go
going to go ahead and do this let me go ahead and pull up VSS code in the cloud
ahead and pull up VSS code in the cloud here and if you've never done this
here and if you've never done this before there's a couple of ways to do it
before there's a couple of ways to do it but the simplest way to upload a file to
but the simplest way to upload a file to your codes space so to speak is just a
your codes space so to speak is just a sort of drag and drop that's going to
sort of drag and drop that's going to magically upload it to the server there
magically upload it to the server there and we'll see that one it has a very
and we'll see that one it has a very long file name which I'm I'm actually
long file name which I'm I'm actually going to clean this up because that's
going to clean this up because that's going to be very tedious to type in my
going to be very tedious to type in my code so I could either rightclick of of
code so I could either rightclick of of course up here but I'm going to use my
course up here but I'm going to use my Linux command so let's move this file
Linux command so let's move this file called cs50 2022 something or other and
called cs50 2022 something or other and let's just name it more simply
let's just name it more simply favorites. CSV so all lowercase no
favorites. CSV so all lowercase no spaces sort of good Basics and let me go
spaces sort of good Basics and let me go ahead now and open up this file with
ahead now and open up this file with code favorites. CSV I'll close my file
code favorites. CSV I'll close my file explorer and we'll see exactly the same
explorer and we'll see exactly the same data as before but not quite as pretty
data as before but not quite as pretty as Google Sheets makes it be rather we
as Google Sheets makes it be rather we see here that I still have three columns
see here that I still have three columns timestamp language problem and then all
timestamp language problem and then all of the values down below including the
of the values down below including the timestamps and the answers therefore but
timestamps and the answers therefore but it doesn't have proper columns it just
it doesn't have proper columns it just has commas separating them now we could
has commas separating them now we could very easily write python code just like
very easily write python code just like we wrote code to manipulate files like
we wrote code to manipulate files like this either to write or read but in
this either to write or read but in instead let's do something that's a
instead let's do something that's a little more pleasant which is indeed in
little more pleasant which is indeed in the form of python so python actually
the form of python so python actually comes with Native support for csvs it
comes with Native support for csvs it has indeed a package called CSV that
has indeed a package called CSV that just lets you read and write and do a
just lets you read and write and do a whole bunch of useful stuff when it
whole bunch of useful stuff when it comes to CSV files so let's go ahead and
comes to CSV files so let's go ahead and do something with this file let me go
do something with this file let me go back here to VSS code I'm going to close
back here to VSS code I'm going to close favorites. CSV for now but just remember
favorites. CSV for now but just remember in your mind that timestamp was the
in your mind that timestamp was the first column language was the second
first column language was the second column and problem was the third and
column and problem was the third and notice because we're using commas they
notice because we're using commas they don't again line up perfectly but that's
don't again line up perfectly but that's not a problem there are two commas in
not a problem there are two commas in every line presumably and I'm going to
every line presumably and I'm going to go ahead and now create a file called
go ahead and now create a file called how about uh favorites. piy so that I
how about uh favorites. piy so that I can start writing some code to
can start writing some code to manipulate this data and let's do
manipulate this data and let's do something simple let's just write a
something simple let's just write a simple program in Python that opens this
simple program in Python that opens this file reads it and prints something out
file reads it and prints something out just as like a safety check that I know
just as like a safety check that I know what I'm doing even though it's not
what I'm doing even though it's not going to be useful so in Python if you
going to be useful so in Python if you want CSV support you import CSV and that
want CSV support you import CSV and that gives you access to all the magical
gives you access to all the magical capabilities thereof let me now go ahead
capabilities thereof let me now go ahead and use this technique to open a file in
and use this technique to open a file in Python which is similar in C but with
Python which is similar in C but with python I'm going to do this the keyword
python I'm going to do this the keyword with I'm going to open a file called
with I'm going to open a file called favorites. CSV which was the shorter
favorites. CSV which was the shorter name I gave it this is optional but just
name I gave it this is optional but just for explicitness I'm going to open it in
for explicitness I'm going to open it in read mode explicitly just like f open
read mode explicitly just like f open took a second argument as well and I'm
took a second argument as well and I'm going to name this file once open quite
going to name this file once open quite simply file though I could call it
simply file though I could call it anything I want and now it's just an
anything I want and now it's just an open file so far as python knows at this
open file so far as python knows at this moment it's just text or better yet it's
moment it's just text or better yet it's just zeros and ones if you want this
just zeros and ones if you want this python package called CSV to actually do
python package called CSV to actually do something useful with it you have to
something useful with it you have to load this file now into the library and
load this file now into the library and the simplest way to do this is to give
the simplest way to do this is to give myself like a variable called reader
myself like a variable called reader because I want to read this file though
because I want to read this file though this too I could call anything else I'm
this too I could call anything else I'm going to then set that equal to the
going to then set that equal to the return value of a function called CSV do
return value of a function called CSV do reader and I pass to that per the
reader and I pass to that per the documentation the open file so step one
documentation the open file so step one I open the file and this just gives me
I open the file and this just gives me me access to the bytes there in step two
me access to the bytes there in step two now with csvreader tells the python
now with csvreader tells the python package called CSV to do something
package called CSV to do something useful with it and start analyzing the
useful with it and start analyzing the commas and uh allow me to uh parse it
commas and uh allow me to uh parse it further so let's go ahead and do this
further so let's go ahead and do this let me go ahead now and within this Loop
let me go ahead now and within this Loop let's say this with sorry within this
let's say this with sorry within this open file let's do this for every row if
open file let's do this for every row if you will or line in the file AKA reader
you will or line in the file AKA reader let's go ahead and print out now just
let's go ahead and print out now just how about Row Bracket one now what's
how about Row Bracket one now what's going on here well it turns out if you
going on here well it turns out if you read the documentation for the CSV
read the documentation for the CSV reader function what it hands you back
reader function what it hands you back is essentially this special object so to
is essentially this special object so to speak that allows you to treat it as
speak that allows you to treat it as though it's just a really big list of
though it's just a really big list of lines from the file AKA reader so by
lines from the file AKA reader so by saying for Row in reader this is a way
saying for Row in reader this is a way more succinct way of saying uh give me
more succinct way of saying uh give me the first line in the file plus plus
the first line in the file plus plus give me the second line in the file Plus
give me the second line in the file Plus plus and so forth that we would have
plus and so forth that we would have done what much more mechanically in C
done what much more mechanically in C this is just much more pythonic and
this is just much more pythonic and English friendly if you will so in every
English friendly if you will so in every iteration of this Loop row is going to
iteration of this Loop row is going to contain all of the data from the current
contain all of the data from the current row but better yet what the reader
row but better yet what the reader function does for me is it hands me each
function does for me is it hands me each row not just as a big string or Stir of
row not just as a big string or Stir of text in Python it gives me what
text in Python it gives me what apparently based on the syntax on line
apparently based on the syntax on line six any Instinct yeah it's giving me
six any Instinct yeah it's giving me back indeed a list and I'm I presume the
back indeed a list and I'm I presume the visual clue for you was the fact that
visual clue for you was the fact that we're using square brackets here and
we're using square brackets here and indeed Row Bracket one is going to be
indeed Row Bracket one is going to be not the first but the second element in
not the first but the second element in that list and so just take a guess when
that list and so just take a guess when I run this code in a moment What's going
I run this code in a moment What's going to get printed the timestamp the
to get printed the timestamp the language or the
problem the yeah oh the language the language because it's the second column
language because it's the second column that is in the file delimited by those
that is in the file delimited by those those commas so let me go ahead and do
those commas so let me go ahead and do this let me clear my terminal down here
this let me clear my terminal down here here let me run python of favorites.i
here let me run python of favorites.i and enter and there's everything it was
and enter and there's everything it was super fast but there's a really long
super fast but there's a really long list here and in fact if I increase the
list here and in fact if I increase the size of my terminal and start scrolling
size of my terminal and start scrolling up you'll just see all of the raw data
up you'll just see all of the raw data now this isn't that useful yet I could
now this isn't that useful yet I could have just glanced at the CSV but clearly
have just glanced at the CSV but clearly now I have the ability to open the file
now I have the ability to open the file parse it so to speak that is break it up
parse it so to speak that is break it up into its constituent parts and do
into its constituent parts and do something with specific Parts therein
something with specific Parts therein all right so if I want to do this a
all right so if I want to do this a little more pleasantly though let me at
little more pleasantly though let me at least least make this semantically a
least least make this semantically a little cleaner and you know what just
little cleaner and you know what just for clarity let me just give myself a
for clarity let me just give myself a variable it's not strictly necessary but
variable it's not strictly necessary but I know that this is the favorite uh for
I know that this is the favorite uh for instance uh language so let's just call
instance uh language so let's just call it favorite set it equal to Row Bracket
it favorite set it equal to Row Bracket 1 and now just to be more explicit in my
1 and now just to be more explicit in my code even though again we don't need the
code even though again we don't need the variable per se this codee's of course
variable per se this codee's of course going to do the same thing it's just
going to do the same thing it's just using an additional variable called
using an additional variable called favorite if I go down here scroll up run
favorite if I go down here scroll up run the program again I get back the exact
the program again I get back the exact same data but this is a stepping stone
same data but this is a stepping stone to something that's even more powerful
to something that's even more powerful about python support for CSV files is
about python support for CSV files is that you don't have to just treat the
that you don't have to just treat the return value as a list with zero and one
return value as a list with zero and one and two so just thinking intuitively
and two so just thinking intuitively here why is this maybe not the best
here why is this maybe not the best design to hand you the programmer back
design to hand you the programmer back the data in a list that's numerically
the data in a list that's numerically indexed with
indexed with 012 it clearly works but critique this
012 it clearly works but critique this what could go wrong what's a little
what could go wrong what's a little poorly designed yeah you have to always
poorly designed yeah you have to always remember what
remember what are
yeah exactly so if yeah so it's up to you to repeat it's up to you to remember
you to repeat it's up to you to remember like what column the data is actually in
like what column the data is actually in and you know God forbid you're you're
and you know God forbid you're you're collaborating with someone else on the
collaborating with someone else on the spreadsheet you know you've if you've
spreadsheet you know you've if you've used Google spreadsheets you can move
used Google spreadsheets you can move columns around maybe just because you
columns around maybe just because you want to visually reorganize things and
want to visually reorganize things and if you do this and then someone else
if you do this and then someone else downloads that same data all of their
downloads that same data all of their code is going to break so that's just
code is going to break so that's just really bad design it's fragile just
really bad design it's fragile just because it's you're sort of on the honor
because it's you're sort of on the honor System that one means the data that you
System that one means the data that you want so wouldn't it be nice if we could
want so wouldn't it be nice if we could be a little more explicit well recall
be a little more explicit well recall that the very first line in this file is
that the very first line in this file is actually this and I paused the output
actually this and I paused the output this time so that we can see more
this time so that we can see more optionally I just reran favorites St pi
optionally I just reran favorites St pi and notice one of these things is not
and notice one of these things is not like the other every output was either
like the other every output was either scratch or C or python except for this
scratch or C or python except for this first one why am I seeing the word
first one why am I seeing the word language
here where did language come from you didn't have the ability to manual input
didn't have the ability to manual input oh no where did it come from
oh no where did it come from yeah yeah the header the very first row
yeah yeah the header the very first row in the file which by human convention
in the file which by human convention generally just defines what the columns
generally just defines what the columns represent so that there's some human
represent so that there's some human useful information there now that's not
useful information there now that's not really intended to be part of my output
really intended to be part of my output at the moment so there is a way to skip
at the moment so there is a way to skip this if you want to skip the first row
this if you want to skip the first row you can actually do something like this
you can actually do something like this you can say next row and that will just
you can say next row and that will just ignore that row so that I'm starting
ignore that row so that I'm starting really with the every row thereafter but
really with the every row thereafter but there's a better way to handle this than
there's a better way to handle this than that that will get rid of the row in the
that that will get rid of the row in the output but let me go ahead and use a
output but let me go ahead and use a different feature of the CSV package
different feature of the CSV package that's just going to make this a little
that's just going to make this a little cleaner Al together so let me clear my
cleaner Al together so let me clear my terminal window here let me undo this
terminal window here let me undo this next thing that I just added and instead
next thing that I just added and instead of using a reader let me go ahead and
of using a reader let me go ahead and use a dictionary reader abbreviated dict
use a dictionary reader abbreviated dict reader that's going to now return me the
reader that's going to now return me the equivalent of all of the rows one at a
equivalent of all of the rows one at a time so I can still call it Reader just
time so I can still call it Reader just as before but as the name implies what
as before but as the name implies what this reader is going to return is not a
this reader is going to return is not a list after list after list but a
list after list after list but a dictionary a dictionary a dictionary and
dictionary a dictionary a dictionary and remember a dictionary is just a
remember a dictionary is just a collection of key value pairs so what
collection of key value pairs so what does that mean what are the keys what
does that mean what are the keys what are the values well now that I'm using a
are the values well now that I'm using a dictionary reader I can actually do this
dictionary reader I can actually do this instead of sort of on the honor System
instead of sort of on the honor System remembering that I want column one I can
remembering that I want column one I can treat row now not as a list but as a
treat row now not as a list but as a dictionary and that means I can go in
dictionary and that means I can go in here and say quote unquote language and
here and say quote unquote language and we saw that back in week six python
we saw that back in week six python allows you to index into dictionaries
allows you to index into dictionaries using square bracket notation in strings
using square bracket notation in strings or stirs on the inside just like lists
or stirs on the inside just like lists allow for numbers but this now I think
allow for numbers but this now I think is going to be a little more robust if I
is going to be a little more robust if I run this again python of favorites. py
run this again python of favorites. py all of that worked out fine and let me
all of that worked out fine and let me pause the output to by using this
pause the output to by using this program called more now I don't even see
program called more now I don't even see the header so now whoever works it uh
the header so now whoever works it uh with python wrote the code for this
with python wrote the code for this package to just analyze that first line
package to just analyze that first line of code use the header as you just
of code use the header as you just called it as the keys and then every
called it as the keys and then every time you iterate through this Loop it
time you iterate through this Loop it updates the values the values the values
updates the values the values the values but the keys stay the same any questions
but the keys stay the same any questions then on this
then on this technique spiced to say this would be
technique spiced to say this would be painful in C
Associated exactly so the keys are always going to be quote unquote
always going to be quote unquote timestamp uh Pro language and problem
timestamp uh Pro language and problem but on each iteration of this Loop here
but on each iteration of this Loop here uh the row is going to contain a
uh the row is going to contain a different row of values different row
different row of values different row values different row values so you're
values different row values so you're going to get back one dictionary for
going to get back one dictionary for every student who submitted the Google
every student who submitted the Google form if you will while iterating through
form if you will while iterating through it there all right so once we have this
it there all right so once we have this ability here why don't we go ahead and
ability here why don't we go ahead and transition
transition into how about not just using that
into how about not just using that dictionary reader which makes the code a
dictionary reader which makes the code a little more robust because now if you
little more robust because now if you move the columns around no big deal it
move the columns around no big deal it doesn't matter if the numeric indices
doesn't matter if the numeric indices change you can still use those keywords
change you can still use those keywords instead well let's actually analyze the
instead well let's actually analyze the data now I'm just spitting it out which
data now I'm just spitting it out which is not solving any problems for anyone
is not solving any problems for anyone so let's go ahead and and count the
so let's go ahead and and count the popularity of scratch C and Python and
popularity of scratch C and Python and see what everyone's uh been thinking
see what everyone's uh been thinking here all right so how might I do this
here all right so how might I do this well let me go ahead and do this up here
well let me go ahead and do this up here before I start iterating let me give
before I start iterating let me give myself let's say three variables and to
myself let's say three variables and to keep things simple I'll say one variable
keep things simple I'll say one variable called scratch set it equal to zero for
called scratch set it equal to zero for zero students so far C is going to equal
zero students so far C is going to equal zero and python is going to equal zero
zero and python is going to equal zero there's a slightly prettier way of doing
there's a slightly prettier way of doing this just because this is like three
this just because this is like three lines of code to do something very
lines of code to do something very simple you could alternatively in Python
simple you could alternatively in Python but not C do scratch comma C comma
but not C do scratch comma C comma python equals 0 comma 0 so kind of
python equals 0 comma 0 so kind of slightly more elegant just to fit it all
slightly more elegant just to fit it all into one line but now let's just do
into one line but now let's just do something more interesting on line seven
something more interesting on line seven I'm still going to figure out what the
I'm still going to figure out what the current favorite language is and now I'm
current favorite language is and now I'm just going to do some conditional checks
just going to do some conditional checks How about if that favorite equals equals
How about if that favorite equals equals quote unquote scratch let's go ahead and
quote unquote scratch let's go ahead and increment Scratch by one we can't do
increment Scratch by one we can't do plus plus in Python but we can do plus
plus plus in Python but we can do plus equals 1 how about L if favorite equals
equals 1 how about L if favorite equals equals c then let's do C+ equal 1 L and
equals c then let's do C+ equal 1 L and not we could do else this is actually a
not we could do else this is actually a good design question should I do else
good design question should I do else should I do L if any instincts here
yeah yeah really good instincts just in case someone goes and adds another
case someone goes and adds another language to the form next week because
language to the form next week because we're obviously going to introduce
we're obviously going to introduce another language today you don't want
another language today you don't want your code to now artificially inflate
your code to now artificially inflate the scores for python just because
the scores for python just because you're conflating multiple languages
you're conflating multiple languages together so the more defensive sort of
together so the more defensive sort of better way to write this code I agree
better way to write this code I agree would be L if favorite equals equals
would be L if favorite equals equals python then let's go ahead and increment
python then let's go ahead and increment python plus equals 1 and if there's a
python plus equals 1 and if there's a new language next week we're obviously
new language next week we're obviously going to have to update the code but at
going to have to update the code but at least we're not miscounting we're just
least we're not miscounting we're just missing the new language so I think
missing the new language so I think that's slightly more robust all right
that's slightly more robust all right now at the very bottom of this program
now at the very bottom of this program and outside of the loop when I'm all
and outside of the loop when I'm all done counting let me go ahead and print
done counting let me go ahead and print out using some f strings how about the
out using some f strings how about the total number of people who uh whose
total number of people who uh whose favorite is scratch so this is just uh
favorite is scratch so this is just uh week six F string syntax let me go ahead
week six F string syntax let me go ahead and print out another F string for C and
and print out another F string for C and I'm of course putting the variables in
I'm of course putting the variables in curly braces all lowercase but the
curly braces all lowercase but the English words I'm doing capitalization
English words I'm doing capitalization for Let's do an final one with f uh
for Let's do an final one with f uh python colon and then in curly braces
python colon and then in curly braces python close quote and I think I'm done
python close quote and I think I'm done so let me just hide my terminal for a
so let me just hide my terminal for a second here's the total program same
second here's the total program same stuff as before open favorites. CSV open
stuff as before open favorites. CSV open it further with the dictionary reader to
it further with the dictionary reader to do that processing for us initialize
do that processing for us initialize three variables to zero just so we have
three variables to zero just so we have something to count with and then iterate
something to count with and then iterate over the file row by row and this is
over the file row by row and this is just some sort of week one style
just some sort of week one style conditional logic albe it in Python
conditional logic albe it in Python counting things all right so how can we
counting things all right so how can we now execute this let me go back to my
now execute this let me go back to my terminal python of favorites.i and here
terminal python of favorites.i and here we go uh as of today everyone who's
we go uh as of today everyone who's reporting in live via the Google form
reporting in live via the Google form their favorite languages are
their favorite languages are interesting that's pretty interesting
interesting that's pretty interesting too after just one week of python no
too after just one week of python no less so but scratch is a healthy
less so but scratch is a healthy Contender there a lot of C so a pretty
Contender there a lot of C so a pretty good mix here so is this going to be the
good mix here so is this going to be the best way to write this program long term
best way to write this program long term well as you noted if there's a new
well as you noted if there's a new language next week this week we're going
language next week this week we're going to have to constantly update this and
to have to constantly update this and here's where you should let your mind
here's where you should let your mind wander to like the future like if we
wander to like the future like if we have a fourth language fifth language
have a fourth language fifth language 6th sth eth which aspect here might kind
6th sth eth which aspect here might kind of have some code smell to it like this
of have some code smell to it like this probably isn't the best design to set us
probably isn't the best design to set us up for the
up for the future what might be better than this
future what might be better than this yeah add language line yeah we have to
yeah add language line yeah we have to keep adding a language to line five and
keep adding a language to line five and okay not a big deal we could add like
okay not a big deal we could add like SQL today and maybe JavaScript next week
SQL today and maybe JavaScript next week but you know anytime a a line of code a
but you know anytime a a line of code a line of logic is just going to kind of
line of logic is just going to kind of grow out of control and we've had this
grow out of control and we've had this chat a couple of times with different
chat a couple of times with different syntax there's probably a better way
syntax there's probably a better way than that so let's do that instead of
than that so let's do that instead of using these individual variables we
using these individual variables we could make maybe use a list but a list
could make maybe use a list but a list would be a little confusing because like
would be a little confusing because like what does braet zero mean what is
what does braet zero mean what is bracket 1 bracket two but a dictionary
bracket 1 bracket two but a dictionary recall is like this Swiss army knife of
recall is like this Swiss army knife of data structures whereby you can
data structures whereby you can associate anything with anything else
associate anything with anything else keys with values so I dare say a cleaner
keys with values so I dare say a cleaner way to solve this problem that sets us
way to solve this problem that sets us up for Less work or confusion later
up for Less work or confusion later would be to create like a new variable
would be to create like a new variable called counts if that's what we're doing
called counts if that's what we're doing counting things up and just set it equal
counting things up and just set it equal to an empty dictionary and you can
to an empty dictionary and you can literally say adct with the open
literally say adct with the open parenthesis closed parenthesis nothing
parenthesis closed parenthesis nothing or the more pythonic just use open and
or the more pythonic just use open and Clos curly braces with nothing inside
Clos curly braces with nothing inside that gives me an empty dictionary just
that gives me an empty dictionary just like square brackets gives me a list now
like square brackets gives me a list now my logic down here has to change a
my logic down here has to change a little bit but what's nice is I don't
little bit but what's nice is I don't need one conditional for every language
need one conditional for every language because again if we have a fourth a
because again if we have a fourth a fifth a sixth that chunk of code is also
fifth a sixth that chunk of code is also going to grow a bit out of control too
going to grow a bit out of control too so I can get rid of this here and what I
so I can get rid of this here and what I think I'm going to do is say this
think I'm going to do is say this whatever the current favorite is from
whatever the current favorite is from the current Row in the file why don't we
the current Row in the file why don't we go into our counts variable at that key
go into our counts variable at that key and again favorite is a variable it's
and again favorite is a variable it's not quote unquote favorite it's going to
not quote unquote favorite it's going to be scratch or C or Python and then why
be scratch or C or Python and then why don't we go ahead and just increment
don't we go ahead and just increment whatever the value of that count is at
whatever the value of that count is at that key now this is technically bugy
that key now this is technically bugy we're really close but there is a
we're really close but there is a bug does anyone want to conjecture what
bug does anyone want to conjecture what the bug is
yeah a good question that answers my question in uh nonetheless so no like
question in uh nonetheless so no like the magic you describe will not happen
the magic you describe will not happen and to repeat the the hypothesis will
and to repeat the the hypothesis will this automatically create a key for
this automatically create a key for every uh language that we try plugging
every uh language that we try plugging into those square brackets short answer
into those square brackets short answer no odds are this is going to create a
no odds are this is going to create a key error one of those traceback error
key error one of those traceback error messages that you've probably seen by
messages that you've probably seen by now either in class or in problem sets
now either in class or in problem sets whereby if scratch hasn't appeared in
whereby if scratch hasn't appeared in the dictionary before or C or python
the dictionary before or C or python like then the dictionary has no clue
like then the dictionary has no clue what you're talking about so I think we
what you're talking about so I think we actually still need some conditional
actually still need some conditional logic but not that's going to grow
logic but not that's going to grow longer and longer with each language
longer and longer with each language what I think we probably want to do is
what I think we probably want to do is this if the current favorite is in the
this if the current favorite is in the counts dictionary and this is the
counts dictionary and this is the pythonic way of just saying is this key
pythonic way of just saying is this key in this dictionary then go ahead and
in this dictionary then go ahead and safely do count favorite plus equals 1
safely do count favorite plus equals 1 else to your conjecture now else what do
else to your conjecture now else what do I want to do counts
I want to do counts favorite
favorite equals yeah one so initialize a brand
equals yeah one so initialize a brand new key to a brand new value of one
new key to a brand new value of one because I'm obviously just seen this
because I'm obviously just seen this language otherwise increment again and
language otherwise increment again and again and now down here I just need to
again and now down here I just need to tweak my syntax a little bit I don't
tweak my syntax a little bit I don't need to print out all of these things
need to print out all of these things one at a time manually I can actually
one at a time manually I can actually get away I think with another loop at
get away I think with another loop at the very bottom here so how about I do
the very bottom here so how about I do this for each favorite in those counts
this for each favorite in those counts and this is again the pythonic way to
and this is again the pythonic way to iterate over all of the keys in a
iterate over all of the keys in a dictionary go ahead and print out using
dictionary go ahead and print out using an F string whatever the current
an F string whatever the current favorite is scratch or C or Python and
favorite is scratch or C or Python and then a colon and then figure out what
then a colon and then figure out what its count is and you can do that by
its count is and you can do that by going into the counts dictionary looking
going into the counts dictionary looking at the favorite key and get back its
at the favorite key and get back its value so close my curly braces I close
value so close my curly braces I close my quotes and even though this looks
my quotes and even though this looks ugly at the moment now this is much more
ugly at the moment now this is much more dynamic because if we go and add SQL to
dynamic because if we go and add SQL to the CSV file tomorrow where we add
the CSV file tomorrow where we add JavaScript next week this will just work
JavaScript next week this will just work it will keep working now automatically
it will keep working now automatically all I change is the Google form not my
all I change is the Google form not my actual code all right let's try python
actual code all right let's try python of favorites.i cross my fingers as
of favorites.i cross my fingers as always and there now is the data as of
always and there now is the data as of now questions on this code here
yeah really good good question what if you wanted to print it in a particular
you wanted to print it in a particular order uh well I could give you a couple
order uh well I could give you a couple of solutions like if you want to print
of solutions like if you want to print it out in it's already coincidentally in
it out in it's already coincidentally in alphabetical order so you got that for
alphabetical order so you got that for free although that's just by chance here
free although that's just by chance here but there is a way to do this and let me
but there is a way to do this and let me propose that we go down here to my Loop
propose that we go down here to my Loop and I explicitly use a function you
and I explicitly use a function you might not have seen in Python yet but
might not have seen in Python yet but it's literally called sorted which is
it's literally called sorted which is going to take either a list or in this
going to take either a list or in this case a dictionary and by default sort it
case a dictionary and by default sort it by key alphabetically now if my
by key alphabetically now if my intuition is correct this is not going
intuition is correct this is not going to change the output because it's
to change the output because it's already alphabetical but if you read the
already alphabetical but if you read the documentation for the sorted function it
documentation for the sorted function it takes multiple parameters potentially
takes multiple parameters potentially some of which are named parameters and
some of which are named parameters and so you can actually do this if you want
so you can actually do this if you want to sort the counts but you want to
to sort the counts but you want to reverse the order for whatever reason
reverse the order for whatever reason here so that it's reverse alphabetical
here so that it's reverse alphabetical order now let me go ahead and rerun this
order now let me go ahead and rerun this and I'll keep the previous output on the
and I'll keep the previous output on the screen enter and now it's backwards uh
screen enter and now it's backwards uh alphabetically if you will other
alphabetically if you will other questions on this here
questions on this here here no how about then how about then we
here no how about then how about then we transition
transition to changing sorting by value and let me
to changing sorting by value and let me this is going to escalate a little
this is going to escalate a little quickly briefly but then we'll we'll
quickly briefly but then we'll we'll tone it down again notice that right now
tone it down again notice that right now this is indeed sorting by key what if
this is indeed sorting by key what if especially if I have lots of data it'd
especially if I have lots of data it'd be nice to make like a top 10 list or in
be nice to make like a top 10 list or in this case a top three list and actually
this case a top three list and actually see in order of the counts the values uh
see in order of the counts the values uh what these popular ones are so it's not
what these popular ones are so it's not C python scratch it should ideally be
C python scratch it should ideally be python then C then scratch because of
python then C then scratch because of the values and the magnitude thereof so
the values and the magnitude thereof so how can I do this well it turns out
how can I do this well it turns out there's another key another uh parameter
there's another key another uh parameter that you can pass to the sorted function
that you can pass to the sorted function that is typically implemented as a
that is typically implemented as a function itself and so I'm going to go
function itself and so I'm going to go ahead and do this I'm going to
ahead and do this I'm going to temporarily Define a function called get
temporarily Define a function called get value just to make my life easier and
value just to make my life easier and this get value function is going to take
this get value function is going to take I'll say a language par parameter and
I'll say a language par parameter and then all I'm going to do is return
then all I'm going to do is return whatever the count is of that language
whatever the count is of that language so out of context this is just a super
so out of context this is just a super simple function that you hand it a
simple function that you hand it a language like scratch or C or python
language like scratch or C or python it's just going to tell you what the
it's just going to tell you what the count is thereof in that uh in that
count is thereof in that uh in that dictionary called counts but what I can
dictionary called counts but what I can do now down here in my newly introduced
do now down here in my newly introduced call to sorted is I can tell it what to
call to sorted is I can tell it what to use as its key instead of using
use as its key instead of using literally the key scratch C python I can
literally the key scratch C python I can sort of override that behavior and say
sort of override that behavior and say you know what to figure out what to sort
you know what to figure out what to sort by go ahead and call this function
by go ahead and call this function called get value notice that I have not
called get value notice that I have not put parentheses after get value because
put parentheses after get value because I don't want to call get value right
I don't want to call get value right then and there I want to pass the get
then and there I want to pass the get value function as itself in argument to
value function as itself in argument to the sorted function so that the sorted
the sorted function so that the sorted function written years ago by the people
function written years ago by the people at python can call my version of get
at python can call my version of get value again and again and again when
value again and again and again when they try to sort sort this actual data
they try to sort sort this actual data so now if I add that and I leave reverse
so now if I add that and I leave reverse equals true let's see what happens
equals true let's see what happens python of favorites. py enter and now I
python of favorites. py enter and now I get my top 10 or in this case top three
get my top 10 or in this case top three list and if I had more sophisticated
list and if I had more sophisticated data with like more columns Al together
data with like more columns Al together that I actually care about I could even
that I actually care about I could even sort this more uh powerfully as well but
sort this more uh powerfully as well but let me clean this up a little bit just
let me clean this up a little bit just so you've seen it even though we won't
so you've seen it even though we won't use these that often in cs50 until the
use these that often in cs50 until the end of the class will they come up again
end of the class will they come up again technically this is a little bit H this
technically this is a little bit H this isn't necessarily the best design to
isn't necessarily the best design to spend all this time implementing a
spend all this time implementing a function and then only use it in one
function and then only use it in one place in general we've argued that H you
place in general we've argued that H you don't necessarily need a variable if
don't necessarily need a variable if you're only going to use it in one place
you're only going to use it in one place you don't really need a function if
you don't really need a function if you're only going to use it in one place
you're only going to use it in one place and here we kind of have a good
and here we kind of have a good candidate for that and so it turns out
candidate for that and so it turns out in Python if you don't want to bother
in Python if you don't want to bother creating a function just to use it once
creating a function just to use it once you can create what's called an
you can create what's called an anonymous function AKA a Lambda function
anonymous function AKA a Lambda function like the lamb Lambda symbol familiar and
like the lamb Lambda symbol familiar and a Lambda function the syntax is a little
a Lambda function the syntax is a little strange looking but you say this you
strange looking but you say this you literally say Lambda you literally then
literally say Lambda you literally then say the name of the argument that you
say the name of the argument that you want this Anonymous function with no
want this Anonymous function with no name to take then you have a colon and
name to take then you have a colon and then quite simply you write what you
then quite simply you write what you want the return value of this function
want the return value of this function to be you don't even say return
to be you don't even say return literally these Lambda functions are
literally these Lambda functions are meant to be used super turly so that you
meant to be used super turly so that you can in one line Express something like
can in one line Express something like this and I admit this looks more cryptic
this and I admit this looks more cryptic I think than the previous version but as
I think than the previous version but as you get more comfortable with python or
you get more comfortable with python or other languages that support this
other languages that support this feature it allows you to not bother with
feature it allows you to not bother with lines of code like that and just tighten
lines of code like that and just tighten up your code a little bit so this line
up your code a little bit so this line here lamba language colon counts
here lamba language colon counts language is the oneline version of this
language is the oneline version of this and you don't even need to bother
and you don't even need to bother picking a name for it Lambda tells
picking a name for it Lambda tells python I didn't waste any time thinking
python I didn't waste any time thinking of a name for this
of a name for this function so questions then on this
function so questions then on this technique technique of using python to
technique technique of using python to analyze data like
analyze data like this any
this any questions we're almost done with python
questions we're almost done with python questions no okay so why don't we make
questions no okay so why don't we make things a little more interesting because
things a little more interesting because we had a much juicier data set with the
we had a much juicier data set with the problems that we've assigned over the
problems that we've assigned over the past several weeks why don't we go ahead
past several weeks why don't we go ahead and quite simply you know I think we
and quite simply you know I think we wrote pretty darn good code here so I
wrote pretty darn good code here so I think we can pretty much just change a
think we can pretty much just change a bit of it to say let's see if I don't
bit of it to say let's see if I don't want language I want problem and if I
want language I want problem and if I want to sort by not language but problem
want to sort by not language but problem I think that's it I think if I didn't
I think that's it I think if I didn't Overlook something here just by changing
Overlook something here just by changing what column I'm reading the data from
what column I'm reading the data from and then just to be consistent renaming
and then just to be consistent renaming my variables just so I know what I'm
my variables just so I know what I'm looking at what will this program now do
looking at what will this program now do after those minor
after those minor changes what will I see when I run
changes what will I see when I run this
this what would be the first thing I see when
what would be the first thing I see when I run
this tough crowd today yes problem yeah the problem this the top problem so the
the problem this the top problem so the most popular problem which I'm a little
most popular problem which I'm a little worried it might be hello or just
worried it might be hello or just scratch but let's go ahead and see so
scratch but let's go ahead and see so let me go ahead and open my terminal
let me go ahead and open my terminal window I'll even maximize my terminal
window I'll even maximize my terminal window so we can see a lot let me go
window so we can see a lot let me go ahead and run python of favorites. py
ahead and run python of favorites. py I'm going to go ahead now and cross my
I'm going to go ahead now and cross my fingers that I didn't mess up and hit
fingers that I didn't mess up and hit enter and okay great we peaked early so
enter and okay great we peaked early so scratch was the most popular program
scratch was the most popular program according to the data at the time I
according to the data at the time I downloaded it I'm sure other votes have
downloaded it I'm sure other votes have come in since filter uh in week four was
come in since filter uh in week four was tied then with tan as well Mario is a
tied then with tan as well Mario is a close third there and so forth so this
close third there and so forth so this is helpful for us on staff that uh not
is helpful for us on staff that uh not so much love down here at the bottom of
so much love down here at the bottom of the list so it was a bunch of code to
the list so it was a bunch of code to write but now that we've written it in
write but now that we've written it in this very versatile Dynamic way it's
this very versatile Dynamic way it's pretty good for just like crunching data
pretty good for just like crunching data and doing some analytics but it's still
and doing some analytics but it's still a decent number of lines to have had to
a decent number of lines to have had to write manually and this is where
write manually and this is where sometimes it isn't necessarily the right
sometimes it isn't necessarily the right tool for the job but rather the job that
tool for the job but rather the job that uh but rather a candidate for using some
uh but rather a candidate for using some other language alog together especially
other language alog together especially when it's not just a one-time program
when it's not just a one-time program that you run and you want to see the
that you run and you want to see the answer what if you want to take input
answer what if you want to take input from the user and ask uh answer
from the user and ask uh answer questions dynamically like a mobile app
questions dynamically like a mobile app would like a website would like
would like a website would like Microsoft Excel or apple numbers or
Microsoft Excel or apple numbers or Google Sheets would for for you well
Google Sheets would for for you well let's make one final change for now to
let's make one final change for now to this version of the program and actually
this version of the program and actually take in some user input so besides just
take in some user input so besides just loading all of the data into memory
loading all of the data into memory let's go ahead and down below here not
let's go ahead and down below here not just print out the top 10 list if you
just print out the top 10 list if you will but prompt the user for their
will but prompt the user for their favorite so I'm going to use Python's
favorite so I'm going to use Python's input function and I'm just going to
input function and I'm just going to prompt them with favorite quote unquote
prompt them with favorite quote unquote like tell me what your favorite problem
like tell me what your favorite problem what problem uh rather uh you interested
what problem uh rather uh you interested in and now let me go ahead and say if
in and now let me go ahead and say if that favorite is in the counts variable
that favorite is in the counts variable so you didn't type in something random
so you didn't type in something random that we didn't actually assign as a
that we didn't actually assign as a problem then let me go ahead and print
problem then let me go ahead and print with a format string whatever that
with a format string whatever that favorite is of yours and show you the
favorite is of yours and show you the actual popularity thereof by indexing
actual popularity thereof by indexing into counts using that favorite as the
into counts using that favorite as the key and printing this so now it's a
key and printing this so now it's a dynamic program it doesn't dump all of
dynamic program it doesn't dump all of the data and all of the summations
the data and all of the summations rather it's going to allow me to see
rather it's going to allow me to see what my choice of favorite is and I'm
what my choice of favorite is and I'm going to go ahead and say uh let's see
going to go ahead and say uh let's see I'm a fan of Mario here so enter and
I'm a fan of Mario here so enter and indeed we see the same value we saw a
indeed we see the same value we saw a moment ago but just for Mario but the
moment ago but just for Mario but the point now is that one all of this is
point now is that one all of this is possible two it's way easier and more
possible two it's way easier and more pleasant than this would have been in C
pleasant than this would have been in C this is still only 15 lines of code and
this is still only 15 lines of code and in C again there's the memory management
in C again there's the memory management there's the iterating over the strings
there's the iterating over the strings trying to find the commas there's just a
trying to find the commas there's just a lot more work but honestly even when you
lot more work but honestly even when you just want to answer a question like this
just want to answer a question like this in Excel in Apple Numbers Google sheets
in Excel in Apple Numbers Google sheets you know generally you can just
you know generally you can just highlight things you can click a button
highlight things you can click a button and boom you get your answer for
and boom you get your answer for summation or Max or Min or any of those
summation or Max or Min or any of those sort of Basics wouldn't it be nice if we
sort of Basics wouldn't it be nice if we weren't taking a step backwards as
weren't taking a step backwards as programmers and being sort of more
programmers and being sort of more powerful and yet we now have to do more
powerful and yet we now have to do more of the work so maybe sometimes Python's
of the work so maybe sometimes Python's not or any language is not the best tool
not or any language is not the best tool for the job and that's going to now
for the job and that's going to now allow us to introduce more generally
allow us to introduce more generally something called a relational database
something called a relational database graduating from Mere flat file databases
graduating from Mere flat file databases like text files or binary files in which
like text files or binary files in which all of your data is stored to something
all of your data is stored to something more proper but first
questions really good question to reiterate if I were to is this case
reiterate if I were to is this case sensitive so if I were to type in Mario
sensitive so if I were to type in Mario in all lowercase and hit enter I
in all lowercase and hit enter I actually get no such response now that
actually get no such response now that might be acceptable because the problem
might be acceptable because the problem technically what is a capital M but
technically what is a capital M but that's a little uh ridiculous to be that
that's a little uh ridiculous to be that pedantic about the input so how could we
pedantic about the input so how could we solve
solve this any tips for how we can make this a
this any tips for how we can make this a little more robust
yeah yeah so we could use a few different functions one of which is
different functions one of which is called title which will change it to
called title which will change it to title case where it capitalizes like in
title case where it capitalizes like in most English sentences the first letter
most English sentences the first letter of that sentence we could use capitalize
of that sentence we could use capitalize we could use upper we could use lower
we could use upper we could use lower but indeed we could just decide how we
but indeed we could just decide how we want to standardize the capitalization
want to standardize the capitalization either uppercase lowercase or some
either uppercase lowercase or some combination thereof and just make sure
combination thereof and just make sure that you change the counts themselves
that you change the counts themselves make sure that you do the same to
make sure that you do the same to favorite and make sure that maybe you
favorite and make sure that maybe you keep a backup of the data if you want to
keep a backup of the data if you want to show the original version that came from
show the original version that came from the CSV without presuming to just
the CSV without presuming to just capitalize everything for the user but
capitalize everything for the user but indeed that would be the most common
indeed that would be the most common scenario you just make things case
scenario you just make things case insensitive when doing those matches
insensitive when doing those matches other questions now on python before we
other questions now on python before we leave it behind for the coming
leave it behind for the coming week all right well then let's introduce
week all right well then let's introduce these relational databases so relational
these relational databases so relational database is what like every is a super
database is what like every is a super popular way of storing lots of data like
popular way of storing lots of data like this is what the Twitter's of the world
this is what the Twitter's of the world the Googles of the world the metas of
the Googles of the world the metas of the world use to store some of their
the world use to store some of their data at scale there are alternatives to
data at scale there are alternatives to relational databases um indeed today
relational databases um indeed today we'll talk about a language called SQL
we'll talk about a language called SQL there's so a movement if you will or an
there's so a movement if you will or an alternative generally called No SQL
alternative generally called No SQL which is just the opposite you don't use
which is just the opposite you don't use SQL there are things called
SQL there are things called objectoriented databases and the like
objectoriented databases and the like but if you've ever heard of MySQL or
but if you've ever heard of MySQL or postgress SQL or uh Microsoft SQL server
postgress SQL or uh Microsoft SQL server or Oracle or Maria DB or a bunch of
or Oracle or Maria DB or a bunch of other products both free and Commercial
other products both free and Commercial this is what they're talking about
this is what they're talking about databases that are designed to store
databases that are designed to store lots of data and what's nice about
lots of data and what's nice about relational databases is that they're
relational databases is that they're really similar to the spreadsheets with
really similar to the spreadsheets with which you were presumably familiar long
which you were presumably familiar long before today's class so a relational
before today's class so a relational database is going to store as you'll see
database is going to store as you'll see all of the data in rows and columns now
all of the data in rows and columns now the terminology will thereafter be a
the terminology will thereafter be a little different instead of having
little different instead of having sheets you're going to have tables but
sheets you're going to have tables but those tables are still going to have
those tables are still going to have rows and columns and you're going to
rows and columns and you're going to have even more control over the
have even more control over the performance of your data when you start
performance of your data when you start to access it using this structured query
to access it using this structured query language or SQL this is a language you
language or SQL this is a language you can use for web apps mobile apps uh a
can use for web apps mobile apps uh a lot of analysts would sit down and their
lot of analysts would sit down and their Mac or PC and actually ask questions of
Mac or PC and actually ask questions of data to get back the answer and
data to get back the answer and wonderfully even though there will be
wonderfully even though there will be some new syntax today SQL really just
some new syntax today SQL really just does four basic things crud is the sort
does four basic things crud is the sort of crude acronym here crud is a way of
of crude acronym here crud is a way of remembering that a relational database
remembering that a relational database supports ultimately creating data
supports ultimately creating data reading data updating data and deleting
reading data updating data and deleting data so even if you're feeling like wow
data so even if you're feeling like wow this is a lot of new syntax which it
this is a lot of new syntax which it isn't relative to our past languages the
isn't relative to our past languages the only things you're doing really are
only things you're doing really are creating dat data reading data updating
creating dat data reading data updating and deleting the same now a little
and deleting the same now a little confusingly in SQL the corresponding
confusingly in SQL the corresponding functions or commands that exist that
functions or commands that exist that map to crud are actually this so it's
map to crud are actually this so it's still create but there's another one
still create but there's another one called insert uh it's not read which is
called insert uh it's not read which is more of the computer scientist way of
more of the computer scientist way of saying it but select which is a little
saying it but select which is a little more explicit like select data you care
more explicit like select data you care about update is still update delete is
about update is still update delete is still delete but there's another command
still delete but there's another command called drop which lets you drop that is
called drop which lets you drop that is delete entire tables as well so you can
delete entire tables as well so you can create tables using syntax that's
create tables using syntax that's generally going to look like this you'll
generally going to look like this you'll say create table you'll give the name of
say create table you'll give the name of the table uh which you can call almost
the table uh which you can call almost anything you want but generally all
anything you want but generally all lowercase no spaces is best then in
lowercase no spaces is best then in parentheses you can specify a comma
parentheses you can specify a comma separated list of the columns that you
separated list of the columns that you might want in this table so this is the
might want in this table so this is the code equivalent in the SQL language of
code equivalent in the SQL language of like manually opening Google Sheets or
like manually opening Google Sheets or Excel or numbers and like clicking in
Excel or numbers and like clicking in the top left cell and like typing
the top left cell and like typing timestamp and then in the next typing
timestamp and then in the next typing language and then in the third typing
language and then in the third typing problem this is the way to sort of
problem this is the way to sort of Define what your headers are if you will
Define what your headers are if you will in a spreadsheet but now it's called a
in a spreadsheet but now it's called a table now we won't use this command
table now we won't use this command manually first let's do something a
manually first let's do something a little simpler we're going to start off
little simpler we're going to start off by just importing this data ourselves
by just importing this data ourselves and I'm going to go ahead and do this
and I'm going to go ahead and do this let me go back to vs code here I'm going
let me go back to vs code here I'm going to leave behind favorites. py for now
to leave behind favorites. py for now because now we're going to transition to
because now we're going to transition to this other language called SQL and to do
this other language called SQL and to do this I am going to create a new database
this I am going to create a new database file and I'm going to do so using a
file and I'm going to do so using a command called SQL light 3 which is just
command called SQL light 3 which is just the third version thereof and I'm going
the third version thereof and I'm going to give the database a name of
to give the database a name of favorites. DB there's different
favorites. DB there's different conventions but this is one of the most
conventions but this is one of the most common when I hit enter this is going to
common when I hit enter this is going to create for me a new empty database just
create for me a new empty database just like opening an Untitled spreadsheet in
like opening an Untitled spreadsheet in Excel Google Sheets or Apple Numbers I'm
Excel Google Sheets or Apple Numbers I'm being prompted do I want to create
being prompted do I want to create favorites. DB I'll hit y for yes okay
favorites. DB I'll hit y for yes okay we're up and running now you're going to
we're up and running now you're going to notice a different prompt I'm not in my
notice a different prompt I'm not in my Linux prompt per se which is always the
Linux prompt per se which is always the dollar sign I'm now inside of the
dollar sign I'm now inside of the program called SQL light and we're going
program called SQL light and we're going to use SQL light SQL light 3 as just an
to use SQL light SQL light 3 as just an interactive way for now of playing with
interactive way for now of playing with SQL code at the end of today we'll show
SQL code at the end of today we'll show you how you can use SQL in Python code
you how you can use SQL in Python code so that you still write python code to
so that you still write python code to do whatever you want but you can talk to
do whatever you want but you can talk to databases using Python and this is
databases using Python and this is exactly how web apps mobile apps work
exactly how web apps mobile apps work for instance on iOS uh and an i phone an
for instance on iOS uh and an i phone an iPad or the like if you want to store
iPad or the like if you want to store data it's very often stored in a SQL
data it's very often stored in a SQL database as we're about to do um but you
database as we're about to do um but you might use a language called Swift or
might use a language called Swift or objective c and same exists in the world
objective c and same exists in the world of Android using Java or cotlin or
of Android using Java or cotlin or something else to query the database so
something else to query the database so we're going to see SQL in isolation for
we're going to see SQL in isolation for now like an analyst might just use at
now like an analyst might just use at their Mac or PC but we're going to tie
their Mac or PC but we're going to tie it together by Day end so at this
it together by Day end so at this terminal uh SQL light let me go ahead
terminal uh SQL light let me go ahead and execute uh this command first I'm
and execute uh this command first I'm going to first put SQL light into CSV
going to first put SQL light into CSV mode because I'm going to cut some
mode because I'm going to cut some Corners initially and I'm just going to
Corners initially and I'm just going to automatically import all of the data
automatically import all of the data that was submitted via that Google form
that was submitted via that Google form which I exported as a CSV and uploaded
which I exported as a CSV and uploaded to my code space and I'm just going to
to my code space and I'm just going to automatically say turn this CSV file
automatically say turn this CSV file into a SQL database for me just so I
into a SQL database for me just so I don't have to figure out what those
don't have to figure out what those create table commands are so to do this
create table commands are so to do this I'm going to say mode CSV so that SQL
I'm going to say mode CSV so that SQL light knows that this is the command uh
light knows that this is the command uh knows that this is a CSV file it's
knows that this is a CSV file it's literally mode so the dot comes before
literally mode so the dot comes before the keyword there and now I'm going to
the keyword there and now I'm going to say do import and then the name of the
say do import and then the name of the file I want to import which is
file I want to import which is favorites. CSV and now the name of the
favorites. CSV and now the name of the table that I want to create with that
table that I want to create with that data and just for consistency I'm going
data and just for consistency I'm going to call it favorites I could change
to call it favorites I could change these things to be anything I want but
these things to be anything I want but I'm going to do that and voila nothing
I'm going to do that and voila nothing seems to have happened but just like in
seems to have happened but just like in C and in Python in Linux when nothing
C and in Python in Linux when nothing seems to happen that's usually a good
seems to happen that's usually a good thing it means I didn't mess up so if I
thing it means I didn't mess up so if I want to see what just happened there's
want to see what just happened there's this other command and these commands
this other command and these commands that start with dots these are SQL light
that start with dots these are SQL light specific which is indeed a lightweight
specific which is indeed a lightweight version of SQL they're not SQL per se so
version of SQL they're not SQL per se so if you're using Oracle or something else
if you're using Oracle or something else like that you're not going to use these
like that you're not going to use these exact commands you'll see the ones we
exact commands you'll see the ones we use in just a moment and here's the
use in just a moment and here's the first when I type schema the schema of a
first when I type schema the schema of a database is the design of the database
database is the design of the database what are the tables what are the columns
what are the tables what are the columns and all of that so when I type schema
and all of that so when I type schema this actually in this case shows me that
this actually in this case shows me that create table command that was
create table command that was automatically run for me by just doing
automatically run for me by just doing this import line once I get more
this import line once I get more comfortable with SQL I could literally
comfortable with SQL I could literally type this out myself or use some program
type this out myself or use some program to generate that as well but what it's
to generate that as well but what it's creating for me is this create table if
creating for me is this create table if it doesn't exist even though it's more
it doesn't exist even though it's more tur than that I want to create a table
tur than that I want to create a table called favorites and then the columns
called favorites and then the columns for that table are going to be timestamp
for that table are going to be timestamp which is going to be text comma language
which is going to be text comma language which is also going to be text comma
which is also going to be text comma problem which is also going to be text
problem which is also going to be text that was just inferred very trivially by
that was just inferred very trivially by the do import command to just figure out
the do import command to just figure out that yes just give me a three column
that yes just give me a three column database table based on the Google
database table based on the Google form okay questions on this these are
form okay questions on this these are commands you run once to get up and
commands you run once to get up and running you don't run these commands
running you don't run these commands frequently but we have them on the slide
frequently but we have them on the slide just for reference all right so now
just for reference all right so now let's do something a little more
let's do something a little more interesting I'm going to clear my SQL
interesting I'm going to clear my SQL light terminal here but I'm still in SQL
light terminal here but I'm still in SQL light I'm going to now use some of my
light I'm going to now use some of my first SQL commands which recall were uh
first SQL commands which recall were uh were among them uh select so crud c r UD
were among them uh select so crud c r UD D the r was select this is maybe the
D the r was select this is maybe the most common the most useful the most
most common the most useful the most powerful thing to use with a SQL
powerful thing to use with a SQL database selecting data to answer
database selecting data to answer questions akin to the ones we were
questions akin to the ones we were trying to answer with python this is the
trying to answer with python this is the general syntax anytime you want to
general syntax anytime you want to select data from a SQL database you
select data from a SQL database you literally say select you then specify
literally say select you then specify the column or columns that you want to
the column or columns that you want to select data from you literally write the
select data from you literally write the word from and then you specify the name
word from and then you specify the name of the table you want to get that data
of the table you want to get that data from semicolon in this case everything
from semicolon in this case everything that's in capitals here is a sequel
that's in capitals here is a sequel keyword strictly speaking you don't have
keyword strictly speaking you don't have to capitalize things but we would
to capitalize things but we would encourage you to do so stylistically and
encourage you to do so stylistically and especially as you're learning and even
especially as you're learning and even as you're writing it it just helps to
as you're writing it it just helps to distinguish SQL from like words you
distinguish SQL from like words you chose like the names of the columns and
chose like the names of the columns and the data there're in so uh do adopt
the data there're in so uh do adopt early on this convention so let me go
early on this convention so let me go back now to my code space here I'm
back now to my code space here I'm running my terminal window with SQL
running my terminal window with SQL light 3 inside of it suppose that I just
light 3 inside of it suppose that I just want to get all of the data from the
want to get all of the data from the favorites table which was automatically
favorites table which was automatically imported let's do this select I want
imported let's do this select I want everything well I can do timestamp comma
everything well I can do timestamp comma language comma problem but you know what
language comma problem but you know what here's a uh convenience already if you
here's a uh convenience already if you want everything there's what's called a
want everything there's what's called a wild card character in SQL which is just
wild card character in SQL which is just a star in asterisk which means give me
a star in asterisk which means give me every column without my knowing even
every column without my knowing even what they're called let me go ahead now
what they're called let me go ahead now and say from favorite it's semicolon and
and say from favorite it's semicolon and this is the SQL way of opening the
this is the SQL way of opening the database iterating over every row
database iterating over every row they're in printing out every row
they're in printing out every row therein done so those three steps which
therein done so those three steps which was like nine lines of python code give
was like nine lines of python code give or take earlier is now one line of SQL I
or take earlier is now one line of SQL I hit enter there is all of the data so I
hit enter there is all of the data so I see now all of the data just outputed as
see now all of the data just outputed as a CSV here but it's not the CSV file
a CSV here but it's not the CSV file it's now actually the table and in fact
it's now actually the table and in fact just for good measure let me do this CU
just for good measure let me do this CU you'll see the behavior a little
you'll see the behavior a little different the next time we open the file
different the next time we open the file I've just exited out of SQL light 3 I'm
I've just exited out of SQL light 3 I'm going to rerun it but I'm not going to
going to rerun it but I'm not going to reimport the data or do anything like
reimport the data or do anything like that because my file now exists in fact
that because my file now exists in fact let me take one step back if I type LS
let me take one step back if I type LS at my Linux prompt there's my favorites.
at my Linux prompt there's my favorites. py from before there's my favorites. CSV
py from before there's my favorites. CSV from before and here's a third file that
from before and here's a third file that I did create a moment ago when I first
I did create a moment ago when I first ran SQL light 3 so the data is
ran SQL light 3 so the data is persistent it's not using Ram or memory
persistent it's not using Ram or memory anything I do now is save there so let's
anything I do now is save there so let's go ahead and rerun SQL light 3 with the
go ahead and rerun SQL light 3 with the same file but I'm not going to I don't
same file but I'm not going to I don't have to reort everything because the
have to reort everything because the file already exists let me now do that
file already exists let me now do that same thing again select star from
same thing again select star from favorites to get all of the data and
favorites to get all of the data and what you'll see now is the same data but
what you'll see now is the same data but it's a little prettier now because I
it's a little prettier now because I reran it I effectively disabled CSV mode
reran it I effectively disabled CSV mode this time and what I'm now seeing is the
this time and what I'm now seeing is the entire contents of this database table
entire contents of this database table called favorit now there's nothing new
called favorit now there's nothing new here but you're just seeing now like an
here but you're just seeing now like an asky or Unicode version of all of the
asky or Unicode version of all of the same data from that database well
same data from that database well suppose I want to get a subset of the
suppose I want to get a subset of the data well let me clear my screen and
data well let me clear my screen and just like in Linux I can hit contrl L
just like in Linux I can hit contrl L just to clean things up aesthetically
just to clean things up aesthetically suppose I want to get just the languages
suppose I want to get just the languages so I could do select language from
so I could do select language from favorites and this will now select not
favorites and this will now select not all three columns AKA star this will
all three columns AKA star this will only select the language column and all
only select the language column and all of the data they're in if I hit enter
of the data they're in if I hit enter voila now I just see those there no time
voila now I just see those there no time Stamps no problems it's just a slice of
Stamps no problems it's just a slice of the table if you will all right not that
the table if you will all right not that interesting still still because it's
interesting still still because it's just a big column of data but now things
just a big column of data but now things get more interesting it turns out in SQL
get more interesting it turns out in SQL that there are functions that come with
that there are functions that come with this language just like C just like
this language just like C just like python in SQL some of the more useful
python in SQL some of the more useful ones some of the simpler ones are these
ones some of the simpler ones are these here average count distinct lower Max
here average count distinct lower Max Min upper which pretty much uh do what
Min upper which pretty much uh do what they say and count is a particularly
they say and count is a particularly useful one let's start with that you
useful one let's start with that you know it's a reasonable question to be
know it's a reasonable question to be asked uh how many people submitted the
asked uh how many people submitted the Google form by the time I actually
Google form by the time I actually downloaded the CSV well why don't we go
downloaded the CSV well why don't we go ahead and do this let me go back to VSS
ahead and do this let me go back to VSS code here in my terminal window let me
code here in my terminal window let me select not star but the count of star so
select not star but the count of star so give me the count of the rows that are
give me the count of the rows that are being returned from the database called
being returned from the database called uh the database table called favorites
uh the database table called favorites now when I hit enter I'm not going to
now when I hit enter I'm not going to get all the data I'm just going to get
get all the data I'm just going to get simply a number 430 rows came back so
simply a number 430 rows came back so that's pretty good I now know how much
that's pretty good I now know how much data is in there well what languages
data is in there well what languages were in there well I could do select
were in there well I could do select language from favorit just as before but
language from favorit just as before but that's not that useful especially if I'm
that's not that useful especially if I'm inheriting the data like I'm the analyst
inheriting the data like I'm the analyst who's been handed a data set by my boss
who's been handed a data set by my boss and they want me to like crunch some
and they want me to like crunch some numbers okay I could like load this into
numbers okay I could like load this into Excel I could sort it but you can use
Excel I could sort it but you can use SQL now to answer pretty basic questions
SQL now to answer pretty basic questions too if you want to select the distinct
too if you want to select the distinct languages in the data set because you
languages in the data set because you didn't you weren't privy to the Google
didn't you weren't privy to the Google form let me go ahead and select only the
form let me go ahead and select only the distinct languages from the favorites
distinct languages from the favorites table and now I hit enter and I get back
table and now I hit enter and I get back a much more succinct answer just the
a much more succinct answer just the three languages in question not really
three languages in question not really that useful since I created the Google
that useful since I created the Google form but certainly if you're inheriting
form but certainly if you're inheriting data from someone else you've just
data from someone else you've just downloaded a data set at least now I'm
downloaded a data set at least now I'm arguably wrapping my mind around what's
arguably wrapping my mind around what's going on now this is not necessary for
going on now this is not necessary for such a small data set but I can combine
such a small data set but I can combine these things select the count of the
these things select the count of the distinct languages in this data set
distinct languages in this data set called favorites and now I should get
called favorites and now I should get back what
back what answer so hopefully indeed an answer
answer so hopefully indeed an answer called three and what you're getting
called three and what you're getting back notice aesthetically too is like a
back notice aesthetically too is like a mini temporary table when I asked for uh
mini temporary table when I asked for uh just the distinct languages what SQL
just the distinct languages what SQL hands me back is this temporary table in
hands me back is this temporary table in memory that has one column called
memory that has one column called language and then two row uh three rows
language and then two row uh three rows now this is not saved anywhere it's just
now this is not saved anywhere it's just executed ephemerally like this but
executed ephemerally like this but that's why it's depicted in this way
that's why it's depicted in this way what you're getting is subsets of your
what you're getting is subsets of your data smaller tables containing some of
data smaller tables containing some of your data and same thing down here this
your data and same thing down here this is like a crazy long
is like a crazy long uh column name you can rename it if you
uh column name you can rename it if you really want uh but uh that's all we're
really want uh but uh that's all we're seeing there and in fact if that's a
seeing there and in fact if that's a little ugly we can actually Alias These
little ugly we can actually Alias These Things N is a common uh name for a
Things N is a common uh name for a variable a number in any programming
variable a number in any programming language so I can actually alas this to
language so I can actually alas this to be a column called n hit enter and now
be a column called n hit enter and now I'm getting a tiny tiny table whose
I'm getting a tiny tiny table whose column is called n that just has the one
column is called n that just has the one value
value there all right questions on
there all right questions on these application of these functions
here no questions yeah say a little louder as oh as as
yeah say a little louder as oh as as literally in English so name this column
literally in English so name this column rename this column as this technically
rename this column as this technically it creates an alias for the column so
it creates an alias for the column so that's all
yeah exactly distinct will operate on whatever you handed in parentheses and
whatever you handed in parentheses and get rid of all of the duplicates giving
get rid of all of the duplicates giving you back just the unique
you back just the unique correct other questions here
yeah good question when you define an alias like n which I just did does it
alias like n which I just did does it become like a variable you can reuse
become like a variable you can reuse short answer no in this case but you can
short answer no in this case but you can reuse it within your same query even
reuse it within your same query even though these these queries are getting a
though these these queries are getting a little longer admittedly statements that
little longer admittedly statements that they are uh you can actually reuse n in
they are uh you can actually reuse n in even longer queries so later in your
even longer queries so later in your query and we'll see a few that are start
query and we'll see a few that are start to going to start to grow in length so
to going to start to grow in length so it's a a nice way of nicknaming things
it's a a nice way of nicknaming things just to be a little more tur in your
just to be a little more tur in your query so we can transition to some of
query so we can transition to some of these more sophisticated queries because
these more sophisticated queries because it turns out there's some other uh
it turns out there's some other uh techniques we can introduce as well here
techniques we can introduce as well here are some other keywords in SQL and again
are some other keywords in SQL and again even though like this is another list of
even though like this is another list of things there's only four things
things there's only four things fundamentally we're doing creating
fundamentally we're doing creating reading updating and deleting data these
reading updating and deleting data these are just allowing us to like fine-tune
are just allowing us to like fine-tune how we do it exactly so where is going
how we do it exactly so where is going to allow us to filter data as we'll do
to allow us to filter data as we'll do in just a moment like select data where
in just a moment like select data where this conditional is true uh like is
this conditional is true uh like is going to be an alternative to an equal
going to be an alternative to an equal sign so instead of looking for exactly
sign so instead of looking for exactly scratch or exactly python or exactly C
scratch or exactly python or exactly C you can look for something like dot dot
you can look for something like dot dot dot and it can be a little bit of a
dot and it can be a little bit of a fuzzier match if you will uh with other
fuzzier match if you will uh with other characters as well order by is going to
characters as well order by is going to deal with sorting limit is going to just
deal with sorting limit is going to just let me limit the total number of rows
let me limit the total number of rows that come back to one or 10 or finite if
that come back to one or 10 or finite if I don't want to see all 400 plus rows
I don't want to see all 400 plus rows all at once because I'm just trying to
all at once because I'm just trying to wrap my mind around it and group by is
wrap my mind around it and group by is best shown by example so let's play with
best shown by example so let's play with just a couple of these as well let me go
just a couple of these as well let me go back to vs code here I'll clear my
back to vs code here I'll clear my screen I'm still in the same SQL light
screen I'm still in the same SQL light instance and let's count how many of you
instance and let's count how many of you likeed C without writing python code as
likeed C without writing python code as before so let me go ahead and select the
before so let me go ahead and select the count of the rows from favorites where
count of the rows from favorites where the language in each row equals c and
the language in each row equals c and the convention in SQL light is to use
the convention in SQL light is to use single quotes anytime you're surrounding
single quotes anytime you're surrounding a string that's meant to represent a
a string that's meant to represent a literal piece of text uh as opposed to C
literal piece of text uh as opposed to C which was double quotes or python which
which was double quotes or python which was either so this is selecting the
was either so this is selecting the count of rows from favorites table where
count of rows from favorites table where the language in question is C enter and
the language in question is C enter and this gives me 98 notice though if I omit
this gives me 98 notice though if I omit that predicate like we did before you'll
that predicate like we did before you'll get back the total number of rows that
get back the total number of rows that were in the table so where is what's
were in the table so where is what's called a predicate that just allows me
called a predicate that just allows me to filter things just like an if
to filter things just like an if condition or the like in a language that
condition or the like in a language that we've seen before you can be a little
we've seen before you can be a little more specific like how many people
more specific like how many people really liked C and the Mario program uh
really liked C and the Mario program uh problem specifically well let's do this
problem specifically well let's do this uh let's go ahead and do select the
uh let's go ahead and do select the number of rows from the favorites table
number of rows from the favorites table where the language is C and so it's uh
where the language is C and so it's uh still literally the word ands and or
still literally the word ands and or just like in Python but not like in C uh
just like in Python but not like in C uh and
and equals Mario so let's see if there's any
equals Mario so let's see if there's any fans of both C and the Mario problem and
fans of both C and the Mario problem and three of us really like those two things
three of us really like those two things together in this case all right what
together in this case all right what else can we do well more compelling
else can we do well more compelling might be to see kind of like in Python
might be to see kind of like in Python for each language what was the
for each language what was the popularity thereof and at the moment we
popularity thereof and at the moment we don't really have a way of doing that
don't really have a way of doing that except in Python where we had the loop
except in Python where we had the loop and we had those variables with the
and we had those variables with the dictionary that did all that counting
dictionary that did all that counting for us you know totally doable but
for us you know totally doable but tedious especially if your job is to
tedious especially if your job is to analyze data my God like even writing 15
analyze data my God like even writing 15 lines of code to answer simple questions
lines of code to answer simple questions is kind of ridiculous SQL can do better
is kind of ridiculous SQL can do better for us so let me go ahead and do this
for us so let me go ahead and do this let me go ahead and select every
let me go ahead and select every language and the count thereof from the
language and the count thereof from the favorites table but this time Group by
favorites table but this time Group by language so this was another one of the
language so this was another one of the keywords that we can use in this
keywords that we can use in this abbreviated list of extra features of
abbreviated list of extra features of SQL and this one's a little takes a
SQL and this one's a little takes a moment to wrap your mind around but this
moment to wrap your mind around but this is going to give me a two column
is going to give me a two column temporary table where the First Column
temporary table where the First Column is a language and the second column is
is a language and the second column is the count thereof from this data set and
the count thereof from this data set and group by language just means that only
group by language just means that only show me scratch once only show me c once
show me scratch once only show me c once only show me python once that is group
only show me python once that is group all of the identical values together but
all of the identical values together but keep track of how many of them there are
keep track of how many of them there are and so now if I go over to SQL light and
and so now if I go over to SQL light and I hit enter now I have in SQL version
I hit enter now I have in SQL version the exact same output that I had from
the exact same output that I had from python that took me what 15 plus lines
python that took me what 15 plus lines before now we're down to just one
before now we're down to just one because SQL structured query language is
because SQL structured query language is all about constructing queries like this
all about constructing queries like this to answer questions and get back answers
to answer questions and get back answers quickly if we want to clean this up a
quickly if we want to clean this up a little bit you asked earlier about
little bit you asked earlier about sorting order well we can do that too
sorting order well we can do that too there's another uh key phrase we can use
there's another uh key phrase we can use here we can order by the count of those
here we can order by the count of those rows and then run that query here so now
rows and then run that query here so now unfortunately they're from smallest to
unfortunately they're from smallest to biggest but we can reverse that it turns
biggest but we can reverse that it turns out and my query is starting to wrap
out and my query is starting to wrap here I'll I'll zoom out for a moment if
here I'll I'll zoom out for a moment if you want to order by count the default
you want to order by count the default is in ascending order abbreviated ASC if
is in ascending order abbreviated ASC if you want to reverse the sort in SQL
you want to reverse the sort in SQL instead of using reverse equals true
instead of using reverse equals true like we did in Python you say DC for
like we did in Python you say DC for descending order and now we get almost
descending order and now we get almost the same output but flipped in Reverse
the same output but flipped in Reverse so it's just a lot faster to answer
so it's just a lot faster to answer questions once of course you get some
questions once of course you get some muscle memory and some comfort with it
muscle memory and some comfort with it well what else can I do you know what if
well what else can I do you know what if I just care about the most popular
I just care about the most popular language I don't care about the second
language I don't care about the second place or the third place languages or
place or the third place languages or anything else well let me add one more
anything else well let me add one more Clause here limit the answer to one and
Clause here limit the answer to one and no matter how many rows should come back
no matter how many rows should come back now I just get the number one language
now I just get the number one language as of the data set we collected with 27
as of the data set we collected with 27 uh 270 votes for
uh 270 votes for it
it questions on
questions on this any questions
this any questions here no well what if uh you know we're
here no well what if uh you know we're starting to introduce SQL and it was
starting to introduce SQL and it was kind of too late to make it into the
kind of too late to make it into the this the Google form so it turns out
this the Google form so it turns out there Syntax for this too you can create
there Syntax for this too you can create data of course not just the tables but
data of course not just the tables but the data they're in and here's like the
the data they're in and here's like the typical Syntax for inserting data into a
typical Syntax for inserting data into a SQL database you literally say insert
SQL database you literally say insert into the name of the table and then in
into the name of the table and then in parentheses you specify one or more
parentheses you specify one or more columns for which you have values that
columns for which you have values that you want to insert this is to say you
you want to insert this is to say you don't have to give values for every
don't have to give values for every column in the given row if you only have
column in the given row if you only have answers to some of those questions you
answers to some of those questions you can enumerate them here like this but
can enumerate them here like this but the values you insert are going to be
the values you insert are going to be these so you literally say after the
these so you literally say after the closed parenthesis values and then in a
closed parenthesis values and then in a second set of parentheses with the same
second set of parentheses with the same length comma separated list you specify
length comma separated list you specify what values do you want to insert so
what values do you want to insert so it's a little verbose and frankly longer
it's a little verbose and frankly longer term you're going to use like python
term you're going to use like python code to automatically do these kinds of
code to automatically do these kinds of insertions but let's go ahead and try
insertions but let's go ahead and try this right now if I do select
this right now if I do select distinct uh language from favorites
distinct uh language from favorites again we see this just these three
again we see this just these three candidates but we've now taught you a
candidates but we've now taught you a bit of sequel so let's do insert into
bit of sequel so let's do insert into favorites the column called
favorites the column called language uh and you know what let's I'm
language uh and you know what let's I'm going to give a problem here the values
going to give a problem here the values for which and let me Zoom back out are
for which and let me Zoom back out are going to be quote unquote SQL and quote
going to be quote unquote SQL and quote unquote 50v you'll see soon see what
unquote 50v you'll see soon see what that's all about semicolon nothing seems
that's all about semicolon nothing seems to happen but that's usually a good
to happen but that's usually a good thing and now if I scroll back up in my
thing and now if I scroll back up in my my queries in SQL light three you can
my queries in SQL light three you can scroll back and forth in time and uh to
scroll back and forth in time and uh to avoid retyping things now I should see
avoid retyping things now I should see indeed four candidate languages here now
indeed four candidate languages here now suppose that you were never really a fan
suppose that you were never really a fan of c and maybe you uh programmed a
of c and maybe you uh programmed a little bit in high school or in the real
little bit in high school or in the real world and you liked C++ well there's a
world and you liked C++ well there's a whole lot of answers for C so select
whole lot of answers for C so select star from favorites where language
star from favorites where language equals quote unquote C so here's
equals quote unquote C so here's everyone who submitted the answer for C
everyone who submitted the answer for C let's presume that no they didn't really
let's presume that no they didn't really want C they wanted C++ which is not a
want C they wanted C++ which is not a language we teach in the class but I
language we teach in the class but I could also now do this you can use the
could also now do this you can use the update command to set a column or
update command to set a column or columns to different values where some
columns to different values where some condition is met so if I do update table
condition is met so if I do update table name set column name equal to some value
name set column name equal to some value filtering it perhaps by where some
filtering it perhaps by where some condition is true so suppose I've
condition is true so suppose I've changed my mind or you know what let's
changed my mind or you know what let's go ahead and do
go ahead and do update uh favorites set language equal
update uh favorites set language equal to maybe C++ where language equals c now
to maybe C++ where language equals c now this is destructive so you generally
this is destructive so you generally don't want to do this unless you have a
don't want to do this unless you have a backup of your data too overriding what
backup of your data too overriding what people's answers are this seems to have
people's answers are this seems to have been successful because no error
been successful because no error messages and if I rerun the previous
messages and if I rerun the previous select that gives me all of the
select that gives me all of the favorites where language equals c now
favorites where language equals c now indeed I get none but if I search for
indeed I get none but if I search for C++ now I get a lot and if I get rid of
C++ now I get a lot and if I get rid of that wear Clause altogether and just
that wear Clause altogether and just look at the contents of my database now
look at the contents of my database now you see that indeed C++ is comingled
you see that indeed C++ is comingled with all the other data this is not what
with all the other data this is not what you all intended of course so I can undo
you all intended of course so I can undo this let me go ahead and undo what I
this let me go ahead and undo what I just did let me set my favorite language
just did let me set my favorite language to C where language equals C plus plus
to C where language equals C plus plus but the predicate is important this I'm
but the predicate is important this I'm not going to do what if I accidentally
not going to do what if I accidentally omitted this predicate the wear Clause
omitted this predicate the wear Clause how would that screw things up might you
think uh yeah I'm back it would set Every Rose language to
back it would set Every Rose language to indeed see and this is dangerous and if
indeed see and this is dangerous and if you start googling around for like
you start googling around for like sequel mistakes or the like people in
sequel mistakes or the like people in the real world have accidentally run
the real world have accidentally run commands like this and without naming
commands like this and without naming names a former member of our teaching
names a former member of our teaching staff at one point accidentally re ran a
staff at one point accidentally re ran a command like this and changed every
command like this and changed every student's name in our database to Bobby
student's name in our database to Bobby I think it was the same name for every
I think it was the same name for every row because they simply forgot a
row because they simply forgot a predicate so here to two like there's
predicate so here to two like there's dangers in code and you should adopt The
dangers in code and you should adopt The Habit quite quickly of always one
Habit quite quickly of always one backing up your data like with CP for
backing up your data like with CP for instance in Linux or any other technique
instance in Linux or any other technique or just making sure before you hit enter
or just making sure before you hit enter that yes this is indeed the query I want
that yes this is indeed the query I want to execute and generally speaking in the
to execute and generally speaking in the real world there should be process
real world there should be process controls in place like the intern should
controls in place like the intern should not have access to the datab the
not have access to the datab the production database the live database
production database the live database and the like but you have a lot of power
and the like but you have a lot of power now with these queries so just be all
now with these queries so just be all the more careful cuz very easily can you
the more careful cuz very easily can you do bad things so let me undo this where
do bad things so let me undo this where language equals quote unquote C++ and
language equals quote unquote C++ and I'll Zoom back out enter and now I think
I'll Zoom back out enter and now I think we're back in business C is among the
we're back in business C is among the answers
yeah is essentially doing what at the end replace it's essentially find and
end replace it's essentially find and replace yes in like lay person's terms
replace yes in like lay person's terms this is find and replace implemented
this is find and replace implemented with SQL and in fact the authors of
with SQL and in fact the authors of Microsoft Word or Google Docs might very
Microsoft Word or Google Docs might very well be using language like this sequel
well be using language like this sequel when you go to the nice graphical
when you go to the nice graphical userfriendly find and replace box this
userfriendly find and replace box this may very well be what they're doing
may very well be what they're doing underneath the hood or of course they
underneath the hood or of course they could be using some other language
could be using some other language altogether there's one last uh syntax
altogether there's one last uh syntax that's worth knowing delete which for
that's worth knowing delete which for better for worse is even more
better for worse is even more destructive whereby it allows you to
destructive whereby it allows you to delete rows from tables it's distinct
delete rows from tables it's distinct from drop which lets you delete tables
from drop which lets you delete tables themselves this focuses on rows so
themselves this focuses on rows so suppose that you really really didn't
suppose that you really really didn't like let's say uh tedin was a little
like let's say uh tedin was a little challenging if you adopt if you tackle
challenging if you adopt if you tackle that more comfortable problem so if you
that more comfortable problem so if you really don't want to even think about t
really don't want to even think about t in anymore so why don't we do uh delete
in anymore so why don't we do uh delete from favorites where problem equals and
from favorites where problem equals and I won't execute it for real tiamin this
I won't execute it for real tiamin this would have the effect of deleting every
would have the effect of deleting every row including the language therein and
row including the language therein and the time stamp where the student
the time stamp where the student answered tiamin worse than this would be
answered tiamin worse than this would be this why might this be bad
okay chuckling because like there's no predicate there's no filter which means
predicate there's no filter which means literally this would delete all of the
literally this would delete all of the data so again with great power here
data so again with great power here comes great responsibility now this has
comes great responsibility now this has just been a data set of what 430 rows by
just been a data set of what 430 rows by us dynamically created there's of course
us dynamically created there's of course some really juicy data sets in the real
some really juicy data sets in the real world and one website you might have
world and one website you might have heard or an app you might have used is
heard or an app you might have used is IMDb the internet movie database which
IMDb the internet movie database which wonderfully makes some of their data
wonderfully makes some of their data available for download as CSV files or
available for download as CSV files or technically tsv files tab separated
technically tsv files tab separated values but what we did in advance of
values but what we did in advance of class was download some of that data for
class was download some of that data for both TV shows in the real world and
both TV shows in the real world and movies in the real world and what's
movies in the real world and what's wonderful about this data set is it's
wonderful about this data set is it's not just dozens or hundreds or even
not just dozens or hundreds or even thousands of lines there are millions of
thousands of lines there are millions of rows of Juicy data TV shows and movies
rows of Juicy data TV shows and movies with which most folks are probably
with which most folks are probably familiar at least with the subset and
familiar at least with the subset and we'll see in just a little bit that this
we'll see in just a little bit that this data comes in the form of now six
data comes in the form of now six different tables that we've given you
different tables that we've given you and the tables and question for today
and the tables and question for today are going to be the people in the TV
are going to be the people in the TV business the Stars they in the shows
business the Stars they in the shows that people are producing and the like
that people are producing and the like this is a picture we'll revisit to let
this is a picture we'll revisit to let enable you to wrap your minds around
enable you to wrap your minds around what the actual data is this feels like
what the actual data is this feels like a good opportunity though for a snack in
a good opportunity though for a snack in fact in just a moment we have a whole
fact in just a moment we have a whole lot of Rice Krispie treats out in the
lot of Rice Krispie treats out in the lobby but if folks could perhaps
lobby but if folks could perhaps acknowledge uh this mini wedding cake
acknowledge uh this mini wedding cake here cs50 own Carter zeni is getting
here cs50 own Carter zeni is getting married this week so congratulations to
married this week so congratulations to Carter
Carter as
as well
well congrats all right there's
only okay there's only one piece of cake in that box but a lot of Rice Krispy
in that box but a lot of Rice Krispy Treats in the transip let's take 10
Treats in the transip let's take 10 minutes and we'll be back with Internet
minutes and we'll be back with Internet Movie Database in 10 all right we are
Movie Database in 10 all right we are back so if you've never been like you
back so if you've never been like you can actually go to I imdb.com right now
can actually go to I imdb.com right now and play around or download the mobile
and play around or download the mobile app and it's just big database of a lot
app and it's just big database of a lot of TV show and movies and actors and the
of TV show and movies and actors and the like but what indeed is nice is you can
like but what indeed is nice is you can download some of that data and that's in
download some of that data and that's in what I've done in advance and what we've
what I've done in advance and what we've done is we wrote some python code to
done is we wrote some python code to convert some of the uh flat file
convert some of the uh flat file databases that they let you download and
databases that they let you download and we converted it into a SQL database with
we converted it into a SQL database with six tables so not just one but six that
six tables so not just one but six that ultimately are these here and let me
ultimately are these here and let me just help you wrap your minds around
just help you wrap your minds around what this picture is which is a entity
what this picture is which is a entity relationship diagram which is just to
relationship diagram which is just to say each of these boxes on the screen
say each of these boxes on the screen represents a table and each of the
represents a table and each of the arrows or edges represents some kind of
arrows or edges represents some kind of relationship across the tables because
relationship across the tables because up until now the only data we had were
up until now the only data we had were those three columns in the favorites
those three columns in the favorites table but what's gets really useful
table but what's gets really useful about SQL databases just like a Google
about SQL databases just like a Google spreadsheet or an Excel file is you can
spreadsheet or an Excel file is you can have multiple sheets or in a database
have multiple sheets or in a database multiple tables and so what we're about
multiple tables and so what we're about to see is that in this IMDb database for
to see is that in this IMDb database for TV shows there's going to be a dedicated
TV shows there's going to be a dedicated table for all the people in the TV
table for all the people in the TV business there's going to be a dedicated
business there's going to be a dedicated table for all of the TV shows that are
table for all of the TV shows that are in their database as of right now
in their database as of right now there's going to be a dedicated table
there's going to be a dedicated table for writers in that industry for the
for writers in that industry for the ratings of uh shows for the genres to
ratings of uh shows for the genres to which shows belong comedy and the like
which shows belong comedy and the like and then lastly there's going to be this
and then lastly there's going to be this table which somehow
table which somehow Associates people with the TV shows that
Associates people with the TV shows that they star in and vice versa and so let's
they star in and vice versa and so let's consider first what this looks like in
consider first what this looks like in code and we'll see that it's going to
code and we'll see that it's going to overwhelm intentionally at first but I'm
overwhelm intentionally at first but I'm going to do this I'm going to go back to
going to do this I'm going to go back to my terminal window and during the break
my terminal window and during the break I downloaded from the course's website a
I downloaded from the course's website a file called shows. DB which we made in
file called shows. DB which we made in advance for you and if I type LS I'll
advance for you and if I type LS I'll see all of my favorites files from
see all of my favorites files from before the CSV the DB and the python
before the CSV the DB and the python file but now there's shows. DB so I'm
file but now there's shows. DB so I'm going to go ahead in my full screen
going to go ahead in my full screen terminal window here I'm not using
terminal window here I'm not using actual tabs or code files now I'm going
actual tabs or code files now I'm going to run SQL light3 on the file called
to run SQL light3 on the file called shows. DB and I'm just going to see this
shows. DB and I'm just going to see this version information here let me clear my
version information here let me clear my screen and run the one command I ran
screen and run the one command I ran earlier to show us the schema of the
earlier to show us the schema of the favorites database now we'll see the
favorites database now we'll see the schema for the shows database and
schema for the shows database and there's a lot going on here but let me
there's a lot going on here but let me scroll back up to the very top the
scroll back up to the very top the beginning and we see this here so when I
beginning and we see this here so when I run. schema we see a dump really of all
run. schema we see a dump really of all of the SQL create table commands that
of the SQL create table commands that were run in order to create this
were run in order to create this database for you and one of those tables
database for you and one of those tables is called genres and another people
is called genres and another people ratings shows stars and so forth and the
ratings shows stars and so forth and the columns therein even though it's
columns therein even though it's formatted a little more prettily than
formatted a little more prettily than the automatically generated create table
the automatically generated create table statement for favorites whereby we have
statement for favorites whereby we have one column per line of output here uh in
one column per line of output here uh in the for instance people table there's
the for instance people table there's going to be an ID column like unique
going to be an ID column like unique identifier like a Harvard ID a Yale ID
identifier like a Harvard ID a Yale ID or the like uh a name column a birth
or the like uh a name column a birth year and then some other stuff if I
year and then some other stuff if I scroll down to shows every show in the
scroll down to shows every show in the world is going to have a unique ID as
world is going to have a unique ID as well a title of course the year in which
well a title of course the year in which it debuted and the total number of
it debuted and the total number of episodes as of the time we downloaded
episodes as of the time we downloaded the data and then what else is there
the data and then what else is there some of these are a little less obvious
some of these are a little less obvious like ratings here so ratings don't have
like ratings here so ratings don't have an ID column but they have a show ID
an ID column but they have a show ID column and a rating like a fivepoint
column and a rating like a fivepoint scale or 10o scale or the like and then
scale or 10o scale or the like and then the total number of votes that were
the total number of votes that were collected to contribute to that rating
collected to contribute to that rating IMDb allows people to like up vote and
IMDb allows people to like up vote and down vote uh shows and movies and the
down vote uh shows and movies and the like and then similarly is genre
like and then similarly is genre structured there's a show ID and then
structured there's a show ID and then there's a genre which is going to be
there's a genre which is going to be like an English word like comedy or
like an English word like comedy or drama or something else and then what
drama or something else and then what else let's go a little further at the
else let's go a little further at the bottom here for stars and writers if we
bottom here for stars and writers if we go to the very bottom here stars and
go to the very bottom here stars and writers are similarly structured too
writers are similarly structured too they have a show ID and a person ID so
they have a show ID and a person ID so show and person and then this writer's
show and person and then this writer's table has a show ID and a person ID and
table has a show ID and a person ID and there's a whole lot of other words that
there's a whole lot of other words that we'll come to in just a moment but what
we'll come to in just a moment but what are the what is this code hinting at
are the what is this code hinting at well if I go back to the picture from
well if I go back to the picture from earlier here you'll see that this
earlier here you'll see that this picture captures the relationships among
picture captures the relationships among these various tables so for instance if
these various tables so for instance if we focus on shows for just a moment a
we focus on shows for just a moment a show again has a unique ID a title a
show again has a unique ID a title a year in which it debuted and a total
year in which it debuted and a total number of episodes if you want to figure
number of episodes if you want to figure out what genre or genr a show belongs to
out what genre or genr a show belongs to cuz some shows are just comedies some
cuz some shows are just comedies some shows are just just dramas but you know
shows are just just dramas but you know some shows are arguably comedies and
some shows are arguably comedies and dramas depending on the episode or the
dramas depending on the episode or the like so you can imagine wanting to
like so you can imagine wanting to associate two or three or even more
associate two or three or even more genres with a show this line here in
genres with a show this line here in this second table allows us to do that
this second table allows us to do that every Row in the genres table we'll see
every Row in the genres table we'll see has uh two items a show ID which relates
has uh two items a show ID which relates to the ID of a show and that's why these
to the ID of a show and that's why these lines literally line up with that
lines literally line up with that specific column name and genre which is
specific column name and genre which is going to be like quote unquote comedy
going to be like quote unquote comedy quote unquote drama or something else
quote unquote drama or something else now with that said design question why
now with that said design question why have we deliberately not just gotten rid
have we deliberately not just gotten rid of this genre's table and made our lives
of this genre's table and made our lives simpler by just adding a genre column to
simpler by just adding a genre column to this shows table and again a table is
this shows table and again a table is just like a sheet with rows and columns
just like a sheet with rows and columns at the moment shows only have four
at the moment shows only have four columns ID title year episodes why not
columns ID title year episodes why not just add a fifth column called genre and
just add a fifth column called genre and put the show genre
put the show genre there any intuition
there any intuition here why not just keep things simple
here why not just keep things simple like yeah and
back exactly if you add a fifth column here and call it genre then you have to
here and call it genre then you have to pick a genre specifically you have to
pick a genre specifically you have to put in that cell presumably comedy or
put in that cell presumably comedy or drama or music or something else now you
drama or music or something else now you could write multiple words in the cell
could write multiple words in the cell but generally speaking that would be
but generally speaking that would be sloppy bad design like every cell just
sloppy bad design like every cell just like in a spreadsheet should really have
like in a spreadsheet should really have one value it might have multiple words
one value it might have multiple words but it shouldn't be like a weirdly comma
but it shouldn't be like a weirdly comma separated list of multiple things it
separated list of multiple things it should just be in a different cell in
should just be in a different cell in that case so if you instead were to
that case so if you instead were to design this with just a single column
design this with just a single column called genre you're imposing what a
called genre you're imposing what a computer scientist would call a on toone
computer scientist would call a on toone relationship every show has one genre
relationship every show has one genre and that's not necessarily a good thing
and that's not necessarily a good thing or strictly speaking it would be a many
or strictly speaking it would be a many to one because the same genre could
to one because the same genre could belong to multiple shows but each show
belong to multiple shows but each show could only have one genre in that case
could only have one genre in that case what a relational database allows you to
what a relational database allows you to do and relational is indeed the
do and relational is indeed the operative word it allows you to factor
operative word it allows you to factor out some of your information and then
out some of your information and then have maybe one show here in one row but
have maybe one show here in one row but then in this genres table you could have
then in this genres table you could have one row for that one show genre or you
one row for that one show genre or you could have two rows in the genres table
could have two rows in the genres table for comedy and for drama or if it has a
for comedy and for drama or if it has a third genre you could just add another
third genre you could just add another row here so you still have one row for
row here so you still have one row for the show itself with all the juiciest
the show itself with all the juiciest details but a variable number of rows by
details but a variable number of rows by having this relationship with another
having this relationship with another table meanwhile ratings work the same
table meanwhile ratings work the same way at least in this case a show has ID
way at least in this case a show has ID title year and episodes but if you want
title year and episodes but if you want to figure out its rating you have to
to figure out its rating you have to kind of Follow the arrow here so to
kind of Follow the arrow here so to speak and look up the corresponding show
speak and look up the corresponding show ID in this table find the rating of that
ID in this table find the rating of that show and the total number of ratings so
show and the total number of ratings so that's been factored out Two For Better
that's been factored out Two For Better or For Worse um now let's consider
or For Worse um now let's consider people people have just three columns ID
people people have just three columns ID name and birth but there's no mention of
name and birth but there's no mention of the TV show in which people have starred
the TV show in which people have starred or the TV shows that a person has
or the TV shows that a person has written well why is that well if you
written well why is that well if you just had a fourth column here called
just had a fourth column here called show well you would have to decide what
show well you would have to decide what show is that person in and no one could
show is that person in and no one could ever act again in another show because
ever act again in another show because there's no room to store the data but if
there's no room to store the data but if someone of course a popular actor can St
someone of course a popular actor can St star in multiple shows well we could
star in multiple shows well we could have one ID for that person one name one
have one ID for that person one name one birth year obviously like there's only
birth year obviously like there's only one Steve Carell as an actor in the
one Steve Carell as an actor in the world of people but Steve Carell in this
world of people but Steve Carell in this example could have his person ID
example could have his person ID whatever his Harvard ID equivalent Yale
whatever his Harvard ID equivalent Yale ID equivalent is appear in multiple rows
ID equivalent is appear in multiple rows in this table so that it can be
in this table so that it can be associated with multiple shows and this
associated with multiple shows and this allows you to create what's called a one
allows you to create what's called a one to many relationship or technically it's
to many relationship or technically it's bidirectional it's a many to many
bidirectional it's a many to many relationship why well one show can
relationship why well one show can certain certainly have multiple people
certain certainly have multiple people in it and multiple people writing for it
in it and multiple people writing for it just in the real world but conversely
just in the real world but conversely one uh person could certainly act in
one uh person could certainly act in multiple shows or write multiple shows
multiple shows or write multiple shows so this is what you get with relational
so this is what you get with relational databases you put your sort of canonical
databases you put your sort of canonical data for people in one place for for
data for people in one place for for shows in another place and then you use
shows in another place and then you use these additional tables to relate one
these additional tables to relate one thing to another so we won't dwell on
thing to another so we won't dwell on the pictures that's just if you sort of
the pictures that's just if you sort of uh can wrap your mind around the data
uh can wrap your mind around the data set better that way that's one way of
set better that way that's one way of thinking about it but recall that the
thinking about it but recall that the code we just saw for the schema again
code we just saw for the schema again escalated quickly like there's a lot of
escalated quickly like there's a lot of keywords I haven't mentioned yet but
keywords I haven't mentioned yet but some of these are perhaps familiar
some of these are perhaps familiar they're capitalized differently here but
they're capitalized differently here but integer is on the list here null is on
integer is on the list here null is on the list albeit technically not null so
the list albeit technically not null so let's tease apart some of these key
let's tease apart some of these key words and consider what they're actually
words and consider what they're actually doing for your database because now
doing for your database because now we're in exploring features that do not
we're in exploring features that do not exist in the world of spreadsheets alone
exist in the world of spreadsheets alone so it turns out in a SQL database
so it turns out in a SQL database specifically SQL light which is the
specifically SQL light which is the version of SQL we use in cs50 and which
version of SQL we use in cs50 and which is commonly used for things like mobile
is commonly used for things like mobile applications nowadays it's like a
applications nowadays it's like a lightweight version of SQL it's when you
lightweight version of SQL it's when you aren't trying to run Twitter and have
aren't trying to run Twitter and have billions and billions of rows
billions and billions of rows necessarily you've got hundreds
necessarily you've got hundreds thousands tens of thousands maybe even a
thousands tens of thousands maybe even a few million but not crazy uh numbers uh
few million but not crazy uh numbers uh crazy amounts of data in the world of
crazy amounts of data in the world of SQL light specifically there's these
SQL light specifically there's these five data types so just like in C we had
five data types so just like in C we had int and Char and the like in SQL we have
int and Char and the like in SQL we have these uh BL which is kind of funny but
these uh BL which is kind of funny but it just means binary large object so
it just means binary large object so it's like a binary data type zeros and
it's like a binary data type zeros and ones that aren't necessarily uh fitting
ones that aren't necessarily uh fitting into the other categories integer which
into the other categories integer which of course is an integer as we know it
of course is an integer as we know it numeric which is kind of a catchall for
numeric which is kind of a catchall for numbers that are formatted specially so
numbers that are formatted specially so like a date uh would be like year year
like a date uh would be like year year year year-month month Dash day day um
year year-month month Dash day day um and this is actually a wonderful thing
and this is actually a wonderful thing depending on the country you're from you
depending on the country you're from you might think your date system in your
might think your date system in your country is great or it's horrible the US
country is great or it's horrible the US system is horrible because we have month
system is horrible because we have month day and then year which is impossible to
day and then year which is impossible to sort it is the wrong way objectively to
sort it is the wrong way objectively to store data and yet here we are using
store data and yet here we are using this at scale other countries have
this at scale other countries have gotten this better numeric and SQL
gotten this better numeric and SQL itself standardizes that stuff so it
itself standardizes that stuff so it doesn't matter what country you're from
doesn't matter what country you're from you're storing your data in this
you're storing your data in this particular way for instance times are
particular way for instance times are standardized and other types of numeric
standardized and other types of numeric data as well real is synonymous with
data as well real is synonymous with float so something with a decimal point
float so something with a decimal point and some number of uh digits thereafter
and some number of uh digits thereafter and then text is just uh for strings and
and then text is just uh for strings and the like with other even fancier
the like with other even fancier databases like MySQL postgress SQL
databases like MySQL postgress SQL Oracle and other products you might have
Oracle and other products you might have heard of there's even more data types
heard of there's even more data types where you have to make even finer grain
where you have to make even finer grain decisions but for SQL light it's indeed
decisions but for SQL light it's indeed pretty lightweight and you or we just
pretty lightweight and you or we just have to decide the data types for each
have to decide the data types for each column in a table but there's these
column in a table but there's these additional constraints in the world of
additional constraints in the world of SQL you can additionally say that cells
SQL you can additionally say that cells in this column may or may not be null so
in this column may or may not be null so if you want to protect yourself from
if you want to protect yourself from yourself so you don't screw up and
yourself so you don't screw up and insert a null that is a blank value you
insert a null that is a blank value you can explicitly design a table to have a
can explicitly design a table to have a column that cannot be null and so in
column that cannot be null and so in fact someone came up during the break to
fact someone came up during the break to ask me about my having manually inserted
ask me about my having manually inserted SQL quote unquote SQL into our favorites
SQL quote unquote SQL into our favorites database you might recall that I kind of
database you might recall that I kind of cheated I just inserted uh SQL quote
cheated I just inserted uh SQL quote unquote and 50v the name of a new
unquote and 50v the name of a new problem quote unquote but what did I not
problem quote unquote but what did I not insert into the
insert into the database a Tim stamp and I could have I
database a Tim stamp and I could have I could have put like the current day in
could have put like the current day in time a few minutes ago but I didn't and
time a few minutes ago but I didn't and that's fine if it's uh if it's
that's fine if it's uh if it's acceptable to you and the product you're
acceptable to you and the product you're building but I could have prevented that
building but I could have prevented that if we had defined the table to have a
if we had defined the table to have a timestamp column that isn't just text
timestamp column that isn't just text but it's text that's not null SQL would
but it's text that's not null SQL would have complained and would not have let
have complained and would not have let me complete that insertion so there's
me complete that insertion so there's these kinds of built-in defenses that
these kinds of built-in defenses that you don't necessarily get with a
you don't necessarily get with a spreadsheet alone and unique means
spreadsheet alone and unique means exactly that if you want to make sure
exactly that if you want to make sure that every Row in that column is unique
that every Row in that column is unique maybe for email addresses or in the US
maybe for email addresses or in the US Social Security numbers or anything that
Social Security numbers or anything that you want to make sure you don't have two
you want to make sure you don't have two versions of you can specify that the
versions of you can specify that the column is unique and there's other such
column is unique and there's other such constraints as well but again this is
constraints as well but again this is just a list of features that you get
just a list of features that you get from a proper relational database but
from a proper relational database but perhaps the most intellectually
perhaps the most intellectually interesting one and the most powerful
interesting one and the most powerful one is what's called here a primary key
one is what's called here a primary key and a foreign key and let me go back now
and a foreign key and let me go back now to this output if we look at shows
to this output if we look at shows you'll see that a show again has an ID a
you'll see that a show again has an ID a title a year and a number of episodes
title a year and a number of episodes and now the data types might make sense
and now the data types might make sense the ID it turns out just like a Harvard
the ID it turns out just like a Harvard ID a Yale ID is going to be an integer
ID a Yale ID is going to be an integer so a simple number the title of course
so a simple number the title of course is going to be text but not null like it
is going to be text but not null like it would be weird if a TV show had no name
would be weird if a TV show had no name like that can't be like every the whole
like that can't be like every the whole world would break or your TV gu and
world would break or your TV gu and whatnot so that makes sense there to say
whatnot so that makes sense there to say not null uh year is numeric so it's a
not null uh year is numeric so it's a standardized form episodes is an integer
standardized form episodes is an integer like how many episodes have been
like how many episodes have been produced and then lastly notice this the
produced and then lastly notice this the primary key of the show's table is
primary key of the show's table is apparently the column called ID
apparently the column called ID mentioned a few lines earlier this just
mentioned a few lines earlier this just means that the database will use the ID
means that the database will use the ID column as the unique identifier so it's
column as the unique identifier so it's similar to the unique keyword but
similar to the unique keyword but primary key just means the database is
primary key just means the database is going to treat it as special too and
going to treat it as special too and make sure that it is uniquely
make sure that it is uniquely identifying your data but what's
identifying your data but what's interesting is this notice if I scroll
interesting is this notice if I scroll back up to people people were sort of
back up to people people were sort of similarly structured but with different
similarly structured but with different attributes like up here we had a person
attributes like up here we had a person has an ID a name a birth year and a
has an ID a name a birth year and a primary key of ID so a ID is again
primary key of ID so a ID is again integer name is text but not null
integer name is text but not null because it'd be weird to have a human
because it'd be weird to have a human with absolutely no name textually birth
with absolutely no name textually birth is going to be numeric but the primary
is going to be numeric but the primary key of people is ID as well so those are
key of people is ID as well so those are the unique columns that the database
the unique columns that the database will just treat special why well we just
will just treat special why well we just looked at shows we just looked at people
looked at shows we just looked at people let's focus now on this one down here
let's focus now on this one down here Stars how do you determine who star in a
Stars how do you determine who star in a TV show well we had two columns the show
TV show well we had two columns the show ID and the person ID this is the ex the
ID and the person ID this is the ex the Incarnation of a many to- many
Incarnation of a many to- many relationship one person could be in many
relationship one person could be in many shows one show could certainly have many
shows one show could certainly have many people in it or writing for it but
people in it or writing for it but notice this within this table of two
notice this within this table of two columns show ID and person ID there's
columns show ID and person ID there's what's going to be called a foreign key
what's going to be called a foreign key called show ID that references the shows
called show ID that references the shows tables ID column and then another
tables ID column and then another foreign key called person ID though I
foreign key called person ID though I could call these things in parentheses
could call these things in parentheses anything I want that references the
anything I want that references the people tables ID column now you're not
people tables ID column now you're not going to often have to type commands
going to often have to type commands like this again you set the database up
like this again you set the database up once in the beginning typically maybe
once in the beginning typically maybe with some help from a TF maybe with help
with some help from a TF maybe with help of Google or the like but once your
of Google or the like but once your database is designed it's back to the
database is designed it's back to the crud like create read update delete the
crud like create read update delete the selects the inserts the deletions and
selects the inserts the deletions and the like but what's this implying these
the like but what's this implying these keywords like primary key and foreign
keywords like primary key and foreign key are what are doing in code what this
key are what are doing in code what this picture was painting a moment ago these
picture was painting a moment ago these lines here are drawn literally to line
lines here are drawn literally to line up with the corresponding things
up with the corresponding things people's ID lines up with person ID
people's ID lines up with person ID Show's ID lines up with show ID and so
Show's ID lines up with show ID and so you're just seeing graphical version
you're just seeing graphical version code version graphical code that creates
code version graphical code that creates these relationships now given that let's
these relationships now given that let's actually see what these things look like
actually see what these things look like so let me go back to vs code here let me
so let me go back to vs code here let me clear my screen I'm still within SQL
clear my screen I'm still within SQL light with shows. DB let me go ahead and
light with shows. DB let me go ahead and do what I do with like any new database
do what I do with like any new database if I ever download something or I'm
if I ever download something or I'm trying to wrap my mind around a problem
trying to wrap my mind around a problem you know usually it doesn't come with a
you know usually it doesn't come with a pretty picture or uh you know a
pretty picture or uh you know a three-hour lecture to explain what the
three-hour lecture to explain what the data set is rather you just have the
data set is rather you just have the data set in your own knowledge of SQL so
data set in your own knowledge of SQL so let me play around so schema shows me
let me play around so schema shows me all the tables that might be a starting
all the tables that might be a starting point okay this is interesting I know PE
point okay this is interesting I know PE what people are let's go ahead and show
what people are let's go ahead and show me all the people so select star from
me all the people so select star from people I'm just trying to wrap my mind
people I'm just trying to wrap my mind around what this data set looks like in
around what this data set looks like in a more userfriendly way okay okay that's
a more userfriendly way okay okay that's already a lot of people as you see the
already a lot of people as you see the years flying by there's been a lot of
years flying by there's been a lot of people in the TV
people in the TV business so this was maybe not the best
business so this was maybe not the best query to run but this is indicative of
query to run but this is indicative of just how large this data set is from
just how large this data set is from IMDb okay when in doubt and when
IMDb okay when in doubt and when whenever you lose control over your
whenever you lose control over your computer control C is your friend to
computer control C is your friend to interrupt what would have been better
interrupt what would have been better because I don't think I need to know all
because I don't think I need to know all the million people in the world I could
the million people in the world I could do like limit me to 10 people all right
do like limit me to 10 people all right and that's enough now to get us sense of
and that's enough now to get us sense of like Fred a stair has an IDE of one
like Fred a stair has an IDE of one first person ever um birth year of 1899
first person ever um birth year of 1899 Lauren ball and all of these other uh
Lauren ball and all of these other uh people from yesterday year you see that
people from yesterday year you see that they are the first 10 people in the
they are the first 10 people in the database so there's an example of some
database so there's an example of some of the data now if I want to rep my mind
of the data now if I want to rep my mind around what a show is you know I know it
around what a show is you know I know it technically I know it from the picture
technically I know it from the picture but let's just look at some raw data so
but let's just look at some raw data so instead of saying select star from
instead of saying select star from people let me go ahead and select star
people let me go ahead and select star from shows limit 10 and okay I've only
from shows limit 10 and okay I've only heard heard of or seen a couple of these
heard heard of or seen a couple of these but these are older shows at that but I
but these are older shows at that but I see that every show has an ID a title a
see that every show has an ID a title a year in which it debuted and a number of
year in which it debuted and a number of episodes but perhaps most opaque is
episodes but perhaps most opaque is going to be this select star from
going to be this select star from Stars where this is the table that
Stars where this is the table that Associates people with shows am I going
Associates people with shows am I going to see any names or show titles here not
to see any names or show titles here not according to the definition we saw
according to the definition we saw earlier oh I should have done my limit
earlier oh I should have done my limit let me interrupt that let me do that
let me interrupt that let me do that again limit 10
again limit 10 no and this is where now you're
no and this is where now you're definitely in the programmer world
definitely in the programmer world because like this would be the most
because like this would be the most annoying spreadsheet to use on your Mac
annoying spreadsheet to use on your Mac or PC ever if you just had like a sheet
or PC ever if you just had like a sheet with all of these numbers that
with all of these numbers that Associates one thing with the other like
Associates one thing with the other like my God how do you figure out who this is
my God how do you figure out who this is or what this is you have to like
or what this is you have to like manually control F or command F looking
manually control F or command F looking for the data but a database doesn't care
for the data but a database doesn't care once you know SQL you can sort of Stitch
once you know SQL you can sort of Stitch these things back together so what
these things back together so what you're seeing here are foreign Keys
you're seeing here are foreign Keys foreign Keys why because show ID
foreign Keys why because show ID corresponds to the same numbers from
corresponds to the same numbers from that other table called shows that has
that other table called shows that has an proper primary key called ID person
an proper primary key called ID person ID is a foreign key in this context
ID is a foreign key in this context because it refers to numbers that belong
because it refers to numbers that belong to really the people table and its ID
to really the people table and its ID column so this is just a way of somehow
column so this is just a way of somehow linking them and so if you think of I
linking them and so if you think of I always like think of um this in my
always like think of um this in my mind's eye is this if this is like the
mind's eye is this if this is like the people table this is the shows table and
people table this is the shows table and there's this middle table in between the
there's this middle table in between the Stars table there's some way of like
Stars table there's some way of like stitching those two together by lining
stitching those two together by lining up the idas of one with the other and
up the idas of one with the other and getting back some more data so let's
getting back some more data so let's actually play with some of this data how
actually play with some of this data how about we start where we emphasized
about we start where we emphasized earlier genres so let me go ahead and
earlier genres so let me go ahead and take a quick look at all of the genres
take a quick look at all of the genres in this database so select star from
in this database so select star from genres star is usually going to be a
genres star is usually going to be a little overwhelming but it just gives me
little overwhelming but it just gives me a sense of what the data is but let's
a sense of what the data is but let's actually look at um uh let's go look at
actually look at um uh let's go look at all of them there okay that's a lot
all of them there okay that's a lot these are all official genres from IMDb
these are all official genres from IMDb let me oh okay it went okay it wasn't
let me oh okay it went okay it wasn't terribly long let me filter that down so
terribly long let me filter that down so from genres where genre equals comedy uh
from genres where genre equals comedy uh Capital C just based on the data I'm
Capital C just based on the data I'm seeing okay so what am I seeing now and
seeing okay so what am I seeing now and in fact let me limit this arbitrarily to
in fact let me limit this arbitrarily to 10 though I could limit it to anything I
10 though I could limit it to anything I want here are 10 comedies what are they
want here are 10 comedies what are they well who the heck knows like all I know
well who the heck knows like all I know are the 10 show IDs now I could do
are the 10 show IDs now I could do something like this as we've seen before
something like this as we've seen before with SQL I could do all right well let's
with SQL I could do all right well let's figure out what this show idea is Select
figure out what this show idea is Select star from shows where the IDE of the
star from shows where the IDE of the show I'm looking for equals what 62 614
show I'm looking for equals what 62 614 semicolon so I could like manually look
semicolon so I could like manually look it up by cross referencing the other
it up by cross referencing the other table okay so that was the show in
table okay so that was the show in question there the first comedy in the
question there the first comedy in the data set let me look up the second one
data set let me look up the second one so instead of that let's do 6 3881 enter
so instead of that let's do 6 3881 enter okay so that's that show and let's do
okay so that's that show and let's do one more and suffice it to say this is
one more and suffice it to say this is just getting tedious and vulnerable to
just getting tedious and vulnerable to mistakes quickly this is not this surely
mistakes quickly this is not this surely can't be the way to do this and indeed
can't be the way to do this and indeed SQL is going to let us do this a little
SQL is going to let us do this a little more powerfully instead let's do this
more powerfully instead let's do this instead of getting this table
instead of getting this table temporarily with all these show IDs and
temporarily with all these show IDs and all these genres let's refine the query
all these genres let's refine the query so let's just select the show ID from
so let's just select the show ID from the genres table where the genre equals
the genres table where the genre equals quote unquote comedy now I have a big b
quote unquote comedy now I have a big b list of show IDs all of which are comedy
list of show IDs all of which are comedy how many well I can combine ideas from
how many well I can combine ideas from earlier I can just count all of those
earlier I can just count all of those show IDs and or Star if I want to just
show IDs and or Star if I want to just do that too but I can count all those
do that too but I can count all those show IDs
show IDs 4876 comedies and IMDB's database for TV
4876 comedies and IMDB's database for TV shows so feels like a lot but how can I
shows so feels like a lot but how can I now use that information and get back
now use that information and get back the titles of comedies in the database
the titles of comedies in the database without doing it manually well let's do
without doing it manually well let's do this I have a moment ago this query
this I have a moment ago this query select the show ID from genres where the
select the show ID from genres where the current genre is quote unquote
current genre is quote unquote comedy um what if I kind of Nest these
comedy um what if I kind of Nest these queries kind of like grade school math
queries kind of like grade school math in parenthesis what if I combine this
in parenthesis what if I combine this whole thing in parenthesis and now let
whole thing in parenthesis and now let me select what I really want let me go
me select what I really want let me go ahead and select how about uh the title
ahead and select how about uh the title of all shows where the idea of the show
of all shows where the idea of the show is in this list of show
is in this list of show IDs so if you agree that the shows table
IDs so if you agree that the shows table has an ID column which is its otherwise
has an ID column which is its otherwise known as its primary key the unique ID
known as its primary key the unique ID that identifies it just like our Harvard
that identifies it just like our Harvard IDs our Yale IDs and you agree that per
IDs our Yale IDs and you agree that per a moment ago this shorter query will
a moment ago this shorter query will give me back just the show IDs of all of
give me back just the show IDs of all of the comedies in the database you can
the comedies in the database you can actually combine or Nest these queries
actually combine or Nest these queries together uh it's going to respect SQL
together uh it's going to respect SQL light order of operations with
light order of operations with parentheses just like grade school math
parentheses just like grade school math so the thing in parentheses will be
so the thing in parentheses will be executed first that gives it back a list
executed first that gives it back a list of IDs like what 48,000 IDs and then
of IDs like what 48,000 IDs and then this query the outer query is going to
this query the outer query is going to get the title from all of the shows
get the title from all of the shows where the ID of the show is in that big
where the ID of the show is in that big list of 48,000 so if I now execute these
list of 48,000 so if I now execute these together I think the list is still going
together I think the list is still going to be a little long but let me execute
to be a little long but let me execute it together now I see this long list of
it together now I see this long list of outputs a little overwhelming let's go
outputs a little overwhelming let's go ahead and maybe limit it to just 10 as
ahead and maybe limit it to just 10 as before for discussion sake and now I see
before for discussion sake and now I see 10 comedies ordered arbitrarily from
10 comedies ordered arbitrarily from however they're in the database that
however they're in the database that happen to indeed be have comedy as their
happen to indeed be have comedy as their genre if I want to do this a little more
genre if I want to do this a little more cleanly I could do this let's see uh why
cleanly I could do this let's see uh why don't I order by title ascending order
don't I order by title ascending order which is alphabetically or the default
which is alphabetically or the default is also uh ascending limit 10 now I see
is also uh ascending limit 10 now I see the top 10 I mean weirdly named things
the top 10 I mean weirdly named things with hash symbols presumably to get
with hash symbols presumably to get their titles up to the beginning or
their titles up to the beginning or maybe these are hashtags uh here now we
maybe these are hashtags uh here now we have alphabetically the top first 10
have alphabetically the top first 10 shows that are
shows that are comedies any questions on these kinds of
comedies any questions on these kinds of queries it's kind of a lot but at the
queries it's kind of a lot but at the same time it's just like composing the
same time it's just like composing the smaller ideas from before into slightly
smaller ideas from before into slightly more useful
more useful queries
yeah do for Keys have to set the relationship when you create the table
relationship when you create the table the programmer or the database
the programmer or the database administrator would create that
administrator would create that relationship by using those keywords
relationship by using those keywords primary key and foreign key that create
primary key and foreign key that create that teaches the database what is
that teaches the database what is related to what per the picture so you
related to what per the picture so you do that once and now I being the sort of
do that once and now I being the sort of programmer who's familiar with the
programmer who's familiar with the database I am just using these foreign
database I am just using these foreign keys in a way in a manner consistent
keys in a way in a manner consistent with their design but and this is where
with their design but and this is where it's useful at some point even if you no
it's useful at some point even if you no one hands you a picture to make sure you
one hands you a picture to make sure you understand the database because that's
understand the database because that's going to inform literally what you type
going to inform literally what you type in SQL to get the data you care about
in SQL to get the data you care about about well let's do something a little
about well let's do something a little more precise how about very reasonable
more precise how about very reasonable question and honestly this is exactly
question and honestly this is exactly what imdb.com in the app are for what if
what imdb.com in the app are for what if you want to find all of the shows that
you want to find all of the shows that Steve Carell is in like kind of a
Steve Carell is in like kind of a reasonable query like literally
reasonable query like literally something someone might type into Google
something someone might type into Google or more specifically IMDb it's not
or more specifically IMDb it's not really obvious at first glance how to do
really obvious at first glance how to do that though because right like from my
that though because right like from my database if these are my six tables well
database if these are my six tables well I can pretty easily get Steve Carell
I can pretty easily get Steve Carell from here but I can really only get his
from here but I can really only get his ID number whatever that is his name
ID number whatever that is his name which I know already and his birth year
which I know already and his birth year okay interesting but has nothing to do
okay interesting but has nothing to do with the shows that he's in I can look
with the shows that he's in I can look at shows over here but there's no
at shows over here but there's no mention of Steve Carell right because
mention of Steve Carell right because there's no person ID here where is that
there's no person ID here where is that relationship implemented well it's
relationship implemented well it's implemented down here so how do we do
implemented down here so how do we do this well here's the perfect example of
this well here's the perfect example of a a lesson we've trying been trying to
a a lesson we've trying been trying to emphasize for weeks of taking these baby
emphasize for weeks of taking these baby steps like break larger problems down
steps like break larger problems down into smaller ones and let's do something
into smaller ones and let's do something like this let's just get everything I
like this let's just get everything I know about Steve Carell from the
know about Steve Carell from the database let's select star from people
database let's select star from people where the name of the person is quote
where the name of the person is quote unquote Steve Carell I just want to see
unquote Steve Carell I just want to see what data we've got and here's what we
what data we've got and here's what we have okay there's only one Steve Carell
have okay there's only one Steve Carell born in 1962 and his unique ID is 136
born in 1962 and his unique ID is 136 797 according to IMDb this isn't some
797 according to IMDb this isn't some like Global uh actor identifier per se
like Global uh actor identifier per se all right well how do I get now all of
all right well how do I get now all of the shows that Steve Carell is in well I
the shows that Steve Carell is in well I could do this
could do this select star from Stars not to confuse
select star from Stars not to confuse the two one's the symbol one's the table
the two one's the symbol one's the table name uh where person ID equals 13
name uh where person ID equals 13 6797 so I think this will now give me
6797 so I think this will now give me everything from the Stars table that
everything from the Stars table that relates to Steve Carell okay and you'll
relates to Steve Carell okay and you'll see person ID is the same because I'm
see person ID is the same because I'm literally searching for just Steve
literally searching for just Steve Carell but there are what like 20 or so
Carell but there are what like 20 or so shows that he's been in all right well
shows that he's been in all right well here's where things would get tedious
here's where things would get tedious what are those shows well I could do
what are those shows well I could do Select Title from shows where the ID of
Select Title from shows where the ID of the show equals and here's you know
the show equals and here's you know whenever you copy paste you're probably
whenever you copy paste you're probably doing something wrong okay he's was in
doing something wrong okay he's was in The Dana Carvey Show familiar with that
The Dana Carvey Show familiar with that let's do another one we'll copy paste
let's do another one we'll copy paste this uh where ID equals this over the
this uh where ID equals this over the top another and if we keep digging we'll
top another and if we keep digging we'll probably find the office but my God like
probably find the office but my God like that's going to take forever to do 20
that's going to take forever to do 20 queries manually it's not very Dynamic
queries manually it's not very Dynamic but what if we just Nest these queries a
but what if we just Nest these queries a little more dynamically so let me start
little more dynamically so let me start from the beginning again what if we go
from the beginning again what if we go ahead and select everything we want we
ahead and select everything we want we know about uh people whose name
know about uh people whose name equals
equals Steve Carell that gave us earlier this
Steve Carell that gave us earlier this data I don't need all of that data I
data I don't need all of that data I know his name I don't care about his
know his name I don't care about his birth year so let's change this to just
birth year so let's change this to just be give me the ID of Steve Carell and
be give me the ID of Steve Carell and that gives me back now this smaller
that gives me back now this smaller temporary data set all right can I now
temporary data set all right can I now use
use this uh inside of another query well let
this uh inside of another query well let me wrap the whole thing with parentheses
me wrap the whole thing with parentheses and now let me say select star from the
and now let me say select star from the Stars table where the person ID equals
Stars table where the person ID equals this so I'm Del liely not using in
this so I'm Del liely not using in because I'm assuming there's indeed only
because I'm assuming there's indeed only one Steve Carell in the world so I'm not
one Steve Carell in the world so I'm not getting back a list of Steve Carell I'm
getting back a list of Steve Carell I'm getting back the one and only in this
getting back the one and only in this case so equal is fine in is when you
case so equal is fine in is when you have multiple equal is when you have one
have multiple equal is when you have one let me go ahead and hit enter now okay
let me go ahead and hit enter now okay that's more data than I need I don't
that's more data than I need I don't need like 20 copies of Steve Carell's
need like 20 copies of Steve Carell's person ID so let me hit up let me go
person ID so let me hit up let me go back and let me just get show ID from
back and let me just get show ID from Steve Carell and now I have a list of
Steve Carell and now I have a list of just the 20 or so show IDs that he has
just the 20 or so show IDs that he has been in all right how can I now use this
been in all right how can I now use this well let me hit up let me put the whole
well let me hit up let me put the whole thing in parentheses and now let me
thing in parentheses and now let me select what I really want Select Title
select what I really want Select Title from shows where and here's the final
from shows where and here's the final flourish the shows table has an ID has a
flourish the shows table has an ID has a title has a year and has an episode and
title has a year and has an episode and what I really want though is to check
what I really want though is to check which
which shows have ID that is what anyone want
shows have ID that is what anyone want to finish the thought
to finish the thought I just want to
yeah exactly ID in this and this is getting ugly and when you actually write
getting ugly and when you actually write your queries in like a text file you can
your queries in like a text file you can format them nicely and indent them my
format them nicely and indent them my font is just getting I don't want to
font is just getting I don't want to make it too small to fit everything but
make it too small to fit everything but now we have three queries one is in
now we have three queries one is in doubly nested parenthesis then there's
doubly nested parenthesis then there's the middle one then there's the outer
the middle one then there's the outer one so this last query is going to get
one so this last query is going to get me the title from shows where the ID of
me the title from shows where the ID of the show is in this big list of 20 or so
the show is in this big list of 20 or so show IDs that Steve Carell is in and I
show IDs that Steve Carell is in and I knew that because I looked up his name
knew that because I looked up his name here and notice what I did not do this
here and notice what I did not do this time is I didn't manually hardcode his
time is I didn't manually hardcode his ID number there's no need that would be
ID number there's no need that would be kind of a bad way to implement a website
kind of a bad way to implement a website uh if you're using a database underneath
uh if you're using a database underneath the hood you want the IMDb for real to
the hood you want the IMDb for real to search for whatever the human typed in
search for whatever the human typed in and no one's going to know Steve
and no one's going to know Steve Carell's person ID or anything else so
Carell's person ID or anything else so here we've done this all dynamically and
here we've done this all dynamically and now if I hit enter I think I get all of
now if I hit enter I think I get all of his shows let's go ahead and order this
his shows let's go ahead and order this by title just to make it tidy and you
by title just to make it tidy and you probably will see at least one or more
probably will see at least one or more shows that you know and probably the
shows that you know and probably the most popular is dot dot dot the office
most popular is dot dot dot the office so this is literally the kind of query
so this is literally the kind of query that's being executed underneath the
that's being executed underneath the hood when you go to uh websites or apps
hood when you go to uh websites or apps like IMDb your textual query is probably
like IMDb your textual query is probably being plugged into a longer SQL query
being plugged into a longer SQL query like this where some programmer at IMDb
like this where some programmer at IMDb probably wrote this whole query in
probably wrote this whole query in advance weeks months years ago and
advance weeks months years ago and they're just somehow plugging in the
they're just somehow plugging in the value that you the human type into the
value that you the human type into the search box or the like questions
search box or the like questions now on finding this data or any
now on finding this data or any other no okay so where else could we go
other no okay so where else could we go with this well let's
with this well let's consider how else we might combine data
consider how else we might combine data suppose that the next question actually
suppose that the next question actually perhaps appropriately would be focusing
perhaps appropriately would be focusing in on not just like people and shows and
in on not just like people and shows and these stars but how do we kind of like
these stars but how do we kind of like gather more information about the shows
gather more information about the shows themselves like the genres the ratings
themselves like the genres the ratings or the like so indeed let's focus on
or the like so indeed let's focus on just these two tables here recall that
just these two tables here recall that every show has an ID a title a year and
every show has an ID a title a year and episodes but it also might have one or
episodes but it also might have one or more relationships with rose and this
more relationships with rose and this other table is called genres and this is
other table is called genres and this is so that a show can be a comedy can be a
so that a show can be a comedy can be a drama can be any number of other things
drama can be any number of other things one row per so you would see the same
one row per so you would see the same show ID again and again and again with a
show ID again and again and again with a different genre written in English like
different genre written in English like comma comedy drama or the like well how
comma comedy drama or the like well how do I kind of Recon subtitute that data
do I kind of Recon subtitute that data Well turns out there's a few different
Well turns out there's a few different ways to do this and let me propose that
ways to do this and let me propose that we introduce this keyword here join and
we introduce this keyword here join and this is really the most powerful of the
this is really the most powerful of the keywords in SQL itself it doesn't have
keywords in SQL itself it doesn't have to be used we've seen with uh nested
to be used we've seen with uh nested queries that you can still select data
queries that you can still select data across multiple tables but here is
across multiple tables but here is another way so let me do this let me go
another way so let me do this let me go back to my SQL light database and let me
back to my SQL light database and let me select sort of in one uh breath exactly
select sort of in one uh breath exactly the data I want select star from shows
the data I want select star from shows and let's just limit this initially to
and let's just limit this initially to 10 to see what it looks like all right
10 to see what it looks like all right that's again the shows data select star
that's again the shows data select star from genres let's limit that to 102 just
from genres let's limit that to 102 just to wrap our minds around it and now this
to wrap our minds around it and now this is not that useful however the data in
is not that useful however the data in the leftmost column here is the primary
the leftmost column here is the primary key in the shows table these are just
key in the shows table these are just unique IDs the data here in the genres
unique IDs the data here in the genres table recall show ID is the foreign key
table recall show ID is the foreign key so it's the same numbers but just copied
so it's the same numbers but just copied into another table so that we can have
into another table so that we can have this relationship across them how do I
this relationship across them how do I kind of line up these numbers with these
kind of line up these numbers with these numbers to get back like a wider table
numbers to get back like a wider table that has title and year and episodes and
that has title and year and episodes and genre and heck ratings and all of that
genre and heck ratings and all of that too if we want well you can join these
too if we want well you can join these Tables by just telling the database what
Tables by just telling the database what to join on what so let me do this select
to join on what so let me do this select star from shows join that table though
star from shows join that table though on the genres table well how do you want
on the genres table well how do you want to join those two tables and again the
to join those two tables and again the two tables from the picture looked like
two tables from the picture looked like this how do you tell SQL
this how do you tell SQL programmatically to sort of you know put
programmatically to sort of you know put one of them right next to the other line
one of them right next to the other line up all of the ID so that you just get
up all of the ID so that you just get one larger data set well we can use
one larger data set well we can use indeed this this query this uh syntax
indeed this this query this uh syntax called join so back to VSS code here and
called join so back to VSS code here and let me join these two
let me join these two tables sorry typo here join genres on
tables sorry typo here join genres on the shows tables ID column AKA it's
the shows tables ID column AKA it's primary key equaling the genres tables
primary key equaling the genres tables show ID column aka the foreign key so in
show ID column aka the foreign key so in other words it looks a little cryptic
other words it looks a little cryptic but I'm just telling SQL how to line up
but I'm just telling SQL how to line up these two tables and what column to
these two tables and what column to match with the other so that the numbers
match with the other so that the numbers line up and I get essentially a wider
line up and I get essentially a wider table let me go ahead and hit uh
table let me go ahead and hit uh semicolon and enter and this is now
semicolon and enter and this is now going to give me a lot of data we might
going to give me a lot of data we might have to interrupt it but notice even at
have to interrupt it but notice even at a glance we're getting the ID the title
a glance we're getting the ID the title the year the number of episodes the ID
the year the number of episodes the ID again redundantly but that's to be
again redundantly but that's to be expected if I'm joining them and the
expected if I'm joining them and the genre all the way on the right let me
genre all the way on the right let me hit contrl C to interrupt let me just
hit contrl C to interrupt let me just limit this to the office so where title
limit this to the office so where title equals quote unquote the office so we
equals quote unquote the office so we can focus on just one sample uh datum
can focus on just one sample uh datum and here fun fact there's been more than
and here fun fact there's been more than one office the one that you all probably
one office the one that you all probably like is this one that started in 2005
like is this one that started in 2005 with 188 episodes its ID in the shows
with 188 episodes its ID in the shows table is 3866 76 that's confirmed over
table is 3866 76 that's confirmed over here too so again we've just joined the
here too so again we've just joined the two tables how by lining up those fields
two tables how by lining up those fields but now that we can see that almost all
but now that we can see that almost all of the offices produced over the decades
of the offices produced over the decades are comedies except for this one there
are comedies except for this one there was a version of the office produced in
was a version of the office produced in 2001 that was considered more of uh a
2001 that was considered more of uh a drama no unsure if it's related to the
drama no unsure if it's related to the other how can we Link in other data well
other how can we Link in other data well let's go ahead and Link in ratings too
let's go ahead and Link in ratings too or instead so instead of joining this
or instead so instead of joining this with genres let me go ahead and Rewind
with genres let me go ahead and Rewind here and join shows on ratings on shows.
here and join shows on ratings on shows. ID equals ratings. show ID and let's
ID equals ratings. show ID and let's limit it to the office 2 for discussion
limit it to the office 2 for discussion sake where uh title equals quote unquote
sake where uh title equals quote unquote the office semicolon and now you can see
the office semicolon and now you can see that among the various offices it looks
that among the various offices it looks like the one that most of us probably
like the one that most of us probably know and love is the highest rated also
know and love is the highest rated also with a 9.0 with like 585,000 people
with a 9.0 with like 585,000 people having cast votes for whereas these
having cast votes for whereas these other shows seem to have been less
other shows seem to have been less popular perhaps that's why indeed you
popular perhaps that's why indeed you see fewer episodes for them as well so
see fewer episodes for them as well so even though we've put the data in
even though we've put the data in multiple places you can still kind of
multiple places you can still kind of reconstitute it by lining things up in
reconstitute it by lining things up in this way and rejoining the tables
this way and rejoining the tables questions now on
questions now on this like this is the heart of what SQL
this like this is the heart of what SQL does and what relational databases do
does and what relational databases do for
for you
you questions all right a few final features
questions all right a few final features like there's not all that much that uh
like there's not all that much that uh you know SQL takes practice like
you know SQL takes practice like anything else but in terms of syntax and
anything else but in terms of syntax and capabilities let's just introduce you to
capabilities let's just introduce you to a couple of final features here and how
a couple of final features here and how and problems that arise and how we might
and problems that arise and how we might solve them let's do this uh as well so
solve them let's do this uh as well so let me go back into VSS code here and
let me go back into VSS code here and let's just find out um Steve Carell's
let's just find out um Steve Carell's information again last time we did it
information again last time we did it with this nested nested query by getting
with this nested nested query by getting his ID and then the show IDs and then
his ID and then the show IDs and then the titles for those show IDs with join
the titles for those show IDs with join you can do it a little differently and
you can do it a little differently and any of these ways are fine one might
any of these ways are fine one might become EAS easier to you mentally than
become EAS easier to you mentally than another let's go ahead and select the
another let's go ahead and select the titles from what let's select the title
titles from what let's select the title from uh the people table but and I'm
from uh the people table but and I'm going to hit enter and when you're using
going to hit enter and when you're using SQL light 3 interactively if you ever
SQL light 3 interactively if you ever find yourself with a prompt that says
find yourself with a prompt that says dot dot dot angle bracket it means
dot dot dot angle bracket it means you're continuing your thought onto the
you're continuing your thought onto the next line if you didn't intend that you
next line if you didn't intend that you can sometimes hit uh semicolon to just
can sometimes hit uh semicolon to just end the thought and hit enter and just
end the thought and hit enter and just even if it triggers an error but this is
even if it triggers an error but this is one way of formatting my queries now a
one way of formatting my queries now a little more nicely I'm just going to add
little more nicely I'm just going to add some wh space so that it's a little
some wh space so that it's a little easier to read what do I want to select
easier to read what do I want to select well I want to select the title of shows
well I want to select the title of shows from the people table joined with the
from the people table joined with the Stars table on the people tables ID
Stars table on the people tables ID column equaling the Stars tables person
column equaling the Stars tables person ID column so in other words if you think
ID column so in other words if you think back to what people are and what stars
back to what people are and what stars are one has an ID one has a person ID
are one has an ID one has a person ID I'm just now connecting those two tables
I'm just now connecting those two tables I'm joining those two but I want to do
I'm joining those two but I want to do this as well with with another table let
this as well with with another table let me additionally join in so now I only
me additionally join in so now I only have two hands but now I'm putting a
have two hands but now I'm putting a third table joined in together here join
third table joined in together here join shows on stars. show ID equals shows. ID
shows on stars. show ID equals shows. ID so this is now linking three tables
so this is now linking three tables together but I only care about this for
together but I only care about this for one person so where the name of the
one person so where the name of the person equals quote unquote Steve Carell
person equals quote unquote Steve Carell so more cryptic to be to be sure but
so more cryptic to be to be sure but what we're doing with this query is just
what we're doing with this query is just taking all three tables that we care
taking all three tables that we care about and we're joining them them all
about and we're joining them them all together at once using this new join
together at once using this new join syntax literally telling the database
syntax literally telling the database what columns to line up with what and
what columns to line up with what and then we filter at the very end just like
then we filter at the very end just like before to get back if I hit enter the
before to get back if I hit enter the answer we want which in this case is a
answer we want which in this case is a little slower at the moment but that
little slower at the moment but that same list of uh 20 or so shows that he's
same list of uh 20 or so shows that he's been in there's one other way to do this
been in there's one other way to do this and again uh these are all in the slides
and again uh these are all in the slides online so you can repeat them without
online so you can repeat them without having to jot down everything and we'll
having to jot down everything and we'll put them in the notes too but there's
put them in the notes too but there's another way to do this I could also use
another way to do this I could also use an implicit join so that was an explicit
an implicit join so that was an explicit join because I literally typed the word
join because I literally typed the word join multiple times at that but let me
join multiple times at that but let me go ahead and select the title from these
go ahead and select the title from these three tables people stars and shows and
three tables people stars and shows and this might just be nicer because if you
this might just be nicer because if you know what tables you want to select data
know what tables you want to select data from just enumerate them separated by
from just enumerate them separated by commas which you might prefer in your
commas which you might prefer in your mind where the people ID equals the
mind where the people ID equals the Stars person
Stars person ID and the Stars show ID equals the
ID and the Stars show ID equals the shows ID and the name of the person
shows ID and the name of the person equals Steve Carell so this is an
equals Steve Carell so this is an implicit joint and honestly I constantly
implicit joint and honestly I constantly reference my notes for some of this
reference my notes for some of this stuff too it's not the kind of thing
stuff too it's not the kind of thing that's going to come like this to you
that's going to come like this to you after just one day but it's just a
after just one day but it's just a different way of expressing the same
different way of expressing the same thing I want to select data from three
thing I want to select data from three different tables and hey SQL here is how
different tables and hey SQL here is how I want you to line those tables up so
I want you to line those tables up so that I can get like related data for
that I can get like related data for Steve Carell and this now will achieve
Steve Carell and this now will achieve the same results ultimately let me hit
the same results ultimately let me hit enter
enter H and there we go so a little slower and
H and there we go so a little slower and performance might vary based on computer
performance might vary based on computer based on uh implementation of SQL but I
based on uh implementation of SQL but I think I still have the same answers now
think I still have the same answers now suppose as I'm often do and I had to
suppose as I'm often do and I had to look it up again last time suppose you
look it up again last time suppose you forget what uh how to spell Steve
forget what uh how to spell Steve Carell's name is it two RS two L's or
Carell's name is it two RS two L's or the like well I could also do something
the like well I could also do something like this select well let's just keep
like this select well let's just keep this simple select star from people
this simple select star from people where name equals I've been deliberately
where name equals I've been deliberately getting it right so as to not embarrass
getting it right so as to not embarrass myself that's the Steve Carell I keep
myself that's the Steve Carell I keep querying if you forget well you could
querying if you forget well you could try searching for just Steves but there
try searching for just Steves but there interestingly there's a bunch of Steves
interestingly there's a bunch of Steves we don't know when they were born uh but
we don't know when they were born uh but that's probably not the Steve Carell we
that's probably not the Steve Carell we want if we don't have his last name so I
want if we don't have his last name so I could alternatively do well it's Steve
could alternatively do well it's Steve and then it's starts with a C I think
and then it's starts with a C I think well it turns out there's another wild
well it turns out there's another wild card you can use in SQL we used the
card you can use in SQL we used the asterisk to select all of the columns
asterisk to select all of the columns you can in quotes use a percent sign to
you can in quotes use a percent sign to say see something so there's some zero
say see something so there's some zero or more characters after the letter c
or more characters after the letter c and now this doesn't work cuz now I
and now this doesn't work cuz now I would be literally looking for Steve
would be literally looking for Steve space c something but recall earlier I
space c something but recall earlier I mentioned that one other keyword which
mentioned that one other keyword which is for fuzzier matching so to speak
is for fuzzier matching so to speak where it's like not exactly what you're
where it's like not exactly what you're looking for but it's like what you're
looking for but it's like what you're looking for if you instead say where his
looking for if you instead say where his name is like Steve space c something now
name is like Steve space c something now we'll get back a whole bunch of Steves
we'll get back a whole bunch of Steves but I think now I could probably find
but I think now I could probably find the one I'm actually looking for if I
the one I'm actually looking for if I don't remember his name you can use
don't remember his name you can use multiple percent signs if you forget
multiple percent signs if you forget what his first name is you could reverse
what his first name is you could reverse the order but that too is a uh very
the order but that too is a uh very powerful sequel feature at that
powerful sequel feature at that questions on these queries here
questions on these queries here yeah
sorry what about it oh yeah sure so the query I used here
it oh yeah sure so the query I used here there's a lot of Steves whose last name
there's a lot of Steves whose last name starts with C oops too far uh the last
starts with C oops too far uh the last query I executed was this one here so
query I executed was this one here so where the name is like quote unquote
where the name is like quote unquote Steve C percent sign so that's just
Steve C percent sign so that's just another tool for your toolkit here but
another tool for your toolkit here but you'll you'll perhaps have noticed that
you'll you'll perhaps have noticed that those two the prior to that query the
those two the prior to that query the joins I did were sort of slow and
joins I did were sort of slow and honestly this database isn't even that
honestly this database isn't even that big like yes it has tens of thousands of
big like yes it has tens of thousands of rows in it but like in the real world
rows in it but like in the real world and most of the apps you and I use a lot
and most of the apps you and I use a lot every day or websites like there's
every day or websites like there's Millions even billions of rows of data
Millions even billions of rows of data and like if I had to wait on like my
and like if I had to wait on like my computer here or my code space like a
computer here or my code space like a second or two to get the data like
second or two to get the data like that's not going to work for millions of
that's not going to work for millions of users or customers certainly so how can
users or customers certainly so how can we actually improve things well it turns
we actually improve things well it turns out another upside of a proper
out another upside of a proper relational database is that it's not
relational database is that it's not just a spreadsheet where the onus is on
just a spreadsheet where the onus is on like you to like find the data you're
like you to like find the data you're looking for you can also tell the
looking for you can also tell the database to index the data for you an
database to index the data for you an index is an an efficient uh cheat sheet
index is an an efficient uh cheat sheet for Finding data fast like books in the
for Finding data fast like books in the real world often have indices at the end
real world often have indices at the end of the at the end of the book where you
of the at the end of the book where you can look things up alphabetically and
can look things up alphabetically and then you can cross reference it for the
then you can cross reference it for the pages that that topic appears on same
pages that that topic appears on same idea in a database if you tell the
idea in a database if you tell the database in advance that you want to
database in advance that you want to search on a certain column frequently
search on a certain column frequently you can tell it to build a fancy index
you can tell it to build a fancy index that will just allow you to search that
that will just allow you to search that column Faster by default these columns
column Faster by default these columns are going to be searched most likely via
are going to be searched most likely via linear search like not even binary
linear search like not even binary search because the data might not be
search because the data might not be sorted because it came in in any order
sorted because it came in in any order but if you create an index you're
but if you create an index you're probably going to get something closer
probably going to get something closer to logar rmic than linear and that's
to logar rmic than linear and that's going to be a big plus overall so let me
going to be a big plus overall so let me do something simple here first let me
do something simple here first let me turn on a SQL light specific feature
turn on a SQL light specific feature that just is going to time all of my
that just is going to time all of my queries by writing do timer on I just
queries by writing do timer on I just want to keep track of uh how long each
want to keep track of uh how long each of these commands takes this one is not
of these commands takes this one is not a slow command so this is just going to
a slow command so this is just going to be relative but let's just select
be relative but let's just select everything from the shows table where
everything from the shows table where the title thereof is the office let's
the title thereof is the office let's see how long this relatively simple
see how long this relatively simple query takes all right not very long at
query takes all right not very long at all in real terms like less than a
all in real terms like less than a second .035 seconds so not slow by any
second .035 seconds so not slow by any means but if you've got hundreds
means but if you've got hundreds thousands millions of users like every
thousands millions of users like every one of those milliseconds could very
one of those milliseconds could very well add up so can we do better well we
well add up so can we do better well we can if I do this if I use syntax like
can if I do this if I use syntax like this once in the beginning of the design
this once in the beginning of the design of my database I create not a table but
of my database I create not a table but an index with some name on a specific
an index with some name on a specific table on one or more columns I can give
table on one or more columns I can give a clue a hint to the database in advance
a clue a hint to the database in advance saying please optimize with some Secret
saying please optimize with some Secret Sauce searching or selecting on this
Sauce searching or selecting on this column in this table so that my searches
column in this table so that my searches are faster so let me do this let me go
are faster so let me do this let me go back to vs code here let me create an
back to vs code here let me create an index called how about title index I
index called how about title index I could call it anything I want but I want
could call it anything I want but I want to search faster on titles so I'm going
to search faster on titles so I'm going to call this a title index where uh
to call this a title index where uh rather uh title index on the table
rather uh title index on the table called shows and then in parentheses is
called shows and then in parentheses is the syntax the column called title so
the syntax the column called title so again I've just borrowed this canonical
again I've just borrowed this canonical syntax and I've just translated into
syntax and I've just translated into something that's TV show specific all
something that's TV show specific all right what is this going to do for me
right what is this going to do for me once I hit enter this is going to create
once I hit enter this is going to create in the computer's memory the database's
in the computer's memory the database's memory something called a b tree it's
memory something called a b tree it's not a binary tree a b tree is actually a
not a binary tree a b tree is actually a potentially more efficient data
potentially more efficient data structure that we didn't talk about a
structure that we didn't talk about a few weeks back in week five but it looks
few weeks back in week five but it looks a little something like this which looks
a little something like this which looks similar to a binary tree but does anyone
similar to a binary tree but does anyone notice what makes this not a binary tree
exactly binary tree by implying two has no more than two children per node but
no more than two children per node but here's a perfect example one two three
here's a perfect example one two three and there could be four children five
and there could be four children five children or more but the effect of that
children or more but the effect of that if you have a very wide tree the upside
if you have a very wide tree the upside is that it's like very short like it
is that it's like very short like it pulls the data higher up closer to the
pulls the data higher up closer to the node to the root node and recall that
node to the root node and recall that the root node is where we began our
the root node is where we began our searches in the past whether it was a
searches in the past whether it was a BST a binary search tree even a try or
BST a binary search tree even a try or data structures we always began at the
data structures we always began at the top so the higher up you can pull the
top so the higher up you can pull the data even if it makes the data structure
data even if it makes the data structure very wide you're going to be able to do
very wide you're going to be able to do boom boom boom look up queries look up
boom boom boom look up queries look up data probably much faster certainly than
data probably much faster certainly than if it's just a very long list like a
if it's just a very long list like a column by default so with that said let
column by default so with that said let me go back to vs code I didn't create
me go back to vs code I didn't create the index yet let me go ahead and hit
the index yet let me go ahead and hit enter and create it all right it took a
enter and create it all right it took a minute a moment it took like half a
minute a moment it took like half a second which obviously is not that slow
second which obviously is not that slow but with more data that could have been
but with more data that could have been even slower but it's a onetime operation
even slower but it's a onetime operation as of now and now let me hit up and let
as of now and now let me hit up and let me select the same data from shows where
me select the same data from shows where title equals the office last time just a
title equals the office last time just a moment ago it took 0.035 seconds no not
moment ago it took 0.035 seconds no not slow but also that's going to add up if
slow but also that's going to add up if I have lots of users of IMDb let's go
I have lots of users of IMDb let's go ahead now and execute the same query
ahead now and execute the same query again how long did that
again how long did that take
take 0.01 seconds now I mean practically
0.01 seconds now I mean practically nothing and so that's the sort of
nothing and so that's the sort of opportunity now when you've got lots of
opportunity now when you've got lots of data and you want to really speed up
data and you want to really speed up these searches these indexes these
these searches these indexes these indices that just create for you these
indices that just create for you these magical data structures in the databases
magical data structures in the databases memory it allows you to search on
memory it allows you to search on columns that you are pretty sure you
columns that you are pretty sure you want to search on more effectively Now
want to search on more effectively Now by contrast if you've ever used like
by contrast if you've ever used like Google or Bing or some search engine
Google or Bing or some search engine that has advanced search some of those
that has advanced search some of those text boxes that you can search more
text boxes that you can search more precisely in might very well be slower
precisely in might very well be slower why well probably you don't want to go
why well probably you don't want to go crazy and just index every column on
crazy and just index every column on every table why well what might be the
intuition like if logically indexes speed things up why not index everything
speed things up why not index everything there's always going to be a tradeoff
there's always going to be a tradeoff here what might that
here what might that be
be yeah yeah it's going to take a lot of
yeah yeah it's going to take a lot of storage right this is just a slide on
storage right this is just a slide on the screen but like this has to go
the screen but like this has to go somewhere like this needs space in the
somewhere like this needs space in the computer's memory or on the hard drive
computer's memory or on the hard drive or the like and that's fine if you have
or the like and that's fine if you have unlimited space but odds are like you
unlimited space but odds are like you don't and that's going to get expensive
don't and that's going to get expensive for different reasons so maybe you only
for different reasons so maybe you only want to index certain columns and
want to index certain columns and certain tables and not all of them
certain tables and not all of them because you know what even if a user
because you know what even if a user really wants to search maybe VI advanced
really wants to search maybe VI advanced search on some other column or table
search on some other column or table altogether fine if once in a while a
altogether fine if once in a while a query is slow like we're probably
query is slow like we're probably getting the bigger bang for our buck by
getting the bigger bang for our buck by optimizing the common cases the more
optimizing the common cases the more popular queries that people actually
popular queries that people actually care about
care about too all right so let's come full circle
too all right so let's come full circle and bring this now back to uh how how we
and bring this now back to uh how how we actually began which was with some uh
actually began which was with some uh python code so it turns out these are
python code so it turns out these are not either or decisions it turns out in
not either or decisions it turns out in the real world developers are constantly
the real world developers are constantly using one two three languages at once in
using one two three languages at once in fact next week I rattled off HTML CSS
fact next week I rattled off HTML CSS and JavaScript one of which is a proper
and JavaScript one of which is a proper programming language but those languages
programming language but those languages are often used together totally normal
are often used together totally normal and common to use Python and SQL or Java
and common to use Python and SQL or Java in SQL or Swift in SQL or any number of
in SQL or Swift in SQL or any number of different combinations with a database
different combinations with a database language you might use use your
language you might use use your preferred programming language Java
preferred programming language Java python C++ to create the user interface
python C++ to create the user interface and the logic that builds the uh the
and the logic that builds the uh the that implements the program itself but
that implements the program itself but for your data like SQL is a really good
for your data like SQL is a really good candidates and indeed we've seen already
candidates and indeed we've seen already that SQL can just speed up certain
that SQL can just speed up certain operations you can change you can uh
operations you can change you can uh collapse 15 lines of code into just one
collapse 15 lines of code into just one and you can use these things together so
and you can use these things together so let me come back to I'm going to quit
let me come back to I'm going to quit out of SQL light I'm going to minimize
out of SQL light I'm going to minimize my terminal window and here's where we
my terminal window and here's where we left off before with favorite .p with
left off before with favorite .p with favorites. everything was being stored
favorites. everything was being stored in uh favorites. CSV and recall that we
in uh favorites. CSV and recall that we eventually imported that CSV file into
eventually imported that CSV file into favorites. DB automatically with import
favorites. DB automatically with import just so we could start playing around
just so we could start playing around with SQL but we can now tie these two
with SQL but we can now tie these two together and a way to do that is as
together and a way to do that is as follows um cs50 has a library for python
follows um cs50 has a library for python you might recall having available uh get
you might recall having available uh get string get int get float you don't
string get int get float you don't strictly need to use them in Python
strictly need to use them in Python because it's much easier to just use the
because it's much easier to just use the input function and then try accept and
input function and then try accept and convert things to int or float or the
convert things to int or float or the like but it's a lot more work to use SQL
like but it's a lot more work to use SQL in Python without a third-party Library
in Python without a third-party Library a lot of the commercial options or
a lot of the commercial options or popular open source options are actually
popular open source options are actually just complicated to use so cs50 does
just complicated to use so cs50 does have a very useful function inside of
have a very useful function inside of its library for python that you should
its library for python that you should use and must use for the problem set
use and must use for the problem set that just makes it easy to execute
that just makes it easy to execute python uh execute SQL inside of your
python uh execute SQL inside of your python code but it's built on top of a
python code but it's built on top of a very popular open source alternative so
very popular open source alternative so you can use that too in the real world
you can use that too in the real world so the documentation for that at this
so the documentation for that at this URL here but I'll show you what we need
URL here but I'll show you what we need to know here by focusing back on
to know here by focusing back on favorites.i so what I'm going to do here
favorites.i so what I'm going to do here as follows is this let me delete
as follows is this let me delete everything from favorites. py except for
everything from favorites. py except for let's say uh this from cs50 import SQL
let's say uh this from cs50 import SQL in all caps so that's importing a SQL
in all caps so that's importing a SQL feature from cs50's library that's going
feature from cs50's library that's going to allow me to open a DB file in code
to allow me to open a DB file in code how do I do that well let me create a
how do I do that well let me create a variable called DB for database though I
variable called DB for database though I could call it anything I want let me
could call it anything I want let me call this SQL function and pass in using
call this SQL function and pass in using special syntax that's not cs50 specific
special syntax that's not cs50 specific it's an industry thing SQL light colon
it's an industry thing SQL light colon slash slash slash unlike every other URL
slash slash slash unlike every other URL you type this one literally has three in
you type this one literally has three in this context here and then the name of
this context here and then the name of the database which in this case is
the database which in this case is favorites. DB so this is just a way of
favorites. DB so this is just a way of telling this SQL library that we wrote
telling this SQL library that we wrote but that works exactly like third party
but that works exactly like third party Alternatives openen favorites. DB using
Alternatives openen favorites. DB using the SQL light technology if you will all
the SQL light technology if you will all right let's just ask the user a question
right let's just ask the user a question give me your favorite um uh problem so
give me your favorite um uh problem so we're going to use input instead of get
we're going to use input instead of get string but we could use get string but
string but we could use get string but they're pretty much the same for our
they're pretty much the same for our purposes let's ask the user for their
purposes let's ask the user for their favorite and now in Python code let us
favorite and now in Python code let us select from favorites. DB all of the
select from favorites. DB all of the rows where students specify that problem
rows where students specify that problem as their favorite so in SQL alone it
as their favorite so in SQL alone it would be this select uh star from
would be this select uh star from favorites where problem equals and I'll
favorites where problem equals and I'll do um well whatever my favorite's going
do um well whatever my favorite's going to be like problem equals Mario for
to be like problem equals Mario for instance so if I were just using SQL I
instance so if I were just using SQL I would literally write something like
would literally write something like that but I'm in a piy file now like I
that but I'm in a piy file now like I have to use Python syntax but python
have to use Python syntax but python supports strings SQL is just text it's
supports strings SQL is just text it's just a string so I could certainly just
just a string so I could certainly just put my SQL code in a string perhaps and
put my SQL code in a string perhaps and then pass it to a python function and
then pass it to a python function and here's the bridge between the two if you
here's the bridge between the two if you just treat SQL as any old text we can
just treat SQL as any old text we can put it in a string and execute it so let
put it in a string and execute it so let me actually do this let me go ahead and
me actually do this let me go ahead and create a variable called rows which is
create a variable called rows which is eventually going to contain all the rows
eventually going to contain all the rows from the database let me go ahead and uh
from the database let me go ahead and uh select db. execute this is the one
select db. execute this is the one function you need to know about inside
function you need to know about inside of cs50's library and it literally
of cs50's library and it literally executes a SQL statement and then in
executes a SQL statement and then in quotes you pass it literally what you
quotes you pass it literally what you want to execute and let me go ahead and
want to execute and let me go ahead and close the parenthesis at the end there
close the parenthesis at the end there and now let me just try this so for Row
and now let me just try this so for Row in rows let's iterate over all of the
in rows let's iterate over all of the rows let me go ahead and print out how
rows let me go ahead and print out how about uh row quote unquote and what do I
about uh row quote unquote and what do I want here uh let's print out the Tim
want here uh let's print out the Tim stamp of that person for kicks all right
stamp of that person for kicks all right let me open open my terminal window
let me open open my terminal window python of favorites. Pi crossing my
python of favorites. Pi crossing my fingers here for
fingers here for sure
sure enter uh uh there we go favorit so I'll
enter uh uh there we go favorit so I'll type in
type in Mario okay so I got back it's not very
Mario okay so I got back it's not very interesting but I got back all of the
interesting but I got back all of the timestamps of students who typed in
timestamps of students who typed in Mario that we imported into this
Mario that we imported into this database well what I really care about
database well what I really care about is how popular Mario is so let me change
is how popular Mario is so let me change this a little bit let me change this to
this a little bit let me change this to count the number of rows and let me keep
count the number of rows and let me keep it simple let me give an alias like I
it simple let me give an alias like I proposed earlier like as n where N is a
proposed earlier like as n where N is a number so that now down here I can
number so that now down here I can actually just do this print out the
actually just do this print out the value of n all right let me go back to
value of n all right let me go back to my terminal window run python to
my terminal window run python to favorites. py let me type in Mario enter
favorites. py let me type in Mario enter okay 39 now technically I'm cheating
okay 39 now technically I'm cheating like honestly if I'm executing select
like honestly if I'm executing select count we've seen before it only ever
count we've seen before it only ever returns one row not multiple so there's
returns one row not multiple so there's really nothing to iterate over but it's
really nothing to iterate over but it's working fine it's just just iterating
working fine it's just just iterating once but I'm getting lucky so
once but I'm getting lucky so technically what I should probably just
technically what I should probably just do is this I should probably give myself
do is this I should probably give myself like a variable called row set it equal
like a variable called row set it equal to the very first row and only row that
to the very first row and only row that came back and now print out that rows n
came back and now print out that rows n column let me rerun the program I'll
column let me rerun the program I'll type in Mario again enter and I still
type in Mario again enter and I still see
see 39 so this of course I don't strictly
39 so this of course I don't strictly need to do this I don't really need a
need to do this I don't really need a variable I can do Row Bracket Z instead
variable I can do Row Bracket Z instead but let me focus on what this library is
but let me focus on what this library is now doing so per the documentation what
now doing so per the documentation what the C what the cs-50 execute function
the C what the cs-50 execute function always does for you is it returns a list
always does for you is it returns a list of dictionaries so if your query returns
of dictionaries so if your query returns nothing like no matches you get back an
nothing like no matches you get back an empty list like Open Bracket close
empty list like Open Bracket close bracket nothing in it any Loop is not
bracket nothing in it any Loop is not going to execute anything useful because
going to execute anything useful because there's nothing in it if though you get
there's nothing in it if though you get back one row you're going to get back a
back one row you're going to get back a list of size one in inside of which is a
list of size one in inside of which is a single dictionary that dictionary is
single dictionary that dictionary is going to have keys that correspond to
going to have keys that correspond to whatever you selected be it the columns
whatever you selected be it the columns or the count so when I selected star
or the count so when I selected star before I could have like I would have
before I could have like I would have gotten all of the columns that's how I
gotten all of the columns that's how I was able to access timestamp here I'm
was able to access timestamp here I'm just selecting count and I don't want to
just selecting count and I don't want to have to type this down here that would
have to type this down here that would just look kind of atrocious it would
just look kind of atrocious it would work but it would look weird to just
work but it would look weird to just keep retyping count peren star close pen
keep retyping count peren star close pen so I just created an alias called n just
so I just created an alias called n just to like make this my life easier or
to like make this my life easier or cleaner down here so to be clear the SQL
cleaner down here so to be clear the SQL the cs50 execute function returns a list
the cs50 execute function returns a list of dictionaries when you're using select
of dictionaries when you're using select and that is how I can now get back the
and that is how I can now get back the first and only row and then print out
first and only row and then print out that Row's n value it is identical to
that Row's n value it is identical to let me do this let me highlight this
let me do this let me highlight this whole line of text let me in my terminal
whole line of text let me in my terminal window run SQL light three of favorites.
window run SQL light three of favorites. DB like we did before break let me just
DB like we did before break let me just copy paste this query enter that that's
copy paste this query enter that that's the table I got back earlier when we
the table I got back earlier when we played with SQL manually and so when I
played with SQL manually and so when I get back this table here's the key
get back this table here's the key here's the value and I only have one row
here's the value and I only have one row which is why I'm just blindly indexing
which is why I'm just blindly indexing into rows bracket zero because I know
into rows bracket zero because I know there's always going to be an answer
there's always going to be an answer there it's going to be zero or one or
there it's going to be zero or one or more but I know now it's going to be
more but I know now it's going to be called n because of this here so what
called n because of this here so what have I just done well this is SQL down
have I just done well this is SQL down here and this is just me being like a
here and this is just me being like a data scientist asking questions about my
data scientist asking questions about my data just using like black and white SQL
data just using like black and white SQL queries this is me now being a python
queries this is me now being a python programmer who wants to talk to a SQL
programmer who wants to talk to a SQL database using Python and The Bridge
database using Python and The Bridge we're using happens to be the cs-50
we're using happens to be the cs-50 library but again there's third party
library but again there's third party free libraries you can also use as well
free libraries you can also use as well ours is just very simple and indeed the
ours is just very simple and indeed the documentation will explain how execute
documentation will explain how execute behaves a little differently for inserts
behaves a little differently for inserts updates and deletes you don't get back a
updates and deletes you don't get back a list because you're not selecting
list because you're not selecting anything but you do get back some return
anything but you do get back some return values questions on
values questions on this that's the last of our python code
this that's the last of our python code that ties everything together in
that ties everything together in spirit
yeah uh what this this one here yes so db. execute by definition
here yes so db. execute by definition returns a list of rows and each of those
returns a list of rows and each of those rows happens to be a dictionary because
rows happens to be a dictionary because it's convenient key value pairs if I'm
it's convenient key value pairs if I'm selecting the count of rows I just know
selecting the count of rows I just know from Having learned squel an hour ago
from Having learned squel an hour ago that this is always going to give me a
that this is always going to give me a single row whose column in this case is
single row whose column in this case is called n so if I know it's a single row
called n so if I know it's a single row I can just blindly just like in C go
I can just blindly just like in C go into that list or an array in C and go
into that list or an array in C and go to the first location and then treat
to the first location and then treat that as the single row what you don't
that as the single row what you don't want to do is this even if you the human
want to do is this even if you the human know the query returns one row you can't
know the query returns one row you can't just magically change the variable name
just magically change the variable name to be singular and expect to have only
to be singular and expect to have only one value you will always have a list so
one value you will always have a list so even if there is only one value in it
even if there is only one value in it it's up to you to do something like this
it's up to you to do something like this to get at it or if you prefer more
to get at it or if you prefer more succinctness you can do rose bracket I
succinctness you can do rose bracket I bracket n that'll achieve the same thing
bracket n that'll achieve the same thing without a variable
yeah good so I have been misleading this whole time and cheating because this is
whole time and cheating because this is only ever going to return Mario I'm
only ever going to return Mario I'm ignoring the favorite that the human
ignoring the favorite that the human typed in here on line five so let me fix
typed in here on line five so let me fix that and that's going to lead us to some
that and that's going to lead us to some of the problems that arise ultimately
of the problems that arise ultimately with SQL the right way to solve that
with SQL the right way to solve that problem let me get rid of my terminal
problem let me get rid of my terminal window here the right way to solve this
window here the right way to solve this problem is not to use an F string like
problem is not to use an F string like we did in Python generally because SQL
we did in Python generally because SQL queries as we'll see in a moment can be
queries as we'll see in a moment can be dangerous when you want to plug in users
dangerous when you want to plug in users uh data into a query that you've written
uh data into a query that you've written most of in advance you should you must
most of in advance you should you must you had better use a placeholder namely
you had better use a placeholder namely a question mark in this case this is
a question mark in this case this is somewhat specific to cs50's library but
somewhat specific to cs50's library but we just borrowed the convention that
we just borrowed the convention that like every other Library uses too in the
like every other Library uses too in the world of SQL single question marks are
world of SQL single question marks are used as placeholders and the way you do
used as placeholders and the way you do this is as follows if you want to plug
this is as follows if you want to plug in a value for that question mark just
in a value for that question mark just like in print F in C you specify as a
like in print F in C you specify as a second or a third or fourth argument all
second or a third or fourth argument all of the values you want plugged into this
of the values you want plugged into this so in C weeks ago we were using percent
so in C weeks ago we were using percent s same exact idea in SQL it's a question
s same exact idea in SQL it's a question mark that you use instead this now if I
mark that you use instead this now if I open back my terminal window and I run
open back my terminal window and I run uh python of favorites. type in Mario I
uh python of favorites. type in Mario I should still get 39 but now I can also
should still get 39 but now I can also type in scratch perhaps and get 44 for
type in scratch perhaps and get 44 for that very first piece at zero and that
that very first piece at zero and that one is even more popular here so this
one is even more popular here so this now is correct it would work to use an F
now is correct it would work to use an F string here and then plug in a value
string here and then plug in a value like favorite here but you'll see in
like favorite here but you'll see in just a moment don't do that you will
just a moment don't do that you will expose yourself to potential hack or
expose yourself to potential hack or attacks um by trusting the user's input
attacks um by trusting the user's input and so in fact let's transition from
and so in fact let's transition from that to exact some of these kinds of
that to exact some of these kinds of challenges namely two before we wrap up
challenges namely two before we wrap up so in the world of SQL especially when
so in the world of SQL especially when it's used at scale at the Twitter and
it's used at scale at the Twitter and the Googles of the world like a lot of
the Googles of the world like a lot of lots of data is probably coming into the
lots of data is probably coming into the database all at once because multiple
database all at once because multiple people are opening their phones at the
people are opening their phones at the same time around the world they're
same time around the world they're clicking on the same links roughly at
clicking on the same links roughly at the same time around the world when you
the same time around the world when you have thousands of people all using your
have thousands of people all using your site at once like order of operations is
site at once like order of operations is going to be important but unfortunately
going to be important but unfortunately in SQL and in other contexts of
in SQL and in other contexts of computing there's this risk of what's
computing there's this risk of what's known as a race condition so for
known as a race condition so for instance has anyone ever seen or liked
instance has anyone ever seen or liked this this is like yes the world record
this this is like yes the world record egg or it's like this thing that was
egg or it's like this thing that was very popular while back it's still kind
very popular while back it's still kind of going strong but if you go to the
of going strong but if you go to the Instagram profile for world record egg
Instagram profile for world record egg uh the goal was to make the most most
uh the goal was to make the most most liked Instagram post ever and they did
liked Instagram post ever and they did pretty well it's just this it's just a
pretty well it's just this it's just a picture of an egg now at the height of
picture of an egg now at the height of the popularity like there might have
the popularity like there might have been hundreds thousands tens of
been hundreds thousands tens of thousands of people clicking pretty much
thousands of people clicking pretty much at the same time on this egg so it
at the same time on this egg so it actually creates a potential problem
actually creates a potential problem with the Integrity of Instagram's data
with the Integrity of Instagram's data why well if you're have all these
why well if you're have all these requests coming in at once how do you
requests coming in at once how do you possibly keep track of all of them and
possibly keep track of all of them and update your counter in a way that can
update your counter in a way that can keep up with all of that traffic why
keep up with all of that traffic why well let's just hypothesize what meta
well let's just hypothesize what meta formerly Facebook was doing underneath
formerly Facebook was doing underneath the hood with Instagram if this were
the hood with Instagram if this were their code so suppose for the sake of
their code so suppose for the sake of discussion that Instagram servers are
discussion that Instagram servers are using a mix of python and SQL probably
using a mix of python and SQL probably not using the cs50 library but they
not using the cs50 library but they could absolutely be using those two
could absolutely be using those two languages or two others together um
languages or two others together um suppose they do this in order to update
suppose they do this in order to update the number of likes for that post they
the number of likes for that post they first execute a SQL query like select
first execute a SQL query like select the current number of likes from a table
the current number of likes from a table called posts where the ID of the post
called posts where the ID of the post equals whatever the unique identifier is
equals whatever the unique identifier is for that spec specific egg in the table
for that spec specific egg in the table and then they store the result in this
and then they store the result in this row variable just like I did and then
row variable just like I did and then they do this they grab uh they create a
they do this they grab uh they create a variable called likes they set it equal
variable called likes they set it equal to rows bracket Z so the very first row
to rows bracket Z so the very first row in the result set and they get the likes
in the result set and they get the likes key so this is literally what I just did
key so this is literally what I just did with the count let me hypothesize that
with the count let me hypothesize that Instagram does something similar with
Instagram does something similar with the total number of likes why are they
the total number of likes why are they doing this because they then want to
doing this because they then want to execute a third line of code that
execute a third line of code that executes update the posts table set the
executes update the posts table set the new number of likes equal to something
new number of likes equal to something where the ID of the post equals this
where the ID of the post equals this other thing now notice just like in
other thing now notice just like in printf there's the comma separated list
printf there's the comma separated list of values they want to update the
of values they want to update the current number of likes from the current
current number of likes from the current value to the current value plus one so
value to the current value plus one so it's likes plus one and then we plug in
it's likes plus one and then we plug in the ID for this so suppose this is what
the ID for this so suppose this is what Instagram's doing unfortunately whenever
Instagram's doing unfortunately whenever you execute multiple lines of code
you execute multiple lines of code independently and you're so popular like
independently and you're so popular like Instagram that you have thousands
Instagram that you have thousands hundreds of thousands of servers
hundreds of thousands of servers potentially it is quite possible that if
potentially it is quite possible that if you and I and everyone else in the room
you and I and everyone else in the room clicks that egg at the same time it's
clicks that egg at the same time it's not going to be the case statistically
not going to be the case statistically that like three lines of code are
that like three lines of code are executed for me and then three lines for
executed for me and then three lines for you and then three lines for you they're
you and then three lines for you they're probably going to get interspersed like
probably going to get interspersed like this gets executed for me and then this
this gets executed for me and then this gets executed for you and then they get
gets executed for you and then they get back to doing work for me and so forth
back to doing work for me and so forth just to kind of multitask just like a
just to kind of multitask just like a human might but at a super speed here
human might but at a super speed here the problem though is if these lines of
the problem though is if these lines of code get interrupted what could go wrong
code get interrupted what could go wrong well suppose that Carter and I both
well suppose that Carter and I both click the egg at the same time and
click the egg at the same time and suppose the current number of likes back
suppose the current number of likes back in the day is 100 that stores in this
in the day is 100 that stores in this variable the value 100 but if we click
variable the value 100 but if we click so close in time we might get back the
so close in time we might get back the same answer to this select query as of
same answer to this select query as of that moment in time when David and
that moment in time when David and Carter clicked it had a 100 likes but
Carter clicked it had a 100 likes but then this last line of code is executed
then this last line of code is executed for me and then maybe Carter because
for me and then maybe Carter because that answer the state of the database
that answer the state of the database was stored in this variable then both
was stored in this variable then both Carter and I will result in this line of
Carter and I will result in this line of code being executed with the same value
code being executed with the same value update the post table setting the likes
update the post table setting the likes equal to
equal to 101 for that post's ID why because again
101 for that post's ID why because again if each of these lines of code running
if each of these lines of code running on different servers are checking the
on different servers are checking the value of the current number of likes but
value of the current number of likes but then getting interrupted because Carter
then getting interrupted because Carter clicked the darn thing too and then
clicked the darn thing too and then resuming their work on my behalf we
resuming their work on my behalf we might have a race condition where the
might have a race condition where the code is sort of racing to finish but
code is sort of racing to finish but getting interrupted by other users
getting interrupted by other users clicks and the problem with that is that
clicks and the problem with that is that if you are inspecting the value of some
if you are inspecting the value of some variable or in this case a database cell
variable or in this case a database cell and making a decision based on it like
and making a decision based on it like how to update it you might now lose data
how to update it you might now lose data and Instagram probably not good for
and Instagram probably not good for advertising if they're losing likes and
advertising if they're losing likes and so that's probably a problem not to
so that's probably a problem not to retain the value 102 and instead insert
retain the value 102 and instead insert the number 101 twice it's actually
the number 101 twice it's actually similar in spirit to a story that uh was
similar in spirit to a story that uh was told in a databases course I took myself
told in a databases course I took myself years ago
years ago whereby uh it's somewhat analogous to
whereby uh it's somewhat analogous to kind of a contrived scenario involving
kind of a contrived scenario involving like a refrigerator and this is the
like a refrigerator and this is the closest thing to a refrigerator we could
closest thing to a refrigerator we could get on stage but imagine you've got like
get on stage but imagine you've got like one of these little dorm fridges in your
one of these little dorm fridges in your dorm 2 and your roommate and maybe both
dorm 2 and your roommate and maybe both of you as the story was told to me
of you as the story was told to me really like milk and one of you is at
really like milk and one of you is at class but the other of you comes home
class but the other of you comes home and you open your dorm fridge and you're
and you open your dorm fridge and you're like oh darn it we're out of milk and so
like oh darn it we're out of milk and so you close the fridge you walk across the
you close the fridge you walk across the street to CVS or some other store and
street to CVS or some other store and you get in line to buy some milk
you get in line to buy some milk Meanwhile your roommate gets out of
Meanwhile your roommate gets out of class they come back to your dorm room
class they come back to your dorm room they're really thirsty for some milk
they're really thirsty for some milk they open up the fridge they say oh
they open up the fridge they say oh we're out of milk and then they take a
we're out of milk and then they take a different route perhaps to CVS or some
different route perhaps to CVS or some other store nearby get in line to buy
other store nearby get in line to buy some milk fast forward some amount of
some milk fast forward some amount of time in this very contrived story and
time in this very contrived story and what happens oh damn it we now ended up
what happens oh damn it we now ended up with two gallons of milk and there's no
with two gallons of milk and there's no way we can fit gallons of milk in there
way we can fit gallons of milk in there let alone two of them so that's a
let alone two of them so that's a problem but what's the relationship to
problem but what's the relationship to this here well both of us yeah did
what exactly
exactly so to summarize both of us had a very similar thought process made a
very similar thought process made a similar decision based on the same
similar decision based on the same information not realizing that the
information not realizing that the information the fridge was in the
information the fridge was in the process of being updated and of course
process of being updated and of course in the Instagram World happens like this
in the Instagram World happens like this in the fridge World it might take a few
in the fridge World it might take a few minutes but the problem is ultimately
minutes but the problem is ultimately the result of our having made a decision
the result of our having made a decision about the state of the world and the
about the state of the world and the state of the world was in the middle of
state of the world was in the middle of being updated the queries got mingled
being updated the queries got mingled with others or in this case someone was
with others or in this case someone was already on their way to the store so
already on their way to the store so what's the solution in the real world
what's the solution in the real world well you could you know very simply like
well you could you know very simply like take a Post-It note and put like gone
take a Post-It note and put like gone for milk so as to communicate to your
for milk so as to communicate to your roommate that they should not inspect
roommate that they should not inspect the value of that variable and make a
the value of that variable and make a decision on it why because it's not yet
decision on it why because it's not yet consistent with the outcome that's about
consistent with the outcome that's about to happen you could be more traumatic
to happen you could be more traumatic and you could actually lock the fridge
and you could actually lock the fridge somehow put a pad lock around it or the
somehow put a pad lock around it or the like so they can't even get in there and
like so they can't even get in there and that would achieve the result uh the
that would achieve the result uh the same effect too and that is actually
same effect too and that is actually pretty much the solution to this problem
pretty much the solution to this problem in code too it's not safe it's not
in code too it's not safe it's not sufficient to only execute three lines
sufficient to only execute three lines of code like this rather what you
of code like this rather what you probably want to do is use additional
probably want to do is use additional SQL keywords that we won't spend much
SQL keywords that we won't spend much time on in the class itself but these
time on in the class itself but these there are solutions to this problem you
there are solutions to this problem you can begin what's called a transaction
can begin what's called a transaction and you can more explicitly commit to
and you can more explicitly commit to making a decision like updating the
making a decision like updating the database to 10 1001 or 10 1002 or if you
database to 10 1001 or 10 1002 or if you realize wait a minute Carter's query
realize wait a minute Carter's query inist interrupting mine let me roll back
inist interrupting mine let me roll back to the previous state and just uh rewind
to the previous state and just uh rewind let me undo contrl Z if you will there's
let me undo contrl Z if you will there's also another keyword that's not so much
also another keyword that's not so much used anymore in SQL which is locking you
used anymore in SQL which is locking you could literally back in the day lock the
could literally back in the day lock the entire database table preventing anyone
entire database table preventing anyone from updating it or making changes or
from updating it or making changes or even reading it while someone else was
even reading it while someone else was accessing it that was a very
accessing it that was a very heavy-handed solution because it slowed
heavy-handed solution because it slowed everything down but in short
everything down but in short transactions are now a feature of SQL
transactions are now a feature of SQL that you won't necessarily need to use
that you won't necessarily need to use yourselves that do solve this problem by
yourselves that do solve this problem by doing the equivalent of saying while
doing the equivalent of saying while David's like counter is in the process
David's like counter is in the process of being updated keep Carter at Bay
of being updated keep Carter at Bay ideally briefly and then let his data go
ideally briefly and then let his data go through too it's equivalent too to
through too it's equivalent too to putting a note or a lock on the fridge
putting a note or a lock on the fridge and indeed I mean lock litter they they
and indeed I mean lock litter they they were Once Upon a Time called and still
were Once Upon a Time called and still are in some texts called locks on
are in some texts called locks on databases 2 and the last Pro and the
databases 2 and the last Pro and the code for which you might do this is
code for which you might do this is almost the same you simply wrap the
almost the same you simply wrap the three queries uh with a transaction
three queries uh with a transaction statement and a uh commit and the term
statement and a uh commit and the term of art here is that this makes your uh
of art here is that this makes your uh your uh statements Atomic so Atomic
your uh statements Atomic so Atomic means they're either all executed or not
means they're either all executed or not at all that is they're all very tightly
at all that is they're all very tightly coupled together without interruption
coupled together without interruption transactions solves that problem and
transactions solves that problem and avoid having two gallons of milk and the
avoid having two gallons of milk and the last problem that arises that Is
last problem that arises that Is tragically so darn common in the real
tragically so darn common in the real world today is what's called a SQL
world today is what's called a SQL injection attack and it's what I alluded
injection attack and it's what I alluded to earlier with the question mark So
to earlier with the question mark So suppose you're in the habit of logging
suppose you're in the habit of logging in uh to Yale websites with your net ID
in uh to Yale websites with your net ID or password or at Harvard your Harvard
or password or at Harvard your Harvard key and password as well suppose for the
key and password as well suppose for the sake of discussion that the people that
sake of discussion that the people that implemented like Harvard key login allow
implemented like Harvard key login allow you to type in your email address of
you to type in your email address of course and your password but suppose
course and your password but suppose that they are using SQL underneath the
that they are using SQL underneath the hood to check your username and password
hood to check your username and password to make sure that you are David menen or
to make sure that you are David menen or Carter zeni or whoever you claim to be I
Carter zeni or whoever you claim to be I haven't shown you the syntax yet but it
haven't shown you the syntax yet but it turns out that in SQL Das Dash is a
turns out that in SQL Das Dash is a special way of indicating a comment it
special way of indicating a comment it means ignore everything to the right so
means ignore everything to the right so it's just like SL slash and C or the
it's just like SL slash and C or the hash symbol in Python dash dash just
hash symbol in Python dash dash just means ignore everything to the right and
means ignore everything to the right and we've of course seen single quotes So
we've of course seen single quotes So one way to wage a SQL injection attack
one way to wage a SQL injection attack is to try to inject malicious SQL code
is to try to inject malicious SQL code into someone else's database without
into someone else's database without them realizing it how do you do this
them realizing it how do you do this well suppose I log in as M harvard.edu
well suppose I log in as M harvard.edu single quote Das Dash I'm not double
single quote Das Dash I'm not double quoting anything clearly and there's
quoting anything clearly and there's nothing to the right of the dash dash
nothing to the right of the dash dash anyway but it this imbalance is going to
anyway but it this imbalance is going to be useful why because if I'm a hacker
be useful why because if I'm a hacker and I'm presuming you know someone at
and I'm presuming you know someone at Harvard probably is using SQL uh single
Harvard probably is using SQL uh single quotes to wrap the user's email address
quotes to wrap the user's email address and wrap the user's password what if I
and wrap the user's password what if I try to like complete their thought for
try to like complete their thought for them and close one of those quotes for
them and close one of those quotes for them what might happen well we could do
them what might happen well we could do this here for instance let me
this here for instance let me hypothesize is the code that Harvard
hypothesize is the code that Harvard wrote hopefully not underneath the hood
wrote hopefully not underneath the hood so they're using CS library and Python
so they're using CS library and Python and they're using SQL inside suppose
and they're using SQL inside suppose that they have a query like this select
that they have a query like this select star from users where username equals uh
star from users where username equals uh question mark and password equals
question mark and password equals question mark and then suppose they just
question mark and then suppose they just plug in whatever username and password
plug in whatever username and password was typed in and then if they get back
was typed in and then if they get back some number of rows dot dot dot they
some number of rows dot dot dot they assume I am David they assume Carter is
assume I am David they assume Carter is Carter if both the username and password
Carter if both the username and password are in the database just end of story
are in the database just end of story there this is good this has the question
there this is good this has the question mark placeholders we discussed earlier
mark placeholders we discussed earlier but what if you don't quite remember
but what if you don't quite remember that you don't quite take that to heart
that you don't quite take that to heart and you use your more familiar last week
and you use your more familiar last week F strings whereby we use these curly
F strings whereby we use these curly braces to plug in values what if you do
braces to plug in values what if you do this instead so it's almost the same
this instead so it's almost the same idea it's still DB execute but now it's
idea it's still DB execute but now it's select star from users where username
select star from users where username equals and now notice I'm doing the
equals and now notice I'm doing the single quotes which is required by SQL
single quotes which is required by SQL but I'm using F strings with the curly
but I'm using F strings with the curly braces and the password equals single
braces and the password equals single quote password and then close single
quote password and then close single quote the problem is if you're just
quote the problem is if you're just blindly pasting equ effectively the
blindly pasting equ effectively the user's input into that web form into the
user's input into that web form into the username field and the password field
username field and the password field there's nothing stopping a malicious
there's nothing stopping a malicious user student faculty staff from
user student faculty staff from including a single quote in their name
including a single quote in their name or maybe even an uh uh you know
or maybe even an uh uh you know benevolently if their name happens to
benevolently if their name happens to have a single quote as some last names
have a single quote as some last names in particular do so this is very fragile
in particular do so this is very fragile why well suppose that if we plug in my
why well suppose that if we plug in my malicious value Ma at harvard.edu single
malicious value Ma at harvard.edu single quote-- notice what happens to username
quote-- notice what happens to username here the username variable inside of the
here the username variable inside of the curly quotes will get replaced with this
curly quotes will get replaced with this and notice single quote which the
and notice single quote which the Harvard programmer wrote M an
Harvard programmer wrote M an harvard.edu single quote which I wrote
harvard.edu single quote which I wrote dash dash which I wrote single quote
dash dash which I wrote single quote which Harvard wrote and whatever else
which Harvard wrote and whatever else they want after that what's the
they want after that what's the implication though of the dash
dash everything to the right is going to be ignored so the password is never even
be ignored so the password is never even checked in this scenario I'm tricking
checked in this scenario I'm tricking the server into ignoring everything
the server into ignoring everything after the dash dash but I have
after the dash dash but I have constructed very cleverly very
constructed very cleverly very maliciously a syntactically valid query
maliciously a syntactically valid query why because I provided the single quote
why because I provided the single quote that's going to finish the thought of
that's going to finish the thought of that first single quote and now I would
that first single quote and now I would only know how to do this if I saw the
only know how to do this if I saw the code or if I just randomly try putting
code or if I just randomly try putting apostrophes into web forms and see if
apostrophes into web forms and see if things break that's often how
things break that's often how adversaries attack systems they type in
adversaries attack systems they type in potentially dangerous characters hit
potentially dangerous characters hit enter if something breaks they're not
enter if something breaks they're not necessarily into the system but they
necessarily into the system but they know that there might be a vulnerability
know that there might be a vulnerability and then they start trying more
and then they start trying more methodically things like this so This
methodically things like this so This Then is going to be bad because it
Then is going to be bad because it effectively Grays out the rest of the
effectively Grays out the rest of the query and this query is surely going to
query and this query is surely going to return some rows without even knowing my
return some rows without even knowing my password and so this logic here dot dot
password and so this logic here dot dot dot means well if a data came back from
dot means well if a data came back from this query Harvard is presumably going
this query Harvard is presumably going to assume that men logged in show them
to assume that men logged in show them uh show him like uh his account or
uh show him like uh his account or whatever is being protected here so in
whatever is being protected here so in short using F strings bad using any
short using F strings bad using any equivalent like p uh percent s in C bad
equivalent like p uh percent s in C bad when it comes to SQL using question
when it comes to SQL using question marks or whatever a thirdparty library
marks or whatever a thirdparty library like cs50's prescribed is the way to
like cs50's prescribed is the way to solve this why because libraries like
solve this why because libraries like ours are designed to at least be smart
ours are designed to at least be smart and be paranoid and what we will do is
and be paranoid and what we will do is this when you use the question marks and
this when you use the question marks and the values are plugged in we will escape
the values are plugged in we will escape any potentially dangerous characters
any potentially dangerous characters inside of those placeholders and so
inside of those placeholders and so effectively the single quote will no
effectively the single quote will no longer be considered a grammatical
longer be considered a grammatical single quote it will just be literally a
single quote it will just be literally a character in the username or password so
character in the username or password so the library takes care of this for you
the library takes care of this for you because you're plugging in the username
because you're plugging in the username and password as separate arguments and
and password as separate arguments and then we or the third party you're using
then we or the third party you're using actually sanitize that is uh clean up
actually sanitize that is uh clean up the data and prevent those bad
the data and prevent those bad characters now this is kind of an
characters now this is kind of an internet meme that went around for a
internet meme that went around for a while um if you've ever uh driven a car
while um if you've ever uh driven a car been in a car where there's like the
been in a car where there's like the automatic reader for tolls uh this
automatic reader for tolls uh this person thought it might be funny to try
person thought it might be funny to try doing something like this what are they
doing something like this what are they presumably doing the presumption here is
presumably doing the presumption here is whether or not it worked as unclear is
whether or not it worked as unclear is that here's like the end of a actual
that here's like the end of a actual license plate number but here's an
license plate number but here's an interesting single quote and a semicolon
interesting single quote and a semicolon that's especially bad because it means
that's especially bad because it means you can maybe execute a second query on
you can maybe execute a second query on the database this is someone having fun
the database this is someone having fun trying to drop the entire database table
trying to drop the entire database table for whatever municipality is scanning
for whatever municipality is scanning through cameras uh their license plate
through cameras uh their license plate code and I would be remiss if we didn't
code and I would be remiss if we didn't end on this note at least in computer
end on this note at least in computer science circles um there is someone
science circles um there is someone named no relation to the TF name we put
named no relation to the TF name we put in the database earlier um little Bobby
in the database earlier um little Bobby tables which ends with this XKCD
tables which ends with this XKCD comic and if you chuckle if you laugh
comic and if you chuckle if you laugh you're now legit SQL
programmers nice nice like every CS student out there
nice like every CS student out there knows about little Bobby tables so if
knows about little Bobby tables so if you name drop little Bobby tables now
you name drop little Bobby tables now like you're in all right that's it
like you're in all right that's it though for today we will see you next
[Music] [Applause]
[Music] [Applause]
all the way to the top and then you're passing
passing down this is for you Yale we love you
down this is for you Yale we love you Yale we're here to Har
Yale we're here to Har go
down it's going to happen it's actually going to happen I
happen it's actually going to happen I can't believe this what do you think of
can't believe this what do you think of Y they don't think
good can't does everyone have it does everyone have their stuff does everyone
everyone have their stuff does everyone have their stuff probably that it's
have their stuff probably that it's going to beable very
small I know what
know what houses how many exra how many
EXT no F forer yeah just make sure everyone
everyone has pass all the car distributed
all right we can do it [Applause]
[Applause] [Music]
what do you think of Y sir go go one more time one
[Applause] more there goes
more there goes [Applause]
[Applause] again
again [Applause]
all right this is cs50 welcome to week 8 last week we learned how to create read
last week we learned how to create read update and delete databases using squl
update and delete databases using squl but this
but this week Adam everyone happy
Halloween all right so this is cs50 and this is week eight already my thanks to
this is week eight already my thanks to Adam on today this happy Halloween uh in
Adam on today this happy Halloween uh in the coming moments we're going to learn
the coming moments we're going to learn all about how the internet itself Works
all about how the internet itself Works which of course is a technology that
which of course is a technology that like we're all take we all use every day
like we're all take we all use every day probably using in some form right now
probably using in some form right now but we'll see that if you start to
but we'll see that if you start to understand some of the underlying
understand some of the underlying building blocks that power the internet
building blocks that power the internet itself we can actually start to build
itself we can actually start to build interesting things on top of it and a
interesting things on top of it and a lot of the apps the websites that you
lot of the apps the websites that you all use every day should become all the
all use every day should become all the more familiar things that you yourself
more familiar things that you yourself VES can create and honestly when things
VES can create and honestly when things go wrong you'll have all the more of a
go wrong you'll have all the more of a mental model for how things work or are
mental model for how things work or are not in fact working so that you can
not in fact working so that you can ultimately diagnose diagnose all the
ultimately diagnose diagnose all the more issues yourselves so if we take a
more issues yourselves so if we take a look at the internet in the early days
look at the internet in the early days it pretty much was just this this
it pretty much was just this this happens to be of course the geography of
happens to be of course the geography of the United States and just some of the
the United States and just some of the first uh points on the internet were
first uh points on the internet were these here this was so-called arpanet
these here this was so-called arpanet back in 1969 and indeed the internet had
back in 1969 and indeed the internet had its Origins here in the United States
its Origins here in the United States with just a few computers interconnect
with just a few computers interconnect somehow initially that of course began
somehow initially that of course began to grow over time such that we
to grow over time such that we eventually had the West Coast connected
eventually had the West Coast connected to the east coast and nowadays what you
to the east coast and nowadays what you can think of these dots on the screen is
can think of these dots on the screen is representing are these things called
representing are these things called routers sort of computers or really
routers sort of computers or really servers that somehow have wires or maybe
servers that somehow have wires or maybe wireless connections between them that
wireless connections between them that allow data to flow from point A to B to
allow data to flow from point A to B to C and then this of course has been now
C and then this of course has been now magnified across the entire Globe um and
magnified across the entire Globe um and even above ground as well so that we can
even above ground as well so that we can connect all the more readily uh to
connect all the more readily uh to systems anywhere now in order to Route
systems anywhere now in order to Route the data from one router to another we
the data from one router to another we need to somehow make routing decisions
need to somehow make routing decisions and this is the kind of thing that the
and this is the kind of thing that the internet service providers the isps of
internet service providers the isps of the world just handle for us you and I
the world just handle for us you and I plug our Macs our PCS into the network
plug our Macs our PCS into the network here at Harvard or equivalently at Yale
here at Harvard or equivalently at Yale or we somehow get online via Wi-Fi or
or we somehow get online via Wi-Fi or cellular technology and then some of
cellular technology and then some of these larger entities these bigger
these larger entities these bigger companies or countries handle most of
companies or countries handle most of the data getting from point A to point B
the data getting from point A to point B and if you think about what these
and if you think about what these routers present they're indeed just
routers present they're indeed just servers somehow interconnected not
servers somehow interconnected not unlike this grid of tiles here for
unlike this grid of tiles here for instance back in the zoom days and in
instance back in the zoom days and in fact here we have I claim a grid of
fact here we have I claim a grid of routers implemented here by the courses
routers implemented here by the courses teaching fellows and course assistants
teaching fellows and course assistants and Tas and if the goal at hand for
and Tas and if the goal at hand for instance is for Phyllis to Route some
instance is for Phyllis to Route some piece of information maybe it's an email
piece of information maybe it's an email maybe it's a request for a web page in
maybe it's a request for a web page in the bottom right hand corner all the way
the bottom right hand corner all the way up to say Brian here in the top left
up to say Brian here in the top left hand corner suffice it to say each each
hand corner suffice it to say each each of these tiles represents a router a
of these tiles represents a router a server that can move the data back forth
server that can move the data back forth left and right that packet of
left and right that packet of information so to speak from Phyllis to
information so to speak from Phyllis to Brian could take any number of different
Brian could take any number of different possible routes up down left right to go
possible routes up down left right to go from the one corner to another so let me
from the one corner to another so let me go ahead and hit play on this video here
go ahead and hit play on this video here we're in the teaching fellows play the
we're in the teaching fellows play the same
same [Music]
[Music] role
all right so in this particular case the data was routed pretty straightforwardly
data was routed pretty straightforwardly up and then to the left but suppose that
up and then to the left but suppose that one or more of the staff were a bit busy
one or more of the staff were a bit busy maybe one of the routers is congested
maybe one of the routers is congested that is to say just got way more
that is to say just got way more envelopes at a moment in time that it
envelopes at a moment in time that it can handle thankfully the design of the
can handle thankfully the design of the internet is such that there's often
internet is such that there's often multiple ways that data can get from
multiple ways that data can get from point A to point B maybe going through
point A to point B maybe going through Point C or Point D instead and so
Point C or Point D instead and so there's a resilience there even as some
there's a resilience there even as some of these servers themselves might go
of these servers themselves might go down so allow me to propose that we use
down so allow me to propose that we use the same grid of routers now to Route
the same grid of routers now to Route the data in a slightly different way
the data in a slightly different way this
this [Music]
[Music] [Applause]
[Applause] [Music]
[Music] [Applause]
[Music] time
so success you'll see perhaps later just how many takes it took us to actually
how many takes it took us to actually get that routing right but it does in
get that routing right but it does in fact manifest that you can uh travel
fact manifest that you can uh travel different paths in order to get the data
different paths in order to get the data from point A to point B so as we talk
from point A to point B so as we talk about routers as you think of the
about routers as you think of the internet I mean think of those humans as
internet I mean think of those humans as just representing these routers points a
just representing these routers points a to p and everywhere in between now how
to p and everywhere in between now how did the teaching staff know to route
did the teaching staff know to route that packet up and then down or left and
that packet up and then down or left and right in order to get get to the
right in order to get get to the destination well all of them were
destination well all of them were programmed so to speak to understand
programmed so to speak to understand protocols it's called TCP and IP
protocols it's called TCP and IP Otherwise Known together typically as
Otherwise Known together typically as tcpip and you've probably seen these
tcpip and you've probably seen these acronyms at some point in the real world
acronyms at some point in the real world on the internet on some kind of
on the internet on some kind of documentation a text even if you haven't
documentation a text even if you haven't really thought hard about it but IP is
really thought hard about it but IP is certainly the more common of the two
certainly the more common of the two perhaps in common culture so what does
perhaps in common culture so what does TCP and IP do for us well really two
TCP and IP do for us well really two primary things any computer or any
primary things any computer or any teaching staff member who understands
teaching staff member who understands tcpip knows how to get data from point A
tcpip knows how to get data from point A to point B but how well let's break down
to point B but how well let's break down what that problem to be solved is IP
what that problem to be solved is IP otherwise known as Internet Protocol is
otherwise known as Internet Protocol is a protocol that computers speak that
a protocol that computers speak that allow them to know how to address
allow them to know how to address computers on the internet and a protocol
computers on the internet and a protocol is just a set of conventions that
is just a set of conventions that computers adhere to so someone wrote
computers adhere to so someone wrote code that probably has a whole lot of
code that probably has a whole lot of conditionals that tells the computer
conditionals that tells the computer what to do if something happens like if
what to do if something happens like if I receive a packet then send it to the
I receive a packet then send it to the next server or something like that in
next server or something like that in the human world we have protocols too
the human world we have protocols too you know pre in healthier times it was
you know pre in healthier times it was quite common to sort of extend your hand
quite common to sort of extend your hand to another human in order to greet them
to another human in order to greet them and if they're following human protocol
and if they're following human protocol they would presumably grab your hand and
they would presumably grab your hand and shake it at least in a a culture like
shake it at least in a a culture like this one here on campus and now that is
this one here on campus and now that is a human protocol in that someone
a human protocol in that someone initiates it someone responds to it and
initiates it someone responds to it and you both sort of know what to do you're
you both sort of know what to do you're programmed to know what to do so same
programmed to know what to do so same idea with Internet Protocol computers
idea with Internet Protocol computers just know what to do when they've been
just know what to do when they've been programmed to do so so what does this
programmed to do so so what does this mean IP had decides that every compter
mean IP had decides that every compter computer in the world will have a unique
computer in the world will have a unique address just like uh a the science
address just like uh a the science center around the corner might have a
center around the corner might have a unique address of one Oxford Street
unique address of one Oxford Street Cambridge Massachusetts 02138 USA IP
Cambridge Massachusetts 02138 USA IP dictates that every computer on the
dictates that every computer on the internet have a unique address of this
internet have a unique address of this form and this too is probably something
form and this too is probably something you've seen in the real world even if
you've seen in the real world even if you haven't thought too hard about it
you haven't thought too hard about it it's a number and what's called dotted
it's a number and what's called dotted decimal notation which means it's a
decimal notation which means it's a decimal number do something do something
decimal number do something do something do something so four digits separated by
do something so four digits separated by convention by decimal points although
convention by decimal points although there are newer and bigger versions of
there are newer and bigger versions of the same and these so-called IP
the same and these so-called IP addresses that might be as simple as
addresses that might be as simple as 1.2.3.4 uniquely identify a computer on
1.2.3.4 uniquely identify a computer on the internet uh the numbers have to
the internet uh the numbers have to range from 0 to 255 each and that's a
range from 0 to 255 each and that's a bit of a hint just as you start to think
bit of a hint just as you start to think more computationally if each of these is
more computationally if each of these is a number from 0 to 255 how many bits
a number from 0 to 255 how many bits does that suggest each number is
does that suggest each number is using feel free to shout it
using feel free to shout it out how many bits gives us 255 256 total
out how many bits gives us 255 256 total possibilities so eight bits that should
possibilities so eight bits that should just be sort of a heuristic in your mind
just be sort of a heuristic in your mind anytime you hear something that's in the
anytime you hear something that's in the range of 0 to 255 or 256 values total
range of 0 to 255 or 256 values total think back to week zero which gives us
think back to week zero which gives us eight bits plus another eight bits
eight bits plus another eight bits another eight bits and another eight
another eight bits and another eight bits which is to say an IP address
bits which is to say an IP address typically is 32 bits in total now if we
typically is 32 bits in total now if we do another bit of quick mental math or
do another bit of quick mental math or think back to week zero if every IP
think back to week zero if every IP address is 32 bits how many computers
address is 32 bits how many computers can we have on the
can we have on the internet at once give or take
internet at once give or take roughly 4 billion is the ballpark and we
roughly 4 billion is the ballpark and we don't need to be super precise for
don't need to be super precise for discussion's sake but roughly 4 billion
discussion's sake but roughly 4 billion is how high you can account assuming no
is how high you can account assuming no negative numbers if you have 32 bits in
negative numbers if you have 32 bits in total now that's not terribly uh many
total now that's not terribly uh many number uh numbers of addresses
number uh numbers of addresses especially considering the number of
especially considering the number of humans in the world the number of us
humans in the world the number of us that do have laptops or desktops or
that do have laptops or desktops or devices more generally phones in our
devices more generally phones in our pockets and the like so let me just
pockets and the like so let me just stipulate for today's purposes that
stipulate for today's purposes that there's even a newer and improved
there's even a newer and improved version of Ip otherwise known as version
version of Ip otherwise known as version 6 this is version four but still super
6 this is version four but still super popular version six uses 128 bits which
popular version six uses 128 bits which is a huge number of possible
is a huge number of possible premutations I dare say I can't even
premutations I dare say I can't even pronounce that number it's so big so
pronounce that number it's so big so there are ways around even this
there are ways around even this limitation already so every computer has
limitation already so every computer has an address like this what does that
an address like this what does that really mean well suppose that I was
really mean well suppose that I was Phyllis in the story told visually
Phyllis in the story told visually earlier and I want to send a message to
earlier and I want to send a message to Brian well both Phyllis and Brian have I
Brian well both Phyllis and Brian have I IP addresses and suppose that Brian's IP
IP addresses and suppose that Brian's IP address happens to be 1.2.3.4 in that
address happens to be 1.2.3.4 in that top left hand corner well phyllis's Mac
top left hand corner well phyllis's Mac or PC or phone would essentially do the
or PC or phone would essentially do the equivalent on this human envelope by
equivalent on this human envelope by writing the two address in the middle of
writing the two address in the middle of the envelope as is our human convention
the envelope as is our human convention like this so this is an envelope a piece
like this so this is an envelope a piece of information an email a text message
of information an email a text message whatever destined for Brian and so she
whatever destined for Brian and so she would have her computer put Brian's IP
would have her computer put Brian's IP address in the middle her IP address is
address in the middle her IP address is Maybe 5.6.7 do8 so just like our human
Maybe 5.6.7 do8 so just like our human convention I might write 5.
convention I might write 5. 6.7.8 at the top of the envelope
6.7.8 at the top of the envelope 5678 thereby indicating what the return
5678 thereby indicating what the return address is and this is helpful because
address is and this is helpful because if Brian's computer needs to acknowledge
if Brian's computer needs to acknowledge receipt if he needs to reply in some
receipt if he needs to reply in some form this way the envelope has all the
form this way the envelope has all the information we need but in the real
information we need but in the real world servers do a lot of things
world servers do a lot of things nowadays not just email but maybe chat
nowadays not just email but maybe chat Maybe video conferencing maybe any
Maybe video conferencing maybe any number of other services as well and so
number of other services as well and so it turns out that an address alone might
it turns out that an address alone might not be sufficient because how do how
not be sufficient because how do how does Brian's computer know when he opens
does Brian's computer know when he opens the envelope so to speak that this
the envelope so to speak that this should be interpreted as an email or
should be interpreted as an email or interpret it as a chat message or
interpret it as a chat message or interpret it as like a video attachment
interpret it as like a video attachment that Phyllis has sent well we need some
that Phyllis has sent well we need some other mechanism some other hint on this
other mechanism some other hint on this envelope to distinguish one type of
envelope to distinguish one type of Internet service from another and so
Internet service from another and so that's where the other acronym in TCI
that's where the other acronym in TCI pip comes in which is TCP so this stands
pip comes in which is TCP so this stands for transmission control protocol which
for transmission control protocol which is just a different set of conventions
is just a different set of conventions that computers adhere to in order to
that computers adhere to in order to solve a couple of different problems one
solve a couple of different problems one is this problem of distinguishing one
is this problem of distinguishing one type of service from another now what
type of service from another now what does that mean well humans decades ago
does that mean well humans decades ago decided as they started inventing all of
decided as they started inventing all of these various internet services the web
these various internet services the web being the first one how they might or
being the first one how they might or the web now being one of the most
the web now being one of the most popular ones they decided to assign
popular ones they decided to assign different services that can be used on
different services that can be used on the internet unique numbers and so two
the internet unique numbers and so two of the most common are these 80 is the
of the most common are these 80 is the number that a bunch of humans decided
number that a bunch of humans decided years ago will represent what you and I
years ago will represent what you and I know as HTTP and we'll talk more
know as HTTP and we'll talk more technically in a bit about what HTTP is
technically in a bit about what HTTP is but obviously it's the thing that's in
but obviously it's the thing that's in the beginning of every URL nowadays or
the beginning of every URL nowadays or https which of course has the S added to
https which of course has the S added to it and that has its own unique number
it and that has its own unique number and for now the S just means secure one
and for now the S just means secure one is encrypted or scrambled somehow for
is encrypted or scrambled somehow for privacy sake and the other is
privacy sake and the other is unencrypted it's a little more
unencrypted it's a little more vulnerable to interception so these two
vulnerable to interception so these two numbers are what the world decided when
numbers are what the world decided when implementing TCP shall uniquely identify
implementing TCP shall uniquely identify those services so what does this mean
those services so what does this mean well this means that if Brian's computer
well this means that if Brian's computer in the story from before is hosting not
in the story from before is hosting not like an email server but maybe he has a
like an email server but maybe he has a website and Phyllis is requesting
website and Phyllis is requesting Brian's homepage or something like that
Brian's homepage or something like that she would have her Mac or PC or phone
she would have her Mac or PC or phone not only write Brian's IP address in the
not only write Brian's IP address in the middle of the envelope but also the
middle of the envelope but also the number otherwise known as a port number
number otherwise known as a port number that she wants this envelope to be
that she wants this envelope to be routed to now 80 would be insecure
routed to now 80 would be insecure nowadays HTTP col is sort of p and we
nowadays HTTP col is sort of p and we almost always see https colon now so I'm
almost always see https colon now so I'm just going to go with best practice and
just going to go with best practice and I'm going to add a colon and then the
I'm going to add a colon and then the number
number 443 at the end of Brian's IP address so
443 at the end of Brian's IP address so now I have an IP address for Brian the
now I have an IP address for Brian the port number for the service that this is
port number for the service that this is relevant to and I'm not going to bother
relevant to and I'm not going to bother writing it but it turns out that
writing it but it turns out that phyllis's computer would also choose a
phyllis's computer would also choose a port number maybe a random port number
port number maybe a random port number so that Brian can conversely reply and
so that Brian can conversely reply and then the computer can know which
then the computer can know which response is in uh coming back for which
response is in uh coming back for which request but the most important one is
request but the most important one is this one in the two field whereby this
this one in the two field whereby this distinguishes this from like an email a
distinguishes this from like an email a chat message a video conference session
chat message a video conference session or a zoom or whatnot from anything else
or a zoom or whatnot from anything else happening on Brian's computer at the
happening on Brian's computer at the same time so all this time if you've
same time so all this time if you've seen these terms TCP and IP those are
seen these terms TCP and IP those are really two of the most important things
really two of the most important things that they do but TCP does one other
that they do but TCP does one other thing that's super useful too it turns
thing that's super useful too it turns out that it's
out that it's super common nowadays especially to
super common nowadays especially to transmit a lot of media on the internet
transmit a lot of media on the internet internet whether it's an image or maybe
internet whether it's an image or maybe it's a movie file and it would be a
it's a movie file and it would be a little obnoxious to say the least if
little obnoxious to say the least if you're downloading a really big file
you're downloading a really big file meant that no one else in your dorm room
meant that no one else in your dorm room or your household could actually
or your household could actually download anything until you're actually
download anything until you're actually done so of course multiple people
done so of course multiple people nowadays can be on the internet at once
nowadays can be on the internet at once even if all of the connections are a
even if all of the connections are a little slower but like one person's
little slower but like one person's usage does not block someone else's now
usage does not block someone else's now how does this work well TCP in
how does this work well TCP in conjunction with IP can also allow you
conjunction with IP can also allow you to take like a really big image of a cat
to take like a really big image of a cat which is the internet of course is
which is the internet of course is filled with and take a big image of a
filled with and take a big image of a cat or a big video file of a cat and
cat or a big video file of a cat and fragment it into multiple pieces so I'm
fragment it into multiple pieces so I'm just going to sort of roughly tear it
just going to sort of roughly tear it down the middle and then maybe tear it
down the middle and then maybe tear it down the middle again so now it's four
down the middle again so now it's four different fragments and I'm I'm sorry
different fragments and I'm I'm sorry but the computer will be reassembling
but the computer will be reassembling these for us and what phyllis's computer
these for us and what phyllis's computer could do now if she's like uploading
could do now if she's like uploading this picture of a cat to Brian's web
this picture of a cat to Brian's web server well she could put one fragment
server well she could put one fragment in this envelope and then have three
in this envelope and then have three separate envelopes for the other three
separate envelopes for the other three fragments and what you could then do on
fragments and what you could then do on the outside of this envelope is just
the outside of this envelope is just kind of number them somehow and in fact
kind of number them somehow and in fact this is something else that TCP and IP
this is something else that TCP and IP together would do for us this first
together would do for us this first envelope now might say something like
envelope now might say something like one out of four in the memo field so to
one out of four in the memo field so to speak of the uh metaphorical envelope
speak of the uh metaphorical envelope here now this should be enough
here now this should be enough information because now if Brian gets
information because now if Brian gets all four of these envelopes he
all four of these envelopes he presumably knows how to reassemble the
presumably knows how to reassemble the picture of the cat in order top to
picture of the cat in order top to bottom left to right but more
bottom left to right but more importantly suppose that one of the
importantly suppose that one of the routers one of the TFS in the video is
routers one of the TFS in the video is sort of distracted and they sort of drop
sort of distracted and they sort of drop one of the packets and that's a metaphor
one of the packets and that's a metaphor actually in practice for when a router
actually in practice for when a router gets really busy it's got way too much
gets really busy it's got way too much data coming in it might metaphorically
data coming in it might metaphorically drop packets what does that mean in
drop packets what does that mean in practice I mean it literally just
practice I mean it literally just ignores the zeros and ones it doesn't
ignores the zeros and ones it doesn't save them to its memory because there's
save them to its memory because there's just no room left so it's equivalent to
just no room left so it's equivalent to sort of dropping the packet so suppose
sort of dropping the packet so suppose now that Brian gets one of four three of
now that Brian gets one of four three of four and four of four what can his
four and four of four what can his computer infer
computer infer now after receiving those three packets
now after receiving those three packets one of four three of four and four of
one of four three of four and four of four what's the use there yeah I think
four what's the use there yeah I think you're you're signaling with your
you're you're signaling with your fingers which one did which one can I
fingers which one did which one can I call on you yeah so he's missing two out
call on you yeah so he's missing two out of four the second of the packets and
of four the second of the packets and this is useful now because you could
this is useful now because you could imagine he can send some message back to
imagine he can send some message back to Phyllis saying hey please retransmit
Phyllis saying hey please retransmit number two of four without having to
number two of four without having to redownload the entirety of the cat so
redownload the entirety of the cat so there's there's an efficiency there as
there's there's an efficiency there as well so tcpip allows data really to go
well so tcpip allows data really to go from point A to point B while solving a
from point A to point B while solving a bunch of these problems along the way so
bunch of these problems along the way so nowadays if you ever see mention on your
nowadays if you ever see mention on your Mac or PC of your so-called IP address
Mac or PC of your so-called IP address that is the sort of problem that's being
that is the sort of problem that's being solved questions now on these protocols
solved questions now on these protocols these conventions called TCP and
these conventions called TCP and IP that's the extent to which we'll need
IP that's the extent to which we'll need to understand them won't have to
to understand them won't have to implement them per se we'll just take
implement them per se we'll just take them Hereafter for
them Hereafter for granted any questions that you've ever
granted any questions that you've ever been wondering wondering about your home
been wondering wondering about your home network
yeah a really good question uh how does TCP know that a user got a message
TCP know that a user got a message another aspect another feature of TCP is
another aspect another feature of TCP is that Brian's computer by design of this
that Brian's computer by design of this protocol will also acknowledge the
protocol will also acknowledge the packets that he's received and it will
packets that he's received and it will do it efficiently if Brian receives all
do it efficiently if Brian receives all four packets in a pretty narrow window
four packets in a pretty narrow window of time his computer will send to
of time his computer will send to phyllis's computer a quick message
phyllis's computer a quick message saying essentially received all four
saying essentially received all four otherwise he'll say the opposite which
otherwise he'll say the opposite which is that I'm missing for for instance two
is that I'm missing for for instance two out of four and that just ensures
out of four and that just ensures ultimately that all of the data is
ultimately that all of the data is indeed uh arrived so that you're not
indeed uh arrived so that you're not missing like uh a quarter a quadrant of
missing like uh a quarter a quadrant of the cat in question all right but th
the cat in question all right but th that's not the only problem that needs
that's not the only problem that needs to be solved ultimately we also need to
to be solved ultimately we also need to make the internet userfriendly if you
make the internet userfriendly if you will and it would be really tedious if
will and it would be really tedious if you had to visit websites for instance
you had to visit websites for instance by way of of their IP addresses right
by way of of their IP addresses right 1.2.3.4 is pretty memorable but there's
1.2.3.4 is pretty memorable but there's like 4 billion other possible addresses
like 4 billion other possible addresses available and it would be super tedious
available and it would be super tedious to remember those it would be bad
to remember those it would be bad marketing to advertise those in fact
marketing to advertise those in fact most of you probably don't even know the
most of you probably don't even know the phone numbers of your closest friends
phone numbers of your closest friends and family members anymore because you
and family members anymore because you instead store them in your contacts
instead store them in your contacts you're in your dress book associating
you're in your dress book associating with numbers that are completely opaque
with numbers that are completely opaque with actual names or strings if you will
with actual names or strings if you will the same is goes for the internet too
the same is goes for the internet too even though every computer does have and
even though every computer does have and must have a unique IP address
must have a unique IP address numerically why well routers or
numerically why well routers or computers computers just crunch numbers
computers computers just crunch numbers very readily but we humans work better
very readily but we humans work better with strings of text we need some system
with strings of text we need some system for converting userfriendly strings like
for converting userfriendly strings like harvard.edu or yale.edu or google.com to
harvard.edu or yale.edu or google.com to the underlying IP addresses and that's
the underlying IP addresses and that's where the next acronym comes in today
where the next acronym comes in today which is DNS domain name system so this
which is DNS domain name system so this is just another technology that's been
is just another technology that's been in use for some time now and it's a
in use for some time now and it's a collection of servers on the internet
collection of servers on the internet that whose purpose in life is to convert
that whose purpose in life is to convert domain names to IP addresses and maybe
domain names to IP addresses and maybe vice versa as well so let me stipulate
vice versa as well so let me stipulate for today's purposes there are some root
for today's purposes there are some root DNS servers in the world that long story
DNS servers in the world that long story short know about all of the Dooms all of
short know about all of the Dooms all of the edus all of the dot dot dot all of
the edus all of the dot dot dot all of the other top level domains around the
the other top level domains around the world as well as in the US and then
world as well as in the US and then there are some smaller DNS servers owned
there are some smaller DNS servers owned by companies owned by universities and
by companies owned by universities and even in your apartments or homes most
even in your apartments or homes most likely indeed if you have a home router
likely indeed if you have a home router plugged into the wall somewhere that's
plugged into the wall somewhere that's not only routing your data in and out of
not only routing your data in and out of your apartment or home or dorm room it's
your apartment or home or dorm room it's also typically serving as a local
also typically serving as a local therefore faster DNS server a cash if
therefore faster DNS server a cash if you will it's sort of locally saving
you will it's sort of locally saving your most frequently accessed websites
your most frequently accessed websites and their IP addresses just to avoid
and their IP addresses just to avoid bothering the bigger more expensive
bothering the bigger more expensive busier servers all day long so there's
busier servers all day long so there's DNS servers all over if you poke around
DNS servers all over if you poke around your settings in Windows or Mac OS or
your settings in Windows or Mac OS or Android or iOS you'll see mention of DNS
Android or iOS you'll see mention of DNS and you'll probably see the IP addresses
and you'll probably see the IP addresses of the servers whose purpose in life is
of the servers whose purpose in life is to do this conversion for you but this
to do this conversion for you but this is a requisite feature if we just want
is a requisite feature if we just want the internet to be user friendly and
the internet to be user friendly and allow us to use words instead of numbers
allow us to use words instead of numbers alone what's inside of these DNS servers
alone what's inside of these DNS servers you know it's essentially a spreadsheet
you know it's essentially a spreadsheet or if we can say it more geeky it's
or if we can say it more geeky it's essentially like a hash table of some
essentially like a hash table of some sort which it has keys and values like
sort which it has keys and values like the key is the domain name harvard.edu
the key is the domain name harvard.edu gale. edug google.com and the value is
gale. edug google.com and the value is the corresponding IP address or in many
the corresponding IP address or in many cases IP addresses plural of the
cases IP addresses plural of the corresponding servers so here already
corresponding servers so here already even though I've drawn it fairly
even though I've drawn it fairly abstractly like you would on a
abstractly like you would on a chalkboard it's really probably
chalkboard it's really probably implemented as some kind of table maybe
implemented as some kind of table maybe a hash table maybe a database table
a hash table maybe a database table maybe SQL or something like that or
maybe SQL or something like that or maybe it's even just a link list or an
maybe it's even just a link list or an array we just have to somehow enable
array we just have to somehow enable this computer to convert one to the
this computer to convert one to the other now just to be super precise
other now just to be super precise DNS servers actually convert what are
DNS servers actually convert what are called fully qualified domain names
called fully qualified domain names which is generally not just harvard.edu
which is generally not just harvard.edu but more verbosely
but more verbosely www.harvard.edu and
www.harvard.edu and www.google.com so the whole thing that
www.google.com so the whole thing that you would see as a substring of the URL
you would see as a substring of the URL so that's what DNS does and that's what
so that's what DNS does and that's what your University your company your home
your University your company your home router are doing for you let me pause
router are doing for you let me pause here to see if there are any
here to see if there are any questions this to is just a technology
questions this to is just a technology now we'll take for granted just
now we'll take for granted just works questions at all all right so
works questions at all all right so let's now transition among our protocols
let's now transition among our protocols really to the last for today which will
really to the last for today which will set the stage for actually solving
set the stage for actually solving problems with these and writing some
problems with these and writing some code ultimately um HTTP this is
code ultimately um HTTP this is something that you see or hear all day
something that you see or hear all day long even though you rarely have to
long even though you rarely have to bother typing it anymore odds are if you
bother typing it anymore odds are if you go to harvard.edu yale.edu google.com
go to harvard.edu yale.edu google.com you don't bother typing HTTP let alone
you don't bother typing HTTP let alone https manually anymore why because your
https manually anymore why because your browser Auto completes that kind of
browser Auto completes that kind of thing just to make life easier but it is
thing just to make life easier but it is officially at the beginning of every URL
officially at the beginning of every URL you visit either HTTP or the more secure
you visit either HTTP or the more secure https whenever you're using your browser
https whenever you're using your browser to access some website so
to access some website so HTTP stands for hyper text transfer
HTTP stands for hyper text transfer protocol and it's uh easily one of the
protocol and it's uh easily one of the most popular dare say one of the most
most popular dare say one of the most powerful features of the internet
powerful features of the internet nowadays but the mental model to have
nowadays but the mental model to have here is that HTTP or the web more
here is that HTTP or the web more generally is kind of a service that runs
generally is kind of a service that runs on top of the internet and maybe Zoom or
on top of the internet and maybe Zoom or Microsoft teams is another service that
Microsoft teams is another service that runs on top of the internet an iMessage
runs on top of the internet an iMessage and Technologies like it is another
and Technologies like it is another service that runs on top of the internet
service that runs on top of the internet so the internet is really like the lower
so the internet is really like the lower level Plumbing the tcpip stuff the DNS
level Plumbing the tcpip stuff the DNS stuff that just gets data from point A
stuff that just gets data from point A to point B but now and we're in a a
to point B but now and we're in a a software development class ultimately
software development class ultimately here in cs50 HTTP is the application
here in cs50 HTTP is the application Level protocol it's sort of what
Level protocol it's sort of what programmers use what companies use what
programmers use what companies use what uh developers use ultimately to use the
uh developers use ultimately to use the underlying Plumbing to build interesting
underlying Plumbing to build interesting and Powerful things so what does this
and Powerful things so what does this mean when it comes to accessing Services
mean when it comes to accessing Services via HTTP or the more secure
via HTTP or the more secure https well here is a representative URL
https well here is a representative URL even though you might not type the whole
even though you might not type the whole thing if you poke around your address
thing if you poke around your address bar this is what's up there with that
bar this is what's up there with that said a lot of browsers nowadays are kind
said a lot of browsers nowadays are kind of simplifying if not dumbing down what
of simplifying if not dumbing down what you see with your human eyes just to
you see with your human eyes just to shorten the strings especially on mobile
shorten the strings especially on mobile devices but almost always if you click
devices but almost always if you click the URL or highlight it then you see the
the URL or highlight it then you see the whole thing but on many browsers you
whole thing but on many browsers you might only ever see example.com but all
might only ever see example.com but all of this information is there it's just
of this information is there it's just getting more and more hidden just for
getting more and more hidden just for user interfaces sake well it turns out
user interfaces sake well it turns out when you visit a URL by default
when you visit a URL by default especially if you type nothing after the
especially if you type nothing after the do com in this case you're technically
do com in this case you're technically implicitly adding a single slash so a
implicitly adding a single slash so a single slash denotes the root of the
single slash denotes the root of the server that is the default page or
server that is the default page or folder in the server and the slash
folder in the server and the slash whether or not you type it or not is
whether or not you type it or not is implicitly going to be there and that
implicitly going to be there and that just means give me the default whatever
just means give me the default whatever is at www.example.com give me that page
is at www.example.com give me that page or that folder but URLs can be longer
or that folder but URLs can be longer than this and more generally there can
than this and more generally there can be a path so to speak and this is a term
be a path so to speak and this is a term of art a path is some sequence of folder
of art a path is some sequence of folder Andor file names after a URL like this
Andor file names after a URL like this and so you might see more specifically
and so you might see more specifically that a URL contains a very specific file
that a URL contains a very specific file this isn't as common nowadays anymore
this isn't as common nowadays anymore though we will begin to today by using
though we will begin to today by using this technique but if there is a file
this technique but if there is a file called literally file. HTML or something
called literally file. HTML or something else on the server that file is going to
else on the server that file is going to be what this URL pulls up on the
be what this URL pulls up on the computer meanwhile you might have slash
computer meanwhile you might have slash folder slash which just means show me
folder slash which just means show me whatever is inside of this folder or you
whatever is inside of this folder or you might have more verbosely folder file.
might have more verbosely folder file. HTML which will show you that file in
HTML which will show you that file in that folder and meanwhile just to give
that folder and meanwhile just to give some other terms of art this is the
some other terms of art this is the so-called fully qualified domain name
so-called fully qualified domain name and again these vocab don't matter all
and again these vocab don't matter all that much but you'll hear or see them
that much but you'll hear or see them over time we generally colloquially just
over time we generally colloquially just refer to this as the domain name which
refer to this as the domain name which is a little less precise but gets the
is a little less precise but gets the job done certainly in conversation and
job done certainly in conversation and this part here I described briefly
this part here I described briefly earlier what's the name for this suffix
earlier what's the name for this suffix at the very end of the fully qualified
at the very end of the fully qualified domain
domain name the yeah yeah top level domain or
name the yeah yeah top level domain or TLD and this is just some form of
TLD and this is just some form of categorization of the URLs now the
categorization of the URLs now the internet in got it start within the
internet in got it start within the United States and a lot of the first
United States and a lot of the first websites of course came from the US and
websites of course came from the US and so For Better or For Worse the sort of
so For Better or For Worse the sort of steak was planted in the ground so
steak was planted in the ground so generally do indicated at least early on
generally do indicated at least early on that it was some kind of commercial
that it was some kind of commercial Enterprise a business that owned a
Enterprise a business that owned a domain name edu is a educational
domain name edu is a educational institution net was some kind of
institution net was some kind of network. goov was the US government now
network. goov was the US government now there are also country code tlds cctlds
there are also country code tlds cctlds like UK or JP every country in the world
like UK or JP every country in the world has its own two letter uh TLD um that
has its own two letter uh TLD um that might very well be restricted to only
might very well be restricted to only servers or companies or people in that
servers or companies or people in that uh in that country many of them can be
uh in that country many of them can be used by anyone you've used a lot of URLs
used by anyone you've used a lot of URLs in this class ending in
in this class ending in cs50. that doesn't mean input output um
cs50. that doesn't mean input output um it actually is a TLD from another
it actually is a TLD from another country that lets anyone on the internet
country that lets anyone on the internet um pay for and on an annual basis using
um pay for and on an annual basis using that domain. TV for instance you might
that domain. TV for instance you might see in some cases like twitch.tv and the
see in some cases like twitch.tv and the like um that too is owned by another
like um that too is owned by another country that allows others in the
country that allows others in the English-speaking World in this case to
English-speaking World in this case to use it as though it connotes TV but
use it as though it connotes TV but those are just different types of TLS
those are just different types of TLS that roughly categorize where the domain
that roughly categorize where the domain lives but it doesn't necessarily mean
lives but it doesn't necessarily mean it's commercial anymore it doesn't
it's commercial anymore it doesn't necessarily mean it's a Network anymore
necessarily mean it's a Network anymore for the most part there are hundreds of
for the most part there are hundreds of tlds now for better for worse most of
tlds now for better for worse most of which are less common than these big
which are less common than these big ones um but most anyone can buy most of
ones um but most anyone can buy most of them with just some restrictions on
them with just some restrictions on things like edu and goov that are still
things like edu and goov that are still very much regulated this meanwhile is
very much regulated this meanwhile is what we might call the host name www
what we might call the host name www it's obviously a super common convention
it's obviously a super common convention like almost every website uses www as
like almost every website uses www as its host name but that's a human
its host name but that's a human convention it's not a requirement and
convention it's not a requirement and indeed some websites don't even bother
indeed some websites don't even bother having a host name they just use their
having a host name they just use their domain to advertise their websites this
domain to advertise their websites this now is going to be the scheme or the
now is going to be the scheme or the protocol and this is just going to
protocol and this is just going to indicate via what protocol the computer
indicate via what protocol the computer your Mac your PC your phone should use
your Mac your PC your phone should use when accessing content at that address
when accessing content at that address because indeed there are other protocols
because indeed there are other protocols you can use but for the most part
you can use but for the most part will'll only focus on HTTP or
will'll only focus on HTTP or equivalently
equivalently https all right any questions now on
https all right any questions now on those just definitions building blocks
those just definitions building blocks of URLs just so we all sort of share a
of URLs just so we all sort of share a common
common vocabulary any questions at all yeah
vocabulary any questions at all yeah what is the
what is the local sure we'll come back to this
local sure we'll come back to this actually later today there's a a
actually later today there's a a technical term known as Local Host which
technical term known as Local Host which is a generic name for your computer your
is a generic name for your computer your Mac your PC your phone especially when
Mac your PC your phone especially when you're doing software development and by
you're doing software development and by convention your own computer has not
convention your own computer has not only whatever IP address you get from
only whatever IP address you get from your University or your internet service
your University or your internet service provider it also has a reflexive IP
provider it also has a reflexive IP address one that just always refers to
address one that just always refers to itself which is 127.0.0.1 and that's
itself which is 127.0.0.1 and that's just a human convention humans decided
just a human convention humans decided that shall refer always to your computer
that shall refer always to your computer and it's actually going to be useful
and it's actually going to be useful today and onward because we can use that
today and onward because we can use that when development on our own computers
when development on our own computers ultimately other questions on URLs IP
ultimately other questions on URLs IP DNS or any of these building
DNS or any of these building blocks all right so what do we mean by
blocks all right so what do we mean by HTTP being a protocol when I extended my
HTTP being a protocol when I extended my hand earlier as a human handshake you
hand earlier as a human handshake you know a typical human in healthy times
know a typical human in healthy times would know to respond in turn well how
would know to respond in turn well how does Brian's computer know to respond to
does Brian's computer know to respond to phyllis's envelope whatever message is
phyllis's envelope whatever message is they're in well assuming that Brian is
they're in well assuming that Brian is indeed still a web server in the store
indeed still a web server in the store and Phyllis was trying to upload a cat
and Phyllis was trying to upload a cat or maybe download a cat from Brian's web
or maybe download a cat from Brian's web server inside of phyllis's envelope
server inside of phyllis's envelope would have been a message literally in
would have been a message literally in text and it's English for the most part
text and it's English for the most part and it would contain at the beginning of
and it would contain at the beginning of that message literally one of these
that message literally one of these English verbs either get which means
English verbs either get which means just that get me the homepage get me a
just that get me the homepage get me a picture of a cat get me a picture of a
picture of a cat get me a picture of a dog or a video or anything else or post
dog or a video or anything else or post which often means post that is um put
which often means post that is um put that is um upload a picture of a cat or
that is um upload a picture of a cat or a dog or something else to the server
a dog or something else to the server instead that's not strictly the only use
instead that's not strictly the only use cases for these but you can generally
cases for these but you can generally think them as one is just getting
think them as one is just getting information and the other is posting
information and the other is posting from the client from Phyllis to the
from the client from Phyllis to the server in this case Brian so those are
server in this case Brian so those are the two keywords that we might see and
the two keywords that we might see and why is this useful well it turns out we
why is this useful well it turns out we can start to see in our own Mac or PC
can start to see in our own Mac or PC some of these very same messages for
some of these very same messages for instance if Phyllis were visiting not
instance if Phyllis were visiting not Brian's but example.com that web server
Brian's but example.com that web server inside of her metaphorical envelope
inside of her metaphorical envelope there would be a textual message that
there would be a textual message that literally starts with get slash then the
literally starts with get slash then the word HTTP then the version she's using
word HTTP then the version she's using 1.1 is very common two and three are
1.1 is very common two and three are becoming more common but HTTP generally
becoming more common but HTTP generally looks like this the next line of text in
looks like this the next line of text in her envelope would probably say host
her envelope would probably say host colon then literally the fully qualified
colon then literally the fully qualified domain name of the server she's
domain name of the server she's accessing just in case and this happens
accessing just in case and this happens super commonly especially on small
super commonly especially on small websites if one server is hosting
websites if one server is hosting multiple domain names multiple websites
multiple domain names multiple websites this just distinguishes which one she
this just distinguishes which one she actually wants and then there's usually
actually wants and then there's usually a whole bunch of other lines of text as
a whole bunch of other lines of text as well so where can you actually see this
well so where can you actually see this well let me actually go ahead and do
well let me actually go ahead and do this give me just a moment and I'm going
this give me just a moment and I'm going to open up on my computer here uh an
to open up on my computer here uh an empty Chrome window in incognito mode
empty Chrome window in incognito mode generally speaking incognito mode or
generally speaking incognito mode or private mode is used when you don't want
private mode is used when you don't want there to be left remnants of what
there to be left remnants of what websites you visited and it has the
websites you visited and it has the effect for software developers of just
effect for software developers of just forgetting any things you might have
forgetting any things you might have tried already within your browser
tried already within your browser including things called cookies more on
including things called cookies more on those another time uh your autocomplete
those another time uh your autocomplete history and the like so for development
history and the like so for development purposes incognito mode is especially
purposes incognito mode is especially helpful because it's sort of like
helpful because it's sort of like starting with a clean slate every time
starting with a clean slate every time you open a new private or incognito mode
you open a new private or incognito mode so there's not going to be like any
so there's not going to be like any remnants of previous testing or code
remnants of previous testing or code that you've been playing with and I'm
that you've been playing with and I'm going to go ahead and do this I'm going
going to go ahead and do this I'm going to go ahead and uh right click or
to go ahead and uh right click or control click on Chrome I'm going to
control click on Chrome I'm going to choose inspect and it's going to pull up
choose inspect and it's going to pull up this window sometimes on the side
this window sometimes on the side sometimes on the bottom I'm going to
sometimes on the bottom I'm going to move it to the bottom just so we can see
move it to the bottom just so we can see it a little more readily and I'm going
it a little more readily and I'm going to zoom in and it's going to look a
to zoom in and it's going to look a little Arcane at first and I'm going to
little Arcane at first and I'm going to just highlight a few of these tabs we'll
just highlight a few of these tabs we'll see here along the top that there's
see here along the top that there's elements console sources Network and
elements console sources Network and whole bunch of other things as well this
whole bunch of other things as well this is sort of the advanced mode in Chrome
is sort of the advanced mode in Chrome and Safari and Firefox and Edge have
and Safari and Firefox and Edge have their own equivalent of these features
their own equivalent of these features they've always been there even if you've
they've always been there even if you've never clicked the right button to enable
never clicked the right button to enable these features and I'm going to focus
these features and I'm going to focus for a moment on network like this
for a moment on network like this this is a feature of the browser that's
this is a feature of the browser that's going to allow me the programmer in this
going to allow me the programmer in this case so the the engineer to just kind of
case so the the engineer to just kind of look at what messages my browser is
look at what messages my browser is actually sending to a server so let me
actually sending to a server so let me go ahead and do something like this let
go ahead and do something like this let me go ahead and visit uh for instance uh
me go ahead and visit uh for instance uh in my browser here and I'm going to
in my browser here and I'm going to shrink the window just a little bit so
shrink the window just a little bit so we can see it exactly I'm going to visit
we can see it exactly I'm going to visit https uh
https uh www.harvard.edu and now I'm going to hit
www.harvard.edu and now I'm going to hit enter
enter and a whole bunch of stuff just happened
and a whole bunch of stuff just happened along the bottom of my screen and I'm
along the bottom of my screen and I'm going to try to pull my window up just a
going to try to pull my window up just a little bit so we can focus on a subset
little bit so we can focus on a subset of this let me pull this up covering up
of this let me pull this up covering up really the content of the page focusing
really the content of the page focusing on these lower level details down here
on these lower level details down here and what I want to see first
and what I want to see first is let me oh sorry let me go ahead and
is let me oh sorry let me go ahead and reload this page here after retaining
reload this page here after retaining the log so that we can see absolutely
the log so that we can see absolutely everything on the screen and to be clear
everything on the screen and to be clear I just checked because I forgot earlier
I just checked because I forgot earlier preserve log because I wanted to
preserve log because I wanted to preserve everything on the screen I want
preserve everything on the screen I want to see everything all at once and we'll
to see everything all at once and we'll see this the very first line of output
see this the very first line of output is completely overwhelming with detail
is completely overwhelming with detail at first glance but what you'll see here
at first glance but what you'll see here if I start to scroll down and down and
if I start to scroll down and down and down and down are the so-called request
down and down are the so-called request headers and let me zoom in here and what
headers and let me zoom in here and what you're seeing inside of chrome inside of
you're seeing inside of chrome inside of its Network tab in its so-called
its Network tab in its so-called developer tools again this is just for
developer tools again this is just for engineering types you'll see all of the
engineering types you'll see all of the headers all of the lines of text that
headers all of the lines of text that magically were sent by my Mac to
magically were sent by my Mac to harvard.edu much like from Phyllis to
harvard.edu much like from Phyllis to Brian server in that story so I can see
Brian server in that story so I can see exactly what messages are being sent and
exactly what messages are being sent and a lot of this we haven't talked about
a lot of this we haven't talked about yet but we do see some mention of get
yet but we do see some mention of get and we see some mention of Slash and a
and we see some mention of Slash and a bunch of other Arcane details but notice
bunch of other Arcane details but notice they're all sort of key value pairs with
they're all sort of key value pairs with the here indicating what the
the here indicating what the corresponding value is now most of this
corresponding value is now most of this is not going to be interesting and we're
is not going to be interesting and we're not going to focus too much on the weeds
not going to focus too much on the weeds of of all of this but it indeed gives us
of of all of this but it indeed gives us a sense of what's inside of that virtual
a sense of what's inside of that virtual envelope now harvard.edu is one thing
envelope now harvard.edu is one thing but there's other uh websites we might
but there's other uh websites we might visit as well um and no matter what they
visit as well um and no matter what they are we're going to expect ultimately an
are we're going to expect ultimately an HTTP response so in addition to a
HTTP response so in addition to a computer like my Mac or phyllis's
computer like my Mac or phyllis's computer sending a request containing
computer sending a request containing gets and host and those details too
gets and host and those details too you'll see here in my slide form just
you'll see here in my slide form just representative response from the server
representative response from the server and notice that key here is that the
and notice that key here is that the server is responding in the same version
server is responding in the same version of HTTP in this example it's sending
of HTTP in this example it's sending back this so-called status code just a
back this so-called status code just a numeric code that indicates in this case
numeric code that indicates in this case that everything's okay and it includes
that everything's okay and it includes this header this HTTP header which again
this header this HTTP header which again is just a key value pair saying that the
is just a key value pair saying that the type of this content that's coming back
type of this content that's coming back from the server is text/html more on
from the server is text/html more on HTML in just a little bit but for our
HTML in just a little bit but for our purposes now this just means that
purposes now this just means that harvard.edu is sending me back a web
harvard.edu is sending me back a web page and indeed if we hide all of this
page and indeed if we hide all of this techn techical stuff that's the web page
techn techical stuff that's the web page that we saw up here with all of the
that we saw up here with all of the usual imagery and the like and in fact I
usual imagery and the like and in fact I can see this if I scroll back up not to
can see this if I scroll back up not to request headers but response headers
request headers but response headers you'll see up here that we get back
you'll see up here that we get back responses including the date that the
responses including the date that the server responded and a whole bunch of
server responded and a whole bunch of other details as well and honestly this
other details as well and honestly this has always been under your fingertips
has always been under your fingertips and it will soon be useful as we start
and it will soon be useful as we start making web-based applications ultimately
making web-based applications ultimately but this very quickly gets overwhelming
but this very quickly gets overwhelming quickly and so better in this might
quickly and so better in this might actually be a tool that we can use
actually be a tool that we can use within our code space itself so let me
within our code space itself so let me go back to vs code here I didn't open
go back to vs code here I didn't open any code tabs I'm just going to use my
any code tabs I'm just going to use my terminal window for a moment and I'm
terminal window for a moment and I'm going to run a couple of commands that
going to run a couple of commands that are going to allow me to actually see
are going to allow me to actually see what is going on when I request one
what is going on when I request one website let me go ahead and use a
website let me go ahead and use a command called curl for connect URL and
command called curl for connect URL and this is like a command line black and
this is like a command line black and white program that's going to pretend to
white program that's going to pretend to be a browser and it's going to connect
be a browser and it's going to connect to the URL show me the headers but it's
to the URL show me the headers but it's not going to show me the images are the
not going to show me the images are the graphics which might very well be useful
graphics which might very well be useful to the humans but not to me right now as
to the humans but not to me right now as the developer so I'm going to do curl
the developer so I'm going to do curl I'm going to do Dashi and then I'm going
I'm going to do Dashi and then I'm going to do
to do https www.harvard.edu as though I'm
https www.harvard.edu as though I'm pretending to be a browser requesting
pretending to be a browser requesting the home page and what's nice about curl
the home page and what's nice about curl is albeit overwhelming too you'll get
is albeit overwhelming too you'll get back a whole response from the server
back a whole response from the server containing only those header values the
containing only those header values the key value pairs inside of the envelope
key value pairs inside of the envelope and we'll ignore almost all of these but
and we'll ignore almost all of these but here is the response from the server it
here is the response from the server it responded using a new and improved
responded using a new and improved version of HTTP in this case version two
version of HTTP in this case version two and it gave me back a 200 there's my
and it gave me back a 200 there's my content type text/html and then this
content type text/html and then this Char set happens to do with the encoding
Char set happens to do with the encoding if it's Unicode or asky or something
if it's Unicode or asky or something else and then there's all this other
else and then there's all this other overwhelming detail for now but this is
overwhelming detail for now but this is the beginnings of my ability to just
the beginnings of my ability to just kind of poke around and see how the
kind of poke around and see how the server works and it turns out too that
server works and it turns out too that we'll be able to see other potential
we'll be able to see other potential responses as well so for instance uh
responses as well so for instance uh HTTP might not only return 100 what if I
HTTP might not only return 100 what if I do this instead let me go ahead and
do this instead let me go ahead and visit c-i
visit c-i HTTP
HTTP colon uh www.harvard.edu so notice I
colon uh www.harvard.edu so notice I deliberately use the insecure version of
deliberately use the insecure version of the URL which maybe Harvard's
the URL which maybe Harvard's Administration system administration
Administration system administration doesn't like anymore well how can they
doesn't like anymore well how can they ensure that I the end user the student
ensure that I the end user the student nonetheless use
nonetheless use https even if I didn't type it myself
https even if I didn't type it myself well let me run just that command with
well let me run just that command with just HTTP not https and you'll see that
just HTTP not https and you'll see that everything is not okay it didn't come
everything is not okay it didn't come back with a 200 it came back with 301 in
back with a 200 it came back with 301 in this message saying Harvard moved
this message saying Harvard moved permanently but here's where you can
permanently but here's where you can look for another clue among all of these
look for another clue among all of these lines most of which I don't care about
lines most of which I don't care about there's a location header colon that's a
there's a location header colon that's a little hint to me that says where
little hint to me that says where Harvard University has apparently moved
Harvard University has apparently moved too on the web and what's different
too on the web and what's different about this URL just to be
about this URL just to be clear it has the S included and what
clear it has the S included and what your browser will do by default because
your browser will do by default because Google and Microsoft and Mozilla
Google and Microsoft and Mozilla programmed it this way whenever it sees
programmed it this way whenever it sees a 301 response instead of 200 it won't
a 301 response instead of 200 it won't show you any web page it will look for a
show you any web page it will look for a location header find that URL and then
location header find that URL and then automatically quote unquote redirect you
automatically quote unquote redirect you there to so this is why it doesn't
there to so this is why it doesn't matter what we type in the browser
matter what we type in the browser Harvard can have its server send these
Harvard can have its server send these semi-secret messages to our browsers and
semi-secret messages to our browsers and then it will just visit a second URL all
then it will just visit a second URL all automatically and you can do this with
automatically and you can do this with host names as well suppose that Harvard
host names as well suppose that Harvard does not want to standardize on
does not want to standardize on harvard.edu why they just want it to
harvard.edu why they just want it to always be www maybe it's a branding
always be www maybe it's a branding thing maybe it's a technical thing we
thing maybe it's a technical thing we can see the exact same response here
can see the exact same response here this first tells me when I visit HTTP
this first tells me when I visit HTTP harvard.edu with no www Harvard
harvard.edu with no www Harvard minimally wants me to be using a secure
minimally wants me to be using a secure connection if I then okay fine cooperate
connection if I then okay fine cooperate let me go ahead and clear my screen let
let me go ahead and clear my screen let me add the s but not the www you can see
me add the s but not the www you can see here that it again responded with 301 up
here that it again responded with 301 up here and the location now adds the www
here and the location now adds the www so it's just a way of bouncing users
so it's just a way of bouncing users from one place to another and this is
from one place to another and this is all thanks to http boiling down to
all thanks to http boiling down to relatively simple messages inside of the
relatively simple messages inside of the envelope that tell the computer the
envelope that tell the computer the browser in this case how to respond now
browser in this case how to respond now odds are you've seen others besides 301
odds are you've seen others besides 301 even though you've probably never seen
even though you've probably never seen that actual number unless you've done
that actual number unless you've done this kind of thing before but there
this kind of thing before but there probably is a number that like everyone
probably is a number that like everyone in this room has seen if if You' never
in this room has seen if if You' never really wondered why is it that number I
really wondered why is it that number I think you're smiling what number are you
think you're smiling what number are you thinking of yeah so 404 why is 404 well
thinking of yeah so 404 why is 404 well 404 indicates by convention not found
404 indicates by convention not found and now why the world decided years ago
and now why the world decided years ago to show us normal humans on the internet
to show us normal humans on the internet 404 is anything significant is unclear
404 is anything significant is unclear that's sort of like bad design like what
that's sort of like bad design like what do I care if the status code is 404 but
do I care if the status code is 404 but it's common enough on the internet that
it's common enough on the internet that probably all of us have seen it but that
probably all of us have seen it but that just means that some server when you
just means that some server when you visit a URL that's incorrect maybe it's
visit a URL that's incorrect maybe it's outdated the URL has been changed if you
outdated the URL has been changed if you see a 404 it just means that the virtual
see a 404 it just means that the virtual envelope that came from the server back
envelope that came from the server back to your Mac or PC or phone contains not
to your Mac or PC or phone contains not 200 okay not 301 moved permanently but
200 okay not 301 moved permanently but 404 not found instead and it's usually
404 not found instead and it's usually accompanied by a technical message maybe
accompanied by a technical message maybe a cute picture of a cat sort of hiding
a cute picture of a cat sort of hiding because it means not found or something
because it means not found or something like that the Aesthetics are entirely up
like that the Aesthetics are entirely up to the server but that's what the 404
to the server but that's what the 404 means and there's other codes too a few
means and there's other codes too a few of which you'll use in the coming weeks
of which you'll use in the coming weeks as we transition from commandline
as we transition from commandline programs in C and python to web-based
programs in C and python to web-based ones we've seen a few of these already
ones we've seen a few of these already and several of them are kind of variants
and several of them are kind of variants of the same thing like these 300 ones
of the same thing like these 300 ones here but we'll see others like 403
here but we'll see others like 403 Forbidden means like you probably forgot
Forbidden means like you probably forgot to log in or you need to log in or
to log in or you need to log in or something like that uh internal server
something like that uh internal server error right now no in the coming weeks
error right now no in the coming weeks you will absolutely encounter 500 errors
you will absolutely encounter 500 errors and they're always your fault it's going
and they're always your fault it's going to be the equivalent of like a
to be the equivalent of like a segmentation fault in C a 500 error
segmentation fault in C a 500 error means like you or I screwed up when
means like you or I screwed up when writing some code so we're going to see
writing some code so we're going to see that but it's just going to be an
that but it's just going to be an opportunity for us to fix it if a
opportunity for us to fix it if a server's overloaded you often see 503
server's overloaded you often see 503 like something's unavailable because
like something's unavailable because something's too popular uh or is maybe
something's too popular uh or is maybe worse getting attacked um this is an old
worse getting attacked um this is an old uh um April Fool's joke 418 is not
uh um April Fool's joke 418 is not actually used in practice but someone
actually used in practice but someone like took the time to write up an entire
like took the time to write up an entire formal text technical proposal so that
formal text technical proposal so that servers can respond saying I'm a teapot
servers can respond saying I'm a teapot so it's kind of part of Internet lore
so it's kind of part of Internet lore and there's other ones of these status
and there's other ones of these status codes as well but this is useful because
codes as well but this is useful because eventually we'll see in code you can use
eventually we'll see in code you can use this understanding uh high level as it
this understanding uh high level as it might be of HTTP to do some interesting
might be of HTTP to do some interesting and Powerful things so for instance we
and Powerful things so for instance we can even send fancier URLs to servers it
can even send fancier URLs to servers it turns out as we'll soon see if you send
turns out as we'll soon see if you send a message like this get/ search question
a message like this get/ search question question mark Q equals cats and then
question mark Q equals cats and then HTTP 1.1 or whatever version and you
HTTP 1.1 or whatever version and you send that message to Google server
send that message to Google server www.google.com this is how you can
www.google.com this is how you can specify not just the path of a web page
specify not just the path of a web page that you want SL search in this case the
that you want SL search in this case the question mark it turns out is going to
question mark it turns out is going to be a convention in the internet in the
be a convention in the internet in the web specifically for passing hum human
web specifically for passing hum human uh user input to the server as well in
uh user input to the server as well in fact you've probably never paid close
fact you've probably never paid close attention to URLs but they very often
attention to URLs but they very often have question question marks they very
have question question marks they very often have equal signs and indeed even
often have equal signs and indeed even google.com supports a certain key Q in
google.com supports a certain key Q in this case for query and you can put
this case for query and you can put anything you want after that in order to
anything you want after that in order to search for actual cats so if I actually
search for actual cats so if I actually go back to Chrome itself here for a
go back to Chrome itself here for a moment let me pull back my uh pull back
moment let me pull back my uh pull back open my Chrome browser here previously I
open my Chrome browser here previously I was using uh incognito mode for
was using uh incognito mode for harvard.edu I've gone ahead and Clos
harvard.edu I've gone ahead and Clos that window and opened a new one so we
that window and opened a new one so we can start fresh by visiting Google
can start fresh by visiting Google normally you and I are in the habit of
normally you and I are in the habit of going to google.com and searching via
going to google.com and searching via the form or nowadays you just type like
the form or nowadays you just type like your search query in the browser itself
your search query in the browser itself and it brings you automatically to
and it brings you automatically to Google or Bing or something else but I
Google or Bing or something else but I can really be pedantic here let me go
can really be pedantic here let me go ahead and zoom in and I'll manually go
ahead and zoom in and I'll manually go to
to https
https www.google.com search question mark Q
www.google.com search question mark Q equals maybe cats now this would not be
equals maybe cats now this would not be a very userfriendly experience if all of
a very userfriendly experience if all of us had to manually type out something
us had to manually type out something crazy like that but that's what the form
crazy like that but that's what the form is redirecting you to when you type in
is redirecting you to when you type in more user friendly cats into like a text
more user friendly cats into like a text box if I hit enter here we'll get back
box if I hit enter here we'll get back indeed a whole bunch of search results
indeed a whole bunch of search results about cats if I zoom back in and maybe I
about cats if I zoom back in and maybe I change it from cats to dogs that too is
change it from cats to dogs that too is going to change and notice it's
going to change and notice it's pre-populating the text box because
pre-populating the text box because Google has written its code in order to
Google has written its code in order to do so as well now appr propo of the
do so as well now appr propo of the video with which we began today from
video with which we began today from yester year one of the better uh Yale
yester year one of the better uh Yale pranks over the years um has anyone one
pranks over the years um has anyone one actually ever been to uh
actually ever been to uh safetyschool.org
safetyschool.org and to our friends at Yale watching live
and to our friends at Yale watching live hi safetyschool.org so it's kind of fun
hi safetyschool.org so it's kind of fun if you actually visit it uh depending on
if you actually visit it uh depending on who you are so if I open up a new window
who you are so if I open up a new window here and I go to
here and I go to https
https safetyschool.org zooming
safetyschool.org zooming in enter my oh my look at where it goes
in enter my oh my look at where it goes now how does that okay okay thank
you this is not a cs50 thing someone out there who I don't know who they are for
there who I don't know who they are for like 20 years has been paying annually
like 20 years has been paying annually for that domain safetyschool.org for
for that domain safetyschool.org for just this joke uh but if I now go back
just this joke uh but if I now go back into VSS code here in my terminal window
into VSS code here in my terminal window and a little more you know
and a little more you know sophisticatedly let me do curl dasi HTTP
sophisticatedly let me do curl dasi HTTP col andsafety
col andsafety school.org the browser of course
school.org the browser of course immediately redirected me to the website
immediately redirected me to the website but what's going on under underneath the
but what's going on under underneath the hood well 20 some years ago someone
hood well 20 some years ago someone bought the domain configured the server
bought the domain configured the server quite simply to spit this out safety
quite simply to spit this out safety school.org for years has moved
school.org for years has moved permanently to
permanently to www.yale.edu so just a little
www.yale.edu so just a little demonstration of what you can do with
demonstration of what you can do with just a little bit of understanding of
just a little bit of understanding of HTTP that's been a lot already we've
HTTP that's been a lot already we've laid the foundation for understanding
laid the foundation for understanding how the internet works now we're going
how the internet works now we're going to use it with a language called HTML
to use it with a language called HTML hypertext markup language CSS cascading
hypertext markup language CSS cascading stylesheets and JavaScript a proper
stylesheets and JavaScript a proper program in language but first it feels
program in language but first it feels opportune to take a 10-minute break we
opportune to take a 10-minute break we have some wonderful Halloween candy in
have some wonderful Halloween candy in the transcept and we'll be back in 10
the transcept and we'll be back in 10 with those
with those languages we're headed up to uh
languages we're headed up to uh Austin checking out the stadium for the
prank a few years ago I was at a math conference and I was sitting around at
conference and I was sitting around at the table at dinner with a few other
the table at dinner with a few other mathematicians and uh one of them went
mathematicians and uh one of them went to Harvard and started telling the story
to Harvard and started telling the story of this amazing prank that was you know
of this amazing prank that was you know against Harvard and at that point I felt
against Harvard and at that point I felt I had to interrupt and said well
I had to interrupt and said well actually I can tell you a lot more about
actually I can tell you a lot more about that okay okay the idea was perfected in
that okay okay the idea was perfected in a dorm room came up with the idea
a dorm room came up with the idea actually to prank them with signs at the
actually to prank them with signs at the football game we threw some ideas out
football game we threw some ideas out there as far as what what the signs
there as far as what what the signs would say we uh eventually settled on we
would say we uh eventually settled on we suck and my immediate reaction was no
suck and my immediate reaction was no this will never work however the problem
this will never work however the problem solver in me started thinking well maybe
solver in me started thinking well maybe we can make this
we can make this work the problem they had to infiltrate
work the problem they had to infiltrate Harvard Stadium without getting caught
Harvard Stadium without getting caught sneak in 1,800 placards distribute them
sneak in 1,800 placards distribute them to unsuspecting Harvard fans and then
to unsuspecting Harvard fans and then convince those fans to prank themselves
convince those fans to prank themselves it's great we thought about basically
it's great we thought about basically every possible thing that could go wrong
every possible thing that could go wrong and tried to come up with a solution for
and tried to come up with a solution for it and then you put two Reds on top of
it and then you put two Reds on top of it they made fake Harvard IDs and fake
it they made fake Harvard IDs and fake backstories
backstories fake placard designs and a 28 member
fake placard designs and a 28 member fake Pep
fake Pep Squad on November 20th 2004 a fake
Squad on November 20th 2004 a fake Harvard student smuggled the placards
Harvard student smuggled the placards into the game what do you think
[Music] [Applause]
[Applause] of but then trouble what houses
of but then trouble what houses how many how many extra
are I you know just showed him the front of this ID and all of a sudden he just
of this ID and all of a sudden he just ran away and he felt so
ran away and he felt so embarrassed having escaped one
embarrassed having escaped one confrontation they couldn't risk another
confrontation they couldn't risk another it was
it was time this just looks like a total mess
time this just looks like a total mess we have absolutely no idea if this is
we have absolutely no idea if this is going to work look at the it's going to
going to work look at the it's going to happen it's actually going to
happen it's actually going to happen I can't believe this what was
happen I can't believe this what was once a
once a prank became a
legend and immediately we started hearing chance from the other side
you and I think it was at that point in time that we knew we had pulled it
time that we knew we had pulled it off on
off on there it goes
there it goes [Applause]
[Applause] again I really think it didn't matter
again I really think it didn't matter that Harvard won because of the prank
that Harvard won because of the prank for a lot of Yale students in alumni we
for a lot of Yale students in alumni we definitely won that
definitely won that [Music]
Hallen thank you all okay thank you oh here oh all right little Halloween candy
here oh all right little Halloween candy all right thank you all so glad I wore
all right thank you all so glad I wore the same thing
the same thing today so in just a
today so in just a moment thank you okay so in just a
moment thank you okay so in just a moment we'll transition to understanding
moment we'll transition to understanding all the now what we can do with this
all the now what we can do with this underlying infrastructure so again HTTP
underlying infrastructure so again HTTP and below it TCP IP is all about just
and below it TCP IP is all about just getting the data from point A to pointb
getting the data from point A to pointb in some standardized way but now let's
in some standardized way but now let's talk about HTML this is the language in
talk about HTML this is the language in which web pages themselves are written
which web pages themselves are written hyper text markup language now some of
hyper text markup language now some of you might have used this before to make
you might have used this before to make personal homepages some of you might
personal homepages some of you might have dabbled even if using some website
have dabbled even if using some website to create your own homepage but
to create your own homepage but understanding this language is useful
understanding this language is useful certainly for creating the Aesthetics of
certainly for creating the Aesthetics of a web page can vying data that's of
a web page can vying data that's of interest but at the end of the day it
interest but at the end of the day it and the language we look at next CSS uh
and the language we look at next CSS uh are not programming languages there's
are not programming languages there's going to be no functions no Loops uh no
going to be no functions no Loops uh no programming logic but we will end today
programming logic but we will end today with a teaser of a proper programming
with a teaser of a proper programming language called JavaScript via which you
language called JavaScript via which you can manipulate all of these various uh
can manipulate all of these various uh other languages as well so ultimately
other languages as well so ultimately HTML has like two features and this is a
HTML has like two features and this is a language that we spend very brief amount
language that we spend very brief amount of time on because it really boils down
of time on because it really boils down to just a couple of basic ideas and then
to just a couple of basic ideas and then vocabulary that you'll build out over
vocabulary that you'll build out over time just by Googling looking up
time just by Googling looking up references looking at other Pages source
references looking at other Pages source code but tags and attributes are what
code but tags and attributes are what characterize HTML now what do I mean by
characterize HTML now what do I mean by that here for instance is the HTML code
that here for instance is the HTML code via which you can make probably the
via which you can make probably the simplest of all web pages one that quite
simplest of all web pages one that quite simply says in the uh browser window
simply says in the uh browser window hello title and hello body for instance
hello title and hello body for instance now what does this actually mean if you
now what does this actually mean if you imagine opening up uh this code in a
imagine opening up uh this code in a browser be it on a Mac or PC or phone
browser be it on a Mac or PC or phone you'll see typically like some kind of
you'll see typically like some kind of rectangular window and there's usually a
rectangular window and there's usually a tab that has the title of that page and
tab that has the title of that page and then most of the rectangular region is
then most of the rectangular region is the web page itself what you're looking
the web page itself what you're looking at then is the code that's going to put
at then is the code that's going to put hello title in the title bar in the tab
hello title in the title bar in the tab at the very top and down at the bottom
at the very top and down at the bottom hello body is going to be all that's in
hello body is going to be all that's in the big black and white box that
the big black and white box that composes the rest of the browser window
composes the rest of the browser window itself now what are the Salient
itself now what are the Salient characteristics here that we'll now
characteristics here that we'll now start to take for granted well first
start to take for granted well first whoops uh first let's go ahead and give
whoops uh first let's go ahead and give me just a moment here um and actually do
me just a moment here um and actually do something with this code so I'm going to
something with this code so I'm going to go ahead and do this back in vs code
go ahead and do this back in vs code here I'm going to first create a file
here I'm going to first create a file called say hello.html and in this tab
called say hello.html and in this tab I'm going to go ahead and really repeat
I'm going to go ahead and really repeat exactly that same code now I had this
exactly that same code now I had this line first DOC type HTML then I had this
line first DOC type HTML then I had this line HTML Lang equals quote unquote n
line HTML Lang equals quote unquote n close quote then I had inside of that
close quote then I had inside of that head then I had inside of that title
head then I had inside of that title then inside of that I had hello title
then inside of that I had hello title and I'm doing this quickly because we'll
and I'm doing this quickly because we'll tease apart in a moment what it actually
tease apart in a moment what it actually all means and then down here below that
all means and then down here below that so-called head I had just the text hello
so-called head I had just the text hello body so at the moment I that I claim is
body so at the moment I that I claim is the entirety of a web page but it
the entirety of a web page but it currently lives in my code space so to
currently lives in my code space so to speak in a file called hello.html that's
speak in a file called hello.html that's fine if I want to create it but how do I
fine if I want to create it but how do I how do you how does anyone on the
how do you how does anyone on the internet actually view it well to serve
internet actually view it well to serve a web page you indeed need a web server
a web page you indeed need a web server and it turns out that codespaces comes
and it turns out that codespaces comes with one of these pre-installed because
with one of these pre-installed because we cs50 staff uh did so for you and what
we cs50 staff uh did so for you and what you can do in a terminal window once you
you can do in a terminal window once you have an HTML file ready to go that you
have an HTML file ready to go that you want the world to see you can literally
want the world to see you can literally run in your terminal window
run in your terminal window http-server single command and what
http-server single command and what that's going to do for you is start a
that's going to do for you is start a web server that is to say a program
web server that is to say a program whose purpose in life is just to serve
whose purpose in life is just to serve web pages and even though probably up
web pages and even though probably up until now for years you probably if
until now for years you probably if you're like me equate server quote
you're like me equate server quote unquote with a physical device server is
unquote with a physical device server is really aie piece of software it just
really aie piece of software it just tends to run on big fancy devices so
tends to run on big fancy devices so when we say server we often all think of
when we say server we often all think of in our Mind's Eye you know big expensive
in our Mind's Eye you know big expensive devices perhaps but a server is just a
devices perhaps but a server is just a program whose purpose in life is to
program whose purpose in life is to respond to requests with responses and
respond to requests with responses and that's the vernacular there now once you
that's the vernacular there now once you run HTTP server and I'm going to do a
run HTTP server and I'm going to do a bit of magic because I set this up
bit of magic because I set this up before class just to make sure it goes
before class just to make sure it goes smoothly you'll see some output like
smoothly you'll see some output like this whereby your server is now
this whereby your server is now available on a very long URL mine here
available on a very long URL mine here uh uh is a very long URL that will be
uh uh is a very long URL that will be different from yours but what this is is
different from yours but what this is is a unique identifier that your codes
a unique identifier that your codes space has temporarily generated so that
space has temporarily generated so that you can now access and ideally only you
you can now access and ideally only you can access that file using your browser
can access that file using your browser now if I flip the URL or you flip the
now if I flip the URL or you flip the your all to public by right clicking or
your all to public by right clicking or control clicking the right features of
control clicking the right features of vs code you can enable anyone in the
vs code you can enable anyone in the world to visit it but we're not going to
world to visit it but we're not going to ultimately host our websites in your
ultimately host our websites in your code space because as soon as you log
code space because as soon as you log off for the night and the thing shuts
off for the night and the thing shuts down like the website will go down but
down like the website will go down but at the end end of the semester
at the end end of the semester particularly for final projects we'll
particularly for final projects we'll show you ways that you can put your own
show you ways that you can put your own website your own code on the actual
website your own code on the actual internet
internet 247365 even with your own domain name if
247365 even with your own domain name if you want to get one so that it lives uh
you want to get one so that it lives uh independent of your own sleep schedule
independent of your own sleep schedule and usage schedule of vs code here so
and usage schedule of vs code here so I'm going to go ahead now and visit um
I'm going to go ahead now and visit um this URL in another tab of my browser
this URL in another tab of my browser and what I'll see here is this this is
and what I'll see here is this this is the output of that program called HTTP
the output of that program called HTTP server and essentially what it is doing
server and essentially what it is doing is it's using TCP and IP in conjunction
is it's using TCP and IP in conjunction with HTTP to just run your very own web
with HTTP to just run your very own web server on gith hubs own servers as well
server on gith hubs own servers as well and that's because of different ports
and that's because of different ports again we won't go too much into the
again we won't go too much into the weeds of the TCP the IP and all of that
weeds of the TCP the IP and all of that stuff but recall that different port
stuff but recall that different port numbers can allow you to distinguish one
numbers can allow you to distinguish one service from another now one of the
service from another now one of the services is of course your code space
services is of course your code space VSS code in the cloud that we've been
VSS code in the cloud that we've been using for weeks but if you want to use
using for weeks but if you want to use the same physical server that GitHub
the same physical server that GitHub controls but actually visit your own web
controls but actually visit your own web server that I just ran in my terminal
server that I just ran in my terminal window in another tab that's fine
window in another tab that's fine they're just going to be using different
they're just going to be using different TCP ports and you and I don't have to
TCP ports and you and I don't have to care what they are but just that this is
care what they are but just that this is a feature that TCP supports so what you
a feature that TCP supports so what you see here is somewhat Arcane this is not
see here is somewhat Arcane this is not like a thing that most people on the
like a thing that most people on the internet should ever see I'm just doing
internet should ever see I'm just doing this for development purposes but this
this for development purposes but this is the index that is the directory the
is the index that is the directory the the folder contents of my code space and
the folder contents of my code space and because I deleted everything from prior
because I deleted everything from prior weeks already all we see right now is
weeks already all we see right now is hello.html
hello.html which I just created so if I click on
which I just created so if I click on hello.html within this folder listing
hello.html within this folder listing you'll be a little underwhelmed and I'll
you'll be a little underwhelmed and I'll zoom in just so there's something more
zoom in just so there's something more interesting there but now you see hello
interesting there but now you see hello comma body but what's interesting
comma body but what's interesting perhaps is that after this long very
perhaps is that after this long very cryptic and uninteresting URL notice at
cryptic and uninteresting URL notice at the very end of it and I'll zoom in in a
the very end of it and I'll zoom in in a moment what do you see slh hello.html
moment what do you see slh hello.html which follows the convention I claimed
which follows the convention I claimed before break is how a browser would
before break is how a browser would allow you to to access a specific file
allow you to to access a specific file on a server by doing slash and then the
on a server by doing slash and then the name of the file name everything before
name of the file name everything before it is very cryptic it would be better if
it is very cryptic it would be better if I you know uh buy a domain name that's a
I you know uh buy a domain name that's a little more easy to remember and set
little more easy to remember and set that up some other time but for now
that up some other time but for now let's just focus on only the file names
let's just focus on only the file names that I'm actually creating all right so
that I'm actually creating all right so the code is up and running underwhelming
the code is up and running underwhelming though it might be with the body in the
though it might be with the body in the middle of the page and let me zoom in up
middle of the page and let me zoom in up here too hello comma title is indeed in
here too hello comma title is indeed in the tab just as promised so what's
the tab just as promised so what's actually going on with code like this
actually going on with code like this that we just created well let's go back
that we just created well let's go back to the slide version of the same and let
to the slide version of the same and let me just highlight a few of these lines
me just highlight a few of these lines the very first line is what's called
the very first line is what's called your document type declaration doesn't
your document type declaration doesn't really matter to remember that by
really matter to remember that by phrasing and this is just something you
phrasing and this is just something you copy paste or do from memory at the top
copy paste or do from memory at the top of any HTML file that you create when
of any HTML file that you create when making your own web page it's a implicit
making your own web page it's a implicit indicator to the browser that you're
indicator to the browser that you're using the very latest version of HTML
using the very latest version of HTML which is version five you don't mention
which is version five you don't mention the number five just browsers now
the number five just browsers now nowadays are program to look for this to
nowadays are program to look for this to know that you're using the very latest
know that you're using the very latest version of the language languages just
version of the language languages just like human languages evolve over time
like human languages evolve over time we're up to version five of HTML but new
we're up to version five of HTML but new features get added every few years so
features get added every few years so indeed this lecture this class has been
indeed this lecture this class has been evolving over time too so let's now
evolving over time too so let's now focus on the next line as well as the
focus on the next line as well as the bottom line and you'll notice some
bottom line and you'll notice some deliberate symmetry here this here is
deliberate symmetry here this here is what we're going to call a tag and it's
what we're going to call a tag and it's technically different from this this is
technically different from this this is a document type declaration it's got the
a document type declaration it's got the weird exclamation point that's the only
weird exclamation point that's the only anomaly everything else follows pattern
anomaly everything else follows pattern this is a tag in HTML and it's the HTML
this is a tag in HTML and it's the HTML tag and a tag generally both starts and
tag and a tag generally both starts and stops or opens and closes at some point
stops or opens and closes at some point so this is the so-called start tag or
so this is the so-called start tag or open tag and this just means essentially
open tag and this just means essentially to the browser hey browser here comes
to the browser hey browser here comes some HTML the language in which web
some HTML the language in which web pages are written this here with the
pages are written this here with the forward slash after the angled bracket
forward slash after the angled bracket means hey browser that's it for the HTML
means hey browser that's it for the HTML of this page so that's what I mean by
of this page so that's what I mean by symmetry I started a thought here
symmetry I started a thought here finished it down here what's in between
finished it down here what's in between those two thoughts well here browser or
those two thoughts well here browser or rather let me clarify one thing this
rather let me clarify one thing this thing here is that other uh keyword an
thing here is that other uh keyword an attribute an attribute is something that
attribute an attribute is something that modifies the behavior of a tag so it's
modifies the behavior of a tag so it's similar to an argument in C or in Python
similar to an argument in C or in Python like a parameter to a function these
like a parameter to a function these aren't functions but it's the same idea
aren't functions but it's the same idea it just modifies the default behavior of
it just modifies the default behavior of something instead Lang equals you can
something instead Lang equals you can probably guess just means that hey
probably guess just means that hey browser assume that everything Hereafter
browser assume that everything Hereafter is in English and that might be useful
is in English and that might be useful for like Google translate or just search
for like Google translate or just search engine optimization so that just the
engine optimization so that just the server the browser know like what human
server the browser know like what human language you have actual content in like
language you have actual content in like hello title hello body even though a
hello title hello body even though a good computer can probably infer from
good computer can probably infer from Context often all right so that's an
Context often all right so that's an attribute that's a tag and the whole
attribute that's a tag and the whole thing here everything in between the
thing here everything in between the start tag and end tag we would also call
start tag and end tag we would also call an HTML element that just means
an HTML element that just means everything related to that open and
everything related to that open and close tag all right now notice indented
close tag all right now notice indented inside of so to speak the HTML open and
inside of so to speak the HTML open and close tag are another pair of tags the
close tag are another pair of tags the head tag and the body tag or the head
head tag and the body tag or the head element collectively and the body
element collectively and the body element collectively and same idea hey
element collectively and same idea hey browser here comes the head of my page
browser here comes the head of my page hey browser that's it for the head hey
hey browser that's it for the head hey browser here comes the body of my page
browser here comes the body of my page hey browser that's it for the body the
hey browser that's it for the body the head is essentially the tiny little
head is essentially the tiny little strip at the very top including the tab
strip at the very top including the tab itself the body is like 95% of
itself the body is like 95% of everything else the big rectangular
everything else the big rectangular region what's inside of your title at
region what's inside of your title at the at of your head of the web page at
the at of your head of the web page at the moment just the title so this
the moment just the title so this indicates hey browser Here Comes My
indicates hey browser Here Comes My Title hey browser that's it for the
Title hey browser that's it for the title the title of course is literally
title the title of course is literally quote unquote hello comma title
quote unquote hello comma title meanwhile if we bounce back out here is
meanwhile if we bounce back out here is the uh second element inside of the HTML
the uh second element inside of the HTML tag uh this says Hey browser here comes
tag uh this says Hey browser here comes the body hey browser that's it for the
the body hey browser that's it for the body and hey browser this is the
body and hey browser this is the contents these are the contents of the
contents these are the contents of the body itself now the indentation is a
body itself now the indentation is a stylistic thing I did it just to be sort
stylistic thing I did it just to be sort of neat and en TIY because it suggests
of neat and en TIY because it suggests what is inside of what but it also
what is inside of what but it also suggests a sort of hierarchy and in fact
suggests a sort of hierarchy and in fact we'll use terminology from like the
we'll use terminology from like the world of family trees if this is like a
world of family trees if this is like a parent so to speak head and body would
parent so to speak head and body would be the child elements of the HTML tag
be the child elements of the HTML tag meanwhile title is a child of the head
meanwhile title is a child of the head tag or equivalently tital is a
tag or equivalently tital is a grandchild of HTML so you can use the
grandchild of HTML so you can use the same sort of vernacular as in the human
same sort of vernacular as in the human world when it comes to uh familial
world when it comes to uh familial relationships too and that just H set
relationships too and that just H set again the same hierarchy so we have tags
again the same hierarchy so we have tags and they include HTML head title body
and they include HTML head title body and that's it for now we have attributes
and that's it for now we have attributes we've seen one example of them Lang but
we've seen one example of them Lang but we'll see many other examples of the
we'll see many other examples of the same idea but these building blocks are
same idea but these building blocks are exactly the same generally you start a
exactly the same generally you start a thought you finish a thought and you
thought you finish a thought and you might do something in between questions
might do something in between questions on this basic structure of any web
on this basic structure of any web page any questions at
page any questions at all no all right so let's now now allow
all no all right so let's now now allow things to ramp up a little more
things to ramp up a little more interestingly and do something with
interestingly and do something with these building blocks but so that you
these building blocks but so that you have a mental model for everything
have a mental model for everything that's going on here on after think of
that's going on here on after think of this same HTML being related in spirit
this same HTML being related in spirit to week five when we talked all about
to week five when we talked all about data structures if I really wanted to I
data structures if I really wanted to I could take to heart this idea of
could take to heart this idea of children and parents and grandparents
children and parents and grandparents and really depict this thing graphically
and really depict this thing graphically and in fact this tree here if you will
and in fact this tree here if you will and it's not a binary tree it's not a
and it's not a binary tree it's not a binary search tree it's just a tree uh
binary search tree it's just a tree uh used week five's terminology if this
used week five's terminology if this special node here represents the whole
special node here represents the whole document well the root element as I
document well the root element as I called it is HTML HTML has two children
called it is HTML HTML has two children head and body the head tag has in turn a
head and body the head tag has in turn a title uh child and in turn has some text
title uh child and in turn has some text just as the body has some text and so
just as the body has some text and so this is what your browser is doing you
this is what your browser is doing you and I the programmers write this stuff
and I the programmers write this stuff the browser reads this code top to
the browser reads this code top to bottom left to right whenever you visit
bottom left to right whenever you visit a website and inside of the computer's
a website and inside of the computer's memory Chrome Edge Firefox Safari what
memory Chrome Edge Firefox Safari what what not they build this data structure
what not they build this data structure in the computer's memory so as to know
in the computer's memory so as to know what it is you have told them to do and
what it is you have told them to do and we'll see over time at the end of today
we'll see over time at the end of today you can write code in an actual
you can write code in an actual programming language JavaScript to maybe
programming language JavaScript to maybe dynamically add or remove things from
dynamically add or remove things from this tree and this is how things like
this tree and this is how things like Gmail work when you open up your Gmail
Gmail work when you open up your Gmail inbox if you're a Gmail user if you just
inbox if you're a Gmail user if you just stay there long enough you'll probably
stay there long enough you'll probably get more and more mail and what happens
get more and more mail and what happens you don't have to like reload the page
you don't have to like reload the page or rebuild the tree per se it just all
or rebuild the tree per se it just all of a sudden appears at the top
of a sudden appears at the top at the top at the top what's happening
at the top at the top what's happening there is that Google wrote some code
there is that Google wrote some code that just keeps adding more nodes to
that just keeps adding more nodes to this tree every time they realize you've
this tree every time they realize you've got a new message again and again so
got a new message again and again so that's the relationship now even with
that's the relationship now even with this world of HTML with all of the
this world of HTML with all of the programmatic ideas we looked at in the
programmatic ideas we looked at in the past all right so let's go ahead and do
past all right so let's go ahead and do something with this that's a little more
something with this that's a little more interesting than just hello itself I'm
interesting than just hello itself I'm going to go ahead and hide my terminal
going to go ahead and hide my terminal window because the server is now running
window because the server is now running and all I want to do now is experiment
and all I want to do now is experiment with uh hello and other examples as well
with uh hello and other examples as well let me go ahead and actually before I do
let me go ahead and actually before I do that let me go ahead and run uh code of
that let me go ahead and run uh code of paragraphs. HTML just so I can keep my
paragraphs. HTML just so I can keep my code separate and now I'll hide the
code separate and now I'll hide the terminal window again um paragraphs.
terminal window again um paragraphs. HTML I'm going to do almost exactly the
HTML I'm going to do almost exactly the same let me go ahead and start with
same let me go ahead and start with something familiar and eventually I'll
something familiar and eventually I'll start copying and pasting just to save
start copying and pasting just to save time so doc type HTML is always there
time so doc type HTML is always there open the HTML tag and now notice I
open the HTML tag and now notice I didn't type the rest of that just like
didn't type the rest of that just like with C just like with python we try to
with C just like with python we try to save you some keystrokes by closing
save you some keystrokes by closing parentheses adding quotes the HTML
parentheses adding quotes the HTML support in VSS code is pretty good too
support in VSS code is pretty good too and it tries to finish your thought when
and it tries to finish your thought when it comes to tags as well it can screw
it comes to tags as well it can screw things up if you if it does something
things up if you if it does something you don't want it to do so sometimes you
you don't want it to do so sometimes you have to delete but it's just
have to delete but it's just autocomplete as we've seen before uh
autocomplete as we've seen before uh let's go ahead and let me add Lang
let's go ahead and let me add Lang equals as all of my examples today will
equals as all of my examples today will be let's add the head tag let's go and
be let's add the head tag let's go and proactively add the body tag and now
proactively add the body tag and now let's go ahead and give this a title tag
let's go ahead and give this a title tag uh which has a I'll just call this
uh which has a I'll just call this paragraphs just so I remember which
paragraphs just so I remember which example is what now notice all of this
example is what now notice all of this white space and all of this neat and
white space and all of this neat and tidy indentation the browser ultimately
tidy indentation the browser ultimately is not going to care about this is just
is not going to care about this is just for us humans to kind of keep ourselves
for us humans to kind of keep ourselves saying when we look at the code it's
saying when we look at the code it's just easier to read but strictly
just easier to read but strictly speaking I could minimally delete all of
speaking I could minimally delete all of this white space and I could just move
this white space and I could just move all of this tag up to the same line both
all of this tag up to the same line both I think are fine I'll just going to
I think are fine I'll just going to follow a certain convention but this too
follow a certain convention but this too would have the exact same meaning but
would have the exact same meaning but we'll see where that detail about whites
we'll see where that detail about whites space could potentially get us into
space could potentially get us into trouble later in my paragraphs tag let's
trouble later in my paragraphs tag let's do this in advance I've written up some
do this in advance I've written up some Latin like text a really long paragraph
Latin like text a really long paragraph of Latin like text like this it's
of Latin like text like this it's actually random nonsense it's not real
actually random nonsense it's not real Latin even though a couple of the words
Latin even though a couple of the words might look familiar and so here we have
might look familiar and so here we have three paragraphs of text and I've
three paragraphs of text and I've deliberately hit enter in between them
deliberately hit enter in between them so that just like an essay in Google
so that just like an essay in Google Docs or Microsoft Word hopefully I'll
Docs or Microsoft Word hopefully I'll see three separate paragraphs let me now
see three separate paragraphs let me now change tabs and I'll close hello.html
change tabs and I'll close hello.html from before I'm going to go back to my
from before I'm going to go back to my other tab here I'm going to click back
other tab here I'm going to click back to go back to that index of all of my
to go back to that index of all of my files which I started at earlier and
files which I started at earlier and you'll see now that I have two files
you'll see now that I have two files because I obviously just created a
because I obviously just created a second file called paragraphs. HTML so
second file called paragraphs. HTML so let's click on this to see our three
let's click on this to see our three paragraphs of Latin like text and voila
paragraphs of Latin like text and voila I'll zoom out all right first bug if you
I'll zoom out all right first bug if you will this just looks like one massive
will this just looks like one massive blob of text not three Blobs of text and
blob of text not three Blobs of text and why might that be borrowing the the hint
why might that be borrowing the the hint I offered a moment
I offered a moment ago why are we not seeing break yeah so
ago why are we not seeing break yeah so we need some kind of line breaks here
we need some kind of line breaks here because the browser turns out is only
because the browser turns out is only going to take us literally and if you
going to take us literally and if you just give it text text text it's just
just give it text text text it's just going to show you text and anytime
going to show you text and anytime there's more than a single white space
there's more than a single white space whether it's two or 20 or 200 it's going
whether it's two or 20 or 200 it's going to just assume that you did that just to
to just assume that you did that just to be neat and tidy and it's going to
be neat and tidy and it's going to collapse them into just one space
collapse them into just one space visually like this so there are in fact
visually like this so there are in fact a couple of solutions one is this here I
a couple of solutions one is this here I could add some explicit line breaks and
could add some explicit line breaks and it turns out that there's a br tag like
it turns out that there's a br tag like this and just for uh visibility sake let
this and just for uh visibility sake let me do two of them so like hitting enter
me do two of them so like hitting enter enter on my keyboard I'll do it here too
enter on my keyboard I'll do it here too BR for break break and now let me go
BR for break break and now let me go back to my other tab nothing's changed
back to my other tab nothing's changed yet but that's because I have to reload
yet but that's because I have to reload I've changed it on the server but now I
I've changed it on the server but now I need to change it in the browser by
need to change it in the browser by reloading and now it looks a little
reloading and now it looks a little better albeit nonsensical but you'll
better albeit nonsensical but you'll know is a curiosity per this BR tag this
know is a curiosity per this BR tag this is kind of poorly designed it's a little
is kind of poorly designed it's a little hackish to just say enter enter and make
hackish to just say enter enter and make the browser do this breaks line breaks
the browser do this breaks line breaks don't actually require Clos tags or end
don't actually require Clos tags or end tags so not all tags need to be closed
tags so not all tags need to be closed at least those that it just makes no
at least those that it just makes no semantic sense to close them right like
semantic sense to close them right like the break is there or it's not you can't
the break is there or it's not you can't imagine like starting to move to the
imagine like starting to move to the next line and then eventually getting
next line and then eventually getting around to finishing like it's either
around to finishing like it's either there or it's not so some tags do not
there or it's not so some tags do not have closed tags as necessary but
have closed tags as necessary but there's a more elegant way here I dare
there's a more elegant way here I dare say not just sort of hackish putting in
say not just sort of hackish putting in these line breaks let me do this instead
these line breaks let me do this instead I'll delete those and let me go ahead
I'll delete those and let me go ahead and as the name of this file suggests
and as the name of this file suggests let me add a paragraph tag now here I
let me add a paragraph tag now here I need to fight with vs codes autocomplete
need to fight with vs codes autocomplete because I don't want to finish the
because I don't want to finish the sentence uh the thought there let me go
sentence uh the thought there let me go ahead and open the paragraph tag and
ahead and open the paragraph tag and close a paragraph tag and just to keep
close a paragraph tag and just to keep things tidy I'll go ahead and indent two
things tidy I'll go ahead and indent two even though the indentation itself
even though the indentation itself doesn't matter let me go ahead and
doesn't matter let me go ahead and create another tag for opening this
create another tag for opening this paragraph and I'll close this one here
paragraph and I'll close this one here and now let me see sometimes it's
and now let me see sometimes it's fighting with my auto complete but
fighting with my auto complete but that's fine because I did this sort of
that's fine because I did this sort of the wrong way at first and now let me go
the wrong way at first and now let me go ahead and finish this thought by closing
ahead and finish this thought by closing this paragraph tag here and I've
this paragraph tag here and I've manually fixed all of my indentation so
manually fixed all of my indentation so now on line 10 I have the equivalent of
now on line 10 I have the equivalent of hey browser start a paragraph and then
hey browser start a paragraph and then it does the Latin like text then on line
it does the Latin like text then on line 12 hey browser that's it for this
12 hey browser that's it for this paragraph and repeat repeat repeat if I
paragraph and repeat repeat repeat if I now go back to my other Tab and reload
now go back to my other Tab and reload again shouldn't be all that different
again shouldn't be all that different but semantically it's a little bit
but semantically it's a little bit better why because just saying break
better why because just saying break break doesn't really mean anything but
break doesn't really mean anything but by saying paragraph paragraph paragraph
by saying paragraph paragraph paragraph now there's some more semantic
now there's some more semantic information there now if like Google is
information there now if like Google is analyzing your page or if the programmer
analyzing your page or if the programmer is trying to understand what it is you
is trying to understand what it is you did in the past when writing this code
did in the past when writing this code you just know semantically oh this is a
you just know semantically oh this is a paragraph This is a paragraph This is a
paragraph This is a paragraph This is a paragraph just like in a book or or an
paragraph just like in a book or or an essay so it's a little more clear
essay so it's a little more clear focusing more on what it is not how you
focusing more on what it is not how you want to display it any questions then on
want to display it any questions then on these
these paragraphs now all right so a few more
paragraphs now all right so a few more tags and indeed these first few examples
tags and indeed these first few examples will really just be like sort of bang
will really just be like sort of bang bang bang just a bunch of different
bang bang just a bunch of different vocabulary words in the form of these
vocabulary words in the form of these new tags but we won't go through the
new tags but we won't go through the entire laundry list of tags this is
entire laundry list of tags this is indeed the thing for which web
indeed the thing for which web references and books and the like are
references and books and the like are ultimately helpful just like a
ultimately helpful just like a dictionary in the real world so I'm
dictionary in the real world so I'm going to go ahead and do this let me go
going to go ahead and do this let me go ahead and copy this let me create a new
ahead and copy this let me create a new file called headings. HTML just so we
file called headings. HTML just so we have a new file for this to save time
have a new file for this to save time I'm just going to paste that exact same
I'm just going to paste that exact same code just to get me started I'm going to
code just to get me started I'm going to change the title of it for clarity for
change the title of it for clarity for the code online to headings and now just
the code online to headings and now just like a book or an essay or a thesis let
like a book or an essay or a thesis let me actually put some actual headings
me actually put some actual headings here now if my first heading like
here now if my first heading like chapter one I could do something like
chapter one I could do something like this up here I could have a paragraph
this up here I could have a paragraph like I just learned and I could say
like I just learned and I could say something like chapter one here but
something like chapter one here but that's not really a paragraph and so
that's not really a paragraph and so it's sort of better designed to tell the
it's sort of better designed to tell the browser and really tell the world what
browser and really tell the world what it is so it turns out there's another
it is so it turns out there's another tag I can use like H1 for heading and
tag I can use like H1 for heading and like most important heading and in here
like most important heading and in here I'm just going to keep it simple and I'm
I'm just going to keep it simple and I'm going to say something like one and in
going to say something like one and in fact this is so short here's a good
fact this is so short here's a good candidate for just keeping this all on
candidate for just keeping this all on the same line but this has no functional
the same line but this has no functional difference but it'll just make it a
difference but it'll just make it a little tur more tur on the screen now
little tur more tur on the screen now let me go ahead down here and I could
let me go ahead down here and I could have multiple headings so H1 2 and down
have multiple headings so H1 2 and down here I could have another one H1
here I could have another one H1 three and if I go back to my other tab I
three and if I go back to my other tab I reload it now we should see just like a
reload it now we should see just like a book or an essay now we have some
book or an essay now we have some proper now we have some oops I have to
proper now we have some oops I have to go to the right file sorry if I go back
go to the right file sorry if I go back to the index now we see the new third
to the index now we see the new third file called headings. HTML and now we
file called headings. HTML and now we indeed see some fairly pretty if simple
indeed see some fairly pretty if simple headings as well now if these aren't
headings as well now if these aren't three chapters 1 two three but maybe
three chapters 1 two three but maybe it's a chapter then a section then a
it's a chapter then a section then a subsection such that just visually you
subsection such that just visually you want things to get smaller and smaller
want things to get smaller and smaller well those exist too and in fact you can
well those exist too and in fact you can do H1 through H6 H1 a little
do H1 through H6 H1 a little paradoxically is the biggest and boldest
paradoxically is the biggest and boldest H6 is the smallest but still bold so it
H6 is the smallest but still bold so it might make sense to make this H2 both
might make sense to make this H2 both open and close and maybe this H3 open
open and close and maybe this H3 open and close if again this is a section or
and close if again this is a section or a subsection inside of that chapter if I
a subsection inside of that chapter if I reload now notice just gets a little
reload now notice just gets a little smaller so it's more similar to what
smaller so it's more similar to what you'd see on the printed page but this
you'd see on the printed page but this now is just another three tags that I
now is just another three tags that I might use in my own code all right well
might use in my own code all right well how about lists of things I have three
how about lists of things I have three paragraphs here but let's do this let me
paragraphs here but let's do this let me go back to vs code I'm going to copy
go back to vs code I'm going to copy this code so I have a starting point I'm
this code so I have a starting point I'm going to create a new file called say
going to create a new file called say list. HTML here I'm going to copy paste
list. HTML here I'm going to copy paste I'm going to change my title to be list
I'm going to change my title to be list just for clarity and in here I'm going
just for clarity and in here I'm going to go ahead and get rid of this whole
to go ahead and get rid of this whole body because let's move away from these
body because let's move away from these massive paragraphs and keep it simpler
massive paragraphs and keep it simpler for now if I want to have a list of
for now if I want to have a list of things uh for instance uh if you haven't
things uh for instance uh if you haven't seen these already a computer scientist
seen these already a computer scientist when they're fishing for just some
when they're fishing for just some arbitrary meaningless words uh they
arbitrary meaningless words uh they often use Foo bar and baz just as their
often use Foo bar and baz just as their go-to just like a mathematician might
go-to just like a mathematician might use XY Z for variables so Fu bar and baz
use XY Z for variables so Fu bar and baz are on three separate lines and maybe
are on three separate lines and maybe this is like my to-do list or my
this is like my to-do list or my shopping list but you can probably
shopping list but you can probably imagine if I go back to my other tab go
imagine if I go back to my other tab go back to the index I now see my new file
back to the index I now see my new file list.
list. HTML but it's probably going to look
HTML but it's probably going to look wrong I think I'm just going to say yeah
wrong I think I'm just going to say yeah Fubar baz all in one breath if you will
Fubar baz all in one breath if you will the not on new line and you can try to
the not on new line and you can try to fight this like you can be like really
fight this like you can be like really want to put some line breaks there go
want to put some line breaks there go back and reload it's still not going to
back and reload it's still not going to make any change how do I want to fix
make any change how do I want to fix this well I can do this in a few ways I
this well I can do this in a few ways I could make them paragraphs but they're
could make them paragraphs but they're not really paragraphs they're a list so
not really paragraphs they're a list so I'm going to use a different tag instead
I'm going to use a different tag instead I'm going to create for instance an
I'm going to create for instance an unordered list using the UL tag open and
unordered list using the UL tag open and close inside of that I'm going to use
close inside of that I'm going to use the list item tag Li I and I'm going to
the list item tag Li I and I'm going to say Foo inside of another tag I'm going
say Foo inside of another tag I'm going to say bar inside of a third tag open
to say bar inside of a third tag open and close I'm going to say baz so it's
and close I'm going to say baz so it's getting a little verbose but it's still
getting a little verbose but it's still relatively succinct Li is all you need
relatively succinct Li is all you need type for list item UL is all you need
type for list item UL is all you need type for unordered list so there's some
type for unordered list so there's some shorthand syntax here that's adopted if
shorthand syntax here that's adopted if I now reload you're going to see a
I now reload you're going to see a so-called unordered list like not sorted
so-called unordered list like not sorted which means by convention to show it as
which means by convention to show it as bullets though it could be displayed in
bullets though it could be displayed in different ways visually as well if you
different ways visually as well if you actually change your mind and you
actually change your mind and you realize oh I'd really like to number
realize oh I'd really like to number this well you could obviously like just
this well you could obviously like just add one and two and three but that's
add one and two and three but that's going to get annoying especially if the
going to get annoying especially if the list grows you want to change something
list grows you want to change something insert something in the middle then you
insert something in the middle then you have to reorder it I mean we using
have to reorder it I mean we using computers here they can do this for us
computers here they can do this for us so we can change the UL to any
so we can change the UL to any guesses o maybe for ordered list which
guesses o maybe for ordered list which is sort of the opposite here so let's
is sort of the opposite here so let's try changing that to O let me go back to
try changing that to O let me go back to my browser and I'm just hitting command
my browser and I'm just hitting command r or controlr to reload the page instead
r or controlr to reload the page instead of clicking the button every time now I
of clicking the button every time now I automatically get 1 2 and three and you
automatically get 1 2 and three and you can even override the Aesthetics using
can even override the Aesthetics using different numera bles or symbology
different numera bles or symbology instead but that would be perhaps the
instead but that would be perhaps the most common there as well all right it's
most common there as well all right it's a lot of tags quickly but any questions
a lot of tags quickly but any questions on lists paragraphs headings or the like
on lists paragraphs headings or the like no all right so let me go ahead and
no all right so let me go ahead and propose this here um let's go ahead and
propose this here um let's go ahead and create what we'll call a table so let me
create what we'll call a table so let me copy and paste this into a new file code
copy and paste this into a new file code of table. HTML and in table. HTML let me
of table. HTML and in table. HTML let me again rename the title to table let me
again rename the title to table let me get rid of that ordered list from before
get rid of that ordered list from before and let me now use a table tag open and
and let me now use a table tag open and close this one's a little weird but
close this one's a little weird but inside of a table you typically have a
inside of a table you typically have a head of the table so uh I'll say well
head of the table so uh I'll say well let's say the first row we'll keep this
let's say the first row we'll keep this one simple a table row or TR inside of a
one simple a table row or TR inside of a table row you would ideally have columns
table row you would ideally have columns but that's not the nomenclature instead
but that's not the nomenclature instead you have data so TD for table data and
you have data so TD for table data and let me go ahead and just have the first
let me go ahead and just have the first datm be one I'm just going to
datm be one I'm just going to arbitrarily do 1 two three just so we
arbitrarily do 1 two three just so we have something to play with and you know
have something to play with and you know what just for demonstration sake I am
what just for demonstration sake I am going to deliberately copy paste this
going to deliberately copy paste this twice and I'm just going to manually
twice and I'm just going to manually change the numbers just so we can see
change the numbers just so we can see what I'm creating 789 and then maybe
what I'm creating 789 and then maybe just for good measure if you're seeing
just for good measure if you're seeing where this is going let me copy this one
where this is going let me copy this one more time and give myself a final row
more time and give myself a final row with an asterisk a zero and a pound sign
with an asterisk a zero and a pound sign if maybe you see where this is going let
if maybe you see where this is going let me go back to my other tab let me go
me go back to my other tab let me go back to the index there's my new file
back to the index there's my new file table. HTML I'll click that and while
table. HTML I'll click that and while it's not very pretty I'll zoom in it's
it's not very pretty I'll zoom in it's indeed a table of data I happen to mimic
indeed a table of data I happen to mimic like a key uh a telephone keypad but you
like a key uh a telephone keypad but you could imagine this being much juicier
could imagine this being much juicier much more interesting scientific or
much more interesting scientific or financial data or the like laid out into
financial data or the like laid out into these rows TRS and these columns AKA
these rows TRS and these columns AKA table data as well so we have that
table data as well so we have that ability as well for structured data now
ability as well for structured data now of course the internet um has lots of
of course the internet um has lots of images on it and in fact this is all
images on it and in fact this is all just text how can we introduce images
just text how can we introduce images well let me go ahead and do this let me
well let me go ahead and do this let me first uh sort of semi secretly copy an
first uh sort of semi secretly copy an image file that I brought from uh
image file that I brought from uh earlier just so we have something to
earlier just so we have something to play with and I have in my account here
play with and I have in my account here now an image called harbor. JPEG and I
now an image called harbor. JPEG and I uploaded this semi- secretly a second
uploaded this semi- secretly a second ago into my account so that I can
ago into my account so that I can reference a second file let me go ahead
reference a second file let me go ahead and copy this HTML just to save myself
and copy this HTML just to save myself some keystrokes
some keystrokes let me go ahead and do code of image.
let me go ahead and do code of image. HTML and let me paste that code and hide
HTML and let me paste that code and hide my terminal window I'm going to get rid
my terminal window I'm going to get rid of all of this table as just
of all of this table as just uninteresting now we're going to make
uninteresting now we're going to make this even simpler by changing my title
this even simpler by changing my title to image to keep all these
to image to keep all these demonstrations uh separate and now if I
demonstrations uh separate and now if I want to make a web page that when
want to make a web page that when visited shows us a picture of Harvard
visited shows us a picture of Harvard well there's an image tag abbreviated
well there's an image tag abbreviated IMG for short I can specify what the
IMG for short I can specify what the source of that image is and if my file a
source of that image is and if my file a JPEG in this case is literally in the
JPEG in this case is literally in the same folder I can just say quote unquote
same folder I can just say quote unquote harvard. jpeg if it's in a folder I
harvard. jpeg if it's in a folder I should mention the folder name in a
should mention the folder name in a slash or something like that if the
slash or something like that if the image is on the internet somewhere with
image is on the internet somewhere with a URL I could also have a whole URL
a URL I could also have a whole URL https colon and then the URL of the
https colon and then the URL of the image but I upload it in in advance now
image but I upload it in in advance now this is just going to visually display
this is just going to visually display on the screen but not everyone of course
on the screen but not everyone of course can see images screen readers might need
can see images screen readers might need a bit of assistance and even search
a bit of assistance and even search engines might want to analyze the page
engines might want to analyze the page and know what this is an image of now
and know what this is an image of now machine learning and artificial
machine learning and artificial intelligence are maybe getting better to
intelligence are maybe getting better to be fair at figuring out just by
be fair at figuring out just by analyzing images what they are but
analyzing images what they are but they're certainly imperfect I am a human
they're certainly imperfect I am a human I know pretty well what I took a photo
I know pretty well what I took a photo of for instance so maybe what I should
of for instance so maybe what I should do proactively which would be good for
do proactively which would be good for accessibility is have this alt tag for
accessibility is have this alt tag for alternative text and then literally say
alternative text and then literally say like Harvard University so that someone
like Harvard University so that someone who can't see or so that a a server can
who can't see or so that a a server can actually know with higher probability
actually know with higher probability what it is uh they're looking at and I
what it is uh they're looking at and I could be even more detailed than just a
could be even more detailed than just a phrase I could describe the image as
phrase I could describe the image as well all right let me go back to my
well all right let me go back to my index in the second tab let me go back
index in the second tab let me go back and zoom back out there's my new file
and zoom back out there's my new file and there's my new JPEG that I quickly
and there's my new JPEG that I quickly uploaded before I can click now on
uploaded before I can click now on image. HTML and albeit a little
image. HTML and albeit a little overwhelming that is a really big image
overwhelming that is a really big image of harvor now apparently it's too big to
of harvor now apparently it's too big to fit on the screen so this isn't the best
fit on the screen so this isn't the best user experience to have to scroll up
user experience to have to scroll up okay so there's the image horrible
okay so there's the image horrible horrible design if you will at least in
horrible design if you will at least in terms of my code but there's going to be
terms of my code but there's going to be ways where I can sort of rein that in
ways where I can sort of rein that in and affect the height or the width as
and affect the height or the width as well but for now it's just deliberately
well but for now it's just deliberately a little overwhelming instead now we can
a little overwhelming instead now we can do something a little more fun and
do something a little more fun and topical today uh which might be to use
topical today uh which might be to use an IM a video instead so let me go ahead
an IM a video instead so let me go ahead here and very quickly grab another file
here and very quickly grab another file today which is uh I brought in advance
today which is uh I brought in advance and that you might have seen briefly
and that you might have seen briefly earlier which is an MP4 an actual video
earlier which is an MP4 an actual video file and what I'm going to do here by
file and what I'm going to do here by revealing vs code again is I'm going to
revealing vs code again is I'm going to code a file called video. HTML get
code a file called video. HTML get another demonstration here I'm going to
another demonstration here I'm going to change my title to video just to keep
change my title to video just to keep these things straight and instead of the
these things straight and instead of the image tag you might imagine using now
image tag you might imagine using now indeed a video tag and this is a
indeed a video tag and this is a relatively newer tag that has increasing
relatively newer tag that has increasing support among browsers so it's good now
support among browsers so it's good now to use and inside of this the syntax is
to use and inside of this the syntax is a little different you specify and this
a little different you specify and this is weirdly annoyingly in consistent not
is weirdly annoyingly in consistent not SRC for Source you literally say source
SRC for Source you literally say source and then in Source you use a source
and then in Source you use a source attribute horrible design semantically
attribute horrible design semantically but like this is what we're stuck with
but like this is what we're stuck with halloween. MP4 is the name of the video
halloween. MP4 is the name of the video we uploaded in advance made by some of
we uploaded in advance made by some of harder Harvard's digital artists and the
harder Harvard's digital artists and the type of this video so that the browser
type of this video so that the browser knows for sure is video/ MP4 that's a
knows for sure is video/ MP4 that's a so-called content type that you just
so-called content type that you just know or you look up to figure it out and
know or you look up to figure it out and just so that this is as animated as
just so that this is as animated as possible I'm going to tell the video tag
possible I'm going to tell the video tag with a few attributes to autoplay and it
with a few attributes to autoplay and it turns out that attributes often have key
turns out that attributes often have key value pairs whereby it's the key the
value pairs whereby it's the key the attribute name equals quote unquote some
attribute name equals quote unquote some value just like Lang equals quote
value just like Lang equals quote unquote for English but not all
unquote for English but not all attributes need values in fact if you
attributes need values in fact if you read the documentation for html's video
read the documentation for html's video tag there's an autoplay attribute where
tag there's an autoplay attribute where you can literally just say the key and
you can literally just say the key and it needs no value it's just going to
it needs no value it's just going to mean autoplay and if you don't want to
mean autoplay and if you don't want to autoplay you just omit it alt together
autoplay you just omit it alt together so so you don't necessarily need a value
so so you don't necessarily need a value on or off uh I want the thing to Loop
on or off uh I want the thing to Loop just so it keeps going I want it to be
just so it keeps going I want it to be muted so that we don't hear any sound in
muted so that we don't hear any sound in fact there is no sound but browsers
fact there is no sound but browsers nowadays for anti-spam and advertising
nowadays for anti-spam and advertising reasons often will not play a video if
reasons often will not play a video if it has sound because it's just kind of
it has sound because it's just kind of obnoxious if you visit a page and all of
obnoxious if you visit a page and all of a sudden your speakers start blaring so
a sudden your speakers start blaring so I know this from having read up on this
I know this from having read up on this that I should mute it so if I want it to
that I should mute it so if I want it to actually autoplay for real and then I'll
actually autoplay for real and then I'll set the width manually for now to be
set the width manually for now to be like 128 pixels across just from some
like 128 pixels across just from some trial and error earlier and that with
trial and error earlier and that with attribute does have a value now I'm
attribute does have a value now I'm being a little um a little uptight here
being a little um a little uptight here by alphabetizing all of my attributes
by alphabetizing all of my attributes not at all necessary I do it just so I
not at all necessary I do it just so I can skim things faster and know if
can skim things faster and know if something is there or not so for me it's
something is there or not so for me it's just a matter of style let me go back to
just a matter of style let me go back to my other tab go back to my index and
my other tab go back to my index and you'll see two new files again the mp4
you'll see two new files again the mp4 file and video. HTML I'll click on the
file and video. HTML I'll click on the ladder and if I did this well here we
ladder and if I did this well here we have thank thanks to our friends in
have thank thanks to our friends in Harvard U our artistic friends at
Harvard U our artistic friends at Harvard very like an oo would help with
Harvard very like an oo would help with the drama here but okay but we have a
the drama here but okay but we have a very dramatic nice Halloween type view
very dramatic nice Halloween type view here as well so we have videos embedded
here as well so we have videos embedded as well and suffice it to say there's
as well and suffice it to say there's ways to embed YouTube videos or Vimeo or
ways to embed YouTube videos or Vimeo or other services as well using yet more
other services as well using yet more tags too but the web is of course all
tags too but the web is of course all about hyperlink Hyper text markup
about hyperlink Hyper text markup language where you click on something
language where you click on something and you end up somewhere else and this
and you end up somewhere else and this is how the web is so powerfully
is how the web is so powerfully interconnected
interconnected so how do we start creating links from
so how do we start creating links from one website or web page to another
one website or web page to another either that I made or someone else well
either that I made or someone else well let me go ahead and open back up my
let me go ahead and open back up my terminal window and let's create a file
terminal window and let's create a file called link. HTML just to demonstrate
called link. HTML just to demonstrate what you and I know as a link I'll hide
what you and I know as a link I'll hide my terminal window now let me copy paste
my terminal window now let me copy paste just to save myself some keystrokes and
just to save myself some keystrokes and let me get rid of the video tag so we
let me get rid of the video tag so we can focus now on links suppose that I
can focus now on links suppose that I want you to visit Harvard virtually well
want you to visit Harvard virtually well I could say something like visit uh
I could say something like visit uh Harvard period this is uninteresting
Harvard period this is uninteresting because it's just going to be text I
because it's just going to be text I probably want you to actually visit
probably want you to actually visit harvard.edu instead more specifically
harvard.edu instead more specifically and I'll lower case it just to be
and I'll lower case it just to be consistent with what browsers do in the
consistent with what browsers do in the address bar all right let me go now to
address bar all right let me go now to the video uh back to this video Tab and
the video uh back to this video Tab and go back where we now see my index I'll
go back where we now see my index I'll Zoom back in and there's link. HTML
Zoom back in and there's link. HTML unfortunately when I click this and I'll
unfortunately when I click this and I'll zoom in you literally just see the text
zoom in you literally just see the text that I wrote and yet on every social
that I wrote and yet on every social media platform nowadays except like
media platform nowadays except like Instagram when you type a URL or what
Instagram when you type a URL or what looks like a URL even if you didn't
looks like a URL even if you didn't bother with the HTTP or https it usually
bother with the HTTP or https it usually automatically links it for you on
automatically links it for you on Facebook on Twitter and other sites as
Facebook on Twitter and other sites as well that's just a convenience Discord
well that's just a convenience Discord and slack do that too but they're just
and slack do that too but they're just doing it to make things more
doing it to make things more userfriendly but they have to generate
userfriendly but they have to generate HTML with the proper tags and attributes
HTML with the proper tags and attributes so to get this to actually work it's not
so to get this to actually work it's not even good enough to say
even good enough to say https www.harvard.edu because if I go
https www.harvard.edu because if I go back now and reload now you'll literally
back now and reload now you'll literally just see all of that as text if you want
just see all of that as text if you want the browser to treat this as a link you
the browser to treat this as a link you need to use the anchor tag it'd be great
need to use the anchor tag it'd be great if it were called the link tag but it's
if it were called the link tag but it's not it's called The Anchor tag or a for
not it's called The Anchor tag or a for short and the way you reference the URL
short and the way you reference the URL to which you want to lead the user is
to which you want to lead the user is via href for hyper reference this is one
via href for hyper reference this is one of the earliest tags perhaps among the
of the earliest tags perhaps among the most Arcane now but if I then put that
most Arcane now but if I then put that whole URL in quotes and close my tag I
whole URL in quotes and close my tag I now have to opportunity to finish my
now have to opportunity to finish my thought in between the start tag and the
thought in between the start tag and the end tag for this anchor and what I put
end tag for this anchor and what I put in between the start and end tag is
in between the start and end tag is whatever the human's going to see so
whatever the human's going to see so here I can say Harvard I can go back to
here I can say Harvard I can go back to my other tab I can reload the page and
my other tab I can reload the page and now you see the familiar blue underline
now you see the familiar blue underline this now is an actual link and if I
this now is an actual link and if I click it I'll be whisked away to the
click it I'll be whisked away to the actual Harvard websites but there's a
actual Harvard websites but there's a risk here can anyone imagine pretty
risk here can anyone imagine pretty simply after like what 60 seconds of the
simply after like what 60 seconds of the link tag of the anchor tag how could
link tag of the anchor tag how could someone an adversary misuse this tag
someone an adversary misuse this tag alone How could a website run by an
alone How could a website run by an adversary How could a spammer misuse
adversary How could a spammer misuse this tag do you think
yeah yeah absolutely you could have it say one thing but lead another lead
say one thing but lead another lead elsewhere so I could say Yale in here
elsewhere so I could say Yale in here nothing stopping me as the developer go
nothing stopping me as the developer go back to the page reload now it says
back to the page reload now it says visit Yale you click on Yale and voila
visit Yale you click on Yale and voila you end up applying to the wrong place
you end up applying to the wrong place instead now there's some hints of this I
instead now there's some hints of this I could hover over this and super small
could hover over this and super small like this isn't very good for your
like this isn't very good for your anti-hacking techniques but way down
anti-hacking techniques but way down here you can actually see the URL that
here you can actually see the URL that it's going to go to in most browsers
it's going to go to in most browsers indeed do this at least on desktops and
indeed do this at least on desktops and laptops so it's a little bit of a hint
laptops so it's a little bit of a hint but what you're seeing here even though
but what you're seeing here even though this is kind of a silly um uh playful
this is kind of a silly um uh playful example this is exactly how fishing
example this is exactly how fishing attacks work p i sh iing work whereby an
attacks work p i sh iing work whereby an adversary tells you to log into your
adversary tells you to log into your PayPal account but it doesn't go to
PayPal account but it doesn't go to paypal.com it goes to some other random
paypal.com it goes to some other random website that they bought and built that
website that they bought and built that then tries to collect your username and
then tries to collect your username and password and stored in their database so
password and stored in their database so now they can log into your PayPal
now they can log into your PayPal account as you and it boils down to that
account as you and it boils down to that simple primitive and you can be even
simple primitive and you can be even more manipulative to you can even say
more manipulative to you can even say the whole URL for Yale like yale.edu or
the whole URL for Yale like yale.edu or Worse htps www.yale.edu reload that and
Worse htps www.yale.edu reload that and now who I mean who among you and people
now who I mean who among you and people in your lives are necessarily going to
in your lives are necessarily going to be so paranoid as to not just blindly
be so paranoid as to not just blindly click on that URL this is why just being
click on that URL this is why just being a defensive real world person nowadays
a defensive real world person nowadays digitally is just ever more so important
digitally is just ever more so important so these same things that can be used
so these same things that can be used for good or uh benign use cases can also
for good or uh benign use cases can also be used for ill purposes too and it is
be used for ill purposes too and it is literally that simple questions now on
literally that simple questions now on any of these tags thus
any of these tags thus far just a few more to offer up any
far just a few more to offer up any questions on this
questions on this here no well let me open up a couple
here no well let me open up a couple that I brought in advance just so we
that I brought in advance just so we don't have to type all of them here um
don't have to type all of them here um if you for instance have a web page
if you for instance have a web page that's got quite a bit of code let me go
that's got quite a bit of code let me go ahead and grab from the website a couple
ahead and grab from the website a couple of examples real fast here namely one
of examples real fast here namely one that we'll call how about uh meta
that we'll call how about uh meta HTML and in this example here give me
HTML and in this example here give me just a moment to full screen it we're
just a moment to full screen it we're going to
going to have a file So codem Meta HTML I'll open
have a file So codem Meta HTML I'll open this up next no relationship to what we
this up next no relationship to what we now know as meta the company but rather
now know as meta the company but rather this is going to be a page that I copied
this is going to be a page that I copied and pasted the same chunk of Latin like
and pasted the same chunk of Latin like text from earlier so it's going to be a
text from earlier so it's going to be a really big paragraph of text and this is
really big paragraph of text and this is an example where if you were to open
an example where if you were to open this web page not on my own Mac or your
this web page not on my own Mac or your PC but on your phone the font might
PC but on your phone the font might actually be really annoying and
actually be really annoying and difficult to read why because your
difficult to read why because your phone's going to try to squeeze all of
phone's going to try to squeeze all of the content onto the tiny viewport the
the content onto the tiny viewport the rectangular region of your phone instead
rectangular region of your phone instead so it turns out there are ways pretty
so it turns out there are ways pretty easy ways to make your website mobile
easy ways to make your website mobile friendly as well Otherwise Known
friendly as well Otherwise Known technically as responsive and the
technically as responsive and the easiest way to do this is to include
easiest way to do this is to include this tag here a meta tag again no
this tag here a meta tag again no relationship to Facebook this has been
relationship to Facebook this has been here much longer and this case here this
here much longer and this case here this meta tag online five has its own sort of
meta tag online five has its own sort of approach to key value pairs this is a
approach to key value pairs this is a good example of where it'd be nice if it
good example of where it'd be nice if it looked just like everything else but
looked just like everything else but this is what we have historically
this is what we have historically you can have a meta tag with an
you can have a meta tag with an attribute called name that refers to the
attribute called name that refers to the name of some feature of the browser in
name of some feature of the browser in this case viewport is the technical term
this case viewport is the technical term for like the big rectangular region to
for like the big rectangular region to which I keep referring the body really
which I keep referring the body really of your page the content for the
of your page the content for the viewport you can say some AR esoteric
viewport you can say some AR esoteric details like this the initial scale
details like this the initial scale should be one that is no matter who
should be one that is no matter who visits your site it shouldn't start
visits your site it shouldn't start zoomed in it shouldn't start zoomed out
zoomed in it shouldn't start zoomed out it should start at just the default
it should start at just the default sizing and then this here with equals
sizing and then this here with equals device width is a very arcane way of
device width is a very arcane way of saying if the user has a small screen
saying if the user has a small screen show the text proportional to that size
show the text proportional to that size don't just try to cram it all into a
don't just try to cram it all into a tiny little window so it's super simple
tiny little window so it's super simple but if for the next problem set or
but if for the next problem set or future projects as well you find that
future projects as well you find that just things look really bad on mobile
just things look really bad on mobile like this kind of tag is the place to
like this kind of tag is the place to start meta there aren't terribly many of
start meta there aren't terribly many of these that you'll use but they're useful
these that you'll use but they're useful for other mechanisms as well in fact let
for other mechanisms as well in fact let me go ahead and semi secretly pull up
me go ahead and semi secretly pull up one other example as well whereby I'm
one other example as well whereby I'm going to grab another example that uses
going to grab another example that uses more of these tags and in just a moment
more of these tags and in just a moment I'll reveal it here give me just one
I'll reveal it here give me just one second here I'll have I'll propose that
second here I'll have I'll propose that in this
in this example of
example of meta we now add these properties instead
meta we now add these properties instead so I copy pasted this from an existing
so I copy pasted this from an existing file just so as to not waste time typing
file just so as to not waste time typing all of these out if you've ever shared a
all of these out if you've ever shared a URL on Facebook or Twitter or slack or
URL on Facebook or Twitter or slack or Discord order any number of websites
Discord order any number of websites nowadays that automatically show a nice
nowadays that automatically show a nice preview where you sudden instantly see
preview where you sudden instantly see like the default image of the page maybe
like the default image of the page maybe a few sentences or words of text or
a few sentences or words of text or something like that sometimes those
something like that sometimes those applications those websites will just
applications those websites will just choose like the first image it finds on
choose like the first image it finds on the website or the first sentence it
the website or the first sentence it sees and show that but that might not be
sees and show that but that might not be very user friendly or search engine
very user friendly or search engine friendly and so a prog a developer might
friendly and so a prog a developer might want to control what it is that slack
want to control what it is that slack Discord Facebook Twitter and other such
Discord Facebook Twitter and other such sites show by default for that can use
sites show by default for that can use nowadays what are called open graph tags
nowadays what are called open graph tags which is to say there's other uses of
which is to say there's other uses of the meta tag and you just look these
the meta tag and you just look these things up even I had to look this up to
things up even I had to look this up to remember what the key value pairs are
remember what the key value pairs are the meta tag can also have a property
the meta tag can also have a property attribute that can be these very
attribute that can be these very specific strings OG title OG description
specific strings OG title OG description OG image which denotes open graph which
OG image which denotes open graph which again is this standard that's evolved in
again is this standard that's evolved in recent years and what you can do here is
recent years and what you can do here is tell browsers and in turn servers what
tell browsers and in turn servers what you want them to show as the default
you want them to show as the default title of the page P the description of
title of the page P the description of the page and even the default image just
the page and even the default image just so you can exercise more control when
so you can exercise more control when sharing things socially nowadays as well
sharing things socially nowadays as well again it just boils down to these key
again it just boils down to these key value pairs this is absolutely the kind
value pairs this is absolutely the kind of thing you look up as needed to cross
of thing you look up as needed to cross check but those capabilities are there
check but those capabilities are there and so literally the next time you paste
and so literally the next time you paste a link into slack or Discord or any
a link into slack or Discord or any online site that then displays it in
online site that then displays it in embedded fashion just know that all this
embedded fashion just know that all this time a little bit of textual code like
time a little bit of textual code like this in HTML has been there by whoever
this in HTML has been there by whoever authored the site
authored the site all right let's do one final example in
all right let's do one final example in HTML alone before we transition to just
HTML alone before we transition to just cleaning up the Aesthetics and improving
cleaning up the Aesthetics and improving the visuals of everything we've been
the visuals of everything we've been creating let me go ahead here and close
creating let me go ahead here and close meta HTML let me code up a new file
meta HTML let me code up a new file called how about search. HTML and see if
called how about search. HTML and see if we can't draw some inspiration from our
we can't draw some inspiration from our cursory understanding earlier of how
cursory understanding earlier of how URLs work to see if we can't reinvent
URLs work to see if we can't reinvent google.com itself so recall that a
google.com itself so recall that a canonical URL might look like this here
canonical URL might look like this here and in particular if you want to pass in
and in particular if you want to pass in user input to that URL again you can
user input to that URL again you can potentially have a question mark and
potentially have a question mark and then a key equals value pair or for that
then a key equals value pair or for that matter you can even have multiple key
matter you can even have multiple key value pairs that by convention are
value pairs that by convention are separated by ampers sense these things
separated by ampers sense these things are everywhere like later today when you
are everywhere like later today when you pull up almost any website in your
pull up almost any website in your browser look at the URL and you'll just
browser look at the URL and you'll just see a lot of this a lot of noise too and
see a lot of this a lot of noise too and distractions but there's going to be
distractions but there's going to be some equal signs most likely Andor some
some equal signs most likely Andor some ampersands as well and those are just
ampersands as well and those are just separating key value pairs now what can
separating key value pairs now what can I do here well if you think back to how
I do here well if you think back to how we manually searched for cats earlier
we manually searched for cats earlier let me quickly do this I'll do this one
let me quickly do this I'll do this one manually doc type HTML as my very first
manually doc type HTML as my very first line HTML tag with how about my Lang
line HTML tag with how about my Lang attribute for English up here and then
attribute for English up here and then inside of this I'll have a head tag
inside of this I'll have a head tag inside of this I'll have a title I'll
inside of this I'll have a title I'll call this example uh search and then
call this example uh search and then down here I'll have my beginning of a
down here I'll have my beginning of a body tag and now let me introduce you to
body tag and now let me introduce you to really a final tag for now uh a form tag
really a final tag for now uh a form tag which will create a web form the thing
which will create a web form the thing with text boxes and buttons that you and
with text boxes and buttons that you and I use every day on any number of
I use every day on any number of websites inside of this form I'm going
websites inside of this form I'm going to have an input like a text box whose
to have an input like a text box whose name is going to be Q for query because
name is going to be Q for query because I'm trying to re-implement Google here
I'm trying to re-implement Google here uh the uh type of that I want to be a
uh the uh type of that I want to be a text box or if I know I'm using this for
text box or if I know I'm using this for search I can actually change this to a
search I can actually change this to a search box and it's going to let me it's
search box and it's going to let me it's going to generally put a little X there
going to generally put a little X there so you can clear it quickly that's a
so you can clear it quickly that's a nice little enhancement as well and then
nice little enhancement as well and then I'm going to give myself a submit button
I'm going to give myself a submit button by doing input whoops I'm going to give
by doing input whoops I'm going to give myself a submit button by doing input
myself a submit button by doing input type equals submit and then I'll leave
type equals submit and then I'll leave that as such here all right now I need
that as such here all right now I need to do a little bit more but let's see
to do a little bit more but let's see how this looks let me go over to my
how this looks let me go over to my other tab let me go back to my index and
other tab let me go back to my index and if I zoom
if I zoom out there is search. HTML I'll click it
out there is search. HTML I'll click it and there's not much going on here even
and there's not much going on here even if I zoom in but I do indeed have a
if I zoom in but I do indeed have a really big text box and a submit button
really big text box and a submit button but I haven't in my HTML told anyone
but I haven't in my HTML told anyone anywhere that I want this input whether
anywhere that I want this input whether I type cat or dog to go to google.com so
I type cat or dog to go to google.com so for that I need a couple of more
for that I need a couple of more attributes and I know this from having
attributes and I know this from having done this before and any online
done this before and any online reference will say the same you can add
reference will say the same you can add an action attribute like what do you
an action attribute like what do you want the action of this form to be and
want the action of this form to be and you can put the URL to which you want
you can put the URL to which you want this form to be submitted and I know
this form to be submitted and I know from tinkering that it should be hdps
from tinkering that it should be hdps www.google.com search I don't need to
www.google.com search I don't need to put any question marks here myself but I
put any question marks here myself but I do want the uh browser to do that for me
do want the uh browser to do that for me so let me go back to my other tab let me
so let me go back to my other tab let me reload and nothing visually has happened
reload and nothing visually has happened but watch this when I now type in cats
but watch this when I now type in cats but before I hit enter notice that I'm
but before I hit enter notice that I'm currently at some long crazy URL search.
currently at some long crazy URL search. HTML is expected if I now go down to the
HTML is expected if I now go down to the submit button and click submit watch
submit button and click submit watch what happens to the URL and the page
what happens to the URL and the page itself I'm whisked away to the actual
itself I'm whisked away to the actual google.com and indeed there are those
google.com and indeed there are those same cats and if I zoom in here you'll
same cats and if I zoom in here you'll see that my URL has changed to be indeed
see that my URL has changed to be indeed SL search question mark Q equals cats so
SL search question mark Q equals cats so this is just how web forms work when you
this is just how web forms work when you submit any form on the web in this way
submit any form on the web in this way the browser automatically goes to that
the browser automatically goes to that action URL adds a question mark puts any
action URL adds a question mark puts any key value pairs that you manually typed
key value pairs that you manually typed into the text boxes and lets the server
into the text boxes and lets the server do its thing now here's where Chrome is
do its thing now here's where Chrome is starting to simplify things Safari does
starting to simplify things Safari does this too if you double click on your url
this too if you double click on your url now you see the full URL but if any
now you see the full URL but if any parts are missing that's just a UI thing
parts are missing that's just a UI thing to eliminate visual distractions
to eliminate visual distractions nowadays meanwhile if I go back to my
nowadays meanwhile if I go back to my own form if I search this time for dogs
own form if I search this time for dogs and hit enter now again the URL changes
and hit enter now again the URL changes to be Q equals dogs and it all reduces
to be Q equals dogs and it all reduces to this basic building block of using an
to this basic building block of using an a form tag now I can be more explicit if
a form tag now I can be more explicit if I know I want to use get it which is
I know I want to use get it which is actually the default I can literally say
actually the default I can literally say quote unquote get in all lowercase even
quote unquote get in all lowercase even though the verb earlier was by Design in
though the verb earlier was by Design in uppercase but here now I'm just being
uppercase but here now I'm just being ever more explicit if I don't want the
ever more explicit if I don't want the label of this button to be very
label of this button to be very generically submit maybe I want it to be
generically submit maybe I want it to be Google search quote unquote well if you
Google search quote unquote well if you read the documentation for forms you can
read the documentation for forms you can actually change the value of the button
actually change the value of the button to be quote unquote Google search and if
to be quote unquote Google search and if I now go back here and reload I get a
I now go back here and reload I get a fresh form and now I get a button that
fresh form and now I get a button that literally says Google Search and if I
literally says Google Search and if I tinker with this further because this
tinker with this further because this isn't very user friendly there's even
isn't very user friendly there's even more attributes I can do I can add on my
more attributes I can do I can add on my t uh search input a uh autoc complete
t uh search input a uh autoc complete equals uh off if I don't want to see my
equals uh off if I don't want to see my own history for whatever reason I don't
own history for whatever reason I don't want people knowing I'm searching for
want people knowing I'm searching for cats and dogs on this page I can
cats and dogs on this page I can autofocus on the text box so that it
autofocus on the text box so that it shows the cursor blinking in that box by
shows the cursor blinking in that box by default and I can even do something like
default and I can even do something like this I can have a placeholder attribute
this I can have a placeholder attribute that says something like query or some
that says something like query or some other documentation for the user and if
other documentation for the user and if I now go back and reload you'll see
I now go back and reload you'll see notice it says query and it's subtle but
notice it says query and it's subtle but my cursor is already positioned there it
my cursor is already positioned there it gave it focus and I can type cats now
gave it focus and I can type cats now without having to click in the Box
without having to click in the Box manually which is just marginally better
manually which is just marginally better for the user's experience any questions
for the user's experience any questions now on all of this
now on all of this here any
here any questions all right that too was a lot
questions all right that too was a lot why don't we take a casual five minute
why don't we take a casual five minute break and when we resume we'll take a
break and when we resume we'll take a look at CSS add in some JavaScript and
look at CSS add in some JavaScript and then wrap up so 5 minutes only for now
then wrap up so 5 minutes only for now all right we are back so that's
all right we are back so that's technically it for HTML like here on out
technically it for HTML like here on out it'll be up to like online resources and
it'll be up to like online resources and references we point you to just to fill
references we point you to just to fill in your vocabulary for more tags and
in your vocabulary for more tags and attributes but like conceptually that's
attributes but like conceptually that's it there's attributes uh there are tags
it there's attributes uh there are tags and there are attributes and the rest of
and there are attributes and the rest of it really is just kind of a laundry list
it really is just kind of a laundry list of capable uh possible features but it
of capable uh possible features but it turns out too you'll see over time that
turns out too you'll see over time that you can even see the HTML for websit so
you can even see the HTML for websit so for instance if I go over to harvard.edu
for instance if I go over to harvard.edu in my browser which I'll go ahead in
in my browser which I'll go ahead in just a moment here here and do here and
just a moment here here and do here and do
do https
https www.harvard.edu
www.harvard.edu enter it again will pull up today's
enter it again will pull up today's version of Harvard's website and if I
version of Harvard's website and if I rightclick or control click on it again
rightclick or control click on it again and go to inspect you'll see those
and go to inspect you'll see those so-called developer tools and earlier we
so-called developer tools and earlier we focused on the network tab just so we
focused on the network tab just so we could see the HTTP stuff going on but
could see the HTTP stuff going on but what I glossed over earlier was the
what I glossed over earlier was the so-called elements tab which actually
so-called elements tab which actually shows you the HTML underlying any web
shows you the HTML underlying any web page on the internet and so for instance
page on the internet and so for instance here is the underlying HTML for
here is the underlying HTML for Harvard's homepage as of right now and
Harvard's homepage as of right now and aesthetically some of it's been
aesthetically some of it's been collapsed so if I click on these various
collapsed so if I click on these various triangles I'll see what's actually
triangles I'll see what's actually inside of that is the children of some
inside of that is the children of some of these HTML tags but here on out if
of these HTML tags but here on out if you're ever curious as to like how a web
you're ever curious as to like how a web page uh made some feature visually you
page uh made some feature visually you can just literally use these developer
can just literally use these developer tools built into your own browser just
tools built into your own browser just to see what the uh web developer
to see what the uh web developer actually did and you can do things too
actually did and you can do things too like this like if you really like maybe
like this like if you really like maybe uh let's see if you really like this
uh let's see if you really like this menu in the top right hand corner of
menu in the top right hand corner of Harvard's website you can even
Harvard's website you can even rightclick that or control click that
rightclick that or control click that specifically choose inspect and what
specifically choose inspect and what browsers will do is jump to the HTML
browsers will do is jump to the HTML corresponding to that visual element on
corresponding to that visual element on the page and here you can see though
the page and here you can see though we've not talked about this tag before
we've not talked about this tag before there's a button tag there's an ID
there's a button tag there's an ID attribute and there's some other
attribute and there's some other attributes as well that Define that
attributes as well that Define that button um you can do other things too in
button um you can do other things too in the web page let me scroll down for
the web page let me scroll down for instance here and let's go actually
instance here and let's go actually let's go to another one like yale.edu
let's go to another one like yale.edu here in today's theme and suppose we
here in today's theme and suppose we want to do something like uh change the
want to do something like uh change the Aesthetics of this website well let's do
Aesthetics of this website well let's do how about this over here life at Yale
how about this over here life at Yale let's rightclick on this choose inspect
let's rightclick on this choose inspect that's going to jump to that part of the
that's going to jump to that part of the page and notice what you can do here in
page and notice what you can do here in this elements tab we can be a little a
this elements tab we can be a little a little playful in return today life at
little playful in return today life at Harvard and voila we've now changed
Harvard and voila we've now changed Yale's website it would seem so have we
Yale's website it would seem so have we really like hopefully hacking is not
really like hopefully hacking is not actually this
actually this easy what did we actually do based on
easy what did we actually do based on today's mental
today's mental model like I have changed the page
model like I have changed the page but yeah just changed how it is for me
but yeah just changed how it is for me right because my browser just like with
right because my browser just like with Phyllis and Brian from the GetGo
Phyllis and Brian from the GetGo requested Yale's web page I got back a
requested Yale's web page I got back a virtual envelope containing that HTML as
virtual envelope containing that HTML as we've now called it my browser has a
we've now called it my browser has a local copy it's got its own tree
local copy it's got its own tree otherwise known as a Dom document object
otherwise known as a Dom document object model built up in its memory and yeah I
model built up in its memory and yeah I went to town and changed my copy of it
went to town and changed my copy of it but of course hopefully I've not changed
but of course hopefully I've not changed the actual server and in fact if I
the actual server and in fact if I reload Yale's website now hopefully it
reload Yale's website now hopefully it will revert back to indeed yep what it
will revert back to indeed yep what it should be instead life at Yale but this
should be instead life at Yale but this ability in your own browser be it Chrome
ability in your own browser be it Chrome or Firefox or Edge or Safari to have
or Firefox or Edge or Safari to have these built-in developer tools are very
these built-in developer tools are very powerful because it's going to enable
powerful because it's going to enable you to not only diagnose problems that
you to not only diagnose problems that will invariably arise in the coming
will invariably arise in the coming weeks with your own code but is also
weeks with your own code but is also going to allow you to learn from other
going to allow you to learn from other sites like how you can do things and
sites like how you can do things and Tinker as well but up until now we
Tinker as well but up until now we focused only on tags and attributes and
focused only on tags and attributes and on the structure of a web page let's now
on the structure of a web page let's now focus more on the Aesthetics and
focus more on the Aesthetics and fine-tuning that it turns out that HTML
fine-tuning that it turns out that HTML has very limited support for anything
has very limited support for anything aesthetic like font sizes and colors and
aesthetic like font sizes and colors and so forth and in recent years people have
so forth and in recent years people have used necessarily a second language
used necessarily a second language called CSS not a programming language
called CSS not a programming language again a markup language if you will to
again a markup language if you will to just fine-tune the Aesthetics of a page
just fine-tune the Aesthetics of a page font sizes colors margins and all of
font sizes colors margins and all of that so CSS is going to allow us to
that so CSS is going to allow us to define a whole bunch of properties which
define a whole bunch of properties which is just another group of people's
is just another group of people's terminology for key value pairs indeed
terminology for key value pairs indeed ever since week five like key value
ever since week five like key value pairs are everywhere in the world not
pairs are everywhere in the world not just at like sweet green and restaurants
just at like sweet green and restaurants but indeed under in line code and
but indeed under in line code and languages and Technologies like these so
languages and Technologies like these so properties is the new word in CSS for
properties is the new word in CSS for what a moment ago we called attributes
what a moment ago we called attributes in H but it's the same idea just
in H but it's the same idea just different vocabulary that you get used
different vocabulary that you get used to over time a few phrases I might use
to over time a few phrases I might use now and you'll hear in the coming days
now and you'll hear in the coming days would be these type selector class
would be these type selector class selector ID selector attribute selector
selector ID selector attribute selector which just refer to different techniques
which just refer to different techniques we're about to see that are going to
we're about to see that are going to allow you to control more precisely the
allow you to control more precisely the Aesthetics of specific things on the
Aesthetics of specific things on the page and the way we're going to do this
page and the way we're going to do this is we're going to take our basic HTML
is we're going to take our basic HTML like we saw earlier and we're going to
like we saw earlier and we're going to introduce in the next few minutes just a
introduce in the next few minutes just a couple of more tags and or attributes
couple of more tags and or attributes one we're going to introduce you to a
one we're going to introduce you to a tag called style which nicely named
tag called style which nicely named allows you to um allows you to control
allows you to um allows you to control the style the Aesthetics the visuals of
the style the Aesthetics the visuals of the web page or we're going to introduce
the web page or we're going to introduce you to a link tag which very confusingly
you to a link tag which very confusingly does not give you a link that you can
does not give you a link that you can click on it just links to another file
click on it just links to another file that then gets automatically included or
that then gets automatically included or imported to borrow our language from C
imported to borrow our language from C or in Python but same idea this will
or in Python but same idea this will allow us to include secondary files and
allow us to include secondary files and we're going to ultimately show you how
we're going to ultimately show you how you can leverage third third party
you can leverage third third party Frameworks libraries that other people
Frameworks libraries that other people wrote so as to not get stuck in the
wrote so as to not get stuck in the weeds of all the fine tuning of
weeds of all the fine tuning of Aesthetics and just make pretty things
Aesthetics and just make pretty things fast so you can focus really on the
fast so you can focus really on the intellectually interesting part if
intellectually interesting part if that's your choice of building the
that's your choice of building the content the site out the application out
content the site out the application out yourself all right so with that said let
yourself all right so with that said let me go back to vs code here and let me go
me go back to vs code here and let me go ahead and create a simple example called
ahead and create a simple example called home.html like a very simple homepage
home.html like a very simple homepage for John Harvard for instance let me
for John Harvard for instance let me give myself three paragraphs initially
give myself three paragraphs initially the first of which is just going to have
the first of which is just going to have uh the person name the second paragraph
uh the person name the second paragraph is going to say something like welcome
is going to say something like welcome to my
to my homepage uh to greet visitors and the
homepage uh to greet visitors and the third is going to be like a little
third is going to be like a little footer like copyright uh how about John
footer like copyright uh how about John Harvard 1636 or something like that all
Harvard 1636 or something like that all right let me go back to my other tab as
right let me go back to my other tab as before reload my index there's my new
before reload my index there's my new file home.html and I'll click that and
file home.html and I'll click that and you'll see okay I mean this is sort of
you'll see okay I mean this is sort of 1636 style web page super simple all
1636 style web page super simple all text nothing really interesting going on
text nothing really interesting going on there
there but we can start to style it a little
but we can start to style it a little differently like if the title of the
differently like if the title of the page is John Harvard and then it's
page is John Harvard and then it's welcome to my homepage and then this
welcome to my homepage and then this less important footer why don't we have
less important footer why don't we have the text be large then medium then small
the text be large then medium then small so something arbitrary but a little more
so something arbitrary but a little more nuanced so let me go back to vs code
nuanced so let me go back to vs code here and in my home.html file let me
here and in my home.html file let me introduce not yet the style tag but what
introduce not yet the style tag but what I'm going to call temporarily the style
I'm going to call temporarily the style attribute both indeed exist this one's
attribute both indeed exist this one's simpler and it's going to be correct but
simpler and it's going to be correct but we'll see in a moment not as well
we'll see in a moment not as well designed arguably as is often our
designed arguably as is often our narrative so inside of the style tag you
narrative so inside of the style tag you can put this language called CSS key
can put this language called CSS key value pairs otherwise known as
value pairs otherwise known as properties the only way you know what
properties the only way you know what properties exist what keys exist is by
properties exist what keys exist is by taking a class reading a book looking at
taking a class reading a book looking at an online reference and we're going to
an online reference and we're going to give you just a sampling of what's out
give you just a sampling of what's out there so suppose I want to control the
there so suppose I want to control the font size of this first paragraph I can
font size of this first paragraph I can literally say font Das size in all lower
literally say font Das size in all lower case colon and then a word like large or
case colon and then a word like large or I can specify 12p point or 18 point or
I can specify 12p point or 18 point or something more precise like that like
something more precise like that like from Google Docs or Microsoft Word and
from Google Docs or Microsoft Word and suppose I want to make this text down
suppose I want to make this text down here uh medium well uh I'll do quote
here uh medium well uh I'll do quote unquote font size colon medium and down
unquote font size colon medium and down here I'll do style equals uh font Das
here I'll do style equals uh font Das size small so I'm going to start with
size small so I'm going to start with just these three key value pairs same
just these three key value pairs same key but different values I'll go back to
key but different values I'll go back to my page and in a moment I'll reload and
my page and in a moment I'll reload and it's going to be somewhat subtle but
it's going to be somewhat subtle but watch how the font size do change when I
watch how the font size do change when I reload now all right so got a little
reload now all right so got a little bigger middle one's about the same and
bigger middle one's about the same and the last one is a little smaller what if
the last one is a little smaller what if I want to center it just like many web
I want to center it just like many web pages have the text like this centered
pages have the text like this centered well I can separate these key value
well I can separate these key value pairs with semicolons and I'm sorry
pairs with semicolons and I'm sorry semicolons are kind of sort of back with
semicolons are kind of sort of back with CSS but I can do text-align Colon Center
CSS but I can do text-align Colon Center strictly speaking I don't need the last
strictly speaking I don't need the last semicolon if there's no more key value
semicolon if there's no more key value pairs but I'll just do it to be
pairs but I'll just do it to be consistent uh text align Colon Center
consistent uh text align Colon Center and then down here after another
and then down here after another semicolon text align Colon Center all
semicolon text align Colon Center all right let's go back reload now it's
right let's go back reload now it's going to be much more obvious the change
going to be much more obvious the change and we now have the beginnings of a
and we now have the beginnings of a homepage still pretty basic but at least
homepage still pretty basic but at least it's a little more interesting turns out
it's a little more interesting turns out we can do a little better with the
we can do a little better with the copyright symbol like most computers
copyright symbol like most computers actually have support for a circle with
actually have support for a circle with a c in it but you can't just do that
a c in it but you can't just do that with uh text like this there's different
with uh text like this there's different ways to do this you could copy paste it
ways to do this you could copy paste it from like a website that already has it
from like a website that already has it so you don't have to figure out the mag
so you don't have to figure out the mag iCal keystroke on your Mac or PC but
iCal keystroke on your Mac or PC but there's also in HTML what are called
there's also in HTML what are called entities and you can actually specify
entities and you can actually specify using heximal or decimal codes numbers
using heximal or decimal codes numbers like this H1 169 semicolon after an
like this H1 169 semicolon after an Amper sand and this is a special symbol
Amper sand and this is a special symbol that you can look up in any online
that you can look up in any online reference for like special characters
reference for like special characters that are hard or impossible to type
that are hard or impossible to type manually at your keyboard and this let
manually at your keyboard and this let me zoom in just so it's obvious if I
me zoom in just so it's obvious if I reload now instead of being two
reload now instead of being two parentheses and a c character
parentheses and a c character now it's a proper Copyright symbol so
now it's a proper Copyright symbol so you'll see these out there they're not
you'll see these out there they're not necessarily that frequently used
necessarily that frequently used nowadays but it's good to know that they
nowadays but it's good to know that they exist but let me go back now to my code
exist but let me go back now to my code and propose that while correct uh this
and propose that while correct uh this is arguably not very welld designed and
is arguably not very welld designed and even if you've never seen HTML never
even if you've never seen HTML never seen CSS before what Instinct might you
seen CSS before what Instinct might you have for why this is poorly
have for why this is poorly designed
designed yeah there's repetition right in general
yeah there's repetition right in general in the past several weeks see python SQL
in the past several weeks see python SQL like repetition generally bad and and
like repetition generally bad and and sloppy and it's not going to scale well
sloppy and it's not going to scale well so the repetition I think you're
so the repetition I think you're probably alluding to is textalign Center
probably alluding to is textalign Center textalign Center textalign Center well
textalign Center textalign Center well we can factor that out in CSS the C in
we can factor that out in CSS the C in CSS means cascading and this means that
CSS means cascading and this means that if you move some properties to like a
if you move some properties to like a parent or a grandparent uh the children
parent or a grandparent uh the children or grandchildren will inherit those
or grandchildren will inherit those properties that is they will Cascade
properties that is they will Cascade down the family tree so to speak and so
down the family tree so to speak and so so let me go ahead and remove all of
so let me go ahead and remove all of these since I claim they're indeed
these since I claim they're indeed redundant and let me preserve just one
redundant and let me preserve just one of them by well let me do this it's not
of them by well let me do this it's not quite right to put a a paragraph inside
quite right to put a a paragraph inside of a paragraph like that's just not a
of a paragraph like that's just not a thing in English writing or in writing
thing in English writing or in writing more generally so I'm going to do one
more generally so I'm going to do one thing first it turns out that these two
thing first it turns out that these two are arguably not paragraphs right this
are arguably not paragraphs right this is like a header the body the essence of
is like a header the body the essence of the page and then the footer so if a
the page and then the footer so if a paragraph isn't quite the right English
paragraph isn't quite the right English semantics you can actually use more
semantics you can actually use more generically a tag that's all over the
generically a tag that's all over the internet called div for division of the
internet called div for division of the page and this is just a very generic
page and this is just a very generic term for like a big rectangular region
term for like a big rectangular region that divides the page again and again
that divides the page again and again just so that you can think about
just so that you can think about different regions now that I have div
different regions now that I have div which really has no more meaning than
which really has no more meaning than that it's a division of the page
that it's a division of the page interpret as you will now I can have
interpret as you will now I can have multiple ones of these and let me go
multiple ones of these and let me go ahead and open a div tag here let me
ahead and open a div tag here let me close a new div tag here and then just
close a new div tag here and then just to keep everything tidy I'm going to
to keep everything tidy I'm going to highlight everything in between and hit
highlight everything in between and hit Tab and that just automatically indents
Tab and that just automatically indents everything for me now I have a three
everything for me now I have a three divs inside of another div and that's
divs inside of another div and that's totally fine this is very commonly done
totally fine this is very commonly done now I'm going to do this style equals
now I'm going to do this style equals quote unquote text align Colon Center
quote unquote text align Colon Center semicolon or not and now I have some
semicolon or not and now I have some cascading capabilities now the parent of
cascading capabilities now the parent of those three children John Harvard welome
those three children John Harvard welome to my homepage and the copyright will
to my homepage and the copyright will now all inherit that property so when I
now all inherit that property so when I hit reload nothing aesthetically has
hit reload nothing aesthetically has changed whoops sorry
changed whoops sorry um I should have done reload slightly
um I should have done reload slightly earlier when you use a div instead of a
earlier when you use a div instead of a paragraph it actually gets rid of the
paragraph it actually gets rid of the par uh the space between those
par uh the space between those paragraphs it just sandwiches them a
paragraphs it just sandwiches them a little closer together I can fix this in
little closer together I can fix this in another way but that aside everything is
another way but that aside everything is still centered and the text is still
still centered and the text is still large medium and small but I should have
large medium and small but I should have called out that change in the paragraph
called out that change in the paragraph spacing but we could bring that back
spacing but we could bring that back before long if we wanted now what more
before long if we wanted now what more could I do to maybe improve this well
could I do to maybe improve this well strictly speaking I don't really need
strictly speaking I don't really need that parent div right because these
that parent div right because these three divs inside already had a parent
three divs inside already had a parent so let me actually get rid of that new
so let me actually get rid of that new div just undo what I did I'll highlight
div just undo what I did I'll highlight this and if you haven't seen this trick
this and if you haven't seen this trick shift tab will unindent nicely which is
shift tab will unindent nicely which is perhaps helpful I could just put that
perhaps helpful I could just put that text align Center on the body tag so
text align Center on the body tag so text align Colon Center quote unquote
text align Colon Center quote unquote this two would work as well so long as
this two would work as well so long as you go up the family tree so to speak
you go up the family tree so to speak reload and now indeed there's nothing
reload and now indeed there's nothing aesthetically that has changed this time
aesthetically that has changed this time but it turns out nowadays the web is
but it turns out nowadays the web is getting a little more sophisticated and
getting a little more sophisticated and even though you will see so many
even though you will see so many examples online and tutorials and books
examples online and tutorials and books using div div div div all over the place
using div div div div all over the place there are newer semantic tags semantic
there are newer semantic tags semantic just means they have more meaning than
just means they have more meaning than this generic notion of a division and if
this generic notion of a division and if you look up the a documentation for HTML
you look up the a documentation for HTML you'll see that if you want to have a
you'll see that if you want to have a header on a page not a heading like H1
header on a page not a heading like H1 H2 but a header there's literally
H2 but a header there's literally nowadays a header tag and this is
nowadays a header tag and this is marginally better because it now says
marginally better because it now says what it is search engines like Google
what it is search engines like Google and Bing can detect oh that's the header
and Bing can detect oh that's the header of the page maybe we should use this and
of the page maybe we should use this and give it more prominence in the search
give it more prominence in the search results you can then have a main part of
results you can then have a main part of the page so literally a tag called main
the page so literally a tag called main nowadays you can literally have a footer
nowadays you can literally have a footer of the page and again these are often
of the page and again these are often useful for screen readers to help recite
useful for screen readers to help recite things verbally for folks who might
things verbally for folks who might otherwise not be able to read them and
otherwise not be able to read them and probably these screen readers might
probably these screen readers might highlight the header and the main part
highlight the header and the main part but maybe not might not spend time for
but maybe not might not spend time for the user on the footer which is arguably
the user on the footer which is arguably a little less important semantically
a little less important semantically usually um or search engines again now
usually um or search engines again now know what's the header what's the footer
know what's the header what's the footer what's the main part of the page so they
what's the main part of the page so they know what to search and analyze so this
know what to search and analyze so this would arguably be a a better design
would arguably be a a better design nowadays as
nowadays as well but what else remains as a problem
well but what else remains as a problem well this is now getting a little bit
well this is now getting a little bit more subtle and takes some experience
more subtle and takes some experience but this practice of putting HTML and
but this practice of putting HTML and CSS all in the same file it's a little
CSS all in the same file it's a little sloppy why because it means I'm
sloppy why because it means I'm co-mingling my data with the
co-mingling my data with the presentation thereof like the juicy
presentation thereof like the juicy stuff I care about like John Harvard and
stuff I care about like John Harvard and the phrase welcome to my homepage and
the phrase welcome to my homepage and all of the Aesthetics that I might want
all of the Aesthetics that I might want to change over time and honestly because
to change over time and honestly because everything is currently in one big file
everything is currently in one big file it's going to make it really hard for me
it's going to make it really hard for me to collaborate with a classmate or a
to collaborate with a classmate or a colleague at work so that maybe I do the
colleague at work so that maybe I do the HTML they do the CSS like uhuh not if
HTML they do the CSS like uhuh not if you're all working in the same file it
you're all working in the same file it would be a nightmare even if you use vs
would be a nightmare even if you use vs code sharing feature like Google Docs
code sharing feature like Google Docs and both are typing at the same time
and both are typing at the same time like you're going to mess up somehow
like you're going to mess up somehow it'd be nice if we could separate these
it'd be nice if we could separate these two languages well one way to do that
two languages well one way to do that would be as follows let me get rid of
would be as follows let me get rid of all of the style tags sorry style
all of the style tags sorry style attributes that I've added up until
attributes that I've added up until now on all four now of these tags and
now on all four now of these tags and let me introduce the style tag that we
let me introduce the style tag that we saw on the slide earlier instead I'm
saw on the slide earlier instead I'm going to go up here into the head of the
going to go up here into the head of the page which is where technically these
page which is where technically these style tags must go so that they're
style tags must go so that they're already loaded into memory before the
already loaded into memory before the body is even analyzed by the browser and
body is even analyzed by the browser and inside of the the style tag I'm actually
inside of the the style tag I'm actually going to select the HTML elements that I
going to select the HTML elements that I want to stylize if you will so if I want
want to stylize if you will so if I want to change the body's Aesthetics I'm
to change the body's Aesthetics I'm going to literally type the name of that
going to literally type the name of that tag body and then I'm sorry curly braces
tag body and then I'm sorry curly braces are back also from c u inside of these
are back also from c u inside of these curly braces I'm going to put text align
curly braces I'm going to put text align Center so the key value pairs are the
Center so the key value pairs are the same the only new thing I've done is
same the only new thing I've done is I've moved some of the syntax up to this
I've moved some of the syntax up to this new style tag in the head if I want to
new style tag in the head if I want to now control the header tag as well I can
now control the header tag as well I can use the same curly braces this is
use the same curly braces this is convention to put the open curly brace
convention to put the open curly brace on the same line the closed curly brace
on the same line the closed curly brace on another the browser doesn't really
on another the browser doesn't really care but this is a common CSS style
care but this is a common CSS style convention I'm going to do font size
convention I'm going to do font size large semicolon then for the main tag
large semicolon then for the main tag I'm going to do font size medium and
I'm going to do font size medium and then for the footer tag I'm going to do
then for the footer tag I'm going to do font size small so same exact thing and
font size small so same exact thing and it's admittedly a little bit more
it's admittedly a little bit more verbose it's taking up more lines of
verbose it's taking up more lines of code it doesn't all quite fit on the
code it doesn't all quite fit on the screen but if you scroll back down now
screen but if you scroll back down now and you'll acquire an eye for this this
and you'll acquire an eye for this this is just better like it's just more
is just better like it's just more compact it's more readable the the
compact it's more readable the the content the data jumps out and there's
content the data jumps out and there's no visual distractions like the CSS
no visual distractions like the CSS properties as before upside here too is
properties as before upside here too is that we don't actually need to uh this
that we don't actually need to uh this doesn't actually change the Aesthetics
doesn't actually change the Aesthetics if I reload the same page it still looks
if I reload the same page it still looks the same but I've taken a step toward
the same but I've taken a step toward some slightly better design but let me
some slightly better design but let me propose that there's other ways to do
propose that there's other ways to do this too we just selected things by way
this too we just selected things by way of their uh type so that was a so-called
of their uh type so that was a so-called type selector when I literally just
type selector when I literally just specifi the type of tag body header main
specifi the type of tag body header main footer but there's other ways that now
footer but there's other ways that now we can lay the foundation for making
we can lay the foundation for making reusable CSS that you and colleagues and
reusable CSS that you and colleagues and classmates can use and reuse in multiple
classmates can use and reuse in multiple files and even in multiple projects so
files and even in multiple projects so let me actually go ahead and do this
let me actually go ahead and do this instead of just very explicitly saying I
instead of just very explicitly saying I want the body to be centered let me
want the body to be centered let me invent an adjective if you will and let
invent an adjective if you will and let me change this to do centered and this
me change this to do centered and this new uh this new vocabulary word centered
new uh this new vocabulary word centered will literally mean texal Center let me
will literally mean texal Center let me go ahead here and I'm just going to
go ahead here and I'm just going to create a new adjective called large a
create a new adjective called large a new adjective called medium and a new
new adjective called medium and a new adjective called small they are
adjective called small they are deliberately consistent with what the
deliberately consistent with what the properties do but these are now my own
properties do but these are now my own vocabulary words and they are called
vocabulary words and they are called classes so a class is just a collection
classes so a class is just a collection of key value
of key value a collection of properties that you get
a collection of properties that you get to invent for yourself and what it lets
to invent for yourself and what it lets you do now is this now if I want the
you do now is this now if I want the whole body to be centered I can add this
whole body to be centered I can add this tag which we actually saw briefly in
tag which we actually saw briefly in Yale's HTML class equals centered down
Yale's HTML class equals centered down here in the header if I want this to be
here in the header if I want this to be large I can say class equals quote
large I can say class equals quote unquote large down here on Main I can
unquote large down here on Main I can say class equals quote unquote medium
say class equals quote unquote medium and down here I can have class equals
and down here I can have class equals quote unquote small now I have taken one
quote unquote small now I have taken one step backward by read addding some of
step backward by read addding some of the Aesthetics to the page but it's not
the Aesthetics to the page but it's not the actual properties it's not the key
the actual properties it's not the key value pairs it's now more semantically
value pairs it's now more semantically nice because now I just know from
nice because now I just know from reading the HTML what these things are
reading the HTML what these things are going to look like whereas the
going to look like whereas the implementation details for all four of
implementation details for all four of those adjectives is now relegated up
those adjectives is now relegated up above and these are literally my words I
above and these are literally my words I could change it to Fu and use class
could change it to Fu and use class equals quote unquote Foo but obviously
equals quote unquote Foo but obviously that would not be the best choice of
that would not be the best choice of words in this case all right any
words in this case all right any questions on this this now is what we
questions on this this now is what we would call a class selector by using
would call a class selector by using literally the dot even though the dot
literally the dot even though the dot does not appear elsewhere but dot means
does not appear elsewhere but dot means this is a
this is a class these are not always the best
class these are not always the best syntactic design decisions that the
syntactic design decisions that the world makes all right well one last
world makes all right well one last trick then notice that this is a little
trick then notice that this is a little Annoying that I'm still working in the
Annoying that I'm still working in the same file and if my classmate wants to
same file and if my classmate wants to clean up my Aesthetics make my homepage
clean up my Aesthetics make my homepage look way better if my colleague wants to
look way better if my colleague wants to do the same wouldn't it be nice if we
do the same wouldn't it be nice if we could actually move all of this code to
could actually move all of this code to a different function file like a python
a different function file like a python library or a c header file well you can
library or a c header file well you can let me go ahead and delete that whole
let me go ahead and delete that whole style tag let me add a confusingly named
style tag let me add a confusingly named link tag the href of which let's call a
link tag the href of which let's call a new file styles.css and let's say that
new file styles.css and let's say that the relationship of that file is that of
the relationship of that file is that of stylesheet so this is a term of Art in
stylesheet so this is a term of Art in the world of web development a
the world of web development a stylesheet is a text file that contains
stylesheet is a text file that contains lots of styles lots of CSS properties
lots of styles lots of CSS properties let me open my terminal real fast
let me open my terminal real fast and let me do code of
and let me do code of styles.css
styles.css enter and in this file I'm going to
enter and in this file I'm going to paste all of those same lines as earlier
paste all of those same lines as earlier but now they're in a separate file and
but now they're in a separate file and indeed if I hide my terminal window and
indeed if I hide my terminal window and I give this file to a colleague they can
I give this file to a colleague they can now work on the Aesthetics of the page
now work on the Aesthetics of the page and make things a lot prettier than this
and make things a lot prettier than this maybe use specific font sizes maybe add
maybe use specific font sizes maybe add colors and the like whereas I can focus
colors and the like whereas I can focus entirely on the HTML because this file
entirely on the HTML because this file now will reference that other and if I
now will reference that other and if I go back to my other Tab and reload Cod
go back to my other Tab and reload Cod the content's going to be exactly the
the content's going to be exactly the same but now I'm using some separate
same but now I'm using some separate file
file instead any questions now about these
instead any questions now about these techniques
techniques here no all right so with that said let
here no all right so with that said let me show just one example now of what I
me show just one example now of what I called a moment ago Frameworks and this
called a moment ago Frameworks and this is where web development gets kind of
is where web development gets kind of fun at least if you like this especially
fun at least if you like this especially if you like the sort of logical design
if you like the sort of logical design the presentation of information you care
the presentation of information you care about but you really don't want to
about but you really don't want to struggle with like font sizes and colors
struggle with like font sizes and colors and getting everything Pixel Perfect so
and getting everything Pixel Perfect so to speak let me propose that I open up
to speak let me propose that I open up here an example in just a moment in vs
here an example in just a moment in vs code that I prepared in advance and this
code that I prepared in advance and this one is going to be an opportunity to
one is going to be an opportunity to consider how you might take some of the
consider how you might take some of the data from last week which we had wherein
data from last week which we had wherein we collected everyone's favorites and
we collected everyone's favorites and lay it out in a really big HTML table so
lay it out in a really big HTML table so I wrote this out in advance because it
I wrote this out in advance because it was a huge amount of data but it's the
was a huge amount of data but it's the same data from the Google form from last
same data from the Google form from last week and you'll see already the hints of
week and you'll see already the hints of a table tag and these TRS and I added a
a table tag and these TRS and I added a few other tags for Aesthetics it turns
few other tags for Aesthetics it turns out when you have a more visually
out when you have a more visually interesting header for your table
interesting header for your table there's another tag called T head
there's another tag called T head there's another tag called T body these
there's another tag called T body these are not all that intellectually
are not all that intellectually interesting I just read the
interesting I just read the documentation and realized oh to make
documentation and realized oh to make things prettier I need a t head a t body
things prettier I need a t head a t body and so forth but what's interesting here
and so forth but what's interesting here is that if I go to my index here and
is that if I go to my index here and open this file called favorites. HTML
open this file called favorites. HTML here is all of the data from last week's
here is all of the data from last week's Google spreadsheet which we exported as
Google spreadsheet which we exported as CS and I manually before class converted
CS and I manually before class converted to just HTML it's indeed a table but
to just HTML it's indeed a table but it's really not pretty like the columns
it's really not pretty like the columns are really close together it's kind of
are really close together it's kind of hard to distinguish one row from another
hard to distinguish one row from another but this is just raw HTML written by me
but this is just raw HTML written by me now I could use CSS and some of the
now I could use CSS and some of the tricks we just saw to maybe change font
tricks we just saw to maybe change font size there's ways to change color
size there's ways to change color background color and a lot of things
background color and a lot of things like that but honestly other surely
like that but honestly other surely other people in the world have presented
other people in the world have presented tabular data in pretty ways right I've
tabular data in pretty ways right I've been to many websites that have prettier
been to many websites that have prettier tables than M can I maybe use someone
tables than M can I maybe use someone else's framework someone else's CSS
else's framework someone else's CSS include it in my page but then stand on
include it in my page but then stand on their shoulders and just make my stuff
their shoulders and just make my stuff look prettier well I dare say I can let
look prettier well I dare say I can let me go ahead here and semi secretly open
me go ahead here and semi secretly open up vs code again and let me grab a
up vs code again and let me grab a slightly different version of favorites.
slightly different version of favorites. HTML that I also opened in advance
HTML that I also opened in advance wherein I add this line of code instead
wherein I add this line of code instead give me just a moment to foreground this
give me just a moment to foreground this version and the data is is all the same
version and the data is is all the same as before but I've added one of these
as before but I've added one of these link tags and I'm not linking to my own
link tags and I'm not linking to my own styles.css I'm using a popular Library
styles.css I'm using a popular Library called bootstrap and bootstrap is just
called bootstrap and bootstrap is just one of many popular libraries out there
one of many popular libraries out there free at that that has a whole bunch of
free at that that has a whole bunch of CSS files and soon JavaScript files that
CSS files and soon JavaScript files that you can just use for free in your own
you can just use for free in your own projects personally or professionally
projects personally or professionally that just make things look and behave
that just make things look and behave better without you having to reinvent
better without you having to reinvent Wheels now to access their CSS I had to
Wheels now to access their CSS I had to read their documentation and grab this
read their documentation and grab this very long URL here but it's the same
very long URL here but it's the same idea link a forre equals quote unquote
idea link a forre equals quote unquote something and I read their documentation
something and I read their documentation and they told me to add this they told
and they told me to add this they told me that if I want my tables to be
me that if I want my tables to be prettier I have to add a class attribute
prettier I have to add a class attribute to my own table tag and specify a little
to my own table tag and specify a little weirdly but this is what bootstrap told
weirdly but this is what bootstrap told me to do a class called table and that
me to do a class called table and that will make it a prettier bootstrap table
will make it a prettier bootstrap table and if I want to stripe it like every
and if I want to stripe it like every other row is gray instead of white just
other row is gray instead of white just to make it pop a little more visually I
to make it pop a little more visually I can also add a second class separated by
can also add a second class separated by a space called table striped that's all
a space called table striped that's all I did I added line five and I changed
I did I added line five and I changed line nine and that is it the rest of the
line nine and that is it the rest of the hundreds of lines in favorites. HTML are
hundreds of lines in favorites. HTML are the same but if I go back here now and
the same but if I go back here now and reload the browser now thanks to
reload the browser now thanks to bootstrap voila like it's much prettier
bootstrap voila like it's much prettier now I can zoom out and that changes the
now I can zoom out and that changes the font size just locally for me and even
font size just locally for me and even if you don't love their Aesthetics I
if you don't love their Aesthetics I mean this is easily better than my own
mean this is easily better than my own there and it turns out we can can do
there and it turns out we can can do even better by adding interactivity to
even better by adding interactivity to this too but to do that we're going to
this too but to do that we're going to need one final language for today and
need one final language for today and this one is an actual programming
this one is an actual programming language and we won't use it all that
language and we won't use it all that much in cs50 but we introduce it here as
much in cs50 but we introduce it here as we begin web stuff because there's just
we begin web stuff because there's just so many free libraries and professional
so many free libraries and professional libraries that you can use just to make
libraries that you can use just to make your web applications fancier and more
your web applications fancier and more interactive mobile applications as well
interactive mobile applications as well increasingly use HTML CSS and JavaScript
increasingly use HTML CSS and JavaScript to power our iPhones and Android devices
to power our iPhones and Android devices as well so a quick tour some syntax and
as well so a quick tour some syntax and then we'll conclude with just some
then we'll conclude with just some hopefully inspiring examples to give you
hopefully inspiring examples to give you a taste of what JavaScript can do so
a taste of what JavaScript can do so JavaScript supports conditionals just
JavaScript supports conditionals just like C and python before it if we rewind
like C and python before it if we rewind to our scratch days here of course is a
to our scratch days here of course is a conditional here is the corresponding
conditional here is the corresponding JavaScript code as of today it's pretty
JavaScript code as of today it's pretty much identical to see with the syntax
much identical to see with the syntax here uh if we had an if an if else in
here uh if we had an if an if else in scratch it looked like this in
scratch it looked like this in JavaScript it's going to look like this
JavaScript it's going to look like this instead so it's a bit of a regression V
instead so it's a bit of a regression V ofv python like the the parentheses are
ofv python like the the parentheses are back the curly braces are back the
back the curly braces are back the semicolons I mentioned in CSS are also
semicolons I mentioned in CSS are also back in JavaScript potentially but it's
back in JavaScript potentially but it's familiar is the point here and it's a
familiar is the point here and it's a different language that's frequently
different language that's frequently used for the web whereas you can't use
used for the web whereas you can't use python in the ways we're about to use
python in the ways we're about to use JavaScript it just wasn't designed for
JavaScript it just wasn't designed for that purpose meanwhile if you have an if
that purpose meanwhile if you have an if El it's else if else in scratch well in
El it's else if else in scratch well in JavaScript just like in C it's going to
JavaScript just like in C it's going to look like this instead variables in
look like this instead variables in JavaScript of course are a thing too and
JavaScript of course are a thing too and in scratch we might have initialized a
in scratch we might have initialized a counter variable to Z in JavaScript a
counter variable to Z in JavaScript a few different ways to do this and just
few different ways to do this and just the for now the keyword is let it's sort
the for now the keyword is let it's sort of a polite way of asking for a variable
of a polite way of asking for a variable let uh counter equal zero semicolon so
let uh counter equal zero semicolon so you don't mention the type but you do
you don't mention the type but you do use a keyword here in this case called
use a keyword here in this case called let if you want to increment counter by
let if you want to increment counter by one few different ways in JavaScript you
one few different ways in JavaScript you can do this just like in C in JavaScript
can do this just like in C in JavaScript you can do this just like in C and in
you can do this just like in C and in Python in JavaScript you can also get
Python in JavaScript you can also get this so plus plus is back so maybe that
this so plus plus is back so maybe that counterbalances the other syntax as well
counterbalances the other syntax as well that was was not the case in Python
that was was not the case in Python Loops are back of course in JavaScript
Loops are back of course in JavaScript whereas in scratch you could repeat
whereas in scratch you could repeat three times like this in JavaScript it's
three times like this in JavaScript it's pretty much just like C the only
pretty much just like C the only difference here is that you say let
difference here is that you say let instead of int for an example like this
instead of int for an example like this meanwhile if you want to do something
meanwhile if you want to do something forever in scratch in JavaScript just
forever in scratch in JavaScript just like in C you say while true in this
like in C you say while true in this case so this is to say we're sort of
case so this is to say we're sort of comfortable spending relatively little
comfortable spending relatively little time on JavaScript at least for today's
time on JavaScript at least for today's purposes because syntactically it's
purposes because syntactically it's really the same as we've seen before
really the same as we've seen before with maybe a slight variance here or
with maybe a slight variance here or there but what's interesting today
there but what's interesting today arguably is just what kinds of things
arguably is just what kinds of things you can do with it so with that said
you can do with it so with that said what kinds of things can we do it's all
what kinds of things can we do it's all comes back to this picture if this is a
comes back to this picture if this is a simple web page on the left and this is
simple web page on the left and this is the corresponding tree or Dom document
the corresponding tree or Dom document object model on the right that is the
object model on the right that is the tree the browser automatically creates
tree the browser automatically creates in memory or RAM for you JavaScript is
in memory or RAM for you JavaScript is now a proper programming language that
now a proper programming language that lets us dynamically manipul ulate like
lets us dynamically manipul ulate like read data from this change this and this
read data from this change this and this is how Google for instance implements
is how Google for instance implements your inbox they might have uh in your
your inbox they might have uh in your inbox it's like a table so TR TR TR TR
inbox it's like a table so TR TR TR TR probably something like that or heck
probably something like that or heck maybe div div div div using JavaScript
maybe div div div div using JavaScript anytime they realize someone sent you
anytime they realize someone sent you new mail they can create a new node a
new mail they can create a new node a new rectangle in memory and you the
new rectangle in memory and you the human see a new div or a new TR again
human see a new div or a new TR again and again and again so with JavaScript
and again and again so with JavaScript you just have the ability to control the
you just have the ability to control the user's experience and instead of like
user's experience and instead of like I've been doing constantly hitting
I've been doing constantly hitting reload in the page to see some new
reload in the page to see some new content to see some new content
content to see some new content JavaScript can be running 247 so that
JavaScript can be running 247 so that you can actually see all of these
you can actually see all of these changes live all right let's go about
changes live all right let's go about writing some JavaScript code now instead
writing some JavaScript code now instead of writing it on the server and
of writing it on the server and executing it on the server we're going
executing it on the server we're going to actually use a very common Paradigm
to actually use a very common Paradigm whereas JavaScript is actually executed
whereas JavaScript is actually executed in the browser client side that is to
in the browser client side that is to say we can actually start writing some
say we can actually start writing some JavaScript code inside of our own HTML
JavaScript code inside of our own HTML file so that when a user visits that web
file so that when a user visits that web page with their browser not only is the
page with their browser not only is the HTML and any CSS downloaded to the
HTML and any CSS downloaded to the user's browser so is that JavaScript
user's browser so is that JavaScript code so that it's executed indeed client
code so that it's executed indeed client side on the browser rather than server
side on the browser rather than server side as has been the case with python in
side as has been the case with python in previous weeks well where do we go about
previous weeks well where do we go about writing some JavaScript code let's go
writing some JavaScript code let's go ahead and revisit hello.html which
ahead and revisit hello.html which previously was a completely static
previously was a completely static example that literally just said hello
example that literally just said hello title hello body indeed if I open this
title hello body indeed if I open this up using HTTP server and view it now in
up using HTTP server and view it now in a separate tab all it said was exactly
a separate tab all it said was exactly that hello title in the tab and hello
that hello title in the tab and hello body in the main part of the viewport so
body in the main part of the viewport so to speak well let's make this example a
to speak well let's make this example a little more Dynamic so it doesn't just
little more Dynamic so it doesn't just say hello body but maybe says hello to
say hello body but maybe says hello to an actual person so let's go ahead and
an actual person so let's go ahead and do this let's go ahead and remove the
do this let's go ahead and remove the hardcoded hello body and let's actually
hardcoded hello body and let's actually go ahead here and use a form tag but
go ahead here and use a form tag but we're not going to use this form in the
we're not going to use this form in the usual way whereby the data gets sent all
usual way whereby the data gets sent all the way back to the server we're going
the way back to the server we're going to Leverage control over this form
to Leverage control over this form client side instead so I'm going to go
client side instead so I'm going to go ahead and create this open form tag
ahead and create this open form tag close form tag inside of that let me
close form tag inside of that let me give myself a text input that's going to
give myself a text input that's going to have autocomplete equals quote unquote
have autocomplete equals quote unquote off just to ensure that what I
off just to ensure that what I previously type in my examples doesn't
previously type in my examples doesn't reappear accidentally we're going to
reappear accidentally we're going to autofocus it so that the cursor is
autofocus it so that the cursor is blinking right there in that text box
blinking right there in that text box and this time I'm going to go ahead and
and this time I'm going to go ahead and give it how about a uh placeholder of
give it how about a uh placeholder of quote unquote name to make clear that
quote unquote name to make clear that prompting the user for their name and
prompting the user for their name and then the type of this text box will be
then the type of this text box will be the default or more explicitly here text
the default or more explicitly here text and then I'm going to have as we've seen
and then I'm going to have as we've seen before a button the type of which is
before a button the type of which is submit also our typical default and then
submit also our typical default and then inside of this button is going to be the
inside of this button is going to be the label how about something like uh how
label how about something like uh how about we'll call this greet so that's
about we'll call this greet so that's what the button will actually say well
what the button will actually say well let me actually go back into my browser
let me actually go back into my browser tab let me reload this page and we
tab let me reload this page and we should now see a relatively simple form
should now see a relatively simple form whereby I have the cursor blinking on a
whereby I have the cursor blinking on a text inp input prompting the user for
text inp input prompting the user for their name and then a greet button that
their name and then a greet button that I can click but if I click this button
I can click but if I click this button now it's not going to do anything useful
now it's not going to do anything useful because I haven't written any code to
because I haven't written any code to tell the browser what to do when I click
tell the browser what to do when I click that button but it turns out there's all
that button but it turns out there's all sorts of events in the world of
sorts of events in the world of JavaScript that you can listen for so to
JavaScript that you can listen for so to speak in fact here's just a list of some
speak in fact here's just a list of some of them anytime something changes in a
of them anytime something changes in a form field anytime the user clicks or
form field anytime the user clicks or drags on something anytime the user
drags on something anytime the user presses a key and maybe lifts their
presses a key and maybe lifts their finger up anytime the mouse goes down or
finger up anytime the mouse goes down or over or up on top of something or
over or up on top of something or anytime a form is submitted those are
anytime a form is submitted those are events in the same way that we talked
events in the same way that we talked about events back in week zero in
about events back in week zero in scratch and in JavaScript just like in
scratch and in JavaScript just like in scratch where you can do something when
scratch where you can do something when green flag clicked in JavaScript you can
green flag clicked in JavaScript you can write code that actually listens for any
write code that actually listens for any of these events or more so with that
of these events or more so with that said let's go back to vs code here and
said let's go back to vs code here and let's make a couple of changes instead
let's make a couple of changes instead let's go ahead and add to this form a
let's go ahead and add to this form a new attribute that's not the best way to
new attribute that's not the best way to do it but it's perhaps the simplest for
do it but it's perhaps the simplest for version one here and let's say onsubmit
version one here and let's say onsubmit do the following so onsubmit is an HTML
do the following so onsubmit is an HTML attribute and curiously its value inside
attribute and curiously its value inside of the quotes there can actually be some
of the quotes there can actually be some JavaScript code and let's go ahead now
JavaScript code and let's go ahead now and let's assume there exists a function
and let's assume there exists a function in the world called greet and what I
in the world called greet and what I want to do is call that function right
want to do is call that function right then and there well now in JavaScript
then and there well now in JavaScript how do I go about making that function
how do I go about making that function exist it doesn't come out of the box
exist it doesn't come out of the box just like print might or say might in
just like print might or say might in python or scratch respectively but I can
python or scratch respectively but I can do this let me go up into the head of
do this let me go up into the head of this page inside of a script tag here
this page inside of a script tag here both open and close let me actually
both open and close let me actually write some JavaScript code and just so
write some JavaScript code and just so it stands out I'm going to give myself a
it stands out I'm going to give myself a couple of blank lines though not
couple of blank lines though not strictly necessary and let me Define a
strictly necessary and let me Define a new function in JavaScript called greet
new function in JavaScript called greet and this is the syntax in JavaScript for
and this is the syntax in JavaScript for creating your own function similar in
creating your own function similar in Python instead of saying defa in
Python instead of saying defa in JavaScript you just say function then
JavaScript you just say function then the name of the function and any
the name of the function and any arguments within the parentheses
arguments within the parentheses thereafter but I'm not going to pass in
thereafter but I'm not going to pass in any here then inside of curly braces
any here then inside of curly braces what I'm going to do is use a built-in
what I'm going to do is use a built-in JavaScript function that comes with any
JavaScript function that comes with any browser called alert it's not the best
browser called alert it's not the best or prettiest user interface but for now
or prettiest user interface but for now it's going to get the job done what do I
it's going to get the job done what do I want to say to the user well let's first
want to say to the user well let's first just say something simple like hello
just say something simple like hello comma World close quote semicolon
comma World close quote semicolon thereby alerting the user with precisely
thereby alerting the user with precisely that message now what I'm going to do
that message now what I'm going to do down here is make one other change I
down here is make one other change I don't want this form to actually get
don't want this form to actually get submitted to the server just like we've
submitted to the server just like we've seen in the past when you submit a form
seen in the past when you submit a form it often goes to something like
it often goes to something like google.com/ search I actually want my
google.com/ search I actually want my JavaScript code to take control over the
JavaScript code to take control over the entire user experience of this form so
entire user experience of this form so that I'm just using the form as a user
that I'm just using the form as a user input mechanism not to actually send via
input mechanism not to actually send via get or post this data to some other
get or post this data to some other server including my own so this is going
server including my own so this is going to look a little ugly but after calling
to look a little ugly but after calling greet I'm actually going to do this
greet I'm actually going to do this which I've read the documentation about
which I've read the documentation about and I know that if you actually hardcode
and I know that if you actually hardcode return false here that just tells the
return false here that just tells the browser please don't actually submit the
browser please don't actually submit the form only call the Greet function all
form only call the Greet function all right well let me go back to my browser
right well let me go back to my browser here let me reload this because I need
here let me reload this because I need to download the latest version of the
to download the latest version of the JavaScript code and I'm just going to go
JavaScript code and I'm just going to go ahead without even typing my name I'm
ahead without even typing my name I'm going to click on the Greet button and
going to click on the Greet button and you'll see that Al be it a little
you'll see that Al be it a little cryptically at the top we see an alert
cryptically at the top we see an alert that says hello world there's my ugly
that says hello world there's my ugly URL of my code space there at the moment
URL of my code space there at the moment but we do indeed see that string but
but we do indeed see that string but what I haven't of course done is taken
what I haven't of course done is taken any actual name from the user so how can
any actual name from the user so how can we go about doing that well ideally I
we go about doing that well ideally I want to alert the user with hello comma
want to alert the user with hello comma David or hello comma Carter whatever a
David or hello comma Carter whatever a name I type into that box so how can I
name I type into that box so how can I go about doing that well let me create a
go about doing that well let me create a variable called name and let me set it
variable called name and let me set it equal to this function call
equal to this function call document. query
document. query selector that comes with JavaScript
selector that comes with JavaScript itself let me then in parenthesis pass
itself let me then in parenthesis pass in an argument that is going to be huh
in an argument that is going to be huh the ID the unique I need a unique
the ID the unique I need a unique identifier for the thing I want to
identifier for the thing I want to select so let me actually go back to my
select so let me actually go back to my HTML code here and instead of giving
HTML code here and instead of giving this form field a name like Q for query
this form field a name like Q for query let me actually use another HTML
let me actually use another HTML attribute called ID where now I can call
attribute called ID where now I can call this anything I want and for clarity I'm
this anything I want and for clarity I'm just going to call this input element
just going to call this input element uniquely name now up here in query
uniquely name now up here in query selector just like in CSS where you can
selector just like in CSS where you can use hashes and dots and other symbology
use hashes and dots and other symbology in order to select certain nodes in your
in order to select certain nodes in your Dom that is rectangles in that memory
Dom that is rectangles in that memory tree well I can go ahead and select hash
tree well I can go ahead and select hash name which again is just the Syntax for
name which again is just the Syntax for uniquely selecting the element whose ID
uniquely selecting the element whose ID is in this case name so you have the
is in this case name so you have the hash up here you don't need the hash as
hash up here you don't need the hash as the value of the attribute down here on
the value of the attribute down here on line 20 and now if I want to actually
line 20 and now if I want to actually get the value of that text box I
get the value of that text box I literally just say do value so document
literally just say do value so document refers to the whole web page itself
refers to the whole web page itself query selector is a function that's
query selector is a function that's built into that object so to speak and
built into that object so to speak and the value accessible via value just like
the value accessible via value just like a c stru or even a python class allows
a c stru or even a python class allows me to go inside of that text field and
me to go inside of that text field and get whatever the value the user has
get whatever the value the user has typed in now as I've uh been able to do
typed in now as I've uh been able to do in languages like python pretty Le
in languages like python pretty Le fairly readily I can concatenate this
fairly readily I can concatenate this name onto the string hello comma space
name onto the string hello comma space so as to form a complete phrase and
so as to form a complete phrase and you'll notice here that I'm actually
you'll notice here that I'm actually using single quotes in my JavaScript
using single quotes in my JavaScript double quotes in my HTML this is perhaps
double quotes in my HTML this is perhaps a common convention in JavaScript the
a common convention in JavaScript the language does not care if you use double
language does not care if you use double quotes or single quotes but I dare say
quotes or single quotes but I dare say single quotes are just more common and
single quotes are just more common and so that's what I've done here all right
so that's what I've done here all right now as always I'm going to cross my
now as always I'm going to cross my fingers go back to this page I'm going
fingers go back to this page I'm going to reload because I've changed the
to reload because I've changed the JavaScript and I need my browser to
JavaScript and I need my browser to download it and now I'm going to type in
download it and now I'm going to type in my name for instance David click greets
my name for instance David click greets with fingers crossed and voila now I see
with fingers crossed and voila now I see hello comma David all right so it turns
hello comma David all right so it turns out that while functional this isn't the
out that while functional this isn't the best design and co-mingling your uh HTML
best design and co-mingling your uh HTML with your JavaScript script code AS with
with your JavaScript script code AS with this onsubmit attribute isn't
this onsubmit attribute isn't particularly clean it's better as with
particularly clean it's better as with CSS to keep your HTML over here your CSS
CSS to keep your HTML over here your CSS over here and your JavaScript now over
over here and your JavaScript now over here so to speak and better still
here so to speak and better still perhaps even in some separate files so
perhaps even in some separate files so how can I go about changing this a
how can I go about changing this a little bit well let me go ahead and
little bit well let me go ahead and actually let's go ahead and delete all
actually let's go ahead and delete all of this code for just a moment and let
of this code for just a moment and let me go and get rid of this on submit
me go and get rid of this on submit Handler down here and really just
Handler down here and really just distill my HTML only into the HTML and
distill my HTML only into the HTML and the attributes therefore and what I'm
the attributes therefore and what I'm instead going to do now is do this I can
instead going to do now is do this I can use JavaScript to achieve the listening
use JavaScript to achieve the listening for that submit event or that onsubmit
for that submit event or that onsubmit event I don't need to actually use HTML
event I don't need to actually use HTML for that I can use JavaScript entirely
for that I can use JavaScript entirely so it turns out I can access some other
so it turns out I can access some other uh member of this document by doing
uh member of this document by doing document. query selector again but this
document. query selector again but this time let's select the actual form tag
time let's select the actual form tag and it doesn't have an ID because it has
and it doesn't have an ID because it has no ID in its HTML but it does have a tag
no ID in its HTML but it does have a tag name so just like in CSS when you can
name so just like in CSS when you can Target Elements by way of their name I'm
Target Elements by way of their name I'm just going to select the one and only
just going to select the one and only form on this page by using that same
form on this page by using that same query selector function and now I'm
query selector function and now I'm going to use another function that just
going to use another function that just comes with JavaScript in the context of
comes with JavaScript in the context of browsers whereby once you select an
browsers whereby once you select an element like that form I can call add
element like that form I can call add event listener which is similar in
event listener which is similar in spirit to scratches when green flag
spirit to scratches when green flag clicked or any block like that you can
clicked or any block like that you can then tell the browser what event you
then tell the browser what event you want to listen for I want to listen for
want to listen for I want to listen for the submit event so you don't say
the submit event so you don't say onsubmit here now that we're in pure
onsubmit here now that we're in pure JavaScript you just say submit and now I
JavaScript you just say submit and now I can do something like this I can go
can do something like this I can go ahead and say call the following
ahead and say call the following function and I'm not even going to
function and I'm not even going to bother giving this function a name and
bother giving this function a name and that is allowed to in JavaScript as we
that is allowed to in JavaScript as we saw briefly in Python and what I'm going
saw briefly in Python and what I'm going to do now inside of curly braces after
to do now inside of curly braces after that keyword function is the same kind
that keyword function is the same kind of code as before I'm going to do let
of code as before I'm going to do let name equals document. query selector I'm
name equals document. query selector I'm going to select that same IDE uh same
going to select that same IDE uh same Name ID as before and get its value and
Name ID as before and get its value and then I'm going to do alert and then pass
then I'm going to do alert and then pass in hello comma a single quote again
in hello comma a single quote again after that concatenate with that the
after that concatenate with that the name and then semicolon but I need to do
name and then semicolon but I need to do one other thing it turns out that this
one other thing it turns out that this function and if you read the
function and if you read the documentation for this technique
documentation for this technique actually takes automatically a special
actually takes automatically a special argument called by convention event and
argument called by convention event and this is just an a variable if you will
this is just an a variable if you will that refers to whatever event just
that refers to whatever event just happened in this case it's of course
happened in this case it's of course going to be submit but in other contexts
going to be submit but in other contexts it might be a click event a mouse down
it might be a click event a mouse down event or something else entirely so this
event or something else entirely so this allows me now to do this which is a
allows me now to do this which is a little cryptic but you get used to these
little cryptic but you get used to these conventions I can use that event
conventions I can use that event whatever it is and then prevent whatever
whatever it is and then prevent whatever the default behavior is by calling a
the default behavior is by calling a special function called prevent default
special function called prevent default with a capital D this is the alternative
with a capital D this is the alternative to that Messier return fall semicolon
to that Messier return fall semicolon that I had inside of my HTML before so
that I had inside of my HTML before so all I've done here now is I've left all
all I've done here now is I've left all of my HTML as pure HTML down here and
of my HTML as pure HTML down here and I've put all of my JavaScript code as
I've put all of my JavaScript code as pure JavaScript up here this sort of
pure JavaScript up here this sort of separation of concerns similar to what
separation of concerns similar to what we started doing with CSS just a bit ago
we started doing with CSS just a bit ago in order to keep those two languages
in order to keep those two languages separate too well let me go back to my
separate too well let me go back to my browser here reload the page and
browser here reload the page and unfortunately there's a subtle mistake
unfortunately there's a subtle mistake I've made here let me go ahead and type
I've made here let me go ahead and type in David and click greet and
in David and click greet and unfortunately nothing actually seems to
unfortunately nothing actually seems to happen well maybe it's just my name
happen well maybe it's just my name Carter greet and nothing seems to happen
Carter greet and nothing seems to happen that alert does not come up well why is
that alert does not come up well why is that well let me go back to vs code here
that well let me go back to vs code here and point out that order of operations
and point out that order of operations in zscript matters similar in spirit to
in zscript matters similar in spirit to C because on line seven I'm selecting
C because on line seven I'm selecting the form and trying to add an event
the form and trying to add an event listener for submitting that form
listener for submitting that form unfortunately the form had better exist
unfortunately the form had better exist at that moment in time but it doesn't
at that moment in time but it doesn't because just like in C and in some cases
because just like in C and in some cases python where the compiler or The
python where the compiler or The Interpreter reads the code top to bottom
Interpreter reads the code top to bottom notice that the form doesn't actually
notice that the form doesn't actually exist and therefore get loaded into the
exist and therefore get loaded into the computer's memory until line 19 so we've
computer's memory until line 19 so we've got to kind of reorder these somehow now
got to kind of reorder these somehow now maybe the simplest way to do this would
maybe the simplest way to do this would just be to perhaps do something like
just be to perhaps do something like this let me scroll back up to my script
this let me scroll back up to my script tag and perhaps a little more explicitly
tag and perhaps a little more explicitly move it into the order in which I want
move it into the order in which I want it to be executed so I'm going to go
it to be executed so I'm going to go below my form and inside of my body
below my form and inside of my body which is actually okay for JavaScript
which is actually okay for JavaScript here and just use that same code and
here and just use that same code and assuming I didn't make any typos let's
assuming I didn't make any typos let's go back to the browser click reload
go back to the browser click reload again to get the latest typee in my name
again to get the latest typee in my name again using that purely JavaScript
again using that purely JavaScript solution and the only change I made was
solution and the only change I made was I move the code from up here to down
I move the code from up here to down here clicking greet now and wow it's now
here clicking greet now and wow it's now back we get the alert with hello comma
back we get the alert with hello comma David so those kinds of things those
David so those kinds of things those kinds of principles matter at least when
kinds of principles matter at least when we're back in this world but there's
we're back in this world but there's other Solutions too and just so that
other Solutions too and just so that you've seen it because it's a common
you've seen it because it's a common convention in libraries as well let me
convention in libraries as well let me undo that change and put that script tag
undo that change and put that script tag back in the head or really anywhere else
back in the head or really anywhere else in the page where it might be
in the page where it might be and let me propose that there's one
and let me propose that there's one other way to solve this problem to
other way to solve this problem to postpone that code on line 7 through 11
postpone that code on line 7 through 11 getting executed until really the whole
getting executed until really the whole Dom the tree is ready to go and the
Dom the tree is ready to go and the Syntax for this might be as follows I
Syntax for this might be as follows I can do document and I can add to the
can do document and I can add to the document a an event listener that's
document a an event listener that's going to listen for something a little
going to listen for something a little special and I always have to look this
special and I always have to look this up myself to remember the spelling and
up myself to remember the spelling and the capitalization but it turns out that
the capitalization but it turns out that the browser itself once it's done
the browser itself once it's done loading all of your HTML top to bottom
loading all of your HTML top to bottom left right it will raise an event called
left right it will raise an event called Dom content loaded capitalized exactly
Dom content loaded capitalized exactly as such and if you want to call some
as such and if you want to call some function and I don't even need an event
function and I don't even need an event argument in this case you can open curly
argument in this case you can open curly braces just as before and put inside of
braces just as before and put inside of those curly braces the code that you
those curly braces the code that you want to execute only once the dumbs
want to execute only once the dumbs content has been loaded top to bottom
content has been loaded top to bottom and now let me just finish my thought
and now let me just finish my thought with a closed curly brace Clos
with a closed curly brace Clos parenthesis and semicolon it gets a
parenthesis and semicolon it gets a little Annoying to visually line all of
little Annoying to visually line all of this up but I think I'm still good and
this up but I think I'm still good and now even though this code is at the top
now even though this code is at the top of my file or really above the form tag
of my file or really above the form tag itself I think we're okay so let's go
itself I think we're okay so let's go back to the browser here reload the page
back to the browser here reload the page type in David and click greet and we
type in David and click greet and we still get the same correct behavior and
still get the same correct behavior and so this is just a very common Paradigm
so this is just a very common Paradigm to use these kinds of events to listen
to use these kinds of events to listen and listen and listen for something to
and listen and listen for something to happen and then only do something once
happen and then only do something once that thing has transpired all right well
that thing has transpired all right well let's take one more step with JavaScript
let's take one more step with JavaScript code before we take a look at what's
code before we take a look at what's really fun about this language and what
really fun about this language and what you can do with browsers in particular
you can do with browsers in particular by just cleaning things up a little bit
by just cleaning things up a little bit further I'm going to go back into the
further I'm going to go back into the code here and I'm actually going to
code here and I'm actually going to remove uh or cut all of this code out of
remove uh or cut all of this code out of the hello.html file itself and I'm going
the hello.html file itself and I'm going to change my script tag to have nothing
to change my script tag to have nothing in between the open and close tag but I
in between the open and close tag but I am going to give it a source attribute
am going to give it a source attribute and let's go ahead and call this for
and let's go ahead and call this for instance hello.js sojs would be the
instance hello.js sojs would be the convention for the file extension for a
convention for the file extension for a Javascript file and even though this is
Javascript file and even though this is a little weird that we have the script
a little weird that we have the script tag and a source attribute then nothing
tag and a source attribute then nothing in between the open and close tag this
in between the open and close tag this is indeed the convention when you want
is indeed the convention when you want to put all of your code in a separate
to put all of your code in a separate file and let me go ahead and do that let
file and let me go ahead and do that let me go ahead and open my terminal window
me go ahead and open my terminal window create a new file called
create a new file called hello.js and then in that file I'm just
hello.js and then in that file I'm just going to paste the very code that I just
going to paste the very code that I just cut from the previous file so no changes
cut from the previous file so no changes to the code all I'm doing is factoring
to the code all I'm doing is factoring it out and now I'm doing something just
it out and now I'm doing something just like our CSS factorization before which
like our CSS factorization before which confusingly use the link tag this uses
confusingly use the link tag this uses the script tag this just now allows me
the script tag this just now allows me to collaborate with someone like Carter
to collaborate with someone like Carter or someone else so that they can do the
or someone else so that they can do the JavaScript code I can do the HTML maybe
JavaScript code I can do the HTML maybe a third person can do the CSS and indeed
a third person can do the CSS and indeed maybe we can build even grander things
maybe we can build even grander things by uh designing things in this way all
by uh designing things in this way all right well let me go back to my browser
right well let me go back to my browser again reload the page I shouldn't see
again reload the page I shouldn't see any visual changes but if I type in my
any visual changes but if I type in my name again David and click greet this
name again David and click greet this still now works and what my browser has
still now works and what my browser has just done underneath the hood is not
just done underneath the hood is not only download the uh hello.html file as
only download the uh hello.html file as always because there's now this script
always because there's now this script tag that's referencing the source of
tag that's referencing the source of another file just like an image tag
another file just like an image tag might reference the source of an image
might reference the source of an image the browser is automatically helping me
the browser is automatically helping me out by loading that into its memory as
out by loading that into its memory as well and now how about one final example
well and now how about one final example and for this one I'm going to go ahead
and for this one I'm going to go ahead and not write it live but open it up as
and not write it live but open it up as prepared in advance just to show you
prepared in advance just to show you what you can do by listening for some of
what you can do by listening for some of these other events as well like the key
these other events as well like the key up the finger going down the finger
up the finger going down the finger going up and listening for exactly that
going up and listening for exactly that so as the user is typing something you
so as the user is typing something you can do something interesting as well I'm
can do something interesting as well I'm going to go back into my directory
going to go back into my directory listing here and I click on this Source
listing here and I click on this Source a directory which has all of the
a directory which has all of the examples that I wrote here in advance
examples that I wrote here in advance and I'm going to scroll down to one
and I'm going to scroll down to one called hello 5. HTML and in hello 5 now
called hello 5. HTML and in hello 5 now we've gotten rid of the button and we
we've gotten rid of the button and we just have this text box but notice now
just have this text box but notice now what happens if I start typing my name
what happens if I start typing my name as d a v i d D I'm not typing enter at
as d a v i d D I'm not typing enter at all and in fact if I start deleting and
all and in fact if I start deleting and I change my mind and start typing
I change my mind and start typing Carter's name notice now that the web
Carter's name notice now that the web page the Dom inside of the computer's
page the Dom inside of the computer's memory is now automatically updating
memory is now automatically updating itself so it's not even listening it
itself so it's not even listening it would seem for a submit event anymore
would seem for a submit event anymore but maybe for a key up event instead so
but maybe for a key up event instead so let me go back to VSS code here open my
let me go back to VSS code here open my terminal window and in my source a
terminal window and in my source a directory let me open up hello 5. HTML
directory let me open up hello 5. HTML inside of the script tag you'll see some
inside of the script tag you'll see some code that's similar in spirit to before
code that's similar in spirit to before whereby I'm adding an event listener to
whereby I'm adding an event listener to the document waiting for the whole Dom's
the document waiting for the whole Dom's content to be loaded but then inside of
content to be loaded but then inside of that function I'm now doing this I'm
that function I'm now doing this I'm creating a variable called input and
creating a variable called input and selecting from the document the one and
selecting from the document the one and only input tag that we saw just a moment
only input tag that we saw just a moment ago I'm then adding on line 11 in event
ago I'm then adding on line 11 in event listener for key up which is exactly
listener for key up which is exactly that gesture so that I can execute some
that gesture so that I can execute some additional code anytime the human lists
additional code anytime the human lists their finger from the keyboard after
their finger from the keyboard after typing a key what do I then do well I'm
typing a key what do I then do well I'm going to go ahead it seems and declare
going to go ahead it seems and declare another variable called name and I'm
another variable called name and I'm just going to select some P tag on the
just going to select some P tag on the page and now we didn't really see a P
page and now we didn't really see a P tag so I think it's time to look at the
tag so I think it's time to look at the HTML if I scroll down to the bottom of
HTML if I scroll down to the bottom of the page where my actual HTML is you'll
the page where my actual HTML is you'll see that there's just a form tag and no
see that there's just a form tag and no onsubmit Handler anymore as before
onsubmit Handler anymore as before there's just an input tag and no button
there's just an input tag and no button at all but there is on line 29 here an
at all but there is on line 29 here an open and close P tag just so I have an
open and close P tag just so I have an empty placeholder in which to put
empty placeholder in which to put something like hello David or hello
something like hello David or hello Carter so that's why now on line 12 I
Carter so that's why now on line 12 I can define a variable called name and I
can define a variable called name and I can select that P tag so that what do I
can select that P tag so that what do I want to do well if inside of the input
want to do well if inside of the input there's a value so this is essentially
there's a value so this is essentially checking for null or the absence of a
checking for null or the absence of a string so if input. value does not equal
string so if input. value does not equal nothing that is there is a value in that
nothing that is there is a value in that text box well this syntax here on line
text box well this syntax here on line 14 is a very clever way of doing the
14 is a very clever way of doing the following go into that name tag that is
following go into that name tag that is to say the empty par paragraph change
to say the empty par paragraph change the inner HTML of it the HTML inside of
the inner HTML of it the HTML inside of it to be literally this hello comma and
it to be literally this hello comma and then just so you've seen an additional
then just so you've seen an additional piece of syntax this is similar to
piece of syntax this is similar to Python's F strings the syntax is a
Python's F strings the syntax is a little weird in JavaScript you actually
little weird in JavaScript you actually use not single quotes not double quotes
use not single quotes not double quotes but back ticks which on a US English
but back ticks which on a US English keyboard are typically the top leftand
keyboard are typically the top leftand key of your keyboard or thereabouts and
key of your keyboard or thereabouts and using dollar sign curly braces just like
using dollar sign curly braces just like the curly braces alone in Python allows
the curly braces alone in Python allows us to plug in whatever the value is of
us to plug in whatever the value is of that input however if there's no value
that input however if there's no value there it looks like I'm just going to
there it looks like I'm just going to say hello whoever you are and in fact we
say hello whoever you are and in fact we can see that behavior now is I delete
can see that behavior now is I delete delete delete delete delete delete and
delete delete delete delete delete and nothing's there now that if condition is
nothing's there now that if condition is no longer true and so we see this
no longer true and so we see this default value instead so this is only to
default value instead so this is only to say that by harnessing these various
say that by harnessing these various events that are constantly happening on
events that are constantly happening on most any web page we can now register
most any web page we can now register code just like we did way back in
code just like we did way back in scratch to actually listen for those
scratch to actually listen for those events and do something with them now it
events and do something with them now it turns out we can do some interesting
turns out we can do some interesting things even using third party coat and
things even using third party coat and just as we used bootstrap a bit ago to
just as we used bootstrap a bit ago to make our table prettier allow me to
make our table prettier allow me to propose that we also take a look at this
propose that we also take a look at this version of favorites as well let me go
version of favorites as well let me go back into my source a directory and open
back into my source a directory and open up favorites 2 which I made in advance
up favorites 2 which I made in advance which looks almost the same though I've
which looks almost the same though I've zoomed in here a bit but you'll notice
zoomed in here a bit but you'll notice somewhat subtly over the leftmost column
somewhat subtly over the leftmost column in this table you'll see now this arrow
in this table you'll see now this arrow in Gray pointing up and pointing down
in Gray pointing up and pointing down previously those were not there all I
previously those were not there all I had was a static HTML table with all of
had was a static HTML table with all of this data sorted in whatever order it
this data sorted in whatever order it was inputed the other day in that form
was inputed the other day in that form but now notice what I can do if I want
but now notice what I can do if I want to sort in uh one order I can click this
to sort in uh one order I can click this Arrow or the other order I can sort in
Arrow or the other order I can sort in this Arrow so essentially doing it
this Arrow so essentially doing it chronologically forward or backward now
chronologically forward or backward now how is that sorting happening it's
how is that sorting happening it's presumably based on all of the
presumably based on all of the timestamps that were registered when we
timestamps that were registered when we submitted that Google form just a bit
submitted that Google form just a bit ago um uh when it was live but now using
ago um uh when it was live but now using JavaScript it turns out that we can use
JavaScript it turns out that we can use some logic somehow and sort this data by
some logic somehow and sort this data by the same and you don't get that
the same and you don't get that automatically just by using HTML alone
automatically just by using HTML alone now how did I achieve that well it turns
now how did I achieve that well it turns out if I go ahead and close these hello
out if I go ahead and close these hello files and in vs code let's open up uh
files and in vs code let's open up uh favorites 2. HTML you'll see that all of
favorites 2. HTML you'll see that all of the HTML is actually the same if I
the HTML is actually the same if I scroll down and down through this file
scroll down and down through this file but I added a little something
but I added a little something interesting at top I copied and pasted
interesting at top I copied and pasted the appropriate URLs and HTML tags from
the appropriate URLs and HTML tags from bootstraps documentation and you'll see
bootstraps documentation and you'll see here that I have a file called not only
here that I have a file called not only bootstrap.css but also
bootstrap.css but also bootstrap.min.js as well as a couple of
bootstrap.min.js as well as a couple of other things as well that allows me
other things as well that allows me ultimately using third-party libraries
ultimately using third-party libraries to add some special HTML attributes that
to add some special HTML attributes that those libraries told me to add and then
those libraries told me to add and then as soon as those libraries detect the
as soon as those libraries detect the presence of these attributes now on my
presence of these attributes now on my own raw data they do their thing and
own raw data they do their thing and javascrip defy not a technical term the
javascrip defy not a technical term the entire table and now make it interactive
entire table and now make it interactive and not static so you'll see here that
and not static so you'll see here that the Aesthetics of the table are as
the Aesthetics of the table are as before table and table striped but I'm
before table and table striped but I'm adding now another HTML attribute called
adding now another HTML attribute called Data toggle whose value is table and I
Data toggle whose value is table and I know that only from the documentation of
know that only from the documentation of these libraries indicating that's that's
these libraries indicating that's that's how I can now enable this table to be
how I can now enable this table to be interactive as I can too by adding data
interactive as I can too by adding data sortable equals quote unquote true on
sortable equals quote unquote true on specifically the timestamp column and
specifically the timestamp column and the only thing unfamiliar here perhaps
the only thing unfamiliar here perhaps is I'm using t P for table heading as
is I'm using t P for table heading as opposed to TD as I do elsewhere but
opposed to TD as I do elsewhere but that's all that it takes to now focus on
that's all that it takes to now focus on the raw data you want to present and let
the raw data you want to present and let someone else do the heavy lifting of
someone else do the heavy lifting of actually implementing the logic well
actually implementing the logic well let's end with just a look at what more
let's end with just a look at what more you can do with Java script and just how
you can do with Java script and just how powerful it is when you combine a
powerful it is when you combine a language like this with the data and the
language like this with the data and the uh user interface you want to convey
uh user interface you want to convey let's go ahead and open up within Source
let's go ahead and open up within Source a directory something called background.
a directory something called background. HTML now this interface here is quite
HTML now this interface here is quite simple it just has three buttons RG and
simple it just has three buttons RG and B and the background of course noticed
B and the background of course noticed by default is just white but when I
by default is just white but when I click on the r the background turns red
click on the r the background turns red when the I click on the G it turns green
when the I click on the G it turns green and the blue it turns blue and again and
and the blue it turns blue and again and again so how is this working well if you
again so how is this working well if you think back again to the available events
think back again to the available events perhaps I'm just listening for a click
perhaps I'm just listening for a click on those buttons and then doing
on those buttons and then doing something with maybe the CSS of the page
something with maybe the CSS of the page to allow me to see those different
to allow me to see those different colors so in fact let's go back to vs
colors so in fact let's go back to vs code here and let's open up background.
code here and let's open up background. HTML and in here you'll see some simple
HTML and in here you'll see some simple HTML at the top just three buttons but
HTML at the top just three buttons but I've given each a unique ID so that I
I've given each a unique ID so that I can reference it in code and then inside
can reference it in code and then inside of a script tag here below because I
of a script tag here below because I didn't bother with the uh Dom content
didn't bother with the uh Dom content loaded event here notice that I'm doing
loaded event here notice that I'm doing the following I'm creating a variable
the following I'm creating a variable called body that lets me select the body
called body that lets me select the body tag I then have in these three line some
tag I then have in these three line some code that handles red what am I doing
code that handles red what am I doing well I'm selecting from the document
well I'm selecting from the document whatever h HTML tag has unique ID of red
whatever h HTML tag has unique ID of red and then I'm adding an event listener
and then I'm adding an event listener for any click on that button and anytime
for any click on that button and anytime someone clicks on that red button I call
someone clicks on that red button I call this function anonymously it doesn't
this function anonymously it doesn't even have or need a name and this syntax
even have or need a name and this syntax here is powerful because now in
here is powerful because now in JavaScript I can alter the CSS of my
JavaScript I can alter the CSS of my page by doing body which is the tag that
page by doing body which is the tag that I selected two lines ago accessing its
I selected two lines ago accessing its style accessing its background color
style accessing its background color property and setting it equal to quote
property and setting it equal to quote unquote red and I do the same down below
unquote red and I do the same down below for green I do the same down below for
for green I do the same down below for blue and the only thing worth noting
blue and the only thing worth noting here is that in CSS it turns out it's
here is that in CSS it turns out it's the case that the CSS property for the
the case that the CSS property for the background color of a page is actually
background color of a page is actually background Das color in all lower case
background Das color in all lower case with a hyphen in between unfortunately
with a hyphen in between unfortunately in the world of JavaScript a hyphen
in the world of JavaScript a hyphen would be mistaken for subtraction like
would be mistaken for subtraction like background minus color which would be
background minus color which would be wrong so the convention in JavaScript is
wrong so the convention in JavaScript is when you're trying to manipulate CSS you
when you're trying to manipulate CSS you take whatever the property name is uh
take whatever the property name is uh font size background color and you
font size background color and you change it into so-called camel case here
change it into so-called camel case here you get rid of The Hyphen and you
you get rid of The Hyphen and you capitalize the subsequent words like
capitalize the subsequent words like color in this case here all right how
color in this case here all right how about another well it turns out back in
about another well it turns out back in the day back in my day there was a HTML
the day back in my day there was a HTML tag that would actually allow you to do
tag that would actually allow you to do this create blinking text on a screen
this create blinking text on a screen it's rather unpleasant at this rate
it's rather unpleasant at this rate certainly but how might this work well
certainly but how might this work well it turns out in JavaScript if we take a
it turns out in JavaScript if we take a look at the blink. HTML file here we'll
look at the blink. HTML file here we'll see that you can in your HTML do
see that you can in your HTML do something literally as simple as hello
something literally as simple as hello world in the body but then you can call
world in the body but then you can call this function here turns out just like
this function here turns out just like document there's another Global special
document there's another Global special variable you can use in JavaScript and
variable you can use in JavaScript and browsers called window which refers to
browsers called window which refers to all things related to the window itself
all things related to the window itself the window comes with a set interval
the window comes with a set interval function which lets you do exactly that
function which lets you do exactly that set an interval in milliseconds and
set an interval in milliseconds and after every expiration of that interval
after every expiration of that interval some function will be called for you so
some function will be called for you so in this case it's saying every 50
in this case it's saying every 50 milliseconds but let's actually slow
milliseconds but let's actually slow that down now to 500 milliseconds or for
that down now to 500 milliseconds or for one half a second call a function called
one half a second call a function called blink notice I do not have parentheses
blink notice I do not have parentheses after the blink name because I don't
after the blink name because I don't want to call blink now I want to tell
want to call blink now I want to tell JavaScript to call the function called
JavaScript to call the function called blink every 500 milliseconds now we'll
blink every 500 milliseconds now we'll see in a moment what that code looks
see in a moment what that code looks like but let's go back to the page and
like but let's go back to the page and reload and you'll see now that it's a
reload and you'll see now that it's a more pleasant blinking if that's even
more pleasant blinking if that's even the case every half second because I'm
the case every half second because I'm now firing that event that is I'm uh
now firing that event that is I'm uh calling that function now every 500
calling that function now every 500 milliseconds instead how am I doing that
milliseconds instead how am I doing that well this same script tag I've invented
well this same script tag I've invented my own blink function this is has the
my own blink function this is has the distinction back in the day of actually
distinction back in the day of actually being an HTML tag and among the few tags
being an HTML tag and among the few tags that the world removed and got rid of so
that the world removed and got rid of so that it's no longer used because it's
that it's no longer used because it's not all that user friendly but down here
not all that user friendly but down here what am I doing I'm getting the body of
what am I doing I'm getting the body of the document itself with this variable
the document itself with this variable and then I'm checking two CSS properties
and then I'm checking two CSS properties that we didn't talk about before but it
that we didn't talk about before but it turns out that you can check and set the
turns out that you can check and set the visibility of an element in JavaScript
visibility of an element in JavaScript by going into that tag checking its
by going into that tag checking its style and getting its visibility uh
style and getting its visibility uh property and if it happens to equal
property and if it happens to equal hidden the next line of code here 13
hidden the next line of code here 13 sets it equal to visible instead else if
sets it equal to visible instead else if it's not hidden it must already be
it's not hidden it must already be visible and so line 17 flips it the
visible and so line 17 flips it the other way and changes it to Hidden here
other way and changes it to Hidden here left hand right hand clearly not talking
left hand right hand clearly not talking no idea why the opposite of visible Is
no idea why the opposite of visible Is Not Invisible it's indeed visible and
Not Invisible it's indeed visible and hidden but this just allows you every
hidden but this just allows you every time this function is called to change
time this function is called to change the property from one value to another
the property from one value to another achieving that blinking effect you can
achieving that blinking effect you can do even more powerful things that you
do even more powerful things that you and I take for granted every day let's
and I take for granted every day let's go into Source 8 and go to autocom
go into Source 8 and go to autocom complete. HTML which I wrote in advance
complete. HTML which I wrote in advance and this is a page that also loads into
and this is a page that also loads into memory a really big dictionary that you
memory a really big dictionary that you might recall from problem set five and
might recall from problem set five and if I go ahead and type in something like
if I go ahead and type in something like c a t you'll notice dynamically an
c a t you'll notice dynamically an unordered bulleted list appearing below
unordered bulleted list appearing below the text box that shows you all of the
the text box that shows you all of the words in that dictionary from pet 5 that
words in that dictionary from pet 5 that start with c and then CA and then T just
start with c and then CA and then T just like the autocomplete you see every day
like the autocomplete you see every day on your phone in Google or websites like
on your phone in Google or websites like it how is that working well probably I'm
it how is that working well probably I'm listening for the key press going up as
listening for the key press going up as soon as that key is pressed I'm probably
soon as that key is pressed I'm probably searching through a big array uh really
searching through a big array uh really of all of those words maybe using linear
of all of those words maybe using linear search maybe binary search if uh faster
search maybe binary search if uh faster than that and then looking for any
than that and then looking for any string in that array that starts with C
string in that array that starts with C or CA or c a t and then I'm generating
or CA or c a t and then I'm generating automattic IC Ally the HTML therefore
automattic IC Ally the HTML therefore but perhaps most familiar nowadays is
but perhaps most familiar nowadays is just how much your phone and your laptop
just how much your phone and your laptop know about you and let me go into a
know about you and let me go into a final example here in Source 8 called
final example here in Source 8 called geolocation HTML which is a term of art
geolocation HTML which is a term of art that just refers to figuring out your
that just refers to figuring out your geography for instance your GPS
geography for instance your GPS coordinates now here we have a third and
coordinates now here we have a third and final Global variable that your browser
final Global variable that your browser provides you with called Navigator and
provides you with called Navigator and in Navigator there's a special object
in Navigator there's a special object called geolocation that comes with a
called geolocation that comes with a function called get current position
function called get current position so if you call Navigator geolocation dog
so if you call Navigator geolocation dog getcurrent position and then pass in a
getcurrent position and then pass in a function of your own that doesn't need a
function of your own that doesn't need a name but does take an argument in this
name but does take an argument in this case I called it position as soon as the
case I called it position as soon as the browser or as soon as the phone has
browser or as soon as the phone has figured out where in the world that user
figured out where in the world that user is with that browser at some latitude
is with that browser at some latitude and longitude the browser will
and longitude the browser will automatically call that function for you
automatically call that function for you and you can do anything you want with
and you can do anything you want with the position that comes back the
the position that comes back the latitude and longitude respectively so
latitude and longitude respectively so I'm going to use a function that's not
I'm going to use a function that's not often that help but for our purposes
often that help but for our purposes today it's just going to write to the
today it's just going to write to the document itself to my big rectangular
document itself to my big rectangular region whatever the latitude is then a
region whatever the latitude is then a comma and then the longitude as well so
comma and then the longitude as well so if we go back with this final flourish
if we go back with this final flourish into Source 8 open up geolocation HTML
into Source 8 open up geolocation HTML you'll see that my browser first wants
you'll see that my browser first wants my permission to let this website my own
my permission to let this website my own know my location I'm going to go ahead
know my location I'm going to go ahead and click allow crossing my fingers
and click allow crossing my fingers because it might take a moment for the
because it might take a moment for the phone or the laptop to figure it out and
phone or the laptop to figure it out and it looks like according to Google I am
it looks like according to Google I am right this moment with my Mac at
right this moment with my Mac at latitude longitude 42375 comma
latitude longitude 42375 comma 7111 let's go ahead and highlight and
7111 let's go ahead and highlight and copy that let's go to a website like
copy that let's go to a website like Googl maps.com crossing our fingers if
Googl maps.com crossing our fingers if you've never done this you can search
you've never done this you can search for GPS coordinates too let's hit enter
for GPS coordinates too let's hit enter and amazing we are indeed in Sanders
and amazing we are indeed in Sanders Theater roughly there standing on this
Theater roughly there standing on this stage on Halloween and that then is week
stage on Halloween and that then is week eight we will see you next time happy
eight we will see you next time happy Halloween
Halloween nothing
nothing go buffering
go buffering okay Josh
[Music] nice it's Moy no oh
Josh uh um Sophie [Music]
[Music] amazing that was
amazing that was [Music]
[Music] perfect I think I
perfect I think I hey to
hey to you
[Music] oh that was amazing thank you all so
oh that was amazing thank you all so good
all right this is cs50 and this is already week nine which is our second to
already week nine which is our second to last indeed this is really the last week
last indeed this is really the last week where you'll learn in this class how to
where you'll learn in this class how to program but indeed it's this week that's
program but indeed it's this week that's really meant to be the the pedagogical
really meant to be the the pedagogical climax of like all of these various
climax of like all of these various languages we've been looking at all of
languages we've been looking at all of these various techniques all of this
these various techniques all of this syntax so that at the end of cs50 in
syntax so that at the end of cs50 in just a few weeks you indeed feel that
just a few weeks you indeed feel that you didn't take a class on C and you
you didn't take a class on C and you didn't take a class on python but you
didn't take a class on python but you really more generally took a class on
really more generally took a class on programming because indeed we know
programming because indeed we know already about half of you uh will go on
already about half of you uh will go on to study computer science further but
to study computer science further but half of you will not and indeed all of
half of you will not and indeed all of your programming chops here on out
your programming chops here on out theoretically will have a foundation in
theoretically will have a foundation in what we been doing these past many weeks
what we been doing these past many weeks but here on out it's really going to be
but here on out it's really going to be up to you to learn some new fangled
up to you to learn some new fangled language when it comes out or to follow
language when it comes out or to follow some new trend when some language
some new trend when some language eclipses the ones we've been using as
eclipses the ones we've been using as more popular as more appropriate for
more popular as more appropriate for problems you want to solve and so today
problems you want to solve and so today really is about synthesizing so many of
really is about synthesizing so many of the past few weeks but doing it in the
the past few weeks but doing it in the context of web programming which For
context of web programming which For Better or For Worse is so very much
Better or For Worse is so very much invogue nowadays both on our laptops and
invogue nowadays both on our laptops and phones and indeed the languages we
phones and indeed the languages we looked at in recent weeks are used not
looked at in recent weeks are used not only to make websites but also full
only to make websites but also full fledged applications and app stores and
fledged applications and app stores and the like so this really will be the
the like so this really will be the culmination of those past several weeks
culmination of those past several weeks and indeed we'll even talk about some
and indeed we'll even talk about some familiar Concepts like shopping carts
familiar Concepts like shopping carts when you're on Amazon and these things
when you're on Amazon and these things called cookies when you're visiting
called cookies when you're visiting websites all of those topics too will
websites all of those topics too will come into play and you'll have an
come into play and you'll have an understanding of what all that means
understanding of what all that means from the ground up so how did we get
from the ground up so how did we get here well just last week we focused on
here well just last week we focused on HTML and CSS primarily which are not
HTML and CSS primarily which are not programming languages they're just about
programming languages they're just about Aesthetics structuring your data
Aesthetics structuring your data presenting your data and so forth um and
presenting your data and so forth um and we served the web pages we wrote using
we served the web pages we wrote using this program HTTP server this is just
this program HTTP server this is just one such program there's dozens hundreds
one such program there's dozens hundreds of different web servers that you can
of different web servers that you can use out there this is just a super
use out there this is just a super simple one we pre-installed in your
simple one we pre-installed in your codes space for you in vs code so that
codes space for you in vs code so that you can just serve up web pages at the
you can just serve up web pages at the end of last week two though we teased
end of last week two though we teased JavaScript a full-fledged programming
JavaScript a full-fledged programming language that you can use to manipulate
language that you can use to manipulate the users experience for the better to
the users experience for the better to make things more Dynamic and interactive
make things more Dynamic and interactive by actually running code in the user
by actually running code in the user users's browser on their Mac their PC
users's browser on their Mac their PC their phone as opposed to server side
their phone as opposed to server side which up until now is where all of our
which up until now is where all of our code in C and python has been written so
code in C and python has been written so you're writing code on a server you're
you're writing code on a server you're serving code from a server but now with
serving code from a server but now with HTML CSS and JavaScript it's getting
HTML CSS and JavaScript it's getting executed in a browser but today we're
executed in a browser but today we're going to give you one final feature of
going to give you one final feature of python or really languages like it that
python or really languages like it that you can also use code on the server to
you can also use code on the server to generate automatically dynamically the
generate automatically dynamically the HTML uh the JavaScript the CSS that you
HTML uh the JavaScript the CSS that you actually want the user to rece receive
actually want the user to rece receive you don't have to hardcode everything as
you don't have to hardcode everything as you have when making your own homepage
you have when making your own homepage well let's consider what what some of
well let's consider what what some of the building blocks were last week so
the building blocks were last week so here's a sample URL and over here slash
here's a sample URL and over here slash is sort of the default page on any web
is sort of the default page on any web server it might be index.html it might
server it might be index.html it might be something else that's just a
be something else that's just a convention but it refers to whatever the
convention but it refers to whatever the default actually is you can visit of
default actually is you can visit of course when any browser like a URL that
course when any browser like a URL that ends in file. HTML or something else.
ends in file. HTML or something else. HTML and that literally means your
HTML and that literally means your browser wants this file on this server
browser wants this file on this server or of course we saw that it can be a
or of course we saw that it can be a folder and inside of that folder is
folder and inside of that folder is presumably some default file name like
presumably some default file name like again index.html or you can be more
again index.html or you can be more explicit like folder file. HTML and
explicit like folder file. HTML and these more generally we just called
these more generally we just called paths and indeed a path is just a
paths and indeed a path is just a location on your Mac your PC or on a
location on your Mac your PC or on a server of some piece of information but
server of some piece of information but today we're just going to rename this
today we're just going to rename this only to use other common terminology but
only to use other common terminology but they're really just synonyms today we're
they're really just synonyms today we're going to refer to those same things as
going to refer to those same things as routes because now today we're going to
routes because now today we're going to ultimately replace HT PTP server which
ultimately replace HT PTP server which just serves up static content that you
just serves up static content that you all write with your own web server like
all write with your own web server like now you will be the ones controlling
now you will be the ones controlling what it is the server does in response
what it is the server does in response to the user in uh requests so that you
to the user in uh requests so that you can respond interactively and
can respond interactively and dynamically but we're still going to see
dynamically but we're still going to see techniques like this these were our
techniques like this these were our so-called HTTP parameters they're
so-called HTTP parameters they're everything that comes after a question
everything that comes after a question mark in a URL and it can be like key
mark in a URL and it can be like key equals value and an example was what
equals value and an example was what when we played with Google what was the
when we played with Google what was the key and what was the value
key and what was the value that I first tried any
recollection I was searching for cats and so the key I figured that was Q
and so the key I figured that was Q because that's what Larry and Sergey who
because that's what Larry and Sergey who created Google years ago decided the
created Google years ago decided the name would be of the HTML text box that
name would be of the HTML text box that you type your query into and if I type
you type your query into and if I type cat for cat the value of that would end
cat for cat the value of that would end up in the URL for Google as being
up in the URL for Google as being question mark cat equals value and I
question mark cat equals value and I mentioned that it's often the case that
mentioned that it's often the case that you want to send two different inputs to
you want to send two different inputs to a server and this is why I propose that
a server and this is why I propose that you just keep an eye out for Amper Sands
you just keep an eye out for Amper Sands and and Ampersand separate these key
and and Ampersand separate these key value pairs but again this is the same
value pairs but again this is the same darn Paradigm as before and we've seen
darn Paradigm as before and we've seen this so many times right key value pairs
this so many times right key value pairs in dictionaries in Python we've seen uh
in dictionaries in Python we've seen uh HTML attributes and their values we've
HTML attributes and their values we've seen CSS properties and their values
seen CSS properties and their values it's all the same thing associating
it's all the same thing associating something with something else even
something with something else even though every language every person seems
though every language every person seems to have their own uh vernacular for it
to have their own uh vernacular for it it really is just the same idea this
it really is just the same idea this associating of something with something
associating of something with something else we'll continue to see and here to
else we'll continue to see and here to be concrete were the
be concrete were the HTTP lines of text that were in those
HTTP lines of text that were in those virtual envelopes if you will if I were
virtual envelopes if you will if I were indeed selecting trying to search for
indeed selecting trying to search for something like cats on Google this
something like cats on Google this recall was the message that got sent to
recall was the message that got sent to the server by my browser in order to
the server by my browser in order to tell Google to please search for not
tell Google to please search for not dogs but in this case cats now what is
dogs but in this case cats now what is HTTP server been doing for us well it's
HTTP server been doing for us well it's just been serving up HTML files CSS
just been serving up HTML files CSS files maybe some js or JavaScript files
files maybe some js or JavaScript files but it has been ignoring any HTTP
but it has been ignoring any HTTP parameters like HTTP server does not
parameters like HTTP server does not take user input why well what's it going
take user input why well what's it going to do with it because you already wrote
to do with it because you already wrote the HTML you already wrote the CSS like
the HTML you already wrote the CSS like there's no decisions to be made until we
there's no decisions to be made until we introduce a proper programming language
introduce a proper programming language on the server and so we're going to move
on the server and so we're going to move away now from this simple HTTP server
away now from this simple HTTP server program and introduce you to your own
program and introduce you to your own server that's going to handle the
server that's going to handle the parsing that is the extraction of these
parsing that is the extraction of these key value pairs so that you and I don't
key value pairs so that you and I don't have to write python code all of a
have to write python code all of a sudden that like analyzes this stuff
sudden that like analyzes this stuff figures out what pages requested the key
figures out what pages requested the key value pairs all of that were still going
value pairs all of that were still going to get for free by just using the right
to get for free by just using the right framework and so today we revisit python
framework and so today we revisit python uh which we've now used in some form the
uh which we've now used in some form the past few weeks and indeed it's kind of
past few weeks and indeed it's kind of been the glue that allows us to stitch
been the glue that allows us to stitch together some of our own logic we saw it
together some of our own logic we saw it with SQL we're going to now see it with
with SQL we're going to now see it with HTML CSS and even JavaScript if we want
HTML CSS and even JavaScript if we want and we're also going to see another
and we're also going to see another language today not a programming
language today not a programming language called Ginga and this is going
language called Ginga and this is going to be a common Paradigm in the real
to be a common Paradigm in the real world whereby different languages
world whereby different languages different libraries different Frameworks
different libraries different Frameworks often like borrow from each other or
often like borrow from each other or they use uh technologies that someone
they use uh technologies that someone else wrote just so they don't have to
else wrote just so they don't have to reinvent that wheel so flask is just a
reinvent that wheel so flask is just a framework that is a third party Library
framework that is a third party Library it's pretty popular nowadays it's
it's pretty popular nowadays it's relatively simple which is why we use it
relatively simple which is why we use it in cs50 if you've programmed before cs50
in cs50 if you've programmed before cs50 Jango is another popular framework or
Jango is another popular framework or library in the python sta space but it's
library in the python sta space but it's a little more complicated so we focus on
a little more complicated so we focus on flask and Ginga we'll see is not a
flask and Ginga we'll see is not a programming language it's just going to
programming language it's just going to be some syntax thankfully familiar with
be some syntax thankfully familiar with curly braces that allow us to use
curly braces that allow us to use placeholders in our actual web pages so
placeholders in our actual web pages so again you'll wrap your minds all
again you'll wrap your minds all eventually around where the lines are
eventually around where the lines are among these various Technologies but
among these various Technologies but these are not the interesting ideas the
these are not the interesting ideas the interesting ideas are the ones we'll
interesting ideas are the ones we'll focus on in code but starting today
focus on in code but starting today instead of running HTTP server to serve
instead of running HTTP server to serve up a static website we'll have you start
up a static website we'll have you start running literally flask space run in
running literally flask space run in your terminal window to run your own web
your terminal window to run your own web server that's implemented in Python
server that's implemented in Python using this flask framework so bootstrap
using this flask framework so bootstrap was a library for making your CSS and
was a library for making your CSS and JavaScript prettier and more interactive
JavaScript prettier and more interactive flask is a framework or library for just
flask is a framework or library for just making your python code more pleasant to
making your python code more pleasant to use since you're borrowing features from
use since you're borrowing features from someone else all right so how can we go
someone else all right so how can we go about doing this well if you are to
about doing this well if you are to write your very own web application your
write your very own web application your own amazon.com your own google.com in
own amazon.com your own google.com in Python using flask minimally you need to
Python using flask minimally you need to have a file called app.py by convention
have a file called app.py by convention which is where all your python code goes
which is where all your python code goes and then a folder called templates which
and then a folder called templates which is where all of your templates go and
is where all of your templates go and for now your templates are just your
for now your templates are just your HTML files so if we're going to now
HTML files so if we're going to now start building more interesting
start building more interesting interactive things like google.com or
interactive things like google.com or amazon.com we need to be able to execute
amazon.com we need to be able to execute code on the server and so this is the
code on the server and so this is the convention it's not index.html anymore
convention it's not index.html anymore necessarily it's these two things at the
necessarily it's these two things at the top level with that said we'll quickly
top level with that said we'll quickly see that there's some other conventions
see that there's some other conventions and in my examples online and in the uh
and in my examples online and in the uh problem set nine you'll see another file
problem set nine you'll see another file called requirements.txt which is just a
called requirements.txt which is just a text file that allows you to enumerate
text file that allows you to enumerate all of the thirdparty libraries your
all of the thirdparty libraries your application might want to use it's a
application might want to use it's a convention so that the server can like
convention so that the server can like automatically install things for you
automatically install things for you without you having to do it manually and
without you having to do it manually and then static is going to be where
then static is going to be where literally your static content goes so if
literally your static content goes so if you've got images for your web
you've got images for your web application if you've got JavaScript
application if you've got JavaScript files CSS files by convention that goes
files CSS files by convention that goes in static these are just conventions
in static these are just conventions like all of this can be changed but this
like all of this can be changed but this is like the way to do things so we'll
is like the way to do things so we'll introduce you to the defaults all right
introduce you to the defaults all right so what does this mean how for instance
so what does this mean how for instance could I go about implementing my own web
could I go about implementing my own web application using python that somehow
application using python that somehow spits out a message like Hello World all
spits out a message like Hello World all right well turns out just this now we'll
right well turns out just this now we'll tease this apart in just a moment but
tease this apart in just a moment but this is the content of a sample app.py
this is the content of a sample app.py file that apparently uses some Library
file that apparently uses some Library stuff like familiar syntax from
stuff like familiar syntax from something import something else we've
something import something else we've seen that before with csvs and other
seen that before with csvs and other libraries this is somewhat new syntax
libraries this is somewhat new syntax but it's kind of copy paste for now this
but it's kind of copy paste for now this is definitely new syntax and kind of
is definitely new syntax and kind of weird with the at sign here but we'll
weird with the at sign here but we'll see this again and again today and it's
see this again and again today and it's just copy paste initially until you
just copy paste initially until you understand what it's doing for you but
understand what it's doing for you but at least there's some familiar stuff
at least there's some familiar stuff here like index.html is still going to
here like index.html is still going to be with us but it's going to be up to us
be with us but it's going to be up to us when and how to show it to the user so
when and how to show it to the user so let's make this more real let me go over
let's make this more real let me go over to vs code here and let me go ahead and
to vs code here and let me go ahead and create a how about we'll do this in
create a how about we'll do this in hello let me do makeer hello to make a
hello let me do makeer hello to make a new folder called hello and I'm going to
new folder called hello and I'm going to CD into it just to isolate all of these
CD into it just to isolate all of these files to the same directory so that we
files to the same directory so that we have different apps today and different
have different apps today and different folders and now I'm going to do code of
folders and now I'm going to do code of let's do this actually let's do our
let's do this actually let's do our maker templates. HTM ah sorry not
maker templates. HTM ah sorry not templates. HTML let me rename that to
templates. HTML let me rename that to templates using the MV command this has
templates using the MV command this has nothing to do with web programming this
nothing to do with web programming this is me making typos so if I type LS now
is me making typos so if I type LS now I've got a folder called templates all
I've got a folder called templates all right in there let's create a file
right in there let's create a file called index.html that is going to be
called index.html that is going to be super simple and pretty much copy paste
super simple and pretty much copy paste from last week let me hide my terminal
from last week let me hide my terminal window and let me just very quickly whip
window and let me just very quickly whip up a simp simple hello world page using
up a simp simple hello world page using my HTML tag Lang will equal English then
my HTML tag Lang will equal English then inside of this I'm going to have a head
inside of this I'm going to have a head tag inside of this I'm going to have a
tag inside of this I'm going to have a title tag and I'm just going to call
title tag and I'm just going to call this thing hello uh I'm going to then
this thing hello uh I'm going to then have a body and in this I'm only going
have a body and in this I'm only going to say something simple like hello comma
to say something simple like hello comma world and just so this is mobile
world and just so this is mobile friendly recall that we touched on these
friendly recall that we touched on these meta tags so just in case you after
meta tags so just in case you after class play with your mobile device
class play with your mobile device instead of your laptop I'll do name
instead of your laptop I'll do name equals quote unquote
equals quote unquote viewport uh viewport and and content
viewport uh viewport and and content equals and I never remember this I'm
equals and I never remember this I'm literally reading it off of a cheat
literally reading it off of a cheat sheet initial scale equals 1 width
sheet initial scale equals 1 width equals device width and this is just
equals device width and this is just this magical incantation that says to
this magical incantation that says to the browser like size things
the browser like size things appropriately for the size of the device
appropriately for the size of the device it blows up the font sizes a bit all
it blows up the font sizes a bit all right so that's what I would have done
right so that's what I would have done last week and I would have served this
last week and I would have served this web page by running HTTP server in the
web page by running HTTP server in the same directory and boom I would see that
same directory and boom I would see that HTML but let's now start to take some
HTML but let's now start to take some control over the user's experience and
control over the user's experience and for now it's going to be underwhelming
for now it's going to be underwhelming it's just going to always say hello
it's just going to always say hello world but in a moment version two is
world but in a moment version two is going to say hello David or hello Carter
going to say hello David or hello Carter a bit more dynamically and we'll quickly
a bit more dynamically and we'll quickly escalate from there to just more
escalate from there to just more interesting applications as well
interesting applications as well culminating with things like cookies and
culminating with things like cookies and shopping carts and the like so let me go
shopping carts and the like so let me go back into my terminal window and as
back into my terminal window and as promised let me create another file
promised let me create another file called
called app.py and this is where now I need to
app.py and this is where now I need to implement the web server I'm going to
implement the web server I'm going to run using this flask framework and for
run using this flask framework and for now I'm just going to kind of do some
now I'm just going to kind of do some copy paste from uh what we saw on the
copy paste from uh what we saw on the slide a moment ago from the flask
slide a moment ago from the flask Library which we've pre-installed for
Library which we've pre-installed for you I'm going to import a uh function
you I'm going to import a uh function called flask capital F it's subtle but
called flask capital F it's subtle but it's important there and I'm also going
it's important there and I'm also going to import a few other things a function
to import a few other things a function called render template and another
called render template and another variable called request and the only way
variable called request and the only way I know this is from having taught this
I know this is from having taught this before read the documentation followed a
before read the documentation followed a tutorial like you wouldn't know this
tutorial like you wouldn't know this unless someone told you or you read how
unless someone told you or you read how to do this this but what this means is
to do this this but what this means is that this Library called flask has three
that this Library called flask has three things in it a function called flask
things in it a function called flask capital f a function called render
capital f a function called render template and a variable built into it
template and a variable built into it called request and this is going to be
called request and this is going to be all the building blocks I need to
all the building blocks I need to implement my own web server the
implement my own web server the convention in flask when you want to
convention in flask when you want to create a web app in Python is you create
create a web app in Python is you create a variable by convention called app and
a variable by convention called app and then you assign it the return value of
then you assign it the return value of that flask function capital F and pass
that flask function capital F and pass into it underscore uncore name uncore
into it underscore uncore name uncore underscore which is weird but we have
underscore which is weird but we have seen this before a few weeks ago anyone
seen this before a few weeks ago anyone recall when and why we mentioned uncore
recall when and why we mentioned uncore uncore name uncore
uncore name uncore uncore yeah I think was
it name or something yeah if we wanted to check if the name of the file was
to check if the name of the file was itself main so that we avoided a
itself main so that we avoided a situation where if you're writing your
situation where if you're writing your own Library code you don't want your
own Library code you don't want your code to be executed automatically you
code to be executed automatically you want to potentially execute the main
want to potentially execute the main function and that was a solution to that
function and that was a solution to that problem here for today's purposes this
problem here for today's purposes this is just the way you do it underscore
is just the way you do it underscore underscore name underscore uncore refers
underscore name underscore uncore refers to the current file and so this is just
to the current file and so this is just a little trick that says turn this file
a little trick that says turn this file into a flask application that's all it
into a flask application that's all it is and for now uh that line suffices all
is and for now uh that line suffices all right what do I want to do after that
right what do I want to do after that well now I'm in charge of the web server
well now I'm in charge of the web server I need to write the code that decides
I need to write the code that decides based on the browsers request what file
based on the browsers request what file or files I'm going to send from the
or files I'm going to send from the server to the browser last week http
server to the browser last week http server did all of this for us just based
server did all of this for us just based on the file name but today I'm going to
on the file name but today I'm going to take over control over that process and
take over control over that process and the way I do that is as follows I say
the way I do that is as follows I say app. route with weirdly an at sign in
app. route with weirdly an at sign in front of it this is known in python as a
front of it this is known in python as a decorator and it's a feature of python
decorator and it's a feature of python not a flask that we just didn't
not a flask that we just didn't introduce in weeks past but it's a
introduce in weeks past but it's a special it's a handy trick to do what
special it's a handy trick to do what we're about to do the route I want to
we're about to do the route I want to Define is quote unquote slash so that is
Define is quote unquote slash so that is here is code I want the server to
here is code I want the server to execute whenever I user visits forward
execute whenever I user visits forward slash the default page of the website
slash the default page of the website well what code do I want them to execute
well what code do I want them to execute well I want them to execute a function
well I want them to execute a function and I can therefore Define in Python a
and I can therefore Define in Python a function I can technically call this
function I can technically call this thing anything I want X or Y or Z but
thing anything I want X or Y or Z but because they're accessing the default
because they're accessing the default page otherwise known as the index of the
page otherwise known as the index of the site I'm going to just more reasonably
site I'm going to just more reasonably call this function index but just a
call this function index but just a convention you could call it anything
convention you could call it anything you want but X Y or Z is probably a bad
you want but X Y or Z is probably a bad stylistic choice it doesn't need to take
stylistic choice it doesn't need to take any arguments in this case and the only
any arguments in this case and the only thing this code this function is going
thing this code this function is going to do is for now let's go ahead and have
to do is for now let's go ahead and have it return hello world quote unquote and
it return hello world quote unquote and that's it all right now let me go into
that's it all right now let me go into my terminal window let me go ahead and
my terminal window let me go ahead and do flask run in the same directory that
do flask run in the same directory that has appy and hit enter I'm going to see
has appy and hit enter I'm going to see some cryptic output but including a URL
some cryptic output but including a URL of my code space and if I open that URL
of my code space and if I open that URL after hovering over it I'll indeed see
after hovering over it I'll indeed see hello world as you might hope but let me
hello world as you might hope but let me do this let me go ahead and rightclick
do this let me go ahead and rightclick on the page and click view page source
on the page and click view page source which if you haven't done before shows
which if you haven't done before shows you all of the HTML for a page however
you all of the HTML for a page however pretty or messy it is and that's it
pretty or messy it is and that's it there's no HTML that I've spit out it's
there's no HTML that I've spit out it's just quote unquote hello world well if I
just quote unquote hello world well if I actually want to spit out a full web
actually want to spit out a full web page which is not a big deal here
page which is not a big deal here because who cares it's just the text
because who cares it's just the text anyway but if I want to spit out a whole
anyway but if I want to spit out a whole file let me do this I want to return
file let me do this I want to return essentially the contents of index.html
essentially the contents of index.html which have all of the tags I want the
which have all of the tags I want the mobile friend stuff and all of that well
mobile friend stuff and all of that well I can't just return index.html but I can
I can't just return index.html but I can return this render template quote
return this render template quote unquote
unquote index.html and per the documentation for
index.html and per the documentation for flask this render template function will
flask this render template function will go find that file for me in my templates
go find that file for me in my templates folder by convention it will open it up
folder by convention it will open it up and then it will spit the whole thing
and then it will spit the whole thing out to the browser for me so I can keep
out to the browser for me so I can keep all my HTML in one place and all my
all my HTML in one place and all my python code in this one place so now if
python code in this one place so now if I go back to my browser and reload I
I go back to my browser and reload I don't think I'll really see a difference
don't think I'll really see a difference because it's the same text ultimately
because it's the same text ultimately but if I view page Source now notice
but if I view page Source now notice that ah there is all of the HTML that
that ah there is all of the HTML that was just sent to the browser so this is
was just sent to the browser so this is only to say we have the building blocks
only to say we have the building blocks the puzzle pieces if you will via which
the puzzle pieces if you will via which to now store all of our HTML in one
to now store all of our HTML in one place and presumably CSS JavaScript and
place and presumably CSS JavaScript and so forth but then serve up whatever we
so forth but then serve up whatever we want even though I'm just blindly
want even though I'm just blindly spitting out
spitting out index.html so before we proceed any
index.html so before we proceed any questions on this which again I claim is
questions on this which again I claim is like my manual version of what HTTP
like my manual version of what HTTP server was doing for us automatically
server was doing for us automatically last week but this is how you do it
last week but this is how you do it yourself any
yourself any questions all right well let's make it
questions all right well let's make it more interesting which we could not do
more interesting which we could not do with HTTP server and HTML alone why
with HTTP server and HTML alone why don't we go ahead and do this let me
don't we go ahead and do this let me visit the same URL and I'm going to zoom
visit the same URL and I'm going to zoom in and your url will differ from my code
in and your url will differ from my code space but it's going to end similarly
space but it's going to end similarly here I'm going to do slash question mark
here I'm going to do slash question mark name equals David for instance or Q
name equals David for instance or Q equals cats or name equals Carter any
equals cats or name equals Carter any key value pair I want I'm going to
key value pair I want I'm going to append after a slash and a question mark
append after a slash and a question mark thereby providing user input to the
thereby providing user input to the server albe it in a very user unfriendly
server albe it in a very user unfriendly way no one's going to normally do this
way no one's going to normally do this in their browser enter nothing changes
in their browser enter nothing changes here it just says hello world but
here it just says hello world but wouldn't it be nice if it says hello
wouldn't it be nice if it says hello David or equivalently if I zoom in here
David or equivalently if I zoom in here again and change David to Carter and hit
again and change David to Carter and hit enter wouldn't it be nice if it says
enter wouldn't it be nice if it says hello Carter instead so we need some
hello Carter instead so we need some dynamism there and here's now python is
dynamism there and here's now python is going to be our friend if I want to
going to be our friend if I want to access the HTTP parameters that the user
access the HTTP parameters that the user has provided via the URL be it Q equals
has provided via the URL be it Q equals cats or name equals David I can use this
cats or name equals David I can use this special variable I already preemptively
special variable I already preemptively imported earlier and I can do this if
imported earlier and I can do this if there is an HTTP parameter called name
there is an HTTP parameter called name in what I'm going to call request. args
in what I'm going to call request. args then I'm going to go ahead and create a
then I'm going to go ahead and create a variable called name and I'm going to
variable called name and I'm going to set it equal to request. args bracket
set it equal to request. args bracket name else if there is no quote unquote
name else if there is no quote unquote name key in this special variable called
name key in this special variable called request. args I'm going to just assume
request. args I'm going to just assume that the user's name is World by default
that the user's name is World by default now what's going on here well it turns
now what's going on here well it turns out that flask provides us with this
out that flask provides us with this special variable called request. ARs and
special variable called request. ARs and in there is all of the key value pairs
in there is all of the key value pairs that might have come in via the URL so
that might have come in via the URL so if you had to guess what type of data or
if you had to guess what type of data or what data type is request. args that's
what data type is request. args that's its name and here is in context line n
its name and here is in context line n might provide a clue in Python what data
might provide a clue in Python what data type might request. args
type might request. args be
be yeah uh it's not going to be an array or
yeah uh it's not going to be an array or a list because those are always in every
a list because those are always in every language we've seen numerically indexed
language we've seen numerically indexed but you're
but you're close someone else it's a dictionary so
close someone else it's a dictionary so a dictionary is similar syntactically to
a dictionary is similar syntactically to a list in Python but instead of numeric
a list in Python but instead of numeric indices like 012 you can literally use
indices like 012 you can literally use strings like quote unquote name now
strings like quote unquote name now that's a bit of a white lie it is a
that's a bit of a white lie it is a dictionary but it's flask's special
dictionary but it's flask's special fancy version of a dictionary but the
fancy version of a dictionary but the syntax via which you can access it is
syntax via which you can access it is exactly the same and I actually this is
exactly the same and I actually this is a typo I didn't mean to say names there
a typo I didn't mean to say names there I meant to say name singular but
I meant to say name singular but otherwise I think the code is correct
otherwise I think the code is correct this is going to on line eight check if
this is going to on line eight check if there's a key called name in request.
there's a key called name in request. ARs and if so it's going to set it equal
ARs and if so it's going to set it equal to that value otherwise it's going to
to that value otherwise it's going to default to world I deliberately did not
default to world I deliberately did not do this I added this IFL
do this I added this IFL and did not do this why what error might
and did not do this why what error might happen if I just blindly grab
name exactly if there was nothing at the end of the URL that was of the form
end of the URL that was of the form question mark name equals someone then
question mark name equals someone then there would be no name key and this is
there would be no name key and this is uh you know a couple weeks back but this
uh you know a couple weeks back but this would give you one of those annoying key
would give you one of those annoying key errors when you get a trace back because
errors when you get a trace back because you screwed up because you used a string
you screwed up because you used a string that doesn't exist that's why I'm just
that doesn't exist that's why I'm just proactively trying to avoid that
proactively trying to avoid that situation just like I'm might have a
situation just like I'm might have a couple of weeks ago so even though it's
couple of weeks ago so even though it's more verbose this is just much more
more verbose this is just much more defensive so that I don't accidentally
defensive so that I don't accidentally index into a dictionary where there is
index into a dictionary where there is no key but we'll see how we can tighten
no key but we'll see how we can tighten this up to be not four lines but one but
this up to be not four lines but one but I think now I can do this wouldn't it be
I think now I can do this wouldn't it be nice if now in my index.html file which
nice if now in my index.html file which recall is in my templates folder
recall is in my templates folder wouldn't it be nice if I could do the
wouldn't it be nice if I could do the equivalent in C of like a percent s here
equivalent in C of like a percent s here for instance or in Python something like
for instance or in Python something like this name well it's close and this is
this name well it's close and this is just because different humans invent
just because different humans invent different languages invent different uh
different languages invent different uh Frameworks the Syntax for this in flask
Frameworks the Syntax for this in flask is to actually do whoops two curly
is to actually do whoops two curly braces and then name of the variable
braces and then name of the variable inside of it why it's just probably
inside of it why it's just probably someone figured what are the odds that a
someone figured what are the odds that a normal person is ever going to use two
normal person is ever going to use two curly braces at once versus just one so
curly braces at once versus just one so this is probably decreasing the
this is probably decreasing the probability that people actually want to
probability that people actually want to Output literal curly braces like this so
Output literal curly braces like this so it's similar in spirit to Python's F
it's similar in spirit to Python's F strings it's similar in spirit to C's
strings it's similar in spirit to C's percent s it's similar in spirit to
percent s it's similar in spirit to sql's question marks same idea slightly
sql's question marks same idea slightly different syntax and this there is
different syntax and this there is ginger so it's not programming code per
ginger so it's not programming code per se it's just a template and indeed
se it's just a template and indeed that's why this folder is called
that's why this folder is called templates it is sort of like a a
templates it is sort of like a a blueprint for what I want to be spit out
blueprint for what I want to be spit out to the user but I've got these
to the user but I've got these placeholders like this variable that I
placeholders like this variable that I want to plug into that value now this
want to plug into that value now this alone is not enough watch what happens
alone is not enough watch what happens if I go back to my other browser and I
if I go back to my other browser and I reload the page after changing up here
reload the page after changing up here let's do name equals David again enter
let's do name equals David again enter nothing outputs after the hello comma so
nothing outputs after the hello comma so it seems that the name variable doesn't
it seems that the name variable doesn't exist yet and that's why indeed if I do
exist yet and that's why indeed if I do view page Source you can see what was
view page Source you can see what was sent to the browser something's wrong
sent to the browser something's wrong with my placeholder but I just need to
with my placeholder but I just need to be a little more explicit as to what I
be a little more explicit as to what I want to send where so it turns out that
want to send where so it turns out that the render template function takes not
the render template function takes not just one argument the name of the
just one argument the name of the template you want to spit out but it
template you want to spit out but it takes after that with commas all of the
takes after that with commas all of the placeholders you want to plug in so for
placeholders you want to plug in so for instance if you want the placeholder to
instance if you want the placeholder to be this literally placeholder inside of
be this literally placeholder inside of those curly braces you can then specify
those curly braces you can then specify as the second argument to rep uh render
as the second argument to rep uh render template a placeholder named argument
template a placeholder named argument equals whatever the name is so name is
equals whatever the name is so name is the variable in the lines above
the variable in the lines above placeholder is the name of my literal
placeholder is the name of my literal placeholder in the curly braces and so
placeholder in the curly braces and so now if I go back to my browser and
now if I go back to my browser and reload this with still quote unquote
reload this with still quote unquote with still question mark name equals
with still question mark name equals David in the URL now I indeed see hello
David in the URL now I indeed see hello comma David and if I zoom in here and
comma David and if I zoom in here and let me move over here let me type in
let me move over here let me type in Carter and hit enter now I see Hello
Carter and hit enter now I see Hello Carter instead now this is a little
Carter instead now this is a little unnecessary to explicitly call the
unnecessary to explicitly call the placeholder placeholder especially if
placeholder placeholder especially if you want to have two or three of them so
you want to have two or three of them so you can actually call this anything you
you can actually call this anything you want and I'm going to change it back to
want and I'm going to change it back to name which is a little more
name which is a little more straightforward the only weird thing
straightforward the only weird thing here is that now you'll see that you're
here is that now you'll see that you're writing code like this and this is
writing code like this and this is correct and this is the norm it just
correct and this is the norm it just looks weird but the thing on the left of
looks weird but the thing on the left of the equal sign is the placeholder you're
the equal sign is the placeholder you're using in the template the thing on the
using in the template the thing on the right can be any value you want
right can be any value you want including a variable so even though I'm
including a variable so even though I'm naming them exactly the same which looks
naming them exactly the same which looks stupid admittedly like this is what
stupid admittedly like this is what people tend to do just because it's uh
people tend to do just because it's uh simpler than introducing another word
simpler than introducing another word like placeholder any questions now on
like placeholder any questions now on this
this any questions on these
any questions on these placeholders no all right well let's
placeholders no all right well let's tighten this up a little bit and see if
tighten this up a little bit and see if we can't get things to be more Dynamic
we can't get things to be more Dynamic still let me propose now that instead of
still let me propose now that instead of outputting instead of using this
outputting instead of using this condition which made a very simple idea
condition which made a very simple idea like very verbose with four different
like very verbose with four different lines it turns out there's an easier way
lines it turns out there's an easier way to do this you can actually still create
to do this you can actually still create a variable called name and you can set
a variable called name and you can set it equal to request. args but instead of
it equal to request. args but instead of just blindly indexing into to that
just blindly indexing into to that dictionary it turns out that request.
dictionary it turns out that request. ARS comes with a function as well called
ARS comes with a function as well called get you can pass it an argument that
get you can pass it an argument that tells you what value you want to get and
tells you what value you want to get and by default if there is no key called
by default if there is no key called name in that dictionary this function
name in that dictionary this function will not throw a key error it's just
will not throw a key error it's just going to return none an O the special V
going to return none an O the special V the Special Value in Python so it avoids
the Special Value in Python so it avoids a bug in your code but it tightens up
a bug in your code but it tightens up four lines into one but even nicer if
four lines into one but even nicer if you read the documentation
you read the documentation the get function can also take a
the get function can also take a explicit default value so if you don't
explicit default value so if you don't want none to be on the screen like hello
want none to be on the screen like hello comma blank or I mean that would be
comma blank or I mean that would be weird too you can just put in a default
weird too you can just put in a default value per the documentation of this
value per the documentation of this function like world so now we've gone
function like world so now we've gone from four lines to just one so arguably
from four lines to just one so arguably it's better designed and if I go back to
it's better designed and if I go back to the browser now still with Carter in the
the browser now still with Carter in the URL and hit reload same thing happens
URL and hit reload same thing happens but we notice this suppose I uh get rid
but we notice this suppose I uh get rid of the name parameter altogether and hit
of the name parameter altogether and hit enter now it goes to the default instead
enter now it goes to the default instead world so it's just a little better a
world so it's just a little better a little better designed than doing it the
little better designed than doing it the other way instead all right how about we
other way instead all right how about we take things up one more Notch and how
take things up one more Notch and how about we introduce multiple routes and
about we introduce multiple routes and actually introduce perhaps a form to the
actually introduce perhaps a form to the mix because again no normal person is
mix because again no normal person is going to like visit a URL and add a
going to like visit a URL and add a slash and a question mark and their name
slash and a question mark and their name like that's not how browsers work uh
like that's not how browsers work uh well that's how browsers work that's not
well that's how browsers work that's not how humans interact with browsers you
how humans interact with browsers you and I use a form to quickly instead so
and I use a form to quickly instead so now things can get a little more
now things can get a little more interesting when making our own web
interesting when making our own web application cuz maybe we could do
application cuz maybe we could do something like this let me go and zoom
something like this let me go and zoom out again let me go back to my code here
out again let me go back to my code here and let me move this around and focus
and let me move this around and focus now on the index.html file instead of
now on the index.html file instead of just this placeholder why don't we go
just this placeholder why don't we go ahead and give ourselves a form like
ahead and give ourselves a form like we've played with a little bit in the
we've played with a little bit in the past be it for Google or something else
past be it for Google or something else and let's do this uh form and inside of
and let's do this uh form and inside of this form let's have an input and the
this form let's have an input and the name of this input will be quote unquote
name of this input will be quote unquote name so that too is confusing but inputs
name so that too is confusing but inputs have name attributes but this is a
have name attributes but this is a person's name so I'm saying name equals
person's name so I'm saying name equals name here so just a messy world of
name here so just a messy world of semantics and let me go ahead and make
semantics and let me go ahead and make this a text box by default and then let
this a text box by default and then let me give myself a button whose default
me give myself a button whose default type will be submit and the name of this
type will be submit and the name of this button will be greet for instance so
button will be greet for instance so let's see what happens here but let me
let's see what happens here but let me change app.py to just be the original
change app.py to just be the original simpler I'm not passing in any
simpler I'm not passing in any placeholders now and I'm going to even
placeholders now and I'm going to even get rid of this I'm just going to rewind
get rid of this I'm just going to rewind to the first version of this for
to the first version of this for Simplicity let's now change the url to
Simplicity let's now change the url to get rid of Carter and myself so we just
get rid of Carter and myself so we just go to slash and hit enter and now we
go to slash and hit enter and now we have a super simple form again all right
have a super simple form again all right this is not super userfriendly but
this is not super userfriendly but there's some nice enhancements we can
there's some nice enhancements we can make for instance like we can uh for
make for instance like we can uh for instance turn off autocomplete
instance turn off autocomplete especially if I want to type David and
especially if I want to type David and Carter manually and I don't want it
Carter manually and I don't want it finishing my thought during class uh we
finishing my thought during class uh we can do autofocus which puts the cursor
can do autofocus which puts the cursor there BL linking by default which is
there BL linking by default which is nice cuz then the human doesn't have to
nice cuz then the human doesn't have to deal with that um and then we can even
deal with that um and then we can even have a placeholder attribute placeholder
have a placeholder attribute placeholder equals name so that it's like built in
equals name so that it's like built in instructions for this thing and so now
instructions for this thing and so now if I go back to the other tab nothing's
if I go back to the other tab nothing's changed yet because I have to download
changed yet because I have to download the HTML again reload okay now it's a
the HTML again reload okay now it's a little more user friendly it says name
little more user friendly it says name and light gray the cursor is blinking
and light gray the cursor is blinking and I'm sort of ready to go but this
and I'm sort of ready to go but this form hasn't been wired up to go anywhere
form hasn't been wired up to go anywhere yet and so let's do this let's for
yet and so let's do this let's for instance say that the action of this
instance say that the action of this form
form is not going to be something like
is not going to be something like google.com which we did last time with
google.com which we did last time with for cats I am now going to be both the
for cats I am now going to be both the front end and the back end of this
front end and the back end of this website the front end is what the human
website the front end is what the human sees the web page the graphics the forms
sees the web page the graphics the forms the back end is the stuff the human
the back end is the stuff the human typically doesn't see the python code
typically doesn't see the python code the SQL code the server itself but now
the SQL code the server itself but now I'm in control of both sides of the
I'm in control of both sides of the experience the HTML and also the routes
experience the HTML and also the routes so let's just propose that we invent our
so let's just propose that we invent our own route and instead of calling it SL
own route and instead of calling it SL search like Google does let's call it
search like Google does let's call it SLG greet and let me specify that the
SLG greet and let me specify that the method this form will use which is
method this form will use which is technically the default will be get and
technically the default will be get and confusingly it is lowercase get even
confusingly it is lowercase get even though in the envelope we keep talking
though in the envelope we keep talking about virtually it's actually capitals
about virtually it's actually capitals again left hand wasn't talking to right
again left hand wasn't talking to right hand when these things were decided all
hand when these things were decided all right so all I've done is create a web
right so all I've done is create a web form that's going to submit whatever the
form that's going to submit whatever the text box value is to a route called SLG
text box value is to a route called SLG greet by default because there's no HTTP
greet by default because there's no HTTP or htps or no domain name SLG greet is
or htps or no domain name SLG greet is going to be assumed to be not at
going to be assumed to be not at google.com but whatever my own serers
google.com but whatever my own serers URL is so whatever my code spaces URL is
URL is so whatever my code spaces URL is that's going to be the implicit prefix
that's going to be the implicit prefix this SLG greet is just the route so now
this SLG greet is just the route so now let's go back to VSS codes app.py file
let's go back to VSS codes app.py file how do I now Stitch this together well I
how do I now Stitch this together well I think we're good to go with index.html
think we're good to go with index.html if index. html's purpose in life is just
if index. html's purpose in life is just to spit out this form we're done with
to spit out this form we're done with one of my routes but if I want to have a
one of my routes but if I want to have a second route greet that actually spits
second route greet that actually spits out some greeting to the user well let's
out some greeting to the user well let's prepare that template too let me go
prepare that template too let me go ahead and highlight all of this HTML let
ahead and highlight all of this HTML let me go back into my terminal window and
me go back into my terminal window and into my hello directory and then into my
into my hello directory and then into my templates directory and let me create
templates directory and let me create another template called greet HTML whose
another template called greet HTML whose purpose in life will not be to show a
purpose in life will not be to show a form but to greet the user with hello so
form but to greet the user with hello so and so so in this file I'm going to
and so so in this file I'm going to paste all that same HTML but I'm going
paste all that same HTML but I'm going to get rid of the form and essentially
to get rid of the form and essentially revert to our previous version hello
revert to our previous version hello comma and then using the ginger syntax
comma and then using the ginger syntax name so one template index.html is for
name so one template index.html is for the form the second template now is for
the form the second template now is for the greeting of hello comma so and so
the greeting of hello comma so and so but otherwise these files notice are
but otherwise these files notice are almost the same except one has the form
almost the same except one has the form one has just the hello so now let's
one has just the hello so now let's finish this up in app doop High let me
finish this up in app doop High let me go down here after a couple of blank
go down here after a couple of blank lines stylistically let me do app. route
lines stylistically let me do app. route quote unquote SLG greet but I could call
quote unquote SLG greet but I could call this route anything I want I'm just
this route anything I want I'm just using a a reasonable verb then let's
using a a reasonable verb then let's define another function I could call the
define another function I could call the function anything I want X Y or Z I'm
function anything I want X Y or Z I'm going to call it more reasonably greet
going to call it more reasonably greet no arguments and then now is the code
no arguments and then now is the code where I want to render the template so I
where I want to render the template so I do return render template greet HTML but
do return render template greet HTML but but I need to do one more
but I need to do one more thing what else do I want to do if I
thing what else do I want to do if I want greet HTML to have access to the
want greet HTML to have access to the human's name just to
human's name just to recap I think we solved this already but
recap I think we solved this already but I deleted it but what do I have to add
I deleted it but what do I have to add back
back yeahh yeah so I got to pass in the
yeahh yeah so I got to pass in the placeholder somehow so I can do this a
placeholder somehow so I can do this a couple of different ways I I'm going to
couple of different ways I I'm going to keep it a little more elegant this time
keep it a little more elegant this time I'm just going to put my name uh
I'm just going to put my name uh argument there and I'm going to set it
argument there and I'm going to set it equal to request. ar. get quote unquote
equal to request. ar. get quote unquote name comma world before I used a
name comma world before I used a separate variable but I only used it in
separate variable but I only used it in one place so that's not strictly
one place so that's not strictly necessary so this is fine too but if
necessary so this is fine too but if this gets a little overwhelming notice
this gets a little overwhelming notice that I can alternatively do this I can
that I can alternatively do this I can create an actual variable called name
create an actual variable called name and then I can pass in an argument
and then I can pass in an argument called name with a value that is that
called name with a value that is that variable but again what's really the
variable but again what's really the point here it was kind of prettier all
point here it was kind of prettier all on one line so these are the exact same
on one line so these are the exact same things I'm just trying to tighten things
things I'm just trying to tighten things up further here all right so what just
up further here all right so what just happened if I go back to my form this is
happened if I go back to my form this is still index.html if I reload it nothing
still index.html if I reload it nothing has changed if I type in my name to this
has changed if I type in my name to this form notice again the URL I'm currently
form notice again the URL I'm currently at this is Chrome hiding things it's
at this is Chrome hiding things it's technically slash by default even though
technically slash by default even though many browsers are just hiding
many browsers are just hiding unnecessary uh characters these days but
unnecessary uh characters these days but Watch What Happens now if I scroll over
Watch What Happens now if I scroll over here and I click greet on this new
here and I click greet on this new form notice my URL my route changed to
form notice my URL my route changed to SL greet question mark name equals David
SL greet question mark name equals David and the body of the page at top left
and the body of the page at top left says hello comma David so this is
says hello comma David so this is exactly how google.com works and it's
exactly how google.com works and it's how we implemented search. HTML last
how we implemented search. HTML last time but instead of submitting the form
time but instead of submitting the form to Google via the form I'm submitting it
to Google via the form I'm submitting it to myself my very own route so I'm
to myself my very own route so I'm implementing my own backend for this
implementing my own backend for this same front end all right any questions
same front end all right any questions just yet much less interesting than
just yet much less interesting than Google certainly but we kind of have all
Google certainly but we kind of have all of the wiring
of the wiring now any
now any questions no
questions no all right so what can we do to further
all right so what can we do to further uh tighten this up and adhere to some
uh tighten this up and adhere to some conventions well let me propose that in
conventions well let me propose that in this version we solve one problem and
this version we solve one problem and even if you've never done this sort of
even if you've never done this sort of thing before I dare say we have enough
thing before I dare say we have enough weeks of cs-50 where if I show you
weeks of cs-50 where if I show you index.html again and greet HTML again
index.html again and greet HTML again odds are to someone's mind there's a
odds are to someone's mind there's a opportunity for
opportunity for improvement why is this web app super
improvement why is this web app super simple though it is arguably poorly
simple though it is arguably poorly designed at the
designed at the moment and the answer lies somewhere in
moment and the answer lies somewhere in these two templates index.html and
these two templates index.html and greet.tolowercase
probably did necessarily for your homepage why because when you have HTML
homepage why because when you have HTML only maybe CSS and even JavaScript
only maybe CSS and even JavaScript that's all you can do is copy paste copy
that's all you can do is copy paste copy paste and just make sure that you have
paste and just make sure that you have the same structure maybe you have the
the same structure maybe you have the same CSS file the same Javascript file
same CSS file the same Javascript file the same third party libraries but it
the same third party libraries but it makes it very very annoying as you might
makes it very very annoying as you might have realized already to just m make a
have realized already to just m make a change that affects everything so
change that affects everything so wouldn't it be nice to like factor out
wouldn't it be nice to like factor out all of this and all of this and just let
all of this and all of this and just let the body change so here too is something
the body change so here too is something that flask and really other equivalent
that flask and really other equivalent Frameworks let us do it allows us to
Frameworks let us do it allows us to create what we're going to call
create what we're going to call conventionally a layout instead so I'm
conventionally a layout instead so I'm going to go ahead and do this I'm going
going to go ahead and do this I'm going to copy one last time all of the same
to copy one last time all of the same HTML I'm going to go into uh my terminal
HTML I'm going to go into uh my terminal window and I'm going to create by
window and I'm going to create by convention a file called layout. HTML
convention a file called layout. HTML this is truly going to be a blueprint of
this is truly going to be a blueprint of sorts and in layout. HTML I'm going to
sorts and in layout. HTML I'm going to paste all of that same code but I'm
paste all of that same code but I'm going to use now some ginger syntax to
going to use now some ginger syntax to indicate that I don't want to plug in
indicate that I don't want to plug in just a variable like name here I want to
just a variable like name here I want to actually plug the contents of a whole
actually plug the contents of a whole other file so instead of just using
other file so instead of just using curly braces two of them left and right
curly braces two of them left and right I have to use slightly different syntax
I have to use slightly different syntax to say I want a whole block of HTML here
to say I want a whole block of HTML here from some other file and the way to do
from some other file and the way to do this even though the syntax is a little
this even though the syntax is a little non-obvious is you use open curly brace
non-obvious is you use open curly brace percent sign block then you can call the
percent sign block then you can call the next word anything you want it just has
next word anything you want it just has to be a special type of placeholder for
to be a special type of placeholder for an actual file not for just a variable
an actual file not for just a variable I'm going to call it body only because
I'm going to call it body only because I'm in the body so I'm want a
I'm in the body so I'm want a placeholder to be the entire body and
placeholder to be the entire body and then outside of this you then say in one
then outside of this you then say in one word no space end block so it looks kind
word no space end block so it looks kind of stupid honestly and why do we have
of stupid honestly and why do we have yet more ugly syntax again just
yet more ugly syntax again just different software developers in the
different software developers in the world are all choosing their own Syntax
world are all choosing their own Syntax for their own libraries so they all kind
for their own libraries so they all kind of look different but are all kind of
of look different but are all kind of similar in spirit and you just get used
similar in spirit and you just get used to seeing the different syntax this now
to seeing the different syntax this now is not nearly as pretty as the pair of
is not nearly as pretty as the pair of curly braces for variables but this is
curly braces for variables but this is how I can say plug the contents of an
how I can say plug the contents of an entire file Here and Now what does this
entire file Here and Now what does this let me do I can now go back into my
let me do I can now go back into my index.html file which at the moment
index.html file which at the moment still looks like this but almost all of
still looks like this but almost all of this is copy paste the only lines that
this is copy paste the only lines that are interesting and different are these
are interesting and different are these four lines here in the body so what I
four lines here in the body so what I can actually do now is I'm going to
can actually do now is I'm going to highlight that and cut it and then I'm
highlight that and cut it and then I'm going to highlight everything else and
going to highlight everything else and just delete the entire file and I'm
just delete the entire file and I'm going to use some of that same syntax
going to use some of that same syntax and say curly brace percent sign extends
and say curly brace percent sign extends quote unquote layout. HTML and then I
quote unquote layout. HTML and then I close my thought with a percent sign and
close my thought with a percent sign and close curly brace so this syntax as you
close curly brace so this syntax as you might just be inferring is now saying
might just be inferring is now saying please extend whatever layout. HTML
please extend whatever layout. HTML looks like that's the original blueprint
looks like that's the original blueprint the mold out of which I want to make
the mold out of which I want to make this web page and now here the syntax is
this web page and now here the syntax is a little weird too but similar at least
a little weird too but similar at least from before I can now say the block the
from before I can now say the block the body block that I want you to plug into
body block that I want you to plug into that layout is going to be everything
that layout is going to be everything between these two tags which we already
between these two tags which we already saw earlier but in layout. HTML they're
saw earlier but in layout. HTML they're sort of giving a placeholder in
sort of giving a placeholder in index.html this is what I'm going to
index.html this is what I'm going to plug in to those other placeholders as
plug in to those other placeholders as well so I'm just going to give myself
well so I'm just going to give myself some extra white space I'm going to
some extra white space I'm going to paste the HTML that was there if I want
paste the HTML that was there if I want to make clear what's going on I can
to make clear what's going on I can indent it although this has no no
indent it although this has no no functional impact but it just makes
functional impact but it just makes clear that just like in HTML you can
clear that just like in HTML you can open a ginger tag and close it but in
open a ginger tag and close it but in ginger here we have this here hey uh Hey
ginger here we have this here hey uh Hey python here comes the body of this page
python here comes the body of this page hey python that's it for the body of
hey python that's it for the body of this page and all of this stuff should
this page and all of this stuff should be plugged into this main parent layout
be plugged into this main parent layout if you will so super ugly admittedly but
if you will so super ugly admittedly but now at least things get way less
now at least things get way less redundant because I'm going to do the
redundant because I'm going to do the exact same thing over here in greet HTML
exact same thing over here in greet HTML it looks like this but now I'm going to
it looks like this but now I'm going to do this extends layout. HTML also just
do this extends layout. HTML also just as before uh the body the uh the body
as before uh the body the uh the body that I want to plug in is going to be
that I want to plug in is going to be everything inside of these tags here and
everything inside of these tags here and this body is just going to be hello
this body is just going to be hello comma name in curly braces like that so
comma name in curly braces like that so again ugly syntax got really ugly fast
again ugly syntax got really ugly fast but it's really just following these
but it's really just following these patterns now and we have two types of
patterns now and we have two types of placeholders two curly braces for
placeholders two curly braces for variables and now this kind of syntax
variables and now this kind of syntax with the percent signs and the single
with the percent signs and the single curly braces for like contents of actual
curly braces for like contents of actual files and so now in this world or in the
files and so now in this world or in the world of a homepage if you were using
world of a homepage if you were using flask and python to make your personal
flask and python to make your personal homepage with all of those various Pages
homepage with all of those various Pages you would probably design one main
you would probably design one main layout with all of your pretty logos and
layout with all of your pretty logos and colors and fonts and like what you want
colors and fonts and like what you want the site to look like and then each of
the site to look like and then each of your smaller Pages would now be
your smaller Pages would now be distilled into just these smaller
distilled into just these smaller fragments and whether you're using
fragments and whether you're using python or Java or Javas script or other
python or Java or Javas script or other languages too all different programming
languages too all different programming languages have popular Frameworks that
languages have popular Frameworks that do things like this the idea is the same
do things like this the idea is the same across all of them all right let's see
across all of them all right let's see if it works let's go back into the
if it works let's go back into the browser let me go back to my slash route
browser let me go back to my slash route there's that same form let me type in
there's that same form let me type in David and type and click greet and
David and type and click greet and indeed I see hello comma David I see
indeed I see hello comma David I see that greet was automatically added to
that greet was automatically added to the URL by the browser when I submitted
the URL by the browser when I submitted the form followed by the key value Pairs
the form followed by the key value Pairs and if I view the page source as I did
and if I view the page source as I did earlier you'll see that you have the
earlier you'll see that you have the entirety of that layout with hello David
entirety of that layout with hello David plugged in meanwhile if I go back to the
plugged in meanwhile if I go back to the form and view this page Source you'll
form and view this page Source you'll see the exact same layout but with the
see the exact same layout but with the form tag plugged in and here's where you
form tag plugged in and here's where you can be a little less uh nitpicky with
can be a little less uh nitpicky with styling okay yes this isn't technically
styling okay yes this isn't technically indented inside of the body but it was
indented inside of the body but it was relative to the original file so at this
relative to the original file so at this point in the game you don't need to
point in the game you don't need to worry about your outputed HTML looking
worry about your outputed HTML looking super pretty you want your source code
super pretty you want your source code that the see to be pretty not the
that the see to be pretty not the browser this is not a stylistic
browser this is not a stylistic concern okay questions on these
concern okay questions on these capabilities then a
capabilities then a flask or problems that we've just solved
flask or problems that we've just solved and why
yeah uh okay so if the files in question are in different folders for instance if
are in different folders for instance if I go back into my uh index page which
I go back into my uh index page which has the form um the routes here are
has the form um the routes here are entirely dependent on what is an app.py
entirely dependent on what is an app.py there's no notion of a folder when it
there's no notion of a folder when it comes to implementing a web application
comes to implementing a web application anymore they are more generically routes
anymore they are more generically routes however and we've not done this yet you
however and we've not done this yet you can put your static content your images
can put your static content your images your video files your CSS files in a
your video files your CSS files in a folder called Static and there can be
folder called Static and there can be subfolders therein and that would affect
subfolders therein and that would affect what you use as your Source attributes
what you use as your Source attributes for images or your Source uh tags for
for images or your Source uh tags for video or any of those kinds of assets
video or any of those kinds of assets and we'll see that eventually in the
and we'll see that eventually in the home in the uh the problem set next
home in the uh the problem set next other questions on what we've just done
other questions on what we've just done here
here yeah good question how do I how did I
yeah good question how do I how did I ensure that the web app starts on the
ensure that the web app starts on the form and then goes to the hello page so
form and then goes to the hello page so whatever you decide your default index
whatever you decide your default index route is like the implicit slash that is
route is like the implicit slash that is what is going to be pulled up when a
what is going to be pulled up when a user visits the domain name where your
user visits the domain name where your website is hosted so if I go back over
website is hosted so if I go back over here to app Pi because my slash route is
here to app Pi because my slash route is designed to return index.html that's
designed to return index.html that's exactly why that response came back good
exactly why that response came back good question all right so beyond this let's
question all right so beyond this let's consider now issues of privacy and we'll
consider now issues of privacy and we'll touch on this too as we get to issues
touch on this too as we get to issues like shopping carts and cookies if I
like shopping carts and cookies if I zoom in on the URL here even though the
zoom in on the URL here even though the URL itself is a little long and ugly and
URL itself is a little long and ugly and cryptic but when I type in my name and
cryptic but when I type in my name and hit uh greet of course we keep seeing
hit uh greet of course we keep seeing name equals David in the URL in what
name equals David in the URL in what sense might this be bad
sense might this be bad design or in what kinds of web apps
design or in what kinds of web apps might you not want the name to show up
might you not want the name to show up in the URL like that because this is
in the URL like that because this is what Google does this is what my app
what Google does this is what my app does
does yeah yeah so if I'm logging in with a
yeah yeah so if I'm logging in with a username and password I I could imagine
username and password I I could imagine that they show up in the URL after the
that they show up in the URL after the question mark where username equals
question mark where username equals mailin and password equals 1 2 3 4 5 but
mailin and password equals 1 2 3 4 5 but then all my you know like nosy siblings
then all my you know like nosy siblings need to do is go through through my
need to do is go through through my browser history and boom like it's right
browser history and boom like it's right there for them to copy paste so that
there for them to copy paste so that doesn't seem particularly uh secure or
doesn't seem particularly uh secure or if someone's walking past you in a cafe
if someone's walking past you in a cafe they can just look at your url if it's
they can just look at your url if it's revealed by the browser and they see it
revealed by the browser and they see it too so get is not necessarily the best
too so get is not necessarily the best verb to use even though it's the default
verb to use even though it's the default when submitting forms typically when
when submitting forms typically when you've got anything remotely sensitive
you've got anything remotely sensitive or anything large so be it a password or
or anything large so be it a password or credit card number or like an image or a
credit card number or like an image or a video that you're uploading to uh
video that you're uploading to uh Instagram or to YouTube or any such site
Instagram or to YouTube or any such site like that you don't want the data going
like that you don't want the data going into the URL and thankfully there's
into the URL and thankfully there's actually an easy way to fix this I can
actually an easy way to fix this I can go into my form which is currently in
go into my form which is currently in index.html and I can just change the
index.html and I can just change the method from get to post in lower case
method from get to post in lower case here but the verb in the virtual
here but the verb in the virtual envelope we discussed last week would
envelope we discussed last week would itself be capital p o St now
itself be capital p o St now unfortunately Watch What Happens here
unfortunately Watch What Happens here let me go over to my original form by
let me go over to my original form by going back to the Slash rout and I'm
going back to the Slash rout and I'm reloading the page to make sure I get
reloading the page to make sure I get the latest freshest HTML and just as a
the latest freshest HTML and just as a um just to confirm here yep if I view
um just to confirm here yep if I view Source method is now post so let me go
Source method is now post so let me go ahead and type in David now and click
ahead and type in David now and click greet and before we saw hello David but
greet and before we saw hello David but now I get method not allowed and this is
now I get method not allowed and this is somewhat subtle but in the title of the
somewhat subtle but in the title of the tab notice that it's a 405 error which
tab notice that it's a 405 error which is not familiar probably almost all of
is not familiar probably almost all of us have seen 404 file not found turns
us have seen 404 file not found turns out 405 a little more Arcane is the
out 405 a little more Arcane is the method the HTTP verb is not allowed why
method the HTTP verb is not allowed why because by default my app.py only
because by default my app.py only currently supports get by default how do
currently supports get by default how do I support post well I just need a little
I support post well I just need a little bit more syntax so let me go back into
bit more syntax so let me go back into vs code here let me go into app.py now
vs code here let me go into app.py now and after changing the form I just need
and after changing the form I just need to inform flask that you know what the
to inform flask that you know what the method I want this GRE route to use
method I want this GRE route to use should not be the default which is only
should not be the default which is only get I want it to use these methods and
get I want it to use these methods and it takes a second argument called
it takes a second argument called Methods the value of which is a list the
Methods the value of which is a list the default of which is quote unquote get so
default of which is quote unquote get so that's the default this has not made any
that's the default this has not made any changes but if I want to support post
changes but if I want to support post instead I can explicitly pass a list
instead I can explicitly pass a list with one string in it P instead and now
with one string in it P instead and now what does this mean we didn't talk about
what does this mean we didn't talk about this in any detail last week but inside
this in any detail last week but inside of this virtual envelope typically is
of this virtual envelope typically is that line like get slash search Q equals
that line like get slash search Q equals cat after the ex after the question mark
cat after the ex after the question mark if you want to hide that kind of
if you want to hide that kind of information for privacy sake or because
information for privacy sake or because you want to upload like an image which
you want to upload like an image which just doesn't make sense to put in the
just doesn't make sense to put in the URL essentially the part of the story
URL essentially the part of the story would be well the computer looks deeper
would be well the computer looks deeper inside of that virtual envelope and
inside of that virtual envelope and anything submitted via post goes below
anything submitted via post goes below the htdp headers like deeper in that
the htdp headers like deeper in that envelope so they're still there they're
envelope so they're still there they're just not obviously visible uh for prying
just not obviously visible uh for prying eyes in the user's own browser so just
eyes in the user's own browser so just by making that change in the HTML
by making that change in the HTML telling the browser to submit the data
telling the browser to submit the data via post and changing app.py to tell the
via post and changing app.py to tell the route to expect the data via post I can
route to expect the data via post I can now go back to my other tab let me go
now go back to my other tab let me go back to the original page let me reload
back to the original page let me reload just so I've got the latest HTML and
just so I've got the latest HTML and indeed view page Source it's still yep
indeed view page Source it's still yep it's still post but now when I type in
it's still post but now when I type in DAV ID and click greet now it works but
DAV ID and click greet now it works but but but notice the Privacy implication
but but notice the Privacy implication I'm at the SLG greet route but where's
I'm at the SLG greet route but where's my name it's not actually there it's
my name it's not actually there it's still went to the server but it's not in
still went to the server but it's not in your autocomplete or your history now
your autocomplete or your history now for privacy sake questions now on
for privacy sake questions now on post yeah oh No Just scratching all
post yeah oh No Just scratching all right can you the programmer see this
right can you the programmer see this well let me show you a couple of other
well let me show you a couple of other features of Chrome's uh Chrome and
features of Chrome's uh Chrome and Safari and other browsers as well I keep
Safari and other browsers as well I keep going to view page source which just
going to view page source which just shows you like a readon version of your
shows you like a readon version of your HTML but recall that last time I
HTML but recall that last time I actually right-clicked and went to
actually right-clicked and went to inspect or viewed developer tool tools
inspect or viewed developer tool tools and this brings up a much fancier
and this brings up a much fancier version of the developer tools and under
version of the developer tools and under elements here you see everything and
elements here you see everything and it's nice and pretty printed it's
it's nice and pretty printed it's hierarchical it collapses things into
hierarchical it collapses things into these clickable triangles but it's the
these clickable triangles but it's the exact same thing it's just more
exact same thing it's just more interactive but notice what I can do
interactive but notice what I can do today is this if I go to the network tab
today is this if I go to the network tab here and let me zoom out a little bit
here and let me zoom out a little bit let me go ahead and re uh load the form
let me go ahead and re uh load the form here and type in David again and click
here and type in David again and click greet notice now in the network tab of
greet notice now in the network tab of Chrome's developer tools I see a few
Chrome's developer tools I see a few things as we saw before one I see that
things as we saw before one I see that the request method is post two I see
the request method is post two I see that the server automatically without me
that the server automatically without me writing any code for this returns 200
writing any code for this returns 200 when it's successful but I can scroll
when it's successful but I can scroll down down down down down and you'll see
down down down down down and you'll see that eventually after all these cookies
that eventually after all these cookies more on those later if I click on
more on those later if I click on payload the second tab next to headers
payload the second tab next to headers you can see as the developer what was
you can see as the developer what was actually sent to the server so indeed
actually sent to the server so indeed this is going to be super useful like
this is going to be super useful like when doing problem set 9 maybe your
when doing problem set 9 maybe your final projects if you want to see what's
final projects if you want to see what's going from browser to server you have
going from browser to server you have complete control over all of that
complete control over all of that information even if you're using htps
information even if you're using htps because your browser and you the
because your browser and you the developer can certainly see all of this
developer can certainly see all of this so again these developer tools even
so again these developer tools even though there's a lot of tabs and buttons
though there's a lot of tabs and buttons you probably won't need anytime soon
you probably won't need anytime soon some of them like elements and network
some of them like elements and network and with JavaScript console are going to
and with JavaScript console are going to be super useful to start to get familiar
be super useful to start to get familiar with all right any questions now on this
with all right any questions now on this implication of
implication of post anything at
post anything at all no okay how about one final hello
all no okay how about one final hello example that ties a few of these things
example that ties a few of these things together how about now we try to tighten
together how about now we try to tighten things up further only in anticipation
things up further only in anticipation of something like problem set 9 or
of something like problem set 9 or really more complicated web apps where
really more complicated web apps where you might have not two but 20 or maybe
you might have not two but 20 or maybe even more different routes it might
even more different routes it might might be ideal to just minimize how many
might be ideal to just minimize how many total routes we have so we don't get a
total routes we have so we don't get a little too overwhelmed and I dare say
little too overwhelmed and I dare say that these two routes are so short maybe
that these two routes are so short maybe I can combine them into one and maybe I
I can combine them into one and maybe I can keep the user at what seems to be
can keep the user at what seems to be the same URL but just a kind of Tidy
the same URL but just a kind of Tidy things up so let me propose that we do
things up so let me propose that we do this instead let me get rid of my greet
this instead let me get rid of my greet route and let me go into my form in
route and let me go into my form in index.html and let me go ahead and just
index.html and let me go ahead and just have the action of this form still slash
have the action of this form still slash so I want the form to be visible at
so I want the form to be visible at slash the index of the site but I also
slash the index of the site but I also want the form to submit to itself if
want the form to submit to itself if only because I don't want to introduce
only because I don't want to introduce another route like SLG greet which
another route like SLG greet which eventually indeed will be compelling so
eventually indeed will be compelling so you don't have one route for everything
you don't have one route for everything you want your website to do so
you want your website to do so technically this is the default to and
technically this is the default to and if I omit action the exact same thing
if I omit action the exact same thing would happen as well but let me rewind
would happen as well but let me rewind and let me now go into app.py to see how
and let me now go into app.py to see how we can make this happen well if I want
we can make this happen well if I want my one and now only route to support
my one and now only route to support both methods I can say methods equals
both methods I can say methods equals and then a list with both get and post
and then a list with both get and post in any order but I'll keep them
in any order but I'll keep them alphabetical like this this now tells
alphabetical like this this now tells python hey this route should handle both
python hey this route should handle both get and post requests at the same place
get and post requests at the same place let's now go into this function I kind
let's now go into this function I kind of want to say the equivalent of this if
of want to say the equivalent of this if get then I want to return the form else
get then I want to return the form else if post I want to then return render
if post I want to then return render template of greet Doh HTML with the
template of greet Doh HTML with the user's name but this is not yet complete
user's name but this is not yet complete code but I think I can do this I'm going
code but I think I can do this I'm going to go ahead and say the following I'm
to go ahead and say the following I'm going to go ahead and say if
going to go ahead and say if request. method equals equals get then
request. method equals equals get then indeed return
indeed return index.html L if request. method equals
index.html L if request. method equals equals post then go ahead and return
equals post then go ahead and return greet HTML this isn't quite enough
greet HTML this isn't quite enough though because I still want to pass in
though because I still want to pass in that placeholder so let me again add
that placeholder so let me again add back name equals request. args doget
back name equals request. args doget quote unquote name and then a default
quote unquote name and then a default value of world what does this Now do for
value of world what does this Now do for me well let me go back to my other tab
me well let me go back to my other tab here let me close the developer tools
here let me close the developer tools let me go back to the form here let me
let me go back to the form here let me reload to make sure I have the latest
reload to make sure I have the latest let me view page Source just to make
let me view page Source just to make sure I have the latest and yep I have
sure I have the latest and yep I have the latest because it still says post
the latest because it still says post but it now says slash and let's see what
but it now says slash and let's see what happens now if I type in my name David
happens now if I type in my name David previously this submitted bya post so I
previously this submitted bya post so I didn't see any name or value thereof in
didn't see any name or value thereof in the URL but I did end up at SLG greet
the URL but I did end up at SLG greet but if the action is now slash and I
but if the action is now slash and I click greet notice that it still kind of
click greet notice that it still kind of works I see hello comma World although
works I see hello comma World although that didn't quite work so we'll come
that didn't quite work so we'll come back to that issue in a moment but
back to that issue in a moment but notice the URL ends in just slash and
notice the URL ends in just slash and again Chrome is hiding the slash because
again Chrome is hiding the slash because that's all that's there but it does not
that's all that's there but it does not end in name equals David in this case or
end in name equals David in this case or name and equals world now notice this
name and equals world now notice this too if I reload I'm going to get this
too if I reload I'm going to get this warning do you want to confirm form
warning do you want to confirm form resubmission the page you're looking for
resubmission the page you're looking for used information that you entered
used information that you entered returning to that page might cause any
returning to that page might cause any action you took to be repeated do you
action you took to be repeated do you want to continue you might have seen
want to continue you might have seen this on websites you've actually visited
this on websites you've actually visited where you hit reload and you're prompted
where you hit reload and you're prompted wait a minute do you want to do that
wait a minute do you want to do that odds are you've been prompted to reload
odds are you've been prompted to reload explicitly because why whatever you just
explicitly because why whatever you just did was post instead of get and by
did was post instead of get and by convention besides post being used for
convention besides post being used for privacy to like hide your username your
privacy to like hide your username your password your credit card number or the
password your credit card number or the like besides being used to upload bigger
like besides being used to upload bigger files like images or videos post is also
files like images or videos post is also used by convention to make changes to
used by convention to make changes to the server to add something to your
the server to add something to your shopping cart to add something to the
shopping cart to add something to the database whereas get is the name
database whereas get is the name suggests is all about getting
suggests is all about getting information not posting or sending
information not posting or sending information instead so this is Chrome
information instead so this is Chrome being a little careful because you know
being a little careful because you know if you just checked out on Amazon and
if you just checked out on Amazon and then you hit reload you don't want to
then you hit reload you don't want to accidentally like buy the same book
accidentally like buy the same book again so to speak even though Amazon and
again so to speak even though Amazon and fancy websites have other defenses for
fancy websites have other defenses for this too to avoid this issue now there
this too to avoid this issue now there is a bug though here it says hello comma
is a bug though here it says hello comma World instead of hello comma David and
World instead of hello comma David and it actually would have said the same a
it actually would have said the same a moment ago and I just didn't retest the
moment ago and I just didn't retest the code and reveal as much to you or if I
code and reveal as much to you or if I did I didn't even notice it said hello
did I didn't even notice it said hello world instead of hello David it turns
world instead of hello David it turns out that request. RGS is only used for
out that request. RGS is only used for get when using get request. args is a
get when using get request. args is a dictionary that contains all of your key
dictionary that contains all of your key value pairs but somewhat confusingly
value pairs but somewhat confusingly when using post with flask you have to
when using post with flask you have to go into request. form I have no idea why
go into request. form I have no idea why these are not sort of more obvious
these are not sort of more obvious opposites like request.get or request.
opposites like request.get or request. form and sorry request.get and request.
form and sorry request.get and request. poost would be sort of sensible names in
poost would be sort of sensible names in this case though we have request. args
this case though we have request. args for get and request. form for post all
for get and request. form for post all right that's an easy fix though if I go
right that's an easy fix though if I go back to vs code here let's change
back to vs code here let's change request. ARS to request. form let's go
request. ARS to request. form let's go back to my other tab let me just reload
back to my other tab let me just reload and you know what I'm going to say okay
and you know what I'm going to say okay continue to resubmit the same form
continue to resubmit the same form because the form was okay it was my
because the form was okay it was my python code that was buggy hitting enter
python code that was buggy hitting enter now it's accessing David okay but watch
now it's accessing David okay but watch this again if I hit reload command r or
this again if I hit reload command r or control r i get the same warning are you
control r i get the same warning are you sure you want to submit the form yes if
sure you want to submit the form yes if I do it manually with the reload icon I
I do it manually with the reload icon I get the same warning as before but if I
get the same warning as before but if I want to manually induce a get request
want to manually induce a get request well that's fine don't hit reload and
well that's fine don't hit reload and send the same request instead go up to
send the same request instead go up to your URL and just put the cursor up
your URL and just put the cursor up there and hit enter and now notice same
there and hit enter and now notice same URL is a get by default so anytime you
URL is a get by default so anytime you and I have typed URLs into browsers get
and I have typed URLs into browsers get is always the default only when you
is always the default only when you click on a button typically that the
click on a button typically that the programmer has configured to use post
programmer has configured to use post are you actually adding things to your
are you actually adding things to your shopping cart or the like all right so
shopping cart or the like all right so we are back and if I go way back in time
we are back and if I go way back in time myself like this is actually like the
myself like this is actually like the first web application I made back in
first web application I made back in 1997 I believe uh so at the time this
1997 I believe uh so at the time this was would have been what my sophomore or
was would have been what my sophomore or so year I had taken cs50 I took a
so year I had taken cs50 I took a follow- on class called cs51 which is a
follow- on class called cs51 which is a different type of programming and then I
different type of programming and then I pretty much taught myself a language
pretty much taught myself a language called Pearl which is somewhat less
called Pearl which is somewhat less popular nowadays but it's another
popular nowadays but it's another language like python like Java like
language like python like Java like JavaScript like others that can be used
JavaScript like others that can be used to make web-based applications and the
to make web-based applications and the web was very young at the time and the
web was very young at the time and the process via which students my classmates
process via which students my classmates could register uh for the first year
could register uh for the first year intral sports program AKA Frost's was to
intral sports program AKA Frost's was to grab a piece of paper and like write
grab a piece of paper and like write your name and email address on it and
your name and email address on it and walk it across across the yard to
walk it across across the yard to Wigglesworth I believe where the Proctor
Wigglesworth I believe where the Proctor lived and you'd Slide the piece of paper
lived and you'd Slide the piece of paper under the door and like that was how we
under the door and like that was how we submitted forms in my day um so this was
submitted forms in my day um so this was an opportunity even back in 1997 is to
an opportunity even back in 1997 is to like move things online and the website
like move things online and the website went on to live on until I think like
went on to live on until I think like 2007 I found this online and then it's
2007 I found this online and then it's become something else since um but this
become something else since um but this was a website via which people could
was a website via which people could register for sports and people could log
register for sports and people could log in the scores for various games and
in the scores for various games and whatnot and so underneath the hood I
whatnot and so underneath the hood I didn't even know anything anything about
didn't even know anything anything about databases at the time it was just like
databases at the time it was just like CSV files that I was storing the data in
CSV files that I was storing the data in but there were HTML forms and there was
but there were HTML forms and there was with pearl the language at the time the
with pearl the language at the time the way to do the exact kind of stuff that
way to do the exact kind of stuff that we've just been doing already with flask
we've just been doing already with flask and so what I thought we'd do is
and so what I thought we'd do is Implement a slightly less ugly version
Implement a slightly less ugly version of this um repeating uh graphical
of this um repeating uh graphical backgrounds were invogue in like 1997 as
backgrounds were invogue in like 1997 as you can see here um but this is where
you can see here um but this is where these were the Aesthetics of the day uh
these were the Aesthetics of the day uh including the the so-called blink tag so
including the the so-called blink tag so let's at least focus on the
let's at least focus on the functionality of this website and not so
functionality of this website and not so much the Aesthetics and see if we can't
much the Aesthetics and see if we can't Implement some of the plumbing for
Implement some of the plumbing for actually solving like a real world
actually solving like a real world representative problem be it for
representative problem be it for freshman inal sports or something else
freshman inal sports or something else like it where you're getting data from
like it where you're getting data from users and processing it somehow so let
users and processing it somehow so let me go over here to VSS code let me
me go over here to VSS code let me create a new directory called fros IMS
create a new directory called fros IMS just so we can keep all of this code in
just so we can keep all of this code in its own directory let me CD into fros
its own directory let me CD into fros IMS let me proactively make another
IMS let me proactively make another directory called templates in which our
directory called templates in which our templates our HTML files do need to live
templates our HTML files do need to live and eventually I'm going to go ahead and
and eventually I'm going to go ahead and create a two files minimally app.py and
create a two files minimally app.py and index.html so let's do the first of
index.html so let's do the first of those app.py will live in my Frost im's
those app.py will live in my Frost im's directory and I'm just going to recreate
directory and I'm just going to recreate something very simple like we have
something very simple like we have previously so from flask in lowercase
previously so from flask in lowercase import flask capitalized render template
import flask capitalized render template and also request so same first line is
and also request so same first line is before let me then give myself a
before let me then give myself a variable called app set it equal to
variable called app set it equal to calling the flask function capital f
calling the flask function capital f with underscore uncore name underscore
with underscore uncore name underscore uncore and then let me give myself a
uncore and then let me give myself a route for slash as before with an index
route for slash as before with an index function though again I could call that
function though again I could call that anything I want and just for now let's
anything I want and just for now let's return render template of quote unquote
return render template of quote unquote index.html as though that exists so this
index.html as though that exists so this is not really a web application as much
is not really a web application as much as it is at the moment just a recreation
as it is at the moment just a recreation of HTTP server for one file let's now in
of HTTP server for one file let's now in another tab create a templates file uh
another tab create a templates file uh called called
called called index.html and I'm going to save myself
index.html and I'm going to save myself a few keystrokes I let me copy paste
a few keystrokes I let me copy paste from earlier almost all of the layout
from earlier almost all of the layout from before I've change the title in
from before I've change the title in advance to frosts instead of hello but
advance to frosts instead of hello but this is essentially the same template
this is essentially the same template and for now though because I'm in an
and for now though because I'm in an index.html I'm not going to use extends
index.html I'm not going to use extends or any of that fancy block stuff yet I'm
or any of that fancy block stuff yet I'm just going to go ahead and create a
just going to go ahead and create a relatively simple form via which back in
relatively simple form via which back in the day my classmates could have
the day my classmates could have registered for interal Sports so let's
registered for interal Sports so let's go ahead here and I'll propose that we
go ahead here and I'll propose that we do this um in this page we'll have a
do this um in this page we'll have a form the action of which will be a route
form the action of which will be a route called SL register though I could call
called SL register though I could call that anything I want it'll be somewhat
that anything I want it'll be somewhat private so I'm going to use post instead
private so I'm going to use post instead of get just so that people don't
of get just so that people don't accidentally maybe register twice by
accidentally maybe register twice by hitting reload uh without warning uh
hitting reload uh without warning uh inside of this form let's go ahead and
inside of this form let's go ahead and give them an input uh where autocomplete
give them an input uh where autocomplete will be off as always for demonstration
will be off as always for demonstration sake autofocus so the cursor goes there
sake autofocus so the cursor goes there initially the name of this field will be
initially the name of this field will be literally named because I want my
literally named because I want my classmates's name if they want to
classmates's name if they want to register for some sport the placeholder
register for some sport the placeholder will again be quote unquote Nam just so
will again be quote unquote Nam just so they see some gray instructions and the
they see some gray instructions and the type of this field will indeed be text
type of this field will indeed be text as before and then I need to give them
as before and then I need to give them the ability to register for a few Sports
the ability to register for a few Sports why don't we keep it simple like back in
why don't we keep it simple like back in the day basketball soccer and Ultimate
the day basketball soccer and Ultimate Frisbee were three of the sports that we
Frisbee were three of the sports that we supported and so let me do this and you
supported and so let me do this and you might not have seen this before uh
might not have seen this before uh unless you dabbled further on with forms
unless you dabbled further on with forms on your own but I can create a select
on your own but I can create a select menu otherwise known as a drop-down menu
menu otherwise known as a drop-down menu in HTML inside of which are a whole
in HTML inside of which are a whole bunch of options and each option
bunch of options and each option typically follows this Paradigm the
typically follows this Paradigm the value of the option and then the actual
value of the option and then the actual text that the human sees so the value of
text that the human sees so the value of these options will be how about we do uh
these options will be how about we do uh basketball as one and I want the human
basketball as one and I want the human to see literally the same thing though
to see literally the same thing though just like with a link in HTML they could
just like with a link in HTML they could be different but I'm going to keep them
be different but I'm going to keep them the same another option will be
the same another option will be uh let's say
uh let's say soccer and whoops let me fix my quotes
soccer and whoops let me fix my quotes and this human will see the exact same
and this human will see the exact same thing though it could say something else
thing though it could say something else and then lastly the value will be quote
and then lastly the value will be quote unquote Ultimate Frisbee and the humans
unquote Ultimate Frisbee and the humans will see the same thing there ultimate
will see the same thing there ultimate frisbe all right so this is going to
frisbe all right so this is going to create as we'll soon see just a drop-
create as we'll soon see just a drop- down menu with three separate options if
down menu with three separate options if I want the students to be able to submit
I want the students to be able to submit this now let me give them a button the
this now let me give them a button the type of which is submit and this button
type of which is submit and this button will be like the word register on it so
will be like the word register on it so I think we're pretty much good to go
I think we're pretty much good to go like this is all just HTML no python no
like this is all just HTML no python no flask per se except for the rendering of
flask per se except for the rendering of the same template so let me go into my
the same template so let me go into my terminal window let me do flask run
terminal window let me do flask run inside of this directory because I need
inside of this directory because I need to serve this app instead I'm going to
to serve this app instead I'm going to see some ugly output including my own
see some ugly output including my own URL and if I hover over that and then
URL and if I hover over that and then open that URL I should now see a more
open that URL I should now see a more interesting form it's got not only a
interesting form it's got not only a field for their name but also this
field for their name but also this drop-down menu with all three Sports now
drop-down menu with all three Sports now this isn't maybe the best user uh
this isn't maybe the best user uh experience thus far because I feel like
experience thus far because I feel like I'm biasing people to registering for
I'm biasing people to registering for basketball maybe because it's checked by
basketball maybe because it's checked by default I mean a lot of forms nowadays
default I mean a lot of forms nowadays have like a blank placeholder for the
have like a blank placeholder for the form so this is just an aesthetic thing
form so this is just an aesthetic thing but I can do this let me go back to the
but I can do this let me go back to the same form and let me give myself just a
same form and let me give myself just a a blank option at the top that in fact
a blank option at the top that in fact I'm going to disable so you technically
I'm going to disable so you technically can't select it proactively but I am
can't select it proactively but I am going to select it by default and so we
going to select it by default and so we probably haven't seen those HTML
probably haven't seen those HTML attributes before but if I want to
attributes before but if I want to create the equivalent of like a a a
create the equivalent of like a a a title for this dropdown I'm going to
title for this dropdown I'm going to literally create a disabled option
literally create a disabled option that's automatically selected called
that's automatically selected called sport so that you can't select it per se
sport so that you can't select it per se but it is there at the top so if I go
but it is there at the top so if I go back now to my other tab reload you know
back now to my other tab reload you know it's just marginally prettier than
it's just marginally prettier than before and I'm not biasing people toward
before and I'm not biasing people toward accidentally registering for basketball
accidentally registering for basketball alone and if I click on this you'll see
alone and if I click on this you'll see that sport is great out and therefore
that sport is great out and therefore not manually selectable but I can select
not manually selectable but I can select any of these other three still all right
any of these other three still all right well un unfortunately if I type in David
well un unfortunately if I type in David and I try registering for instance for
and I try registering for instance for soccer and click register I do end up
soccer and click register I do end up atreg and there's no question mark or
atreg and there's no question mark or name or sport so it's probably indeed
name or sport so it's probably indeed post instead of get those are hints but
post instead of get those are hints but not found notice the tab here very uh uh
not found notice the tab here very uh uh uh succinctly says 404 not found well
uh succinctly says 404 not found well why is that just to be clear why did SL
why is that just to be clear why did SL register give me a
404 what's the logic here perhaps just State the obvious
here perhaps just State the obvious or it doesn't exist right we haven't
or it doesn't exist right we haven't done that step yet all right so
done that step yet all right so something as simple as that and so I
something as simple as that and so I actually U sort of belabor that point
actually U sort of belabor that point because as you're learning like a lot of
because as you're learning like a lot of these conventions and some of this new
these conventions and some of this new syntax like honestly you're just going
syntax like honestly you're just going to make stupid mistakes something's not
to make stupid mistakes something's not going to work but again go back to First
going to work but again go back to First principles why is it not found all
principles why is it not found all right/ register should be a template
right/ register should be a template maybe called register. HTML oh I forgot
maybe called register. HTML oh I forgot my app. route so that should be the kind
my app. route so that should be the kind of of thinking as you try to diagnose
of of thinking as you try to diagnose these problems moving forward all right
these problems moving forward all right so let me go into app. and let me give
so let me go into app. and let me give myself a second route here uh so app.
myself a second route here uh so app. route quote unquote SL route then let me
route quote unquote SL route then let me Define a function called anything I want
Define a function called anything I want but I'm going to call it oh sorry not SL
but I'm going to call it oh sorry not SL route SL register let me call the
route SL register let me call the function just to be consistent register
function just to be consistent register so but I could call that anything I want
so but I could call that anything I want and just for now let's not do anything
and just for now let's not do anything too interesting let's just return the
too interesting let's just return the rendering of a template called success
rendering of a template called success .html let's just pretend for now that
.html let's just pretend for now that registration is successful no matter who
registration is successful no matter who you are or what you do now I need that
you are or what you do now I need that template and I only have index.html at
template and I only have index.html at this point so let me actually now do my
this point so let me actually now do my best practices let me copy all of that
best practices let me copy all of that let me in a separate terminal window let
let me in a separate terminal window let me do code uh let me go into my Frost
me do code uh let me go into my Frost im's directory and let me create a new
im's directory and let me create a new template called layout. HTML just like
template called layout. HTML just like before let me paste all that same code
before let me paste all that same code let me delete the form and just put in
let me delete the form and just put in that big placehold folder so block body
that big placehold folder so block body and then end block is all I did earlier
and then end block is all I did earlier this is just kind of boiler plate now
this is just kind of boiler plate now convention everything else I'm going to
convention everything else I'm going to leave the same but if I wanted to make
leave the same but if I wanted to make it prettier I could add my CSS up top if
it prettier I could add my CSS up top if I wanted to add like this crazy
I wanted to add like this crazy repeating background I could probably do
repeating background I could probably do that up top too so I could make every
that up top too so I could make every page look as ugly as it did back in my
page look as ugly as it did back in my day but we'll focus just today on the
day but we'll focus just today on the text all right so now that I have
text all right so now that I have layout. HTML let me clean up index.html
layout. HTML let me clean up index.html I don't need all this redundancy I don't
I don't need all this redundancy I don't need all of these tags at the top
need all of these tags at the top instead recall I think I just need
instead recall I think I just need extends quote unquote layout. HTML with
extends quote unquote layout. HTML with the appropriate percent signs and curly
the appropriate percent signs and curly braces I then have the appropriate block
braces I then have the appropriate block body though I could call Body anything I
body though I could call Body anything I want but I'm going to stick with my
want but I'm going to stick with my convention earlier and I'm going to
convention earlier and I'm going to delete the tags down here that I no
delete the tags down here that I no longer need why because if I go into
longer need why because if I go into layout. HTML I already have all my open
layout. HTML I already have all my open tags all my Clos tags the only stuff I
tags all my Clos tags the only stuff I want in index.html is is going to be
want in index.html is is going to be which belongs in the body so end block
which belongs in the body so end block down here and just to be pedantic let me
down here and just to be pedantic let me go ahead and highlight all that hit
go ahead and highlight all that hit shift Tab and that will like unindent it
shift Tab and that will like unindent it just to line things up just to be tidy
just to line things up just to be tidy all right so better even though it looks
all right so better even though it looks a little cryptic now but now I've laid
a little cryptic now but now I've laid the foundation for making a third page a
the foundation for making a third page a fourth page that don't have all of that
fourth page that don't have all of that same copy paste all right so now let's
same copy paste all right so now let's go back into app.py success. HTML is
go back into app.py success. HTML is where I left off so okay let me open my
where I left off so okay let me open my terminal window let me code up a
terminal window let me code up a template called success. HTML whose
template called success. HTML whose purpose in life is literally just going
purpose in life is literally just going to be like to say you are registered
to be like to say you are registered just so that we see some informative
just so that we see some informative message on the screen so this part I do
message on the screen so this part I do still need extends layout. HTML so
still need extends layout. HTML so there's a little bit of copy paste still
there's a little bit of copy paste still which is a little ugly but so be it
which is a little ugly but so be it block body for this template and I'm
block body for this template and I'm just going to say you are registered
just going to say you are registered exclamation point all right and then end
exclamation point all right and then end block so super simple it's just an
block so super simple it's just an informative message claiming that the
informative message claiming that the Stu the student is registered all right
Stu the student is registered all right let's go back to the original form which
let's go back to the original form which is this let me reload to make sure my
is this let me reload to make sure my HTML has reloaded type in David I'm
HTML has reloaded type in David I'm going to register again for soccer and
going to register again for soccer and click register and oh interesting method
click register and oh interesting method not allowed so I'm not getting a 404
not allowed so I'm not getting a 404 anymore I'm getting
anymore I'm getting 405 at SL
405 at SL register what's the deduction here how
register what's the deduction here how did I screw up this
time 405 is progress yeah the placeholder uh so it's not the
placeholder uh so it's not the placeholder I think is okay this is now
placeholder I think is okay this is now about the underlying HTTP stuff the
about the underlying HTTP stuff the method was disallow was not
method was disallow was not allowed say again so get purposes post
allowed say again so get purposes post thing too so by default all of these
thing too so by default all of these routes in flask just by default assume
routes in flask just by default assume get because it's safe it doesn't allow
get because it's safe it doesn't allow you to send information to the server in
you to send information to the server in quite the same way but if I do want to
quite the same way but if I do want to support post recall that we changed this
support post recall that we changed this to be methods equals and then a list
to be methods equals and then a list with quote quote Post in it so I just
with quote quote Post in it so I just need to enable support for that method
need to enable support for that method that that is that HTTP verb all right
that that is that HTTP verb all right let's go back to the form reload just to
let's go back to the form reload just to make sure I haven't screwed up type in
make sure I haven't screwed up type in my name David select soccer from the
my name David select soccer from the dropdown click register and now I'm not
dropdown click register and now I'm not only at SL register in the URL it claims
only at SL register in the URL it claims that I am indeed registered now of
that I am indeed registered now of course I'm not I've done nothing
course I'm not I've done nothing interesting there's no database there's
interesting there's no database there's no CSV file we'll get to that in a bit
no CSV file we'll get to that in a bit but at least I now have the plumbing in
but at least I now have the plumbing in place to do something Dynamic based on
place to do something Dynamic based on that sport all right well how can I now
that sport all right well how can I now improve upon this how about we go ahead
improve upon this how about we go ahead and implement store the actual
and implement store the actual registrant in a dictionary in the
registrant in a dictionary in the computer's memory so instead of just
computer's memory so instead of just claiming that they're registered let's
claiming that they're registered let's actually make a notation and the
actually make a notation and the simplest way as we did weeks ago in
simplest way as we did weeks ago in Python is just store things in like a
Python is just store things in like a variable in memory like a list or a
variable in memory like a list or a dictionary a set anything like that all
dictionary a set anything like that all right well let me go back into vs code
right well let me go back into vs code and in app.py and I think what I'm going
and in app.py and I think what I'm going to have to do here is change my register
to have to do here is change my register route to actually do some useful
route to actually do some useful information but before I register the
information but before I register the user let's consider where I want to
user let's consider where I want to actually put them and so let me propose
actually put them and so let me propose that how about we do this at the top of
that how about we do this at the top of my file let me go ahead and declare a
my file let me go ahead and declare a global variable called registrant uh and
global variable called registrant uh and set that equal to an empty dictionary so
set that equal to an empty dictionary so we've done this before when we were
we've done this before when we were playing around previously with using
playing around previously with using dictionaries to store key value Pairs
dictionaries to store key value Pairs and I'm going to propose that we store
and I'm going to propose that we store the registrant as a dictionary why cuz
the registrant as a dictionary why cuz I'm going to keep it simple like the
I'm going to keep it simple like the name is going to be the student's name
name is going to be the student's name sorry the key is going to be the
sorry the key is going to be the student's name and the value is going to
student's name and the value is going to be whatever sport they registered for so
be whatever sport they registered for so David and soccer and Carter and
David and soccer and Carter and basketball and so it kind of makes sense
basketball and so it kind of makes sense for like a two column dictionary so to
for like a two column dictionary so to speak as we often depict it on screen so
speak as we often depict it on screen so how can I use this dictionary well let
how can I use this dictionary well let me go ahead and do this down here under
me go ahead and do this down here under SL register let me go ahead and
SL register let me go ahead and initially do this how about we get the
initially do this how about we get the user's name from request. form. Get and
user's name from request. form. Get and set it equal to whatever the value of
set it equal to whatever the value of name is and I'm not going to give a
name is and I'm not going to give a default value now because I don't want
default value now because I don't want to call the student world or something
to call the student world or something strange like that I'm just going to
strange like that I'm just going to assume for now that it's there let's
assume for now that it's there let's then create another variable called
then create another variable called Sport and do request. form doget quote
Sport and do request. form doget quote unquote sport to get these students
unquote sport to get these students Sport and then let's go ahead and do
Sport and then let's go ahead and do this in the
this in the registrant dictionary let's index into
registrant dictionary let's index into it using the student's name and let's
it using the student's name and let's set it equal to whatever the sport is so
set it equal to whatever the sport is so I've got these variables just to keep my
I've got these variables just to keep my code tidy and I'm now putting a key
code tidy and I'm now putting a key value pair in that uh into that
value pair in that uh into that dictionary all right well what do I want
dictionary all right well what do I want to now do and I'll I'll go ahead and say
to now do and I'll I'll go ahead and say success. HTML sure let's go ahead and do
success. HTML sure let's go ahead and do that but now I think success. HTML means
that but now I think success. HTML means that so let me go back to the form
that so let me go back to the form reload let me type in David and soccer
reload let me type in David and soccer register okay let me go back and say
register okay let me go back and say Carter and basketball registered okay
Carter and basketball registered okay now let's see what I want to do next how
now let's see what I want to do next how about I go
about I go into um Let me give myself another route
into um Let me give myself another route and let's play around here so app. route
and let's play around here so app. route let's give myself another third route
let's give myself another third route called registrant whose purpose in life
called registrant whose purpose in life is just to show me who all of those
is just to show me who all of those registrants are just like you would
registrants are just like you would expect from a website like this and then
expect from a website like this and then let me Define a function called
let me Define a function called registrants or anything else and then
registrants or anything else and then let me return the rendering of a
let me return the rendering of a template called
template called registrant H ML and let me pass in this
registrant H ML and let me pass in this is kind of neat I can do registrant
is kind of neat I can do registrant equals registrant which again looks
equals registrant which again looks weird but what am I doing I'm presuming
weird but what am I doing I'm presuming to pass in a placeholder called
to pass in a placeholder called registrant the value of which is this
registrant the value of which is this dictionary that I've been collecting all
dictionary that I've been collecting all of the registrations in so similar to
of the registrations in so similar to the name placeholder before but it's a
the name placeholder before but it's a little more powerful because now it's a
little more powerful because now it's a whole dictionary not just a single
whole dictionary not just a single string so I think now let me be creative
string so I think now let me be creative here let me go into my
here let me go into my templates under uh my templates folder
templates under uh my templates folder and let's do this uh let's go into uh my
and let's do this uh let's go into uh my terminal window let's create another
terminal window let's create another template called registrant HTML that's
template called registrant HTML that's actually going to do this displaying of
actually going to do this displaying of all of the registrant for us so extends
all of the registrant for us so extends layout. HTML just so I can borrow all of
layout. HTML just so I can borrow all of the same HTML as
the same HTML as before and let's define block body just
before and let's define block body just like before and inside of this and block
like before and inside of this and block I want to put I don't know like a
I want to put I don't know like a bulleted list or an ordered list list of
bulleted list or an ordered list list of all of the registrant so how can I do
all of the registrant so how can I do this well let's do uh an unordered list
this well let's do uh an unordered list ul and here's where Ginger and flask
ul and here's where Ginger and flask more generally get kind of interesting
more generally get kind of interesting like I want there to be something like
like I want there to be something like this An
this An Li uh and then like the students's name
Li uh and then like the students's name and then an L uh maybe Li yeah like that
and then an L uh maybe Li yeah like that and then maybe like sport something like
and then maybe like sport something like this but I didn't pass in a name I
this but I didn't pass in a name I didn't pass in a sport I passed in the
didn't pass in a sport I passed in the entire dictionary of registrants now in
entire dictionary of registrants now in Python if we were just doing something
Python if we were just doing something at the black and white terminal window
at the black and white terminal window and doing a command line program you
and doing a command line program you know I'd probably have some kind of for
know I'd probably have some kind of for Loop in Python Ginger does allow you to
Loop in Python Ginger does allow you to do this so a templating language tends
do this so a templating language tends to come with very lightweight mechanisms
to come with very lightweight mechanisms for doing placeholders doing Simple
for doing placeholders doing Simple Loops doing Simple conditions so python
Loops doing Simple conditions so python like syntax and it's almost identical so
like syntax and it's almost identical so watch what I can do inside of this
watch what I can do inside of this unordered list let me not start to
unordered list let me not start to manually output a single Li let me use
manually output a single Li let me use this syntax the same Ginger syntax that
this syntax the same Ginger syntax that I used for Block so curly brace percent
I used for Block so curly brace percent sign and I'm going to say this for name
sign and I'm going to say this for name in registrant so this is just like
in registrant so this is just like python Syntax for iterating over a
python Syntax for iterating over a dictionary and now this is going to look
dictionary and now this is going to look stupid but the opposite of that is end4
stupid but the opposite of that is end4 so in HTML you use the slash in ginger
so in HTML you use the slash in ginger you literally use the word end no space
you literally use the word end no space and then the name of the keyword so end4
and then the name of the keyword so end4 is how you close this but this is where
is how you close this but this is where templating gets really cool you can now
templating gets really cool you can now do Li and in here I can do something
do Li and in here I can do something like that student's name and that's it
like that student's name and that's it I'm going to leave it like that and what
I'm going to leave it like that and what I'm doing here is using really a
I'm doing here is using really a template as templates are intended I've
template as templates are intended I've got like the basic building blocks of
got like the basic building blocks of what I want this output to look like but
what I want this output to look like but thanks to this little for Loop here
thanks to this little for Loop here thanks to Ginger syntax the curly brace
thanks to Ginger syntax the curly brace and the percent sign I'm going to
and the percent sign I'm going to iterate over every dictionary printing
iterate over every dictionary printing out name name name name and so if I've
out name name name name and so if I've got two kids registered now I'm going to
got two kids registered now I'm going to see two liis David and Carter
see two liis David and Carter respectively so let's see let me go back
respectively so let's see let me go back to uh my Frost IM tab here and I don't
to uh my Frost IM tab here and I don't have a link yet so I got to do this
have a link yet so I got to do this manually uh like a developer would let
manually uh like a developer would let me go to slash registrant and I'll zoom
me go to slash registrant and I'll zoom out and hit
out and hit enter and you'll see what you'll
enter and you'll see what you'll probably see two when making mistakes
probably see two when making mistakes for the first time in this world so
for the first time in this world so where is the error message unfortunately
where is the error message unfortunately internal Ser server error is not all
internal Ser server error is not all that useful but we do tell you you see
that useful but we do tell you you see terminal window so if I go to the
terminal window so if I go to the terminal window I haven't been paying
terminal window I haven't been paying attention to this for quite some time
attention to this for quite some time and in fact I have two terminal windows
and in fact I have two terminal windows open so that I can still use commands at
open so that I can still use commands at the prompt but if I go back to my first
the prompt but if I go back to my first terminal window AKA bash there you'll
terminal window AKA bash there you'll see in your terminal window When
see in your terminal window When developing web applications like all of
developing web applications like all of the mistakes you made in the terminal
the mistakes you made in the terminal itself this is one of those python
itself this is one of those python tracebacks that's related to me screwing
tracebacks that's related to me screwing up here now let me go ahead here and
up here now let me go ahead here and let's see uh type error function is not
let's see uh type error function is not iterable
iterable and block for name function is not
iterable all right so what mistake did I make well this is what happens when I
make well this is what happens when I don't follow my notes and make changes
don't follow my notes and make changes on the fly so I have this variable on
on the fly so I have this variable on line five called registrant in all lower
line five called registrant in all lower case but what did I then do on the Fly
case but what did I then do on the Fly here in line
22 I defined a function called registrant so like newbie mistake like I
registrant so like newbie mistake like I shouldn't have done this I can't have a
shouldn't have done this I can't have a variable and a function of the same name
variable and a function of the same name name because the symbols are literally
name because the symbols are literally identical so just to make clear that
identical so just to make clear that this variable up here is actually Global
this variable up here is actually Global we'll use our convention like we did in
we'll use our convention like we did in C often when we had a global variable
C often when we had a global variable we'll capitalize it all just to make it
we'll capitalize it all just to make it stand out like a a constant value up
stand out like a a constant value up there and so down here what I'm going to
there and so down here what I'm going to do is pass in registrants in all cap so
do is pass in registrants in all cap so that was stupid didn't mean to confuse
that was stupid didn't mean to confuse there but the reason for that error to
there but the reason for that error to be clear is that you can't have a
be clear is that you can't have a function that's the same name as a
function that's the same name as a variable I could just change the
variable I could just change the variable name alt together I'm going to
variable name alt together I'm going to go ahead and just capitalize it to make
go ahead and just capitalize it to make it really stand out that this is in fact
it really stand out that this is in fact a global variable up top all right now
a global variable up top all right now I'm going to go back to my browser let's
I'm going to go back to my browser let's do David and
socer all right but there's going to be some other mistakes here so on line 17
some other mistakes here so on line 17 let me go ahead and change this variable
let me go ahead and change this variable to be capitalized there because indeed I
to be capitalized there because indeed I want to put the key and the value in
want to put the key and the value in this newly named variable as all
this newly named variable as all capitals registrant let me now go back
capitals registrant let me now go back to vs code here let me go back to the
to vs code here let me go back to the form and let me start adding some data
form and let me start adding some data fresh let me register David for soccer
fresh let me register David for soccer clicking register now and we should see
clicking register now and we should see you are registered but hopefully now
you are registered but hopefully now it's indeed in the computer's memory let
it's indeed in the computer's memory let me go back and register now Carter for
me go back and register now Carter for basketball clicking register again and
basketball clicking register again and hopefully it's now registered if I now
hopefully it's now registered if I now change my route manually to B SL
change my route manually to B SL registrant which is this newly added
registrant which is this newly added route that I made and hit enter now I
route that I made and hit enter now I see thank God now I see the unordered
see thank God now I see the unordered list containing everything in the
list containing everything in the computer's memory so when I say you are
computer's memory so when I say you are registered I kind of mean it now because
registered I kind of mean it now because the server is still running and in the
the server is still running and in the computer's memory is in this registrant
computer's memory is in this registrant Global variable a dictionary of key
Global variable a dictionary of key value pairs of course we're only seeing
value pairs of course we're only seeing the keys at the moment so it might be
the keys at the moment so it might be nice to actually see the values in as
nice to actually see the values in as well so let me go back to VSS code and
well so let me go back to VSS code and let me go into registr trans. HTML and
let me go into registr trans. HTML and I'll just do something a little messy
I'll just do something a little messy I'll just say uh how about let just make
I'll just say uh how about let just make it a sentence is registered
it a sentence is registered four and now another placeholder I'm
four and now another placeholder I'm going to say registrant bracket name so
going to say registrant bracket name so just like in Python if registr is itself
just like in Python if registr is itself a dictionary registr bracket and then
a dictionary registr bracket and then the key you want to index into is
the key you want to index into is perfectly valid syntax as well so now
perfectly valid syntax as well so now let me go back to SL registrant let me
let me go back to SL registrant let me click reload again so why isn't it
click reload again so why isn't it working everyone what's the bug that I
working everyone what's the bug that I introduced earlier if David is
introduced earlier if David is registered for none and Carter is
registered for none and Carter is registered for none but David and Carter
registered for none but David and Carter are in the dictionary like that's a good
are in the dictionary like that's a good thing so some of the data is in there so
thing so some of the data is in there so why are there no Sports Associated well
why are there no Sports Associated well the first thing I literally just did in
the first thing I literally just did in front of you all was I went to app.py
front of you all was I went to app.py and I stared at line 17 thinking like
and I stared at line 17 thinking like how did I screw this up I'm putting
how did I screw this up I'm putting sport as the value of the key which is
sport as the value of the key which is the student's name all right line 17
the student's name all right line 17 looked fine to me a few seconds ago so I
looked fine to me a few seconds ago so I looked then with my eyes at line 16 and
looked then with my eyes at line 16 and this too looked okay my first thought
this too looked okay my first thought was oh did I use request. args instead
was oh did I use request. args instead of request. form instead because that
of request. form instead because that would have assumed get instead of post
would have assumed get instead of post but no like that looks okay too so then
but no like that looks okay too so then my final Instinct was oh my god did I
my final Instinct was oh my god did I screw up the HTML form and so that's why
screw up the HTML form and so that's why I went back over to my tab here I went
I went back over to my tab here I went to the original form here I then view
to the original form here I then view page source and this might not be as
page source and this might not be as obvious to you if you've never seen the
obvious to you if you've never seen the select menu before what is apparently
select menu before what is apparently missing here that might explain my
missing here that might explain my mistake yeah
mistake yeah yeah I didn't name this form field quote
yeah I didn't name this form field quote unquote sport now to be fair you haven't
unquote sport now to be fair you haven't seen me do this as a select menu before
seen me do this as a select menu before and it's different from this input when
and it's different from this input when you have an input tag you literally say
you have an input tag you literally say name equals whatever on the input tag it
name equals whatever on the input tag it turns out I don't know why I skipped
turns out I don't know why I skipped this earlier I probably meant to come
this earlier I probably meant to come back to it the select tag also can take
back to it the select tag also can take a name parameter so if I go back to the
a name parameter so if I go back to the name
name parameter here and go back and add the
parameter here and go back and add the name parameter let me go into that
name parameter let me go into that template which is
template which is index.html let me add name equals quote
index.html let me add name equals quote unquote sport in all lowercase which is
unquote sport in all lowercase which is different from the visual aesthetic of
different from the visual aesthetic of this temporary disabled option that's
this temporary disabled option that's just there to make things prettier for
just there to make things prettier for the human now let me go ahead here and
the human now let me go ahead here and first I'm going to go into my terminal
first I'm going to go into my terminal window and I'm actually going to hit
window and I'm actually going to hit control C to stop the server alt
control C to stop the server alt together because I want to throw away
together because I want to throw away the contents of memory and therefore get
the contents of memory and therefore get rid of that dictionary that had David
rid of that dictionary that had David and Carter and those nun values so this
and Carter and those nun values so this is sort of me clearing the computer's
is sort of me clearing the computer's memory I'm going to rerun flask run I
memory I'm going to rerun flask run I get that same URL as before so I'm going
get that same URL as before so I'm going to hover over that and open the new tab
to hover over that and open the new tab and just to be sure I'm going to do view
and just to be sure I'm going to do view page source and here I see now okay now
page source and here I see now okay now the form has both a name and a sport in
the form has both a name and a sport in it all right now I'm really going to
it all right now I'm really going to cross my fingers because I intend for
cross my fingers because I intend for this now to work David will register
this now to work David will register again for soccer register claims we are
again for soccer register claims we are registered I'm going to go back and do
registered I'm going to go back and do it again for Carter and basketball
it again for Carter and basketball register we still don't have a link so
register we still don't have a link so I'm going to manually go up to the URL
I'm going to manually go up to the URL and change/ register to registrant as
and change/ register to registrant as before zooming out and hit enter and
before zooming out and hit enter and thank God now I'm actually registered
thank God now I'm actually registered properly for this so oh thank
you so what is it like 20 years later I'm still struggling to implement this
I'm still struggling to implement this site okay so um so here now we have for
site okay so um so here now we have for the first time in Python and web stuff
the first time in Python and web stuff like now we have a proper web
like now we have a proper web application and it's not just echoing
application and it's not just echoing back hello David hello Carter this could
back hello David hello Carter this could now work for any of you and it's
now work for any of you and it's currently served privately but if I made
currently served privately but if I made this URL public I could put this on the
this URL public I could put this on the web now and let anyone in the world
web now and let anyone in the world register but there's kind of some issues
register but there's kind of some issues here there's some security flaws
here there's some security flaws potentially and so for instance let me
potentially and so for instance let me go back to the web form here and let me
go back to the web form here and let me open up the inspect tab the developer
open up the inspect tab the developer tools and just remind you that anyone on
tools and just remind you that anyone on the internet not only you the developer
the internet not only you the developer but a an adversary can see all of your
but a an adversary can see all of your HTML see all of your CSS see all of your
HTML see all of your CSS see all of your JavaScript but more importantly because
JavaScript but more importantly because this is all client side in the browser
this is all client side in the browser there is literally nothing technically
there is literally nothing technically stopping them from changing the HTML or
stopping them from changing the HTML or at least their copy of it and I did that
at least their copy of it and I did that last week with Yale I changed their
last week with Yale I changed their website but no I changed my copy of
website but no I changed my copy of their website but when forms get
their website but when forms get involved you could maybe be actually
involved you could maybe be actually malicious now because even though this
malicious now because even though this drop-down menu only has basketball
drop-down menu only has basketball soccer and Ultimate Frisbee suppose I
soccer and Ultimate Frisbee suppose I really want to register for how about uh
really want to register for how about uh let's say uh name your favorite sport
let's say uh name your favorite sport volleyball we really want to register
volleyball we really want to register for volleyball but like this website
for volleyball but like this website won't let me well there's nothing
won't let me well there's nothing stopping me from going under the
stopping me from going under the elements tab in my browser going into
elements tab in my browser going into this select menu here and you know what
this select menu here and you know what no one ultimate for me let's change this
no one ultimate for me let's change this to volleyball and let's change this to
to volleyball and let's change this to volleyball enter I'm going to close the
volleyball enter I'm going to close the inspector now and as requested now we
inspector now and as requested now we support volleyball in the form now now
support volleyball in the form now now it's not changed on the server to be
it's not changed on the server to be fair but think about how HTTP works when
fair but think about how HTTP works when I fill out this with uh say let's see
I fill out this with uh say let's see Bernie's name Bernie really wants to
Bernie's name Bernie really wants to register for volleyball as well at the
register for volleyball as well at the moment my code is just going to trust
moment my code is just going to trust that what's in request. form is what was
that what's in request. form is what was in the original form itself no matter
in the original form itself no matter whether the human adversarially actually
whether the human adversarially actually changed it so if I actually submit this
changed it so if I actually submit this form and click register for Bernie and
form and click register for Bernie and volleyball even though that's not one of
volleyball even though that's not one of the supported available Sports if I now
the supported available Sports if I now go to registrant my website nonetheless
go to registrant my website nonetheless has trusted that uh Bernie and perhaps
has trusted that uh Bernie and perhaps you are registered for volleyball so
you are registered for volleyball so what's the implication of this this has
what's the implication of this this has surely happened in the past when like
surely happened in the past when like really poorly implemented websites um
really poorly implemented websites um allow you to uh specify the price of an
allow you to uh specify the price of an item for instance in your shopping cart
item for instance in your shopping cart and they just trust that when you click
and they just trust that when you click submit or add to cart it adds the price
submit or add to cart it adds the price to the backend server if you're not
to the backend server if you're not validating the price and making sure as
validating the price and making sure as with a database that wait a minute that
with a database that wait a minute that price is valid valid or wait a minute
price is valid valid or wait a minute those sports are valid like who knows
those sports are valid like who knows what people are going to do to your site
what people are going to do to your site and it's that simple to actually hack a
and it's that simple to actually hack a website accordingly now we can very
website accordingly now we can very easily fix this with some um some week
easily fix this with some um some week six style python we really just need to
six style python we really just need to do a bit of logic here and so let me
do a bit of logic here and so let me propose this let me go into app.py here
propose this let me go into app.py here and at the very top let me also create
and at the very top let me also create how about a uh Global variable called
how about a uh Global variable called Sports in all caps and I'm going to set
Sports in all caps and I'm going to set that equal to in square brackets the
that equal to in square brackets the list of Sports I actually want to
list of Sports I actually want to support so I'm going to put in
support so I'm going to put in basketball here I'm going to put in
basketball here I'm going to put in soccer here and I'm sorry no volleyball
soccer here and I'm sorry no volleyball officially I'm going to put in ultimate
officially I'm going to put in ultimate frisbee here so I've got this Global
frisbee here so I've got this Global list of supported Sports now think about
list of supported Sports now think about how I made this form a while ago I just
how I made this form a while ago I just hardcoded these Sports here well I don't
hardcoded these Sports here well I don't have to do that I can sort of draw upon
have to do that I can sort of draw upon my own official list of sports instead
my own official list of sports instead so let me scroll down to my
so let me scroll down to my index.html rendering template here let
index.html rendering template here let me say that the sports I want to support
me say that the sports I want to support are are these so just using the same
are are these so just using the same placeholder trick as before but I'm now
placeholder trick as before but I'm now telling the template what sports we
telling the template what sports we currently support now if I go back into
currently support now if I go back into index.html I don't have to manually do
index.html I don't have to manually do any of this let me get rid of all three
any of this let me get rid of all three of those options which I manually
of those options which I manually inputed earlier let me use my new trick
inputed earlier let me use my new trick with ginger syntax and say for sport in
with ginger syntax and say for sport in sports then let me proactively say end
sports then let me proactively say end for just to finish that thought and then
for just to finish that thought and then in here let me do option value equals in
in here let me do option value equals in curly BRAC is Sport and then so that the
curly BRAC is Sport and then so that the human also sees the same words I'm going
human also sees the same words I'm going to say sport out here so I've completely
to say sport out here so I've completely changed what was hardcoded manually
changed what was hardcoded manually typed to something now that's completely
typed to something now that's completely Dynamic so now it's not going to stop
Dynamic so now it's not going to stop someone adversar like me from changing
someone adversar like me from changing the HTML but watch this the behavior on
the HTML but watch this the behavior on the form if we go back is still now the
the form if we go back is still now the same drop down as before so
same drop down as before so aesthetically it looks the same but you
aesthetically it looks the same but you know what why don't we be clever now and
know what why don't we be clever now and let's go into app.py and the/ register
let's go into app.py and the/ register route and why don't we say this uh if
route and why don't we say this uh if how about sport not in sports then let's
how about sport not in sports then let's return render template uh failure. HTML
return render template uh failure. HTML now this template doesn't exist yet so
now this template doesn't exist yet so let me just quickly make this real fast
let me just quickly make this real fast I'm going to copy that code from before
I'm going to copy that code from before let me create a code file in uh failure.
let me create a code file in uh failure. HTML I'm just going to paste this here
HTML I'm just going to paste this here so I have a super simple error message
so I have a super simple error message and I'm going to say you are not
and I'm going to say you are not registered just to that's what we mean
registered just to that's what we mean by failure and now in app.py consider
by failure and now in app.py consider what logic I've added Sports in all caps
what logic I've added Sports in all caps on line 22 is that same Global list as
on line 22 is that same Global list as before by asking pythonic if sport not
before by asking pythonic if sport not in sports well then you hacked me like
in sports well then you hacked me like you tried to inject volleyball or some
you tried to inject volleyball or some other sport into request. form so I'm
other sport into request. form so I'm just going to say no failure not letting
just going to say no failure not letting you register and I can do this a little
you register and I can do this a little more uh verbosely too why don't I also
more uh verbosely too why don't I also say this if if not name so if the name
say this if if not name so if the name is blank let's similarly return a render
is blank let's similarly return a render template of failure. HTML in other words
template of failure. HTML in other words if you didn't give me a name you left it
if you didn't give me a name you left it blank that's not useful for me running
blank that's not useful for me running the sports program let's also consider
the sports program let's also consider that to be a failure so if I go back to
that to be a failure so if I go back to this tab now I'm going to reload just to
this tab now I'm going to reload just to make sure I have the latest client side
make sure I have the latest client side let me be lazy and just click register
let me be lazy and just click register enter you are not registered because I
enter you are not registered because I didn't give it an actual name all right
didn't give it an actual name all right well let's go back how about I now type
well let's go back how about I now type David but no no I'm not going to choose
David but no no I'm not going to choose a sport I just want register myself nope
a sport I just want register myself nope that did not work now let me go ahead
that did not work now let me go ahead here and choose soccer this I think does
here and choose soccer this I think does work let me go back now and try this
work let me go back now and try this hacker trick whereby I go into the drop-
hacker trick whereby I go into the drop- down menu I go into the select menu I
down menu I go into the select menu I change as before Ultimate Frisbee to
change as before Ultimate Frisbee to volleyball and I'll change this one here
volleyball and I'll change this one here to
to volleyball let me close the tab now this
volleyball let me close the tab now this looks like it's available now but when I
looks like it's available now but when I click register this this time it says
click register this this time it says you are not registered and this is much
you are not registered and this is much better than relying on other techniques
better than relying on other techniques you might see or have seen online with
you might see or have seen online with regard to HTML because there's also this
regard to HTML because there's also this trick let me go back to the screen here
trick let me go back to the screen here let me go back to index.html and you
let me go back to index.html and you might have seen online or you might
might have seen online or you might eventually see online that there's other
eventually see online that there's other attributes you can use like required you
attributes you can use like required you can literally tell the browser uhuh this
can literally tell the browser uhuh this field is required you cannot leave it
field is required you cannot leave it blank if I go back to the browser now
blank if I go back to the browser now reload and I again presume to be lazy
reload and I again presume to be lazy and I don't type in any name and click
and I don't type in any name and click register okay so that's kind of nice
register okay so that's kind of nice like now the browser is being a little
like now the browser is being a little more helpful for me saying no no no this
more helpful for me saying no no no this is required you have to fill this out
is required you have to fill this out but again if you know what you're doing
but again if you know what you're doing okay well I disagree with your requiring
okay well I disagree with your requiring a name of me let me go in here let me go
a name of me let me go in here let me go over to this tag let me delete the
over to this tag let me delete the required attribute and now I slip
required attribute and now I slip through but I didn't slip through on the
through but I didn't slip through on the server and so there's a difference here
server and so there's a difference here and an important distinction and so many
and an important distinction and so many people in the real world still screw
people in the real world still screw this up there's client side validation
this up there's client side validation like actually checking that the data is
like actually checking that the data is as you expect on the client side the
as you expect on the client side the browser and there server side validation
browser and there server side validation and even though client side validation
and even though client side validation like adding that required attribute
like adding that required attribute makes things more user friendly right
makes things more user friendly right like that was a pretty little popup it
like that was a pretty little popup it tells me that it's required it just
tells me that it's required it just looks better than the previous version
looks better than the previous version it is not trustable you cannot trust any
it is not trustable you cannot trust any input that ever comes from the user
input that ever comes from the user because clearly with like a an hour or
because clearly with like a an hour or so of cs50 like they can learn how to
so of cs50 like they can learn how to turn all of these defenses off so even
turn all of these defenses off so even if you like the user interface better
if you like the user interface better client side you have to have to have to
client side you have to have to have to do server side validation always users
do server side validation always users are not to be trusted and as soon as any
are not to be trusted and as soon as any app or website you make becomes popular
app or website you make becomes popular unfortunately then you have to deal with
unfortunately then you have to deal with all of the adversarial possibilities as
well oh good question could the adversary potentially access things
adversary potentially access things sensitive like app.py theoretically no
sensitive like app.py theoretically no like if flask itself is buggy then sure
like if flask itself is buggy then sure maybe if you're running some other
maybe if you're running some other software on your server on your laptop
software on your server on your laptop then sure uh it it's possible however if
then sure uh it it's possible however if your server is properly configured
your server is properly configured theoretically they should not be able to
theoretically they should not be able to get access to that with that said we'll
get access to that with that said we'll soon see or You might with your final
soon see or You might with your final project if you do something web based
project if you do something web based you're never going to want to write like
you're never going to want to write like usernames and passwords in your actual
usernames and passwords in your actual code you can put them in what are called
code you can put them in what are called environment variables so sort of in the
environment variables so sort of in the computer's memory but not in your code
computer's memory but not in your code just in case you or someone screws up
just in case you or someone screws up there are uh still ways to defend
there are uh still ways to defend against those kinds of possibilities
against those kinds of possibilities however slim
yeah a good question and this comes back to First principles just like in C and
to First principles just like in C and in python as soon as you return from a
in python as soon as you return from a function that's it nothing below that
function that's it nothing below that line of code executes and so to
line of code executes and so to summarize the question even though I'm
summarize the question even though I'm returning this failure. HTML template
returning this failure. HTML template how am I making sure we still don't
how am I making sure we still don't accidentally put volleyball in that
accidentally put volleyball in that Global dictionary it's because for
Global dictionary it's because for instance if you don't give me a name on
instance if you don't give me a name on line 22 at the moment I'm returning the
line 22 at the moment I'm returning the failure template and that's it lines 23
failure template and that's it lines 23 24 25 26 27 never execute in particular
24 25 26 27 never execute in particular 26 never executes and that's where I
26 never executes and that's where I would have been saving the name
would have been saving the name similarly if we do we get a invalid
similarly if we do we get a invalid sport that is either blank or not in the
sport that is either blank or not in the original authoritative list we return
original authoritative list we return failure. HTML is a template in line 25
failure. HTML is a template in line 25 we never get to line 26 so it just boils
we never get to line 26 so it just boils down to return and what that means in
down to return and what that means in Python 2 good question other questions
Python 2 good question other questions as
as well no all right how about we make one
well no all right how about we make one more enhancement here or so because the
more enhancement here or so because the problem with storing everything in this
problem with storing everything in this Global dictionary might be what we've
Global dictionary might be what we've got it all working finally but why is
got it all working finally but why is probably a global dictionary not the
probably a global dictionary not the place to store Frost's registration
place to store Frost's registration data what's the implication of
data what's the implication of this okay might slow the whole process
this okay might slow the whole process down but that actually Ram is actually
down but that actually Ram is actually good memory is actually generally a good
good memory is actually generally a good thing so not going to be a deal breaker
thing so not going to be a deal breaker here why might I not want to store that
here why might I not want to store that data though in that
variable you can perhaps infer how I fix something
something earlier yeah and
earlier yeah and back yeah it gets it's the memory gets
back yeah it gets it's the memory gets deleted garbage collected if you will as
deleted garbage collected if you will as soon as flask stops running so if you so
soon as flask stops running so if you so much as hit control C like you've just
much as hit control C like you've just lost all of your freshmen who registered
lost all of your freshmen who registered for the sport probably not a good thing
for the sport probably not a good thing I did this deliberately a moment ago and
I did this deliberately a moment ago and I hit control C because I did want to
I hit control C because I did want to clear the dictionary but trusting that
clear the dictionary but trusting that your server will never crash and your
your server will never crash and your code will always work and the power will
code will always work and the power will never go out like that's not the right
never go out like that's not the right way to build any kind of web application
way to build any kind of web application with persistent data so what we probably
with persistent data so what we probably want to do is reintroduce csvs and we've
want to do is reintroduce csvs and we've played with those in C and in Python
played with those in C and in Python could totally use csvs but we also now
could totally use csvs but we also now have p uh SQL at our disposal and let me
have p uh SQL at our disposal and let me propose that we do this in SQL in
propose that we do this in SQL in instead and for this let me go ahead and
instead and for this let me go ahead and open up a version of the program that I
open up a version of the program that I wrote in advance so let me go ahead and
wrote in advance so let me go ahead and close these templates which will look
close these templates which will look very similar but a little different from
very similar but a little different from the ones I wrote in advance and let me
the ones I wrote in advance and let me go ahead and open up uh in today's let
go ahead and open up uh in today's let me go into Source 9 let me go into Frost
me go into Source 9 let me go into Frost im's how about version four technically
im's how about version four technically in the versions online and let me go
in the versions online and let me go ahead and open up app.py as follows so
ahead and open up app.py as follows so here is an already made version that
here is an already made version that does just a little something different
does just a little something different at the very top I'm importing cs50 SQL
at the very top I'm importing cs50 SQL Library which you might recall we used a
Library which you might recall we used a couple of weeks past just to write
couple of weeks past just to write python that talks to a SQL database and
python that talks to a SQL database and this feels like an opportune moment to
this feels like an opportune moment to bring that idea back down here on line
bring that idea back down here on line eight I'm creating a DB variable that
eight I'm creating a DB variable that opens up a file called frost. DB using
opens up a file called frost. DB using syntax that we've seen before I did
syntax that we've seen before I did create this FRS DB file in advance of
create this FRS DB file in advance of class just so that we have a couple of
class just so that we have a couple of columns in which to store names and
columns in which to store names and sports and such here's that same Global
sports and such here's that same Global array a global list called Sports and
array a global list called Sports and let's just see what's going on down
let's just see what's going on down below if I scroll down to Index this is
below if I scroll down to Index this is the same as before as we wrote together
the same as before as we wrote together on the Fly let's skip deregister for a
on the Fly let's skip deregister for a moment and go now into register so this
moment and go now into register so this one's a little different but let's see
one's a little different but let's see what I've done I've got some comments in
what I've done I've got some comments in here because I wrote it in advance and I
here because I wrote it in advance and I think this logic is pretty much the same
think this logic is pretty much the same though I tightened it up and I'm asking
though I tightened it up and I'm asking two questions at once using on line 38
two questions at once using on line 38 the or keyword here just to say if
the or keyword here just to say if there's not a name or the sport is not
there's not a name or the sport is not in sports that is what we'll call now a
in sports that is what we'll call now a failure but what's fun now is that on
failure but what's fun now is that on line 42
line 42 I'm using the cs50 SQL library to
I'm using the cs50 SQL library to execute some actual SQL and I'm going to
execute some actual SQL and I'm going to insert into a table called registrant
insert into a table called registrant two columns name and Sport what names
two columns name and Sport what names and Sport well these two values with
and Sport well these two values with placeholders plugging in name and Sport
placeholders plugging in name and Sport notice I'm using the question marks
notice I'm using the question marks absolutely necessary so we don't get one
absolutely necessary so we don't get one of those SQL injection attacks because
of those SQL injection attacks because that too could be possible if someone
that too could be possible if someone typed in some dangerous words or
typed in some dangerous words or keywords like delete or single quotes or
keywords like delete or single quotes or semicolons in the form here I'm letting
semicolons in the form here I'm letting the library sanitize the data and then
the library sanitize the data and then this is a trick we haven't yet seen and
this is a trick we haven't yet seen and it's really going to start tying things
it's really going to start tying things together I can also use a redirect
together I can also use a redirect function in flask that has the effect of
function in flask that has the effect of doing the the if you will uh
doing the the if you will uh safetyschool.org trick again whereby
safetyschool.org trick again whereby after the user registers if I want to
after the user registers if I want to automatically show them now everyone who
automatically show them now everyone who is registered atreg I don't have to
is registered atreg I don't have to manually expect that they'll change the
manually expect that they'll change the url like I've been doing for the past
url like I've been doing for the past few minutes I can just redirect them
few minutes I can just redirect them anywhere I want on my app or heck I
anywhere I want on my app or heck I could redirect them to any URL on the
could redirect them to any URL on the internet using this function call and
internet using this function call and it's just a nice way to send them to a
it's just a nice way to send them to a different route if you want them to see
different route if you want them to see in this case those registrant so let me
in this case those registrant so let me do this in the same directory let me
do this in the same directory let me increase my terminal window size let me
increase my terminal window size let me do SQL light three of frost. DB and let
do SQL light three of frost. DB and let me type schema and you can indeed see it
me type schema and you can indeed see it wraps on to two lines here that each
wraps on to two lines here that each registrant has an ID which will be
registrant has an ID which will be automatically assigned one two three on
automatically assigned one two three on up a name which is not null text and a
up a name which is not null text and a sport which is also the same and the
sport which is also the same and the primary key is just going to be this
primary key is just going to be this unique identifier so that I made in
unique identifier so that I made in advance but if I do select star from
advance but if I do select star from registrant semicolon there's no one
registrant semicolon there's no one currently registered for any sports but
currently registered for any sports but let's try now running this let me go
let's try now running this let me go ahead and close my old version which we
ahead and close my old version which we wrote together and I'll close that tab
wrote together and I'll close that tab let me do flask run in this version four
let me do flask run in this version four here all right I'm going to see some
here all right I'm going to see some similar output I'm going to open the URL
similar output I'm going to open the URL now and you'll see that I made a couple
now and you'll see that I made a couple of Chang changes before instead of using
of Chang changes before instead of using a select menu I used what are called
a select menu I used what are called radio buttons now which is a reference
radio buttons now which is a reference to Old School radio buttons that were
to Old School radio buttons that were mutually exclusive in cars back in the
mutually exclusive in cars back in the day and we'll see how to do this but
day and we'll see how to do this but it's just an alternative to a select
it's just an alternative to a select menu and I'm going to go ahead and type
menu and I'm going to go ahead and type in my name again here so I'll do David
in my name again here so I'll do David I'll do soccer by selecting this radio
I'll do soccer by selecting this radio button and I'm going to click register
button and I'm going to click register now and notice what happened it's a
now and notice what happened it's a little ugly the formatting but so again
little ugly the formatting but so again was this 20 years ago here I have now
was this 20 years ago here I have now now at the slash registrant route
now at the slash registrant route instead of an unordered list I'm just
instead of an unordered list I'm just using a simple HTML table so I'll show
using a simple HTML table so I'll show you what this looks like in just a
you what this looks like in just a moment too and I'll show you this
moment too and I'll show you this deregister button which is sort of
deregister button which is sort of unnecessarily large I also have
unnecessarily large I also have functionality we'll soon see for how you
functionality we'll soon see for how you can unregister someone from a sport as
can unregister someone from a sport as well so take your name out of contention
well so take your name out of contention well let me go back to my terminal
well let me go back to my terminal window here and I'm going to click the
window here and I'm going to click the plus to give myself s a second terminal
plus to give myself s a second terminal so I can go back into Source 9 Frost IMS
so I can go back into Source 9 Frost IMS 4 I'm going to do SQL light of frost. DB
4 I'm going to do SQL light of frost. DB I'm going to do select star from
I'm going to do select star from registrant now and now you'll see that
registrant now and now you'll see that indeed there's David registered for
indeed there's David registered for soccer and in fact if I quit the flask
soccer and in fact if I quit the flask program with control C and rerun it
program with control C and rerun it again no big deal because that next
again no big deal because that next version of flask will just use the
version of flask will just use the database as well so I'm persisting
database as well so I'm persisting keeping the data in SQL light whereas
keeping the data in SQL light whereas I'm actually grabbing it using my python
I'm actually grabbing it using my python code in flask all right let's put one
code in flask all right let's put one more person in here so we can delete one
more person in here so we can delete one of us too Carter for basketball register
of us too Carter for basketball register and now we see both of us here all right
and now we see both of us here all right so let's see how we did this let's go
so let's see how we did this let's go back over to VSS code let me shrink down
back over to VSS code let me shrink down my terminal window let me go into the
my terminal window let me go into the actually let's go into the templates
actually let's go into the templates directory and let's look at for instance
directory and let's look at for instance index.html so previously we were using a
index.html so previously we were using a select menu turns out radio buttons use
select menu turns out radio buttons use the input tag but instead of having
the input tag but instead of having input of uh type equals text like for
input of uh type equals text like for the human's name you have type equals
the human's name you have type equals radio and so long as each of your radio
radio and so long as each of your radio buttons has the same name the same name
buttons has the same name the same name the same name that's what makes them
the same name that's what makes them mutually exclusive so checking one radio
mutually exclusive so checking one radio button turns off the others because they
button turns off the others because they have the same name the value I want to
have the same name the value I want to assign to each of these radio buttons is
assign to each of these radio buttons is just the sport placeholder this is what
just the sport placeholder this is what the human sees on the screen so it's
the human sees on the screen so it's almost the same as the select menu it
almost the same as the select menu it just looks aesthetically different but
just looks aesthetically different but there's my same button so that's all the
there's my same button so that's all the difference I made there and I added a
difference I made there and I added a heading tag H1 just to say register to
heading tag H1 just to say register to make clear what it is but let's take a
make clear what it is but let's take a look at another file uh this one now
look at another file uh this one now being how about the SL registrant route
being how about the SL registrant route so if I open up registr trans. HTML here
so if I open up registr trans. HTML here now it's way more verbose than my
now it's way more verbose than my unordered list but this is just kind of
unordered list but this is just kind of boring HTML here's my table tag table
boring HTML here's my table tag table head table row table heading this makes
head table row table heading this makes things bold as the first row of the
things bold as the first row of the table name sport are my two columns I've
table name sport are my two columns I've got a third empty column just so I can
got a third empty column just so I can fit that button as we'll soon see again
fit that button as we'll soon see again T body for table body here's the same
T body for table body here's the same for Loop trick again so that I can
for Loop trick again so that I can output for every registrant a whole
output for every registrant a whole table row and there's this weird form in
table row and there's this weird form in there but we'll come back to that but
there but we'll come back to that but there's the registrant's name there's
there's the registrant's name there's the registrant sport but notice the
the registrant sport but notice the slightly different syntax here recall
slightly different syntax here recall that cs50's select uh cs50's execute
that cs50's select uh cs50's execute function when it returns to you a list
function when it returns to you a list of dictionaries you can then get at the
of dictionaries you can then get at the individual columns by way of those keys
individual columns by way of those keys so let's go to the/ registrant Route let
so let's go to the/ registrant Route let me go back to app.py scroll down here
me go back to app.py scroll down here and it's actually super simple here I
and it's actually super simple here I have a SL registr route that first
have a SL registr route that first executes select star from registrant so
executes select star from registrant so just old SQL stuff give me everyone from
just old SQL stuff give me everyone from the registr table let me then render the
the registr table let me then render the template called registr trans. HTML and
template called registr trans. HTML and just pass in this list of dictionaries
just pass in this list of dictionaries and we haven't quite done this yet but
and we haven't quite done this yet but if you go back to register. HTML how do
if you go back to register. HTML how do you iterate over each dictionary in that
you iterate over each dictionary in that list well the syntax is just for
list well the syntax is just for registrant in registrants that makes
registrant in registrants that makes this a dictionary one at a time in the
this a dictionary one at a time in the list just like in Python so registrant
list just like in Python so registrant name and registrants sport is just
name and registrants sport is just another Syntax for using the square
another Syntax for using the square bracket notation it's just a little
bracket notation it's just a little cleaner and slightly more succinct than
cleaner and slightly more succinct than having quotes and square brackets
having quotes and square brackets everywhere and then the rest of this is
everywhere and then the rest of this is just
just HTML so what happens now if I want to uh
HTML so what happens now if I want to uh like Carter has been cut from the
like Carter has been cut from the basketball team if you will so how do we
basketball team if you will so how do we do that well we want to click this
do that well we want to click this button deregister next to Carter's name
button deregister next to Carter's name but how does this work and think about
but how does this work and think about now any website you visited that has
now any website you visited that has something like a shopping cart uh where
something like a shopping cart uh where you can remove things from your cart or
you can remove things from your cart or update quantities or add more quantities
update quantities or add more quantities to your shopping cart on Amazon or
to your shopping cart on Amazon or anything else well let's actually look
anything else well let's actually look at the HTML that my app has spit out
at the HTML that my app has spit out let's actually look at this here and
let's actually look at this here and we'll see the following we'll see that
we'll see the following we'll see that we have here in the HTML that reached
we have here in the HTML that reached the user not only is David in the first
the user not only is David in the first column socer in the second notice that
column socer in the second notice that my register. HTML form is also spitting
my register. HTML form is also spitting out a tiny little web form of its own
out a tiny little web form of its own it's ugly but I only care about its
it's ugly but I only care about its functionality for now and notice what
functionality for now and notice what I'm doing here every registrant in this
I'm doing here every registrant in this database gets their very own deregister
database gets their very own deregister button and that form has a button that
button and that form has a button that says deregister but notice what else
says deregister but notice what else each of those forms have there's no text
each of those forms have there's no text box there's no drop down menu there's no
box there's no drop down menu there's no radio buttons rather you have a hidden
radio buttons rather you have a hidden input field here so there is a way with
input field here so there is a way with HTML to have a form that will submit
HTML to have a form that will submit information but you don't have to give
information but you don't have to give the user the ability to change that
the user the ability to change that information you can just go ahead and
information you can just go ahead and tuck it inside of the form invisibly if
tuck it inside of the form invisibly if you will hidden in fashion and so what's
you will hidden in fashion and so what's going to happen is if I click the D
going to happen is if I click the D register button next to Carter his
register button next to Carter his primary key is two
primary key is two mine is instead one so what's going to
mine is instead one so what's going to happen if I click his deregister button
happen if I click his deregister button it submits a form with a
it submits a form with a ID parameter whose value is two and it
ID parameter whose value is two and it submits it to the deregister route so
submits it to the deregister route so what do that mean well if I go to VSS
what do that mean well if I go to VSS code and I go to app.py let's look at
code and I go to app.py let's look at the deregister route that I skipped over
the deregister route that I skipped over so if you access the deregister route
so if you access the deregister route via post this code gets called I grab
via post this code gets called I grab from request form the ID that was
from request form the ID that was submitted in Hidden fashion if there's
submitted in Hidden fashion if there's indeed an ID that is it's not blank it's
indeed an ID that is it's not blank it's not zero it's an actual number like one
not zero it's an actual number like one 2 three or more I execute delete from
2 three or more I execute delete from registrant where ID equals that value
registrant where ID equals that value with a question mark Place holder and
with a question mark Place holder and then I redirect the user back to
then I redirect the user back to registrant now if I go back to this form
registrant now if I go back to this form here I click deregister we'll see that
here I click deregister we'll see that in action gone is now Carter and in fact
in action gone is now Carter and in fact if I go back to my terminal window here
if I go back to my terminal window here I open open up uh SQL light 3 of frost.
I open open up uh SQL light 3 of frost. DB and rerun select star from registrant
DB and rerun select star from registrant Carter is now gone so again using very
Carter is now gone so again using very simple HTML forms you can get buttons
simple HTML forms you can get buttons and links and other such UI mechanisms
and links and other such UI mechanisms to like do things on the server that you
to like do things on the server that you want but there is a danger here this now
want but there is a danger here this now is really meant this example is like an
is really meant this example is like an administrative website like it was some
administrative website like it was some 20 years ago just for us internal staff
20 years ago just for us internal staff to be doing things technically this is
to be doing things technically this is dangerous what I've just done
dangerous what I've just done two even though Carter's ID is two and
two even though Carter's ID is two and hidden and mine is one in Hidden what
hidden and mine is one in Hidden what could this allow an adversary to do if
could this allow an adversary to do if they had admin access to the same
site any thoughts
thoughts yeah
yeah yeah yeah they could change the value of
yeah yeah they could change the value of that hidden attri by opening up chrom's
that hidden attri by opening up chrom's like developer tools change the number
like developer tools change the number in the HTML they could delete anyone
in the HTML they could delete anyone deregister anyone want from the database
deregister anyone want from the database now in this case I claim this is fine
now in this case I claim this is fine because this is only meant for us staff
because this is only meant for us staff who were running Sports back in the day
who were running Sports back in the day but it's indeed a risk so wouldn't it be
but it's indeed a risk so wouldn't it be nice if we could actually ensure that
nice if we could actually ensure that only those users who are authorized are
only those users who are authorized are allowed to execute certain actions I
allowed to execute certain actions I think for this capability we're actually
think for this capability we're actually going to need to introduce something a
going to need to introduce something a bit more and so here of course is an
bit more and so here of course is an opportunity to talk briefly about really
opportunity to talk briefly about really what you and I do all day long every day
what you and I do all day long every day we log into one or more websites or apps
we log into one or more websites or apps or at least until uh you're logged out
or at least until uh you're logged out automatically and you have to do it
automatically and you have to do it again so here for instance is a
again so here for instance is a screenshot of Gmail when you type in
screenshot of Gmail when you type in your username you type in your password
your username you type in your password maybe your to factor code that gets
maybe your to factor code that gets texted or sent to your phone then you're
texted or sent to your phone then you're logged in and thankfully you're not
logged in and thankfully you're not prompted to log in again typically for
prompted to log in again typically for number of hours or days or weeks
number of hours or days or weeks depending on the website like Gmail
depending on the website like Gmail keeps you logged in for ages your bank
keeps you logged in for ages your bank probably logs you out within an hour or
probably logs you out within an hour or so for safety sake so that is completely
so for safety sake so that is completely configurable on the server but how does
configurable on the server but how does g Emil know how does Google know that
g Emil know how does Google know that even as you're checking different mails
even as you're checking different mails again and again and again how do they
again and again and again how do they know that you're still the same person
know that you're still the same person who logged in well it turns out that
who logged in well it turns out that using these same building blocks as
using these same building blocks as today HTTP and HTML and more you can
today HTTP and HTML and more you can actually implement the notion of a login
actually implement the notion of a login feature by doing the equivalent of
feature by doing the equivalent of something with something called cookies
something with something called cookies essentially what happens when you first
essentially what happens when you first log into a website for the very first
log into a website for the very first time successfully with your username and
time successfully with your username and password a cookie so to speak is planted
password a cookie so to speak is planted on your computer and metaphorically this
on your computer and metaphorically this is kind of like taking a hand stamp and
is kind of like taking a hand stamp and your hand is now stamped in this case a
your hand is now stamped in this case a smiley face so that every other time you
smiley face so that every other time you click on a link on that same website
click on a link on that same website google.com gmail.com whatever
google.com gmail.com whatever unbeknownst to you your browser is
unbeknownst to you your browser is constantly presenting that handstamp
constantly presenting that handstamp just like going into a club or an
just like going into a club or an amusement park or something like that
amusement park or something like that where they don't want to check your ID
where they don't want to check your ID again they don't want to check your
again they don't want to check your ticket again they just want to quickly
ticket again they just want to quickly see the same hand stamp and so that is
see the same hand stamp and so that is one of the things that a browser is
one of the things that a browser is always doing for you once you're logged
always doing for you once you're logged in any cookies that have been planted so
in any cookies that have been planted so to speak in your Mac or PC or phone are
to speak in your Mac or PC or phone are constantly represented to the site every
constantly represented to the site every time you click a link or make another
time you click a link or make another request to that website and mechanically
request to that website and mechanically how this works not just metaphorically
how this works not just metaphorically and ink essentially this is what happens
and ink essentially this is what happens here is a example of an HTTP request to
here is a example of an HTTP request to something like Gmail and suppose for
something like Gmail and suppose for instance that you've logged in typically
instance that you've logged in typically as of last week we said that coming back
as of last week we said that coming back from the server would be another virtual
from the server would be another virtual envelope containing like a 200 okay
envelope containing like a 200 okay message and then like the actual web
message and then like the actual web page or the picture of a cat or whatever
page or the picture of a cat or whatever it may be but Google can also if they
it may be but Google can also if they verified that you have some username and
verified that you have some username and password correctly inputed they can do
password correctly inputed they can do the equivalent of stamping your hand and
the equivalent of stamping your hand and the way they do this is they send an
the way they do this is they send an additional line of text in that virtual
additional line of text in that virtual envelope from the server to your browser
envelope from the server to your browser literally using another HTTP header not
literally using another HTTP header not content type which just mundanely tells
content type which just mundanely tells you what kind of content has come back
you what kind of content has come back they literally send an http header
they literally send an http header called set-cookie and then they set a
called set-cookie and then they set a key value pair on your Mac or PC this is
key value pair on your Mac or PC this is the technical equivalent of this smiley
the technical equivalent of this smiley face handstamp and what your computer is
face handstamp and what your computer is designed to do because your computer and
designed to do because your computer and intern browser are supposed to implement
intern browser are supposed to implement HTTP anytime you click another link on
HTTP anytime you click another link on Gmail or click on another mail or the
Gmail or click on another mail or the like your browser unbeknownst to you
like your browser unbeknownst to you presents that handstamp and how it does
presents that handstamp and how it does it technically is in the envelope it
it technically is in the envelope it sends to Google from your browser it
sends to Google from your browser it doesn't send set cookie it just sends
doesn't send set cookie it just sends cookie colon and the exact same thing
cookie colon and the exact same thing and so long as Google is smart and they
and so long as Google is smart and they have a database or something of all of
have a database or something of all of the session Valu session is the
the session Valu session is the technical term for this maintenance of
technical term for this maintenance of information across HTTP so long as
information across HTTP so long as Google has a big database that knows
Google has a big database that knows that my cookie value is one 123 and your
that my cookie value is one 123 and your cookie value is 456 they can infer from
cookie value is 456 they can infer from this virtual handstamp whose emails they
this virtual handstamp whose emails they should be showing mine or yours or the
should be showing mine or yours or the like so this is just scratching the
like so this is just scratching the surface but if I really wanted to enable
surface but if I really wanted to enable only Carter to deregister himself I just
only Carter to deregister himself I just have to make sure that I log him in
have to make sure that I log him in somehow username password and all of
somehow username password and all of that I stamp his hand or really put a
that I stamp his hand or really put a cookie on his computer and only if his
cookie on his computer and only if his cookie lines up with the user ID he's
cookie lines up with the user ID he's trying to deregister should he be
trying to deregister should he be allowed to in fact do so so all of this
allowed to in fact do so so all of this is quite possible and indeed the
is quite possible and indeed the technical term for this is session and
technical term for this is session and what we thought we'd do in our remaining
what we thought we'd do in our remaining time Today show you some examples of
time Today show you some examples of exactly how some of the most familiar
exactly how some of the most familiar web functionality is implemented today
web functionality is implemented today some of which you'll use in your own uh
some of which you'll use in your own uh problem set 9 uh which itself will be a
problem set 9 uh which itself will be a web app or perhaps even your final
web app or perhaps even your final project so let me go ahead and do this
project so let me go ahead and do this let me close my previous tabs and all
let me close my previous tabs and all things for ashs and let's move on to
things for ashs and let's move on to implementing some notion of login so in
implementing some notion of login so in just a moment I'll switch over here to
just a moment I'll switch over here to VSS code and what I'm going to do is
VSS code and what I'm going to do is indeed in my source 9 directory I'm
indeed in my source 9 directory I'm going to go into a login directory and
going to go into a login directory and if I type LS here you'll see app.py
if I type LS here you'll see app.py requirements. text which just refers to
requirements. text which just refers to libraries I want to automatically
libraries I want to automatically install and a templates folder as well
install and a templates folder as well I'm going to go ahead and stop the
I'm going to go ahead and stop the previous server and close that terminal
previous server and close that terminal window and I'm going to open up this
window and I'm going to open up this version of
version of app.py so there's a few new lines here
app.py so there's a few new lines here and we'll give you these lines for
and we'll give you these lines for problems at nine but I've got some of
problems at nine but I've got some of the familiar stuff up here including
the familiar stuff up here including this new redirect function we just used
this new redirect function we just used and I have a session variable that comes
and I have a session variable that comes with flask 2 so what's nice again about
with flask 2 so what's nice again about flask is that it deals with all of this
flask is that it deals with all of this cookie stuff for you it sets the cookie
cookie stuff for you it sets the cookie it checks the cookie and what flask does
it checks the cookie and what flask does for you is it gives you the abstraction
for you is it gives you the abstraction of a variable called session so that
of a variable called session so that anything you put in the session variable
anything you put in the session variable which itself is a dictionary will be
which itself is a dictionary will be there again and again and again whenever
there again and again and again whenever that same user comes back a session is
that same user comes back a session is how you implement essentially like the
how you implement essentially like the proverbial shopping cart uh if I'm
proverbial shopping cart uh if I'm logged into Amazon you're logged into
logged into Amazon you're logged into Amazon Amazon knows which of us is which
Amazon Amazon knows which of us is which by way of that cookie and Amazon if
by way of that cookie and Amazon if they're using flask provides the
they're using flask provides the programmer with a dictionary called
programmer with a dictionary called session and flasks make sure that when
session and flasks make sure that when Carter is visiting the site the code
Carter is visiting the site the code uses his session object when I'm
uses his session object when I'm visiting the site it uses my session
visiting the site it uses my session object but it's all implemented with
object but it's all implemented with those same cookies this is the same as
those same cookies this is the same as before these lines are new and you'll
before these lines are new and you'll see these in problem Set n this is how
see these in problem Set n this is how we enable sessions in a web application
we enable sessions in a web application and I'll just wave my hands at the
and I'll just wave my hands at the detail there's different ways to
detail there's different ways to implement sessions whether you use
implement sessions whether you use cookies on the server cookies on the
cookies on the server cookies on the browser or other things these just
browser or other things these just ensure that we're storing the the
ensure that we're storing the the session information the shopping cart on
session information the shopping cart on the server itself now down here let's go
the server itself now down here let's go ahead and do this let's go ahead and run
ahead and do this let's go ahead and run flask run so I can see what this app
flask run so I can see what this app does if I do this and visit the URL that
does if I do this and visit the URL that gets outputed you'll see a very simple
gets outputed you'll see a very simple web page here and if I type in for
web page here and if I type in for instance my name I'm not going to bother
instance my name I'm not going to bother with a password and click log in you'll
with a password and click log in you'll see that you are logged in as David and
see that you are logged in as David and now I can log out so I'm going to go
now I can log out so I'm going to go ahead and click log out and now it seems
ahead and click log out and now it seems to know that I'm not logged in again I
to know that I'm not logged in again I can log in as Carter because I didn't
can log in as Carter because I didn't bother implementing passwords for
bother implementing passwords for Simplicity but now the site knows I'm
Simplicity but now the site knows I'm logged in as Carter better yet if I
logged in as Carter better yet if I reload reload reload or click this
reload reload reload or click this button again and again notice it still
button again and again notice it still knows that I'm Carter uh until such time
knows that I'm Carter uh until such time as I log out all right well how is this
as I log out all right well how is this working well let's go back to vs code
working well let's go back to vs code here and let me scroll down to first
here and let me scroll down to first this route this is a very common
this route this is a very common Paradigm here whereby I'm checking for
Paradigm here whereby I'm checking for the index route if there is not a name
the index route if there is not a name in the session redirect the user to SL
in the session redirect the user to SL login now what does that mean well let
login now what does that mean well let me go back to vs code here let me go to
me go back to vs code here let me go to the slash route so again your url will
the slash route so again your url will be different but I'm just going to go to
be different but I'm just going to go to slash and hit enter notice that I got
slash and hit enter notice that I got automatically redirected to login and so
automatically redirected to login and so many websites do this if you go to a
many websites do this if you go to a website and you're not logged in you're
website and you're not logged in you're very often redirected to SL login or SL
very often redirected to SL login or SL account or something like that where
account or something like that where you're prompted the code for doing that
you're prompted the code for doing that is right here if there's no name in the
is right here if there's no name in the session if there's no name in the
session if there's no name in the shopping cart if you will go ahead and
shopping cart if you will go ahead and return the user to login that route
return the user to login that route otherwise implicitly if they are logged
otherwise implicitly if they are logged in show them index.html so let's go down
in show them index.html so let's go down that rabbit hole let me open up in VSS
that rabbit hole let me open up in VSS code a second terminal window let me go
code a second terminal window let me go into Source 9 and go into this same
into Source 9 and go into this same login demonstration and let me open up
login demonstration and let me open up the template called
the template called index.html and here's all this is
index.html and here's all this is there's some layout but who cares at
there's some layout but who cares at this point it's just the boiler plate
this point it's just the boiler plate generic HTML here's the body block I
generic HTML here's the body block I have this and this two is ginger because
have this and this two is ginger because of the curly brace and the percent sign
of the curly brace and the percent sign if there's a name in the session this is
if there's a name in the session this is just python syntax then say this
just python syntax then say this sentence you are logged in as whatever
sentence you are logged in as whatever name is in the session in the shopping
name is in the session in the shopping cart if you will and then I just have
cart if you will and then I just have this HTML link for logging the user out
this HTML link for logging the user out else if there is no name in the session
else if there is no name in the session logically just say you are not logged in
logically just say you are not logged in and give them a manual link for logging
and give them a manual link for logging in instead so that's all this particular
in instead so that's all this particular template does but how does the slash
template does but how does the slash login work well let's go into this other
login work well let's go into this other template code of login. HTML to which
template code of login. HTML to which I'm redirected super simple this is just
I'm redirected super simple this is just copy paste from HTML before I've got a
copy paste from HTML before I've got a login form that's going to have an
login form that's going to have an action of Slash login submits for
action of Slash login submits for privacy sake just via post and then the
privacy sake just via post and then the rest of this is just a simple form and
rest of this is just a simple form and I'm using using an input type equals
I'm using using an input type equals submit instead of button equals type
submit instead of button equals type equals submit but same idea here too and
equals submit but same idea here too and if I go back to app.py well let's see
if I go back to app.py well let's see how login works all right it's a lot all
how login works all right it's a lot all at once but they're relatively simple
at once but they're relatively simple reapplications of the same idea so if
reapplications of the same idea so if the user visits SL login via get or post
the user visits SL login via get or post call this function login if the user has
call this function login if the user has submitted via post and we saw this
submitted via post and we saw this technique before go ahead and do this on
technique before go ahead and do this on line 23 store in this special session
line 23 store in this special session variable that comes with flask a name
variable that comes with flask a name key and store in it the user's own name
key and store in it the user's own name so quote unquote name will have a value
so quote unquote name will have a value of David or Carter or the like and as
of David or Carter or the like and as soon as you do that redirect the user
soon as you do that redirect the user back to slash just so they see the
back to slash just so they see the homepage again and this is how Amazon
homepage again and this is how Amazon and all these other websites work too
and all these other websites work too otherwise if they visit this page
otherwise if they visit this page implicitly via get and even though I
implicitly via get and even though I didn't say equals equals get anywhere
didn't say equals equals get anywhere that's sort of the implication because
that's sort of the implication because if you can only get here via get or post
if you can only get here via get or post and we already handled post logically
and we already handled post logically All That Remains is get well then just
All That Remains is get well then just show them the login screen instead but
show them the login screen instead but there's half a dozen ways we could
there's half a dozen ways we could express that same logic and then for log
express that same logic and then for log out this is kind of straightforward if
out this is kind of straightforward if the user clicks that log out link and
the user clicks that log out link and ends up at SL logout this route well
ends up at SL logout this route well just change the value of that key in the
just change the value of that key in the session to be none effectively no Carter
session to be none effectively no Carter is gone David's gone there's no one
is gone David's gone there's no one logged in so that is all that's required
logged in so that is all that's required to actually implement the notion of
to actually implement the notion of logging in and logging out of website
logging in and logging out of website plus the password thing which should
plus the password thing which should probably involve a database but one
probably involve a database but one thing at a time and really session is
thing at a time and really session is sort of like the code version of a
sort of like the code version of a shopping cart whereby if I visit the
shopping cart whereby if I visit the same code I get my own session object if
same code I get my own session object if Carter visits the website he gets his
Carter visits the website he gets his own session object and the way flask
own session object and the way flask Keeps Us straight is they put one cookie
Keeps Us straight is they put one cookie on my computer a different cookie on his
on my computer a different cookie on his computer and uses those to line up with
computer and uses those to line up with making sure the right session gets shown
making sure the right session gets shown to the right actual
to the right actual user questions on this notion of
sessions no all right how about a couple final examples just to tie this all
final examples just to tie this all together let me go back into vs code
together let me go back into vs code here let me quit my previous version of
here let me quit my previous version of flask let's go into Source 9 and go into
flask let's go into Source 9 and go into store which is a separate app Al
store which is a separate app Al together and let's start by just running
together and let's start by just running flask to see what it does let's hover
flask to see what it does let's hover over the URL and open it in another Tab
over the URL and open it in another Tab and this is pretty ugly too let me zoom
and this is pretty ugly too let me zoom in but it's a very simple bookstore like
in but it's a very simple bookstore like an early amazon.com for each of these
an early amazon.com for each of these seven books here Each of which seems to
seven books here Each of which seems to be like maybe this is H1 this is H2 H2
be like maybe this is H1 this is H2 H2 H2 H2 and then there's a button
H2 H2 and then there's a button underneath each well now let's use this
underneath each well now let's use this as an opportunity to kind of infer like
as an opportunity to kind of infer like for any website how this thing works let
for any website how this thing works let me go ahead and do view page source and
me go ahead and do view page source and you can do this for any website on the
you can do this for any website on the internet let's try to figure out how
internet let's try to figure out how this bookstore adds things to a cart
this bookstore adds things to a cart well here's the H1 tag uninteresting H2
well here's the H1 tag uninteresting H2 H2 H2 so the juicy part is in these
H2 H2 so the juicy part is in these forms each of these forms has an action
forms each of these forms has an action of SL cart so that's the route that's
of SL cart so that's the route that's going to be interesting in a moment and
going to be interesting in a moment and it uses post for privacy sake each of
it uses post for privacy sake each of these forms like the deregister feature
these forms like the deregister feature for Carter has an ID attribute an ID
for Carter has an ID attribute an ID parameter that's hidden visually that
parameter that's hidden visually that has a value of one or two or three so
has a value of one or two or three so like the unique uh uh like barcodes for
like the unique uh uh like barcodes for the books if you will but super small
the books if you will but super small numbers in our case and then each of
numbers in our case and then each of these other forms just had each of these
these other forms just had each of these other books has an identical form except
other books has an identical form except for the value of this year now in this
for the value of this year now in this case this isn't such a big deal that a
case this isn't such a big deal that a user could technically hack the HTML of
user could technically hack the HTML of this bookstore amazon.com and change the
this bookstore amazon.com and change the IDS because whoa what's the worst
IDS because whoa what's the worst they're going to do like buy more books
they're going to do like buy more books by adding more IDs to their shopping
by adding more IDs to their shopping cart like that's not a problem there's
cart like that's not a problem there's no prices here it's just the unique
no prices here it's just the unique ideas of books so whereas the deregister
ideas of books so whereas the deregister was maybe worrisome because you're
was maybe worrisome because you're changing the server I think it's okay
changing the server I think it's okay because the user can only at worst buy
because the user can only at worst buy more books than then uh they might via
more books than then uh they might via the buttons alone so how does this now
the buttons alone so how does this now work well let's go into vs code again
work well let's go into vs code again here let me give myself another terminal
here let me give myself another terminal window and in Source 9/ store let me
window and in Source 9/ store let me open up app.py which is where all the
open up app.py which is where all the logic is so I'll flip through most of
logic is so I'll flip through most of this quickly because we've seen this
this quickly because we've seen this before these Imports are pretty much the
before these Imports are pretty much the same as before this line is the same
same as before this line is the same this line is almost the same but the
this line is almost the same but the database now is called store. DB instead
database now is called store. DB instead of frost. DB this is the boilerplate
of frost. DB this is the boilerplate code for just enabling sessions this
code for just enabling sessions this notion of a shopping cart and so let's
notion of a shopping cart and so let's see how the index Works how is that I'm
see how the index Works how is that I'm seeing all seven books at once well in
seeing all seven books at once well in this index function I'm using on line 19
this index function I'm using on line 19 select star from books to get all the
select star from books to get all the books from the database and then I'm
books from the database and then I'm rendering a template called books. HTML
rendering a template called books. HTML passing in as a placeholder all of those
passing in as a placeholder all of those books all right let's go down that
books all right let's go down that rabbit hole for a second let me open up
rabbit hole for a second let me open up books. HTML in my templates directory
books. HTML in my templates directory and here again even though it's you know
and here again even though it's you know new today it's probably increasingly
new today it's probably increasingly familiar syntactically here's the H1
familiar syntactically here's the H1 here is my Loop here's the H2 which is
here is my Loop here's the H2 which is going to Output the current book's title
going to Output the current book's title in that Loop here's the form here's the
in that Loop here's the form here's the ID of that book that's going to be
ID of that book that's going to be outputed in the form so I didn't
outputed in the form so I didn't manually type out seven long forms I
manually type out seven long forms I just did one in this template so that it
just did one in this template so that it gets generated automatically literally
gets generated automatically literally what a website CL Amazon would do to
what a website CL Amazon would do to show you 10 books at a time or 10 search
show you 10 books at a time or 10 search results or more at a time all right well
results or more at a time all right well let's go to SLC cart which was the juicy
let's go to SLC cart which was the juicy one and this one's longer but let's see
one and this one's longer but let's see if we can reason through it if the user
if we can reason through it if the user submits via get or post to/ cart well we
submits via get or post to/ cart well we first do this and this is just necessary
first do this and this is just necessary in some boilerplate rewrote we're going
in some boilerplate rewrote we're going to use the session object to store a
to use the session object to store a variable called cart that in this case
variable called cart that in this case is going to be a list so session again
is going to be a list so session again is just a dictionary you can put
is just a dictionary you can put anything in it you want previously we
anything in it you want previously we put students names and sports now uh
put students names and sports now uh what I want to do is actually sorry prev
what I want to do is actually sorry prev previously we put uh the student name
previously we put uh the student name the user's name in it now I'm going to
the user's name in it now I'm going to actually store a cart key whose value is
actually store a cart key whose value is a list why because I want to aggregate
a list why because I want to aggregate more and more books in this list all
more and more books in this list all right so that just makes sure that I
right so that just makes sure that I have at least an empty shopping cart the
have at least an empty shopping cart the very first time the user does this if
very first time the user does this if they visit this form via post let's go
they visit this form via post let's go ahead and get the ID of the book that
ahead and get the ID of the book that they posted if it is not empty if there
they posted if it is not empty if there is a number like one or two or three
is a number like one or two or three let's go ahead and go into the shopping
let's go ahead and go into the shopping C cart which is a list per this line and
C cart which is a list per this line and just append that ID so this list of
just append that ID so this list of books in your shopping cart is going to
books in your shopping cart is going to contain like one comma 2 comma four
contain like one comma 2 comma four comma six whatever books you're actually
comma six whatever books you're actually buying and then the user gets redirected
buying and then the user gets redirected to cart what if though the user got here
to cart what if though the user got here via get and not post well this one's
via get and not post well this one's relatively straightforward if you just
relatively straightforward if you just visit slart we select star from books
visit slart we select star from books where ID is in okay so this is
where ID is in okay so this is interesting this list and and this is a
interesting this list and and this is a syntax you might not have seen before
syntax you might not have seen before but if you read the documentation for
but if you read the documentation for cs50's Library if you select something
cs50's Library if you select something and use a question mark placeholder and
and use a question mark placeholder and the placeholder itself is a list we
the placeholder itself is a list we output a comma separated list of values
output a comma separated list of values just like you would use maybe in problem
just like you would use maybe in problem Set uh seven for doing SQL queries on
Set uh seven for doing SQL queries on your own so this just means show me only
your own so this just means show me only those books in my shopping cart not
those books in my shopping cart not Carters not someone else's not in the
Carters not someone else's not in the whole database only show me the books in
whole database only show me the books in my shopping cart and then
my shopping cart and then render it as such so we only saw what
render it as such so we only saw what the catalog here looks like at slash
the catalog here looks like at slash books Let's go ahead and in slash let's
books Let's go ahead and in slash let's go ahead and add maybe the first book to
go ahead and add maybe the first book to my cart and now I see at SLC cart only
my cart and now I see at SLC cart only that first book whose ID is one let me
that first book whose ID is one let me now go back to the bookstore here scroll
now go back to the bookstore here scroll down to maybe the seventh book and add
down to maybe the seventh book and add that to my cart and now I see this here
that to my cart and now I see this here too meanwhile all of this information is
too meanwhile all of this information is stored in my session and so when I
stored in my session and so when I reload this cart again and again the
reload this cart again and again the reason I'm only seeing my two is because
reason I'm only seeing my two is because we're checking only the list in my
we're checking only the list in my session and flasks make sure again that
session and flasks make sure again that my session is different from your
my session is different from your session is different from Carter's
session is different from Carter's session as well but you write the code
session as well but you write the code once and it works for thousands millions
once and it works for thousands millions of people in
of people in parallel any questions on
parallel any questions on this this yes in the
this this yes in the back sorry say a little louder
uh so to recap so users will never have the same session values theoretically
the same session values theoretically the cookie that gets planted does not
the cookie that gets planted does not look like a smiley face for everyone
look like a smiley face for everyone each of us gets a big random number
each of us gets a big random number that's assigned to us so it' be like
that's assigned to us so it' be like each of us gets a completely unique hand
each of us gets a completely unique hand stamp that no one else can see the
stamp that no one else can see the reason no one else can see it is because
reason no one else can see it is because if the website's using https every time
if the website's using https every time this hand stamp is shown every time this
this hand stamp is shown every time this cookie is sent back and forth It's all
cookie is sent back and forth It's all encrypted as well so each of us can even
encrypted as well so each of us can even if we have the same contents by
if we have the same contents by coincidence because we like the same
coincidence because we like the same books they will be separate cookies
books they will be separate cookies separate memory separate sessions behind
separate memory separate sessions behind you
yeah really good question when does the session end totally configurable
session end totally configurable typically it ends when you close the tab
typically it ends when you close the tab or when you quit the browser or you can
or when you quit the browser or you can also configure cookies to themselves be
also configure cookies to themselves be persistent for a day for a week for
persistent for a day for a week for longer so for instance when you log into
longer so for instance when you log into um uh say Gmail they plant a cookie on
um uh say Gmail they plant a cookie on your computer probably for a week a
your computer probably for a week a month a year something like that because
month a year something like that because it would be annoying and probably drive
it would be annoying and probably drive you to like Outlook or something else if
you to like Outlook or something else if you kept having to log into your account
you kept having to log into your account whereas your bank account might actually
whereas your bank account might actually wait for you to just close the tab and
wait for you to just close the tab and then for your own Financial safety they
then for your own Financial safety they just automatically delete the session
just automatically delete the session far sooner but totally configurable by
far sooner but totally configurable by default as I'm using it it will
default as I'm using it it will typically be thrown away when the
typically be thrown away when the browser itself quits and here too is
browser itself quits and here too is another reason to develop websites using
another reason to develop websites using incognito mode because if you want to
incognito mode because if you want to just throw throw away all of your
just throw throw away all of your cookies you close the incognito window
cookies you close the incognito window mode open a new one and now you're
mode open a new one and now you're starting from scratch you don't have to
starting from scratch you don't have to manually delete all your cookies which
manually delete all your cookies which could log you out of websites you
could log you out of websites you actually care about
yeah a good question when using sessions if someone maliciously changes the value
if someone maliciously changes the value of sub forms could it affect other
of sub forms could it affect other people theoretically no because the
people theoretically no because the worst you can do is like add books to
worst you can do is like add books to your own shopping cart that you don't
your own shopping cart that you don't want there so at that point even though
want there so at that point even though it's on the server it doesn't affect you
it's on the server it doesn't affect you or Carter or anyone else unless there is
or Carter or anyone else unless there is something more globally happening like
something more globally happening like registering or deregistering for a sport
registering or deregistering for a sport or removing books from the Amazon
or removing books from the Amazon database that would be problematic but
database that would be problematic but in this case we're removing things only
in this case we're removing things only from my own session that the website is
from my own session that the website is giving me all right the last topic for
giving me all right the last topic for today is this thing here which is sort
today is this thing here which is sort of everywhere nowadays these things
of everywhere nowadays these things called apis or application programming
called apis or application programming interfaces and this is a very generic
interfaces and this is a very generic term in fact because any function you've
term in fact because any function you've used in C in scratch in Python in SQL
used in C in scratch in Python in SQL are all apis like there is a standard
are all apis like there is a standard way of interfacing with those functions
way of interfacing with those functions they all have names they sometimes have
they all have names they sometimes have return values they sometimes have
return values they sometimes have arguments and an API is just how you use
arguments and an API is just how you use a function or more generally an API just
a function or more generally an API just specifies how you interact with some
specifies how you interact with some service and so nowadays there's a lot of
service and so nowadays there's a lot of web-based services that you can use to
web-based services that you can use to get back data like the weather or the
get back data like the weather or the current time or the database of Amazon
current time or the database of Amazon books for instance all might have apis
books for instance all might have apis often web-based that allow you using
often web-based that allow you using URLs or some other technology to just
URLs or some other technology to just get data from someone else as though
get data from someone else as though it's a function you're calling remotely
it's a function you're calling remotely but HTTP is very often the mechanism
but HTTP is very often the mechanism that's used to actually get data from
that's used to actually get data from servers and the way the data can come
servers and the way the data can come back can be as follows let me end with
back can be as follows let me end with one final example using some of our
one final example using some of our familiar shows from uh weeks past let me
familiar shows from uh weeks past let me go ahead and close the old flask version
go ahead and close the old flask version version go back into Source 9 and go
version go back into Source 9 and go into how about an example called shows
into how about an example called shows and the first version of this zero I'm
and the first version of this zero I'm just going to go ahead and run with
just going to go ahead and run with flask run I'll hover over my URL and
flask run I'll hover over my URL and open it here and you'll see now that I
open it here and you'll see now that I have a very simple form as we keep doing
have a very simple form as we keep doing today I'm going to type in like o f f i
today I'm going to type in like o f f i c office into this search box and click
c office into this search box and click search and you'll see now that I ended
search and you'll see now that I ended up at a URL ending in/ search question
up at a URL ending in/ search question mark Q equals office so this is like my
mark Q equals office so this is like my own baby version of google.com but I
own baby version of google.com but I implemented it myself and for any title
implemented it myself and for any title of a TV show from a couple of weeks past
of a TV show from a couple of weeks past that matches o i I spit it out into an
that matches o i I spit it out into an unordered list how is this working you
unordered list how is this working you can maybe imagine even if you might not
can maybe imagine even if you might not be able to program this off the top of
be able to program this off the top of your head certainly so soon let me go
your head certainly so soon let me go into Source 9 let me go into uh show
into Source 9 let me go into uh show zero let me open up
zero let me open up app.py and in this file you'll see that
app.py and in this file you'll see that that I'm grabbing a uh file called
that I'm grabbing a uh file called shows. DB which is like a simpler
shows. DB which is like a simpler version of the one from a couple of
version of the one from a couple of weeks past uh here is why I see the web
weeks past uh here is why I see the web form my first route my index is super
form my first route my index is super simple it just spits out that form and
simple it just spits out that form and my search route like you can think of
my search route like you can think of this as google.com there only like four
this as google.com there only like four lines of code so if the user sends data
lines of code so if the user sends data to SL search this function called search
to SL search this function called search is called I declare a variable called
is called I declare a variable called shows I execute a SQL command that is
shows I execute a SQL command that is Select star from shows where title like
Select star from shows where title like question mark and the syntax here is a
question mark and the syntax here is a little crazy but I want to prefix to the
little crazy but I want to prefix to the user's input percent sign and suffix it
user's input percent sign and suffix it with a percent sign as well putting in
with a percent sign as well putting in the between those two values the actual
the between those two values the actual input why in SQL what is it mean if you
input why in SQL what is it mean if you have a percent sign to the left and to
have a percent sign to the left and to the right nothing to do with Ginger
the right nothing to do with Ginger today yeah it's a it's a wild card so it
today yeah it's a it's a wild card so it means match zero or more characters on
means match zero or more characters on the left or match zero or more
the left or match zero or more characters on the right you have to do
characters on the right you have to do the concatenation
the concatenation as the second argument to this function
as the second argument to this function you can't do something clever like put
you can't do something clever like put it here around the like the is the
it here around the like the is the placeholder that you plug these values
placeholder that you plug these values into but this just means hey SQL show me
into but this just means hey SQL show me all of the titles that have o f i
all of the titles that have o f i somewhere in them that gives me back an
somewhere in them that gives me back an a list of dictionaries I pass that in as
a list of dictionaries I pass that in as a placeholder for a variable called
a placeholder for a variable called shows and if we look at search. HTML
shows and if we look at search. HTML let's look at that in my templates
let's look at that in my templates directory there's something called
directory there's something called search. HTML super simple I mean this is
search. HTML super simple I mean this is like the essence of google.com search
like the essence of google.com search results I'm using an unordered list to
results I'm using an unordered list to keep things simple but I iterate over
keep things simple but I iterate over every show in the shows list that came
every show in the shows list that came back and I output An Li with each of
back and I output An Li with each of those shows titles and that's it now
those shows titles and that's it now Google has like Blue Links and like
Google has like Blue Links and like little previews and other text the first
little previews and other text the first sentence or so from each page but like
sentence or so from each page but like that's the idea like this is really
that's the idea like this is really similar in spirit to what google.com
similar in spirit to what google.com search does for you now how is this
search does for you now how is this working there's no API involved here yet
working there's no API involved here yet this is just very basic HTTP I submit
this is just very basic HTTP I submit the the form I go to another route and I
the the form I go to another route and I get back the results but check out this
get back the results but check out this version Let me close these tabs here and
version Let me close these tabs here and open my first terminal window let me go
open my first terminal window let me go into shows one from today's Source 9
into shows one from today's Source 9 directory and do flask run this time let
directory and do flask run this time let me go ahead and hover over that URL and
me go ahead and hover over that URL and open it here and gone now is the submit
open it here and gone now is the submit button now I'm going to make an user
button now I'm going to make an user interface that uses a technique called
interface that uses a technique called Ajax for asynchronous uh JavaScript and
Ajax for asynchronous uh JavaScript and XML which is somewhat of a data term
XML which is somewhat of a data term because we're not using something called
because we're not using something called ml anymore but Ajax is a technique
ml anymore but Ajax is a technique whereby you don't have to submit forms
whereby you don't have to submit forms anymore to get more data from the server
anymore to get more data from the server you can use JavaScript per last week
you can use JavaScript per last week listen for an event like the key press
listen for an event like the key press coming down or up and as soon as you
coming down or up and as soon as you hear such an event you can secretly in
hear such an event you can secretly in JavaScript code send a request to the
JavaScript code send a request to the server to get back more data and then
server to get back more data and then plug it into the Dom the tree in the
plug it into the Dom the tree in the computer's memory and this just makes
computer's memory and this just makes for more seamless experiences like
for more seamless experiences like autocomplete on any website so now let
autocomplete on any website so now let me try typing o okay we got auto
me try typing o okay we got auto complete super fast f f i c e and you'll
complete super fast f f i c e and you'll see every time I add more keys to my
see every time I add more keys to my input I'm doing another search another
input I'm doing another search another search another search and the data is
search another search and the data is changing now how is this working well
changing now how is this working well let me go back to vs code here and in my
let me go back to vs code here and in my other terminal window let me open up uh
other terminal window let me open up uh app.py and in app.py you'll see that
app.py and in app.py you'll see that there's still a
there's still a search route down below that turns a
search route down below that turns a search template but watch this let me go
search template but watch this let me go into templates search. HTML and notice
into templates search. HTML and notice here that we're indeed getting back an
here that we're indeed getting back an unordered list of shows again and again
unordered list of shows again and again and again and this HTML that's coming
and again and this HTML that's coming back let me go here let me open my
back let me go here let me open my terminal oh sorry this is the wrong
terminal oh sorry this is the wrong version uh sorry I was in the wrong
version uh sorry I was in the wrong folder let's fix this and shows one code
folder let's fix this and shows one code of app.py it's almost the same thing
of app.py it's almost the same thing inserch
inserch here okay well I changed this slightly
here okay well I changed this slightly let me show you this version of search
let me show you this version of search if I open up app.py here's my search
if I open up app.py here's my search route I'm getting a variable called Q
route I'm getting a variable called Q giving it the value of whatever request.
giving it the value of whatever request. RX has from the user like Q equals
RX has from the user like Q equals office and then I'm checking if the user
office and then I'm checking if the user actually typed something in execute this
actually typed something in execute this SQL query select star from shows where
SQL query select star from shows where title is like that using the same and
title is like that using the same and this time just to keep things efficient
this time just to keep things efficient I limited the total results to 50
I limited the total results to 50 instead of an infinite number otherwise
instead of an infinite number otherwise if the user type nothing just to be
if the user type nothing just to be super safe here I'm setting shows equal
super safe here I'm setting shows equal to an empty list so if you don't type
to an empty list so if you don't type anything there's nothing to show and no
anything there's nothing to show and no matter what I render this template
matter what I render this template called search. HTML well let's look at
called search. HTML well let's look at that if I open up templat search. HTML
that if I open up templat search. HTML this time there's no layout there's no
this time there's no layout there's no inheritance of that layout. HTML I am
inheritance of that layout. HTML I am literally generating just a whole bunch
literally generating just a whole bunch of Li fragments why well let's see what
of Li fragments why well let's see what what's happening in the browser and this
what's happening in the browser and this is the beginning of an API if I wanted
is the beginning of an API if I wanted to make this API available this
to make this API available this application programming interface I
application programming interface I could tell the world that if you want to
could tell the world that if you want to search for TV shows in my database go to
search for TV shows in my database go to URL that's something something/ search
URL that's something something/ search question mark Q equals office or cat or
question mark Q equals office or cat or dog or anything else and what I will
dog or anything else and what I will return to you is this enter I will just
return to you is this enter I will just give you a whole bunch of Li tags which
give you a whole bunch of Li tags which almost looks the same but let me view
almost looks the same but let me view the page source only am I going to hand
the page source only am I going to hand you back a fragment of HTML I'm not
you back a fragment of HTML I'm not giving you an HTML tag a body tag a
giving you an HTML tag a body tag a title tag a head tag I'm not giving you
title tag a head tag I'm not giving you a web page I'm giving you a fragment of
a web page I'm giving you a fragment of HTML that you can now do whatever you
HTML that you can now do whatever you want including insert this into your own
want including insert this into your own unordered list so notice what happens in
unordered list so notice what happens in this actual app if I go back to vs code
this actual app if I go back to vs code here let me open up my index template
here let me open up my index template here and you'll see some JavaScript
here and you'll see some JavaScript magic so in JavaScript here in my form
magic so in JavaScript here in my form that only had the text box and no button
that only had the text box and no button what am I doing in a script tag here I
what am I doing in a script tag here I am creating a variable called input and
am creating a variable called input and I'm using this function called query
I'm using this function called query selector that just gets me a reference
selector that just gets me a reference to the input text box on the form so I
to the input text box on the form so I can see what the human typed this is a
can see what the human typed this is a little different today but I'm using
little different today but I'm using input. atevent listener which is a way
input. atevent listener which is a way in JavaScript to tell it just like in
in JavaScript to tell it just like in scratch listen for something to happen
scratch listen for something to happen like the green flag being clicked but in
like the green flag being clicked but in this case listen for an event that
this case listen for an event that involves input that is like typing on
involves input that is like typing on the keyboard whether it's by a copy
the keyboard whether it's by a copy paste manual input or anything else then
paste manual input or anything else then whenever that happens call this function
whenever that happens call this function and async stands for asynchronous this
and async stands for asynchronous this is a term of art which means that this
is a term of art which means that this this function might take like a split
this function might take like a split second maybe even a second or two to
second maybe even a second or two to execute so it's going to do it behind
execute so it's going to do it behind the scenes like in the background so to
the scenes like in the background so to speak and what is it going to do well
speak and what is it going to do well it's going to call a JavaScript function
it's going to call a JavaScript function that all browsers now Support called
that all browsers now Support called Fetch which is a function that uses HTTP
Fetch which is a function that uses HTTP to go fetch more data via the from the
to go fetch more data via the from the server it's going to fetch data from a
server it's going to fetch data from a route called SL search question mark Q
route called SL search question mark Q equals and whatever the value is that
equals and whatever the value is that the user typed in so I'm just sort of
the user typed in so I'm just sort of manually creating my own mini URL and
manually creating my own mini URL and telling JavaScript go fetch me that HTML
telling JavaScript go fetch me that HTML when it comes back via this line of text
when it comes back via this line of text here called response. text and let me
here called response. text and let me wave my hand at await await just means
wave my hand at await await just means this might not come back immediately
this might not come back immediately let's await the response and when it
let's await the response and when it does come and then let's execute this
does come and then let's execute this code I'm going to do this I'm going to
code I'm going to do this I'm going to search the document the whole web page
search the document the whole web page for this UL tag which is somewhere in
for this UL tag which is somewhere in this page that we'll see in a moment
this page that we'll see in a moment change its inner HTML to be that
change its inner HTML to be that fragment of Li Li Li of all of those
fragment of Li Li Li of all of those matching shows and where does this all
matching shows and where does this all go well if we scroll up here you'll
go well if we scroll up here you'll notice that there's my usual HTML up at
notice that there's my usual HTML up at top head tag body tag and all of that
top head tag body tag and all of that there's the text box that we've talked
there's the text box that we've talked about already there's no button for
about already there's no button for submitting cuz it all happens
submitting cuz it all happens automatically but there is by default
automatically but there is by default this empty UL that has nothing by
this empty UL that has nothing by default until we start using that API
default until we start using that API and the final flourish here is this this
and the final flourish here is this this is kind of a uh ugly sloppy way to get
is kind of a uh ugly sloppy way to get data from a server to just get back like
data from a server to just get back like a fragment of HTML like what if I'm not
a fragment of HTML like what if I'm not using HTML I want to store these uh TV
using HTML I want to store these uh TV shows in a PDF or in some other wet tag
shows in a PDF or in some other wet tag in a table or something like that it
in a table or something like that it doesn't really make sense for the server
doesn't really make sense for the server to be presuming that I want Li tags
to be presuming that I want Li tags surrounding each of my data better would
surrounding each of my data better would be to get a more generic format back and
be to get a more generic format back and that format is almost always nowadays
that format is almost always nowadays called this our final acronym Json
called this our final acronym Json JavaScript object notation and let me do
JavaScript object notation and let me do this let me close these two tabs here
this let me close these two tabs here and open my terminal window and C cancel
and open my terminal window and C cancel the previous version of flask that was
the previous version of flask that was running let me close this version and
running let me close this version and look at our final version called shows
look at our final version called shows two and do flask run on I'm going to
two and do flask run on I'm going to hover over that URL and open it in a
hover over that URL and open it in a browser and I'm going to manually visit
browser and I'm going to manually visit after zooming in let's do again slash uh
after zooming in let's do again slash uh search question mark uh Q equals Office
search question mark uh Q equals Office enter and this is what Json looks like
enter and this is what Json looks like now at a glance this does not seem like
now at a glance this does not seem like an improvement like this looks crazy
an improvement like this looks crazy that it's just this Big Blob of text but
that it's just this Big Blob of text but it's just enough text for the computer
it's just enough text for the computer to be able to process it reliably notice
to be able to process it reliably notice that there's a cur a square bracket here
that there's a cur a square bracket here and if I actually scroll to the Bott
and if I actually scroll to the Bott botom there' be a closed square bracket
botom there' be a closed square bracket like way down there inside of that
like way down there inside of that square bracket is a curly brace then ID
square bracket is a curly brace then ID colon and then a number then a title
colon and then a number then a title quote unquote colon and then the title
quote unquote colon and then the title and then the closed curly brace so what
and then the closed curly brace so what you're seeing in JavaScript object
you're seeing in JavaScript object notation is a very standard super
notation is a very standard super popular format that's just text that
popular format that's just text that still uses square brackets for lists AKA
still uses square brackets for lists AKA arrays that still uses curly braces for
arrays that still uses curly braces for dictionaries key value pairs so what you
dictionaries key value pairs so what you see here is a massive list up to 50 I
see here is a massive list up to 50 I think think shows that came back from
think think shows that came back from this API Each of which has a dictionary
this API Each of which has a dictionary if you will an object of key value pairs
if you will an object of key value pairs what keys and values an ID key and a
what keys and values an ID key and a title key Each of which has a value
title key Each of which has a value respectively and this is the same data
respectively and this is the same data from IMDb some of which you might be
from IMDb some of which you might be recalling visually this is just a very
recalling visually this is just a very raw computer friendly way of returning a
raw computer friendly way of returning a whole bunch of data that we humans don't
whole bunch of data that we humans don't need to see but I can use this data by
need to see but I can use this data by going back into vs code let me open
going back into vs code let me open another terminal window and go into
another terminal window and go into Source
Source 9/ shows 2 and in here let me go ahead
9/ shows 2 and in here let me go ahead and open up how about uh templates
and open up how about uh templates index.html which previously just used
index.html which previously just used that inner HTML trick and this is not
that inner HTML trick and this is not going to impress you're not going to be
going to impress you're not going to be pleased with this syntax but let me just
pleased with this syntax but let me just at least explain what we're doing it
at least explain what we're doing it turns out that Json is just the better
turns out that Json is just the better way in general the more generic
way in general the more generic multi-purpose user agnostic language
multi-purpose user agnostic language agnostic way of returning data from a
agnostic way of returning data from a server because it's just text so it
server because it's just text so it doesn't matter if you're using python or
doesn't matter if you're using python or C or C++ or JavaScript or Ruby or PHP or
C or C++ or JavaScript or Ruby or PHP or something else like all of those
something else like all of those languages can process Json information
languages can process Json information and indeed here is some JavaScript that
and indeed here is some JavaScript that does just that same code as before
does just that same code as before initially I declare a variable called
initially I declare a variable called input that gives me access to the user's
input that gives me access to the user's text box I listen for input like key
text box I listen for input like key strokes going up and down and when they
strokes going up and down and when they happen I call this Anonymous function uh
happen I call this Anonymous function uh I fetch data from the server using the
I fetch data from the server using the exact same code as before search
exact same code as before search question mark Q equals office or
question mark Q equals office or anything else and then this is just now
anything else and then this is just now new code that I use to convert that Json
new code that I use to convert that Json data into my own HTML format be it an
data into my own HTML format be it an unordered list an ordered list a table
unordered list an ordered list a table or anything else what am I doing I've
or anything else what am I doing I've got a variable called HTML initialized
got a variable called HTML initialized to nothing so I've got no HTML initially
to nothing so I've got no HTML initially I then iterate over every ID in those
I then iterate over every ID in those shows so every one of IMDB's unique
shows so every one of IMDB's unique identifiers I iterate over them one at a
identifiers I iterate over them one at a time and then I go into the show at its
time and then I go into the show at its ID location and I grab its title and
ID location and I grab its title and this
this is forget this for just a moment I then
is forget this for just a moment I then take this HTML variable concatenate or
take this HTML variable concatenate or join onto it my own Li tag plus the
join onto it my own Li tag plus the title plus the close Li tag and I
title plus the close Li tag and I skipped this because it got scary pretty
skipped this because it got scary pretty fast but it turns out that if some TV
fast but it turns out that if some TV shows have actually angled brackets in
shows have actually angled brackets in that could break my HTML entirely so it
that could break my HTML entirely so it turns out you might recall super briefly
turns out you might recall super briefly last week we had the copyright symbol
last week we had the copyright symbol using an HTML entity using the Ampersand
using an HTML entity using the Ampersand and the hash symbol and 169 semicolon it
and the hash symbol and 169 semicolon it turns out there are other such cryptic
turns out there are other such cryptic sequences of characters that represent
sequences of characters that represent otherwise dangerous or untypable
otherwise dangerous or untypable characters like this which could confuse
characters like this which could confuse the computer into thinking it's at the
the computer into thinking it's at the beginning of a tag and an Amper sand
beginning of a tag and an Amper sand which could similarly trick the computer
which could similarly trick the computer into thinking that it's a entity which
into thinking that it's a entity which it isn't but long story short there are
it isn't but long story short there are libraries thankfully that handle much of
libraries thankfully that handle much of this for you for our purposes the
this for you for our purposes the takeaway is that now that you understand
takeaway is that now that you understand a bit of
a bit of HTTP uh now that you understand a bit of
HTTP uh now that you understand a bit of HTML CSS and JavaScript all of which
HTML CSS and JavaScript all of which they have their roles you can use them
they have their roles you can use them ultimately to start assembling your own
ultimately to start assembling your own web applications as you will for problem
web applications as you will for problem set nine um stitching together all of
set nine um stitching together all of those languages and building
those languages and building full-fledged web applications mobile
full-fledged web applications mobile applications or anything more and for
applications or anything more and for that I think we are all set and if the
that I think we are all set and if the first one up here uh can have these
first one up here uh can have these cookies as well we'll see you next time
cookies as well we'll see you next time for our very last cs50
[Music] lecture
good afternoon my name is Sarah and my name is Grant and we are the Harvard
name is Grant and we are the Harvard crocodillos and pitches now Sarah and I
crocodillos and pitches now Sarah and I understand that today is the final
understand that today is the final lecture of cs50 it's been a tough
lecture of cs50 it's been a tough semester we made it through pets four
semester we made it through pets four five and even Finance now I know this is
five and even Finance now I know this is an unpopular opinion but I particularly
an unpopular opinion but I particularly enjoyed Finance I spent a lot of time
enjoyed Finance I spent a lot of time with my
with my flask the P set there was a p
flask the P set there was a p set well um at least things are looking
set well um at least things are looking up um today is our last lecture and look
up um today is our last lecture and look how far we've come if I were an emoji
how far we've come if I were an emoji right now I'd be the face with tears of
right now I'd be the face with tears of joy sorry about that we're just trying
joy sorry about that we're just trying to work some cs50 references into the uh
to work some cs50 references into the uh intro I I mean uh boy I sure hope this
intro I I mean uh boy I sure hope this uh tide man doesn't run off my Mario
uh tide man doesn't run off my Mario filter you could say that for Loop x
filter you could say that for Loop x equals Open Bracket one comma 2 close
equals Open Bracket one comma 2 close bracket boy I sure wish we had checked
bracket boy I sure wish we had checked to see if these jokes were funny when we
to see if these jokes were funny when we wrote
wrote them any who we hope that you'll enjoy
them any who we hope that you'll enjoy this brief
this brief [Music]
serenade your boot
KN House of well there's friers Andy truck barbecue ribs Tri
the boy boy bo boy bo bo boy boy down at the
boy bo boy bo bo boy boy down at the house the house of well your root and
house the house of well your root and you walk on down to a knock down Shack
you walk on down to a knock down Shack on the edge of town There's a l
on the edge of town There's a l there just quit you see
there just quit you see fall down the house the house
[Applause] of up your boots and you walk on down
at the house the House of Blue
[Applause] good afternoon everyone we are the
good afternoon everyone we are the Harvard crocodillos and it is such an
Harvard crocodillos and it is such an honor to be here with the Radcliff
honor to be here with the Radcliff pitches performing for cs50's final
pitches performing for cs50's final lecture congratulations to everyone and
lecture congratulations to everyone and we hope you'll enjoy this our tribute to
4 C is for the language I once knew O is for all notation I must do D is for
for all notation I must do D is for dynamic flask run and finance it is even
dynamic flask run and finance it is even more than David May in canor so code is
more than David May in canor so code is all that I can give to you to youe
all that I can give to you to youe debugging it since
debugging it since PE soon deadlines I'll make it hit
PE soon deadlines I'll make it hit compile and please don't break it code
compile and please don't break it code was made by
was made by [Music]
I for notation I must do is
more is [Music]
I is for the language 0 1 0 1 0 oh is for otation 0 1 1 D is for dynamic flas
for otation 0 1 1 D is for dynamic flas run and finance e is even more than
run and finance e is even more than David mail in canor so C is all that I
David mail in canor so C is all that I can give to
can give to you debugging it since
you debugging it since PE soon deadlines I'll make it hit
PE soon deadlines I'll make it hit compile and please don't break it code
compile and please don't break it code was made by me for COD was made by me
was made by me for COD was made by me for COD was
for COD was [Music]
all right this is cs50 and cs50 this was the Harvard crocodillos and the Radcliff
the Harvard crocodillos and the Radcliff pitches if one more time we could thank
pitches if one more time we could thank them for joining us
today so this is already week 10 our last and indeed among the goals for
last and indeed among the goals for today are to hopefully give you all the
today are to hopefully give you all the more of appreciation of truly just how
more of appreciation of truly just how far you've come recall that in week zero
far you've come recall that in week zero we began with this this visual here
we began with this this visual here whereby it was described this class as a
whereby it was described this class as a bit of a fire hose whereby drinking from
bit of a fire hose whereby drinking from that fire hose or really a fire hose
that fire hose or really a fire hose from a water fountain uh is not unlike
from a water fountain uh is not unlike getting an education down the road too
getting an education down the road too and so this is to say that if you're
and so this is to say that if you're feeling like you didn't quite get it all
feeling like you didn't quite get it all down like that's actually okay and
down like that's actually okay and that's to be expected and even if you
that's to be expected and even if you felt that with each passing week 0 1 two
felt that with each passing week 0 1 two all the way up now till 10 it never
all the way up now till 10 it never really ever got easier perhaps just
really ever got easier perhaps just consider that what was once hard like
consider that what was once hard like Mario and like getting hello world to
Mario and like getting hello world to compile is indeed the right measure of
compile is indeed the right measure of the Delta between week Zer and now in
the Delta between week Zer and now in week 10 in fact you might recall that
week 10 in fact you might recall that again in week zero 2third of your
again in week zero 2third of your classmates had never taken a CS course
classmates had never taken a CS course before now of course you all have and
before now of course you all have and indeed if you think back too to this
indeed if you think back too to this final sentiment from week zero that
final sentiment from week zero that indeed what ultimately matters in this
indeed what ultimately matters in this course is not where you end up relative
course is not where you end up relative to your classmates but where you end up
to your classmates but where you end up relative to where you yourself began so
relative to where you yourself began so I would take some pride take some
I would take some pride take some satisfaction take some relief even
satisfaction take some relief even though a little bit more work does
though a little bit more work does remain at really just how far you've
remain at really just how far you've come since that week zero and recall
come since that week zero and recall that in week zero we literally started
that in week zero we literally started with just zeros and ones and by now many
with just zeros and ones and by now many of you might have gleaned that these 64
of you might have gleaned that these 64 zeros and ones have been spelling
zeros and ones have been spelling something week by week in fact today is
something week by week in fact today is our very last message here in binary uh
our very last message here in binary uh encoded on stage but then quickly we
encoded on stage but then quickly we introduced scratch and we started to
introduced scratch and we started to assemble some building blocks of
assemble some building blocks of programming Loops conditions uh uh Loops
programming Loops conditions uh uh Loops conditions functions and the like but
conditions functions and the like but without all the distractions of
without all the distractions of semicolons and curly braces and all of
semicolons and curly braces and all of that which admittedly we introduce the
that which admittedly we introduce the next week when we introduce you to C but
next week when we introduce you to C but even now that we've transitioned to
even now that we've transitioned to python hopefully even those kinds of
python hopefully even those kinds of Curiosities or confusions are hopefully
Curiosities or confusions are hopefully starting to just get more familiar and
starting to just get more familiar and so you finally start to see the missing
so you finally start to see the missing semicolon as opposed to spending time on
semicolon as opposed to spending time on that kind of struggle recall too that in
that kind of struggle recall too that in week two we started talking already
week two we started talking already about memory and like how you can manage
about memory and like how you can manage things in arrays that later became of
things in arrays that later became of course in Python lists uh the week after
course in Python lists uh the week after we talked not only about uh debugging uh
we talked not only about uh debugging uh bugs in code but how to debug those same
bugs in code but how to debug those same programs um thereafter we started
programs um thereafter we started talking talking about algorithms and we
talking talking about algorithms and we took a step back from code and looked at
took a step back from code and looked at the bubble sorts and the selection sorts
the bubble sorts and the selection sorts and the merge sorts and all of the
and the merge sorts and all of the searches as well that go hand in hand
searches as well that go hand in hand with that and indeed this ultimately is
with that and indeed this ultimately is what a lot of problem solving moving
what a lot of problem solving moving forward is going to be about just
forward is going to be about just solving problems with some form of
solving problems with some form of algorithm and you have so many different
algorithm and you have so many different languages um in your toolkit now with
languages um in your toolkit now with which to approach problems like those we
which to approach problems like those we talk thereafter about pointers which are
talk thereafter about pointers which are not likely to come back in any modern
not likely to come back in any modern languages that you now use but hopefully
languages that you now use but hopefully you have an all the better of a sense
you have an all the better of a sense underneath the hood of like what's going
underneath the hood of like what's going on inside of the computer so that when
on inside of the computer so that when you're designing something you're using
you're designing something you're using something something crashes you at least
something something crashes you at least have a mental model for what's going on
have a mental model for what's going on and it's no longer that week zero black
and it's no longer that week zero black box as it once was I mean you built
box as it once was I mean you built things like this think back to week five
things like this think back to week five when you built your own hash table and
when you built your own hash table and those things are everywhere key value
those things are everywhere key value pairs whether it's in python or in C or
pairs whether it's in python or in C or if it's now in CSS and JavaScript and
if it's now in CSS and JavaScript and even HTML like that principle of key
even HTML like that principle of key value pairs is really everywhere and so
value pairs is really everywhere and so of course now code doesn't necessarily
of course now code doesn't necessarily have to look like this it now
have to look like this it now wonderfully looks a little something
wonderfully looks a little something more like this but eventually you're
more like this but eventually you're probably not I'm not going to use Python
probably not I'm not going to use Python anymore something new and better is
anymore something new and better is going to come along but odds are like a
going to come along but odds are like a lot of the building blocks from these
lot of the building blocks from these past 11 weeks are still going to be
past 11 weeks are still going to be useful for wrapping your mind around
useful for wrapping your mind around those new worlds and indeed SQL we
those new worlds and indeed SQL we introduced you a little bit too and even
introduced you a little bit too and even if you don't feel yourself yet an expert
if you don't feel yourself yet an expert hopefully have a sense of like what you
hopefully have a sense of like what you can do with it and what problems you can
can do with it and what problems you can solve uh it's of course a better
solve uh it's of course a better alternative to something like something
alternative to something like something simple like a spreadsheet and now of
simple like a spreadsheet and now of course like web stuff is everywhere
course like web stuff is everywhere whether it's on your laptop or desktop
whether it's on your laptop or desktop or a lot of the mobile apps that you use
or a lot of the mobile apps that you use on your phone even though they're native
on your phone even though they're native applications like you install them from
applications like you install them from Google Play or the Apple App Store like
Google Play or the Apple App Store like they're implemented increasingly with
they're implemented increasingly with HTML CSS and JavaScript but they're put
HTML CSS and JavaScript but they're put in a little rectangular window so you
in a little rectangular window so you don't even notice that that's actually
don't even notice that that's actually really just an embedded browser and then
really just an embedded browser and then of course you can build things as you
of course you can build things as you might for your final project that to
might for your final project that to might very well be web based if you go
might very well be web based if you go that route I mean I'm still clinging to
that route I mean I'm still clinging to like the very first like web app I ever
like the very first like web app I ever made years ago um but honestly I do that
made years ago um but honestly I do that in part because I was just so darn proud
in part because I was just so darn proud that like I taught myself how to do
that like I taught myself how to do something and it actually worked and was
something and it actually worked and was used by other people so whether it's
used by other people so whether it's just used by you or your classmates or
just used by you or your classmates or your roommates or your family or your
your roommates or your family or your company down the line there's a great
company down the line there's a great sense of satisfaction that comes despite
sense of satisfaction that comes despite all of the the pain that might be along
all of the the pain that might be along the way when you just can't see or fix
the way when you just can't see or fix that bug now of course we'll transition
that bug now of course we'll transition as you'll see in the coming days to try
as you'll see in the coming days to try empowering you to code client side as
empowering you to code client side as well up until now you've been using our
well up until now you've been using our own vs code installation in the cloud
own vs code installation in the cloud which is nice cuz you got up in running
which is nice cuz you got up in running super fast in week one focusing only on
super fast in week one focusing only on code challenges not on technical
code challenges not on technical difficulties but among the goals now if
difficulties but among the goals now if you so choose and want to program after
you so choose and want to program after this class even if you never take
this class even if you never take another CS course before you can use
another CS course before you can use these same real world deao standard
these same real world deao standard tools on your own Mac or PC and so
tools on your own Mac or PC and so pictured here is a screenshot of like vs
pictured here is a screenshot of like vs code on the Mac and even though yes
code on the Mac and even though yes you're going to have to like jump
you're going to have to like jump through a couple of more Hoops to just
through a couple of more Hoops to just get python or some other language
get python or some other language working on your own Mac or PC like
working on your own Mac or PC like that's what programming is ultimately
that's what programming is ultimately going to be about and we deliberately
going to be about and we deliberately transition you to this at terms end so
transition you to this at terms end so that now you have 11 weeks of more
that now you have 11 weeks of more Comfort under your belt with which to
Comfort under your belt with which to solve sort of silly technical support
solve sort of silly technical support headaches that might have been deal
headaches that might have been deal breakers so many weeks ago so there's
breakers so many weeks ago so there's still more to be done uh in the coming
still more to be done uh in the coming weeks and indeed a support structure
weeks and indeed a support structure there for the cs50 hackathon of course
there for the cs50 hackathon of course will be this 700 p.m. till 7 a.m.
will be this 700 p.m. till 7 a.m. opportunity to dive into your final
opportunity to dive into your final project well really continue diving into
project well really continue diving into your final project ideally at that point
your final project ideally at that point uh alongside classmates perhaps your
uh alongside classmates perhaps your project Partners if you're working
project Partners if you're working collaboratively
collaboratively uh and awaiting you will be such
uh and awaiting you will be such memories and excitement hopefully as
memories and excitement hopefully as these even as you then turn your
these even as you then turn your attention back to your final project
attention back to your final project there of course will be several meals
there of course will be several meals during the day culminating with 500 a.m.
during the day culminating with 500 a.m. uh shuttles to IHOP the local Pancake
uh shuttles to IHOP the local Pancake Place uh if you are so awake at that
Place uh if you are so awake at that point or even if you get there uh this
point or even if you get there uh this is not an uncommon site as you might
is not an uncommon site as you might recall from week zero and then lastly is
recall from week zero and then lastly is the cs-50 fair which is finally back
the cs-50 fair which is finally back after a couple of years now of it not
after a couple of years now of it not being on campus and this will be an
being on campus and this will be an opportunity for everyone to present
opportunity for everyone to present their final projects to passers by
their final projects to passers by classmates faculty and staff and really
classmates faculty and staff and really just Delight in what it is you created
just Delight in what it is you created on your Mac your PC your phone in the
on your Mac your PC your phone in the cloud or anywhere else and indeed it's
cloud or anywhere else and indeed it's just going to be an opportunity to bring
just going to be an opportunity to bring your laptop to a shared space or your
your laptop to a shared space or your phone and introduce your project to
phone and introduce your project to passers by such as might appear and
passers by such as might appear and ultimately celebrate what you all uh
ultimately celebrate what you all uh accomplished and indeed will you be
accomplished and indeed will you be handed at the cs50 fair your very own I
handed at the cs50 fair your very own I took cs50 t-shirt which I dare say I'm
took cs50 t-shirt which I dare say I'm still wearing all of these years later
still wearing all of these years later and so you
and so you to
to we'll have that uh ahead of you as well
we'll have that uh ahead of you as well so for what's on the agenda today we
so for what's on the agenda today we thought we would not only look back but
thought we would not only look back but look forward but first we wanted to
look forward but first we wanted to thank so many of the team members that
thank so many of the team members that have been helping both on stage and off
have been helping both on stage and off who've made this course and these
who've made this course and these sections and so much more about cs50
sections and so much more about cs50 possible of course um the building that
possible of course um the building that we are now in there's a whole team
we are now in there's a whole team downstairs in Memorial Hall who helps us
downstairs in Memorial Hall who helps us get set up and organized each day our
get set up and organized each day our thanks to them there's the education
thanks to them there's the education Support Services team who makes
Support Services team who makes everything look and sound so well down
everything look and sound so well down here especially when we have all of the
here especially when we have all of the more microphones as well our friends the
more microphones as well our friends the harbard crocodillos and the redcliff
harbard crocodillos and the redcliff pitches most recently and then of course
pitches most recently and then of course cs50's own team uh but butter cs50's own
cs50's own team uh but butter cs50's own favorite restaurant Chang show down the
favorite restaurant Chang show down the road indeed if you find yourself in
road indeed if you find yourself in Cambridge for the next one two 3 four
Cambridge for the next one two 3 four years or visiting from out of town uh do
years or visiting from out of town uh do pay a visit to our friends just down the
pay a visit to our friends just down the road and in fact we'll have our very
road and in fact we'll have our very last cs50 lunch this Friday if you're
last cs50 lunch this Friday if you're able locally to partake and then there
able locally to partake and then there cs50's own team um both on stage and off
cs50's own team um both on stage and off in thanks truly because not only do they
in thanks truly because not only do they make the everything run so smoothly they
make the everything run so smoothly they capture it for students here who might
capture it for students here who might not be physically present here for our
not be physically present here for our friends down in New Haven at Yale and
friends down in New Haven at Yale and certainly for anyone online who might be
certainly for anyone online who might be tuning in as well and then lastly wanted
tuning in as well and then lastly wanted to thank of course the huge team of your
to thank of course the huge team of your classmates your peers that make cs50
classmates your peers that make cs50 possible in sections and office hours
possible in sections and office hours tutorials and more allow me to share
tutorials and more allow me to share with you the outtakes so that even we
with you the outtakes so that even we the teaching staff sometimes struggle
the teaching staff sometimes struggle with computer science here are some of
with computer science here are some of the clips that we captured when just
the clips that we captured when just passing packets via tcpip a while back
passing packets via tcpip a while back you saw the finally the the nicely
you saw the finally the the nicely polished version but here are if I may
polished version but here are if I may if we could dim the lights are some of
if we could dim the lights are some of the
the outakes nothing
outakes nothing go buffering
go buffering okay Josh
okay Josh nice
[Music] Helen no oh wait
Josh uh um [Music]
Sophie amazing that was
amazing that was perfect
B I think I what
I what you oh nice
you oh nice [Music]
[Music] guy that was amazing thank you all so
good indeed and that moment if we could just one round of applause for everyone
just one round of applause for everyone who's helped out this
semester so back in week zero uh we introduced you of course to this idea of
introduced you of course to this idea of computational thinking which is to think
computational thinking which is to think a a little more methodically a little
a a little more methodically a little more algorithmically and by way of these
more algorithmically and by way of these various languages hopefully that is
various languages hopefully that is something you notice maybe not in the
something you notice maybe not in the moment but in the months and the years
moment but in the months and the years to come that you do find that your
to come that you do find that your thoughts are indeed a little more
thoughts are indeed a little more cleaned up and you're just able to
cleaned up and you're just able to express yourself a little more precisely
express yourself a little more precisely and even spot illogic in someone else's
and even spot illogic in someone else's document or statements as well but at
document or statements as well but at the end of the day really a course like
the end of the day really a course like this is also about critical thinking and
this is also about critical thinking and indeed rewind again to week zero when we
indeed rewind again to week zero when we frame the entirety of computer science
frame the entirety of computer science is really just this like problem solving
is really just this like problem solving and any problem in the world be it CS or
and any problem in the world be it CS or otherwise has some input and we decided
otherwise has some input and we decided how to represent those inputs it needs
how to represent those inputs it needs some output the solution there too and
some output the solution there too and then all of what you focused on doing
then all of what you focused on doing and learning and applying these past
and learning and applying these past several weeks is in that proverbial
several weeks is in that proverbial black box which hopefully is not such an
black box which hopefully is not such an abstraction anymore but is indeed
abstraction anymore but is indeed something that you know how to harness
something that you know how to harness and know what could be going on
and know what could be going on underneath the hood even if it's in some
underneath the hood even if it's in some technology or some language that maybe
technology or some language that maybe we ourselves didn't cover because a lot
we ourselves didn't cover because a lot of those first principles remain the
of those first principles remain the same now along the way we talked about
same now along the way we talked about the quality of solutions to those
the quality of solutions to those problems we happen to focus on
problems we happen to focus on correctness just does it work design
correctness just does it work design which is a bit more qualitative and
which is a bit more qualitative and subjective and then style the Aesthetics
subjective and then style the Aesthetics of it all and these two are
of it all and these two are characteristic maybe not with these same
characteristic maybe not with these same words of just how you might write or
words of just how you might write or evaluate other creations in life be it
evaluate other creations in life be it physical or written or the like so think
physical or written or the like so think about too as you solve problems just how
about too as you solve problems just how you can sort of frame for yourself like
you can sort of frame for yourself like am I doing a good job or not by
am I doing a good job or not by quantizing it along these or perhaps
quantizing it along these or perhaps other axes as well and we thought we'd
other axes as well and we thought we'd highlight just two topics from that week
highlight just two topics from that week zero that have really been manifest for
zero that have really been manifest for the past several weeks namely
the past several weeks namely abstraction like taking complicated
abstraction like taking complicated things and ideas and trying to simplify
things and ideas and trying to simplify them so that we can sort of operate at
them so that we can sort of operate at this level and like solve problems we
this level and like solve problems we care about without getting into the
care about without getting into the weeds of implementation detail so to
weeds of implementation detail so to speak but there's this tension because
speak but there's this tension because you know now from all of these different
you know now from all of these different languages that code is fairly
languages that code is fairly unforgiving I mean even omitting a
unforgiving I mean even omitting a stupid semicolon sometimes breaks
stupid semicolon sometimes breaks everything and so Precision is sort of
everything and so Precision is sort of at odd sometimes with this idea of
at odd sometimes with this idea of leveraging abstraction and so we thought
leveraging abstraction and so we thought we would try to tease this apart
we would try to tease this apart especially all these Weeks Later here uh
especially all these Weeks Later here uh with a bit of a uh live demonstration so
with a bit of a uh live demonstration so on the way in you probably all received
on the way in you probably all received a sheet of paper if not but someone near
a sheet of paper if not but someone near you did just tear theirs in half and
you did just tear theirs in half and borrow half a sheet if you can or any
borrow half a sheet if you can or any piece of loose leaf paper or the like
piece of loose leaf paper or the like will suffice so long as you have a pen
will suffice so long as you have a pen or pencil and for this allow me to
or pencil and for this allow me to propose that we invite up maybe two
propose that we invite up maybe two final cs50 volunteers this semester and
final cs50 volunteers this semester and like a lot of hands are going up in this
like a lot of hands are going up in this okay a lot of hands how about I saw the
okay a lot of hands how about I saw the first hand there uh yes yes who's yes
first hand there uh yes yes who's yes who's pointing at herself now come on
who's pointing at herself now come on down we just need the one hand for now
down we just need the one hand for now but oh wait oh wait uh you'll be our
but oh wait oh wait uh you'll be our number two well okay we have way too
number two well okay we have way too many volunteers now no no please please
many volunteers now no no please please come down yes in the black shirt and if
come down yes in the black shirt and if you guys we will okay we'll do pair
you guys we will okay we'll do pair programming in just a bit if you want to
programming in just a bit if you want to hang out in the wings here we'll have
hang out in the wings here we'll have our second demonstration as well so okay
our second demonstration as well so okay now maybe a round of applause for our
now maybe a round of applause for our three
volunteers oh come on up first oh second and third okay you come first we'll do
and third okay you come first we'll do order no uh this is a q okay Q here okay
order no uh this is a q okay Q here okay what's your name I'm Danny Danny okay
what's your name I'm Danny Danny okay take this mic okay so we will DQ you
take this mic okay so we will DQ you momentarily all right so Danny come on
momentarily all right so Danny come on over to the middle here and in a moment
over to the middle here and in a moment I'm going to hand to Danny a sheet of
I'm going to hand to Danny a sheet of paper that has a picture on it and this
paper that has a picture on it and this picture is going to be something that
picture is going to be something that I'd like you to verbally program the
I'd like you to verbally program the audience to draw you can use any words
audience to draw you can use any words any abstractions any PR level of
any abstractions any PR level of precision that you want but you just
precision that you want but you just can't make hand gestures or sort of show
can't make hand gestures or sort of show them what to draw But first you want to
them what to draw But first you want to tell us a little something about
tell us a little something about yourself including everyone here I'm
yourself including everyone here I'm daddy and I took ts50 okay wonderful
daddy and I took ts50 okay wonderful wonderful so I'm going to reveal the
wonderful so I'm going to reveal the picture only to Danny and if each of you
picture only to Danny and if each of you would like to take out that sheet of
would like to take out that sheet of paper and just make sure that no one
paper and just make sure that no one else can see this if you want to hold it
else can see this if you want to hold it up this way everyone here is now holding
up this way everyone here is now holding their pen or pencil and in some number
their pen or pencil and in some number of steps give them a verbal algorithm
of steps give them a verbal algorithm for drawing what you see and you can say
for drawing what you see and you can say anything you want but no gestures okay
anything you want but no gestures okay so you're going to want to
so you're going to want to draw a square in the center of the paper
draw a square in the center of the paper with the
with the diagonal pointing to
diagonal pointing to the center of the
edge wait no actually I scratched that draw a rbus in the center of your
paper and for those who forget what a rhombus is for um a d
rhombus is for um a d Diamond a a square that's on its
Diamond a a square that's on its side and then from the bottom vertex
side and then from the bottom vertex draw a straight line down but not all
draw a straight line down but not all the way to the edge of the
paper okay and then keep your pencil or pen at that point and you're going to
pen at that point and you're going to want to draw a line that's parallel to
want to draw a line that's parallel to the line of the original rumus to the
right and then keep your pencil or pen at that point and draw a line straight
at that point and draw a line straight up connecting to the side
vertex yes and then go back to the line that you drew from the bottom vertex to
that you drew from the bottom vertex to the bottom of the paper and then draw a
the bottom of the paper and then draw a line parallel to the left edge of the
line parallel to the left edge of the rumus and then then keep your L your
rumus and then then keep your L your paper your pencil at that point and draw
paper your pencil at that point and draw a line up to the vertex of the rumus
again the end the end all right well thank you to Danny hang on to your paper
thank you to Danny hang on to your paper thank you so much and if you want to
thank you so much and if you want to step off to the stage there we will
step off to the stage there we will reveal thank you a round of applause if
reveal thank you a round of applause if we could for Danny that is not an easy
we could for Danny that is not an easy task I'm sure and if Carter wouldn't
task I'm sure and if Carter wouldn't mind just grabbing a few samples here
mind just grabbing a few samples here let's actually take a look on the
let's actually take a look on the overhead if we could I'm going to pop
overhead if we could I'm going to pop down over here real fast we don't need
down over here real fast we don't need to collect them all but if you're
to collect them all but if you're feeling either good or bad with what you
feeling either good or bad with what you drew happy to collect a few of them
drew happy to collect a few of them okay okay thank you thank
okay okay thank you thank you okay hope you won't mind if I can't
you okay hope you won't mind if I can't reach everyone just a couple more okay
reach everyone just a couple more okay over here okay all right that's that's
over here okay all right that's that's okay this one's really funny okay I'm
okay this one's really funny okay I'm going to go with this one if I may and
going to go with this one if I may and Carter has some
Carter has some too
okay thank you so much okay so just a random assortment here let me turn on a
random assortment here let me turn on a camera so I can show you what I see here
camera so I can show you what I see here for instance is one classmate drawing
for instance is one classmate drawing which might resemble perhaps what you
which might resemble perhaps what you drew here uh here is the beginnings of a
drew here uh here is the beginnings of a house it
house it seems nice this
seems nice this one okay this one is larger
one okay this one is larger and how about a couple of others that
and how about a couple of others that were getting closer I think okay so more
were getting closer I think okay so more edges and vertices there this one seems
edges and vertices there this one seems a little similar in
a little similar in spirit if not
spirit if not proportional this is uh Zach's the best
proportional this is uh Zach's the best one but it turns out if I may Zach
one but it turns out if I may Zach you're not all that far off here Denny
you're not all that far off here Denny is what you were reciting to everyone
is what you were reciting to everyone algorithmically indeed it was this here
algorithmically indeed it was this here Cube and so Danny can you come on back
Cube and so Danny can you come on back up for a
up for a moment so if you'd like to share for
moment so if you'd like to share for just a moment like what were some of the
just a moment like what were some of the thoughts going through your head and why
thoughts going through your head and why did you choose the words that you did
did you choose the words that you did okay so what was going through my head
okay so what was going through my head when I saw the cube um I didn't know if
when I saw the cube um I didn't know if I could say draw Cube so I decided to
I could say draw Cube so I decided to start with the top and so draw a Remus
start with the top and so draw a Remus in the center of your paper and then
in the center of your paper and then draw a line down and just like do the
draw a line down and just like do the first part then the second part then the
first part then the second part then the third part and then you would get a cube
third part and then you would get a cube like Zach yeah and so had you said and
like Zach yeah and so had you said and you could have said draw a cube which
you could have said draw a cube which would be more of an abstraction even
would be more of an abstraction even that's not necessarily sufficiently
that's not necessarily sufficiently precise right because you don't
precise right because you don't necessarily know what the orientation of
necessarily know what the orientation of that cube is the size of it the
that cube is the size of it the positioning on the paper so you instead
positioning on the paper so you instead took a lower level approach which is not
took a lower level approach which is not unlike scratch if you think about
unlike scratch if you think about scratch being able to move up down left
scratch being able to move up down left right turn 90 degrees turn 15 degrees
right turn 90 degrees turn 15 degrees and the like I mean that is ultimately
and the like I mean that is ultimately how a lot of graphical programs and
how a lot of graphical programs and games even might be implemented by
games even might be implemented by really focusing at the level you do but
really focusing at the level you do but of course there's this tension with us
of course there's this tension with us humans whereby we prefer often to think
humans whereby we prefer often to think at this level but even that might not be
at this level but even that might not be sufficient which is to say here in week
sufficient which is to say here in week 10 like these are still going to be in
10 like these are still going to be in our hard problems uh but a hand if we
our hard problems uh but a hand if we could one more time for Danny for
could one more time for Danny for getting us that far along thank you so
getting us that far along thank you so much let me give you a stress ball here
much let me give you a stress ball here all right and if we could have both of
all right and if we could have both of our volunteers come up here we're going
our volunteers come up here we're going to have yes come on up come on up and
to have yes come on up come on up and let me uh have you guys introduce
let me uh have you guys introduce yourselves in the middle
yourselves in the middle here hi I'm
here hi I'm hey there I'm sadik from Turkey nice to
hey there I'm sadik from Turkey nice to meet you all wonderful welcome and this
meet you all wonderful welcome and this time we're going to flip it around so as
time we're going to flip it around so as to have the audience do what Danny just
to have the audience do what Danny just did for us the only catch here is that
did for us the only catch here is that the only means we have for showing the
the only means we have for showing the audience what they need to tell you to
audience what they need to tell you to draw is like literally right above the
draw is like literally right above the chalkboard so on our system here that
chalkboard so on our system here that your eyes must stay on the chalkboard
your eyes must stay on the chalkboard and not look up and in just a moment if
and not look up and in just a moment if you guys want to both stand in front of
you guys want to both stand in front of the chalkboard back to the audience and
the chalkboard back to the audience and as you're talking with each other other
as you're talking with each other other verbalize it through the microphone if
verbalize it through the microphone if you will I'm going to show everyone else
you will I'm going to show everyone else in the room a second and final drawing
in the room a second and final drawing and we'll just go rapid fire around the
and we'll just go rapid fire around the room give us one step at a time
room give us one step at a time collectively and we'll see if these guys
collectively and we'll see if these guys can't draw exactly that same outcome so
can't draw exactly that same outcome so is there another chunk what's that is
is there another chunk what's that is there another chunk or just just the one
there another chunk or just just the one so you'll have to collaborate and let's
so you'll have to collaborate and let's give you a clean slate here
give you a clean slate here literally all right so no looking up
literally all right so no looking up that's the only rule for you guys here
that's the only rule for you guys here we go for the audience here is what we'd
we go for the audience here is what we'd like them ironically to
like them ironically to draw step one from anyone in the
draw step one from anyone in the audience yes draw a circle draw a circle
audience yes draw a circle draw a circle anywhere not anywhere not
anywhere okay that's step one step two someone else yeah in the
middle draw a line down from the bottom of the
draw a line down from the bottom of the circle about halfway down I think there
circle about halfway down I think there was a hand in front of you too number
three
[Music] okay okay the overarching goal here for
okay okay the overarching goal here for those unable here is to draw a person
those unable here is to draw a person fig okay it may be a SI figure draw the
fig okay it may be a SI figure draw the left leg of the person of this person
left leg of the person of this person okay good job all right next step four
okay good job all right next step four yeah
yeah of line up Circle
of line up Circle okay the left oh sorry to the right draw
okay the left oh sorry to the right draw a v okay to the right of the vertex at
a v okay to the right of the vertex at the bottom of the circle draw a v draw a
the bottom of the circle draw a v draw a v um like what V nope not interactive
v um like what V nope not interactive draw a v um no no
well yeah it seems Weir weird let's get ready for maybe
weird let's get ready for maybe something like step
something like step five okay we'll go with that step five
five okay we'll go with that step five someone else step five someone else
someone else step five someone else someone else
someone else yeah draw the right side of the leg okay
yeah draw the right side of the leg okay nice step six step six happy face six
nice step six step six happy face six six yes erase the line
erase the line that you have on the left on the
on the okay okay step seven yes instead of like
okay okay step seven yes instead of like that line that was before going up make
that line that was before going up make it go down instead of that line before
it go down instead of that line before going up make it go down mhm okay step
going up make it go down mhm okay step eight step eight step eight yes connect
eight step eight step eight yes connect that line to the hip connect that line
that line to the hip connect that line to the hip not like not touching not
to the hip not like not touching not touching something like this
touching something like this maybe okay compromise not touching okay
maybe okay compromise not touching okay not touching okay all right step nine
not touching okay all right step nine almost there I think step
back uh write the word high on the top left of the circle
left of the circle here okay
here okay and step 10 almost there line draw a
and step 10 almost there line draw a line pointing to high so like a spee
line pointing to high so like a spee bubble basically
bubble basically yeah okay and step uh 10
yeah okay and step uh 10 11
11 yeah erase the exclamation point nice 12
yeah erase the exclamation point nice 12 do we want to give them one
do we want to give them one more 12 or we good yeah last one erase
more 12 or we good yeah last one erase the erase erace the right arm okay I
the erase erace the right arm okay I think we're going to need a 13
think we're going to need a 13 then and
then and then yeah
then yeah repat repeat the left arm but rotate it
repat repeat the left arm but rotate it by
wrong wait how would you like as an organic human being how put your
organic human being how put your arms like would you put your would you
arms like would you put your would you ever structure your arm like that that
ever structure your arm like that that would not be a stick
would not be a stick figure CU would you do this or would you
figure CU would you do this or would you do that or a little hint maybe get a get
do that or a little hint maybe get a get a give me a step 14 step 14 and final
a give me a step 14 step 14 and final step
step 14 I think we just got to tell them what
14 I think we just got to tell them what to do step 14
yes think of a walking man and and have the left the right hand
and and have the left the right hand walking to your right all right so like
walking to your right all right so like it's like where could the hand go where
it's like where could the hand go where should the hand go on that
should the hand go on that arm but yeah yeah okay yes
arm but yeah yeah okay yes no yeah I mean look look look like right
no yeah I mean look look look like right here look right here look right I yes
here look right here look right I yes sorry thank you 14
St that's pretty close so congratulations to you guys and thank
congratulations to you guys and thank you as well all right so I mean these
you as well all right so I mean these these things too are not yes Round of
these things too are not yes Round of Applause then sure
so so this is to say that these ideas of abstraction and precision and really
abstraction and precision and really every other term of art that we explored
every other term of art that we explored this term are sort of omnipresent and
this term are sort of omnipresent and can be easier or harder to implement
can be easier or harder to implement depending on exactly what the problem is
depending on exactly what the problem is but what we thought we' do now in our uh
but what we thought we' do now in our uh final day is try to now similarly
final day is try to now similarly prepare prepare you for life after cs50
prepare prepare you for life after cs50 and this is really going to be a list of
and this is really going to be a list of really potential to-dos so that you can
really potential to-dos so that you can stand on your own after the class after
stand on your own after the class after the class's infrastructure write actual
the class's infrastructure write actual code and then we'll come full circle one
code and then we'll come full circle one final time with our friend Jennifer 8
final time with our friend Jennifer 8 Lee to look at the world of emojis and
Lee to look at the world of emojis and how they relate to all forms of
how they relate to all forms of representation that we've talked to uh
representation that we've talked to uh talked about up until now so one how can
talked about up until now so one how can you go about programming after cs50 so
you go about programming after cs50 so one you can actually install command
one you can actually install command line tools on your own Mac or PC perhaps
line tools on your own Mac or PC perhaps unbeknownst to you Windows has what's
unbeknownst to you Windows has what's generally called a command prompt Mac OS
generally called a command prompt Mac OS literally comes with a terminal program
literally comes with a terminal program in your applications utilities folder
in your applications utilities folder and so even if you've never run those
and so even if you've never run those programs you've actually had a sort of
programs you've actually had a sort of blinking cursor black and white prompt
blinking cursor black and white prompt available to you might not have all of
available to you might not have all of the same software installed as your code
the same software installed as your code space in the cloud but you have that
space in the cloud but you have that command line interface even within
command line interface even within today's graphical tools and among the
today's graphical tools and among the tools you can install within that
tools you can install within that command line interface would be
command line interface would be something called xcode on the Mac which
something called xcode on the Mac which comes not only with a guey IDE
comes not only with a guey IDE integrated development environment but
integrated development environment but also those command line tools and
also those command line tools and Microsoft for Windows has something
Microsoft for Windows has something similar as well learning git so we've
similar as well learning git so we've used git only unbeknownst to you
used git only unbeknownst to you underneath the hood for the most part
underneath the hood for the most part but git is a very very popular tool if
but git is a very very popular tool if challenging to pick up for the first
challenging to pick up for the first time that makes it easy to push code to
time that makes it easy to push code to a website called GitHub or any
a website called GitHub or any equivalent and then collaborate more
equivalent and then collaborate more effectively with classmates there's
effectively with classmates there's definitely a bit of a learning curve but
definitely a bit of a learning curve but thanks to cs50's own Brian youu you can
thanks to cs50's own Brian youu you can start for instance with a video like
start for instance with a video like this and this indeed is going to be one
this and this indeed is going to be one of these deao standards in the real
of these deao standards in the real world at least for the next several
world at least for the next several years that you'll probably encounter if
years that you'll probably encounter if you work in Tech or really any company
you work in Tech or really any company where you're doing some programming vs
where you're doing some programming vs code itself will walk you through this
code itself will walk you through this process in the coming days but you can
process in the coming days but you can indeed install it on your own Mac or PC
indeed install it on your own Mac or PC and what can you do when you write code
and what can you do when you write code well you can certainly write software
well you can certainly write software for your Mac for your PC for your phone
for your Mac for your PC for your phone or of course per week 10 uh week nine
or of course per week 10 uh week nine you can host uh your own website be it
you can host uh your own website be it static as in week uh week eight um
static as in week uh week eight um hosting it at websites like these which
hosting it at websites like these which gener generally have free or
gener generally have free or student-friendly accounts via which you
student-friendly accounts via which you can put something statically on the web
can put something statically on the web at a real domain name that you might
at a real domain name that you might choose or you can host a full-fledged
choose or you can host a full-fledged web app and using uh student tiers on
web app and using uh student tiers on Amazon and Microsoft and Google's cloud
Amazon and Microsoft and Google's cloud services or others you can sign up for
services or others you can sign up for being a student certainly a whole lot of
being a student certainly a whole lot of free software free hosting so as to if
free software free hosting so as to if nothing else um experiment and uh
nothing else um experiment and uh perhaps maximally get your own app or
perhaps maximally get your own app or website up and running so know that
website up and running so know that those are resources available to you and
those are resources available to you and this is by uh certainly a non-exhaustive
this is by uh certainly a non-exhaustive list if you'd like to geek out in the
list if you'd like to geek out in the coming months in the coming years these
coming months in the coming years these are just some of the places that people
are just some of the places that people who take computer science classes who
who take computer science classes who write code might tend to hang out and
write code might tend to hang out and ask and answer questions of each other
ask and answer questions of each other um so keep an eye for instance on these
um so keep an eye for instance on these here and then cs50 has its own
here and then cs50 has its own communities as you'll see if you go to
communities as you'll see if you go to this URL here via the open coreware
this URL here via the open coreware version of cs50 which is open to the
version of cs50 which is open to the world do uh is there a Vibrant Community
world do uh is there a Vibrant Community uh thanks to time zones that's pretty
uh thanks to time zones that's pretty much active 247 365 talking about not
much active 247 365 talking about not only cs50 going on in problem sets and
only cs50 going on in problem sets and projects but really technology more
projects but really technology more generally as well so certainly feel
generally as well so certainly feel welcome to partake either asking or
welcome to partake either asking or answering questions now in speaking of
answering questions now in speaking of asking and answering questions a couple
asking and answering questions a couple of weeks ago you kindly gave us a whole
of weeks ago you kindly gave us a whole bunch of review questions which we
bunch of review questions which we called through and picked out our
called through and picked out our favorite 20 of them these of course were
favorite 20 of them these of course were multiple choice questions and in
multiple choice questions and in preparation for this week uh in
preparation for this week uh in preparation for life ahead we thought we
preparation for life ahead we thought we would choreograph a bit of a a quiz show
would choreograph a bit of a a quiz show here and ceed as you came in at the
here and ceed as you came in at the start of class you might recall being
start of class you might recall being invited to go to this URL here
invited to go to this URL here cs50. either here in person or if you're
cs50. either here in person or if you're watching live from home at this URL here
watching live from home at this URL here you can use a phone or a laptop and if
you can use a phone or a laptop and if it's easier on a phone you can point
it's easier on a phone you can point your camera at this 2D barcode here
your camera at this 2D barcode here we'll give folks a moment to pull that
we'll give folks a moment to pull that up and again that URL was cs50.
l/p and once it looks like most folks have
and once it looks like most folks have it up and running our friend Carter here
it up and running our friend Carter here will help us dive into this uh review
will help us dive into this uh review session if you will with a bit of fun
session if you will with a bit of fun along the way all right Carter if you'd
along the way all right Carter if you'd like to take it away what do we have as
like to take it away what do we have as our first question you should see on
our first question you should see on your phone or laptop this same question
your phone or laptop this same question being asked the first question is how do
being asked the first question is how do you print quote un quote hello world in
you print quote un quote hello world in Python so among the possible answers are
Python so among the possible answers are these here
these here buzz in on your phone or your
laptop we've got a few hundred responses are
are ready 7 Seconds to make your
decision this is question one of 20 go to it with some confidence I think we're
to it with some confidence I think we're down to zero on the clock and Carter it
down to zero on the clock and Carter it looks like 98% of you uh indeed said
looks like 98% of you uh indeed said hello world and Carter per the check
hello world and Carter per the check mark That's indeed the correct answer
mark That's indeed the correct answer here now to make things interesting in
here now to make things interesting in know that you'll see some number of
know that you'll see some number of points and we've deliberately anonymized
points and we've deliberately anonymized it so only you know what number you are
it so only you know what number you are so a whole lot of guests have a perfect
so a whole lot of guests have a perfect score of 1,000 at the moment hopefully
score of 1,000 at the moment hopefully we'll see over the next several
we'll see over the next several questions things start to bridge out uh
questions things start to bridge out uh but know that the speed with which you
but know that the speed with which you buzz in will also factor into how many
buzz in will also factor into how many points you now get So the faster you
points you now get So the faster you move the more points you get question
move the more points you get question two if we could what does DNS stand for
two if we could what does DNS stand for from just a couple of weeks back domain
from just a couple of weeks back domain number system domain name system data
number system domain name system data numbering structure or there's no such
numbering structure or there's no such thing as
thing as DNS few hundred responses are
DNS few hundred responses are in 8 seconds
in 8 seconds remain fewer points now but still a
remain fewer points now but still a chance to buzz in and now as we hit zero
chance to buzz in and now as we hit zero the responses are these domain name
the responses are these domain name system which is indeed correct and 84%
system which is indeed correct and 84% of you got that one correctly and indeed
of you got that one correctly and indeed exists we talked about it a couple of
exists we talked about it a couple of weeks ago so we're still seeing a whole
weeks ago so we're still seeing a whole lot lot of ties at 2,000 we'll see if
lot lot of ties at 2,000 we'll see if someone starts to pull away before long
someone starts to pull away before long question three what is the upper bound
question three what is the upper bound of merge sorts
of merge sorts runtime so that escalated
runtime so that escalated quickly Big O of n log n Big O of log n
quickly Big O of n log n Big O of log n Theta or Omega of log n or Big O of
Theta or Omega of log n or Big O of one what is the upper bound of merge
one what is the upper bound of merge sorts
sorts runtime that was the last of the
runtime that was the last of the algorithms we solve for sorting and in 1
algorithms we solve for sorting and in 1 second we'll see that the correct answer
second we'll see that the correct answer is is just edging out everyone else
is is just edging out everyone else indeed 46% it is n I know it's n log now
indeed 46% it is n I know it's n log now if I may as the teacher it can't be
if I may as the teacher it can't be login because login is strictly less
login because login is strictly less than n and you can't possibly sort n
than n and you can't possibly sort n elements unless you minimally look at or
elements unless you minimally look at or touch each of them so it's got to be at
touch each of them so it's got to be at least greater than n intuitively we
least greater than n intuitively we still have a whole bunch of ties let's
still have a whole bunch of ties let's move on to number four what is stored in
move on to number four what is stored in ARG
ARG C back to the language C is it in Array
C back to the language C is it in Array of arguments the maximum size of an
of arguments the maximum size of an array the count of arguments given to a
array the count of arguments given to a program when first run or how much
program when first run or how much memory is allocated to a
memory is allocated to a function again you wrote all of these
function again you wrote all of these questions and we have 5 seconds for the
questions and we have 5 seconds for the reveal ARG C
reveal ARG C is indeed the count of arguments given
is indeed the count of arguments given to a program when first run think back
to a program when first run think back to C when we did command line arguments
to C when we did command line arguments there was argc and arv argv was the
there was argc and arv argv was the array but Arc was indeed the count the
array but Arc was indeed the count the CN AR save all right we still have a
CN AR save all right we still have a whole bunch of ties at the top here but
whole bunch of ties at the top here but let's move on then to number five what
let's move on then to number five what is the duck debugger favorite hobby
is the duck debugger favorite hobby according to one of your classmates
according to one of your classmates dressing up like Dracula swimming across
dressing up like Dracula swimming across the stage filling up the entire bathroom
the stage filling up the entire bathroom of a guy's house and sitting quietly on
of a guy's house and sitting quietly on stage the third of course is a reference
stage the third of course is a reference to a YouTube video that was on the
to a YouTube video that was on the course's website that week but according
course's website that week but according to your class mate number two seconds
to your class mate number two seconds remaining sitting quietly on stage is
remaining sitting quietly on stage is its favorite pastime so a little harder
its favorite pastime so a little harder perhaps than the
perhaps than the others 5,000 now we're starting to see
others 5,000 now we're starting to see some spread so we only have six six
some spread so we only have six six guests in contention for first place and
guests in contention for first place and the next question now is six what is the
the next question now is six what is the function used to open a file in
function used to open a file in C F open open file open
C F open open file open file
file what is the function used to open a file
what is the function used to open a file in
C 7 Seconds there's some differences between
Seconds there's some differences between C and python here and the reveal it is
C and python here and the reveal it is indeed fop it's 77% correct too all
indeed fop it's 77% correct too all right let's see the rankings now if you
right let's see the rankings now if you are guest 15 9715 6171 3753 or 3273
are guest 15 9715 6171 3753 or 3273 you're now in the lead as we move on on
you're now in the lead as we move on on to se question seven how does Sterling
to se question seven how does Sterling compute the average sorry how does
compute the average sorry how does Sterling compute the length of a string
Sterling compute the length of a string in C it looks at how much memory the
in C it looks at how much memory the string uses it counts the number of
string uses it counts the number of characters until it reaches back0 it
characters until it reaches back0 it counts the number of bits in the string
counts the number of bits in the string or it creates pointers for each
or it creates pointers for each character and counts
character and counts them 10 seconds Sterling in
them 10 seconds Sterling in C recall that we implemented this
C recall that we implemented this ourselves in class but then we used the
ourselves in class but then we used the library thereafter and in indeed with
library thereafter and in indeed with 85% it simply counts the number of
85% it simply counts the number of characters until it reaches that
characters until it reaches that Sentinel back sl0 AKA null and in this
Sentinel back sl0 AKA null and in this case we
case we have five four four of you tied now for
have five four four of you tied now for first all right question eight where
first all right question eight where does Malo allocate memory
does Malo allocate memory from the stack the Heap the pointers or
from the stack the Heap the pointers or the
the temp where does malok allocate memory
temp where does malok allocate memory from responses are coming in 8
seconds a good review question at that in two seconds we'll see that malok
in two seconds we'll see that malok allocates memory from woo close one the
allocates memory from woo close one the Heap is correct the Heap is correct the
Heap is correct the Heap is correct the stack recall is where functions store
stack recall is where functions store their local variables and their
their local variables and their arguments and that just happens
arguments and that just happens automatically the Heap represented in
automatically the Heap represented in our pictures up top is where malok draws
our pictures up top is where malok draws from now we have guest
from now we have guest 15 has
15 has made its way to the top here but others
made its way to the top here but others can catch up if they don't buzz in fast
can catch up if they don't buzz in fast enough so number nine how many people
enough so number nine how many people flew from 50v to New York on the day of
flew from 50v to New York on the day of the
the crime 16 29 8 or
three anyone with a laptop perhaps has an advantage
here 5 Seconds and the answers are but the answer is
and the answers are but the answer is 16 let's see if guest 15 got
16 let's see if guest 15 got this they did not goodbye to guest 15 at
this they did not goodbye to guest 15 at the top all right question 10 we're
the top all right question 10 we're about halfway there what are meta tags
about halfway there what are meta tags used for in
used for in HTML to describe a web page to Define
HTML to describe a web page to Define parameters for an element to group
parameters for an element to group elements together to translate content
elements together to translate content into machine readable format
into machine readable format at what are meta tags used for in HTML
at what are meta tags used for in HTML we saw a few of
we saw a few of them for different use
them for different use cases and with one second we see that
cases and with one second we see that indeed the number one answer was to
indeed the number one answer was to describe the web page be it for a mobile
describe the web page be it for a mobile device be it for screen scrapers like
device be it for screen scrapers like Facebook and Twitter and other such apps
Facebook and Twitter and other such apps that grab images and descriptions
that grab images and descriptions thereof all right we're in the second
thereof all right we're in the second half now guest nine guest four 669 has
half now guest nine guest four 669 has edged ahead guess 15 is now in sixth
edged ahead guess 15 is now in sixth place all right number 11 is how do you
place all right number 11 is how do you find the address of a variable in C
find the address of a variable in C think back a few weeks star dollar sign
think back a few weeks star dollar sign Ampersand or
ask from one of your own classmates how do you find the address of a variable in
C and the number one answer is ersan which is indeed the address of operator
which is indeed the address of operator at 62% nicely done let's see who's the
at 62% nicely done let's see who's the top of the list now guess 4669 has
top of the list now guess 4669 has retained their lead so we move on to 12
retained their lead so we move on to 12 what does the arrow operator mean in C a
what does the arrow operator mean in C a hyphen and a greater than sign nothing
hyphen and a greater than sign nothing starts a comment replaces a star and Dot
starts a comment replaces a star and Dot operator declares a
operator declares a pointer what does this Arrow operator
pointer what does this Arrow operator mean in
mean in C
C again from a few weeks back 3 seconds
again from a few weeks back 3 seconds harder assortment perhaps and it's oh
harder assortment perhaps and it's oh replaces a star and Dot operator the
replaces a star and Dot operator the number two answer was indeed correct
number two answer was indeed correct this was just a cleaner way syntactic
this was just a cleaner way syntactic sugar for collapsing what would be a
sugar for collapsing what would be a star and then some parentheses and then
star and then some parentheses and then a DOT into quite simply something that
a DOT into quite simply something that looks like an arrow itself all right
looks like an arrow itself all right Carter who's in the lead now still that
Carter who's in the lead now still that same guest and let's see what 13 has for
same guest and let's see what 13 has for us which of these is not a data type in
us which of these is not a data type in SQL light blob string integer
text we used a few of these more commonly than others but not all of
commonly than others but not all of these are for
these are for real 5 Seconds to make your
real 5 Seconds to make your decision and the results are blob is a
decision and the results are blob is a thing string is not in SQL light it's of
thing string is not in SQL light it's of course called text as we've seen it blob
course called text as we've seen it blob as goofy as it sounds is just binary
as goofy as it sounds is just binary large object but indeed it's how you
large object but indeed it's how you might store a binary file in your
might store a binary file in your database all right the rankings now oh
database all right the rankings now oh guest 8444 has Eed ahead so we move on
guest 8444 has Eed ahead so we move on to 14 which of the following is a valid
to 14 which of the following is a valid way to print Exclamation point
way to print Exclamation point Exclamation point Exclamation point
Exclamation point Exclamation point Exclamation point in
Exclamation point in Python I'll let you read these
Python I'll let you read these yourselves
yourselves which is a valid
which is a valid way everyone got quiet in is
thinking all right 6 seconds few hundred responses in so
responses in so far all right and yes nicely done 78% is
far all right and yes nicely done 78% is correct you can use the star operator to
correct you can use the star operator to essentially multiply the character at
essentially multiply the character at left all right who's in the lead Carter
left all right who's in the lead Carter we now have still G guest
we now have still G guest 8444 and so we move on now to 15 what
8444 and so we move on now to 15 what does the free function do D allocates
does the free function do D allocates memory from A Primitive deallocates
memory from A Primitive deallocates memory at the given pointer terminates a
memory at the given pointer terminates a loop or returns a value from the
loop or returns a value from the function what does the free function
do all right 5 Seconds what does the free function do call it's the opposite
free function do call it's the opposite essentially of malok and it deallocates
essentially of malok and it deallocates the memory at the given pointer as well
the memory at the given pointer as well and in C that's on you in Python you
and in C that's on you in Python you don't need to worry about allocating or
don't need to worry about allocating or freeing aik all right we now have at the
freeing aik all right we now have at the top still guest 8444 and seeing more and
top still guest 8444 and seeing more and more spread 16 which is not a step of
more spread 16 which is not a step of compiling think back now to week two
compiling think back now to week two compiling pre-processing linking or
compiling pre-processing linking or threading which is not a step of
threading which is not a step of compiling we use it as a catchall but it
compiling we use it as a catchall but it technically means a few different things
collectively all right and threading is indeed not on the list that is a
indeed not on the list that is a technical thing generally meaning a
technical thing generally meaning a program can do multiple things at once
program can do multiple things at once but that is not related here to
but that is not related here to compiling all right guest 8444 is still
compiling all right guest 8444 is still at the top we have just a few questions
at the top we have just a few questions left and so 17 what was the surprise at
left and so 17 what was the surprise at the beginning of the Halloween lecture
the beginning of the Halloween lecture according to your classmates someone
according to your classmates someone scared me someone hid candy under every
scared me someone hid candy under every chair someone dressed up as me the
chair someone dressed up as me the entire staff dressed up as
Carter interesting litmus test of who came to or watch lecture
came to or watch lecture perhaps let's see which guests got this
perhaps let's see which guests got this correct someone someone's really dressed
correct someone someone's really dressed up as indeed me at 64% so I think we
up as indeed me at 64% so I think we have attendance here essentially from
have attendance here essentially from that lecture so let's move on now to the
that lecture so let's move on now to the final few questions after seeing that
final few questions after seeing that guest a 8444 8444 is still doing well
guest a 8444 8444 is still doing well can anyone Dethrone them why is it
can anyone Dethrone them why is it incorrect to use the equals equals
incorrect to use the equals equals operator in C to compare
operator in C to compare strings it's computationally inefficient
strings it's computationally inefficient you comparing the locations of the
you comparing the locations of the strings strings don't exist using equal
strings strings don't exist using equal equals will lead to buffer
overflow why is it incorrect to use equal equals in C to compare strings 3
equal equals in C to compare strings 3 seconds we saw this live and it
seconds we saw this live and it motivated like an entire week because
motivated like an entire week because you're comparing the locations that is
you're comparing the locations that is the addresses in memory of those same
the addresses in memory of those same strings all right let's see if this
strings all right let's see if this leaves the rankings the same guest 8444
leaves the rankings the same guest 8444 is still at the top and pulling ahead
is still at the top and pulling ahead two final questions 19 what is the
two final questions 19 what is the difference between null one L and null 2
difference between null one L and null 2 L's they null and null mean the same
L's they null and null mean the same thing nulls refers to back sl0 whereas
thing nulls refers to back sl0 whereas null to L's is the zero address null is
null to L's is the zero address null is the zero address whereas null to L's
the zero address whereas null to L's refers to back sl0 null is null but
refers to back sl0 null is null but lazier 5
lazier 5 Seconds subtle not the best design
Seconds subtle not the best design perhaps to have in technical terms but
perhaps to have in technical terms but indeed 62% of you got that NL is the
indeed 62% of you got that NL is the first thing we talked about when we
first thing we talked about when we talked about back sl0 and N is a pointer
talked about back sl0 and N is a pointer it's the zero pointer same thing same
it's the zero pointer same thing same number but different context all right
number but different context all right Carter guest 8444 is the person to beat
Carter guest 8444 is the person to beat with our final 20th questions what do
with our final 20th questions what do the binary bulbs on stage spell
today and these are your four choices different from usual we usually use 8
different from usual we usually use 8 bit asky today we are using utf8 which
bit asky today we are using utf8 which is a form of Unicode which is the larger
is a form of Unicode which is the larger subset that uses one or two or three or
subset that uses one or two or three or even four bites to spell a single
even four bites to spell a single character and the answer wow close close
character and the answer wow close close is indeed a cupcake indeed a cupcake
is indeed a cupcake indeed a cupcake well done and let's see the final
well done and let's see the final results 8444 is the winner are they here
results 8444 is the winner are they here in person perhaps
in person perhaps 8444 you're 844 come on
8444 you're 844 come on [Applause]
down thank you here you go congratulations oh you're all right so
congratulations oh you're all right so today if we
today if we may give me just one
right all right so today we are so pleased to be joined by uh Jennifer 8
pleased to be joined by uh Jennifer 8 Lee who's an of the college a dear
Lee who's an of the college a dear friend and is actually really the reason
friend and is actually really the reason why there's evidence of Muppets in cs50
why there's evidence of Muppets in cs50 in fact some years ago I was visiting
in fact some years ago I was visiting her and she had on her shelf like this
her and she had on her shelf like this custom Muppet it wasn't one that appears
custom Muppet it wasn't one that appears on TV but she had somehow gone on a
on TV but she had somehow gone on a website former toy store called FAO
website former toy store called FAO Schwarz at the time and you're allowed
Schwarz at the time and you're allowed to configure your own Muppet whatnot
to configure your own Muppet whatnot choose the eyes the nose the face and
choose the eyes the nose the face and the Torso and I just thought this was
the Torso and I just thought this was the coolest thing and so in the taxi on
the coolest thing and so in the taxi on the way home I was like going on the
the way home I was like going on the website trying to purchase our very
website trying to purchase our very first Muppet I then woke up the next
first Muppet I then woke up the next morning thinking why did I just buy a
morning thinking why did I just buy a puppet in the back of a taxi and so it
puppet in the back of a taxi and so it sat on the shelf for really 2 years and
sat on the shelf for really 2 years and then a colleague of mine within cs50's
then a colleague of mine within cs50's team decided after I brought it into the
team decided after I brought it into the office to sit on a shelf there to
office to sit on a shelf there to actually bring it to life and indeed if
actually bring it to life and indeed if you Google around cs50 Muppet and
you Google around cs50 Muppet and puppetry online you'll see in fact these
puppetry online you'll see in fact these as characters not only over the past
as characters not only over the past couple of years in coid times when
couple of years in coid times when really there was next to no one actually
really there was next to no one actually here and so they were instead um but
here and so they were instead um but indeed she's brought not only this this
indeed she's brought not only this this educational element this pedagogical
educational element this pedagogical element this playful element El to cs50
element this playful element El to cs50 and we have her here today to speak to
and we have her here today to speak to exactly the sorts of encodings that are
exactly the sorts of encodings that are here on stage Jenny is the former Vice
here on stage Jenny is the former Vice chair of the Unicode subcommittee on
chair of the Unicode subcommittee on emoji which is to say that she and her
emoji which is to say that she and her colleagues have been influential in
colleagues have been influential in taking emoji from what was a very
taking emoji from what was a very limited character set early on and by
limited character set early on and by far unrepresentative of much uh human
far unrepresentative of much uh human emotion in speech into really an
emotion in speech into really an initiative now to capture digitally all
initiative now to capture digitally all of the world's languages past present
of the world's languages past present and future as well as the range of
and future as well as the range of emotions that might see here in the form
emotions that might see here in the form of that pillow or even in the cake that
of that pillow or even in the cake that awaits so allow me to introduce Jennifer
awaits so allow me to introduce Jennifer 8 Lee thank
8 Lee thank [Music]
[Music] you I much drink okay clicker hi all
you I much drink okay clicker hi all right hold on I have to hide my drinks I
right hold on I have to hide my drinks I might need more water all right um I'm
might need more water all right um I'm really excited to speak to speak here
really excited to speak to speak here last time last year I was here uh one I
last time last year I was here uh one I was wearing a mask which is like a real
was wearing a mask which is like a real bummer if you're lecturing and then the
bummer if you're lecturing and then the entire like front part was all Muppet so
entire like front part was all Muppet so I'm really happy to see humans actually
I'm really happy to see humans actually um and it's always an honor to speak at
um and it's always an honor to speak at Sanders and and then Dave and I were
Sanders and and then Dave and I were actually classmates way back when so I
actually classmates way back when so I do remember him when he was an undergrad
do remember him when he was an undergrad much like you so I um I'm going to give
much like you so I um I'm going to give a talk on the world of emoji and how I
a talk on the world of emoji and how I kind of became an emoji activist so it
kind of became an emoji activist so it kind of all starts um with my friend e
kind of all starts um with my friend e inl who is a designer who's well known
inl who is a designer who's well known for doing the Twitter fail whale which
for doing the Twitter fail whale which was kind of laid to rest except now this
was kind of laid to rest except now this week it seems like it may be like may be
week it seems like it may be like may be necessary again um so she and I one day
necessary again um so she and I one day were texting this is back in 2015 we're
were texting this is back in 2015 we're texting about dumplings because we are
texting about dumplings because we are Chinese is women and we like like to
Chinese is women and we like like to text about food and so I sent her this
text about food and so I sent her this picture of dumplings she was like yum
picture of dumplings she was like yum yum yum yum yum yum yum yum um and then
yum yum yum yum yum yum yum um and then she was like oh Apple doesn't have a
she was like oh Apple doesn't have a dumpling emoji and I was like oh that's
dumpling emoji and I was like oh that's kind of interesting and didn't really
kind of interesting and didn't really think anything about it because like you
think anything about it because like you know people Point things out to you all
know people Point things out to you all the time and then you just like forget
the time and then you just like forget you just like move on but then half an
you just like move on but then half an hour later um on my phone appears this
hour later um on my phone appears this like dumpling with hard eyes and you
like dumpling with hard eyes and you don't see it in in because it's a still
don't see it in in because it's a still shot but it actually had like blinking
shot but it actually had like blinking eyes so she liked to call it like bling
eyes so she liked to call it like bling bling dumpling so she as a designer had
bling dumpling so she as a designer had decided to like go in and like make her
decided to like go in and like make her own dumpling Emoji um because she was
own dumpling Emoji um because she was like I'm a designer I can fix it but
like I'm a designer I can fix it but that actually got me thinking I was like
that actually got me thinking I was like where do Emoji come from and like how is
where do Emoji come from and like how is there not a dumpling Emoji cuz from my
there not a dumpling Emoji cuz from my perspective dumplings are this kind of
perspective dumplings are this kind of universal food right so and there are a
universal food right so and there are a lot of Japanese Foods
lot of Japanese Foods on the emoji keyboard and I I was not
on the emoji keyboard and I I was not this was like back in 2015 I was like
this was like back in 2015 I was like not a big Emoji user like at all so I
not a big Emoji user like at all so I mean you have things like Ramen you have
mean you have things like Ramen you have Bento boxes you have Curry you have
Bento boxes you have Curry you have tempura you even have like kind of
tempura you even have like kind of obscure um kind of foods like this thing
obscure um kind of foods like this thing things on a stick turns out to be fish
things on a stick turns out to be fish fish things on a stick then this pink
fish things on a stick then this pink and white white swirly thing is also a
and white white swirly thing is also a fish thing and there's even like that
fish thing and there's even like that triangle rice ball that looks like it's
triangle rice ball that looks like it's had a bikini wax all well represented on
had a bikini wax all well represented on the Emoji Keyboard but no dumplings and
the Emoji Keyboard but no dumplings and it's very strange cuz like all cultures
it's very strange cuz like all cultures kind of have their dumpling right
kind of have their dumpling right whether or not it's um kinali or ravioli
whether or not it's um kinali or ravioli or aanas like essentially everyone sort
or aanas like essentially everyone sort of like discover the idea of like um
of like discover the idea of like um yummy goodness inside a carbo hydrate
yummy goodness inside a carbo hydrate carbohydrate shell whether not baked or
carbohydrate shell whether not baked or fried or um steamed so I was like okay I
fried or um steamed so I was like okay I literally Google I was like who controls
literally Google I was like who controls emoji and you discover that they're
emoji and you discover that they're actually regulated by a nonprofit called
actually regulated by a nonprofit called the Unicode Consortium and um it is you
the Unicode Consortium and um it is you know and I just like went on their
know and I just like went on their website and I discovered that they had
website and I discovered that they had 12 full voting members as of 2015 so
12 full voting members as of 2015 so this is 2015 and they were like mostly
this is 2015 and they were like mostly us multinational tech companies it was
us multinational tech companies it was Oracle it was IBM Microsoft Adobe Google
Oracle it was IBM Microsoft Adobe Google Apple Facebook and Yahoo and of the
Apple Facebook and Yahoo and of the three that were not multinational US
three that were not multinational US tech companies they were let's see a
tech companies they were let's see a German company called sap a Chinese
German company called sap a Chinese Telecom company called Huawei and then
Telecom company called Huawei and then like the government of Oman like those
like the government of Oman like those were basically the 12 full voting
were basically the 12 full voting members of the US multi-national tech
members of the US multi-national tech companies so they at that point paid um
companies so they at that point paid um $118,000 a year to have full voting
$118,000 a year to have full voting power on the you know Unicode committee
power on the you know Unicode committee and I was like oh that's a that's a lot
and I was like oh that's a that's a lot of money and I kind of felt indignant
of money and I kind of felt indignant about this and uh but then like if you
about this and uh but then like if you kind of keep on digging on their website
kind of keep on digging on their website you found like there was this kind of
you found like there was this kind of interesting loophole which is you could
interesting loophole which is you could join as an individual for
join as an individual for $75 um you don't get voting power but it
$75 um you don't get voting power but it gave you the right to put yourself on
gave you the right to put yourself on the email list and also to like attend
the email list and also to like attend the quarterly Unicode meeting so I was
the quarterly Unicode meeting so I was like I'll do that I had no idea what I
like I'll do that I had no idea what I was doing but I'm like I'm going to go
was doing but I'm like I'm going to go fight um for this dumpling Emoji because
fight um for this dumpling Emoji because from my perspective dumplings are
from my perspective dumplings are Universal Emoji are kind of universal so
Universal Emoji are kind of universal so uh the fact there was no dumpling Emoji
uh the fact there was no dumpling Emoji meant like something was wrong in the
meant like something was wrong in the universe and I was I was determined to
universe and I was I was determined to fix this so um you know I was on this
fix this so um you know I was on this email list and then a couple of like
email list and then a couple of like maybe even like a couple weeks later I
maybe even like a couple weeks later I got you know they they they kind of sent
got you know they they they kind of sent out this note that's like hey who's
out this note that's like hey who's coming to the quarterly meeting and I
coming to the quarterly meeting and I was like um I looked at the calendar I
was like um I looked at the calendar I looked at my schedule I was like oh I'll
looked at my schedule I was like oh I'll be in like you know Silicon Valley that
be in like you know Silicon Valley that time so I basically like rsvpd and I was
time so I basically like rsvpd and I was like I will be there and took um cow
like I will be there and took um cow train to an Apple building it's a legal
train to an Apple building it's a legal building in I think it was sunny Veil so
building in I think it was sunny Veil so I just like show up and I don't know
I just like show up and I don't know what I was sort of expecting like with
what I was sort of expecting like with like you know the Unicode I think maybe
like you know the Unicode I think maybe thought it was going to be like a baby
thought it was going to be like a baby Congress like you know like with a
Congress like you know like with a little you know very formal seats people
little you know very formal seats people with gabbles um that is not what I found
with gabbles um that is not what I found uh basically it is a it was a conference
uh basically it is a it was a conference room full of people who skewed wider
room full of people who skewed wider skewed older skewed Mal skewed engineers
skewed older skewed Mal skewed engineers and this is basically the room where it
and this is basically the room where it happen so this is 2015 these were the
happen so this is 2015 these were the people who decided your Emoji um all
people who decided your Emoji um all very nice and um there was you know one
very nice and um there was you know one one even had a daughter who had a sense
one even had a daughter who had a sense of humor and made him a shirt that said
of humor and made him a shirt that said shadowy Emoji Overlord um so I just kind
shadowy Emoji Overlord um so I just kind of listen to them debate things like
of listen to them debate things like milk emoji and beans emoji and it just
milk emoji and beans emoji and it just seemed like not quite right to me that
seemed like not quite right to me that like it would be uh This Global visual
like it would be uh This Global visual language that were basically decided
language that were basically decided decided by like a a small group of
decided by like a a small group of people inside a conference room in
people inside a conference room in Silicon Valley so I decided to former
Silicon Valley so I decided to former group called Emoji nation whose motto is
group called Emoji nation whose motto is like Emoji by the people for the people
like Emoji by the people for the people and it basically advocates for more kind
and it basically advocates for more kind of Representative inclusive Emoji um you
of Representative inclusive Emoji um you know we we we we start with a
know we we we we start with a Kickstarter campaign uh dumpling Emoji
Kickstarter campaign uh dumpling Emoji process trying to like you know write
process trying to like you know write the wrong in this world and uh made this
the wrong in this world and uh made this little cute video sort of advocating
little cute video sort of advocating dink for one of the most universal
dink for one of the most universal crosscultural Foods in the world Georgia
crosscultural Foods in the world Georgia has Kali Japan has giosa Korea has Mandu
has Kali Japan has giosa Korea has Mandu Italy has ravioli Poland has barog
Italy has ravioli Poland has barog Russia has pelman Argentina has
Russia has pelman Argentina has empanadas Jewish people have kler China
empanadas Jewish people have kler China has pot stickers Nepal and Tibet have
has pot stickers Nepal and Tibet have Momos yet somehow despite their
Momos yet somehow despite their popularity there is no dumpling Emoji in
popularity there is no dumpling Emoji in the standard set why is that emoji
the standard set why is that emoji exists for pizza tempora Sushi spaghetti
exists for pizza tempora Sushi spaghetti hot dog and now tacos which Taco Bell
hot dog and now tacos which Taco Bell takes credit for we need to write this
takes credit for we need to write this disparity dumplings are Global Emoji a
disparity dumplings are Global Emoji a global isn't it time we brought them
global isn't it time we brought them together
together oh yeah and while we're at it how about
oh yeah and while we're at it how about an emoji for Chinese
takeout so uh I did put together a dumpling Emoji proposal I wrote this uh
dumpling Emoji proposal I wrote this uh I remember Thanksgiving Day 2015 on a
I remember Thanksgiving Day 2015 on a plane and uh actually and we got it past
plane and uh actually and we got it past basically dumpling takeout box
basically dumpling takeout box Chopsticks and uh fortune cookie I have
Chopsticks and uh fortune cookie I have to say I don't think fortune cookie
to say I don't think fortune cookie would have made it on its own merits but
would have made it on its own merits but it kind of like slid in on the clo tails
it kind of like slid in on the clo tails of the other ones um and so these were
of the other ones um and so these were the proposals as we submitted them and
the proposals as we submitted them and then these are the ones that kind of uh
then these are the ones that kind of uh exist now on the Apple keyboard and I
exist now on the Apple keyboard and I have to say the dumpling looks really
have to say the dumpling looks really really realistic um oddly realistic and
really realistic um oddly realistic and whereas like the fortune cookie is think
whereas like the fortune cookie is think it's like a big fail because first of
it's like a big fail because first of all there's like it has no it has no Gap
all there's like it has no it has no Gap it looks like a dead 3D Pac-Man so I'm
it looks like a dead 3D Pac-Man so I'm very
very disappointed in uh the manifestation of
disappointed in uh the manifestation of that but that's okay that's okay um and
that but that's okay that's okay um and so it's kind of interesting like what is
so it's kind of interesting like what is the process of getting an emoji um ped
the process of getting an emoji um ped and I will sort of walk you through it
and I will sort of walk you through it so first of all you come up with your
so first of all you come up with your idea right and then you kind of write
idea right and then you kind of write this proposal and then you submit it to
this proposal and then you submit it to the Unicode Emoji subcommittee who then
the Unicode Emoji subcommittee who then kind of gives you comments and then
kind of gives you comments and then sends it back to you and you kind of go
sends it back to you and you kind of go around and around in the circle um and
around and around in the circle um and when so these are things that we
when so these are things that we consider so somewhere in there I also
consider so somewhere in there I also like fought my way on to the Emoji
like fought my way on to the Emoji committee and then also became a vice
committee and then also became a vice chair became a vice chair sort of a
chair became a vice chair sort of a extracurricular that's like completely
extracurricular that's like completely run a muck in my life um um so things
run a muck in my life um um so things that matter uh popular demand is a
that matter uh popular demand is a frequently requested Emoji um multiple
frequently requested Emoji um multiple usages usages and meaning so that's
usages usages and meaning so that's actually kind of very important for
actually kind of very important for something like you know certain animals
something like you know certain animals have meaning so we did you know sloth a
have meaning so we did you know sloth a while ago and that also has not only the
while ago and that also has not only the literal meaning but sort of like um like
literal meaning but sort of like um like um connotations there are visually
um connotations there are visually distinctiveness it can be recognized so
distinctiveness it can be recognized so this was a blocker for one of the uh
this was a blocker for one of the uh emojis that I have worked on before
emojis that I have worked on before which is cave which is kind of hard to
which is cave which is kind of hard to do I think you know meaning wise is
do I think you know meaning wise is pretty good but it's really hard to get
pretty good but it's really hard to get a cave down in emoji sizes and then it
a cave down in emoji sizes and then it filling a gap completeness so a good
filling a gap completeness so a good example for that is like for some reason
example for that is like for some reason for many years we had Red Heart yellow
for many years we had Red Heart yellow heart green heart blue heart purple
heart green heart blue heart purple heart and uh no orange heart so so
heart and uh no orange heart so so somewhat so you know you would do the
somewhat so you know you would do the rainbow but people would substitute the
rainbow but people would substitute the pumpkin in so You' like you know have a
pumpkin in so You' like you know have a have a rainbow heart thing with a little
have a rainbow heart thing with a little pumpkin stuck in the middle so orange
pumpkin stuck in the middle so orange heart obviously should be added and give
heart obviously should be added and give a sense of completeness um and then
a sense of completeness um and then something else is existing vendor cap
something else is existing vendor cap cap compatibility and so a good example
cap compatibility and so a good example for that was um many years ago what's
for that was um many years ago what's app decided to uh add the gender
app decided to uh add the gender non-binary emoji and then once it did
non-binary emoji and then once it did that then all the other kind of vendors
that then all the other kind of vendors um jumped on um so what kind of knocks
um jumped on um so what kind of knocks out an emoji so too specific or narrow
out an emoji so too specific or narrow so we'll often see that with like very
so we'll often see that with like very specific animals or a very specific
specific animals or a very specific group it's redundant so one
group it's redundant so one year oh my God who makes that Butterball
year oh my God who makes that Butterball Butterball makes the turkeys Butterball
Butterball makes the turkeys Butterball submitted um a an emoji proposal that
submitted um a an emoji proposal that was like a cooked turkey but we already
was like a cooked turkey but we already had a live turkey so it seem kind of
had a live turkey so it seem kind of redundant have both like a cooked turkey
redundant have both like a cooked turkey for Thanksgiving and a live turkey so so
for Thanksgiving and a live turkey so so not visually discernible um this is a
not visually discernible um this is a struggle for things like I don't my
struggle for things like I don't my friends have kind of proposed
friends have kind of proposed kimchi kimchi is is really hard on emoji
kimchi kimchi is is really hard on emoji sizes for many reasons and part of that
sizes for many reasons and part of that but part of that tension is because it's
but part of that tension is because it's not visually discernable then there are
not visually discernable then there are no logos Brands deities or celebrities
no logos Brands deities or celebrities so no Nike swish no McDonald's M and
so no Nike swish no McDonald's M and then this is one that we kind of decided
then this is one that we kind of decided in the last uh year or so which is no
in the last uh year or so which is no more flags flags are a very complicated
more flags flags are a very complicated thing um and as a result Unico does not
thing um and as a result Unico does not want to be in the business of deciding
want to be in the business of deciding what is a country or not a country uh so
what is a country or not a country uh so like you know when you get a proposal
like you know when you get a proposal from like Kurdistan you're like yeah so
from like Kurdistan you're like yeah so right now the way that the Emoji flags
right now the way that the Emoji flags are decided is they kind of depend on
are decided is they kind of depend on what the UN recognizes and then those
what the UN recognizes and then those get passed down to the international
get passed down to the international standards organization and then and then
standards organization and then and then Uno just does that like it does not want
Uno just does that like it does not want to be in the business of kind of you
to be in the business of kind of you know geopolitical
know geopolitical Affairs um so once it comes out of the
Affairs um so once it comes out of the subcommittee it goes to the full Unicode
subcommittee it goes to the full Unicode technical committee UTC those were the
technical committee UTC those were the people that were in the room that I
people that were in the room that I showed you um and they vote once a year
showed you um and they vote once a year basically to pass all the Emoji and
basically to pass all the Emoji and takes a while there's a lot of like
takes a while there's a lot of like coordinating with the international
coordinating with the international standards organization and like a lot of
standards organization and like a lot of working with the companies and
working with the companies and eventually it takes a long time but it
eventually it takes a long time but it ends up on your on your devices and that
ends up on your on your devices and that historically is taken about 18 to 24
historically is taken about 18 to 24 months from when you put in the propose
months from when you put in the propose to when you get it in it's going to be a
to when you get it in it's going to be a little bit longer um going forward
little bit longer um going forward probably is my sense as we were're going
probably is my sense as we were're going to Unicode has decided to sort of space
to Unicode has decided to sort of space out some of the Unicode releases so yeah
out some of the Unicode releases so yeah Emoji Nation you know kind of has done
Emoji Nation you know kind of has done its thing so of the weird things is like
its thing so of the weird things is like how did Unicode this like kind of
how did Unicode this like kind of nonprofit organization based in Mountain
nonprofit organization based in Mountain View California end up controlling this
View California end up controlling this like Global visual language um so a lot
like Global visual language um so a lot of it has to start with has has to do
of it has to start with has has to do with the fact that emoji started in
with the fact that emoji started in Japan uh back in the late 1990s one of
Japan uh back in the late 1990s one of the this set from dok 1999 is considered
the this set from dok 1999 is considered is widely considered sort of like the
is widely considered sort of like the first like color Emoji set it has been
first like color Emoji set it has been collected by the Museum of Modern Art
collected by the Museum of Modern Art and um so these the Japanese telecom
and um so these the Japanese telecom companies would use
companies would use basically would have their own sets of
basically would have their own sets of emoji and then they were different
emoji and then they were different companies so they would have different
companies so they would have different sets so you could basically only send
sets so you could basically only send these like visual characters with
these like visual characters with someone who is on your same carrier so
someone who is on your same carrier so it's like it's like basically equivalent
it's like it's like basically equivalent of if you were on like Verizon you can
of if you were on like Verizon you can only text people on Verizon with like
only text people on Verizon with like Emoji or if you were on um T-Mobile you
Emoji or if you were on um T-Mobile you could only do that so at a certain point
could only do that so at a certain point they um decided they were they the Apple
they um decided they were they the Apple and Google came into Japan they wanted
and Google came into Japan they wanted to start selling smartphones and they
to start selling smartphones and they realized that it was a hodgepodge of
realized that it was a hodgepodge of systems and they wanted to unify it and
systems and they wanted to unify it and so in 2007 they went to unic code and
so in 2007 they went to unic code and they're like okay help us unify the
they're like okay help us unify the Emoji um like kind of like basically all
Emoji um like kind of like basically all the Emojis so that we have one standard
the Emojis so that we have one standard system and part of the reason is why
system and part of the reason is why Unicode because Unicode basically has
Unicode because Unicode basically has this mission is to enable everyone
this mission is to enable everyone speaking every language on Earth to be
speaking every language on Earth to be able to use your languages on computer
able to use your languages on computer and smartphone so it basically unifies
and smartphone so it basically unifies um all written languages into one
um all written languages into one ginormous set and that was not the case
ginormous set and that was not the case actually when I was uh growing up there
actually when I was uh growing up there was a point where like if you you were
was a point where like if you you were Japanese on Apple that would be
Japanese on Apple that would be different than Japanese on um dos or
different than Japanese on um dos or like Chinese or Arabic so it drove
like Chinese or Arabic so it drove everyone crazy and they basically
everyone crazy and they basically decided um around the late 80s early
decided um around the late 80s early 1990s that they were going to come up
1990s that they were going to come up with one standardized system that sort
with one standardized system that sort of encoded all characters in in one
of encoded all characters in in one ginormous set so um there's three main
ginormous set so um there's three main projects for Unicode if you care so one
projects for Unicode if you care so one is encoding characters including Emoji
is encoding characters including Emoji now they're about 100,000 characters
now they're about 100,000 characters aside assigned so that includes like
aside assigned so that includes like like Chinese Japanese Korean uh Arabic
like Chinese Japanese Korean uh Arabic cilic um actually all the hieroglyphics
cilic um actually all the hieroglyphics all of the Emoji um a lot of things like
all of the Emoji um a lot of things like the Bitcoin symbol or like copy left or
the Bitcoin symbol or like copy left or whatever those all assigned were about
whatever those all assigned were about 100,000 uh characters even those
100,000 uh characters even those languages that are basically out of use
languages that are basically out of use so the other thing it does it creates
so the other thing it does it creates um localization resources so things
um localization resources so things things so that you know like oh if
things so that you know like oh if you're in this country this is this is
you're in this country this is this is uh you're using the Euro or you're using
uh you're using the Euro or you're using the pesos or something so there's a lot
the pesos or something so there's a lot of localization that is that data that
of localization that is that data that is needed depending on which um
is needed depending on which um geography you're using your device from
geography you're using your device from or like that you know you you know that
or like that you know you you know that the time is used this way or the dates
the time is used this way or the dates are shown that way so that is called um
are shown that way so that is called um the common local data repository or cldr
the common local data repository or cldr as they call it and the other thing they
as they call it and the other thing they do is they kind of maintain libraries
do is they kind of maintain libraries for developers in order to do all um to
for developers in order to do all um to kind of so they are not building
kind of so they are not building everything from scratch and that is
everything from scratch and that is called ICU so it's very funny cuz like
called ICU so it's very funny cuz like it's called cldr and then one day one of
it's called cldr and then one day one of our friends girlfriends made him this
our friends girlfriends made him this thing which she called a seal deer
thing which she called a seal deer because whenever he talked about
because whenever he talked about cldr he thought he thought she thought
cldr he thought he thought she thought he was talking about seal deers so now
he was talking about seal deers so now this is his little like mascot for like
this is his little like mascot for like Unicode related things so what is um how
Unicode related things so what is um how does Unicode do this so it basically
does Unicode do this so it basically asss code points and each code point is
asss code points and each code point is a unique number assigned to each Unicode
a unique number assigned to each Unicode character so uh face with tears of joy
character so uh face with tears of joy can be written like this with a code
can be written like this with a code point or it can be written like this in
point or it can be written like this in terms of decimal numbers and it can also
terms of decimal numbers and it can also be written this in binary numbers so
be written this in binary numbers so these are all basically the same and the
these are all basically the same and the key thing to know at least about Emoji
key thing to know at least about Emoji is that when your device if you're
is that when your device if you're talking your iPhone with um someone
talking your iPhone with um someone who's on a laptop or an iPad or like um
who's on a laptop or an iPad or like um an Android device it's not sending the
an Android device it's not sending the images back and forth it's sending just
images back and forth it's sending just a number back and forth and then locally
a number back and forth and then locally your phone or like your laptop decides
your phone or like your laptop decides like oh this number correlates with
like oh this number correlates with which image um in terms of our emoji
which image um in terms of our emoji font and then pulls it up so this is
font and then pulls it up so this is really key to know why different Emoji
really key to know why different Emoji look different on different
look different on different platforms so 2007 to 2010 it took about
platforms so 2007 to 2010 it took about three years but Unicode 6.0 came out
three years but Unicode 6.0 came out with a first our first little baby set
with a first our first little baby set of emoji um and it just kind of hung out
of emoji um and it just kind of hung out there for a year like no one it wasn't
there for a year like no one it wasn't doing anything so 2011 though Apple
doing anything so 2011 though Apple starts adding the emoji keyboard and it
starts adding the emoji keyboard and it it just like explodes like I feel like
it just like explodes like I feel like in some ways Emoji are were not like
in some ways Emoji are were not like invented they were discovered they like
invented they were discovered they like obviously touch something very very
obviously touch something very very Primal to um to like our human desire to
Primal to um to like our human desire to like communicate in like little colorful
like communicate in like little colorful glyphs on electronic devices and you
glyphs on electronic devices and you kind of have what's kind of really
kind of have what's kind of really interesting is like the ambiguity that
interesting is like the ambiguity that comes with what emoji kind of mean and
comes with what emoji kind of mean and so one of the you know this one my
so one of the you know this one my favorite emoji is sort of like an upside
favorite emoji is sort of like an upside down smiley face very very ambiguous
down smiley face very very ambiguous clearly very ambiguous because if you
clearly very ambiguous because if you start typing into Google like the top
start typing into Google like the top hits are like what does it mean from a
hits are like what does it mean from a guy what does it mean from a girl like
guy what does it mean from a girl like it's clearly something that a lot of
it's clearly something that a lot of people are are using in like complex
people are are using in like complex situationships between each other so um
situationships between each other so um so so one of the fun things is who can
so so one of the fun things is who can propose Emoji technically anyone can
propose Emoji technically anyone can anyone here can normal normal humans can
anyone here can normal normal humans can we have basically Google doc uh or
we have basically Google doc uh or Google form that we throw up uh
Google form that we throw up uh historically between August sorry April
historically between August sorry April and August in the last two years um and
and August in the last two years um and uh so this is one of our my our favorite
uh so this is one of our my our favorite examples this is Ru aumed she was a
examples this is Ru aumed she was a 15-year-old Saudi Arabian girl who was
15-year-old Saudi Arabian girl who was living in Vienna at the time that she
living in Vienna at the time that she proposed the um hijab emoji and then you
proposed the um hijab emoji and then you know then she was like Time Magazine
know then she was like Time Magazine like you know coolest teens she got like
like you know coolest teens she got like a whole bunch of different things she
a whole bunch of different things she got into Harvard and Stanford and she
got into Harvard and Stanford and she went to Stanford um and uh so this is a
went to Stanford um and uh so this is a PR we got then there was a group of
PR we got then there was a group of folks from Argentina who got the mate
folks from Argentina who got the mate Emoji kind of they you know similar
Emoji kind of they you know similar their national drink then we worked with
their national drink then we worked with um there was a a nonprofit that really
um there was a a nonprofit that really wanted to like get a menstruation emoji
wanted to like get a menstruation emoji and so what they proposed to begin with
and so what they proposed to begin with was like bloody underwear and I was just
was like bloody underwear and I was just like no no no so many different reasons
like no no no so many different reasons why this is a terrible Emoji uh but so
why this is a terrible Emoji uh but so we did get blood drops so then you can
we did get blood drops so then you can do Moon and blood drop or underwear and
do Moon and blood drop or underwear and blood drop there's a lot of different
blood drop there's a lot of different ways actually it's really funny cuz I
ways actually it's really funny cuz I was ping my friends beforehand what they
was ping my friends beforehand what they would use to indicate menstruation
would use to indicate menstruation before there you know there was a blood
before there you know there was a blood drop so there was like red wine there
drop so there was like red wine there was like kind of that Rose um with the
was like kind of that Rose um with the like falling petal and then my favorite
like falling petal and then my favorite is actually um my friend who used a
is actually um my friend who used a Japanese flag as a way to indicate that
Japanese flag as a way to indicate that she was having her
she was having her period so um one of the biggest
period so um one of the biggest contributors this uh skin tone Emoji
contributors this uh skin tone Emoji appeared I think in 2015 it was amazing
appeared I think in 2015 it was amazing it is and it was proposed by a mom
it is and it was proposed by a mom Katrina parrot she is a entrepreneur and
Katrina parrot she is a entrepreneur and a mom who is just like at home one day
a mom who is just like at home one day and her daughter comes home and is
and her daughter comes home and is like you know I wish there were emoji
like you know I wish there were emoji that looked like me and her mom was like
that looked like me and her mom was like that's great honey what's an emoji and
that's great honey what's an emoji and so she like me I guess Googled and just
so she like me I guess Googled and just figured out that uh Unicode controlled
figured out that uh Unicode controlled emoji and she just came up with a
emoji and she just came up with a proposal saying we should not only have
proposal saying we should not only have the yellow you know skin tones at that
the yellow you know skin tones at that time everything was sort of Simpsons
time everything was sort of Simpsons yellow um it's really interesting to see
yellow um it's really interesting to see how race and like nationality are like
how race and like nationality are like depicted in different parts of the world
depicted in different parts of the world so originally in Japan everyone was
so originally in Japan everyone was yellow um but this these were the non
yellow um but this these were the non you know by default everyone was just
you know by default everyone was just like like you know human or Japanese but
like like you know human or Japanese but they had like a couple things that were
they had like a couple things that were like not like one was you had a blonde
like not like one was you had a blonde person so there's an emoji called like
person so there's an emoji called like blonde blonde woman or whatever that
blonde blonde woman or whatever that represents all westerners okay so that
represents all westerners okay so that was one and then they have one that's
was one and then they have one that's like an Indian guy with a turban so
like an Indian guy with a turban so that's supposed to represent Indian
that's supposed to represent Indian people and then there's like a like a
people and then there's like a like a guy with a little like um little hat
guy with a little like um little hat that's supposed to represent like
that's supposed to represent like Chinese people so that was like that was
Chinese people so that was like that was the view Japanese view of race which was
the view Japanese view of race which was like default then you were I then you
like default then you were I then you were like blonde Western Chinese or
were like blonde Western Chinese or Indian and and that is all there was and
Indian and and that is all there was and um obviously in the United States we
um obviously in the United States we care a lot about race and then so she
care a lot about race and then so she came up with this system with five skin
came up with this system with five skin tones um like just like normal people
tones um like just like normal people some guy in Germany decided that he
some guy in Germany decided that he wanted to do a a a face with one eyebrow
wanted to do a a a face with one eyebrow raised or as we call it the coar Emoji
raised or as we call it the coar Emoji um and oh this one's fun so woman's flat
um and oh this one's fun so woman's flat shoe I have to say not highly used
shoe I have to say not highly used statistically at this point but I really
statistically at this point but I really kind of appreciate it because it was a
kind of appreciate it because it was a mom who was very offended that all
mom who was very offended that all women's shoes had heels even the sandals
women's shoes had heels even the sandals so this is her she had like three kids
so this is her she had like three kids at the time now is four she was very
at the time now is four she was very fertile um and she also did um the women
fertile um and she also did um the women women's flat okay she also did um um one
women's flat okay she also did um um one piece bathing suit because she was also
piece bathing suit because she was also um offended by the fact that the only
um offended by the fact that the only kind of bathing suit you had was like
kind of bathing suit you had was like this like little itsy-bitsy polka dot
this like little itsy-bitsy polka dot bikini thing which is not great if
bikini thing which is not great if you're like taking your six-year-old so
you're like taking your six-year-old so I have to say that got passed but like
I have to say that got passed but like it didn't go over like super well with
it didn't go over like super well with everyone um so you know unic Cod because
everyone um so you know unic Cod because it's very public submits things for
it's very public submits things for comments and um we got this comment back
comments and um we got this comment back so one piece bathing suit why a person
so one piece bathing suit why a person want to indicate the use of swimwear
want to indicate the use of swimwear can't use existing bikini is this really
can't use existing bikini is this really necessary what about a Victorian bathing
necessary what about a Victorian bathing costume or a wet suit or water rings
costume or a wet suit or water rings this is like literally in in like uh the
this is like literally in in like uh the records and like do not encode um and so
records and like do not encode um and so the person who did it's actually very
the person who did it's actually very impressive he's actually the person who
impressive he's actually the person who created the middle finger emoji and
created the middle finger emoji and actually you ever seen the Vulcan Emoji
actually you ever seen the Vulcan Emoji he he the Vulcan hand emoji he's
he he the Vulcan hand emoji he's actually very active and I have to say
actually very active and I have to say this is actually I think one of the more
this is actually I think one of the more impressive Emoji so so obviously
impressive Emoji so so obviously obviously we have a lot of active debate
obviously we have a lot of active debate um sometimes you get like whole
um sometimes you get like whole countries submitting so literally the
countries submitting so literally the government of Finland as in like their
government of Finland as in like their equivalent in the state department their
equivalent in the state department their diplomacy kind of thing uh submitted a
diplomacy kind of thing uh submitted a proposal for SAA and um these were so
proposal for SAA and um these were so creepy and so
creepy and so weird so much is wrong with this and but
weird so much is wrong with this and but I I I felt where they were coming from
I I I felt where they were coming from first of all like they're naked they
first of all like they're naked they have no they have club feet and so we
have no they have club feet and so we decided to help them we're like okay we
decided to help them we're like okay we see where you're going with this let's
see where you're going with this let's see if we can like help you like come up
see if we can like help you like come up you know like we had the spoon like
you know like we had the spoon like should there be like steam around them
should there be like steam around them like should they be naked or wearing a
like should they be naked or wearing a tow towel it's like super like dicey but
tow towel it's like super like dicey but we wanted to help them because it was
we wanted to help them because it was like literally a foreign government
like literally a foreign government coming from an Unicode advocating for
coming from an Unicode advocating for the sauna emoji on behalf of their ENT
the sauna emoji on behalf of their ENT country so then this is um is sort of
country so then this is um is sort of evolved into just basically person in
evolved into just basically person in stey room which is which is the most
stey room which is which is the most sort of like the PG version of sauna no
sort of like the PG version of sauna no and there's no spoon they're all dressed
and there's no spoon they're all dressed it's very odd but um so you can see the
it's very odd but um so you can see the evolution of what it started out what we
evolution of what it started out what we submitted and what it ended up so
submitted and what it ended up so there's a lot of like Evolution
there's a lot of like Evolution throughout the entire process um and
throughout the entire process um and like companies can submit Emoji
like companies can submit Emoji proposals too so Google actually worked
proposals too so Google actually worked on this one I love this one okay so just
on this one I love this one okay so just to give you some context as of 2015
to give you some context as of 2015 there are many ways you could be or have
there are many ways you could be or have an occupation as a male on the Emoji
an occupation as a male on the Emoji Keyboard right like like for example you
Keyboard right like like for example you could be a police officer you could be a
could be a police officer you could be a detective you could be a Buckingham
detective you could be a Buckingham Palace guard you could even be Santa
Palace guard you could even be Santa Claus like these are so many jobs that
Claus like these are so many jobs that you could have but if you were a woman
you could have but if you were a woman as of 2015 there were four things that
as of 2015 there were four things that you could be you could be a princess you
you could be you could be a princess you could be a bride you could be a dancer
could be a bride you could be a dancer or you could be a Playboy Bunny these
or you could be a Playboy Bunny these were the sum total of all the
were the sum total of all the occupations that we can have so so we
occupations that we can have so so we there was sort of this movement at that
there was sort of this movement at that time there was like this like video on
time there was like this like video on YouTube that like went viral there was
YouTube that like went viral there was like a New York Times op that was like
like a New York Times op that was like where are the women with professions so
where are the women with professions so um basically they came up with a set of
um basically they came up with a set of emoji for professions and what's nice is
emoji for professions and what's nice is not only did women have these
not only did women have these professions now men have them too so of
professions now men have them too so of emoji Nation Emoji these are some of the
emoji Nation Emoji these are some of the ones that we've worked on I think about
ones that we've worked on I think about 130 of the Emoji on your keyboard
130 of the Emoji on your keyboard probably came through touched our system
probably came through touched our system in in some way including I I have to say
in in some way including I I have to say uh microbe or virus I think I have the
uh microbe or virus I think I have the opinion that every Emoji has its day
opinion that every Emoji has its day right like it might not be like today it
right like it might not be like today it might not be next year but I have to say
might not be next year but I have to say virus was not doing anything then came
virus was not doing anything then came 2020 and that was like such a good
2020 and that was like such a good moment for it um along with soap we had
moment for it um along with soap we had also done soap so um you know among the
also done soap so um you know among the other emoji that we have worked on are
other emoji that we have worked on are sari moon cake llama like uh teddy bear
sari moon cake llama like uh teddy bear there were no toys I felt really sad for
there were no toys I felt really sad for toys we have like giraffe um there was
toys we have like giraffe um there was Hut bubble te bubble tea was very
Hut bubble te bubble tea was very controversial actually I have to say it
controversial actually I have to say it kind we tried to slide it in originally
kind we tried to slide it in originally with the um takeout box and the
with the um takeout box and the dumplings and people were not not having
dumplings and people were not not having it I understand that CU there's not a
it I understand that CU there's not a lot like compared to like beer or wine
lot like compared to like beer or wine like bubble tea does not is not long
like bubble tea does not is not long does not have a long history on this
does not have a long history on this planet um but I will say that um they
planet um but I will say that um they submitted again actually kind of
submitted again actually kind of originally proposing that it was not
originally proposing that it was not just bubble tea but but like a black
just bubble tea but but like a black ball and milk and tea it was It was kind
ball and milk and tea it was It was kind of cool and I I have to say there was
of cool and I I have to say there was definitely a generational divide between
definitely a generational divide between like the Asian women who sit in that
like the Asian women who sit in that room and are like this absolutely is a
room and are like this absolutely is a thing that we consume like almost like
thing that we consume like almost like every week of our life and people who
every week of our life and people who are a little bit older who are like that
are a little bit older who are like that looks like a parfait how do you not know
looks like a parfait how do you not know that's a parfet and we're like we
that's a parfet and we're like we absolutely know it is uh not a parfait
absolutely know it is uh not a parfait and so so it got in eventually so it
and so so it got in eventually so it does sort of influence it kind of shows
does sort of influence it kind of shows like who is in the room influences you
like who is in the room influences you know the decisions um that get made or
know the decisions um that get made or sometimes in the room now sometimes more
sometimes in the room now sometimes more more likely in the zoom um I actually I
more likely in the zoom um I actually I just say beaver Emoji if you see Beaver
just say beaver Emoji if you see Beaver Emoji that's one of one of the ones I'm
Emoji that's one of one of the ones I'm most proud of so that is actually
most proud of so that is actually co-authored by a professor here at
co-authored by a professor here at Harvard um who is both lesbian and was
Harvard um who is both lesbian and was married to a woman from Canada so it was
married to a woman from Canada so it was very important to her to get um the
very important to her to get um the beaver Emoji passed and she promised me
beaver Emoji passed and she promised me it would always be the first line of her
it would always be the first line of her bio so and indeed if you go to her
bio so and indeed if you go to her Twitter handle uh it's like Joan Donovan
Twitter handle uh it's like Joan Donovan creator of the Beaver Emoji comma is
creator of the Beaver Emoji comma is headed research at the shoren scene
headed research at the shoren scene Center at the Harvard Kennedy School
Center at the Harvard Kennedy School It's Kind it's pretty impressive um and
It's Kind it's pretty impressive um and then we did greens actually greens was
then we did greens actually greens was really interesting because people this
really interesting because people this was also like a generational cultural
was also like a generational cultural thing people were like why do we need
thing people were like why do we need greens we have salad and I was like
greens we have salad and I was like we're Chinese we don't eat raw greens
we're Chinese we don't eat raw greens cuz like you don't know where it's been
cuz like you don't know where it's been or if it's clean so we cook our greens
or if it's clean so we cook our greens so salad is not something that we have
so salad is not something that we have So eventually I got my greens and um so
So eventually I got my greens and um so that was kind of fun and then so these
that was kind of fun and then so these are some of the people who sort of have
are some of the people who sort of have contributed to our little Emoji Nation
contributed to our little Emoji Nation things including a number of Native
things including a number of Native Americans who help get feather so why do
Americans who help get feather so why do I care so part of it is because I'm
I care so part of it is because I'm Chinese I grew up speaking Chinese and
Chinese I grew up speaking Chinese and English at the same time and it's really
English at the same time and it's really interesting to see like Chinese and
interesting to see like Chinese and English characters in terms of emoji and
English characters in terms of emoji and Chinese together right so this is fire
Chinese together right so this is fire you have like Fire characters and now
you have like Fire characters and now you have the Emoji things and it's kind
you have the Emoji things and it's kind of It kind of shows that there is a
of It kind of shows that there is a longevity in The Human Experience of
longevity in The Human Experience of something that was designed 4,000 years
something that was designed 4,000 years ago as the same visual cues in the human
ago as the same visual cues in the human experiences as now so mouth Tre tree
experiences as now so mouth Tre tree Moon Sun you can mix and match them
Moon Sun you can mix and match them which is super fun um so you know two
which is super fun um so you know two trees kind of make a forest oops sorry
trees kind of make a forest oops sorry oh well okay then the um Moon and Sun
oh well okay then the um Moon and Sun together means bright which I like um I
together means bright which I like um I like this one so if you stop and you
like this one so if you stop and you think about this so this is a basically
think about this so this is a basically a pig under a roof and what does that
a pig under a roof and what does that mean
mean it does not mean Farm as you might think
it does not mean Farm as you might think it um it actually means home or family
it um it actually means home or family so in the Chinese kind of structure and
so in the Chinese kind of structure and outlook on The View it's like where you
outlook on The View it's like where you keep your pigs is actually where your
keep your pigs is actually where your home is and what your farm is um so it
home is and what your farm is um so it gets kind of weird in all kinds of ways
gets kind of weird in all kinds of ways so one of my favorite radicals so this
so one of my favorite radicals so this character means woman uh KN
character means woman uh KN and as I was learning Chinese you kind
and as I was learning Chinese you kind of notice like how it shows up so so
of notice like how it shows up so so this is a woman underneath a roof and
this is a woman underneath a roof and you're like oh it means mom or wife or
you're like oh it means mom or wife or or like whatever like home um it does
or like whatever like home um it does not it means peace because things are at
not it means peace because things are at home when the woman is or things are at
home when the woman is or things are at peace when the woman is at home
peace when the woman is at home underneath a roof which I always thought
underneath a roof which I always thought was a little bit odd um then there is
was a little bit odd um then there is also uh woman plus child so you're like
also uh woman plus child so you're like oh and actually specifically Boy Child
oh and actually specifically Boy Child the connotation there is a little
the connotation there is a little unclear so you're like a woman plus
unclear so you're like a woman plus child family mom you know whatever um it
child family mom you know whatever um it is not it means good so the standard for
is not it means good so the standard for goodness in ancient China was a woman
goodness in ancient China was a woman who had a male child child so that kind
who had a male child child so that kind of kind of just like kind of irked me um
of kind of just like kind of irked me um growing up and then you know three women
growing up and then you know three women together means evil which is like very
together means evil which is like very like
like MCB this character means greed this
MCB this character means greed this character means slave this marriage
character means slave this marriage let's see I think this one is jealousy
let's see I think this one is jealousy and this means means adultery or
and this means means adultery or betrayal so like definitely not loving
betrayal so like definitely not loving the way women were P you know portrayed
the way women were P you know portrayed um on the emoji keyboard so uh in case
um on the emoji keyboard so uh in case you're wondering we just came out with a
you're wondering we just came out with a kids book called had emoji that kind of
kids book called had emoji that kind of Compares emoji and uh Chinese and I
Compares emoji and uh Chinese and I think I I think they sent a bunch of
think I I think they sent a bunch of books so that you guys can do some kind
books so that you guys can do some kind of contest some like later on with cs50
of contest some like later on with cs50 um so but the mixing and matching is
um so but the mixing and matching is really interesting right for example the
really interesting right for example the skin tones are actually the same yellow
skin tones are actually the same yellow character plus a layer of skin tone on
character plus a layer of skin tone on top of this so I kind of took my lessons
top of this so I kind of took my lessons from Chinese in terms of seeing how
from Chinese in terms of seeing how things can be binded so there's
things can be binded so there's something you should know about which is
something you should know about which is zge this is also an invisible emoji
zge this is also an invisible emoji character it stands for zero with Joiner
character it stands for zero with Joiner and it was actually originally created
and it was actually originally created mostly for I think Arabic where you
mostly for I think Arabic where you would you would basically kind of force
would you would basically kind of force something to have like be in the
something to have like be in the beginning of of a word or an end of the
beginning of of a word or an end of the word by kind of having this like
word by kind of having this like invisible character so the rainbow flag
invisible character so the rainbow flag for example is actually a rainbow plus
for example is actually a rainbow plus the white flag
the white flag um and we we could have all kinds of fun
um and we we could have all kinds of fun combinations if you look at polar bear
combinations if you look at polar bear it actually is if you have an older
it actually is if you have an older device or it breaks apart it is bear
device or it breaks apart it is bear plus snow which is really cute it was
plus snow which is really cute it was originally I had bear plus white and
originally I had bear plus white and then we decided that bear plus snow made
then we decided that bear plus snow made a lot more sense so uh another one this
a lot more sense so uh another one this is new I I think if you guys should have
is new I I think if you guys should have it if you've updated your phones in the
it if you've updated your phones in the last year or so so mending heart is
last year or so so mending heart is heart plus
heart plus Band-Aid um um what is this one oh this
Band-Aid um um what is this one oh this is interesting um there was a
is interesting um there was a breastfeeding woman for a long time and
breastfeeding woman for a long time and people felt like there was not gender
people felt like there was not gender par was actually really interesting all
par was actually really interesting all the people who wrote it and were like I
the people who wrote it and were like I want to I want to be a dad showing that
want to I want to be a dad showing that I'm holding my baby why is there only a
I'm holding my baby why is there only a woman holding my baby so so we kind of
woman holding my baby so so we kind of created a whole set so this is man plus
created a whole set so this is man plus bottle Tada um and so this is and all of
bottle Tada um and so this is and all of these occupations are actually often
these occupations are actually often times a woman plus like you know the
times a woman plus like you know the fried egg or like um a school or a
fried egg or like um a school or a tractor and that's how you got the
tractor and that's how you got the occupations if you if you if you send an
occupations if you if you if you send an emoji over into an older system
emoji over into an older system sometimes you'll it'll break apart so
sometimes you'll it'll break apart so one of my kind of favorite kind of
one of my kind of favorite kind of contributions in this in this world is
contributions in this in this world is interracial couple Emoji which we did
interracial couple Emoji which we did with um Tinder which is super fun
with um Tinder which is super fun because then you could have you know
because then you could have you know different couples that are and and like
different couples that are and and like so many combin this is like this is a
so many combin this is like this is a fun if you guys ever have to do a
fun if you guys ever have to do a combinatoric test um this is really fun
combinatoric test um this is really fun because you have two genders plus a
because you have two genders plus a third like gender plus two people plus
third like gender plus two people plus five skin tones plus yellow like how
five skin tones plus yellow like how many Emoji couples can you come come up
many Emoji couples can you come come up with when you introduce this Factor so
with when you introduce this Factor so this is um and underneath it it's just a
this is um and underneath it it's just a wige sequence it's like two people
wige sequence it's like two people standing together that are like glued
standing together that are like glued together now this actually gets
together now this actually gets interesting from a cs-50 perspective
interesting from a cs-50 perspective because in many cases even though you
because in many cases even though you only see one character underneath the
only see one character underneath the hood depending on how your system works
hood depending on how your system works they're counting each one of these as an
they're counting each one of these as an individual character so your string
individual character so your string length actually might be five and
length actually might be five and instead of one and this this kind of
instead of one and this this kind of became a problem with things like
became a problem with things like Twitter where things had a hard skin um
Twitter where things had a hard skin um length so gender inclusivity um is
length so gender inclusivity um is actually one of the things that we've
actually one of the things that we've been dealing most with um in the last
been dealing most with um in the last couple years so it's kind of interesting
couple years so it's kind of interesting if you think about both what a pictorial
if you think about both what a pictorial language looks like versus the
language looks like versus the abstractness of a of a spoken language
abstractness of a of a spoken language so because you know we had boy and we
so because you know we had boy and we had
had girl but there was no way to say a
girl but there was no way to say a generic child right like if you were on
generic child right like if you were on you wanted to say child you had to pick
you wanted to say child you had to pick a boy or a girl but not a way to say
a boy or a girl but not a way to say like just some little person and that's
like just some little person and that's really key cuz in English at least there
really key cuz in English at least there is no gender implied by
is no gender implied by child um so how do we mimic that and it
child um so how do we mimic that and it also is key for something like doctor
also is key for something like doctor right doctor and teacher those are those
right doctor and teacher those are those have don't have gender implied but when
have don't have gender implied but when we have them on on the Emoji Keyboard
we have them on on the Emoji Keyboard you to pick a male teacher or um you
you to pick a male teacher or um you know a a a female doctor or whatnot so
know a a a female doctor or whatnot so there was actually uh a guy at Adobe who
there was actually uh a guy at Adobe who can considers himself non-gender binary
can considers himself non-gender binary also The Man Behind the orange um the
also The Man Behind the orange um the orange heart he fought and got basically
orange heart he fought and got basically uh the first three non-gender uh
uh the first three non-gender uh non-gender binary emojis so child adult
non-gender binary emojis so child adult and old older adults so those are
and old older adults so those are creative and then we started having to
creative and then we started having to propagate them through actually all the
propagate them through actually all the occupations so these are the gender
occupations so these are the gender neutral versions of many of those um but
neutral versions of many of those um but then uh we got into this whole thing
then uh we got into this whole thing where every emoji that had a gender
where every emoji that had a gender originally had to be mirrored so
originally had to be mirrored so originally we had bearded man and then
originally we had bearded man and then we're like okay well we actually have to
we're like okay well we actually have to get bearded woman so that is on your
get bearded woman so that is on your keyboard there is pregnant woman there
keyboard there is pregnant woman there is now pregnant man which is interesting
is now pregnant man which is interesting um there is you know woman in a bridal
um there is you know woman in a bridal gown there's now man in a bridal gown um
gown there's now man in a bridal gown um and then there were ones that actually
and then there were ones that actually had to be created that were neither man
had to be created that were neither man or woman so this is a mer person so
or woman so this is a mer person so there was merman there was mermaid and
there was merman there was mermaid and there was M person it was really
there was M person it was really interesting to figure out like how do
interesting to figure out like how do you draw a gender neutral M person like
you draw a gender neutral M person like a bunch of them in the beginning
a bunch of them in the beginning actually had the arms crossed around
actually had the arms crossed around sort of the chest um Monarch so there
sort of the chest um Monarch so there was prince and princess and now there is
was prince and princess and now there is monarch and one of my favorite actually
monarch and one of my favorite actually is there was Santa Claus and there was
is there was Santa Claus and there was Mrs Claus and now there's MX Claus like
Mrs Claus and now there's MX Claus like the name of this character literally in
the name of this character literally in Unicode is MX claw so I feel like it's
Unicode is MX claw so I feel like it's sort of like a very official enshrining
sort of like a very official enshrining of gender non biner in like the world um
of gender non biner in like the world um not everyone loved it New York Post did
not everyone loved it New York Post did not love this that they like you know
not love this that they like you know we're we're cing into like Emoji woke
we're we're cing into like Emoji woke Wars um so some Emoji stats for you this
Wars um so some Emoji stats for you this is very fascinating this is sort of like
is very fascinating this is sort of like the Gen the General Distribution by far
the Gen the General Distribution by far the single emoji that used more than
the single emoji that used more than anything else is the face with tears of
anything else is the face with tears of joy about 10% of all Emoji scent is that
joy about 10% of all Emoji scent is that one character and then number two is
one character and then number two is heart red heart and then it kind of goes
heart red heart and then it kind of goes down so um there's a frequency of emoji
down so um there's a frequency of emoji use these this is sort of done by um
use these this is sort of done by um order magnitude so one is half of two
order magnitude so one is half of two two is half of one so it's really
two is half of one so it's really interesting it's a very very steep drop
interesting it's a very very steep drop off after the first couple um in case
off after the first couple um in case you ever want to go onto the the Emoji
you ever want to go onto the the Emoji kind of Unicode website you can you too
kind of Unicode website you can you too can see all the frequency things so I
can see all the frequency things so I think it's really funny so basically
think it's really funny so basically it's green going this way it increased
it's green going this way it increased in usage between 2019 and uh 2021 and
in usage between 2019 and uh 2021 and it's red going this way it drop and so
it's red going this way it drop and so pleading face which is a relatively new
pleading face which is a relatively new emoe you just sort of shut up on the
emoe you just sort of shut up on the charts um and whereas actually like
charts um and whereas actually like smiling face with heart eyes
smiling face with heart eyes like kind of kind of slipped which is
like kind of kind of slipped which is interesting so we just closed our Emoji
interesting so we just closed our Emoji proposal round for 2022 these were sort
proposal round for 2022 these were sort of the breakdowns people love submitting
of the breakdowns people love submitting Smileys and food and beverages animals
Smileys and food and beverages animals and nature da da da I don't I mean these
and nature da da da I don't I mean these are very googly colors um so what is the
are very googly colors um so what is the future of emoji I will um I will tell
future of emoji I will um I will tell you because we just had a meeting two
you because we just had a meeting two weeks ago so I can now publicly talk
weeks ago so I can now publicly talk about it so historically there was this
about it so historically there was this whole idea like Unico doesn't want to be
whole idea like Unico doesn't want to be in the world of like encoding glyphs for
in the world of like encoding glyphs for like devices everywhere like there was
like devices everywhere like there was very controversial when it started doing
very controversial when it started doing that because mostly what Unicode used to
that because mostly what Unicode used to do was you take an existing language
do was you take an existing language could be dead you know and then it would
could be dead you know and then it would just take it and digitize it right it
just take it and digitize it right it took languages that existed and just
took languages that existed and just digitize them and uh then when it kind
digitize them and uh then when it kind of wandered into Emoji World Suddenly
of wandered into Emoji World Suddenly It's like deciding what deserves to be
It's like deciding what deserves to be like an emoji decides to be digitized so
like an emoji decides to be digitized so um trying to get kind of get out of it
um trying to get kind of get out of it and they have proposals over time where
and they have proposals over time where it's like oh maybe we should like come
it's like oh maybe we should like come up with a a way to just send pictures
up with a a way to just send pictures back and forth where you you it's a
back and forth where you you it's a fixed picture and you like use a hash so
fixed picture and you like use a hash so that you know like we would look at the
that you know like we would look at the picture and then like go do a lookup
picture and then like go do a lookup somewhere like that did not go over well
somewhere like that did not go over well then there was actually a really
then there was actually a really interesting proposal I kind of like uh
interesting proposal I kind of like uh didn't go over well which is using
didn't go over well which is using something called the qid which is in
something called the qid which is in Wiki in the Wikipedia world so in
Wiki in the Wikipedia world so in Wikipedia World items all have uh
Wikipedia World items all have uh numbers across the different language
numbers across the different language Wikipedia so Obama human Earth they will
Wikipedia so Obama human Earth they will have an ID number so that the page in
have an ID number so that the page in English and the page in German the page
English and the page in German the page in Chinese all know that they're
in Chinese all know that they're pointing to the same thing so the
pointing to the same thing so the question so one idea came up like why
question so one idea came up like why don't we use the numbering system so we
don't we use the numbering system so we can use like Eiffel Tower in you know
can use like Eiffel Tower in you know see the number and then like oh people
see the number and then like oh people know like oh you're trying to say Eiffel
know like oh you're trying to say Eiffel Tower that did not go over well so those
Tower that did not go over well so those are both both of those proposals seem
are both both of those proposals seem dead as of yet um and it's too bad cuz
dead as of yet um and it's too bad cuz and you'll see kind of what's happening
and you'll see kind of what's happening okay so what's coming in 22 so these are
okay so what's coming in 22 so these are the Emoji that I actually sort of
the Emoji that I actually sort of thought they would be on your phones by
thought they would be on your phones by now but cuz we're in mid November and
now but cuz we're in mid November and they usually update early November so
they usually update early November so three more Hearts people love hearts
three more Hearts people love hearts like uh wing blackbird Goose Birds also
like uh wing blackbird Goose Birds also purple flowers jellyfish moose face
purple flowers jellyfish moose face donkey donkey was a little bit late for
donkey donkey was a little bit late for the kind of Elections um Ginger peep pod
the kind of Elections um Ginger peep pod Wireless cond shaking face um folding
Wireless cond shaking face um folding hand pan that one was interesting cuz
hand pan that one was interesting cuz when people first proposed it they
when people first proposed it they proposed it as like an electric fan and
proposed it as like an electric fan and that didn't like who knows what electric
that didn't like who knows what electric fans will look like in 100 years because
fans will look like in 100 years because the thing is Once An Emoji always an
the thing is Once An Emoji always an emoji they never retire so they're
emoji they never retire so they're always looking for things that have a
always looking for things that have a long visual longevity floppy disc did
long visual longevity floppy disc did not actually do that so there's always
not actually do that so there's always like we don't want another floppy disc
like we don't want another floppy disc um and then hairpick is interesting so
um and then hairpick is interesting so there was a whole debate about how to
there was a whole debate about how to convey um like afro African hair like
convey um like afro African hair like the the curly hair that they introduced
the the curly hair that they introduced a couple years ago was supposed to do
a couple years ago was supposed to do that and most of the vendors actually
that and most of the vendors actually have it in a sort of Afro way except for
have it in a sort of Afro way except for Apple so there's a lot of complaints but
Apple so there's a lot of complaints but um hairpick was sort of a an interesting
um hairpick was sort of a an interesting way it means both comb but also has sort
way it means both comb but also has sort of an interesting historic connotation
of an interesting historic connotation and it's been around for about 2,000
and it's been around for about 2,000 years uh a couple music things maracus
years uh a couple music things maracus and flute uh Beyond 2022 one of the
and flute uh Beyond 2022 one of the things that's going to die oops can I go
things that's going to die oops can I go back no I can't oh well uh we're going
back no I can't oh well uh we're going to retire the the family Emoji um they
to retire the the family Emoji um they didn't go over so well there were so
didn't go over so well there were so many of them combinatorically if you had
many of them combinatorically if you had all the everything in in uh you know all
all the everything in in uh you know all the race all the races all because you
the race all the races all because you wanted to have skin tones because you
wanted to have skin tones because you didn't want to imply that families can
didn't want to imply that families can only be one race it was such an ordeal
only be one race it was such an ordeal uh essentially we're all like no one
uh essentially we're all like no one uses them and there's so many and it's
uses them and there's so many and it's like the fonts like in terms of the load
like the fonts like in terms of the load is like too large so they're just going
is like too large so they're just going to make them all into like basically
to make them all into like basically little like bathroom symbol Type U folks
little like bathroom symbol Type U folks um so I think that is those will
um so I think that is those will disappear what what's actually really
disappear what what's actually really interesting about the family Emoji is
interesting about the family Emoji is they had gay when they introduced the
they had gay when they introduced the family they had gay family emoji and the
family they had gay family emoji and the Russian government went berserk and
Russian government went berserk and actually you can Google this in 2015
actually you can Google this in 2015 you'll see a bunch of articles about
you'll see a bunch of articles about like the Russian government considering
like the Russian government considering it homosexual propaganda to Youth and
it homosexual propaganda to Youth and there was a big debate about whether or
there was a big debate about whether or not they were going to ban Apple devices
not they were going to ban Apple devices and what so you can see a lot of the
and what so you can see a lot of the media coverage from that time but I
media coverage from that time but I thought it was really interesting how
thought it was really interesting how upset a national government can get
upset a national government can get about little pictures on your phone um
about little pictures on your phone um another thing that's going to that's on
another thing that's going to that's on the agenda as of you know a couple of
the agenda as of you know a couple of weeks ago is directionality in terms of
weeks ago is directionality in terms of emoji so as you know most emojis kind of
emoji so as you know most emojis kind of just flip one way or another and the
just flip one way or another and the reason why it matters is because not all
reason why it matters is because not all languages run in the same direction so
languages run in the same direction so for example Arabic so we are used to
for example Arabic so we are used to left to right but a lot of languages go
left to right but a lot of languages go right to left um so and this kind of It
right to left um so and this kind of It kind of changes the meaning of emoji for
kind of changes the meaning of emoji for example right to left I send this a lot
example right to left I send this a lot to my friends when I'm going flying from
to my friends when I'm going flying from the Bay Area to New York um if you do it
the Bay Area to New York um if you do it from left to right however that is what
from left to right however that is what it looks like so it looks like um you
it looks like so it looks like um you know you're in NE Bay but the plane is
know you're in NE Bay but the plane is still going that you know kind of up and
still going that you know kind of up and to the right and then now it looks like
to the right and then now it looks like you're going from New York to the Bay
you're going from New York to the Bay Area the other place is like oh it's
Area the other place is like oh it's it's a girl and she's running really
it's a girl and she's running really fast right uh that is left to right to
fast right uh that is left to right to left in our world wait left to right
left in our world wait left to right sorry sorry that was that's that's
sorry sorry that was that's that's supposed to be left to right and in here
supposed to be left to right and in here it would be she's like behind like
it would be she's like behind like pollution or something like that so
pollution or something like that so sorry about so so an example this is
sorry about so so an example this is actually in The Proposal like in one
actually in The Proposal like in one case if it's left or right you're
case if it's left or right you're running away from a line of cars and the
running away from a line of cars and the other one it's a warning to not run
other one it's a warning to not run behind car fumes so they are trying to
behind car fumes so they are trying to figure out like how do we mirror a bunch
figure out like how do we mirror a bunch of the Emoji um but the LA the main
of the Emoji um but the LA the main thing that I think sort of I don't know
thing that I think sort of I don't know when it's going to happen I'm really
when it's going to happen I'm really hopeful is going to happen is trying to
hopeful is going to happen is trying to come up with a system that supports
come up with a system that supports little stickers in line that don't need
little stickers in line that don't need Unicode so this is like slack or on um
Unicode so this is like slack or on um twitch you can embed little pictures on
twitch you can embed little pictures on in line and all all all the vendors have
in line and all all all the vendors have to get together and agree to come up
to get together and agree to come up with a standard way to do that they have
with a standard way to do that they have not yet come up with that but that is
not yet come up with that but that is sort of one of the ways that unic coded
sort of one of the ways that unic coded like want it wants to back away from
like want it wants to back away from actually being like a global regulator
actually being like a global regulator for like little colorful glyphs and so
for like little colorful glyphs and so if you ever need to reach me um on in my
if you ever need to reach me um on in my emoji world you can find me um Jenny
emoji world you can find me um Jenny EmojiNation org there will
EmojiNation org there will be um the it will be actually a while I
be um the it will be actually a while I think before we see a next generation of
think before we see a next generation of emoji showing up in it used to be like
emoji showing up in it used to be like every year they would get new code
every year they would get new code points it might be a little bit less
points it might be a little bit less than every year now as they work on
than every year now as they work on things like directionality over time so
things like directionality over time so that uh if anyone has questions you can
that uh if anyone has questions you can ask questions you can find me afterwards
ask questions you can find me afterwards I think I've I feel like there's
I think I've I feel like there's supposed to be some some hubub right now
supposed to be some some hubub right now about maybe maybe
about maybe maybe um microphones but maybe not but maybe
um microphones but maybe not but maybe I'm just done and if if there are
I'm just done and if if there are questions or if David is around I'm
questions or if David is around I'm happy you know he can I'm I'm happy to
happy you know he can I'm I'm happy to to answer any questions that folks have
to answer any questions that folks have yes hi yes I was wondering what were
yes hi yes I was wondering what were your thoughts on the
your thoughts on the emo so the question is what are my
emo so the question is what are my thoughts on the Emoji Movie you're
thoughts on the Emoji Movie you're talking about the Sony animated one yes
talking about the Sony animated one yes okay my thought on that is it is better
okay my thought on that is it is better than a 6% rating on um Rotten Tomatoes
than a 6% rating on um Rotten Tomatoes would lead you to believe so that's my
would lead you to believe so that's my one thought and my my next thought is
one thought and my my next thought is that um that was a rush job from an
that um that was a rush job from an animation perspective that was that was
animation perspective that was that was was about 18 months of whereas a typical
was about 18 months of whereas a typical animated movie takes four years so in my
animated movie takes four years so in my spare time I also produce movies and
spare time I also produce movies and documentaries so one thing that is key
documentaries so one thing that is key to know
to know about about movies and animated movies
about about movies and animated movies and this is very important they take a
and this is very important they take a very long time but you can always fix it
very long time but you can always fix it because you haven't shot anything and a
because you haven't shot anything and a very good example of that is I assume
very good example of that is I assume you guys have seen Frozen if you haven't
you guys have seen Frozen if you haven't seen you seen of the age you would have
seen you seen of the age you would have seen Frozen um I do not understand like
seen Frozen um I do not understand like how huge and a phenomenon or why it was
how huge and a phenomenon or why it was such a huge phenomenon but
such a huge phenomenon but um they actually did a original cut of
um they actually did a original cut of Frozen and it did so I don't know you
Frozen and it did so I don't know you guys know the the Eye the the sort of
guys know the the Eye the the sort of Snow Queen thing but she's like super
Snow Queen thing but she's like super dark and like not fun and like kind of
dark and like not fun and like kind of evil and like not someone you want to
evil and like not someone you want to like get behind as a character so they
like get behind as a character so they actually did sort of a rough cut of that
actually did sort of a rough cut of that of Frozen and they came out of that with
of Frozen and they came out of that with um it's just storyboarding and they're
um it's just storyboarding and they're like that is not good and they killed it
like that is not good and they killed it so they were like we can't go with this
so they were like we can't go with this and then started from scratch more or
and then started from scratch more or less again starting with the song um Let
less again starting with the song um Let It Go which is actually written by a kid
It Go which is actually written by a kid from my elementary school Bobby Lopez or
from my elementary school Bobby Lopez or co-written by Bobby Lopez um I also
co-written by Bobby Lopez um I also actually fun fact I also went I would
actually fun fact I also went I would took the school bus with uh Lyn mmel
took the school bus with uh Lyn mmel Miranda so I was a fourth grader when he
Miranda so I was a fourth grader when he was like a kindergartener so we had a
was like a kindergartener so we had a very musical Elementary School in New
very musical Elementary School in New York City but the thing is they could
York City but the thing is they could fix it because they had enough time and
fix it because they had enough time and have enough money not like movies where
have enough money not like movies where you shoot humans much harder to fix so
you shoot humans much harder to fix so you have the footage that you have and
you have the footage that you have and you can do little pickups but you can't
you can do little pickups but you can't fix it so essentially what happened in
fix it so essentially what happened in that case I think um it's 18 months and
that case I think um it's 18 months and it could have gotten better and a lot of
it could have gotten better and a lot of the movies that you see with Pixar like
the movies that you see with Pixar like it's very it's actually sort emotionally
it's very it's actually sort emotionally similar to the movie called inside out
similar to the movie called inside out and uh but inside they just have more
and uh but inside they just have more time and so it's better so as opposed to
time and so it's better so as opposed to 18 months which is not long enough to
18 months which is not long enough to make a animated movie good but uh the
make a animated movie good but uh the other fun thing is is it was the it it
other fun thing is is it was the it it was so weird cuz they sold sponsorships
was so weird cuz they sold sponsorships it was like like oh my God here comes
it was like like oh my God here comes the Bots and the malware let's go into
the Bots and the malware let's go into Dropbox and protect ourselves and was so
Dropbox and protect ourselves and was so I think that it got a lot of like bad
I think that it got a lot of like bad kind of um kind of uh vibes from the
kind of um kind of uh vibes from the from the press for doing things like
from the press for doing things like that but from a kids perspective it's
that but from a kids perspective it's it's fine I think if um I don't know
it's fine I think if um I don't know that I would like put into my top 10 of
that I would like put into my top 10 of animated pictures but it's better than
animated pictures but it's better than 6% on Rotten Tomatoes
6% on Rotten Tomatoes and then actually if you guys ever care
and then actually if you guys ever care we we have done uh I did a documentary
we we have done uh I did a documentary about Emoji so and all the people that
about Emoji so and all the people that create helped create emoji and uh we did
create helped create emoji and uh we did a cs50x movie night I think during the
a cs50x movie night I think during the pandemic was it during the pandemic
pandemic was it during the pandemic everything sort of like blurred together
everything sort of like blurred together but it was during the pandemic yeah Are
but it was during the pandemic yeah Are we more questions yes um I wanted to
we more questions yes um I wanted to know you mentioned that one of the
know you mentioned that one of the criteria for
criteria for AC do or there's demand
yeah yeah that's a very good question yeah so the question is one of the
yeah so the question is one of the propos one of our criterias of of um
propos one of our criterias of of um getting an emoji accepted is to sort of
getting an emoji accepted is to sort of to demonstrate demand and how do we
to demonstrate demand and how do we demonstrate demand and I would say in a
demonstrate demand and I would say in a in a pretty um clumsy way actually at
in a pretty um clumsy way actually at this point so the main thing that you
this point so the main thing that you have in our in our current proposal
have in our in our current proposal process is we have um a median Emoji
process is we have um a median Emoji which is elephant so elephant is like if
which is elephant so elephant is like if you stack ranked all the emoji for
you stack ranked all the emoji for popularity elephant is like right there
popularity elephant is like right there in the middle and it's also a concept
in the middle and it's also a concept that's like universally understood
that's like universally understood across all
across all languages so um elephant shows up
languages so um elephant shows up somewhere between 500 million and 700
somewhere between 500 million and 700 million in Google search results like if
million in Google search results like if you type it into a laptop you'll see you
you type it into a laptop you'll see you know elephant 500,000 uh 500 million
know elephant 500,000 uh 500 million search results and generally you're
search results and generally you're you're trying to when you're comparing
you're trying to when you're comparing your term to elephant you want to see
your term to elephant you want to see very roughly how many Google search
very roughly how many Google search results B search results sometimes
results B search results sometimes Instagram so actually something that was
Instagram so actually something that was really surprising to me was someone
really surprising to me was someone proposed hummingbird I think hummingbird
proposed hummingbird I think hummingbird is uh a good proposal and um but if you
is uh a good proposal and um but if you look at Hummingbird it's only like 21
look at Hummingbird it's only like 21 million in terms of the stat so which I
million in terms of the stat so which I thought was like very surprisingly low
thought was like very surprisingly low so that's one of the main ways that we
so that's one of the main ways that we kind of we kind of see like is it also
kind of we kind of see like is it also visually used and all of that yeah any
visually used and all of that yeah any other questions are we good I didn't
other questions are we good I didn't even need my water or anything oh can I
even need my water or anything oh can I take I'm going to take a picture I'm
take I'm going to take a picture I'm going to take a picture for because now
going to take a picture for because now you guys are actually human and not
you guys are actually human and not Muppets so I'm very very excited about
Muppets so I'm very very excited about this so I will send this to like my
this so I will send this to like my block mates and be like I just lectured
block mates and be like I just lectured at CS you know in Sanders Theater my
at CS you know in Sanders Theater my thanks to Jenny Lee thank
thanks to Jenny Lee thank you yeah you can stay up here for give
you yeah you can stay up here for give me one
me one second
so if up until now thought it would be appropriate to toss this up on the board
appropriate to toss this up on the board if up until now you've not yet gotten
if up until now you've not yet gotten cs50 stress ball on the way out please
cs50 stress ball on the way out please do grab one we got some extras as well
do grab one we got some extras as well off to the side but I would also keep in
off to the side but I would also keep in mind back in week zero where again we
mind back in week zero where again we began we asked you to categorize
began we asked you to categorize yourselves as to whether you are among
yourselves as to whether you are among those less comfortable those more
those less comfortable those more comfortable or those somewhere in
comfortable or those somewhere in between uh please know now that you are
between uh please know now that you are officially all some uh please know now
officially all some uh please know now that you are all officially among those
that you are all officially among those more comfortable and indeed even though
more comfortable and indeed even though a couple of more Milestones await we
a couple of more Milestones await we cannot wait to see what you accomplish
cannot wait to see what you accomplish with your final projects in the meantime
with your final projects in the meantime as always this is and now this was
[Laughter] [Music]
Mr when I in CS I'll say truth be told I thought
I in CS I'll say truth be told I thought i' be real coding hero because I did
so it picked up so fast turn out that harder scratch hell world and
harder scratch hell world and goodby
goodby [Applause]
[Applause] [Music]
because Mr I was on
Mr I was on the trying so hard not fall behind
the trying so hard not fall behind attending section and wanting a stuck in
attending section and wanting a stuck in the in void of my
computer to pyth in why on because this is
s Mr s we completed JavaScript sequ CSS
JavaScript sequ CSS and every language our to reset don't
get again I repli obviously because
obviously because cie
cie [Applause]
all right so this is cs50 my name is David May and this is Harvard
David May and this is Harvard University's introduction to the
University's introduction to the intellectual Enterprises of computer
intellectual Enterprises of computer science and the Art of programming and
science and the Art of programming and this of course is our special family
this of course is our special family weekend wherein not only are cs50's own
weekend wherein not only are cs50's own students here in the audience but also
students here in the audience but also some family members as well now you're
some family members as well now you're showing up in the semester a little bit
showing up in the semester a little bit late we've just tackled week eight which
late we've just tackled week eight which is really our ninth week since computer
is really our ninth week since computer scientists start counting from zero so
scientists start counting from zero so we've done a whole lot of work over the
we've done a whole lot of work over the past few weeks as you might have heard
past few weeks as you might have heard via emails or text messages home
via emails or text messages home including a language known here as binar
including a language known here as binar so on the screen here of course is a lot
so on the screen here of course is a lot of zeros and ones and suffice it to say
of zeros and ones and suffice it to say let me sum up the past nine weeks with
let me sum up the past nine weeks with this is what's going on underneath the
this is what's going on underneath the hood but of course today we thought we'd
hood but of course today we thought we'd make things a little more accessible a
make things a little more accessible a little more broadly applicable and
little more broadly applicable and indeed our Focus today will not be on
indeed our Focus today will not be on what these patterns of zeros and ones
what these patterns of zeros and ones represent which in astute eye might
represent which in astute eye might notice are replicated visually with
notice are replicated visually with these light bulbs being in a pattern on
these light bulbs being in a pattern on and off and as your child might have
and off and as your child might have hinted uh before class or perhaps now
hinted uh before class or perhaps now this might very well spell a word up to
this might very well spell a word up to eight characters long because you can
eight characters long because you can encode even in the real world things
encode even in the real world things digital too but today we'll focus on
digital too but today we'll focus on things much more high level this notion
things much more high level this notion of cyber security like our the security
of cyber security like our the security of our data our privacy of our systems
of our data our privacy of our systems particularly on the internet nowadays
particularly on the internet nowadays because presumably all of us are
because presumably all of us are carrying Technologies around in our
carrying Technologies around in our pocket using laptops and desktops every
pocket using laptops and desktops every day and so the goal today is to
day and so the goal today is to stipulate that this is what's going on
stipulate that this is what's going on underneath the hood but let's solve some
underneath the hood but let's solve some problems at a higher level so that your
problems at a higher level so that your homework when you go back to wherever
homework when you go back to wherever you're visiting from can actually be to
you're visiting from can actually be to apply some of today's Lessons Learned so
apply some of today's Lessons Learned so with that said perhaps the most common
with that said perhaps the most common familiar defense uh of one's systems and
familiar defense uh of one's systems and data phone and laptops and desktops
data phone and laptops and desktops would just be these simple passwords
would just be these simple passwords unfortunately you and I are frankly as
unfortunately you and I are frankly as humans not all that good at choosing
humans not all that good at choosing passwords and this is in itself a
passwords and this is in itself a relatively weak form of Defense even
relatively weak form of Defense even though each of us has dozens hundreds of
though each of us has dozens hundreds of passwords nowadays or at least dozens or
passwords nowadays or at least dozens or hundreds of accounts Maybe fives or tens
hundreds of accounts Maybe fives or tens of dozens of passwords indeed if you're
of dozens of passwords indeed if you're in the habit of reusing passwords we'll
in the habit of reusing passwords we'll see today probably among our first
see today probably among our first Lessons Learned so for instance if we
Lessons Learned so for instance if we look back at the past year 2021 thanks
look back at the past year 2021 thanks to security researchers who take a look
to security researchers who take a look at data that has been hacked or leaked
at data that has been hacked or leaked online by way of public databases we
online by way of public databases we have a sense as computer scientists of
have a sense as computer scientists of what the most popular or equivalently
what the most popular or equivalently what some of the worst passwords are
what some of the worst passwords are that you and I are choosing for our
that you and I are choosing for our system so as of this past year according
system so as of this past year according to one measure the most commonly used
to one measure the most commonly used password in systems everywhere was 1 2 3
password in systems everywhere was 1 2 3 4 5 6 all right number two password in
4 5 6 all right number two password in our top 10 here list was only slightly
our top 10 here list was only slightly longer 1 2 3 4 5 6 7 8 9 after that we
longer 1 2 3 4 5 6 7 8 9 after that we took a turn in the other direction 1 2 3
took a turn in the other direction 1 2 3 4 five alone after that got a little
4 five alone after that got a little more interesting quiry which might sound
more interesting quiry which might sound pretty cryptic but not if you look down
pretty cryptic but not if you look down at your US keyboard and it's the top
at your US keyboard and it's the top leftand row of the keys on a an American
leftand row of the keys on a an American keyboard so also not all that hard uh
keyboard so also not all that hard uh perhaps not surprisingly uh a little
perhaps not surprisingly uh a little disconcertingly number five was
disconcertingly number five was password meanwhile number six returns us
password meanwhile number six returns us to digits 1 2 3 4 5 6 7 8 after that
to digits 1 2 3 4 5 6 7 8 after that really less effort 111 111 uh after that
really less effort 111 111 uh after that a little more variation but not all that
a little more variation but not all that much 1 two 3 one two 3 after that it's
much 1 two 3 one two 3 after that it's getting even less interesting 1 2 3 4 5
getting even less interesting 1 2 3 4 5 6 7
6 7 890 and then lastly topping the list is
890 and then lastly topping the list is just 1 2 3 4 5 6 7 so this is not a good
just 1 2 3 4 5 6 7 so this is not a good top 10 list to be on so among today's
top 10 list to be on so among today's first takeaways is if you see your
first takeaways is if you see your password on the screen like you didn't
password on the screen like you didn't make the list in a good way this means
make the list in a good way this means hundreds thousands millions of other
hundreds thousands millions of other people probably have that password of
people probably have that password of yours now in of itself that's not
yours now in of itself that's not necessarily worrisome because I don't
necessarily worrisome because I don't know who has these passwords in a room
know who has these passwords in a room as large as this but just intuitively
as large as this but just intuitively why is this a bad thing either parent or
why is this a bad thing either parent or child welcome to raise a hand here why
child welcome to raise a hand here why might this be
might this be intuitively yeah access to it so access
intuitively yeah access to it so access to it like I mean we literally as
to it like I mean we literally as computer scientists now have a database
computer scientists now have a database of really common passwords and your your
of really common passwords and your your thoughts password and can just find it
thoughts password and can just find it out quickly yeah you can just find it
out quickly yeah you can just find it out quickly I mean you could imagine
out quickly I mean you could imagine trying to guess someone's password by
trying to guess someone's password by just typing in random letters random
just typing in random letters random numbers random words but not if you have
numbers random words but not if you have a top 10 list like the the adversaries
a top 10 list like the the adversaries in the world might as well just start
in the world might as well just start with this list now you'll notice that
with this list now you'll notice that even absent from this are slight
even absent from this are slight variants like some of you might be
variants like some of you might be thinking I'm not on the list cuz I do
thinking I'm not on the list cuz I do something clever like I use an
something clever like I use an exclamation point for the number one or
exclamation point for the number one or a three for an e or a five for an S and
a three for an e or a five for an S and based on the smiles in the room right
based on the smiles in the room right now you're not all that clever it turns
now you're not all that clever it turns out because other people are smiling too
out because other people are smiling too which is to say that an adversary can
which is to say that an adversary can take those same fistic that you might
take those same fistic that you might think are making things more secure by
think are making things more secure by just tweaking some letters to numbers or
just tweaking some letters to numbers or vice versa but if you're doing it and
vice versa but if you're doing it and other people are doing it the bad guys
other people are doing it the bad guys so to speak are going to be doing it as
so to speak are going to be doing it as well so unfortunately when it comes to
well so unfortunately when it comes to passwords better is longer and random
passwords better is longer and random and really unguessable but that's not
and really unguessable but that's not what most of us have in fact case in
what most of us have in fact case in point on our phones whether you have an
point on our phones whether you have an Android device or an iPhone nowadays you
Android device or an iPhone nowadays you know odds are you have something
know odds are you have something relatively simplistic protecting it if
relatively simplistic protecting it if you have anything at all but at least
you have anything at all but at least Apple and Google are pretty good at
Apple and Google are pretty good at least nudging us to choose these kinds
least nudging us to choose these kinds of passcodes now and a four-digit
of passcodes now and a four-digit passcode is quite common nowadays and so
passcode is quite common nowadays and so here's where we have an opportunity
here's where we have an opportunity thanks to uh the URL that you saw on the
thanks to uh the URL that you saw on the screen earlier to conjecture as a group
screen earlier to conjecture as a group just how long might it take an adversary
just how long might it take an adversary someone out there who's out to get us or
someone out there who's out to get us or get one of us uh how long might it take
get one of us uh how long might it take an adversary to figure out your phone's
an adversary to figure out your phone's 4digit passcode this is a cs50 is on
4digit passcode this is a cs50 is on Carter Carter if we could switch over
Carter Carter if we could switch over and poll the audience here if you take
and poll the audience here if you take out your phone or laptop whatever device
out your phone or laptop whatever device you might have used a few minutes ago to
you might have used a few minutes ago to scan that QR code or to visit that same
scan that QR code or to visit that same URL you can see these questions on your
URL you can see these questions on your browser and if you can't that's fine
browser and if you can't that's fine we'll share some aggregate data
we'll share some aggregate data nonetheless but you should have an
nonetheless but you should have an opportunity to tap one of your answers
opportunity to tap one of your answers and we'll give folks a few more
and we'll give folks a few more seconds if you'd like to play along at
seconds if you'd like to play along at home and here in just a
home and here in just a moment probably have many people
moment probably have many people reporting but why don't we go ahead and
reporting but why don't we go ahead and take a look at some percentages it looks
take a look at some percentages it looks like most of you 67% are proposing just
like most of you 67% are proposing just a few seconds so that's not all that
a few seconds so that's not all that good news if it's a four-digit passcode
good news if it's a four-digit passcode some of you are hoping it's a few
some of you are hoping it's a few minutes uh 8% are hoping a few hours
minutes uh 8% are hoping a few hours four more than 4% of you are really
four more than 4% of you are really hoping perhaps it's a few days well
hoping perhaps it's a few days well let's actually consider how we can
let's actually consider how we can answer this question and make today not
answer this question and make today not just conceptual but a little
just conceptual but a little quantitative too and see if we can't
quantitative too and see if we can't slap some numbers on questions like
slap some numbers on questions like these so ultimately you can make more
these so ultimately you can make more informed decisions with your system
informed decisions with your system security so for instance when it comes
security so for instance when it comes to four digigit passcodes rather than
to four digigit passcodes rather than just consider how secure it is well
just consider how secure it is well let's make it a more precise question
let's make it a more precise question like what are the forms of attack well
like what are the forms of attack well the simplest attack might be just
the simplest attack might be just someone grabbing your phone be it in
someone grabbing your phone be it in your family or maybe at Starbucks or the
your family or maybe at Starbucks or the airport or the like and just starting
airport or the like and just starting all possible combinations maybe 00000000
all possible combinations maybe 00000000 then 00001 and 00002 we could maybe
then 00001 and 00002 we could maybe automate this a little bit so for
automate this a little bit so for instance I might potentially be able to
instance I might potentially be able to do something like uh roboticize this
do something like uh roboticize this here let me go ahead and full screen a
here let me go ahead and full screen a quick video here that's just going to
quick video here that's just going to paint a picture in just a moment on the
paint a picture in just a moment on the screen of how if we're a really clever
screen of how if we're a really clever adversary and know how to build things
adversary and know how to build things well at least maybe we could automate
well at least maybe we could automate some of that process so here's an
some of that process so here's an Android phone on a counter here's a very
Android phone on a counter here's a very simple tripod and a little touch device
simple tripod and a little touch device robotically doing all of that hacking
robotically doing all of that hacking for you starting at 0000 Z probably all
for you starting at 0000 Z probably all the way up to 9999 now that too wasn't
the way up to 9999 now that too wasn't necessarily all of that all that fast
necessarily all of that all that fast but at least you the adversary can step
but at least you the adversary can step away and doesn't actually have to be
away and doesn't actually have to be bothered with the time involved the cost
bothered with the time involved the cost involved in actually hacking that
involved in actually hacking that particular device well let's go one
particular device well let's go one level deeper a little more interestingly
level deeper a little more interestingly and consider here um the how much much
and consider here um the how much much time really this so-called Brute Force
time really this so-called Brute Force attack would take and that's actually a
attack would take and that's actually a term of art much like in yester year
term of art much like in yester year when maybe there was a battering ram
when maybe there was a battering ram trying to brute force their way into a
trying to brute force their way into a castle or something like that a Brute
castle or something like that a Brute Force attack digitally is just someone
Force attack digitally is just someone trying manually all possible codes or
trying manually all possible codes or maybe robotically trying all possible
maybe robotically trying all possible codes but generally automating the
codes but generally automating the process in some way to go through all
process in some way to go through all possibilities well if you've got for
possibilities well if you've got for instance um a 4digit passcode let's ask
instance um a 4digit passcode let's ask maybe a follow-up question here not how
maybe a follow-up question here not how long will take but how many possible
long will take but how many possible 4digit passcodes are there because then
4digit passcodes are there because then maybe we can do some quick math and if
maybe we can do some quick math and if every passcode takes me a second or a
every passcode takes me a second or a few milliseconds or the like then I
few milliseconds or the like then I think we can try to extrapolate from
think we can try to extrapolate from that whether the first answer was
that whether the first answer was seconds or minutes or days or hours or
seconds or minutes or days or hours or something else so how many four-digit
something else so how many four-digit passcodes are possible if you take out
passcodes are possible if you take out your same device it should have just
your same device it should have just changed automatically if it doesn't seem
changed automatically if it doesn't seem to have maybe reload your browser with
to have maybe reload your browser with some menu option and then tap in here
some menu option and then tap in here how many four digigit passcodes are
how many four digigit passcodes are possible four total 40 99,999 10,000 or
possible four total 40 99,999 10,000 or unsure is okay
unsure is okay too so let's see we'll give you a few
too so let's see we'll give you a few more moments how many four digigit
more moments how many four digigit passcodes are possible and shall we
passcodes are possible and shall we reveal the results so now it looks like
reveal the results so now it looks like uh a a few of you uh 2% of you are
uh a a few of you uh 2% of you are saying just four passcodes 40 99,999
saying just four passcodes 40 99,999 there's definitely some contention here
there's definitely some contention here and 6% are un sure well how do we wrap
and 6% are un sure well how do we wrap our minds around this well let's just
our minds around this well let's just kind of do this real simply here let me
kind of do this real simply here let me switch back over to doing a bit of math
switch back over to doing a bit of math and if we have here 10 possibilities for
and if we have here 10 possibilities for each digit if there's four digits each
each digit if there's four digits each digit can be 0 1 2 3 4 5 6 7 8 nine so
digit can be 0 1 2 3 4 5 6 7 8 nine so that's 10 possibilities so if you think
that's 10 possibilities so if you think about the number of permutations that's
about the number of permutations that's 10 possibilities for the first digit
10 possibilities for the first digit times 10 for the next time 10 to the for
times 10 for the next time 10 to the for the next times 10 for the next and so if
the next times 10 for the next and so if we do that out 10 * 10 * 10 * 10 or 10
we do that out 10 * 10 * 10 * 10 or 10 the 4th there are indeed as 66% of you
the 4th there are indeed as 66% of you uh found uh 10,000 possibilities and so
uh found uh 10,000 possibilities and so now we can kind of work backwards and
now we can kind of work backwards and decide how long is it going to take for
decide how long is it going to take for an adversary to hack into this phone
an adversary to hack into this phone because if it's one attack one guess per
because if it's one attack one guess per second well that's going to map out to
second well that's going to map out to 10,000 seconds but maybe not if the
10,000 seconds but maybe not if the adversary isn't a roboticist or a human
adversary isn't a roboticist or a human what if they're like at a software
what if they're like at a software programmer someone who's taken even a
programmer someone who's taken even a class introductory like cs50 and learned
class introductory like cs50 and learned a little bit of programming well a
a little bit of programming well a little bit frighteningly it's not all
little bit frighteningly it's not all that hard to hack into systems if you
that hard to hack into systems if you just know how to code to and really have
just know how to code to and really have the computer do your work for you so in
the computer do your work for you so in fact let me go ahead and change over to
fact let me go ahead and change over to another screen on my computer here um
another screen on my computer here um this is different for students in the
this is different for students in the group from vs code this is just a black
group from vs code this is just a black and white version of it that we've used
and white version of it that we've used briefly in the past and I'm just going
briefly in the past and I'm just going to go ahead and create a program called
to go ahead and create a program called crack.pie to crack something just
crack.pie to crack something just technically means to figure out what it
technically means to figure out what it is figure out a password in this case
is figure out a password in this case and dop means I'm going to use a
and dop means I'm going to use a programming language that we here in
programming language that we here in cs50 have been dabbling in in the past
cs50 have been dabbling in in the past couple of weeks with more to come next
couple of weeks with more to come next week as well so it turns out and you
week as well so it turns out and you need not understand each of these lines
need not understand each of these lines of code if I want to try maybe
of code if I want to try maybe generating all 10,000 possible codes I'm
generating all 10,000 possible codes I'm not going to bother with a robot I've
not going to bother with a robot I've got all these cables coming out of my
got all these cables coming out of my computer and odds are one of them is a
computer and odds are one of them is a USB cable or a lightning cable surely we
USB cable or a lightning cable surely we could figure out how to connect like
could figure out how to connect like laptop or desktop to phone and just like
laptop or desktop to phone and just like automate the process nowadays by just
automate the process nowadays by just sending all of the numbers into the
sending all of the numbers into the phone until one uh unlocks the trick
phone until one uh unlocks the trick just like in the movies or TV well in
just like in the movies or TV well in Python I could write a program that does
Python I could write a program that does this as follows I can import so to speak
this as follows I can import so to speak all of the decimal digits 0 through n
all of the decimal digits 0 through n and this for students in the room is
and this for students in the room is just a slightly better version of typing
just a slightly better version of typing out 10 different numbers manually I can
out 10 different numbers manually I can also import from a library so to speak
also import from a library so to speak called iter tools for iteration tools
called iter tools for iteration tools which means to do something again and
which means to do something again and again I can import a function called
again I can import a function called Product which means the cross product
Product which means the cross product like combine this with this some number
like combine this with this some number of times and then it's just two more
of times and then it's just two more lines of code I can use what's called a
lines of code I can use what's called a loop and programming so for every
loop and programming so for every passcode in the cross product of all 10
passcode in the cross product of all 10 of those digits repeated a total of four
of those digits repeated a total of four times let me go ahead and rather than
times let me go ahead and rather than bother connecting my phone and hacking
bother connecting my phone and hacking my own phone let me just print out every
my own phone let me just print out every one of those 10,000 codes on the screen
one of those 10,000 codes on the screen and we'll see how fast the hacker could
and we'll see how fast the hacker could do this let me go ahead and print and
do this let me go ahead and print and with an asterisk which is a little trick
with an asterisk which is a little trick to format it nicely I'm going to print
to format it nicely I'm going to print out each of those passcodes and that's
out each of those passcodes and that's it four lines of code maybe 40 seconds
it four lines of code maybe 40 seconds of talking but maybe really four seconds
of talking but maybe really four seconds of code if I actually did this without
of code if I actually did this without the audience and now let me go ahead and
the audience and now let me go ahead and save the file and I'm going to run as we
save the file and I'm going to run as we do every in class of late python of
do every in class of late python of crack.pie and when I hit enter I should
crack.pie and when I hit enter I should see on the screen all 10,000
see on the screen all 10,000 possibilities from 000000 to 9999 so
possibilities from 000000 to 9999 so let's see is it a few seconds minutes
let's see is it a few seconds minutes hours or days done so barely even
hours or days done so barely even seconds plural if that so that should be
seconds plural if that so that should be a little disconcerting because all that
a little disconcerting because all that adversary needs to do is grab your phone
adversary needs to do is grab your phone off the counter plug in a cable and boom
off the counter plug in a cable and boom they're done like there's no ticking
they're done like there's no ticking clock or worries as in the movies or TV
clock or worries as in the movies or TV that maybe you're going to come into the
that maybe you're going to come into the room you don't need that much of a
room you don't need that much of a window of time so what would be better
window of time so what would be better than this well let's consider what our
than this well let's consider what our options might be if we don't want to
options might be if we don't want to just use four-digit passcode some of you
just use four-digit passcode some of you indeed might have better passcodes than
indeed might have better passcodes than that and maybe you use four letter
that and maybe you use four letter passcodes instead so A through Z maybe
passcodes instead so A through Z maybe uppercase and lowercase that starts to
uppercase and lowercase that starts to make things a little more interesting so
make things a little more interesting so should we pull this question too if we
should we pull this question too if we upgrade from four digits to just four
upgrade from four digits to just four letters English letters a through z
letters English letters a through z uppercase and lowercase why don't we go
uppercase and lowercase why don't we go ahead and pull the grip here and ask how
ahead and pull the grip here and ask how many four letter passcodes are there
many four letter passcodes are there instead so this time the range starts at
instead so this time the range starts at four still not the right answer though
four still not the right answer though this time how many four-letter passcodes
this time how many four-letter passcodes are
possible not just just take a couple more
more seconds
seconds all right almost a couple hundred
all right almost a couple hundred responses in already few more
seconds and why don't we go ahead and reveal now
and why don't we go ahead and reveal now the answers which are Okay so we've
the answers which are Okay so we've solved a couple problems at least so we
solved a couple problems at least so we well we well okay someone's just messing
well we well okay someone's just messing with us now all right so it looks like
with us now all right so it looks like most of you 76% of you have claimed it's
most of you 76% of you have claimed it's 7 million plus possibilities so that's
7 million plus possibilities so that's encouraging cuz that's a whole order of
encouraging cuz that's a whole order of magnitude more than before well let's
magnitude more than before well let's figure out how we might do this
figure out how we might do this mathematically so if we've got 26
mathematically so if we've got 26 lowercase 26 uppercase that's 52
lowercase 26 uppercase that's 52 possibilities now for each of those four
possibilities now for each of those four digits so that's 52 times itself four
digits so that's 52 times itself four times which indeed either off the top of
times which indeed either off the top of your head a good guess a calculator on
your head a good guess a calculator on the same device you're using right now
the same device you're using right now indeed gives us 7 million instead well
indeed gives us 7 million instead well what might be slightly better than that
what might be slightly better than that well maybe four characters and this
well maybe four characters and this indeed is what your Macs PCS and phones
indeed is what your Macs PCS and phones are urging us to do nowadays not just
are urging us to do nowadays not just numbers not just letters but like really
numbers not just letters but like really annoying punctuation so it really looks
annoying punctuation so it really looks cryptic not just to the adversary but
cryptic not just to the adversary but also to you and me unfortunately and
also to you and me unfortunately and that's the downside but here now we have
that's the downside but here now we have a mental model and really a
a mental model and really a computational framework via which we can
computational framework via which we can evaluate the security of these and I'll
evaluate the security of these and I'll go ahead and spoil some of the math here
go ahead and spoil some of the math here if we've got 52 uh letters of the
if we've got 52 uh letters of the alphabet uppercase and lowercase 10
alphabet uppercase and lowercase 10 digits and if I count them out on my
digits and if I count them out on my keyboard about 32 punctuation symbols in
keyboard about 32 punctuation symbols in typical English grammar that actually
typical English grammar that actually gives us 94 possibilities now which is
gives us 94 possibilities now which is up from 52 which is up from 10 so now
up from 52 which is up from 10 so now we're really moving and now that would
we're really moving and now that would give us 78 million possibility so
give us 78 million possibility so another order of magnitude now it's
another order of magnitude now it's still going to be relatively fast
still going to be relatively fast because you know what I can actually do
because you know what I can actually do this let me go back into my code here
this let me go back into my code here let me reopen the same program and I can
let me reopen the same program and I can point out just how easy it is to make
point out just how easy it is to make these changes instead of importing
these changes instead of importing digits as before I can import as your uh
digits as before I can import as your uh child might know asky letters which are
child might know asky letters which are a through z uppercase lowercase and I
a through z uppercase lowercase and I can just change this here asky letters
can just change this here asky letters and so this was that first version where
and so this was that first version where we just changed to letters let me now
we just changed to letters let me now rerun the code and instead of seeing
rerun the code and instead of seeing numbers we'll see letters flying across
numbers we'll see letters flying across the screen and if I walk over here to
the screen and if I walk over here to the screen we'll see that by the time I
the screen we'll see that by the time I get here we're halfway through the
get here we're halfway through the entire alphabet lowercase if I now start
entire alphabet lowercase if I now start walking away I think yep we're already
walking away I think yep we're already done now with uppercase as well if I
done now with uppercase as well if I upgrade this slightly further let's go
upgrade this slightly further let's go ahead and take it one more level and
ahead and take it one more level and perhaps do let's say ask letters and
perhaps do let's say ask letters and digits and punctuation and this would be
digits and punctuation and this would be the pythonic way to say that and I'm
the pythonic way to say that and I'm going to add to those letters those same
going to add to those letters those same digits those same punctuation symbols
digits those same punctuation symbols let me shrink my font just so the code
let me shrink my font just so the code still fits on the screen and what we now
still fits on the screen and what we now have is with a two seconds of changes a
have is with a two seconds of changes a program that if I run this version
program that if I run this version whoops without the typographical error
whoops without the typographical error this is what we call in cs50 a bug so
this is what we call in cs50 a bug so now we run the same this is what we call
now we run the same this is what we call in cs50 a second
in cs50 a second bug
bug punctuation okay this is where I cross
punctuation okay this is where I cross my fingers okay so now it's going to be
my fingers okay so now it's going to be a little hard to see as it flies across
a little hard to see as it flies across the screen but you probably are seeing
the screen but you probably are seeing glimpses of some weird punctuation
glimpses of some weird punctuation characters as well and I won't waste our
characters as well and I won't waste our time trying to talk through this because
time trying to talk through this because this is going to take longer we're still
this is going to take longer we're still in the lower case I'm still over here
in the lower case I'm still over here already we've not even gotten to n now O
already we've not even gotten to n now O then P so this is going to run longer
then P so this is going to run longer but let's end with one final question on
but let's end with one final question on the security of all these systems I'm
the security of all these systems I'm going to cancel that by hitting contrl C
going to cancel that by hitting contrl C on my keyboard and let's ask the
on my keyboard and let's ask the question instead if we use eight
question instead if we use eight character passwords so twice as many
character passwords so twice as many characters but even that is not terribly
characters but even that is not terribly long right this is eight characters
long right this is eight characters alone on the stage eight characters
alone on the stage eight characters using letters numbers and punctuation
using letters numbers and punctuation might be better let's do one final vote
might be better let's do one final vote here if we could on your same device how
here if we could on your same device how many eight character possibilities are
many eight character possibilities are there now
there now for these
for these passcodes and now for didn't even make
passcodes and now for didn't even make the list this
the list this time all right few more seconds about
time all right few more seconds about 100 responses so
100 responses so far how about we go ahead and Carter if
far how about we go ahead and Carter if you would't mind let's reveal the
you would't mind let's reveal the results based on the vote a pretty
results based on the vote a pretty decent spread here although the
decent spread here although the quadrillions are quickly buzzing in and
quadrillions are quickly buzzing in and they're contending with the others here
they're contending with the others here looks like 44% of you said quintilian
looks like 44% of you said quintilian 34% said quadrillion and this time for
34% said quadrillion and this time for the first time uh you overbid so indeed
the first time uh you overbid so indeed if we go back to the math here at least
if we go back to the math here at least the majority overbid if we have eight
the majority overbid if we have eight character passcodes that gives us 94
character passcodes that gives us 94 times itself 8 times or 94 to the eth
times itself 8 times or 94 to the eth power and in fact that gives us roughly
power and in fact that gives us roughly 6 quadrillion 95 trillion 689 Bill 385
6 quadrillion 95 trillion 689 Bill 385 mil 410,000 and
mil 410,000 and 86 possible passcodes now what does that
86 possible passcodes now what does that mean well the adversary's algorithm the
mean well the adversary's algorithm the step-by-step code that they write to try
step-by-step code that they write to try to hack into your phone is no different
to hack into your phone is no different and honestly if your passcode is eight
and honestly if your passcode is eight characters long but there're zeros
characters long but there're zeros 00000000000000 you're no more secure
00000000000000 you're no more secure fundamentally you really want to be
fundamentally you really want to be somewhere in The Sweet Spot of that
somewhere in The Sweet Spot of that massive range of values so that if the
massive range of values so that if the adversary tries this Brute Force attack
adversary tries this Brute Force attack just running through all possibilities
just running through all possibilities they will eventually reach your passcode
they will eventually reach your passcode just mathematically it will be there
just mathematically it will be there hopefully though well maybe not
hopefully though well maybe not hopefully you and I and they will be
hopefully you and I and they will be gone from this world because that much
gone from this world because that much time will have passed and if we fact if
time will have passed and if we fact if we do out the math here uh this number
we do out the math here uh this number of seconds for instance is long past uh
of seconds for instance is long past uh when uh uh we will uh no longer be here
when uh uh we will uh no longer be here so that's the sort of measure is we
so that's the sort of measure is we don't sort of fundamentally change the
don't sort of fundamentally change the equation for the adversary it's still
equation for the adversary it's still the same risk it's still the same attack
the same risk it's still the same attack but you significantly drive down the
but you significantly drive down the probability of success on their part or
probability of success on their part or conceptually you drive up the cost to
conceptually you drive up the cost to the adversary and indeed even in the
the adversary and indeed even in the physical world this is true you just
physical world this is true you just want your passcode in the digital world
want your passcode in the digital world really to be better than someone else's
really to be better than someone else's because you want someone else's passcode
because you want someone else's passcode to be the one that the adversary does
to be the one that the adversary does something with with just like in the
something with with just like in the physical world even though it's a bit uh
physical world even though it's a bit uh uncomfortable to consider your house
uncomfortable to consider your house doesn't need to be 100% secure and
doesn't need to be 100% secure and indeed it's difficult to make it such
indeed it's difficult to make it such there's always going to be a a point of
there's always going to be a a point of weakness maybe it's that window the door
weakness maybe it's that window the door or something like that but if your home
or something like that but if your home is more secure than the next door home
is more secure than the next door home just probabilistically you are more
just probabilistically you are more secure you're not secure and indeed any
secure you're not secure and indeed any website you see down the road that says
website you see down the road that says we are secure because we do X Y or Z
we are secure because we do X Y or Z like that's nonsense security is really
like that's nonsense security is really about comparisons and Val ating things
about comparisons and Val ating things if quantitatively relative to some other
if quantitatively relative to some other system relative to some other code so
system relative to some other code so what's the takeaway here well hopefully
what's the takeaway here well hopefully a non-trivial number of you we'll go
a non-trivial number of you we'll go home this weekend on Monday and change
home this weekend on Monday and change at least one passcode um but there's
at least one passcode um but there's going to be a trade-off here and we talk
going to be a trade-off here and we talk about this all the time in cs50 anytime
about this all the time in cs50 anytime we improve something we pay some price
we improve something we pay some price in time and performance and cost
in time and performance and cost somewhere else so what's the downside
somewhere else so what's the downside then of this advice that you should use
then of this advice that you should use minimally eight character
minimally eight character passcodes why might you want to say nay
passcodes why might you want to say nay and not do
and not do this say again you have to remember you
this say again you have to remember you have to remember it right and so here
have to remember it right and so here there's sort of some sociology there's
there's sort of some sociology there's some human behavior you know some of you
some human behavior you know some of you might have colleagues if you're working
might have colleagues if you're working in the real world at least back in
in the real world at least back in healthier times when you had colleagues
healthier times when you had colleagues with desks and cubicles and there's
with desks and cubicles and there's probably one person in the office with
probably one person in the office with like a Post-It note on their monitor
like a Post-It note on their monitor with their passcode you know it's a bit
with their passcode you know it's a bit of a cyber security offense but it's
of a cyber security offense but it's also a sort of real world side effect
also a sort of real world side effect maybe of corporate policies that aren't
maybe of corporate policies that aren't really calibrated for human behavior so
really calibrated for human behavior so we'll see if there's some other defenses
we'll see if there's some other defenses and indeed let me propose that we talk
and indeed let me propose that we talk briefly about one that actually tends to
briefly about one that actually tends to kick in automatically even if your
kick in automatically even if your passcode is not as strong as we've just
passcode is not as strong as we've just seen one of these six quadrillion
seen one of these six quadrillion possibilities well what could we do
possibilities well what could we do instead well as anyone and I'll zoom in
instead well as anyone and I'll zoom in on this here accidentally locked themsel
on this here accidentally locked themsel out of their own phone before like when
out of their own phone before like when does that happen yeah when you try the
does that happen yeah when you try the password too many times yeah so too many
password too many times yeah so too many times maybe your finger's slightly off
times maybe your finger's slightly off maybe you're slightly off and you just
maybe you're slightly off and you just don't input the same passcode correctly
don't input the same passcode correctly after like five times 10 times there's
after like five times 10 times there's some reasonable threshold and why does
some reasonable threshold and why does that happen well Apple and Google
that happen well Apple and Google equivalently figure just
equivalently figure just probabilistically if after 10 guesses
probabilistically if after 10 guesses you still haven't typed in the right
you still haven't typed in the right passcode probably you're not you you're
passcode probably you're not you you're someone else who's picked up your phone
someone else who's picked up your phone so we're just going to go ahead and lock
so we're just going to go ahead and lock you out now what's the effect of this
you out now what's the effect of this well this means now that each of those
well this means now that each of those possible passcodes no longer takes
possible passcodes no longer takes roughly 1 second now it takes roughly 1
roughly 1 second now it takes roughly 1 minute so the attack is still the same
minute so the attack is still the same but if it's now one passcode or 10
but if it's now one passcode or 10 guesses per minute we have significantly
guesses per minute we have significantly by a factor of 60 in this story slowed
by a factor of 60 in this story slowed things down and unfortunately does
things down and unfortunately does anyone know what happens if you screw up
anyone know what happens if you screw up again after a
again after a minute yeah it goes longer it's like
minute yeah it goes longer it's like five minutes and then 10 minutes and uh
five minutes and then 10 minutes and uh Google is kind of obnoxious about it
Google is kind of obnoxious about it they don't even give you a time frame
they don't even give you a time frame they just say try again later and so
they just say try again later and so that keeps not only the adversary out
that keeps not only the adversary out but also potentially you so there in
but also potentially you so there in lies that trade-off if you've forgotten
lies that trade-off if you've forgotten your code if nowadays your finger is
your code if nowadays your finger is slightly wet so the screen isn't
slightly wet so the screen isn't responding correctly these could be
responding correctly these could be usability downsides too so security is
usability downsides too so security is really just about finding The Sweet Spot
really just about finding The Sweet Spot among these various tradeoffs here but
among these various tradeoffs here but there's other mechanisms too and some of
there's other mechanisms too and some of you might recognize this screen from
you might recognize this screen from Gmail via which of course you log in but
Gmail via which of course you log in but after you log into Gmail or similar
after you log into Gmail or similar websites or apps or systems at work
websites or apps or systems at work nowadays especially you might be
nowadays especially you might be presented with uh what's called
presented with uh what's called two-factor authentication and what is
two-factor authentication and what is this in a nutshell in Lay person
this in a nutshell in Lay person terms most many of you if you do
terms most many of you if you do anything digitally at work might have to
anything digitally at work might have to do this now
do this now yeah exactly you get texted at your
yeah exactly you get texted at your phone an additional code that's not your
phone an additional code that's not your same password it's typically a numeric
same password it's typically a numeric code maybe six digits long it expires
code maybe six digits long it expires after a minute or 10 minutes but why is
after a minute or 10 minutes but why is this a good thing well one it's no
this a good thing well one it's no longer just a piece of information that
longer just a piece of information that you know or that you might have written
you know or that you might have written down it's information that changes every
down it's information that changes every time you try to log in but more
time you try to log in but more importantly it's a fundamentally second
importantly it's a fundamentally second factor which means it's not just
factor which means it's not just something you know now it's something
something you know now it's something you have so you for instance are the
you have so you for instance are the only one theoretically that should be
only one theoretically that should be receiving that code and so now the
receiving that code and so now the adversary if they want to get into your
adversary if they want to get into your account not only have to guess or brute
account not only have to guess or brute force or maybe read off of a Post-It
force or maybe read off of a Post-It note your password they also have to
note your password they also have to physically have access now to that phone
physically have access now to that phone so there's still a threat absolutely but
so there's still a threat absolutely but it's not everyone on the internet with
it's not everyone on the internet with an internet connection now it's only the
an internet connection now it's only the people in Starbucks now it's only the
people in Starbucks now it's only the people at work now it's only the people
people at work now it's only the people in your home who might have access to
in your home who might have access to that second Factor so there too it just
that second Factor so there too it just raises the bar to the adversary making
raises the bar to the adversary making it harder more timec consuming more
it harder more timec consuming more geographically impossible for them to
geographically impossible for them to attack you but what's the downside of
attack you but what's the downside of two-factor authentication whether it's a
two-factor authentication whether it's a device or even nowadays it's in software
device or even nowadays it's in software whether it's on your keychain or on your
whether it's on your keychain or on your phone where you're prompted for this
phone where you're prompted for this code what's a downside as some of us
code what's a downside as some of us have probably experienced
have probably experienced too forg you forget your cell phone
too forg you forget your cell phone absolutely right the the factor that you
absolutely right the the factor that you have you don't have with you or maybe
have you don't have with you or maybe you're in a basement somewhere don't
you're in a basement somewhere don't have reception you're on a plane you
have reception you're on a plane you can't get the code and so there too are
can't get the code and so there too are these tradeoffs and even it departments
these tradeoffs and even it departments need to keep that in mind because what
need to keep that in mind because what does that mean for them well if you
does that mean for them well if you don't have your phone with you and you
don't have your phone with you and you are in the habit of calling it to help
are in the habit of calling it to help you fix this now there's a a cost a
you fix this now there's a a cost a human cost maybe even a financial cost
human cost maybe even a financial cost and so it policy nowadays is really just
and so it policy nowadays is really just about finding the right balance and
about finding the right balance and where we want to spend our resources but
where we want to spend our resources but at least raise the bar to the adversary
at least raise the bar to the adversary but of course there's other ways too and
but of course there's other ways too and this is going to be one of our homework
this is going to be one of our homework assignments if you will after today
assignments if you will after today there's this software called password
there's this software called password managers and no need to buzz in on your
managers and no need to buzz in on your phone but maybe with a physical hand how
phone but maybe with a physical hand how many folks here use a password manager
many folks here use a password manager okay let me ballpark this at 10 20%
okay let me ballpark this at 10 20% perhaps Okay so we've got 80% upside
perhaps Okay so we've got 80% upside here and a lesson learned potentially so
here and a lesson learned potentially so a password manager is just a piece of
a password manager is just a piece of software on your Mac your PC or your
software on your Mac your PC or your phone nowadays that manages your
phone nowadays that manages your passwords well what is that mean when
passwords well what is that mean when you go to a website for the first time
you go to a website for the first time or you download an app for the first
or you download an app for the first time and you have to create an account
time and you have to create an account you can can still use your email address
you can can still use your email address or David as your username or whatever
or David as your username or whatever your name might be so you don't have to
your name might be so you don't have to change that methodology but instead of
change that methodology but instead of typing in one 2 3 4 5 six as your same
typing in one 2 3 4 5 six as your same password for that website or app as well
password for that website or app as well as for every other now you use the
as for every other now you use the password manager software to generate
password manager software to generate something difficult to guess for you
something difficult to guess for you that is you tell the password manager
that is you tell the password manager give me an8 character random passcode
give me an8 character random passcode not 0000 but something with punctuation
not 0000 but something with punctuation with numbers with letters and better yet
with numbers with letters and better yet the password manager is the name
the password manager is the name suggests remembers that password for you
suggests remembers that password for you and the next time you go to another
and the next time you go to another website you do it again with a
website you do it again with a completely different password maybe same
completely different password maybe same username maybe two-factor authentication
username maybe two-factor authentication but different password different
but different password different password different password and it
password different password and it doesn't have to be eight I mean I'm in
doesn't have to be eight I mean I'm in the habit of using a dozen two dozen
the habit of using a dozen two dozen characters in total and at that point I
characters in total and at that point I can't even pronounce the number of
can't even pronounce the number of possibilities because it goes well be
possibilities because it goes well be Beyond uh the quadrillions so the
Beyond uh the quadrillions so the probability that someone's going to get
probability that someone's going to get into one of those accounts for me now is
into one of those accounts for me now is very very very low and they're going to
very very very low and they're going to take less interest in me and maybe more
take less interest in me and maybe more interest in someone else that's not
interest in someone else that's not using as good of a password now what
using as good of a password now what does this mean in real terms well when
does this mean in real terms well when you go to log into that managed site you
you go to log into that managed site you don't manually type your password
don't manually type your password anymore in fact you don't generally even
anymore in fact you don't generally even need to know it nowadays I probably
need to know it nowadays I probably don't know 90 plus 99% of my passwords I
don't know 90 plus 99% of my passwords I entrust them to this password manager
entrust them to this password manager now of course you'd like to think that
now of course you'd like to think that the password manager itself is secure so
the password manager itself is secure so what might that mean well those of you
what might that mean well those of you who do use a password manager how do you
who do use a password manager how do you access that software itself what's
access that software itself what's protecting your data in your
protecting your data in your understanding so maybe Biometrics like
understanding so maybe Biometrics like your your face ID or maybe your
your your face ID or maybe your fingerprint or maybe more simply what
fingerprint or maybe more simply what else password maybe just a password and
else password maybe just a password and hopefully that password that primary
hopefully that password that primary password that gatekeeper is not itself 1
password that gatekeeper is not itself 1 2 3 4 5 6 otherwise it doesn't matter
2 3 4 5 6 otherwise it doesn't matter how secure all of the others are but if
how secure all of the others are but if you're willing to put in the effort and
you're willing to put in the effort and pick one pretty long somewhat random
pick one pretty long somewhat random very unguessable password that you just
very unguessable password that you just promise to commit to memory and maybe
promise to commit to memory and maybe for backup you literally print it out
for backup you literally print it out and put it in a safe deposit box or a
and put it in a safe deposit box or a safe or just kind of hide it somewhere
safe or just kind of hide it somewhere physically that there's very low
physically that there's very low probability someone's going to find the
probability someone's going to find the backup copy that might be Al loan but of
backup copy that might be Al loan but of course the flip side is now if you
course the flip side is now if you forget that Ma that primary password
forget that Ma that primary password you've now lost all of the eggs in the
you've now lost all of the eggs in the basket if someone gets that primary
basket if someone gets that primary password now they have access to
password now they have access to everything so that's rather the
everything so that's rather the trade-off but I dare say you're probably
trade-off but I dare say you're probably less threatened depending on your family
less threatened depending on your family uh uh by the people immediately around
uh uh by the people immediately around you than the billions of other people on
you than the billions of other people on the internet that have access
the internet that have access potentially to those same systems so
potentially to those same systems so there to it's a trade-off and it's up to
there to it's a trade-off and it's up to you to decide whether or not to manage
you to decide whether or not to manage your passwords in this way but if you
your passwords in this way but if you were on that top 10 list or even if
were on that top 10 list or even if you're not but you can think of several
you're not but you can think of several accounts that all have the same password
accounts that all have the same password you're probably going to benefit from
you're probably going to benefit from something like this and why is it bad to
something like this and why is it bad to be clear to use the same password on
be clear to use the same password on multiple sites in case that's never sort
multiple sites in case that's never sort of dawned
of dawned in thought why is that a bad thing to
in thought why is that a bad thing to reuse a password on different websites
reuse a password on different websites different apps any intuition yeah and
back exactly once it's attacked you can the adversary presumably by transitivity
the adversary presumably by transitivity can see oh well if this user's username
can see oh well if this user's username is mailin harvard.edu on this website
is mailin harvard.edu on this website and their password is foolishly 1 2 3 4
and their password is foolishly 1 2 3 4 56 or even something way more
56 or even something way more complicated they can probably just
complicated they can probably just assume with high probability that if I'm
assume with high probability that if I'm being a little reckless let's try access
being a little reckless let's try access mailin at harvard.edu other accounts
mailin at harvard.edu other accounts other apps using that exact same
other apps using that exact same password and so by transitivity
password and so by transitivity essentially you're putting your other uh
essentially you're putting your other uh Accounts at risk so what's maybe a
Accounts at risk so what's maybe a takeaway minimally here I would start to
takeaway minimally here I would start to reconsider your passcodes on your most
reconsider your passcodes on your most important data Maybe it's medical maybe
important data Maybe it's medical maybe it's Financial maybe it's email anything
it's Financial maybe it's email anything remotely personal that you really
remotely personal that you really wouldn't want to have access uh do you
wouldn't want to have access uh do you necessarily need the same form level of
necessarily need the same form level of security on eCommerce sites or sites
security on eCommerce sites or sites that you don't really care about or that
that you don't really care about or that you signed up for once and after that H
you signed up for once and after that H that's it probably not so you can decide
that's it probably not so you can decide for yourself but again software like a
for yourself but again software like a password manager and these are just some
password manager and these are just some of the possibilities out there um are
of the possibilities out there um are probably to be your friend a couple of
probably to be your friend a couple of these are free they come with Windows or
these are free they come with Windows or Mac OS a couple are commercial Harvard
Mac OS a couple are commercial Harvard has a site license for students for uh
has a site license for students for uh one of these as well so there are
one of these as well so there are options out there but what else do
options out there but what else do people use what else can people use to
people use what else can people use to keep their system secure so most of us
keep their system secure so most of us nowadays have probably heard of
nowadays have probably heard of encryption this technique for just kind
encryption this technique for just kind of scrambling information so when you
of scrambling information so when you want to send a message an email or
want to send a message an email or upload a a photograph or use your credit
upload a a photograph or use your credit card hopefully it's not just being sent
card hopefully it's not just being sent out for all to see but there's some kind
out for all to see but there's some kind of scrambling going on and some fancy
of scrambling going on and some fancy mathematics ensure that encryption
mathematics ensure that encryption ensures that only you the sender and
ensures that only you the sender and someone else the receiver can
someone else the receiver can theoretically see what that credit card
theoretically see what that credit card number is what that message is what that
number is what that message is what that photograph is instead so encryption is
photograph is instead so encryption is sort of Common Place nowadays both in
sort of Common Place nowadays both in websites and apps and ATMs and other
websites and apps and ATMs and other such devices but how does it work well
such devices but how does it work well back in week two of cs50 uh your child
back in week two of cs50 uh your child learned learned a little something about
learned learned a little something about encryption otherwise known as
encryption otherwise known as cryptography and one of the algorithms
cryptography and one of the algorithms we talked about was quite simply
we talked about was quite simply something like this this is what we
something like this this is what we might call uh not only cs50 but plain
might call uh not only cs50 but plain text so very plain text that in this
text so very plain text that in this case is English and obviously everyone
case is English and obviously everyone in the room can read it but what if I
in the room can read it but what if I wanted to send this message out to
wanted to send this message out to someone in this room or out on the
someone in this room or out on the Internet or maybe equivalently back in
Internet or maybe equivalently back in the day maybe write a message down on a
the day maybe write a message down on a scrap of paper in grade school and pass
scrap of paper in grade school and pass a secret note a secret love note to
a secret note a secret love note to someone in class with hopes that the
someone in class with hopes that the teach or any other students in the class
teach or any other students in the class can't intercept it and read it well you
can't intercept it and read it well you probably don't want to say this is cs50
probably don't want to say this is cs50 or I love you or anything remotely
or I love you or anything remotely sensitive but rather maybe you want to
sensitive but rather maybe you want to encrypt it and let's change the the T to
encrypt it and let's change the the T to a u maybe change the H to an i the I to
a u maybe change the H to an i the I to a j the S to a t the I to a j the S to a
a j the S to a t the I to a j the S to a t again the C to a d the S to a T and
t again the C to a d the S to a T and we'll just leave the numbers alone even
we'll just leave the numbers alone even though I worry someone could probably
though I worry someone could probably guess what this now does say nonetheless
guess what this now does say nonetheless but what was the algorithm as I rattled
but what was the algorithm as I rattled those ch changes off whether student
those ch changes off whether student from week two or parent from week now
from week two or parent from week now yeah a on shift just a onlet shift and
yeah a on shift just a onlet shift and this is more sophisticatedly called a
this is more sophisticatedly called a rotational Cipher or a Caesar Cipher
rotational Cipher or a Caesar Cipher after Caesar back in the day it's
after Caesar back in the day it's relatively simplistic but back in the
relatively simplistic but back in the day it's not so simplistic if you're the
day it's not so simplistic if you're the first person in the world to ever use it
first person in the world to ever use it or think of it but nowadays this is not
or think of it but nowadays this is not actually what we use but it's similarly
actually what we use but it's similarly mathematical in nature it's not quite as
mathematical in nature it's not quite as simple as just adding one or subtracting
simple as just adding one or subtracting one to go from now what we'd call text
one to go from now what we'd call text to plain test but it's similarly math
to plain test but it's similarly math that's involved and let me just
that's involved and let me just stipulate that the way the math works is
stipulate that the way the math works is that the sender and the receiver just
that the sender and the receiver just have to have in mind some kind of secret
have to have in mind some kind of secret and the Secret in this case would very
and the Secret in this case would very trivially be one but it could be a much
trivially be one but it could be a much bigger much more unguessable number or
bigger much more unguessable number or maybe some other secret we share the
maybe some other secret we share the presumption being that my classmates my
presumption being that my classmates my teacher in that grade school classroom
teacher in that grade school classroom if they don't know what that secret is
if they don't know what that secret is that number is yeah they could try to
that number is yeah they could try to brute force it and try all possible
brute force it and try all possible mathematics plus one plus 2 plus three
mathematics plus one plus 2 plus three but that's going to take them some time
but that's going to take them some time and they probably don't care enough and
and they probably don't care enough and so my data might be therefore relatively
so my data might be therefore relatively secure but we use encryption all the
secure but we use encryption all the time nowadays and so for instance this
time nowadays and so for instance this is at the start of most URLs nowadays
is at the start of most URLs nowadays even if you don't type it yourself with
even if you don't type it yourself with that said Safari and even Chrome now or
that said Safari and even Chrome now or kind of simplifying if not dumbing down
kind of simplifying if not dumbing down user interfaces to just hide details
user interfaces to just hide details that you and I as sort of normal users
that you and I as sort of normal users don't need to see 24/7 but it is there
don't need to see 24/7 but it is there and if in fact on your phone or laptop
and if in fact on your phone or laptop you click on the URL even if it's super
you click on the URL even if it's super short initially you'll probably see the
short initially you'll probably see the whole thing starting with this and the s
whole thing starting with this and the s means secure the s means that encryption
means secure the s means that encryption is being used but there's other forms of
is being used but there's other forms of this not just when you visit websites
this not just when you visit websites there's this endtoend encryption which
there's this endtoend encryption which is being talked about more nowadays
is being talked about more nowadays especially during coid times with so
especially during coid times with so many more of us on video and talking
many more of us on video and talking about uh more sensitive things tele
about uh more sensitive things tele medicine talking to doctors things that
medicine talking to doctors things that you also wouldn't want to verbally or
you also wouldn't want to verbally or visually get out into the wild just like
visually get out into the wild just like text what's different about endtoend
text what's different about endtoend encryption versus HTT GPS and the type
encryption versus HTT GPS and the type of encryption that most of us use every
of encryption that most of us use every day on websites
day on websites alone end to end encryption is sort of a
alone end to end encryption is sort of a better feature that you want to
better feature that you want to increasingly seek when using services
increasingly seek when using services like Zoom or Microsoft teams or Whatsapp
like Zoom or Microsoft teams or Whatsapp or the
or the like any instincts here yeah over on the
right good so the encryption the scrambling of information happens in The
scrambling of information happens in The Source the sender and the destination
Source the sender and the destination the receiver without a so-called
the receiver without a so-called middleman in between and this is
middleman in between and this is actually very different from most
actually very different from most contexts nowadays that use just https
contexts nowadays that use just https because when you're using https to buy
because when you're using https to buy something on Amazon securely with your
something on Amazon securely with your credit card well of course Amazon needs
credit card well of course Amazon needs to be able to decrep the message at the
to be able to decrep the message at the end of the day and so that's fine but
end of the day and so that's fine but even when you're using services like
even when you're using services like video conferencing or maybe text
video conferencing or maybe text messaging nowadays well if you're using
messaging nowadays well if you're using Whatsapp that's owned by meta and if
Whatsapp that's owned by meta and if you're using Instagram that's owned by
you're using Instagram that's owned by meta there's a lot of middlemen in these
meta there's a lot of middlemen in these apps that we're using and if they were
apps that we're using and if they were only using encryption period or only
only using encryption period or only using something like https yes your en
using something like https yes your en your connection from you to WhatsApp and
your connection from you to WhatsApp and in turn to the recipient might very well
in turn to the recipient might very well be secure on each end of that channel
be secure on each end of that channel but meta in the between the company and
but meta in the between the company and any other company in between could
any other company in between could theoretically For Better or For Worse be
theoretically For Better or For Worse be looking at that data whether it's to
looking at that data whether it's to mine it for advertising purposes whether
mine it for advertising purposes whether it's to Snoop on data that you're
it's to Snoop on data that you're sending that is not end to-end
sending that is not end to-end encryption if the middleman a company
encryption if the middleman a company typically has technically access to that
typically has technically access to that data now zoom and Microsoft teams and
data now zoom and Microsoft teams and WhatsApp and iMessage and other services
WhatsApp and iMessage and other services with which you're familiar increasingly
with which you're familiar increasingly are offering stronger guarantees of
are offering stronger guarantees of encryption whereby it's indeed between
encryption whereby it's indeed between parties A and B and not the one in the
parties A and B and not the one in the middle now there's downsides here and
middle now there's downsides here and you can actually see this kind of
you can actually see this kind of functionality manifest in certain
functionality manifest in certain settings for instance besides iMessage
settings for instance besides iMessage uh which just does this for you on
uh which just does this for you on iPhones or Macs besides Zoom um you can
iPhones or Macs besides Zoom um you can actually fine-tune these settings indeed
actually fine-tune these settings indeed within Zoom itself s so here's a
within Zoom itself s so here's a screenshot that I took last night of
screenshot that I took last night of just what the user interface looks like
just what the user interface looks like today to create a new Zoom meeting with
today to create a new Zoom meeting with the latest version of Zoom software and
the latest version of Zoom software and maybe unbeknownst to you there's a
maybe unbeknownst to you there's a choice of buttons down here and most
choice of buttons down here and most likely yours is by default on enhanced
likely yours is by default on enhanced encryption which is brilliant marketing
encryption which is brilliant marketing speak because it's just encryption it's
speak because it's just encryption it's not enhanced it actually ironically
not enhanced it actually ironically means worse than this um but they want
means worse than this um but they want you using it most likely why well it's a
you using it most likely why well it's a little easier to implement it's a little
little easier to implement it's a little less expensive for them computationally
less expensive for them computationally and to be fair enhanced encryption does
and to be fair enhanced encryption does scramble the data but not in a way that
scramble the data but not in a way that Zoom can't see it Zoom can indeed see it
Zoom can't see it Zoom can indeed see it but that's actually a plus in some
but that's actually a plus in some context because if you want to do like
context because if you want to do like Cloud recordings and you want a meeting
Cloud recordings and you want a meeting recorded not on your Mac or PC but like
recorded not on your Mac or PC but like let Zoom deal with that if you want
let Zoom deal with that if you want automatic transcription nowadays so the
automatic transcription nowadays so the words to appear whether it's English or
words to appear whether it's English or something else on the screen well you
something else on the screen well you can't really lock Zoom or any other
can't really lock Zoom or any other middleman out of that because someone
middleman out of that because someone needs to save it to the cloud someone
needs to save it to the cloud someone needs to translate the voice to those
needs to translate the voice to those English or some other language words so
English or some other language words so enhanced encryption enables those
enhanced encryption enables those features but they also allow a bad actor
features but they also allow a bad actor malicious employee someone who's just
malicious employee someone who's just nosy at Zoom or the equivalent middleman
nosy at Zoom or the equivalent middleman to just kind of poke around your video
to just kind of poke around your video conference and hear what you've said or
conference and hear what you've said or see what you've typed as well unless you
see what you've typed as well unless you instead check this box as well so
instead check this box as well so increasingly look for mentions of end
increasingly look for mentions of end to-end encryption or give that some
to-end encryption or give that some thought when you choose a technology via
thought when you choose a technology via which to communicate with someone
which to communicate with someone whether it's within your family or
whether it's within your family or without as well now last but not least
without as well now last but not least there's other applications of encryption
there's other applications of encryption too and this too might be a lesson
too and this too might be a lesson learned as well full dis encryption so a
learned as well full dis encryption so a dis is like where your data stored in
dis is like where your data stored in your Mac or PC or even your phone and
your Mac or PC or even your phone and full dis encryption just means ideally
full dis encryption just means ideally that all of your data is encrypted that
that all of your data is encrypted that is somehow scrambled now hopefully your
is somehow scrambled now hopefully your password for your computer or phone is
password for your computer or phone is good enough that even though the device
good enough that even though the device is encrypted with that password at least
is encrypted with that password at least you'll remember it and your phone or
you'll remember it and your phone or your Mac or PC will automatically
your Mac or PC will automatically decrypt it for you of course you can't
decrypt it for you of course you can't scramble the information and hide it
scramble the information and hide it from from ourselves one of us at least
from from ourselves one of us at least for these devices needs to have access
for these devices needs to have access but full dis encryption typically means
but full dis encryption typically means that at least when you close the laptop
that at least when you close the laptop lid or power down for the night that
lid or power down for the night that even if someone else steals that device
even if someone else steals that device opens the lid unless they don't unless
opens the lid unless they don't unless they have your passcode they can't even
they have your passcode they can't even plug in fancy cables to the device and
plug in fancy cables to the device and just rip the zeros and ones off of the
just rip the zeros and ones off of the device and see what's actually there
device and see what's actually there full dis encryption means they could do
full dis encryption means they could do that but they would just see seemingly
that but they would just see seemingly random zeros and ones now there's a
random zeros and ones now there's a downside here too this might slow things
downside here too this might slow things down potentially but it is a feature
down potentially but it is a feature increasingly that's offered and is
increasingly that's offered and is absolutely something you should consider
absolutely something you should consider enabling um in general especially if
enabling um in general especially if your laptop or phone travels with you
your laptop or phone travels with you and certainly your phone does or if you
and certainly your phone does or if you plan to donate or sell or give away a
plan to donate or sell or give away a device you don't want to leave all of
device you don't want to leave all of the zeros and ones the remnants of your
the zeros and ones the remnants of your own sensitive data passed on there so
own sensitive data passed on there so Windows has a feature called bit Locker
Windows has a feature called bit Locker Mac OS has a feature called file Vault
Mac OS has a feature called file Vault there's commercial options as well but
there's commercial options as well but generally we're at the point now in 2022
generally we're at the point now in 2022 we're clicking a button is suff
we're clicking a button is suff efficient to enable these features with
efficient to enable these features with that said don't rush into all of these
that said don't rush into all of these decisions I would make backups of your
decisions I would make backups of your data and don't maybe email cs50 if
data and don't maybe email cs50 if something goes wrong with that process
something goes wrong with that process but I would do your own due diligence
but I would do your own due diligence but this too would be a menu of
but this too would be a menu of possibilities and now the bad side the
possibilities and now the bad side the downside of what seems to be great this
downside of what seems to be great this notion of full dis encryption
notion of full dis encryption unfortunately just as we can encrypt our
unfortunately just as we can encrypt our data to protect it from the adversaries
data to protect it from the adversaries so can the adversaries if they get into
so can the adversaries if they get into our devices encrypt our data and do what
our devices encrypt our data and do what not tell us that secret key and so this
not tell us that secret key and so this is generally applied in the context of
is generally applied in the context of ransomware which tragically you
ransomware which tragically you increasingly hear about in Hospital
increasingly hear about in Hospital Systems school systems municipalities
Systems school systems municipalities where systems are getting attacked and
where systems are getting attacked and the data is not just getting stolen
the data is not just getting stolen because what is the adversary typically
because what is the adversary typically need with like local municipal or even
need with like local municipal or even Hospital data the value to the adversary
Hospital data the value to the adversary is encrypting all of the hospital all of
is encrypting all of the hospital all of the mpali data preventing them from
the mpali data preventing them from accessing it if they have no backups or
accessing it if they have no backups or the like and so ransomware is liter
the like and so ransomware is liter Lally about trying to convince someone
Lally about trying to convince someone to pay you money or pay you Bitcoin or
to pay you money or pay you Bitcoin or something like that to give you that
something like that to give you that secret key and this key in this case is
secret key and this key in this case is surely more sophisticated than the
surely more sophisticated than the number one but it's really the same idea
number one but it's really the same idea so here too yet again a trade-off just
so here too yet again a trade-off just as we sort of invent something for good
as we sort of invent something for good it can also be used for evil and so to
it can also be used for evil and so to speak as well but it's really the same
speak as well but it's really the same underlying principles even though we
underlying principles even though we keep seeing it and hearing about it in
keep seeing it and hearing about it in these different forms and lastly if only
these different forms and lastly if only because folks are generally familiar but
because folks are generally familiar but don't necessarily know what it is that
don't necessarily know what it is that it's doing for them browsers nowadays
it's doing for them browsers nowadays have what's often called incognito mode
have what's often called incognito mode or private mode which has nothing to do
or private mode which has nothing to do with encryption but does have to do with
with encryption but does have to do with cyber security or really cyber privacy
cyber security or really cyber privacy keeping your data from prying eyes uh
keeping your data from prying eyes uh incognito mode if you open it in Chrome
incognito mode if you open it in Chrome for instance looks a little something
for instance looks a little something like this and we use it in cs50 when
like this and we use it in cs50 when introducing students as we did last week
introducing students as we did last week to web programming because it in effect
to web programming because it in effect lets you start with a clean slate like a
lets you start with a clean slate like a brand new browser that has never visited
brand new browser that has never visited any websites before which is good for
any websites before which is good for just diagnosing problems but it's often
just diagnosing problems but it's often commonly used if you want to log into
commonly used if you want to log into maybe your Gmail account on someone
maybe your Gmail account on someone else's computer and you don't want your
else's computer and you don't want your password being saved or you want to
password being saved or you want to visit some website where you don't want
visit some website where you don't want the URL or the Search terms ending up in
the URL or the Search terms ending up in your autocomplete history so there's
your autocomplete history so there's multiple uses for incognito mode but
multiple uses for incognito mode but what does it really do well it doesn't
what does it really do well it doesn't stop your company it doesn't stop your
stop your company it doesn't stop your University your internet service
University your internet service provider be it Comcast Verizon or the
provider be it Comcast Verizon or the like from knowing what websites you go
like from knowing what websites you go to because ask your student a couple
to because ask your student a couple weeks ago we talked about actually a
weeks ago we talked about actually a week ago we talked about how the
week ago we talked about how the internet works and unfortunately
internet works and unfortunately computer has an IP address which is
computer has an IP address which is unique identifier which goes out anytime
unique identifier which goes out anytime you go anywhere incognito mode or not so
you go anywhere incognito mode or not so this isn't really covering your tracks
this isn't really covering your tracks outside of your office or outside of
outside of your office or outside of your home or outside of your company but
your home or outside of your company but it is at least throwing away local
it is at least throwing away local information and so we'll talk in fact in
information and so we'll talk in fact in cs50 is week nine this coming Monday
cs50 is week nine this coming Monday about cookies which you might generally
about cookies which you might generally know about and what are called sessions
know about and what are called sessions and so long story short what incognito
and so long story short what incognito mode does is it throws away when you
mode does is it throws away when you close the window any locally stored
close the window any locally stored information to the these things called
information to the these things called cookies which are sort of like virtual
cookies which are sort of like virtual handstamps that just remember what
handstamps that just remember what you've logged in as or what's in your
you've logged in as or what's in your shopping cart or the like but it doesn't
shopping cart or the like but it doesn't hide any information from anyone outside
hide any information from anyone outside of your own Mac or PC it only prevents
of your own Mac or PC it only prevents those local prying eyes so there too
those local prying eyes so there too even though we have tools that many of
even though we have tools that many of you are probably in the habit of using
you are probably in the habit of using or thinking you should use to um be more
or thinking you should use to um be more private be more secure on the internet
private be more secure on the internet what we do really in cs50 boast week's
what we do really in cs50 boast week's uh past and future is talk about how
uh past and future is talk about how these Technologies work so that
these Technologies work so that ultimately we have all the more of an
ultimately we have all the more of an educated citizenry um here among
educated citizenry um here among undergrads and here as well as on line
undergrads and here as well as on line so that you can apply these same Lessons
so that you can apply these same Lessons Learned to problems you'll encounter in
Learned to problems you'll encounter in the future so as promised the homework
the future so as promised the homework one should probably use a password
one should probably use a password manager doesn't have to be one of those
manager doesn't have to be one of those ones on the list but at least starting
ones on the list but at least starting that conversation maybe with someone who
that conversation maybe with someone who does maybe the you know it's often the
does maybe the you know it's often the you know the the the student in your
you know the the the student in your family perhaps who can advise you on
family perhaps who can advise you on some of these Technologies consider
some of these Technologies consider using a password manager to using
using a password manager to using two-factor authentication whether it's
two-factor authentication whether it's your phone or some key fob or or the
your phone or some key fob or or the like but at least seeking out that
like but at least seeking out that feature at least for accounts that you
feature at least for accounts that you really care about your email social
really care about your email social media Financial medical anything where
media Financial medical anything where you'd be embarrassed at best or really
you'd be embarrassed at best or really violated at worst if that kind of
violated at worst if that kind of information got out and then
information got out and then increasingly using not just encryption
increasingly using not just encryption which you kind of get automatically for
which you kind of get automatically for most Technologies today but increasingly
most Technologies today but increasingly choosing technologies that offer
choosing technologies that offer stronger guarantees that keep those
stronger guarantees that keep those middlemen those companies out of the way
middlemen those companies out of the way if only so that you can trust with
if only so that you can trust with higher probability that only party B
higher probability that only party B knows what party a has said or sent now
knows what party a has said or sent now this of course was a whirlwind tour
this of course was a whirlwind tour there's so much more that you can do
there's so much more that you can do online indeed this course cs50 can be
online indeed this course cs50 can be taken for free online via platforms like
taken for free online via platforms like edex at edx.org cs50 I thought it might
edex at edx.org cs50 I thought it might be appropriate to end on this note if
be appropriate to end on this note if anyone would like to conjecture before
anyone would like to conjecture before we start playing music and adjourn for
we start playing music and adjourn for lunch what our final message here
lunch what our final message here is if we reverse the plus one and maybe
is if we reverse the plus one and maybe start minus one here minus one here and
start minus one here minus one here and indeed thank you so much for coming this
indeed thank you so much for coming this was
was [Applause]
[Applause] [Music]
[Music] cs50
Click on any text or timestamp to jump to that moment in the video
Share:
Most transcripts ready in under 5 seconds
One-Click Copy125+ LanguagesSearch ContentJump to Timestamps
Paste YouTube URL
Enter any YouTube video link to get the full transcript
Transcript Extraction Form
Most transcripts ready in under 5 seconds
Get Our Chrome Extension
Get transcripts instantly without leaving YouTube. Install our Chrome extension for one-click access to any video's transcript directly on the watch page.