CS50P - Lecture 4 - Libraries

269.07k views14856 WordsCopy TextShare
CS50
This is CS50P, CS50's Introduction to Programming with Python. Enroll for free at https://cs50.edx.o...
Video Transcript:
so [Music] [Music] all right this is cs50s introduction to programming with python my name is david maylin and this is our week on libraries so libraries are generally files of code that other people have written that you can use in your own programs or a library's code that you've written that you can use in your own program but maybe not just this program but another and another as well so python supports exactly this idea this ability to share code with others share code across your own projects and it does so by way of what it
calls module a module in python is just a library that typically has one or more functions or other features built into it generally the purpose of a library or a module specifically is to encourage reusability of code if you find yourself using the same types of functions again and again the same functionality if you find yourself copying and pasting from an old project into your new project odds are there's an opportunity there to factor out that code that you keep copying and pasting that you keep reusing and put it into a library that you can
then load into your programs moving forward so as to not just copy and paste it and have all these different copies all over so what are some of the modules or libraries that python comes with well python comes with a random library literally which is to say that when you install the python interpreter on your mac or pc or somewhere in the cloud not only do you get python you get a whole bunch of modules as well now these modules provide you with functions that you don't have access to just by default like you do
print and input print and input and other such functions just work in python but sometimes functions are tucked away in these modules so you have to be more deliberate about loading them into the computer's memory so somewhere on the computer's hard drive once you've installed python there is also it turns out a file probably called random.pi that someone else wrote probably long ago but that you have access to and in that random.pi file there's probably one or more functions that you yourself can use in order to do things randomly that is to say how could
you flip a coin in a program in python how could you pick a a random number between 1 and 10 in python well you need a bit of randomness and while you could figure out mathematically how to write functions like that yourself it's a lot easier to stand on the shoulders of others who've already solved that problem for you so you can focus on the problem that you yourself want to solve so for documentation on most any python module you go to the official python docs and you go to a url like this where the
documentation for that specific module lives and within the documentation you'll see a list of the functions or other functionality that some module provides but how do you go about loading a module into your own program so that you can use the functions in that module well we need a new keyword in python and namely its import the import keyword in python allows you to import the contents of the functions from some module in python well how might i go about using this in practice well let me propose that there exists in that random module this
function among others so i have copied and pasted from the documentation this summary of a function called choice now the function exists in the random module so to speak not a random module the random module and so generally the documentation describes it fully like this random.choice is how you would technically call this function though we'll see alternatives to that in parentheses there is a parameter called seq for sequence and sequence generally means a list or something that is list like if you have a list of numbers or strings or anything else and the documentation elaborates
well how can i go about using this function to solve perhaps a familiar problem well let me go ahead and open up vs code here and let me propose that we implement a program that simulates flipping a coin a coin that in the us heads heads or tails the idea of which is to pick a decision with 50 50 probability 50 probability of heads 50 probability of tails or you can use some other mechanism like that well let me go ahead and open a program with code called generate dot pi because i want to start
generating a whole bunch of random information the first of which is just going to be a coin toss now how do i go about using that function well i first have to import the random library so literally the first or among the first lines of my file should be import random and that just gives me access to all of the functions in that specific module now suppose i want to flip a coin well i can do random dot choice per the documentation a moment ago and that again takes a sequence what's a sequence it's a
list or something that's list like and we know about lists we've used lists to iterate over numbers we've used lists to iterate over students at hogwarts let's go ahead now and let's iterate over just a list of two sides of a coin heads quote unquote or tails now i could call these anything i want these are my strings i just want to simulate a tossing a coin so i'm just going to say in all lower case heads and tails but notice the syntax i have heads and tails and double quotes that's because they're strings i
could also use single quotes so long as i'm consistent there's a comma between them which means the list has two elements there's square brackets to the right and the left which indicates that this is indeed a list that's the syntax recall for defining a list in python and then lastly there's something more familiar there's the parentheses outside of those square brackets but those are just the parentheses that belong to the choice function and specify where its parameter gets passed in but again unlike past functions i have to specify what module this function is in at
least for now and so i do random dot choice to call this specific function all right well it's one thing to flip a coin picking between those with 50 probability and that's what random.choice does it takes in a list and it returns to one of those values randomly with equal probability because i've passed in two items i've got a 50 50 chance if i passed in three items it'd be a 33 chance for each of those items and so forth python does the math for you but i want to store the value of this in
a variable so let's define a variable called coin equals whatever the return value is so this is indeed like flipping a coin i'm going to store in a variable called coin whatever that value is heads or tails and now just so i can see what's going on let's go ahead and print out the value of that string coin all right let me go ahead now and run this program in my terminal window python of generate dot pi enter and it looks like the first coin toss was the heads let's go ahead and run it again
and it looks like it was heads again maybe you want to chime into the chat here if i run it a third time what's it going to be this time if you want to type your thoughts in the chat you might think there's a bug here but this is probability in action if i go ahead and hit enter a third time there it's actually now tails and again tails and again tails and again tails and again tails and again heads now if we did this an infinite number of times it would indeed work out to
be 50 50. if we only do it a few times it might not work out as cleanly but that's how probabilities indeed work all right so i've got that now working could i have implemented this in a different way well let me show you an alternative to actually using the import keyword alone and let me introduce the keyword from in python so from is a keyword in python that you can use when importing functions from a module but it allows you to be a little more specific than import alone so if i go back to
my code here it's worth noting that what technically i'm doing here by importing random is i'm technically importing everything that's in that module so not just the function called random.choice but a few other functions as well so instead of using this line of code at the top of my file import random which will technically give me access to all of the contents they're in a downside of that is that i have to type in random.choicerandom.this random.that because all of the functions i'm calling have to be associated with the scope of that module well suppose that
i just want to call the function as its name choice i can do that as well let me replace this first line here with from random import choice and what this does effectively is it loads the function's name choice into my current namespace into the scope of the file i'm working in what that means is that i now no longer have to specify which choice function i mean i can just say choice and so it loads it into the local namespace that is into my local vocabulary if you will so i can just now say
choice this might be advantageous in what cases do you think when might you want to import the name of the function explicitly like this as opposed to just saying random.choice random.choice throughout your code when calling a function any instincts here for this alternative import using from um hello i'm mohammed ahmar from egypt and maybe if we have a variable that its name is basically like choice if i have a variable called the choice so i need to differentiate which trace i choose so i'm gonna choose random.choice yeah really good instincts by using the first approach
by just importing random you're making sure that all of its contents are associated with are scoped to the random module so that you can have your own choice function you can have your own choice variable you can use the same names as all of the functions or variables that are stored inside of that file without them colliding so to speak and this is a good thing in older languages it was the case that if you imported someone's library you better hope that you're not using the same functions or variables as they are because you might
in fact have some kind of conflict python and certain other languages allow you to scope the names of those functions and variables to the file or the module that they come from so that's a good thing but honestly this is such a short program or equivalently maybe i'm using the choice function in so many places calling random.choicerandom.choicerandom.choice it's just making my code longer and longer and longer marginally so but it's just getting ugly and annoying i can simply import choice and now tighten up my code a little bit so as with so many decisions in
the past there's not necessarily one right approach or another it depends but i think for those very reasons sometimes it's better to do what we did the first time which is only import the module so as to retain the scope therein well let me propose that we transition to another function that comes with python's random module and that's this here from the documentation rand int it's a bit hard to say but it implies get back a random int and if you read the documentation it's a random end that's between a and b inclusive so if
you were to pass in 1 for a and 10 for b you would get back a number between 1 and 10 inclusive including the 1 and including the 10 potentially each with a 10 probability so how might i go about using a program like this well let me come back to my generate.pi file and why don't we go ahead and try generating a random number between one and ten you might do this frequently in the real world when you just want someone to pick a random number you tell them as much and the human responds
let's get the computer to do the same here let me go ahead and delete my two lines of code at the bottom but keep my import random and let's go ahead and define a variable this time called number set it equal to the return value of random.randint and now pass in a a value of 1 and b a value of 10 and now let's go ahead and print the number i'm going to go ahead in my terminal window and run python of generate dot pi and hit enter four python of generate.pi and hit enter eight
again nine again seven again ten again two again and we can do this all day long and if we add all those up they should end up being with ten percent probability each now how might you use this information well maybe we're playing a guessing game or maybe we're trying to randomize the behavior of some character in the game you can imagine using very simple building blocks like this just kind of spicing up your program by getting it to do things a little less predictably because you're choosing these values seemingly randomly and you're deferring to
python to actually do the generation of these numbers using its own algorithms and its own math well what more could we do here let me propose that we introduce another function that comes from this random library yet another that you yourself have to don't have to implement shuffle if you read the documentation for shuffle in the same random module you'll see that it takes in a list for instance of values and just shuffles them up it randomizes them like a deck of cards here you might shuffle them so it's to put them into seemingly random
order well how do i use this based on this function's name well let me propose that we go back to vs code here and let me go ahead and this time do the following because i need to shuffle something like a deck of cards let me go ahead and not just import random but let me give myself a variable called cards that's going to be of type list and just so i have something to shuffle i don't need all 52 cards in a typical deck i'm just going to shuffle three cards a jack a queen
and a king i could call those strings anything i want but i just wanted a list of some values so as to shuffle them up that is randomize the order they're in well how does this now work if you read the documentation for random.shuffle you'll see that it shuffles the argument in place that is unlike many of the functions we've seen it doesn't return to a value that contains the shuffled cards in this case it actually shuffles the list it's given itself so what this means for my code is that i need to do something
like this random random.shuffle and pass in the variable containing those cards and then on a final line here how might i go about printing the cards well i could do this and i could say print cards but if i do that i'm actually going to see python syntax for lists and it's just going to format in its own way using commas and the like i want to print these cards out one at a time just because i think it'll look a little better so we can use some of our syntax from loops and say something
like this for card in cards go ahead and print out the current card so what's now happening here line three i'm defining a list of three cards in this order jack queen king i'm then shuffling those same cards on line four and then on line five i'm using a for loop for each of the cards in that list print it out one at a time and because i'm using print one line at a time well let's see the results down here in my terminal window i'm going to run python of generate.pi and hit enter queen
king jack seemingly shuffled because that's not the order i defined earlier let's do it again queen king jack okay that happens to be the same but let's see this could just be bad chance there we go jack queen king doesn't look like it's shuffled but at least we're getting back different orderings now again jack queen king not so good jack queen king not so good this is someone you probably want to play against with cards queen jack king there we go but of course we only have three cards here so there's not that many permutations
we might see and if we do this over time we will see all of them but if we had of course 13 or 52 cards we'd see a lot more permutations instead so we have now these three ways to generate random information one is simple coin toss if you want to start some kind of athletic event one pick a number between 1 and 10 if you want to decide something based on that and now using shuffle we can even take in a list of things and shuffle them about so that we get some kind of
random behavior but let me pause here and see if there's any questions yet on random on modules or any of these three functions yeah uh can we increase or decrease the probability of cards if you want to uh for example there is three there is a 33 percent chance of probability so is there any chance to increase or decrease the probability can you set these probabilities not using these same functions uh can you set the probabilities but you can absolutely implement some of your own functions or use more sophisticated functions that do exist in this
library and others to exercise more control these are meant to be very user-friendly and simple functions certainly the ones we looked at that give you equal probability for all of those but absolutely you could skew things though hopefully if you're implementing a gambling game or the like you're not actually making some cards more probable than others allow me to turn back now to our implementation here of this randomness and consider how we might leverage other types of functionality that aren't necessarily in this specific library here well it turns out that python also comes with a
statistics library and this contains all sorts of functions for doing things more statistical in nature namely calculating means or medians or modes or other aspects of a data set that you might want to analyze so how might we use the statistics module on python well we might first just take a look at its documentation like any other module in python and we'll see within that library that there's a whole bunch of functions and one of those functions is one that's quite simple it's average a function that allows you to calculate the average of some numbers
that you've passed in let me go ahead and envious code in my terminal window open up a new file called average.pi and at the top of this file i'm going to import a different library this time namely the statistics module in python and now i'm going to go ahead and call a function that i know comes in that module namely mean for the average of some values and i'm going to call statistics.mean and i'm going to pass into this function mean a list of some values and let's suppose that i'm quickly trying to calculate what
my current grade average is in school and i did really well on my first test and i got a hundred percent and on my second i did well but not as well and i got a 90. and ironically i'm not very good with math so i'd like to figure out what my average now is between those two tests so let me go ahead now and in this list type in the number 100 comma 90 thereby passing in a list of two values two ants 190 and ins outside of those are the parentheses because of course
this is now the argument i'm passing to the function called mean and this function mean is in the module called statistics well it's not that interesting to just calculate the mean if i don't actually see what it is so let me additionally pass the return value of that mean function to the print function as usual let me now in my terminal window in vs code type in python of average.pi and hit enter and voila as you might expect my average is 95 so the difference here is that i'm just using a different module that still
comes with python but i need to import it instead of for instance the random module instead and this time i know from the documentation that there exists a function called mean well it turns out there's even more functionality that comes with python and that comes with other modules in python and there's this feature generally known as command line arguments this is a feature not just of python but of languages more generally that allow you to provide input not when prompted inside of a program as happens whenever we call the python function input but rather there's
this feature command line arguments of programs that allows you to provide arguments that is input to the program just when you're executing it at the command line so up until now for instance recall that we've generally run python of something dot pi for instance python of hello dot pi and i've never once really executed any words or phrases after the name of the file but i could in fact when you're running programs in a command like environment like we are you can provide any number of words or numbers or phrases after the command that you're
typing and all of those will somehow be passed in as inputs to the program itself you don't have to prompt the user for one thing at a time by manually calling that input function so what does this mean in real terms well let me go ahead back into vs code here and let me propose that we consider how we might leverage a certain module i'm going to go ahead and create a file called name.pi and i'd like to use a new module this time that's going to give me access to values that have been typed
at that command line but what's this module going to be well this one's going to be called sys and sys short for system contains a whole lot of functionality that's specific to the system itself and the commands that you and i are typing the documentation for this module is at this url here and it lists all of the various functions and variables and the like that come with that module but we're going to focus on something a little more specific namely this thing here it turns out in the sys module in python there is a
variable that just magically exists for you called argv it stands for argument vector which is a fancy way of describing the list of all of the words that the human typed in at their prompt before they hit enter all of those are seemingly magically provided to you via python in a variable called sys.rgv this variable is a list which means that the first element is going to be the first word that you type the second element is going to be the second word that you typed and so forth and by way of this list then
can you figure out what words did the human actually type at the prompt and maybe use that to influence the behavior of your own program so what does this mean now in real terms well in this new tab called name dot pi let me go ahead and import sys within that sys module is going to give me access to sys.rgv but how might i want to use it well let's do this instead of writing a hello world program that all of these times has just looked for the return value of input to figure out what
the user wants me to print let's go ahead and just expect the user to tell us when they run the python program itself what their name is and suppose this time i'd like to generate a whole bunch of name tags initially just one and in the us here it's very common to wear a sticker on your lapel that says hello my name is david so i want to print out some text that resembles that the idea being maybe i could enhance this program someday to even send that text straight to the printer and dynamically generate
those name tags well let me go ahead now and do this let me go ahead and print out as always hello but i'll say a little something more this time to make things more interesting hello my name is quote unquote and then after that i normally have been in the habit of calling input storing the return value in a variable and passing in the name of that variable here but i'm going to instead jump right to this sys.rv bracket one and that's it i'm going to have a program here that says hello my name is
followed by whatever is in sys.rv bracket one and notice sis.rgv again is a list and recall from our discussion of loops and in turn list we use the square bracket notation to get at the various elements inside of a list all right let me go down now into my terminal window and run python of name dot pi but this time rather than just hit enter and wait for the program to prompt me for my name let me proactively just tell this program what my name is at the so-called command line here we go dav id
separated with a space from the name of the file so that now when i execute python name dot pi david i see on the screen voila hello my name is david so based on this demonstration alone i think we can infer exactly what's going on in sys.rg even though it sounds certainly at first glance rather complicated here let's look up at sys.rgv i'm going to bracket one here so clearly sys.rv bracket one is storing dav id but it's one in the past when we looked at loops recall that we said that they were zero indexed
that is the first element is zero the next element is one this next element is two and so forth and yet here i am treating it as though my name is at the start of the list one well let me ask this question what is probably in cis.rgv of zero what is probably insist dot argv of zero the very first element actually in that list oh yeah i think it's like in c the name of program indeed it's indeed like in c in other languages the name of the program well if we consider what it
was i typed i certainly typed python because that's the name of my interpreter and we don't really need to know that because we're using python itself but after that i did type two things i typed name dot pi as i've done so many times anytime i want python to interpret a program i've written and it turns out by convention what python does is it stores in sys.rgv the name of the file that you're executing or interpreting followed by any number of other words that you typed so all this time we could have been accessing the
name of the program which frankly isn't all that interesting but we can also now access words that are typed after that prompt as well but of course if i don't type anything in what might happen here this might be naive of me to assume that there's always going to be something at location 1 in sys.rgv let me go ahead and try this python name dot pi and no i'm not giving you my name because at this point i might not even know that you want my name to be typed so let me hit enter now
and uh uh-oh we see now an error a so-called exception in python this one's a new one this one's an index error that elaborates list index out of range and turns out this is actually one of the most common mistakes in programming whether using a list in python or arrays or vectors in other languages is to try to access some element that does not exist you try to go too far to the left or you try to go too far to the right in this in this object that is just a list of some values
so of course the mistake here is that i'm assuming there's going to be something at location one when really it's location zero that's the only one that has a value but fixing this is not going to amount to doing bracket zero because now if i go ahead and rerun this program with no other words after name.pi it says hello my name is name.pie which is fine if we're making a name tag for the program but that's not of course what my goal here is instead so if the fix is not just to change the one
to a zero how else might i handle this error how else might i handle this error this index error that happens if the user just doesn't remember to or doesn't know to type their actual name at the prompt we could always put an exception into the program saying if there is um if there's nothing at location one we just come out to say okay we haven't got a parameter or something but if there is you continue along with the program perfect so if i might simplify we can try to execute this line of code except
if there's an error we'll deal with it in some other way now ideally and once i'm a strong enough programmer i would have anticipated this and written the following code from the get-go but when you're learning it's certainly reasonable to see an error oh i didn't realize i should detect that and then go back and improve your code but of course if you read the documentation you ingrain some of the lessons learned from the past you'll get into the habit of trying and checking for some of these exceptions yourself so let me solve this in
one possible way as you proposed here let's try to handle this exception as follows let me go ahead now and instead of just blindly calling this print line let me try to print out hello my name is such and such except if there is an issue specifically an index error then what do i want to go ahead and do i'm going to say something like too few arguments i could be more explanatory than that but for now i'm just going to explain to the user that they gave me too few arguments too few words at
the prompt so now it's still not going to work in quite the way i want i'm still not going to be able to generate their name tag but at least they're not going to see some cryptic error message and think that they themselves broke the program let me go ahead now and run python of name.pi enter and too few arguments okay let me go ahead now and do python of name.pi and type in my name david and now we're back in business and i see that my name is on the screen too but strictly speaking
i don't have to try to do this i could actually be a little more defensive in writing this code and maybe i could check whether or not the user has indeed provided a name or multiple names at the prompt so as to give them more refined error messages as well so how might i do this well let me go and undo the exception handling i've added and why don't i instead more modestly try to do this let me go ahead and introduce a conditional here if the length of cis.rgv is less than two or equivalently
equal to just one value but i'll just stick with less than two for now then go ahead and print out too few arguments so i want ultimately two arguments i want the name of the program at location zero and i want the name of the human at location one so that's a total of two arguments so if i have fewer than two arguments let's tell the user with this print line l if the length of sys.rgv is say greater than two like they typed in too many words at the prompt well let's tell them print
quote unquote too many arguments else if they did get it right and they gave me exactly two arguments else let's go ahead and print what i actually care about all right let me go down to my terminal window here and run python of name.pi and voila a completely different type of error this one a syntax error which we've seen in the past now a syntax error recall is me a culpa like i messed up here and i wrote invalid syntax and so no amount of conditionals or exception handling is really going to catch this one
i need to go back and just get my program to work because it's not running at all well let me go up here and see line 4 is the issue and indeed it looks like i have an unterminated string here i need to go ahead and now add this double quote so let me go ahead now when with that red herring gone let me rerun python of name dot pi and hit enter and now we see too few arguments okay maybe it wants my full name let me go ahead now and run python of name
dot pi david malin typing in both words after the name of the file and hit enter and now of course it's too many arguments fine now i'll oblige and do python of name.pi and just david and there we have it my name tag printed on the screen so strictly speaking we don't have to handle exceptions if we can be a little smarter about it and just check for the things that we're worried about especially if we want to give the user more refined advice we don't want to just tell them no something went wrong or
we don't want to pass we want to tell them no that's too few or no that's too many we have conditionals in our vocabulary already via which we can now express that well let me pause here and see if there's any questions now on how we handled the error before with the index error or how now we're just proactively avoiding all index errors all together by just checking first is it too few is it too many or is it exactly what we want oh yeah thank you um so i was wondering you you touched upon
kind of using your full name um could we could we then um is there a way going forwards that perhaps we have people that want their full names and want their just their first name that we separate that into like oh this person has full name this person has just the one name absolutely and allow me to allow me to propose we come back to that support for multiple names but indeed we could do that and i should know too though we can support full names right now if i do this instead of typing in
david spacemailin which is problematic because again by definition of how argv works each word ends up in a specific location in the list but if i add quotes single quotes or double quotes at the command line now python will view this as two total things the name of the file and this full name and now when i hit enter i don't see the quotes the whole thing is passed in as my full name and if i want to adapt this further for multiple people we'll be able to do that as well other questions now on
this version with if elif else or on accept before i want to i want to ask you can we use multiple else statement can you use multiple else's statements no else is the last catch-all statement that you can have you can have multiple l if statements in the middle but not multiple elses all right all right well let's turn our attention back now to this code and see if we can't refine it a bit more by adding in some additional functionality that we get with modules like the sys module one of the things i don't
love about this version of the code even though arguably it is now correct is that the essence of my program which is just to print out the name tag is kind of relegated to this else clause and that's fine logically it's correct but generally speaking there's something nice about keeping all of your error handling separate from the code that you really care about having all of these ifs lifts perhaps at the top of your code that are checking to make sure that all of the data is as expected but then it would be nice if
only for design's sake not to sort of hide in this else statement the actual code that you care about i would prefer for instance to do something logically like this i could check for errors up top and then down here print the name tags it would be nice if those are sort of distinct blocks of code all of which are here left aligned but there's a problem with what i've just done here logically what bug did i just introduced by getting rid of the else and introducing line 10 on its own with no indentation outside
of the conditional what bug have i just introduced what mistake to be clear um name error ironically it's a name error but not a name error exception it's an error with my name but i think you're frozen for me it's going to raise an exception because even though i'm checking the length of sys.rgv up top and even though i'm checking it again for being greater than two not just less than two but greater i'm still then blindly and incorrectly assuming it's now going to exist so just to be clear if i run python of name
dot pi and i don't type any arguments i've got too few i think i'm going to see that i have too few but i'm also going to see that same exception at the very top of my terminal windows output there's my error message too few arguments but again on line 10 i blindly proceed to still index into my list at location one which does not exist so it turns out there's a better way to handle errors like this especially if you're writing a program in python that's just meant to run briefly and then exit anyway
but maybe we could start to exit prematurely if the program itself just can't proceed if the user has not given us the data we want perhaps we should just exit the program earlier than we might otherwise so let me go ahead and do this let me go ahead and remove my comments so as to focus only on the code here and let me propose that instead of just printing quote unquote too few arguments i'm going to use one other function that comes with the sys module i'm going to go ahead and call sys.exit and as
the name suggests it's going to do exactly that with the systems help it's going to exit my program then and there on line four why is that okay well if you gave me too few arguments i have nothing more to say to you the user i might as well exit a bit prematurely and i can do this as well on line six let's go ahead and not just print that but sys.exit quote-unquote too many arguments print out that message and just exit right there now i can trust that by the time i get to line
eight every error condition has been checked for and so it's safe for me to assume that there is in fact an item at location one in sys.rgv so let me go ahead now and run this python of name dot pi enter too few arguments but i'm back at my prompt nothing more has happened let me run it again python of name dot pi david malen with no quotes enter too many arguments is now printed here finally python of name dot pi just david enter hello my name is david so we have then insists two forms
of functionality now we have access to this variable sys.rgv this argument vector that gives me all of the words that were typed at the prompt including the program's own file name and it turns out if we read further in the documentation there's an exit function that can take different types of inputs but if i pass it a string like this it will indeed print that string for me and then exit from my program then in there questions now on exiting from programs like this to be clear all of this time once python gets to the
bottom of your file it's going to exit anyway so i'm using sys.exit now just to make sure that i exit earlier than otherwise um my question is about the cis that are our v so is that capable of accepting or taking multiple elements at once let's say for example a python name that pi david malin i'm a male uh 20 years old and if let's say i only want to access your name which is at the first index and then your your your age is brought say at the sixth index can i say cis.rb one
and another one for six to access what i just want is that possible for cis harvey uh short answer yes i think if i understand your question correctly whereby you're proposing to have many words at the end of the command and you want to access those individual words absolutely at some point it gets a little fragile i would say if you're typing so many words at the prompt that the order really matters and so it turns out there's a lot of programs and there's functionality in python that can allow you to provide those values like
name or age or any number of other fields in any order you want but pass in a bit more information textually that tells the program how you want to use it so in short what you're describing is possible and let me do a small incarnation of it as follows let me propose that we go back to my code here and let's propose that we actually now want to support multiple values at the prompt so there's going to be no such thing as too many arguments suppose that i want to generate name tags not just for
david but for david for carter for wrong shin for others in the group who all want their name tags as well so i'm going to go ahead and do this i'm going to get rid of my elif condition because i don't want to limit the maximum number of words that are typed at the prompt anymore i instead want to iterate over every name at the prompt so i'm going to say this for arg in sys.rgv go ahead and print out this time arg so what am i doing here well even though the syntax is a
little different the idea is the same as before when we've had loops i'm using a for loop to iterate over a list the list in question here is sys.rgv arg is a variable that i'm creating on the fly the for loop is going to make sure that the first time through this loop r gets set to the first word on the command line the second time through the loop python python's going to make sure that arg is now set to the second thing on the command line and so forth that's just how a for loop
works it updates the variable for us i don't have to call it arg i could call it name so long as i change it to name in both places but arg is reasonable if i'm iterating over arguments more generally if i now run this program though unfortunately there's a little bit of a bug even if i type in david and carter and wrong shin i'm not gonna get just three name tags in your mind does anyone see the bug i'm about to trip over it's not a huge deal if i've got enough name tags to
go around but i'm gonna be wasting one because this is gonna print not three but four name tags whereby the first contains the name of the program itself maybe not a big deal maybe that's the sticker we don't bother handing out but it's wasteful and it does look wrong so how could we get access to not all four elements of argv but just a slice of argv and this is actually a technical term in python and some other languages to take a slice of a list means to take a subset of it maybe from the
beginning maybe the middle maybe the end but a slice is a subset of a data structure like a list well how do i actually do this in code well in python it's actually very easy to take a slice of a list that is a subset thereof you can simply do this at the end of the list name sys.rgv in this case you can use square brackets and then in those square brackets you can specify the start and the end of the list that you want to retain i want to start at element one not zero
i want to start at element 1 and i want to just go to the end so i'm actually going to omit a second number altogether it's not necessary to have a second number but i do need that colon because this is going to give me a slice of the list it's going to give me a slice of the list that starts at location one not zero and the colon and then a blank just means it's going to give me everything else so this is equivalently going to slice off the first element of the list and
give me a new list that contains just those three human names not the name of the file itself let me try running this again i'm going to run python of name dot pi david carter wrong shin this time hopefully i'm going to get three and only three name tags hitting enter and indeed i've done now just this so again using some relatively simple syntax in python we can use square brackets not just to go to specific elements like bracket zero or bracket one we can also get subsets of the list slices of the list by
doing bracket something colon something where each of those somethings is a number the beginning or the end and they're optional depending on whether you want all of them or just some any questions now on this version which adds the loop and these slices with that new syntax can we slice starting from the end of the argument argument vector you can you can slice something from the end of the argument vector and this might uh this might blow one's mind a little bit let me go ahead and do this uh let's see let me go ahead
and do negative one at the end using a negative number here and running the same command we've just uninvited wrong shin from receiving a name tag here so if you use a negative number it has the effect of counting to the uh in the other direction from the end of the list a good question there other questions now on slices on looping over sys.rgv hi uh so i remember very early on uh when we were talking about uh only having two decimal places in uh in an um float value um does this is is that
in the same vein like um because we use the the colon point two f uh so that's is that the same thing then uh why would the f be included then in the point two f as opposed to here when you just have the numbers a really good question and it's just the short answer is that context matters so there's only so many keyboard keys on our keyboard and so we sometimes use the same symbols for different things so what you're alluding to is the format code in in fstring for actually formatting a number using
a colon using a period using a number using the letter f and so forth and that is very specific to the f string feature of python this case has nothing to do with any of that syntax per se this is just using a colon in a different context to solve this problem to implement a slice the authors of python could have chosen another symbol but honestly looking down at my keyboard here we don't have that many to choose from that are easy to type so sometimes they have different meanings a good question as well allow
me to propose now that we take things further and move away from using only those modules those libraries that python comes with to talk about more generally packages that exist one of the reasons that python is so popular and powerful these days is that there's a lot of third-party libraries out there as well otherwise known as packages strictly speaking python itself has a term of art called a package which is a module essentially that's implemented in a folder not just a file but a folder but more generally a package is a third-party library that you
that i can install on our own mac or pc or our cloud server and gain access to even and this is a website that is searchable via the command line as well as via the web that allows you to download and install all sorts of packages even cs50 has some of its own packages in services like these now there's a fun one out there that's a throwback to a command that's been around for years in command line environments called cowsei causee is a package in python that allows you to have a cow say something on
your screen if curious to read up on it its own documentation is on pipi.org specifically at this url here but how do you actually get the package into your system well technically you could figure out how to download the file and maybe unzip it and put it into the right location on your mac or pc but nowadays a lot of languages python among them has what's called its own package manager this one here called pip which is just one so pip is a program that generally comes with python itself nowadays that allows you to install
packages onto your own macs or pcs or cloud environment by just running a command and then voila you have access to a whole new library in python that didn't come with python itself but now it's available on your system for you let's go back to vs code here and in my terminal window i'm going to go ahead and type pip install cow say now what's going on here pip is the command the package manager and i want to install what package the package called cow say i'm going to go ahead and hit enter here and
after a little bit of output it has successfully installed cow say now what does that mean that means i can now go about importing this into my own code well let's go ahead and see what this means let me go ahead and create a new file with code called say dot pi because i want something to be said on the screen and in my new tab here i'm going to go ahead and import cowse which presumably is now installed i'm now going to import sys as well because i'd like to use some command line arguments
in this program just so that i can run it quickly and without using the input function i can get the user's name immediately from the prompt and let me go ahead and do this i'm going to do a bit of error checking proactively this time and rather than use less than or greater than i'm this time going to say if the length of sys.org v does does equal two so if the human is provided just the name of the program and their own first name we're good to go i'm going to do the following i'm
going to call a function called cow in the package called cow say and i'm going to pass in a string hello comma and then as in the past i'm going to pass in just one string because according to its documentation it's not like print i can't pass in comma this comma that i can only pass in one string so i'm going to concatenate the contents of sis.org v bracket one so long as then i type in my name david after the name of this program it should end up in sys.org v1 in which case this
line 5 of code should concatenate hello with my name with a space in between and apparently a cow is going to say it so let's see what happens here let me go ahead and clear my screen and increase the size of my terminal window let me go ahead and run python of say dot pi and type my name david and enter there is the program called kausei it literally has a cow say something on the screen and this is a throwback to a program from yesteryear that tended to come with a lot of systems this
is otherwise known as ascii art it's a textual way using just keys on your keyboard to print pictures of sorts on the screen now we can really go down the rabbit hole here and there's questionable academic value of doing so so i'll do so just once turns out the cow say package comes with other functions as well one of those functions for instance is t-rex and if i now increase the size of my terminal window we'll perhaps see where we're going with this let me now run again python of say.pi this time let me not
provide my name just to see if it's broken it's still okay because we have that if condition if the length of sys.rgv equals equals 2 and only if it equals equals 2 do we do anything that's why we're not seeing anything here let me go ahead and cooperate now say.pi space david and it's no longer a cow but if i zoom out on my screen a t-rex why just because these are the things you can do once you know how to program you can even package them up and make them freely available to others as
open source software for us it's demonstrative of a feature more generally here namely being able to install these third-party packages and how you might do so in python now i'll leave this up on the screen for a moment and see if there's any questions about cows or tyrannosaurus rex's or packages more generally i'm really qualified to speak to just one of those hi um i've got two questions it's a bit earlier than what it's supposed to be um so the first question is the packages that you're calling um to use in the program are they
the same as um let's say because i'm doing java the same as calling a class of a java file in order to use its functions and my second question is what's the actual purpose of using command line arguments as you use because it's not really the best way to as you say be user friendly where as in let's say the person who's using the program doesn't know what it what they want what the program's asking them really good questions the first question about the comparison with java python packages are similar to java packages where you
have something dot something dot something at the top of your program that gives you access to a class or something else python itself supports classes more on those down the road and you can do very similar things in python as you can do with java but the analog really is python packages to java packages here as for command line arguments you ask a good question why do we use them especially if they're literally yes user friendly they're a little less user friendly to people who aren't in this zoom to be honest you and i as
we learn more and more about programming and more about command line arguments i daresay will become more comfortable with and tend to prefer the ability to customize commands using these command line arguments why productivity it tends to make you faster because you get into the habit of knowing exactly how you can configure your software without having to manually answer questions and case in point all of this time have we been running python of something dot pi you could imagine not doing that you could imagine typing only python hitting enter and then you're prompted for the
name of the file you want to run so you type in something.pi and then it runs not a big deal but i i would argue that over time you're going to get a little tired of that tedium and you would much prefer to just automate the command again and again and again especially with little conveniences like being able to hit up and down in your keyboard history so as to rerun those same commands automation is big too if you emerge from a class like this and start using python to automate processes at work or for
personal projects or the like the ability to specify all of your inputs on the one line just means you can get work done more quickly so hands down absolutely using command line arguments is a more arcane uh feature of systems that most of us are no longer as familiar with because of windows and mac os and other operating systems that have buttons and guise and menus but the more comfortable get you get with programming i dare say the more you will tend to prefer these capabilities because they allow you to do things more quickly with
that said allow me to propose that we take a turn toward yet another package that's particularly popular and just as easy to install all toward an end of using apis now apis are not something that's python specific more generally an api is an application programming interface and it can refer to python files and functions but often apis really refer to third-party services that you and i can write code that talk to many apis but not all live on the internet these days so that so long as you have a browser or so long as you
have some experience with python programming or programming in any language you can write code that in effect pretends to be a browser connects to that third-party api on a server and downloads some data that you can then incorporate into your own program now how do you do this well python has a very popular package that you can install via pip called requests the requests library allows you to make web requests internet requests using python code essentially as though you were a browser yourself you can automate there for the retrieval of urls that start with http
or https the documentation for this library is a url like this but it too can be installed at the command line and even though it's third party it's one of the most popular and commonly used packages out there in python and this too is one of the reasons again that python is so popular there's just so many solutions to problems that you and i have or are invariably going to have when we write projects of our own there's just a really vibrant ecosystem a really vibrant community of open source software that's that easy for us
to install let me go back to my terminal window now and run pip install requests in order to install this package on my own system and after some lines of output i'll see that it's successfully installed now let's go ahead and create a new file here for instance itunes.pi it turns out that apple has its own api for their itunes service the software that provides you with the ability to download and search for music and songs and other information as well and it turns out that let me go back over to my computer here and
open up a browser like chrome and let me go ahead and visit this url here https colon slash itunes.apple.com search question mark entity equals song ampersand limit equals one ampersand term equals weezer now i constructed this url manually by reading the documentation for apple's api application programming interface for itunes and what they told me is that if i want to search for information about songs in their database i should specify entity equal song so that it's songs and not albums or artists or something like that if i just want to get back information on one
song i'm going to provide limit equals one and if the band i want to search for the artist is weezer i should specify term equals weezer so with this if i go ahead and hit enter and visit this url i actually end up with a text file in my downloads folder on my mac if i go ahead and open that text file that my browser just downloaded will see all of this text here which at first glance might look a bit cryptic but it actually follows a pattern notice this curly brace at the start and
notice this closed curly brace at the end notice this open square bracket here and notice this close square bracket here and in between those pieces of syntax are a whole bunch of strings and values in fact a whole bunch of key value pairs what we're looking at here is a standard text format known as json javascript object notation which yes is technically related to yet another programming language called javascript but json itself is typically used nowadays as a language agnostic format for exchanging data between computers by language agnostic i mean you don't have to use
javascript you can use python or any other language to read json or write it as well and it's a completely text based format which means that if i visit that url with my browser what gets downloaded is just a bunch of text but that text is formatted in a standard way using curly braces and square brackets using quotes and some colons that ultimately contains all of the information in apple's database on weezer's song at least the first one because i limited it to one in their database and that's an api an application programming interface a
mechanism whereby i can access data on someone else's server and somehow integrate it into my own program now of course my browser chrome is not something i wrote i should actually write some python code that perhaps pretends to be a browser to grab the same data so let's do that let me go back to vs code here and let me write a program with code itunes.pi and we're going to write some code by which i can then use the itunes api and in turn python to get information about any band that i might want i'm
going to go here and import first the requests library which i installed earlier in order to make those http requests i'm going to go ahead and import the sys library by which i'll have the ability to use command line arguments like specification of the band that i want to search for if not weezer and then down here i'm going to go ahead and insert some error checking to say if the length of sys.rgv does not equal 2 so if the user does not provide me with the name of the file they want to run and
the name of a band and that's it you know what let's just go ahead and exit for now i could provide a more explanatory message but for now i'm going to keep things simple and just exit the program prematurely so that i can trust hereafter that sys.rt has what i want and now i have the opportunity to use the requests library to write some python code that effectively is pretending to be a web browser so as to connect to that same https url on apple's own server so now that i've guaranteed that the user has
typed in not just the name of the file but also the name of a band at the prompt giving me a length of 2 for sys.rgv let's go ahead and execute requests.get which is a function inside of the request package that will literally get some response from a server and the url that i want to get is exactly the same as before https colon slash itunes.apple.com search question mark entity equal song ampersand limit equals one ampersand term equals previously weezer but let's make this program a little interactive and actually allow the human to specify at
the command line what artists they'd like to search for so i'm going to go ahead and close my quote early and just append using the concatenation operator as in the past sis.rgv bracket 1. and now it'd actually be nice to store the response from the server in a variable so i'm going to go ahead and say response equals and to store all of the response that comes back from the server in a variable called response down here now i'd like to just understand what the server is returning to me to make sure i know how
next to proceed so this isn't going to be very pretty yet but i'm going to go ahead and print out response.json which ensures that the data i'm getting back is formatted on my screen as exactly that json the same text format as we saw on my screen it's not a useful program yet i'm really just learning along the way but let me go ahead now and increase the size of my terminal window and run python of itunes.pi and type in the name of a band like weezer and hit enter and what we see on the
screen formatted almost the same as before is exactly that same text but what you'll see here is that this has been standardized now as a python dictionary what indeed apple is returning is technically a json response javascript object notation but python the request library is converting it to a python dictionary which happens to use wonderfully coincidentally almost the same syntax it uses curly braces to represent the dictionary here and a closed curly brace to represent the end of it here for any lists therein it uses a square bracket here and a closed square bracket down
here it uses quotes single quotes in this case or equivalently double quotes to represent the keys in that dictionary and after a colon it stores the value of that key and so you'll see that indeed we have a result count key whose value is one but then a more interesting result key called results whose value is this entire list of data now honestly this is such a big blob of text that it's going to take me forever to wrap my mind around what i'm seeing so let me propose temporarily we use another library in python
that will allow me to format my data a little more cleanly it turns out that python also comes with a special library uh called json that allows you to manipulate json data and even just printing print it that is formatted in a way that's going to be way easier for you and i to understand so let me go back to my code here let me shrink my terminal window and let me propose that just temporarily again we do this let me import this additional library json which comes with python so i don't need to install
it manually with pip and let me go ahead now and not just print out response.json which was that big blob of hard to understand text let me go ahead and use one other function here called json.dump s for dump string and pass to that function that response dot json return value so again i'm just introducing another function who i claim it has a purpose in life of pretty printing nicely formatting on the screen the exact same information i know this from the documentation having done this before but i'd like things to be nicely indented and
according to the documentation if i pass in a named parameter of indent equals two that's going to indent everything at least two spaces i could do four or something else but it's going to be enough to help me wrap my mind around what the data is i'm getting back because again i'm just learning along with you so let me increase the size of my terminal window again let me run python of itunes.pi and again let's search for weezer and hit enter and now notice it's still a little bit cryptic because there's a lot going on
here but my gosh i can totally read this more easily now notice now that i still see the first curly brace which means hey this is a dictionary in python a collection of keys and values the first key is called result count it happens to be displayed in double quotes now but that's just an issue of formatting it could be double or single so long as we're consistent the value of that key is one why well i told the url to only limit the responses to one weezer song so i've gotten a result set of
one if i increase that limit i could probably get more then the interesting part of this response is really the data itself notice in the results key here there's a really big value the value is a python list as implied by this square bracket what does this list contain well i know from skimming it earlier that this contains one dictionary and that's why we see another curly brace here so again if this gets a little more complicated keep in mind that a dictionary is just a collection of key value pairs and python uses curly braces
to indicate as much it is perfectly reasonable for a dictionary to be inside of another dictionary if the value of some key itself is another dictionary so this is a common paradigm and even though it might seem a bit cryptic it's just something that allows us to associate more keys with more values now most of this information i probably don't care about for instance according to apple the unique identifier for weezer is apparently 115 234. that might be useful if i'm making my own database and i want this to be searchable but for today's purposes
all i care about is the name of the track otherwise called track name as key and the first song and only song because we limited it to one that we got back from itunes here is the song that you might know by weezer called say it ain't so so now i have a bit of a clue if my goal here is to implement a program called itunes.pi that doesn't just dump the response from the server which is admittedly very cryptic but to print out all of the songs that itunes has for the band called weezer
maybe i can iterate over this somehow so let me backtrack here's the key called track name it is inside of a dictionary that is the value of results here so how can i go about getting this well let me go ahead and try this let me go ahead and shrink my terminal window back down and let me propose now for one final flourish we don't just lazily print out the contents of that response because that's not interesting or pretty for anyone let's do this let me go ahead and create a new variable just for the
sake of discussion called o for object and i'm going to go ahead and call o equals response dot json just to store that json response specifically in a variable called o but i could name it anything i want and now i'm going to do this for each result in that object's key called results go ahead and print out that results track name and notice i have used exactly the same capitalization track name has a capital n results is all lowercase and let me rewind before we run the actual program in line eight we are making
an http request using python to the server just like you and i as humans type urls into a browser and hit enter this is the python equivalent thereof i am then on line 10 just grabbing from that variable that contains the server's response the json object that i care about the thing between those curly braces at the very top and the bottom but because we've poked around and because i read the documentation earlier i know that that object has a key called results and that results key again is a list now at the moment that
list contains only one song say it ain't so because i limited my response to one but even so my loop will work it's just going to iterate once and each time through that loop it's going to print the current results track name if i want to make this even more interesting let me change this limit now from 1 to 50 so i'll at least get back 50 track names instead let me go ahead now and increase the size of my terminal once more and go ahead now and run python of itunes dot pi searching again
for band like weezer and here we go and voila there are 50 songs that itunes has for weezer and if we scroll back up to the top here we'll see that the very first one there is indeed say it ain't so but now we got undone the sweater song buddy holly apparently another rendition of say it ain't so perhaps from another album another buddy holly undone my name is jonas and so forth questions now on this program which integrates python with a real-world third-party api yeah hi uh can we use break instead of system.exist exactly
good question but no um break again is used to break out of things like loops like we saw earlier sys.exit is used to break out of the whole program itself use break for loops for now and use cis.exit to terminate the whole program good question other questions now on this program are others from where we bring the name of the key results from where do we get the name of the key uh results itself can we change the results you cannot you so we could in our program so the keys that come back in that
json response to be clear come from itunes.apple.com some engineer some team of engineers decided for us what all of those keys would be called including track name results result count and everything else you and i can absolutely store those same values in variables just like i'm doing here with o just like i'm doing here with results you can rename those keys anything you want using python variables but the json response is coming from that third-party server other questions yes sir so i have a question related to cause a package so like uh yes so so
what sort of uh ascii graphics uh is it capable of outputting the calce package um i would refer you to the url in the slides earlier uh if only because it's more thorough they have not just cows but tyrannosaurus rex's and several other animals as well i should emphasize that this is not a package i suspect you will use much in the real world it's really just meant to be representative of the types of packages you can install but allow me to refer to the documentation for what more is there but ascii art uh is
the all we had before there were emojis let alone gifs and jpegs and pings but it's what is immortalized in cow say well allow me to transition us back now to one final capability of python which is that you yourselves have the ability to make your own libraries up until now we've been writing all of our functions in our one file hello pi and everything since and now that we've introduced modules in python like random and statistics we can import those that come with python but that's other people's code as well and we've now used
pip this package manager to install third-party packages as well in the system and using other people's code still but to come full circle what if you yourself find yourself implementing the same kinds of functions again and again or you find yourself opening up old programs copying and pasting code you wrote into new programs because you have the same problem yet again a good practice would be to somehow bundle up that code you keep reusing and make your own python module or package you can keep it local on your own mac or pc or cloud server
or you can go through the steps of actually bundling it up making it free in open source and putting it on something like pi pi for others to use as well okay i'm going to go ahead and run code of sayings.pi to create a brand new file called sayings.pi which is going to be my own sayings module i'm going to define a couple of simple functions in there i'm going to define a hello function that's going to take a name parameter as input and that function is simply going to print out an f string that
contains hello comma and then in curly braces whatever that person's name actually is then i'm going to go ahead and define one other function a goodbye function that has def goodbye also takes a name as its input and then that prints out by contrast an f string that says goodbye comma and then in curly braces name and now just for good measure just so i can be sure that these functions are working as expected i'm going to go ahead and define a main function in here too just for the purposes of testing and i'm going
to go ahead and define a main function that simply does a couple of tests for instance it calls uh hello of quote-unquote worlds shall we say and then it's going to call goodbye of quote unquote world as well and hopefully what i'll see on the screen then is hello world and goodbye world when i run this program of course as always i need to explicitly tell python to call that function so i'm going to call main at the very bottom of this file all right let's try it out python of sayings.pie enter and indeed i
see hello world and goodbye world and so i think it's reasonable for me to assume that these functions albeit simple are pretty correct at this point but now suppose that i want to use these functions as though i've indeed created my own library my own python module that makes available a hello function for me or anyone else who wants to use it or a goodbye function as well well let me go ahead and open up again say.pi but start fresh and rather than have the cow say anything let me go ahead and have my own
library do the talking so i'm going to go ahead and as before import sys so that i have access to command line arguments and from my own module called sayings i'm going to import hello so because i created a file called sayings.pi i can say from sayings and it's inferred by python that i mean sayings.pi at least in this current directory but i specifically going to import just one of the functions for now namely hello and now i can do something like this if the user obliges by giving me two command line arguments which i
can check by just checking the length of sys.org v i'm going to then go ahead and call this new hello function passing is its input assist.rgv bracket1 which should hopefully be the person's name which i'm going to expect them to type at the prompt so here we go i'm going to go down to my terminal window run python of say.pi and my own name because i want my own name to end up in the command line arguments and therefore be part of the hello so when i hit enter in just a moment i should hopefully
see hello comma david so here we go enter and huh i see hello world goodbye world and then i see hello david so why is this happening well it turns out even though i've done everything according to our own past practice it's not really the right way to go about calling maine after all if i'm blindly calling main here at the bottom of my file that means whenever this file is loaded by python main is going to get called and unfortunately that's true even if i'm importing this file or just a function from this file
as i am here in my say.pi program this is to say on mine three here when i say from sayings import hello this effectively tells python to go find that module sayings.pi read it from top to bottom left to right and then import specifically the hello function unfortunately by the time python has read the file from top to bottom left to right that last line of code recall is to call main main gets called no matter what so really the right way to go about using a main function which does solve that problem of ensuring
that we can order our functions however we want and all the functions will be defined at the time they're invoked i shouldn't be unconditionally calling main at the bottom of this or really any of my programs i should instead use this technique i should say if underscore underscore name underscore underscore equals equals quote unquote underscore underscore main underscore underscore close quote then and only then should you actually call main well it turns out that this variable is a special symbol in python underscore underscore name underscore underscore and notice that vs code because of its font
isn't quite showing those two underscores but they're indeed there to the left and the right this is a special variable whose value is automatically set by python to be quote unquote main when you run a file from the command line as by running python of sayings.pi so watch what happens now with this additional conditional in sayings.pi if i run python of sayings.hi it still works as before because name will be automatically set to underscore underscore main underscore underscore when i run this file using python of sayings.pi but notice this name is not going to be
set to quote unquote main it's going to be set to something else technically the name of the module when i instead import the file like i do here so this highlighted line of code even though it will cause python to go find sayings.pi read it from top to bottom left to right it's going to ignore the call to main this time because it's wrapped in that conditional in this case when i'm importing a file and not running it directly at the command line main will not get called by definition of that names value so let
me go ahead and try this instead of running python of sayings.pi which is the module which contains that conditional main let me go ahead here on run python of say.pi which is the program here before me that imports hello from sayings but because of that conditional it's not going to say hello to anyone else except me in this case all right we're here at the end of our week it's only appropriate i think to import something other than hello why don't i go ahead and import not hello but goodbye from here let me go ahead
and call goodbye instead of hello and this time when i run python of say pi i'm not going to type my own name allow me if i may to type in the whole world so that our final sentiment today is goodbye world indeed that's it for this week we will see you next time
Copyright © 2025. Made with ♥ in London by YTScribe.com