all right hello world this is cs50 live
my name is Colton Ogden today we're
joined by the man himself
David Malin hello everyone good to see
everyone on stream and not just in the
chat today we are doing something pretty
cool today pretty fun we're doing a from
scratch of Python tutorial and my
understanding is that you've actually
brought some questions and topics
problem challenges that I haven't seen
that we're gonna implement and I'm gonna
sort of be a proxy to somebody who maybe
is learning for the first time indeed so
I'm gonna pretty much assume that Colton
may or may not actually know Python and
so along the way we'll try to solve some
problems actively by a coding we'll pull
things up by Google or Stack Overflow as
we go and frankly try to approximate the
experience that Colton and I arguably do
every day when we're writing code to
sort of figure out problems that we
cannot get solved sure I guess maybe we
should start off first off by maybe
explaining what Python is and the honest
folks we've advertised this as sort of
an experience for people who may not
have even programmed or read but at
least have not programmed in Python
before yeah so why don't we assume that
no one here is really that familiar with
Python but if you are that's fine
hopefully we can fill in some gaps in
your knowledge stop us at any point via
the chat if you have a question there's
no dumb questions here if we take them
for granted that you know what some top
concept is or some function is or some
feature just chime and say hey everyone
what what did they mean by that and so
that we can answer here on stream so
Python is a very popular programming
language it's a little different from
languages like C and C++ and a few
others that you would traditionally
compile recall that if you've ever
programmed in one of those languages you
have to actually run some program called
a compiler to convert your source code
the text that you write to machine code
the zeros and ones that the computer
actually understands nice thing about
Python is that we don't have to do that
what would is you want to characterize
Python for folks to distinguish it from
that process yeah no I mean Python is
nice it's it's essentially it runs
almost in this it's called a virtual
machine but it's almost like having a
computer inside a computer and it
executes these you know with the
traditional programming language you're
writing text and I'll switch over to my
computer here just that we can maybe
frame this visually a little bit better
but not Chrome in BS code by the way
this is my text editor of choice it just
vs code you need in order to
any source code some sort of text editor
application you can even use something
like text edit for example if I pull it
TextEdit on my Mac it's a I'm not sure
what that email is there but I think
that might have been from another stream
but essentially what you do is you you
have some program that you can write
text into and usually you don't want to
write it in what's called rich text or
you have formatting you write it in
plain text like this and you write your
instructions to the computer a bunch of
lines tell the computer icon
no this isn't quite Python not unless
you're unless you don't know how to
write good variable names to say this
might look and this might be some
people's Python code but you essentially
go through and you write a set of
instructions for the computer like this
top down and the computer will execute
them but computers don't understand
arbitrary textual information the
computer can only speak in certain
languages computer labs specifically its
own machine code which is binary code
which we talked about in cs50 one of the
more underlying principles of that David
has an awesome chance of lectures on our
YouTube channel
but long story short somewhere along the
line you have to convert all your
instructions into something that the
computer can understand and with
traditional language that you alluded to
before
you do this with the help of a compiler
they take this text and end up
translating it into you know they
basically this which is the computer's
native sort of tongue mm-hmm but how is
Python different here python is
different in that it doesn't actually
perform this step explicitly along the
line it still actually happens but it
basically translates all the lines that
you've written when you just load the
script into the program that is itself
called Python yeah and you can think of
Python then is an interpreter this sort
of middleman that translates between
what you write and what the computer
actually understands so you can think of
Python the program which will run in
just a couple minutes is a program that
reads your own code top to bottom left
to right and it just does what you say
you don't have to actually use something
like Visual Studio or GCC or clang or
any number of other compilers explicitly
all of that sort of happens before you
automatically thanks to the
interpretation process along the way in
D you can even fire up let's see if if
you have it installed on your machine
for example here I have Python three
there's two different versions of Python
3 long story short Python 3 is the new
way
the way that is going to be the official
way very shortly as of 2020 but if I
type Python 3 and my command line
assuming you have it installed you can
see that I actually have this sort of
prompt here these three arrows which
append I can actually type in
instructions and it will immediately
execute things for example if I wanted
to say print David J Malan like like
this assuming that I type it correctly
it'll actually literally do it without
needing to do that intermediary
compilation stuff yeah absolutely this
is an interactive interpreter so we'll
just do what you say the downside of
course is that we're not really saving
any of our work along the way so it's
better generally to write your programs
in an actual text editor like vs code or
sublime or atom or something else so
that you can save your code and let the
interpreter run it and rerun it again
and again so why don't we go ahead and
do that you want to go ahead and open up
a blank editor window so for those of
you following along at home if you want
to go ahead and Google the S code or
Visual Studio code you're welcome to
download the same program for free atom
sublime text and notepad plus plus and a
bunch of others are very popular on Macs
and PCs and Linux machines but odds are
if you're tuning in you probably have
some form of text editor but if not feel
free to try one of those and then what
you should also do if you don't have it
already is make sure your computer if
you do want to follow along has Python
installed
so again python is not only a language
it's an interpreter a program that will
understand your use of that language and
if you go to python org slash downloads
you'll find yourself at the official
website where you can download the
python version 3 installer to make sure
that you have that on your machine
depending on how your machine was
configured you may have Python already
as Colton said but it might be an older
version Python 2 that's officially going
to be end-of-life soon that is it's
being phased out in favor finally of
Python 3 so everything we do today is
Python 3 specific but beware because
when you're googling when you're looking
on Stack Overflow there's a lot of good
answers to old Python code out there so
you don't want to be led astray
Simo has a sweet comment says david
because of you i stopped playing video
games all day I learned it
have a real job at an ecomcon wow that's
amazing I see Andres taking exception to
the fact that we haven't promoted Idol
there's a few other IDE s and editors as
well this is not an exhaustive list
what's nice about things like vs code is
that they are first and foremost a text
editor and you can layer more features
on top of them somebody asked out what
the s code am I using I think I'm using
Dracula right now it's just at the end
of the last stream but I actually prefer
the noctus theme so look real quick just
the important stuff out of the way first
this has nothing to do with Python but
we're gonna switch that Noctis color
that's the one that I recently have like
but they all a lot of the defaults look
very nice nice and you want it let's go
ahead and get rid of the Explorer for
now but let's just go ahead and save
this program as hello dot pi so hello is
gonna be the name of the program pi
indicates that this is a Python program
you don't need number three anywhere in
there you still just use dot pi so our
first program well let's keep it super
simple and literally write the program
that everyone in the world seems to
start writing code in which is just
hello world so instead of David J Malan
how would you go ahead and Python and
just write a program that prints out
literally hello world
I would I would use the print function
which comes with Python and actually the
nice thing about vs code is that it
gives you a lot of this sort of built-in
intelligence depending on how you like
to develop or it will actually tell you
when you write functions you know sort
of what the function it what the
function expects what it does so this is
a nice thing about modern text editors
and integrated development environments
IDE using and you said hello world so
I'm just gonna type in the string hello
world and that's it yeah but some and
you'll notice now that you get syntax
highlighting with programs like this I
see there's a shout-out to Pi charm as
well which is a popular editor for
Python as well yes we do know that IO
might come with the default installation
but we're just we're just focusing on
Python today you can use any text editor
out there that you would like it's
amazing folks in the chat are actually
typing typing some responses to what you
said in is that actually this is kind of
interesting because garage burrs wall
actually says print hello world and if
I'm if I'm reading that correctly
he actually said or he or she actually
said hello world now if we try to do you
think we should try and run this and
I don't like the squigglies things I
suggest that there's a mistake of some
sort
so okay undefined variable hello and
this says unexpected token world
interesting
so let's come let's come back to that
though because I think we should run the
program first because while we have
arguably written a program now we
haven't actually run it previously when
Colton ran the program called Python 3
he was able to write a line of code hit
enter and immediately was executed but
typically the paradigm of course for
writing code is to write one or more
lines of code save it in a file and then
somehow execute that file so I think to
execute this file hello dot pi we're
gonna need a terminal window sure how do
we do this so I already have one so this
is the Mac native terminal application
windows has the same thing with CMD
linux i actually know what's called on
linux what's it called of linux terminal
terminal or x term or any a bunch of
other things and so I'm actually I'm
still in the Python show I'm gonna clear
my screen and then this is assuming that
I'm actually in the directory where
we're doing this code so right now we
are at my I believe dev is that PWD that
tells you where you're working yeah so
this is per present working directory
I'm here users J Harvard dev I want to
actually go into where I just created
that file which is streams slash Python
tutorial and I believe that is where I
created it and you'll see I hit LS for
list and then if I type in Python 3 like
we did before to open up the interactive
show but instead I say hello dot PI
after that we'll see that it actually
outputs exactly what we wrote in the
source code you know and while trivial
go ahead if you could go back to the
terminal window and just literally run
Pi Python 3 hello dot PI again this
should go without saying but because the
code is saved in file you can of course
repeat your steps you can improve on the
program you can fix bugs again and again
can't do that as easily in just the
interactive Church really you lose all
that state they've created as soon as
you exit the interpreter in that context
so the place we usually start when
introducing Python to other students is
to actually now make the program
actually interactive like a program that
just prints out hello world while sort
of nice to be part of that history for
posterity it's not particularly
interactive at all beyond just running
it so how would in Python you go about
asking a user for his or her name
and say hello so and so instead of just
hello world all the time well it's
common in a lot of languages that you
get some sort of input function Java has
one system dot was its you create a
scanner at system dot in C use a scanf
and in Python it's actually pretty easy
all you have to do is just say let's
just say I want to store the user's name
in a variable called name and then I'll
sign it whatever the return value of
this function input is and notice that
I'm still passing in this string hello
world because when I ask them at the
command line maybe I'd presumably want
to prompt them for something or maybe
say in this case not hello world but
please enter your name colon space okay
and then if I wrote let's try it out
let's go ahead and run it so he notice
Colton is using some keyboard shortcuts
he's sitting up on his keyboard which on
a lot of computers will actually rerun
the previous command and you can keep
hitting up up up up to go to previous
commands further so I'm already
realizing there is gonna be a slight
issue with this so please enter your
name let's say my name is Colton and the
program is over nice which is expected
yeah I mean you only wrote one line of
code that is asking the user for input
prompting them for their name and then
storing that value over on the other
hand side the other side in the name
variable so what do you want to do next
well now since I have name actually
stored somewhere I've set name equals or
against the value of input please into
your name I can actually reuse that
variable so I can say print hello space
and then there's multiple ways to do
this which you might have time to look
at but all for now I'll just say hello
and then I'll actually add a name to
that string like that yeah so what's
this plus I mean we're not doing
addition here no so this is common in
light in a lot of languages if this is a
term called concatenation where you're
not actually adding numbers you're
actually adding strings you're basically
taking one string and sticking another
string right at the end of it now in the
common operator for that use case and
there are multiple operators across
different languages but common is the
plus just because it's kind of intuitive
okay all right well let's let's go ahead
and run this it's now two lines of code
Colton has saved his file he's going
back to the interpreter and running it
on this file
and we say hello comes to work now it's
kind of a it's kind of a cold hello if I
wanted I could probably add maybe a
exclamation point at the end of it just
to make it a little bit more you know I
guess inviting or friendly or an unhappy
face to see you oh sure I could say I
could say hello Colton there we go now
unfortunately that's gonna princess my
unhappy face for everyone not just
Colton but even me and for you so why
don't we go ahead and change this I feel
like in a lot of programming languages
that we've seen that folks out there
might have seen you can do things like
conditions you can do something if
something is true else you can do this
other thing so could we modify this
program in Python to maybe be happy to
see anyone except you in which case you
should see the unhappy face wow this
feels like a depressing Hayward this is
this is what the audience is requested
how is this okay so we'll say okay so
well we're gonna get the name and then I
want to basically check to see what the
value of the name is Craig because you
said we want the frown to only appear
whenever it's me and otherwise
exclamation point I'm assume anything
else really anything
okay so we'll say we'll keep it simple
so I'll say if name is equal to Colton :
so this is a Python thing if you want to
do an if statement you Cokely not : yeah
Colton : then you say we'll say print
hello and then we'll do plus name plus
and I guess in this case we can even
hard code the name since it's specific
to this use case and then we'll we'll do
the frown actually put a space in front
of the smiley face because it was
getting put right on me let's make sure
we see how unhappy we artists yeah
exactly and then we'll just do this
other you know thing if that if
statement doesn't turn out to be the
case if the name is David or anybody
else literally in the entire universe
then we'll just say else would do what
we did before which is just print the
name and then in this case though we do
want we do want the program to be happy
to see who it is so we'll do an exercise
so let's run this and then let's come
back and we'll tease apart some of the
syntax that we've just added sure so
we'll say first of all we'll say David
so hello David so if he was happy to see
you very nice and polite how about can
we do another one can we do how about
Richard
Richard Richard okay so please don't
your name is Richard okay delightful to
see you as well
okay focus II Richard now let's let's
test to see if our functionality is
correct
ar-ar-ar I guess one off use case and
we'll use my name Oh perfect this is man
this is a good program so far we have a
smart smart program that knows the
appropriate way to address sort of
though can you run it one more time and
it's like an angry person can you type
your name in all caps this time oh sure
absolutely so I'll say Colton oh it's
happy to see ya no this is a bug this is
objectively a bug so maybe we're gonna
take a look at a couple of other
syntactic features but especially if
you're a little new to programming but
you have the right intuition here if
folks in the chat wouldn't mind
proposing what techniques we could use
to actually solve this problem we'll go
back in and fix it in just a minute or
two Congress that's a feature by the way
that no that's a bug that's a bug but
let's go ahead and back to the code for
just a moment well folks propose some
solutions here perhaps and there's a few
questions I had for instance if I'm
coming a Python new what's with the
colons like I'm used to the indentation
but the colons when we're the curly
braces the so the nice thing about
Python is there are no curly braces
mm-hmm the the interesting thing about
Python is that everything is based on
white space so indentation that blocks
no I mean this is something that folks
may or may not be familiar with but
denoting that something essentially is
inside of something else is done with
indentation so this if statements like
body of code that executes when it's
true we don't use curly break braces
like we do in other languages we can't
in wait we can't inline it but you would
instead of doing something like this and
then having and I actually I'm not 100%
sure I think this I think technically is
it technically possible to you know it's
not I'm getting that conflated with I
think something else but you don't have
another stream entirely but moving on
you you don't need these things you you
can all you just need to do is make sure
this is indented a certain level within
this after this if statement here and
now without opening a can of worms
what's your preference you like tabs you
like spaces I like
tabs that get converted into spaces okay
I'll take that as an acceptable answer
indeed in the Python community unlike
most languages there is an accepted norm
or at least a recommended norm which is
to use spaces so let's put an end to
that debate today we're gonna use spaces
for spaces generally would be pretty
common but I like that you don't have to
do the the curly braces as you know but
I notice you're also using single quotes
and I feel like in different languages
and even in Python I sometimes see
single quotes sometimes double quotes
how do I think about that so there are
for the for the large part
interchangeable you can make strings
with double quotes you can make strings
with single quotes now if you're coming
from a background like C or Java you can
only use double quotes for strings but
you have to use single quotes for single
characters okay Python there's no
difference there are no there is not
really a by default single character
versus string when you're using you know
this apostrophe character or the double
quote character so how would people go
about deciding whether on a single quote
kind of person or a double quote kind of
person well I think that's a matter of
taste but if you are use if your string
for example contains let's say colton's
favorite food pizza we can see that
there's an issue in that different in
that we see the green that we've seen
before with strings after this
apostrophe up until this apostrophe
where we're actually using it for
grammar we're actually using it as a
part of the string itself and there's a
couple ways of dealing with this one way
is we can use the backslash like this
and that'll fix it so now we've actually
said ignore this apostrophe this is part
of the actual string yep Jeanette
Jeanette sax notes that as well nice but
let's say I don't like the way that
looks I don't like having a backslash to
start my strings all over the place
instead I can decide in any case where I
do have a single quote inside the string
itself I'll use double quotes on the
outside and because this doesn't match
up with this it doesn't cause an error
in parsing the string nice yeah an
augment I'm kind of going through an
existential crisis I've been using
double quotes for like 20 years and
frankly I'm tired of holding the shift
key down and I do think you're seeing in
more modern Lee popular languages Python
and JavaScript among them I'm starting
to see frankly I think single quotes
more often but you got to beware because
of the stupid grammar issues alike so
just be consistent I think is the point
or resort to one or the other when you
have two contextual e-commerce a big fan
of single quotes itself as well don't
like whole event sure yeah yeah I know
too much shift at 20 years of hitting
the shift key is enough somebody
proposed an interesting way of getting
around the the problem yeah let's come
back to that I heard someone propose
that we use lower so force the whole
string to lowercase I see Richard
proposed using an array or a list in
Python where we could check for little
Colton or big Colton or alternating caps
Colton I think the downside there is
that you're gonna have an annoying
number of permutations of Colton's if
you will with the C is capitalized or
the O or the L or multiple ones and so
forth so I like this lower ID and this
is actually pretty conventional in a lot
of programming languages especially
Python when you want to compare
something for equality you canonicalize
it so to speak you force it to all
lowercase or all lower all lowercase are
all uppercase so that you know
definitively what you're comparing
against but you don't want to do it
destructively you want to do it
essentially temporarily on a copy of the
variable right exactly so how could we
do that here to make sure that we are
unhappy to see you well we could say
essentially here's here's what happens I
guess we should demonstrate this in the
terminal before week before we do
anything else so I'm gonna pull up
Python 3 and this is a nice thing
actually at first what I'm gonna do it's
gonna clear my CLI and then I'm gonna
pull up Python 3 if you're ever curious
about using a function in Python this is
the downs this is the upside and being
able to use it in an interpreter context
you can actually just fire up your
interpreter and just test variables test
functions right away and I happen to
know that there's a function called dot
lower it works on strings but let's
actually experiment with it so if I say
let's let's do for example the string
Colton and we'll say the lower
parentheses and we'll hit enter and
we'll see that indeed brings it all down
now this is weird why do you have a
period after a string don't you need to
do with that on like a variable or
something so this is then this gets into
kind of the idea of what's called
object-oriented programming which we
won't dive into today but essentially
every string in Python has a series of
functions that it has its own reference
to and the function when you when you
call it using this dot which essentially
means go inside of the thing to the left
and then exit
this thing that belongs to that thing on
the left in this case the lower function
sort of belongs to this string Colton it
will run this function and sort of
implicitly pass in this as a parameter
to this function yeah so what you really
have is that Colton is indeed a string
in this case in a string is an actual
object I can create a class in Python so
it has all of these phone bill tin to it
just one of which is this this lower
function so I see hasanul e proposed I
think what's actually a perfect solution
and that's a little simpler and less
code to write than something like a list
based approach so we want to deploy
Hasan's approach sure so first before we
do that let's just test it again so we
have the the version where it's my name
type normally let's type let's test it
with the version of my name and all
uppercase which does also go to the name
Colton and then just for just for kicks
will do will do all my space style text
and then we'll show an area there we go
okay and then we'll see that no matter
what permutation of capitalization we
use we are unhappy to see you yes
exactly
come with a better town to the same base
case so I'm gonna exit Python and then
we'll adopt what was it Sean said which
was if name
lower is equal to not not to Colton with
a capital C because that would that
won't work there will never be any
capital letters you're right alone I
think well andrey caught it there but
and I overstated we do have to make one
tweak which is just replace that with a
lowercase C indeed and if you run this
let's go back here and we'll do Python 3
of hello dot pi again which I could have
just hit up probably and we'll first
we'll try it with my name like this okay
hello Colton it's a it's a frown okay so
that was the behavior that was happening
before let's try it again but this time
let's let's do just the first few couple
letters capitalized oh oh but here's the
interesting thing is actually okay
nevermind it's still printing out Co a
nice feature would be to have it print
out the name with just a capital letter
and not you should say cuz our next
challenge is gonna be exactly that so
your user does miss type his or her name
in some stupid way like Colton video
capital C capital o l3o in classic
Colton we can at least with some
probability fix this in code now I say
some probability because at least in
English there's a lot of names that have
a mix of uppercase and lowercase
characters O'Reilly for instance is a
last name also the the name on a very
popular series of computer science books
you might know so we could get this
wrong but let's assume that we have very
simple names in this case like Colton
Capital Co how in Python could you force
the users input to be neatened not all
lowercase but capital C and then
lowercase Olten well my first instinct
would be probably to use this named
lower as our starting point well we'll
bring everything we'll canonicalize all
the names back to all lowercase like
this and this actually let's actually
start with that so we'll go name dot
lower actually other way around well
actually no will do in both cases we'll
do in both cases so now you'll notice
that I'm printing named lower in the
actual output for both situations and
what we've typed into name so that way
even if we type in capital C capital o
LTO n it's still gonna give us the
canonicalized
strings and not the best design but
we'll get there well let's try this out
so we'll say this plays into your name
and we'll try Colton like that and it
doesn't need say hello Colton with
everything lowercase we're halfway there
yeah I'm still have to now do the very
first string okay and the first
character and the first character in the
string so I actually offhand don't know
what function strings might have to
capitalize the first letter but the chat
though so the chat happens but don't
know looking at looking at how would you
do this if you were home alone with that
a live twitch stream to answer your
these questions for sure so I would
probably say Python capitalized first
letter string so have the name of the
language is the first word cuz it's
probably gonna be a major thing that
Google optimizes for and then just
literally kind of the main words that I
care about in the query for what I'm
looking at which capitalized not you
know we explore the first letter in a
string yeah and this is something you
learn over time and odds are most
everyone in the chatroom is learning
this already you don't have to ask
Google or Bing or whatever you use full
sentences you can drop the words that
really aren't adding anything to the
query like how and
and it and is and really just focus on
the words like Colton has here that
really have more information content for
the research engine exactly somebody I'm
gonna do that query and the Google is
amazing the first result says in Python
the capitalized method converts the
first character of a string to capital
letter uppercase letter if the string
has its first character as a capital it
returns the original string yeah and I
don't use this particular site too often
myself geeks for geeks or but it seems
on point and just for kicks can you
scroll down just so we can see what some
of the other search results are sure so
we're seeing stuck overflow is number
two stuck overflow is number three a
blog is number four programming program
is calm is the next so you'll start to
see patterns in search results if you're
learning programming or teaching
yourself Stack Overflow is a big one but
sometimes Google thinks that it's pretty
sure what it knows what the answer is so
it'll just bubble it up special to the
top oh all right so let's try this but
shout out to those of you in the chat
juban three and davis 216 all of whom
got this and major if thats if i'm
pronouncing that right also at a clever
solution but arguably you're reinventing
a wheel and i would propose that in
python you really try to reuse some of
the wheels that have been given but odds
are underneath the hood capitalized is
doing something quite like that but
probably with some error checking just
in case name is short indeed
so we'll say name dot lower now this is
an interesting thing so when we call
this method dot lower it's actually
returning a string and new stream and
then that's essentially what we're doing
here we're returning a new string and
putting into the output we're returning
a new string and comparing it to the
string colton so the cool thing is that
you can actually chain these methods
onto themselves so I can say name dot
lower which gives me the name Colton in
all lower case or whatever name and all
lower case letters but I can then do dot
capitalize like so and what this will do
is this will say okay give me the thing
give me the dot lower method of the
thing to the left okay and return it so
this will give whatever the name is all
lower case letters and then this becomes
essentially that then when you call dot
capital wise it's doing the same thing
and saying give me the value to the left
and then capitalize its first letter
that thing to the left is now that lower
case
straight now when you hover over
capitalise based on the definition of
this function the BS code is feeding us
here do we need to force things to
lowercase first and how would you go
about figuring that out I returned a
capitalized version of the string more
specifically make the first character
have uppercase and the rest lowercase ah
that's interesting I actually didn't
realize that that was part of the
signature well let's try it then in that
case so let's say if name dot capitalize
name capitalized and in this case I mean
we could even we could even do it here
and just and say Colton and then dot
capitalized let's try it out so let's
say the name of Colton first okay hello
Colton let's try see Oh LTO n whoops
nice man look we have a whole new
problem and how do we fix that problem
we get better at using the terminal oh
it turns out that that actually worked a
few steps ahead of us right and so
besides trial and error like we just did
too you can certainly read the official
documentation in fact just so folks can
see it if you've never seen pythons
documentation it's so-so
like all the and well most of the
information is there but it's not always
that easy to find what you're looking
for dave is not a huge fan of the Python
document so it could be much more
user-friendly so can you go ahead and
find us the documentation for the
stiring classes capitalized function
yeah so we'll go to STR which is the
string class in Python a string object
type and we went right here string
capitalized and it looks like it's
almost the important verbatim so yes
codes probably getting its instructions
right from here but this is good that we
have the documentation up and notice how
it's ordered you have on the Left
indented all of left aligned you have
all of the function names in that class
boldface no less capitalized case fold
Center someone in the chat just a moment
ago proposed title the title function is
there a title function inside the string
class let's go ahead and look if there's
is title its string title yeah so this
is title case version of the string or
word start with an uppercase character
and the remaining characters are
lowercase so this would be great for
four full names yeah assuming no
weirdness with like lowercase letters
needing to be lowercase still sure but
yeah absolutely
so we can maybe even experiment with
that so if I were to say let's go back
to the s code let's say it most
specifically maybe there's a Colton
Jackson that we that we'd like to see
but Colton Ogden is the one we don't
want to see so why don't we go ahead and
test that out so we'll say name title is
equal to because remember title doesn't
capitalized us but to multiple words in
the string so in this case we don't have
just one word we actually have two words
separated by its space so let's go ahead
and say named title and name title and
there is a better way to do this which
we should probably also take a look at
in just a second as well
so we'll go ahead and run this code and
I'm gonna enter my name as Colton Ogden
but all lowercase letters still don't
like them oh it looks like you figured
it out nice and actually smart
programming this is this artificial
intelligence and this is the very this
is the seed for artificial intelligence
your six-line state-of-the-art you know
I would propose here too we are using on
line three title to compare against
Colton Ogden capital C capital o yep I
admit I don't love that maybe you can
convince me otherwise it just feels
weird to not just choose something super
simple like all lowercase or all
uppercase right because this assumes
that now I have the sophistication or my
colleagues know what title does and
therefore this just seems a little more
fragile would you agree or disagree I
think I would large they agree I think I
would say I think I would say what I
don't like the most is that we're using
name title dot the dot title method
specifically in three places that for
sure but I don't like title for
formatting but for for quality testing
personally I would suggest you use
something that's absolutely robust all
lowercase all uppercase or another
technique altogether but we'll get there
in just a moment again and another
something good up my sleeve for you yeah
well dot lower lower probably be better
sorry so change that's what lower which
this - dot lower and then here's a nice
little shortcut if using vs code and
your highlight something let's say I
want to edit this and this whoops this
at the same time so I actually highlight
this and if I hit command D or control D
on a Windows computer I actually have
multiple cursors
so now I can actually go over here and
type lower amazing in two different
places it's typing twice as fast as I
could have in my day there we go so
let's let's take things up a notch
if we can because you know you have a
middle name right
a lot of people have middle names and we
want to make sure that we want to be
unhappy about all Colton Ogden's not
just you specifically so how could we go
about adapting this program in such a
way then it is unhappy to see Colton
something Ogden or Colton something else
Ogden so that there's a Colton at the
beginning and OGG and at the end we
don't care what's in the middle but
there can be something in the middle how
would you go about solving that problem
a middle name middle name yeah but one
name two names
arbitrary number middle name I just
don't want to see any Colton's over here
or Coggins over here okay sure so in
this case what we can probably do is
figure out well first of all we're gonna
get input in a string of some kind and
it's gonna be this presumably space
separated names no kind of sure so
they're in Python a nice thing that you
can do is you can actually do this thing
called split and split a string okay so
let's go into the end of the terminal
here and actually pull up the
interpreter and you know I like this
because you just want to play yeah
exactly
let's just get a sense of what the
function looks like and so I'm indeed
answered the questions in the chat yes
we're essentially at the moment just
trying to get the first word in the last
word yeah so we'll say let's say I have
Colton Ogden here in a string and I want
to split it into its constituents
because so I'll just do thoughts split
which is another method inside of the
string class exactly and well we have
some interesting syntax here but it
looks like it did find my first name and
it separated out my last name and what
are these square brackets around you why
am i seeing that so these square
brackets are indicative of what you
might have seen in java or c an array
but in Python it's called a list and the
nice thing about lists and python is
that they can be of arbitrary size they
can grow dynamically you don't have to
allocate memory for them if you're
coming from a more statically based
background yeah and I'm smiling because
the sandwich in the chat here is
proposing that your sample middle name
be Colton McLovin Ogden which is a great
reference to a great movie question here
about can we use the in operator from
Richard because if the name is double
bare bare double barrelled that wouldn't
work so in just tests for the presence
of a
string anywhere and the string beginning
middle end or anywhere else in between
so it doesn't feel like the right fit we
deliberately want to check the beginning
and the end so I think the road we're
going down is at least one approach that
we can take yeah can someone who says
stir it up fine as well but again I
think that would also do lead us down
the same track something stir it up find
just literally finds whether and not a
vision asks what if someone has to plus
middle names that - we want to be
sensitive - so how it let's let's go
ahead and well I don't I don't just like
the approach you're taking just
splitting on the strings but how do I go
about checking for colton at the
beginning in Ogden to the end and can we
try and like mclovin and maybe a
two-word middle name just to see what
the result looks like yeah I know what
to code for so calling the glove and
Hodgdon
which is fantastic and actually you have
three you have a David J Malan as your
name we'll use that as well so let's
split that so again it worked just like
you did before even though we have three
names instead of two it's split on
spaces
can I see four names that's this good
we'll see David J McLovin Mela nice
that's a mouthful and we'll split that
and okay so every string from the
original string ends up inside of the
list it does so really we want to check
the beginning of the list and the end of
the list for Colton and Ogden and
unhappiness we do and lists are nice in
that you can just reference if you want
a specific spot in the list you can say
let's make a list here which has five
numbers in it well actually for
confusion let's not do that let's say I
have apple orange and banana as a list
and we can have just use our names as
well but if I want to get a specific
element from the list I can use what's
called an index and this is a numerical
index so lists start their ordering of
their elements at zero so for example in
this case L at index zero is Apple and
wise one if it's the first element cuz
computer scientist
that zero is how they like to start
numbering things in a program three
things you know what Lua is one indexed
scratch just wounded next to yeah okay
presumably for user-friendliness maybe
because Lua
I know Lua was originally designed as a
config language for less technical
people and then scratch is a programming
environment for people that are fresh to
programming in computer science but
ironically it just ends up confusing
because then they eventually have to
change yeah their transition to C or the
Java or whatever now what's this is zero
is that a number one on the left no
that's an L and L is just a shorthand in
this case for a variable for list L for
lips and you can this is typically
frowned upon you don't want to use a
letter as a variable name for most
circumstances outside of maybe some
context we'll we'll get to a little bit
later but in this case I just wanted a
quick list so I'm gonna just use L again
and what L looks like a one and a mono
spaced font if I use these brackets just
like we did for the actual to actually
show the list itself but I say L at
index zero this is how you can almost
like almost like a box if we're thinking
about it like there's little boxes
little cubbies for each individual spot
in our list
mm-hm if we do this we'll see that L at
index zero returns Apple okay L at index
1 gives us orange I am and L at index 2
gives us banana so everything is
sequentially laid out and what fruit are
we gonna see it L bracket 3 ooh
that's a good question it looks like
Python is smart enough to know at
runtime whether or not we're actually
indexing outside of the rainbow we've
allocated for our list so in languages
like C and C++ by default you're
typically going to see no error but
you're gonna be touching memory you
shouldn't or the program could even
crash if you do something like this this
of course is an exception we'll talk
about that perhaps in another stream but
it's a way of handling errors and
similarly what is l bracket negative 1
for instance can we go to the left oh
yeah solutely so L bracket negative 1
now this this would cause chaos I think
if you I'm really worried about this is
gonna break the computer Oh interesting
so Nana L negative one gave us it looks
like banana the last element of our list
interesting now why is that well it
looks like it turns out that in Python
you actually get this nice little
syntactic sugar for indexing into your
array from the
and not the head so to speak the end of
the list as opposed to the beginning and
you know starting at negative one and
going and backwards negative two
negative three you go from the end of
the list actually to the front of the
list of the head of the list so what's
l-bracket negative to going to bell
bracket negative 2 gives us orange which
is again one further from the back of
the list an L Bergen negative 3 o and
indeed all right so this is actually
really handy when you want to do
something relative to the end of the
list but you don't want to have to do
all of the math yourself necessarily you
can just jump immediately to the end if
you know the index but the catch here is
that I think when we go back to your
text editor we need to be robust against
names with two words three words four
words so how can we go about modifying
this program now in hello dot pi to
check that the first element in the list
is Colton the last element is Alden but
we don't really care how many other
things are in between because I don't
think we can hard code 0 + + 2 or 0 + 3
we need to figure out how many elements
are in the list first right sure so our
first step would be again to break the
string up into its constituent parts
inside the split method so we can do
that here so we can say if well there's
a few approaches we can do this we could
do names and actually take name dot
split nice and now we actually have
another variable not with the whole
string but with the individual names
that make up the string that we passed
in okay nice so now what we can do is we
can say if names 0 because I remember
again if we're doing if we're indexing
into our list at index 0 that's the very
first element in our list and we want to
check to see if my first name if the
first name in the list that we've
entered is Colton that's what we care
about so we'll just say if names 0
equals Colton and now here's the thing
there are two conditions though because
we could get a Colton Jackson a Colton
Smith or what-have-you but we care
specifically about Colton Ogden you know
and so the others are delightful this is
there are two actually two conditions
here that we care about not just one
condition
and the nice thing that Python lets you
do is actually say okay if I want to
check for two different things I can
just say if something and
thing then do something if two things
are true cut so you literally just say
and you say and so I'll say if it's the
case that names zero is equal to the
string Colton and names if I can type
names at negative one again the last
element in the list no matter the size
is equal to Ogden then we want to
probably not say name dot lower probably
actually just say name dot well in this
case we do name not capitalized name
stuff what was it title I guess we can
do name that title and then name that
title again for the actual printing out
okay
you know the other thing here though is
that if it's if in this case we
literally have the string Colton or
literally this chain table then it'll
work but if we have if we've missed
types like we talked about before it's
not gonna work yeah I don't like that so
where should we do the lowercase trick
for instance like we did last time so
what we can do is I can say name dot
again lower and force everything to
lowercase initially split exactly so
you'll get back a whole bunch of
lowercase pieces now yeah this name even
with the spaces all of the words all the
letters that are alphanumeric are
specifically alphabetical or going to be
given a lower K they're lowercase
version okay and so then after that I
can change
now we're not comparing for the
capitalized C we're actually comparing
for the lowercase because everything's
been canonicalized names okay it's then
once we've done that now when we render
it when we actually display the name we
don't want to display a lowercase
because then look that look weird in
despite what they've typed it'll look
different we want to clean it up we want
to give it the title aged version yeah
okay so let's actually try and run this
now assuming that I don't have any
errors in here let me exit the
interpreter clear my screen and then run
my hello dot pi so let's test it out
let's say David J Malan oh wow
internationally and actually printed
your name and it's happy to see you it
is which is good let's go ahead and tie
it let's try a Colton will use my actual
full name Colton Taylor Ogden
oh I'm happy to see you and happy to see
me what about Colton McLovin yeah what
about coffee done Colton McLovin Ogden
looks like it knows it's you Missy still
knows this mean I wouldn't
Colton was try Colton McLovin Smith he's
fine
he's cool that's a different Colton what
about just Colton okay let's try that
hello this was suggested in the chat if
we just have one name oh it looks like
it's happy to see interesting cuz it
really is the Ogden that we don't like
correct interest any Colton and
specifically if your string is one
element long and you do negative 1 and 0
these are identical you get the same
element because your your list is only
one size so from the bottom and from the
front it's making the same steps to get
to the same value and I see Osley
underscore t asks can you also do name 0
lower you can but then you're gonna end
up calling lower in two places because
you're gonna do it on names bracket 0
and names bracket negative 1 it's not
wrong it's just you're literally calling
the function twice as many places as you
need to so I like what Colton did by
elevating it to this other temporary
variable the pieces that end up in the
names variable as well this is where you
get points for star for design
specifically would you bucket eyes this
as design this would be designed right
yeah this is design I would say style
would be more about the indentation
whether your variables are named
correctly but design is like the actual
how you arrange your function calls and
sort of mold your data to accomplish the
task at hand so I think if folks are
comfy I think there's a nice opportunity
here even though the program is pretty
small to kind of take things to the next
level and introduce a more advanced
topic known as regular expressions okay
regular expressions allow you to check
for patterns of user input which is nice
because what Colton's something
something Ogden really is it's a pattern
and we're being very deliberate right
now in splitting the string on space
checking the first for Colton checking
the last for Ogden but it turns out
there's a general purpose tool by which
you can just do this by expressing
yourself in terms of a pattern and not
really getting into these lower level
implementation details of splitting a
string and checking this and checking
that would you mind walking us through
an alternative solution that uses
regular expressions instead of splits
and these equality tests on the heels of
that Davis 216 says oh god not regular
expressions oh here we go Davis these
are great it's a great you're gonna
build them up for us step by step baby
steps if you will so I know they didn't
Python Python ships with a module called
our e in a module in Python
just a library that you have access to
well specifically the module in Python
is a folder with a specific file in it
but modules as they're commonly known in
Python are just libraries that ship with
this the with the language itself part
of the standard library so you can do
something at the very top of your
program like import re re being short
for of course regular expressions
regular expression and I might have to
refresh my memory on how to actually use
this library because it's a little while
but this is what we have Google points
Mahlon but re a regular expression is
pretty cool it allows you to actually
specify sort of a string pattern a
grammar so to speak for what you're
looking for what how you want to
actually parse your string extract data
from it you know test whether it fits
some kind of not hard-coded you know
Colton Ogden but whether it's something
a little bit more complicated hmm
so in this case so what do we how do we
want to do this we want to use re just a
test for Colton Ogden or I want a Colton
at the beginning and Ogden at the end
and I'll take anything in between and I
don't care about capitalization I go
away
okay sure now we should note there's a
few there's a little bit of nervousness
in the chat and that's fine and I do
think you'll find that once we introduce
this sort of piecewise a little bit by
little bit honestly you'll found these
pretty straightforward and pretty
powerful with that said sometimes they
get crazy complicated and literally a
day or two ago that I email a colleague
saying hey how do i express this in a
regular expression because even I get
rusty on it sometimes but we're gonna
take it slow and make this pretty pretty
compelling yeah you said there's
nervousness in the chat a little
nervousness here in person oh I'm gonna
I'll be honest with you but we'll figure
it out it's okay this is a judgment-free
zone over here notice so there are a few
functions that I know that exists in our
e and you can do what's called compiling
regular expression where you actually
create an object that takes in a regular
expression string and sort of creates an
object that you can then use to do
matches more efficiently down the line
alternatively you can do something
called re match which doesn't take a
compiled regular expression but just
kind of does it on
little bit more on the fly this is based
on my memory it can be wrong we'll find
out very shortly so if I say so right
now we're getting the the name so please
into your name and you know regular
persons are nice in that you actually
don't need to test forgot lower in this
case you know you can express that
different like yeah you can you can use
a regular expression actually to say hey
regardless of case is this match Colton
Ogden so actually I'm gonna get rid of
this dot lower I'm actually gonna get
rid of split and I actually gonna get
rid of names altogether because I'm not
gonna need a list and I'm actually gonna
get rid of this if state this is what's
getting cool I mean I know there's some
nerves there but this program is gonna
actually end up being shorter using a
regular expression now I'm gonna start
using our this function called re-match
okay and it looks like this we'll try to
apply the pattern at the start of
whatever string we pass into it so it's
gonna take a pattern and re patter regex
pattern rejecting stuff rate of
expressions so I'm gonna say if re match
and I'm gonna use this an R string which
is just a regular expression string and
actually will serve raw string broccoli
and it will format it a little bit
differently electric show you things a
little bit differently and now I'm a
little rusty with my zip with my regular
expressions today oh but I want to
basically do C or C and then Oulton
space actually and I think I might be
misremembering space it's either that or
that I think one of the two is the
opposite there's some deep breathing in
the chat but if you're in the chat you
know a little bit about regular
expressions by all means chime in here
and paste your ideas you can do this in
bunches of different ways and we'll give
you one or more along the way here so
and then what we want to do actually is
say plus for an arbitrary number of
spaces now this might not be the correct
character I'm going to I'm gonna try it
here so let's let's just run it and then
if this is wrong we will we'll do a
little bit of it well do a little bit of
googling and oh I forgot you can see it
when I make faces at the camera there
right
we'll go to a website that I like called
regex pal okay so please enter your name
let's say Colton Ogden Oh missing one
required position oh okay we have to
actually do it on the string itself so
we need to say Nate okay let's try it so
you're gonna match this pattern right
left against this variable on the right
right correct and that's do that so say
enter my name Colton Ogden oh it's I'm
happy to see you it worked
okay well let's make sure let's make
sure that it actually still works for
other use cases let's say Colton Taylor
Ogden oh it's happy to see Colton Taylor
on me you know what I'm realizing is
that with all we did was we just said
check for an arbitrary number of spaces
not four characters other characters hmm
so that's kind of that is kind of a
problem so what I probably want to do is
say how do we do this I guess we can
just use dot plus okay and then dot
essentially means any character and
we'll see we'll see if that works let's
try that out
so we'll say Colton Taylor Ogden oh it
looks like it's working now intro okay
but what if we do what if we okay let's
try David J Malin oh it is happy to see
you okay interests did I mess up is
there well what about a four person name
okay let's try it so let's say call it
up PI Colton Mik love and Taylor Ogden
looks like it's indeed unhappy to see me
still it knows it's babe how about
Colton osku P Ogden
oh yeah that's gonna be a problem isn't
it okay okay
Colton on CPR game but see the thing is
people know that that's me still yeah
but I wanted Colton at the beginning and
Ogden at the end not on colonoscopy so
there's a bug here right it's being a
little too generous and it's looking
apparently for CEO ltn and then anything
else but I want there to be a space
there so what I want to do is I want to
have at least one space and then an
arbitrary number of care okay backslash
backslash s means any space so like
space bar tab other things too so let's
try that so we'll go here we'll try my
name as
Wilton Taylor Ogden which is unhappy to
see me and then Colton ah Skippy done
and it's still happy to see me
okay what is okay I got a little bit
what about let's try one more
how about Colton Colton MOG then all
right I do need to have a space before
that Oh as well okay
um seeing a Hilton mugged and I'm cool
with that's my soda
you win a happy face okay so what we
need to do that is to again there's
there should be a space before the oh
and I guess I should have explained
before to here this C bar C just means
one of the other that bar just music an
or and I'm gonna need to correct you on
that though before long we can I have to
do that now or come back to you're gonna
die now let's go alright so if I can
just interject with some some my elder
wisdom here
so the grab the square brackets here and
here denote what's called a character
class in the regular expression and that
is a set of one or more characters that
you want to treat either of so in this
case the vertical bar unfortunately is
not necessary because this expression
where we literally give you a capital C
or a C that's indeed the role of a carry
well if we did it this way we'd want the
bar right yes but then parentheses
around it so that it's C or little C
like that exactly so that would work as
well but I think we should fix it for
the Ogden as well and the end all right
we'll do it like that and then we'll do
it like that I like that now but I think
person folks in the chat here we're
trying to handle some number of names in
between Colton and Ogden so the pattern
really is Colton one or more spaces
maybe one or more other words then maybe
a space than Ogden so it's getting a
little complicated now and I can
appreciate this is where some of the
pain goes so oh my god we didn't also
cover the the carrot and the dollar sign
no we can now stall and go to those now
instead if you want yeah those are the
probably the easiest part of the whole
folk a so the carrot just means the
string has to start with this okay and
the dollar sign means the string has to
end with this
that's true in Connect can i interject
with my uh my older way don't have the
carrot symbol confusingly is not
necessary here because the match
function in python takes is a check from
the beginning but so let me propose this
let's use our e dot search which is
slightly more general purpose and don't
worry about getting hung up on those
details for efficiency reasons they can
actually be a good thing but in this
case here now we have a general purpose
tool the carrot says start the string
the dollar sign says end of the string
and now we just need to get everything
right in between Colton's hair says I
thought I wouldn't have a headache today
but here we go thanks Dave okay but
again notice what we're doing we're
taking baby steps toward getting it
right a little more right a little more
right and then hopefully a hundred
percent right so what we need to express
in the middle it seems is optionally a
word and a space again and again and
again and again so how could we do that
for folks so if we do so essentially
replace this dot plus yeah that feels a
little too luxe cuz da in this case is
not a period it means any character I
don't want to do a period we would have
to actually backslash escape it exactly
but that's even worse so let's not do
that okay so we know that we're gonna
have a space then we're gonna have a
after the Colton and then a space before
they Ogden and in between that and
arbitrary range of words so I'm going to
say we'll do eight well in this case
also an arbitrary an arbitrary number of
spaces as well if we want to accommodate
possibilities okay and so we'll say if
we're not gonna use the dot essentially
the wild-card thing we want to
accommodate that I'm presumably now what
you talked about before with character
classes we can actually specify a range
of characters using this syntax where we
say A to Z capital A to Z lower case
zero to nine which means all numbers and
this just means all characters from A to
Z capitalized all and all characters
from A to Z lowercase and all digits
from 0 to 9 and I'm guessing that's
probably even a simpler way to do this
with the - W / W would be a word
character so it would give you the
letters of the alphabet in that case
would we want to do something like you
know I'm okay with using dot to just
represent any care
just cuz I think that'll keep it simpler
but we can make it more precise if you
want okay but so dot something so it's
OB director bus and then a space and
then this whole thing really should well
I guess it would go when it go into
square bracket so that actually hundreds
I think you probably what parentheses
okay because you can use parentheses for
grouping you use square brackets for
character classes a finite number of
characters that you want to expect and
then with then this itself or no and
then a space and then and then this in a
bracket and then that with a plus oh
actually this not that okay and I let me
propose we can simplify get rid of all
the brackets in the middle there they're
just not necessary
okay so what I'm seeing here is this if
I can read this from left to right we
want start of the string then give me a
capital C or a lowercase C right then
give me Oh
LTO n then give me one or more spaces
right so even if you're getting a little
overwhelmed right now just take a step
back and consider okay I understand like
the first third of the regular
expression so now we're saying in
parentheses the following group give me
any character one or more times followed
by a space and I like that and then
you're saying here with the plus
whatever I just had in parenthesis allow
me one or more of those but you're
currently therefore requiring a middle
name because plus means one or more of
the things so how can you say zero or
more of those things
Oh actually offhand I don't know we
might have to consult Google for this
okay so regex 0 or more reasonable
search query okay 0 or 1 oh this is 0 or
1 it's not we're not going so sometimes
the first hit is not the best thanks
google okay let's try this so wrong
language but it's probably gonna be
relevant because languages tend to use
the same syntax for regular expressions
okay I'm agitated okay no that's that's
for a range let's see 0 okay here's 0 or
100 here's okay star is 0 or more
okay so that effectively makes it
optional some number of times so then we
don't need this Plus anymore we instead
want this star correct and now the only
thing I would be a little careful of is
this strictly speaking we could have
that there but I don't think we need it
because if we instead just say give me
one or more spaces after the optional
middle name that should offset it from
your last name right so if we recap and
this is what I would do when making my
own regular expressions because honestly
it's getting confusing so you want to
just break it down again
so this says start at the beginning then
give me a capital C or a lowercase C
then an O LT o n then go ahead and give
me one or more spaces to separate it
from everything else right then go ahead
and give me optionally zero or more
times the following stuff the following
stuff is any character one or more times
followed by one space or more spaces but
that whole thing is optional so what the
parenthesized expression really
represents is optional middle name
middle name middle name middle name and
it repeats and someone commented in the
chat earlier about your theory class
being germane if you've ever written
drawn out on a whiteboard or a piece of
paper DFA a deterministic finite
automaton or an N DFA a
non-deterministic find out to Thomas on
the picture you're drawing that has
loops like this that is what the star is
doing give me one or more of these again
and again now that if you've thought if
you follow that okay then at the very
end we just have some Ogden so that
means give me a capital o or a lowercase
O then a GD en end of string so again
crazy complex when you look at it at
first glance but break it down into
those constituent parts and I think
it'll it'll pop for you as well let's s
that's actually make sure it works
so we'll say hello pop quiz it's your
name Colton Taylor Ogden okay
looks a it works alright always
satisfying we spend all that time a
little nervous I don't think we should
test anymore there J Malin nice to see
you okay let's try let's try and
situation where I just have no middle
name okay so wise okay and then if we
try let's let's say we try four names so
make up love and
Taylor Hagen looks like it still
recognizes this
so what Colin's doing here is an example
of course just manually testing his code
trying a bunch of inputs once you get
more comfortable programming can you use
various tools to automate this testing
so Colton or someone would still have to
think about all of those different
examples but you'd save the tests in
another file and then run some automated
process that would constantly run those
sample inputs against your program to
make sure that you haven't messed up and
created a new problem Oh someone
actually says try only Colton
oh come a little nervous okay Cole
please cool because we don't know if
it's the one we don't like what about
though Colton odd then in all caps if
you could someone who's yelling they're
really just they're yelling your name
there we go Colton Hogg then we didn't
we didn't come back we didn't account
for that oh we know we did it no I am
not pleased to see that Colton Ogden
either so how do we fix that yeah so
here's the thing what we want to do is
get back into lower casing things we
want to go to name dot we could do that
way or I think you've read the my face
there yeah I'm pretty sure we can do
this with regular expressions though we
probably could so we could just do the
same thing that we did before which is
not that we do I guess it might be font
oh because I haven't string highlighted
that's why so if I go over here do and
oh oh you could just have a whole bunch
of characters one for each letter
there's probably a more efficient way to
but dear god this looks scary
it's kind of cool though too that your
name is like duplicated this way yeah so
you could do this this is correct but I
would argue bad design why because one
it's just so damn hard to read now it's
just making this string so much longer
and we're not even done with the Ogden
yet so I would propose undo all that and
it turns out there's a way with our
research and also for that other
function re-match to tell them to match
your pattern case-insensitive lee so how
would you go if I told you is like your
tutor like this is possible how do you
go about solving a problem like that now
on your own okay so I would go into
Google and I would say okay our
professor Google search case-insensitive
without re compilers we're not using re
compiles a stack that will another day
Stack Overflow is an awesome resources
and it looks like it says pass our
ignore case in all capital letters to
the flags parameter of search match or
sub now what is this Flags parameter how
do I look that up so if we go over to
the documentation actually I think we
need to have the documentation time open
over here already scroll the very top
Gera this type in read do that go into
the module for re which is our the
module we imported and then I'm gonna
specifically I'm using the search
function so I'm actually gonna type
research and let's go to where it's
actually defined so it looks like it's
defined right here so it does say we
have a pattern as the first parameter
we've talked about this string that we
actually want to search inside of us the
second parameter and then this Flags
equals zero
which is interesting because we're not
passing in a number at least not that we
can see when we're doing it presumably
you're not but the equals zero means
that that argument called Flags has a
default value of zero no matter why you
so you don't have to provide it and and
truly underneath the hood all of the re
dot capital words that we could pass
into it are actually numerical and they
form what's called a mask or a bit mask
a sort of a sequence of powers of two
added together in some arbitrary way to
set a series of flags and one compact
piece of information mm-hmm
and in here says where does it say
doesn't like it actually has any
information on flags itself not that
part of the documentation but it's in
there we go to Flags oh you just yep
you hide it on the screen a moment ago I
did that I saw re multi-line re dot
multi there we go oh here are the flags
okay it looks like they kind of have it
separately from everything else so these
are some of the flags so actually some
of these flags or some of these just
completely other variables
no these the capitalized ones are all
flags and that I don't know how to
pronounce this one but the van
it's proposed adding the flags ignore
case and that's spot on
yeah in this case are ignore case more
specifically this we wanted to import
star from re mmm-hmm indeed so let's go
back to well actually I don't know if
you wanted to talk more about what they
are but essentially ignore case there's
multi line which will let you test
regular expressions at span lines and
then there's ignore case which will
perform case insensitive matching
expressions like a-to-z nice like we
lived up so we can just disable the
notion of case all together without
having to manually force things match
lower case those actually that means
that we can get rid of this we can
actually just say nice Colton like that
yeah and then Ogden and then as our last
parameter to our readout search we can
say re dot ignore cases yeah so it's a
third argument now explicit so that's
gonna tell our research don't even worry
about upper case or lower case for this
match exactly alright so let's go check
on Colton Ogden and all caps in the
meantime Richard pointed out that we
didn't account for Colton Ogden one word
or a few other variants that's ok
because I was looking for a Colton than
one or more spaces then in Ogden not the
concatenation thereof a Colt and Ogden
one word would slip under our radar yeah
oh I'm happy to see you yet again it
works nice this is a nice thing that are
a sort of makes life easier yeah so it's
pretty intimidating honestly at first
glance but honestly do not shy away from
these they will tighten your code they
will allow you to check for things more
robustly we could go down even further
here and be more precise as to what we
mean a last name is currently we're
being a little lazy and we're allowing
things like dog which is any character
so just for kicks can you go ahead and
do Colton one two three Ogden yeah as a
test case sure you know we probably
shouldn't tolerate this cuz at least in
the u.s. you're not gonna have like a
purely numeric middle name I think I
don't think so that probably is not you
so that doesn't really meet the the test
so dot star is a little sloppy we could
be more specific by specifying with
character classes what letters of the
alphabet we want but we do need some
punctuation right there might be hyphens
there might be quotes and other
languages there might be accented
characters so we need to be a little
respectful of the possible variation so
right for now I'm okay with dot star but
you know what's kind of cool too
could we go ahead and could you copy
your regular expression from the carrot
to the dollar sign and there's a bunch
of websites that do this can you go into
google now and search for like regular
expression
tester there's some really cool
web-based applications that help you
learn this stuff these days
let's try a regex 101 try the first one
yeah this is one I've used before so
Colton just went ahead and paste it at
the top of the page the regular
expression that he came up with on the
right hand side you'll see that there's
this explanation which is a machine
generated human-like summary of what it
is sometimes it can get a little
confusing but that might help you wrap
your mind around it but what's cool now
is go ahead and input it like a test
string give us a normal Colton Ogden
okay and you'll go ahead and see let's
say where's my test say oh go ahead and
save the regular expression top left
over there oh I think that makes it oh
that will let us share it and we have to
do the LIA this stuff that's not
necessary because it's already there no
don't need that and don't need the slash
actually can you scroll down it might
just be the debacle you know what we
probably haven't done we probably
haven't done ignore case brothers
probably no no you are going right I
don't know oh yes we can do that click
on the flag fires ignore you can click
right here yeah so you can just add an
eye after that so in other languages you
actually specify these lowercase symbols
not the Python approach that makes it
force it to lowercase so notice this on
the right hand side here of Colton's
screen you'll see that it's saying match
information and Colton Ogden is indeed a
first full match David J Malan is not so
we're not seeing anything else do want
to try a few other of your cousin's try
Colton Taylor Agha nice another full
match and it actually is pieced out the
middle part here because it's in
parentheses it's pretty cool so if you
want to learn regular expressions you
don't have to like struggle with your
own code you can just kind of play with
it a little more visually and I
definitely use this tool in recent years
especially as my regular expressions get
long and complicated because I want to
vet them against sample inputs outside
the context of my code okay and if we do
Colton one-two-three Ogden that's a
night doesn't happen well no if you
scroll down oh you know why because I
specified the A to Z here Oh in this
case you did indeed yes if I go back to
dot plus and get rid of character class
as
oh wait oh this right yep and no you
have the duplicate space yep guys oh but
it looks like it's getting your name as
well what did I miss up there now here
you have Colton spay that's weird
oh you're doing multiline get rid of the
em from the flags that win global I'm
sorry not multiline global colt and no
something's wrong can you scroll down
over here on the right
weird I think this is a bug it should be
not matching it should go ahead and
update the regex just for kicks even
though that's under save and share just
for kicks
it should not be matching multiple lines
like this today yeah that's very strange
no I think that's a bug in the tool oh
ok something's wrong well anyway ok on
that note well let's go ahead I think we
have a little more time to introduce one
or two more features right now our
program is pretty static it just does
one thing and that's it if we wanted a
loop you know I'm kind of fond of the
old games like Zelda U and I've played
that on stream actually the original
Zelda and it's very common to have like
heart power HP so that you have some
number of hearts representing just how
much health you have let's go ahead and
start from scratch in a new file called
hearts a dot PI and can you just write a
program that asks the user for a number
and then just print out that many hearts
using a Unicode character sure Unicode
is pretty cool because you can express
emojis and other things these days but
if you want to use ASCII based hearts
with like an open bracket and a 3 that's
totally fine too
but let's just go ahead and whip this up
ask the user for number then print out
that many hearts for him or her so I'm
googling for heart and Unicode you know
when you get the I want to get the
character and we'll use this one I think
that should work let's try that
so we'll say heart char equals this
little heart character knows that
Unicode renders nicely in vs code and
we'll go ahead and now here's what I
want to do I want to essentially take
the number I'm gonna get a number as
input and I want to
we multiply that number times this heart
and print it out as a result hmm we're
taking a number and actually applying it
to a different string I like what you're
thinking but before we go this clever
route could you do it a little more
familiar relief to folks using like yeah
I'm kind of loop so I would say for I in
maybe the range of numb so for every eye
that exists in numb which is gonna be if
it's 5 it'll be 5 it'll happen 5 times
if it's tantamount to 10 times and then
let's print heart charm well we'll do
this approach for now there's a few
issues that we have here but we'll see
we'll see what happens when we run it so
I'm gonna do hearts duck pie please
enter a number okay I want to see five
hearts on the screen so a stir it says
stir object cannot be interpreted as an
integer interesting and how do you know
where what line is the problem so in the
stack trace to the trace back it kindly
tells us that on line five on this for I
in range numb it's saying that the stir
object cannot be interpreted as an
integer yeah so these are the error
messages frankly early on they're gonna
look pretty scary but if you just start
from the top especially if you get a lot
of errors start with the first one look
for the line number and then honestly
just try to wrap your mind around what
words you do understand and see if you
can deduce then like Colton's about to
where the problem is exactly so I know
that numb is what I'm using here
it's basically what's what I'm inputting
at the command line and the thing about
input is that it'll always return a
string literally what you typed in a
string format because they're just
characters on your keyboard even though
they happen to look like numbers exactly
now the thing is I don't want I want
this to be numeric I want to be able to
iterate over this number I want to do
something x times and the way to do that
is we actually have to convert that
string into a number we have to actually
say in Python int of all of that stuff
and what happens is that this input
value I'll please enter a number the
string it's actually passed to this int
function which just takes in a string
and returns you the number the numerical
version of that same string yeah so
string 5 you get back the numbers but
number 5 the int 5 it can read into
direct so let's try that so
heart stop hi I'm gonna say number five
and okay it worries but it doesn't
really look like heart power because
most games have it like horizontal yeah
we fix that I think if I was a specific
yeah so that this is a nice easy thing
to do in Python so the prince is a
feature of the print statements a
feature
generally speaking or a bug-out
depending on how you look at it but it's
specifically a feature because often you
want to print something on the command
line but you don't want to have to
manually specify that I want you know
this input this output to go you know to
do a new line every time the fact it's a
little bit tedious and the languages
like C and Java you have to specifically
well not as much a Java but in C C++ you
have to specify this yourself so if I
want to say just write this on the same
line but don't do a new line print has a
second parameter and this actually you
can actually see it here written out
there's a set there's end we care about
n specifically notice that it has an
equal sign meaning that you can actually
identify that argument doesn't have to
go in a specific order as long as you
give it a name so if I say N equals and
then just the empty string so nothing by
default that's a newline character by
default into this this backslash n but I
don't want to do a backslash and at the
end of print I want to just nothing
empty string and honestly this drives me
nuts about Python like it granted you
don't always want a newline character
but this is so annoying to type a print
line yeah print versus a print line
function might be might be cool yes I
agree
Java got this a little better yeah
written in Java you have to do system
about out that's a bit bad but we it's
just ugly yeah let's go ahead and run
this now and let's say five okay so I
have my five parts but it looks like as
a byproduct of how we've coded things
now we don't go to a new line when the
program is finished we just end right
after the heart well that - plus all
your hearts are overlapping but I think
that's a function of your using this
this unicode character in a mono spaced
environment yeah yeah it's very small
care but can we how about can we have a
space after every heart
yeah and this up a little pensive lutely
so all we need to do is say
sepals space so separator separator and
then at the very end of all of this
stuff at the very end I'm just gonna do
a print okay outside of the for loop
outside of the for loop because
otherwise we'll get the same behavior
that we got before which was that it
printed every it printed everything on a
new line which is nice not the behavior
we were seeking jenkins from russia
hello from cambridge this is not PyCharm
this is just the s code a free text
editor so if we if we enter a number
five oh and it turns out that that
didn't work is maybe sep not what I'm
thinking it is no it is I think this is
an issue of ask you know what let's go
ahead and instead of printing out the
heart char that you have can you change
it to an angle bracket in a three sure
let's do it old-school style let's do it
that way please enter number five still
didn't print the space oh you know what
because you're only printing one piece
at a time
so you actually do here what we can
change in the ending to be a space will
do this and then we'll go and then we'll
bring back our our heart character I'll
see if that actually fixes it so we'll
copy that separator would work if you
were passing in multiple inputs like a
list that needs to be printed on oh I
say okay well then we can there is a way
we could do that now let's go ahead and
we'll type we'll do this again enter
five and then we have nice long I should
answer okay the five hearts okay very
nice now you started I think to go down
a pretty sophisticated road that might
be unfamiliar to a lot of folks do you
absolutely need to the for loop like
this or is there a more pythonic way to
do there is a more pythonic way and this
is something that's more specific to the
way python handles strings python is
actually overloaded the multiplication
operator so in the same way that it's
overloaded the plus sign to allow you to
add two strings together end to end it's
actually giving you the ability to use
the multiplication sign the asterisk
which we haven't actually looked at yet
to allow string multiplication now
normally it's not a thing in the real
world but in Python but that essentially
does is it just outputs that string x
times depending on what the multiplier
is correct so if we say let's say
instead of what we did before let's
actually print heart char x now
so we're actually multiplying this heart
string by the gnome that we've converted
into an integer up top nice we run this
and enter 5 and we get the same behavior
unfortunately that we had before
now there is another way we can do this
if we wanted to use what's called a list
comprehension if you wanted to talk
about if we want to bring that up what's
up what are we gonna use it for and we
would actually turn this into a list of
five of these then we can use set with
space yeah you know you're not wrong
that that would actually help here so
yeah let's go for it let's all write a
little teaser for what more you can do
with Python
so we'll say we'll do this thing we'll
say heart first of all I'm not going to
call it heart char anymore because
that's not accurate we're gonna say
hearts equals and then this is gonna be
a little bit of odd syntax so it's gonna
look similar to lists that we've looked
at before but I'm gonna say for I in
numb like Vince so hearts equals the
unicode heart for every eye in numb so
these brackets here they're creating a
list just like we did before but we're
using an expression to actually create a
list programmatically we're saying for
every eye and numb so from 0 it's 2 of 4
in this case 0 to 5 we're going to add
this the heart character okay so at the
end of all and at the end of this line
here essentially gonna have this we're
gonna have this in a list it's
essentially the same a same exact thing
very nice and so now what we can do is
we can say print hearts set equals the
space character because we're printing a
list of things and actually I'm I think
you're gonna print the list but let's go
ahead and see what you've done I think
we're gonna print the list actually so I
need to I need to refresh my mind on how
set actually works but I'll do 5 and
then a int object is not iterable oh I
messed that up so you can't actually
iterate on the number itself you
actually have to call range of them so
range gives you back the number 0 up to
5 or s case if you type in yeah
it's a it's what's called a generator in
that it gives you values every time you
call it over and over again but that's
beyond the scope I think
of this tutorial nice so now you have a
list a data type containing five hearts
we did so what can we do with this now
because you're printing it out but we
don't want the square brackets or the
quotes or anything like that so I need
to look and see how step actually works
so Python print set I don't use it too
often set parameter in print
oh I see so if you're actually if you
actually pass it multiple things in the
in the parentheses now is there a way to
unpack I think there is and actually
think I might know it offhand if we were
to say print hearts star except those
that not right or is it star hearts
we're not allowed to do that I think it
starts so first of all let's test to
make sure that works before I before I
okay it does work and then we'll explain
why it works so this star operator is
not multiplication in this case this is
actually an unpacking operator so if you
have a data structure in this case the
list but you want to pass every element
of that data structure into a function
like you were literally calling print of
this five times like this whoops I
messed that up a little bit but if we
were doing essentially that without the
syntax error we we can't do that by
passing in just the list object because
that's just one argument but if you want
to unpack all of the data within the
list you can explode it in a comma
separated list about it I think that's
actually the term I think it's called
the explode operator or something
something like that and you can also or
splaying in JavaScript these days so if
you go to hearts - can I propose one
other approach yeah
if hearts on line five is a list you can
also do this if you know in advance how
you want to separate the elements of any
list there you go you can go ahead and
do quote space unquote right dot join
but give me a space between the quotes
alright because we're doing it on space
as one of that space separate join the
hearts and get rid of the separator
altogether so now we're not asking for
prints help instead we're gonna pass to
print the result of joining of that list
of heart with one space in between and
this way - nicely enough you don't end
up for instance with an accidental space
at the very end that's there even if you
can't see it right and this
this approach is cleaner I think more of
a pythonic way to do it so we'll do five
and then we can lie exact same behavior
but we've we've actually made it a
little bit I think clearer we save some
space in our code yeah absolutely it's a
pretty cool so I think we're we're just
about at the end of the hour here and so
what were some of the takeaways we hope
so one all you need for python is a text
edit or whether it's the one Colton is
using here or really any other and you
need Python itself preferably version
three which you can download for free
the Python org and then after that
notice that if you've programmed before
there's a lot of familiar constructs
you've got functions and you've got
variables and assignment and conditions
and loops but then you have these
fancier features like regular
expressions so powerful not only in
Python but other languages but also some
of these syntactic tricks like list
comprehensions as colton notes and a lot
of the object-oriented features that
we've also touched upon over time so
honestly I think personally I would say
the biggest takeaway for today should be
please do not be afraid of regular
expressions they can absolutely get
scary but they don't have to start that
way and so if you really do take baby
steps and just solve part of your Colton
problem then more of your ogden problem
and then take care of everything in
between you really can bootstrap
yourself so to speak to a really
impressive solution that you're proud of
without having to know what their answer
is right off the top of your head we
have a lot of Colton Ogden problems we
do we are so unhappy to see so many
Colton Ogden's today as well as in the
chat I saw Colton's hair yeah one day
the pointers too many thanks everybody
for tuning in we this is this is pretty
fun maybe in the future if we can find
the time we'll do a part two because
there's a million different things we
could talk about but hopefully this at
least wet your appetite for what Python
is capable of doing very easily very
cleanly especially if you come from a
background programming and something
like C or Java where things are a little
bit more less managed for you absolutely
so feel free to tinker and let us know
via various cs50 various channels if you
have any questions but looking forward
to seeing everyone again and have a
great weekend youtube.com slash cs50
facebook.com slash news 50 check out our
content cool am on the note have a great
weekend we will see you actually I won't
be here next week I'm going to Japan but
the week after that I will see all of
you so in the meantime you know enjoy
enjoy your weekend dude time
break from Colton Ogden because you know
way we that unhappy face every bit a
little bit of that is too much but in
the meantime I'll miss all of you and
we'll see you in just a couple of weeks
so until then take care