Python Programming - Full Course for Beginners to Experts [Tutorial]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
i'm going to show you how to get set up ready for the session first we're going to have to make sure you've got python installed the easiest way to get access to python uh with a bunch of extra tools is by downloading the free tool anaconda so if you first go to anaconda.com click on the download link up there in the top hand right corner and on this page if you scroll down the page you'll end up on the download section where you've got a choice between python 3.7 and python 2.7 make sure at this point you click on the python 3.7 version not python 2.7 click the download link and follow through the installer and once you're all done go ahead and launch anaconda navigator anaconda navigator will look something like this we'll be using jupiter lab in this session as it's easy to get started with but if you have another setup using something like pine charm visual studio code or spyder that's absolutely fine feel free to go ahead and use those on the jupiter lab tile click on the launch button and it will open up a window in your web browser which will hopefully look something like this it might take a few moments to launch now i'm going to go through and show you how we're going to set things up in jupiter lab for this session first we're going to launch a text editor so if we click on file in the top left go to new and click on text file we can now close this launcher window because we don't need that anymore second we're going to launch a terminal so we go to file again and once more new and go down to terminal and click that finally we're going to launch a python console so if we click on file new and the top value there is console this should pop up window asking you which kernel you want if you're asked this make sure you select python 3. now we're going to make sure we lay this session out so it's going to be easiest for us to use i'm going to minimize this area on the left here just to make a bit more space on the screen and i'm going to maximize this window the way i like to have things laid out is with a text editor on one side of the screen and the areas where i run code on the other so to do that i'm going to click and hold on the tab of the console as you do that and move the mouse you'll find that a blue box appears if you move that to the right hand side of the screen and let go you'll see it's laid out side by side i do the same thing with the terminal click drag and hover so that it completely covers the console on the right hand side when i let go we have a console tab a terminal tab and a text editor on the left hand side with that you're ready to start welcome to beginning python i'm going to give just a minute first to check that everyone's got anaconda started up and have able to launch it and get it all working so just take a minute now to start anaconda navigator and if you can also start jupiter lab and in just a moment i'll start going through that myself and those who haven't got it set up themselves can hopefully follow along and be ready to get started so the way to start anaconda navigator will depend on which operating system you're on it varies from place to place but hopefully the instructions that were provided with it gave you the ability to get at least as far as a window that looks something like this one here so it's the green anaconda navigator with the circle and a bunch of tiles on the screen as well anaconda navigator is the way that we are getting access to python so python is provided for free it is a completely free and open source piece of software there are lots of ways to get it installed onto your system but one of the easiest that we found particularly for these training sessions is the anaconda system so anaconda provides us with the programming language python as well as a bunch of tools which we can use to write and run python scripts some are basic tools and some are very advanced and you see a selection of some of these on the screen here for the course today we are going to be using a tool called jupiter lab note that jupiter lab is different to jupiter notebook lab is a web-based simple development environment which allows us to write and run python scripts and more but that's the extent that we're going to be using it for today so go ahead and press the launch button on the jupiter lab tile and i'll do the same and that should open up in a web browser the jupiter lab environment takes a few moments you see your web browser should pop up it thinks about it quite hard you get spinny things and then we are here with that i'm going to put anaconda navigator away make this full screen and nice and big so you should end up in an environment something like this if you've already followed through the video explaining how this stuff gets set up then you'll automatically be left in the environment you last left it in so it should all be laid out correctly so the things we're going to be doing in a session today is writing python scripts and running python scripts so there's two different tools that we are going to be using firstly we're going to be using the text file environment to write our python scripts so i click on text file and that has opened up this whole window here has a text file it's just a text editor i'm going to open a terminal to do that by clicking the plus in the top left and then at the bottom here i press terminal i now have a terminal covering my screen to make it easier to see everything that's going on at once i'm going to click and drag on the terminal tab over to the right hand side so that i have the text editor on one side and the terminal on the other this is all covered in the first video that we sent out so if anything here isn't working or you've just missed what i did feel free to have a look at that first video it's only a few minutes long to catch yourself up i'm just going to do something which you don't need to do so ignore this it's because i've got my files in a special place to avoid confusing things there we go so we're going to start off by writing our very first python script but before i jump ahead and do that i'm just going to give you all 30 seconds or so to make sure you're up to speed with this you've got your text file open you've got your terminal open and it looks largely the same as what you see on my screen bear in mind anything you have in this bit on the right might look slightly different but it's basically the same hopefully everyone has now got their environment set up like seeing on the screen here so we're going to go ahead and run our first python script so before we can run any python code we first need to write some code so let's go over to the text editor on the left hand side and the first thing we want to do because currently this file is called untitled.txt the first thing we want to do is rename this file so that it has the correct file extension so python file should end with dot py not txt and secondly untitled is a undescriptive name for what we're going to be doing so if you right click on the file name in the file browser on the left hand side and go down to rename so right click and rename and we're going to select all of that text including the dot txt and i'm going to call it script.py and then press enter and you'll see it's renamed here and it's renamed up here you should also see the little icon next to it has now changed into the logo of the python programming language so we know that it's understanding that this is a python file with that we can go ahead and start writing our python code we're going to start with what is probably the simplest python program you can write and i'm going to explain the different components that go into it and from there we're going to build up into more and more advanced scripts as we go through the session this afternoon so the first thing we do is write the word print all lowercase and without any spaces before or after the word and we follow that with an opening and a closing round bracket this is the print function functions in python are usually lowercase they can't have any spaces in the name of the function and a function is called by following it with an opening and a closing round bracket some functions don't take any different parameters or arguments so they don't have anything in between the round brackets the print function however does take arguments and the arguments it takes are things that we want to display onto the screen so to give information into the print function we have to put something in between those two round brackets so what we're going to put inside there is some words we're going to ask it to display some words onto the screen the way you represent words or zenon in python strings is using pairs of double quotes so if you do a double quote and another pair of double quotes and then in between them we write hello so what we have here is the word print followed by an opening round bracket saying that we are calling the function then we have the thing that we're giving to the function the information that we want the function to process followed by a closing round bracket to say that we've finished giving information to the function you'll notice here that the text has been colored green and red and so on the specific colors aren't really important it's just the way that the text editor gives a bit of information so it's telling us that it's understanding what's been written other editors that you can use won't have this particular colors they might use blue and red or blue and pink or any other colors in principle the other thing to note is the text here is just plain old text there's nothing special about the program that we're running this in we can write python scripts in a basic text editor like notepad or you can use advanced integrated development environments like visual studio all of them work in the same way they're writing simple text files which are then going to be interpreted as python code so in order to run this script first thing you need to do is make sure it's saved so one way to do that i think is to go to file and save python file make sure you always save your scripts before you run them now that we've saved that file we can go to the terminal so we're going to write our python scripts on the left and run them on the right to run your python script there's a few different ways you can run your python script and i went through the options for that in the second video that was linked and it's been posted in the chat so if you get anything any air is coming up go and check out that second video and that will explain what the other methods are and what kind of error messages you might see and have a go at them and if that doesn't work let us know in the chat and we'll give you a hand but the way i'm going to do it on my screen is by running the program python so i just write the word python ignore the dollar sign that's just something that's built into my terminal that's there by default you see that's just telling me that this is a terminal i write the word python followed by a space and then i give it the name of the script it i want it to read scripts dot py and so what's going to happen here is that this program here which is called python with a lowercase p is going to read the file that i give it which is this text file over here and it is going to read that file one line at a time it's going to look at the text on that line is going to interpret it assuming that it's written in the language python python is a language as well as a program so as long as we've written some text in this file in the python language then the python program is going to be able to understand it and do the things that we've asked it to it's going to read it one line at a time and do the things on that line then read the next line and do the things on that next line and so on and so on until it gets to the end of the file at which point it will exit and say i'm all done success so tell it to actually kick off that process in the terminal over here after writing python space script dot py we just press enter and we see here it has done what we've asked it to it has printed this is a bit of computer jargon print in computer jargon generally means display on the screen it doesn't literally mean print out of a printer it's a hold over from the olden days of computing when everything was literally printed out on a printer but it's displayed the text on the screen hello the same as we wrote in our file over here it is then returned us back to the terminal prompt the same as it was before with a flashing cursor ready for us to run the same thing again so i'm just going to give you uh a minute or so now to have a go at doing that yourself i'm also going to point you at the course notes so what we're going through here at the bottom of the first page as a next link we're having a go at the chapter titled getting started here on the whole i'm going to be just doing the examples as they are in the notes and i'll hopefully have gareth keeping us topped up and tracked as to which chapter we're on but here we are just going through this very first chapter titled getting started in which we've written a text file and we've run that text file and then a little bit of an explanation about how that all works which i've just gone through so have a go at that yourself just take a minute or so and if that's all working then we're gonna carry on in about uh one minute that should be enough time uh ming d um so the color in the terminal doesn't matter at all mine is all multicolored because i'm running zsh on linux which has been set up to be all multi-colored on most systems it's just going to be plain black so if you see hello printed out then everything's working great some questions from uh olivia and ryan saying you're getting syntax error invalid syntax that's again a number of different ways that this can be caused there's two main types of errors that python has there's errors that happen while the program's running like you try to divide by zero or you're trying to use some information that it doesn't know is there the other type of error is a syntax error and that's when your file is written in something which isn't valid python language so the first thing to check is that your python script looks exactly the same as it does over here they're using double quotes and not backticks or anything like that they are using round brackets and not square brackets or curly brackets and that everything is in the same lowercase print as you see here do you get anything else printed out in the terminal apart from the syntax error could you copy and paste the entire thing of you running the script and the output in the chat and that might help me uh work out what's going on there ah okay i see what's going on here so this will happen if you've accidentally got yourself stuck in the python interpreter there's a thing that happens if you just type python and press enter so don't do this i'm just demonstrating if you do this you end up in an environment where you've got these three arrows here now this is like a terminal but it's slightly different if you end up there type exit and then open and close round bracket and press enter and you should end up back at the non three arrow terminal once you're back at the normal terminal you should be able to type python scripts dot py and you should get the text printed out so i'm just gonna clear my terminal on the screen there so that it has a bit more space don't worry about doing yourself just clearing out what we see so in python um there are a bunch of different types of data that you can have the types of information we've used so far has only been one type of data we have had a string they're called strings because they're kind of like a string of letters chained together it's another bit of jargon which you will get used to so we have a string here hello is a string strings are designated by having open quotes at the beginning and open quotes at the end but as well as strings there are also different kinds of data types i'm just going to run the script over here as a shortcut and a tip if you're in the terminal and you press the up key on the keyboard just once it will bring up the previous command you ran so you can press up on the keyboard and then press enter and it will rerun that same previous script i'm going to change this print function here now so that it prints something else so instead of printing hello world i'm going to print a number so 3.14159 that's more than enough digits of pi you'll see here that the number is green where the string was red that's just the decision that the highlighting in this text has decided again the colors aren't particularly important they're just there as a visual aid you'll notice again there's a black circle here and i'll point this out in the video but make sure that before you run your script you save the file on windows and linux it's control s and on mac it's command s to save the file or you can go to file save python file i'm going to use ctrl s so now if i run the script by pressing up and enter we get our number printed out okay so we've got strings we've got numbers and we also have integers which don't have decimal places that works very well so if we save that and run that we just get the number three getting printed out so sam in the chat is asking why don't you need quotes for numbers so the reason for that is python uses syntax to work out what you're trying to tell it there are fundamentally different types of data that you deal with in programs there are words which as a human you interact with because you're trying to describe something to someone like saying hello and when python is reading this it will see the string and it will say okay i know that you're not this stuff inside the quotes isn't special in any way it's just something that a human would understand and i don't need to worry about what's inside those quotes if it sees just as it's reading along the line you know one letter one letter one letter and it gets to here and it sees a number that's not inside quotes it's going to treat it as that particular mathematical number and it's important because something we're about to see in a moment that mathematical numbers in python you can do maths to you can do three times two or three divided by four and that's going to give you another number whereas something like hello it doesn't make sense to divide hello in quotes by three because there's no description of how you divide a string by a number and so by being explicit from the beginning about what kind of data you're dealing with it lets python know what kind of operations can be done to it thing to be aware of is that the string three is different to the number three this one up here python will just see as some words which humans understand and it doesn't have to care about this second one here it will see as the mathematical number three which it can do something with so anytime you're dealing with numbers in python generally you won't put any quotes around it because you want the mathematical concept to be the thing that you're talking about that said it is sometimes um useful to be able to talk about the types of data you're dealing with in a more kind of abstract sense so in that example there we just had a print function and we were just throwing the thing that we wanted to be printed straight inside those double brackets and it was reading whatever we passed in and it was printing out to the screen it's quite common to want to give names to your data so you can refer to it multiple times or to uh to work as a an age to the reader so they understand what's going on so for example 1.3.14 that's good enough here we have a python script which doesn't do anything it's simply a number so this here is a piece of data we can give data names by assigning what's called a variable and the way that you assign a variable to a piece of data or assign a name to a piece of data is by writing the name of the variable you want to create followed by an equal sign followed by the piece of data so this is going to make a new name pi which contains the value 3.14 this means that later on our script we can write print and instead of writing 3.14 sorry 3.14 we can go ahead and replace the number literal that we would have put there with the variable name pi and so when print pi gets read by the python interpreter it's going to say okay here's some letters they're not numbers they're not in quotes so this must be a variable name it's going to look up what that variable name is it's going to see that it's this value here and so 3.14 is what is going to be printed so if i save this file and run the script we see that it prints out 3.14 from the outside it's looking basically exactly the same but inside we've broken up into two parts the parts dealing with our data and the part dealing with our output so take a minute or two now to have a go at that yourself take any print function you want to do anything that you'd want to print but break it up over two lines one line which takes that thing that you want to print and gives it a name and then another line which has a print function which accepts that name as an argument and check that it prints the correct thing to the screen be aware that variable names can't have spaces in they can't start with a number as a few other constraints which you might find as you go along i've introduced variables available is a way of giving a name to data and so in your head it's worth having a sense of the difference between a piece of data like 3.14 and a variable like pi they can often be used in the same context but they're fundamentally different elements of the language so it's worth kind of working out in your head as we go through the exercise thinking about how data and variable names differ from each other we've already seen a few ways for example a string has quotes around it and a variable name doesn't so they look different but they also serve slightly different roles in the language so hopefully as we go through you'll start seeing how that works so i'm going to delete all this text and start with something different so let's make a variable called distance in miles and you'll see here i've used underscores between the words there's a convention in python to write your variable names as lowercase to use full words in your variable names and anywhere where there would be a space to use an underscore other languages have other conventions but this is the general convention in python and it's the one i'll be following and let's give it a value 30. so here we just have a number 30 which has no context or no information about it because we've got context here in the name of the variable that says distance in miles that tells us what this 30 actually represents and so it's useful when writing python scripts to give your variables descriptive names because it will help you understand what the numbers actually mean if i want to make another variable called distance in kilometers i could go ahead and just assign this a a value now i don't even know what that would be but it would be something like 50 or so but of course that is uh not using the computer to my advantage i could google it find out what 30 miles in kilometers and write the value in here but a more flexible and reusable approach is to work out what the conversion factor is and do that conversion ourselves in our code so we can change one variable and always have the other one automatically updated and so when assigning a variable on the right hand side you can do more than just write a piece of data you can also refer to other variables for example distance in miles and we can multiply that by a number and i'm going to copy the number from down here to avoid errors so here we made a new variable and the value of this second variable is based on another one so then when we print distance in kilometers and then i save this file and clear it and run it we see printed out it's 48.2 it's about 50 but it's a much more precise answer this means that we can come back to our original script we can change this to be um 5 000 miles and we can find out what 5 000 miles is in kilometers just by changing one number we run in the script and we find out that it's about 8 000 kilometers so this is starting to show the value of using variables rather than hard coding your numbers it's also showing you how you can use variables which are based on other variables as well as multiplication there are other mathematical things that we can do so i'm just going to copy and paste this one from my notes down here no one wants to sit here watching me type all day and so here we have a variable which is a float floating point number a decimal place number on the right-hand side we'll make a new number which is that plus something and then we're printing that out in python there are four primary mathematical operations you can do plus while using the plus sign minus uses the minus sign as you would expect for multiplication it uses a star and for division you use a slash so those are the four primary different types of operations that you can do to mathematical numbers in python going back to mendy's question earlier about why some things are been quotes and why some aren't i'm going to show you an example now of how python sees the differences between those two types of data in the program so i'm going to make a variable here and i'm just going to call it my underscore number and i'm going to set it to 67 and i'm then going to say my other number equals my so we might underscore number plus 100 and then i'm going to print my other number so we run this script we get the kilometer thing and the celsius thing from stout and we get 167 printed so it's successfully printing out the thing that we had if however we put this 67 in quotes python is no longer seeing my number as being an integer it's no longer seeing it as being a mathematical object it's seeing it as being a text type object and it doesn't know how to do mathematical things to text objects so if we now run our script we will see the first of our python errors it is unable to do the addition operation because it's no longer doing it to two numbers it's trying to add a bunch of letters and a number together and it's going to say that it's assuming that you made a mistake it's not going to just implicitly convert something it's going to make sure that you're going to be explicit about what you're doing as we look at the error over here the way that i read python errors is by starting on the last line it says type error so that's telling it's an error to do with data types where types are things like strings or floats or integers the error message itself is saying it can only concatenate strings and strings remember this is a string which means that my number is a string which means that we're doing my number which is a string plus an integer and so it's interpreting this as us trying to take one set of letters and stick another set of letters on the end and that's what concatenating string to string would mean concatenate means stick next to each other however it's finding an integer and so it's saying i don't know how to do that and so i'm going to bail out and stop and when you get an error like this the program will stop running it will turn you back to the terminal and it will print an error like this on the screen so you can work out what's going on so we start with the last line because sometimes that's enough to let you know exactly what you did wrong and to let you know what's going on if you've got a long program where your errors can crop up in all sorts of different places it's worth looking at the lines above which tell us where the error came from this line here tells us which file we were on which line we were on we see it's on line 14. this is why programmers always have the line numbers turned on in their text editors and then this line here in the output is the exact line that we had in our text file to give us context to let our brain jump back to the error a little bit more quickly so with that i'm going to ask you to have a go now at the exercise at the bottom of the data types chapter so there's an exercise there to edit scripts.py so that it's made up of adding two strings together so originally it said hello python in fact i'll show you how how it looked beforehand it had something like my words equals hello python and then it was print my words it might have had a different variable name but that is not important so have a go at taking this little program here but breaking up this line here into two separate variables one which has the word hello in one which had the word python make a third variable which is those two things added together and then print that out and make sure it looks how you expect once you've done that carry on to the end of that data types chapter there's a section there on printing multiple things so have a go at that reading through that and having a go that exercise as well mendy there has a question about is there a difference between my underscore words and my words with or without the underscore so the first thing i would say to that question mandy is the best way to find things out is to try them for yourself you often discover something really interesting if you have a go with something and it works or doesn't work how you expect that said i will go that through that with everyone because i think it's an interesting thing to look at so here we made a variable called my underscore words if i delete that underscore and put a space and put a space here and then we try and run this script i'm going to clear this and run the script we get a syntax error and the reason we get a syntax error on that first line is because python starts reading at the beginning of the line it sees the word my and that's a variable name by itself so the only thing that's able to follow a variable name by itself at the beginning of a line is an equal sign it only makes sense if you're immediately following it to assign this variable a value it sees the space as a break between two different variables it's expecting there to be an equal sign next and not a space and some words so this is telling us the fact that we get a syntax error here that my space words doesn't get seen by python as a single thing it gets seen as two separate things a thing called my and a thing called words and because i don't make sense next to each other it's not understanding what you're trying to say here so we need to put an underscore in sorry in underscore if we try that again we'll probably still get an error we do but this time the error is on this line here because the same thing's happening as an argument to the print function it's expecting a single variable name here we're giving it two variable names separated by a space it doesn't know what that means and so it's going to give us a syntax error so we need to have underscores we could do it without any underscores at all and no space that worked perfectly fine as well the underscores are just there for visual clarity okay so i'm going to show the answer to that exercise you were just working on hopefully have a chance now to uh have a go with it so as i explained to mindy underscores are used for visual clarity and you can't use spaces in place of them so we have our string here we are going to change it into word one which is going to be hello it's going to be word two which is going to be python and then my words is now word one plus word two we save and run this and we see hello python printed the screen the last section on that page was taking this example and making your life easier you'll notice that you had to put a space here because when you add strings together it doesn't automatically put a space in for you if we get rid of that space and run our script we get hello python for one word which is not usually what we want so what we can do with the print function is get rid of this line entirely and simply print word one comma word two the comma here is telling the print function that we're giving it two separate variable names it will take one it will print it then it will do a space automatically and then it will print the second argument so if we save and run this we get hello python back again with that i'm going to move on to the next section at the bottom of that page there if we press next and we go down to the bottom and press next we end up on a chapter called lists so this is when we start getting a little bit more interesting with things so so far we've been dealing with single pieces of information a single number or a single set of words a single string but quite often in real life and therefore quite often in programming languages you want to deal with something that contains multiple objects the example i always come back to that i think most well represents real life is a shopping list you go to the shops and you have a list of things written down on a piece of paper you have one shopping list but inside that list you have a set or a list of items that you want to buy you can think of each item as a string but we need in our language some way to describe the list as a whole the way we do that in python is with a thing called a list a list is a built-in type in python and it's what's called a container it contains other pieces of data so i'm going to switch back over to my editor and i'm going to make a new file so i'm going to go to file or file i'm going to click on the launcher and put it here and make another text file so i've still got my old file here with my new file here i am going to rename it to list dot py so it's all list py at the top and then we can go ahead and close script dot py we don't need that one anymore so i'm going to go ahead and make one of these containers like anything else we start by giving it a name you almost always want to give your variables names and this is a really good way of making sure that you understand the kind of thing that's inside it same as before we follow with an equal sign because we want to give it some data in the past we would just write a number here or a string or do some kind of numerical work on some other data type to make a list we use square brackets and i'll point this out at this point that in python there are lots of different types of brackets used in different situations it really matters which type of bracket you use so far we've used round brackets for calling functions and here we're going to use square brackets to create a list in between those square brackets we put the things we want in our list for example cat that's a string we follow that item with a comma notice the commas outside of the quotes dog and uh let's put a number in as well 261. so you see here we've got one list one object that has three pieces of pieces of data inside it penguin yeah let's do a penguin as well thank you gareth penguin you can have as many items as you want in the list it's not limited to just three or four you can have lists with thousands of items if you so wish you can also have lists with no items in if we write it like that that's a list with no items in when we print our list we can print it like any other variable my underscore list what's going on there there we go and we run it with python list dot py because our script is now called list dot py run that and you see it's printed out the things that we put inside our list we've got the square brackets printed out on the output to remind us this is a list we're printing it's got commas separating each item and it's got quotes showing that these are strings you'll notice that when you print it out it uses single quotes whereas when we made it it had double quotes that's because in python single quotes and double quotes are almost interchangeable with each other there was a subtle difference between the two but largely they mean the same thing the convention is usually to use double quotes when making strings however regardless of what you make your string with when it prints out it will print with single quotes due to her and older conventions you know what conventions are like they change every week so have a go at that yourself make a list with some items inside it typing some strings in there some numbers some floats all sorts of different things and make sure that you can print it out and it shows up on the screen correctly okay so hopefully you've managed to print a list out and that's all working for you so we're going to move on and do something a bit more useful with a list than just making a block of a list and then taking that whole list and showing it to you on the screen with a shopping list you don't just deal with a shopping list on the whole you deal with the items inside the shopping list and so we need a way in our language to get access to the items inside so we're going to make a new variable called my element element is jargon generally for an item inside a list though the jargon isn't very consistent and you'll see it change later but nonetheless it's often used for that to get an item from inside a list we write the name of the list and we follow its square brackets these square brackets that immediately follow the name of a variable are different conceptually to the square brackets we used when we made the list if you've got square brackets by themselves with a space beforehand that's you making a new list if you've got square brackets and they immediately follow a variable name which is a list that's you accessing things from the list is you asking a question of the list we can put something inside those square brackets kind of like a function call to give tell it what piece of information we want so we can write for example number one and we want to not print the whole list we want to just print that one element so audience participation time for those who are listening who wants to let me know in the chat what's going to get printed out when i run this script and don't be shy okay we've got some cats and some dogs so those who are saying cat might be confused about how it could possibly be dog let's run it and find out what actually happens we do indeed get dog the reason for that is that in python everything starts counting from zero when you're talking about elements in a list the first element is actually the zeroth element this is item zero this is item one this is item two and this is item three everything in python starts counting from zero and it is very consistent with that and so it's a thing that's worth remembering if we change this to my list zero at this point we should indeed get cat printed out by that logic if we want to get penguin for gareth there if we do it with number three we should get penguin printed out next pop quiz what happens if i put in seven what's going to get printed out here any guesses great everyone's telling me that it's going to come together i expect by this point many of you are used to seeing errors because python does throw them at you quite readily if it thinks you've done anything even slightly wrong over time you will learn to appreciate and love python exceptions and errors but when you're learning it can feel like you're being told off the thing to remember is that it's just trying to tell you that it doesn't understand what you're telling it and that it's thinking you might have made a mistake and it's trying to give you the information you need to fix it let's run this and find out we do indeed get an error like before we start reading the bottom line it is an index error it's called an index because this operation with square brackets is sometimes called indexing the actual text of the message here list index out of range is telling us that the list my list the index we're asking for seven is outside of the range of valid numbers we only accept numbers from zero to three so seven is way out five is right out and so it's going to give us an error there's no valid answer we can give here notice that because it failed on line three which is here the program stopped running it didn't print an error and carry on it stopped entirely it never even tried to print my element as soon as you get an error raised the program just stops at that point and tells you what it knows about its state at that moment so we know that seven doesn't work but three does so so far we've dealt with zero one two three they've all worked we've tried bigger numbers four five six and seven those don't work what do you think happens if i put in negative numbers minus one who wants to have a guess about what minus one is going to do costas thinks penguin and you're not sure mercado is guessing the same thing let's find out let's run it we do indeed get penguin we get the last element so the design designers of python realized that all the positive numbers including zero if you count zero as a positive number are potentially valid inputs depending on how big the list is but you would never have any meaningful meaning applied to a negative index it just doesn't make sense because we are counting we aren't dealing with uh mathematical uh comparisons and so on so they realize that because all the negative negative numbers are free to be used and they don't have any uh ambiguous meaning you can use negative numbers for whatever you want and they decided that minus one should mean the last item and in fact beyond that negative numbers just count backwards along the list so minus two would give us two six one minus three will give us dog and minus four will give us cat if we try and go to minus five we'll see that once again we get an error i'm just gonna clear my screen up so that it doesn't disappear off the bottom -5 gives us an error because indeed there's no negative things over here it doesn't just loop around forever it's bounded by the size of the array the last thing i want to show you on this section which comes to rachel's question and i'll come to your question in a moment rachel once i've covered the basics of slicing and that is so far with indexing we have simply selected individual numbers we've asked for this element or this element or another element or we've asked for something which doesn't exist by going off the beginning of the list or off the end of the list it's quite common with lists or with any kind of container which has things in an order to want to grab a selection of items so let's say we wanted to select items dog and 261 we can do that by using what's called a slice it uses the same kind of syntax as we have here so we still use the square brackets but inside the square brackets we tell it where we want to start selecting from and where we want to end selecting from we want to select from here to here so dog is item two and we want to go as far as but not including penguin i don't know sorry dog is it one see even i get mistakes and we want to go as far as but not including penguin which is item three the way i like to do the numbers here because 103 doesn't intuitively say to me those two items is that i count the commas in the list as a way to remember what numbers i should put here i imagine there's a zeroth comma here because zero is invisible so there's no comma so this is comma one this is comma two and this is comma three so if i want to select from this comma to that comma i do one three and that's going to give me this slice of the list so when i print this i get dog and 261 is this subset of the list now coming to rachel's question she asked is there a way to get say the last three elements because if you do minus three minus one it'll print the last three and two elements but not the last element okay so let's work up towards that question so here we've got one and minus three if you remember earlier on number three refer to penguin but also number minus one referred to penguin so we can replace that three with minus one and say we want to go from comma one to comma minus one a little bit confusing i know but the more you do it the more you'll get used to it so this is going to give us that same set of numbers likewise this is comma minus one minus two and minus three we could change this to be minus three minus one and we print this and we get the same bit of information now rachel was asking how can we get it to include penguin as well if we change this to a zero that's not going to work it's going to try and go from here to here there's nothing to represent this comma at the end it's going to try and go backwards and in fact if we try and do this it will give us an empty list because we've tied to index backwards and so it's just going to fail if we want to go right to the end of the list there's a special shorthand and that is you just leave that space there blank you say go from -3 to infinity effectively and so when we run this it's going to do dog 261 and penguin likewise if you do a blank at the beginning it represents the beginning of the list so just a colon by itself inside square brackets means right from the beginning until right at the end so have a go at these exercises on this page here the lists chapter so get through as far as the slicing section if you can the last thing i wanted to cover in this list section is how you can go about changing lists so far the lists we've made have been static objects they haven't had anything changing in them as we've been using them but of course lists are most useful if you can use them to collect information as you're going through your program collect information in them and then do something with that information at the end so let's change our list here so it just contains animals let's delete this my element thing because we're not going to be grabbing something out of it and let's print my list this is now just going to print cat dog penguin let's pretend that what we're doing here is making a a list of the animals that we have on our very strange farm it's got cats and dogs and penguins maybe it's our pets maybe we've got a pet penguin maybe we're that lucky in order to add something to a list we write the name of the variable and follow it by a dot after the dot we write append append is a function and so it takes brackets to call the function just like print does inside those brackets we pass an argument which is the next thing that we want to add onto the list someone give me an example of an animal to add in here that there we go wonderful so when we run this we will see we get cat dog penguin and that all our pets so what's going on here is we are referring to our list this is the object this is our data container that we have by writing dot append after it we are calling a special kind of function which is often called a method that's just a bit of a technical jargon but they are relatively interchangeable terms method or function on our object so the fact that it's a dot followed by the name of the function says that this function is going to be affecting this piece of data and depending on what the append function does it is going to take whatever's passed in and do that thing to my list in this case the append function takes whatever is at the end and call and uh takes whatever is passed in and adds it to the end of the list emma is asking how do you add wrath to be the second in my list so there's two ways of interpreting that question and i'll try and cover both of them one is that maybe you are asking how you can overwrite the second item so maybe you want to take what would be dog second being number one remember and you can assign that to be rat this is going to take this list then it's going to find my list one which is dog it's going to overwrite it with rat if we run that we see that dog has become that if however you want to insert it into the list this is where i can't remember everything i believe there is an insert function let me just let me just google it because that is how we learn python insert list now it sends the python 2 documentation we want the python 3 documentation here we have the list.insert function it takes two arguments the first argument is the position and the second argument is the item i can never remember which way round they go so we want to insert at position one and we want to insert that so when we run this we see cat rat dog penguin hopefully one of those two things is what you are asking about emma so if everyone's okay with lists we can loop over them we can grab items out we can change things to do with them by appending to the end and as we saw there overwriting or inserting so after the list section we are one loops so hopefully as we go through this olivia i will explain the answer to your question and i'll just read it out now so that i remember what it said at the beginning of the loop section you assigned two variables called word with different strings so does the variable store all the data that you assigned to it okay so i will i'll go through that now i didn't realize how how soon it was so let's make a new file and let's call it a loop.py new file file rename loop dot py so the example i start with is writing something like we've got word hello we're coming back to our examples from the beginning here and then we print word we run that print hello of course it does we've done that several times so far i think we're all comfortable with printing and variables now if we want to print two things we could do word equals python and then print word when we run this it runs line one to sign the variable one's line three to print out hello then lines line five to assign a variable and then it runs line seven to print hello olivia's question there was that we have assigned the verbal word with the data hello and then a few lines later we've assigned the same variable name with a different piece of information what happens at this point in python is that python will forget anything about the history of the available word it will no longer know anything about the data hello it will just know that word equals python anything to do with hello from this point on in the program will be lost it has no memory it just remembers the most recent thing that you've done to it the reason we get both hello and python printed out is because we printed this one before we assigned the variable this is one of the ways that variables in programming and variables in maths are different from each other in maths if you say something like a variable equals a number you're stating some kind of mathematical fact that is then going to be always true for the rest of your analysis that you're doing and so saying that word equals hello and then word equals python doesn't make sense because it can't a variable in maths can't have two different pieces of data associated with it in programming however because we're going through sequentially it does one and then it overwrites it with a second the second thing that i hope you're thinking about this little bit of code here is that it looks very repetitive we've basically written two lines of code and then written exactly the same two lines of code with one tiny little change every time we see something being repeated like this in a program we should always think there's got to be a better way programmers are lazy the best programmers are very lazy because they make the computer do the work for them so let's get rid of this let's make this available words and let's make this into a list so here instead of two variables and printing one at a time we've got one piece of one variable which is a list which contains two pieces of data and we're currently printing the variable word which previously was referring to one word and then the other but what we have here is a container a list what we conceptually want to do to this container is for each item in that list we want to print that item to the screen when you want to do something to each item in a list we use a a thing in python called a loop or in this case it's a called a for loop because we are doing something for each item and so we introduce that using the word for you'll see immediately it goes green and bold in our editor here so we know that it's understanding what we're writing i'm going to write it out and then i'm going to explain what these different parts mean so the way i like to think about the structure of for loop is first we just look at this line up here this is the part that introduces the fact that we are going to be repeating ourselves there's five different things in this line one two three four and then the codon at the end number five in every for loop you write the four the in and the colon are always there always in that order and always spelt exactly like that those are the fixed scaffolding that we use to construct our repeating for loop the places that we have flexibility and artistic license inside our for loop are in the other two locations here and here i'm going to start with the second of those between the in and the colon this is where we write the thing that we want to loop over it is the thing that is the list that contains items that we want to do something to each item of we want to do something to each item inside words and so we are doing something for something in words the thing we're looking over goes in this spot here while we are repeating ourself while we are doing something to each item in this list we need to give a name to the current item we need some way to refer to it earlier on we were referring to items inside a list using square brackets and numbers but because here we're just going to be looping over the list we don't care what number we're at we are just going to be dealing with one item at a time as we're looping over the list and so to make that work we have to give each item as we come to it a name we're only ever going to have one item in our hands at any one time so only you have to give one variable name and the variable name we're going to use is word it doesn't matter what you write here you can give this variable any name you like like before we had variables called my list and distance in miles in the same way this is just a variable name we are creating so the first time around the loop this variable name word is going to be pointing at the first item in the list then the next time it loops around this variable word is going to be referring to this item in the list so the first time around is going to print word so it's going to print hello is then going to repeat itself and it's going to print python let's check that that works there we go it prints out exactly the same thing as it did before but what was previously four lines of code is now only three lines of code now that might seem like a small saving but bear in mind that we could be looping over a list that had a thousand items in in which case this would still only be three lines of code whereas if we were having to print each item individually it would be thousands of lines of code and that is a lot of writing the final thing i want to point out about lists about loops sorry is that the codon at the end of the line in python always designates that the next section of code should be indented and that is why we have these spaces at the beginning of the line here python uses the fact that this is indented to know that these are the lines we want to repeat if we write print dot dot dot no in quotes there we go it is going to repeat it's going to print this line and then this line then repeat itself and do those two lines again so if we run this we see we get dot dot hello dot dot dot python likewise we can print end and it'll do dot dot hello end dot dot dot python end i'll just clear that if we don't want this end to be repeated all we have to do is unindent it you do by pressing backspace because this is no longer indented this line of code will not be repeated so we run this we get dot dot dot hello and then dot dot python then because the loop's finished because words has run out of words it then exits the loop unindents and then runs this line of code just once so have a go at the exercises on the loops chapter do the first exercise on there first and then if you've got time move on to the exercise at the bottom of the page don't go quite as far as enumerating um because i'll be covering that in just a moment there's a question there from sam will you zero and one in last exercise why can't you do that again so you're saying why can't we do [Music] print uh word that's it wait words zero i'm gonna delete those and print words one there's no reason why you can't do that and if we look we'll see that those two things come out exactly the same but if we have hi it's matt here then we'd have to do 0 1 do a 2 and then a 3 and then a 4. that's probably not even the right number hi it's matt and i did miscounted so they have to do a 5. and so then it's done the right thing hi it's matt here but we've had to copy and paste we have to repeat ourselves it's very easy to accidentally do that and not notice that you've got a number repeated also if you've got lots of items you have to do lots of lines of code this will always just be those two lines of code regardless of how many items you have in the list it's quite common with the lists that as you're collecting your data you don't know whether you have 99 or 101 items you just know that you have a container with information in and you want to do something to each of those numbers maybe you want to double them maybe you want to print them maybe you want to email everyone that's in that list because of the list of email addresses you don't care about where each item is you just care there's a whole bunch in there so yes it is it certainly is possible but looping gives us the flexibility that we wouldn't otherwise have canal asks what if we want to print each item twice let's have a look i'm going to simplify this back to where we were so here we do this and it prints each item once if we want to print it twice we can just write print word twice beside it if you want to do it three times well we could copy and paste it again but as you can see we're probably starting to get the point where we're repeating ourselves quite a lot so as gawa says in the chat you can actually make a loop inside your loop you could do another four repeat in and the way that you repeat things a certain number of times is by using the range function range three pre int word and here when we run this we get it happening three times and three times so then if you want to do it 100 times we could just change that to 100 and it would print each word 100 times yes so range is a special word or rather it's a built-in function in the same way that print is a built-in function range is a built-in function the job of the print function is to take the argument and display it on the screen the job of the range function is to take the argument and to give us back a list containing that many numbers one odd i'm glad you asked because that's exactly the next little section which i'm going to cover now and then we're going to have a little break for a bit because we've been at this for nearly an hour and a half so let's have a look at how to answer ronald's question so they're asking what's a concise way for accessing the current index slash iteration inside the loop so here we are looping over forward in words the thing that we're looping over is this list and when you loop over a list the thing you get given each time is each item in order if you want to get each item and its position there is another function called enumerate which means give numbers to which when we run it we get back the index and the item it gets given this back to us in this form with the brackets and the commas and stuff which is useful but it means that we can't access those two pieces of information separately from each other so if we want to access the two things individually we can take the fact that enumerator is giving us back a pair of things each time and so instead of having one variable name here we can have two variables separated by commas where i is going to be assigned the first thing that's given back zero the first time and word is going to be assigned the second thing that's given back hello so then now if we print um item i is word if when item zero is hello item one is python canals question so that's a question about documentation how do i find suitable function for my tasks there can be 100 built-in functions which i don't know about so let's go to docs.python.org exactly as girl is linked there so the page that i was on before was a page called built in functions so you can go to the search built in functions and then you can never find them so what happens here is i go to google and i search for python built-ins which is almost certainly what gareth did and there's a link here to built-in functions and we have here a list of all the built-in functions which include print and enumerate and range and that gives you information about what's going on there in general the python documentation will have some have information about every single thing you can do with python the language it's not always easy to navigate through here so on the whole the way i get there is by googling for python followed by the thing i want to search for like python insert into list is what i searched for earlier that will take me to the python documentation i end up in the right place the thing to make sure you're doing on the python documentation page is the link there's a top there's a drop down menu with conversions make sure you're not on the python 2.7 page you'll be able to recognize it because the 2.7 documentation is all blue and it's got a big banner at the top so in that situation click on the link here and click on the latest number up at the top and that will take you to the current versions documentation ronald's asking a question do you take a bigger performance hit if enumerating a long list versus just adding a counter inside the for loop so no the way that enumerate works is that it only generates the indices as it needs them it doesn't generate a big list in advance it just keeps track along the way so it's doing that counter inside the for loop thing for you automatically which means you don't need to worry about um setting to zero before the loop and anything like that it just does all that stuff automatically so there's no performance hit at all it does it all dynamically on the fly that said if you're worrying about performance with a really really long python list you're probably at the point where you want to start looking at more advanced python tools so there's a set of tools called numerical python sometimes it'll be related to numpy or numpy that will have the tools in there for doing that kind of stuff nice and efficiently and also in our python introduction to data analysis course which gary was just asking about where you go through a package called pandas which provides tools for doing this stuff nice and efficiently as well so once you're at the point of worrying about performance it's worth looking outside the built-in python lists at what other tools are out there ronald your question is it possible to have local dummy variables inside a loop yes it absolutely is so i'll just demonstrate that quickly here let's make a variable called my number equals 42 and then let's just print my number when we run that we see it prints the thing out and it prints out the variable as well so you can introduce variables inside the loop and then they just sort of live inside that scope so before the break we were covering loops that's how we perform the same operation multiple times so to look at the example we have up here here we were doing this line of code which we've only written once but it's going to be performed multiple times so you write a line of code once and it runs many times the other kind of thing you want to do to your code sometimes is to write a line of code which may or may not be run at all depending on the context and the situation around it the way we do that in python is by using what's called a conditional so let's make a new file and let's call it if dot py because that is the question that we are asking if let's make a variable that's how we start almost all of our scripts number equals 128. nice nice computery number the way we ask questions of our data in python is by using the if keyword this works similarly to the for loop keyword that we had before and you'll see it's done the same thing where it's gone the green and it's bolded up as well after the if we are going to write something here followed by a colon similar to the for loop where we had our scaffolding of the four here we have the word if and we have a colon at the end of the line the question that we ask can be a whole bunch of different things so i'm going to give an example and i'm going to let you have a go at running that yourself and then i'm going to be building up bit by bit one of the simplest questions you can ask is to compare the size of numbers so let's say is my number greater than 100 so this bit here is whatever we want it to be as long as it's giving back something which is true or false this means greater than and if you have something on the left and the right there are numbers it will compare them and give us back true or false depending on whether they are bigger or not if this thing returns true if my number is bigger than 100 then it will run whatever code is indented in the block below which in this case is going to be our trusty print statement my number is large so if my number is bigger than 100 it will run this line if my number is smaller than 100 it won't run that line at all oh no that's not what i meant to do that's what i'm going to do python if dot py 128 is large copy that code into your own script into a file called py do the same thing i just did there and make sure it prints out the same output then try changing my number to be different things negative numbers numbers that are near to 100 numbers that are small numbers are much bigger than 100. see what you get have a little play and make sure you understand what answers it's giving you back so i had to go here and i set the my number to be 999 and likewise printed 999 is large if you make this smaller than 100 like 50 and we run it nothing gets printed out it just does nothing at all so if we wanted to print out something if our number is less than 100 we need to use a different operation instead of saying is it greater than 100 let's change this to be less than and let's say is it small so now when we run this it says 50 is small as well as i'm just going to do some stuff down here so greater than means uh greater than this means less than greater than time followed by an equal sign means greater than or equal hopefully you can guess that that means greater than or no sorry doesn't mean that's all it means less than or equal if you want to check whether two numbers are exactly the same as each other you use two equal signs next to each other if you use a single equal sign or give an error if you want to compare numbers you use two equal signs are they equal and if you want to check that they are not equal you can use exclamation mark equal sign are not equal are they they speak grammatical so have a go now at tweaking your if statement to use a different sign update the little comment so that it does something that we print something that makes sense just have a little play with a few different uh operators as they're called there and see and check that they make sense to you and then we'll be moving on to the next little section if everyone's happy with that everyone's had a chance to play around with different signs and check that it all works for them then i'll move on to the next little section i'm gonna delete that because it's not valid python code and it'll give us a big old syntax error if we try and run it it was just a comment so a question you might have thought of is i wanted to do one thing when it's greater than 100 and we say it's large and then if we wanted to print something when it's smaller than 100 we had to delete the code and change it and make it say something else what we'd usually want to do is to do one line of code if this condition passes or a separate line of code only in the situation where that line of code doesn't pass and we can do that using a statement called an else an else statement in python always has to follow an if by that i mean you can have an if statement with no else but you can't have an else with no if so you just write else and a colon and here we write print my number if i could spell is small and so now we can run if dot py it says 50 is small we can say 990 990 is large if we do 100 exactly it tells us that 100 is small because 100 is not greater than 100 it is equal to 100. okay so olivia has a question about the indentation here so the indentation here gets interesting um there is a consistency across python with how indentation is treated so it's worth taking some time to understand what's going on the way the indentation gets applied is any line that's indented is always associated with a colon before and that colon and the indentation associates this block of code with this statement here so we could print hello as well so these two lines of code are indented so they're associated with the if passing this block of code is indented after following this colon and so it's associated with this else the if and the else are the same indentation level because the else kind of sits next to the if it's not inside it and as i introduce the next statement here you'll hopefully see why we don't keep on indenting because you'll see that things get a bit messy because we might want to here do one thing if it's greater than 100 another thing if it's less than 100 but a third thing if it's exactly equal to 100. and for that there's a thing called an else if so here we say if things this thing's true do this thing otherwise if this thing's true which let's make this my number is equal to 100 print my number is 100 and if that one's not true either then it finally goes to the else the reason you want to do that and let's just check that works here we say oh we've got a syntax error ah of course that's for demonstration purposes in python you don't write else if you write alef it's an abbreviation they introduced for whatever reason here where it says 100 is 100 because it's taken this number my number it's not larger than 100 it is equal to 100 and so it's printed this and then it stops checking when you're going through a chain of ifs airlifts and elses it will check from top to bottom the first one that matches will run that block of code and then it will drop out and not check anymore and the reason you don't want to keep on indenting these things is because you can have as many elifs as you want you can obviously have zero because we saw that already but you could also have second one which checks if it's 42 so 100 is 100. 42 is 42 and 13 is small if each of these times we had to indent our code we would end up disappearing off the right hand side of the page by the time we checked all our different conditions and that is why we don't have to indent each block as we get to it so for the exercise here i want you to take this block of code or a similar block of code if you've got different elses and elses and fs and put it all inside a loop remember before i show that you can nest loops inside of other loops you can also nest uh if statements inside of loops inside of loops and vice versa so take these lines of code nest them inside a loop and loop over a list of numbers and check each of those numbers in turn and print out whether they're large or small or whatever else you want to check up on i'm going to do it alongside you all on the screen here so that you can see um how it works and then i'll check back with you in a few minutes and then we'll be ready to move on to the next section so this is the exercise at the bottom of the conditionals chapter okay so we've got one person's finished now i'm going to go through the exercise answer with you now to explain um how it's done so we can move on to the next section but with any of this stuff i'll give you information at the end to do uh on how to get in contact with us afterwards but we've got a question quick question here from olivia asking about the difference between the numbers and range so in my example here i want to loop over the number 0 to 9 and so i've used the range function to give us access to them the other way we could have done that is by doing 0 1 2 3 4 5 6 7 9. both of those are going to give us basically exactly the same answer with the difference being that we've had to type whole lot more for doing it explicitly secondly if we wanted to loop over the numbers 0 to 10 000 it would grind our fingers to the bone having to type it all out whereas doing it using range we just do that and it's immediately going to give us access to them all so that's one of the reasons to use uh range instead but we start off with my numbers equals range 10 which we saw by 4 give us the numbers from 0 up to 10 but not including 10. so that gives the numbers 0 to 9. the if statements inside here are exactly the same type of thing as we were doing before is a number bigger than five is it less than five or is it neither of the two in which case well it must be equal to five mathematically the thing that we have done extra here is that we have indented all of this as you can see and we've done that because it is now inside a for loop we are now going to be repeating this set of if statements multiple times we are going to be doing it once for each of the items in my numbers which is the number zero to nine each time around that loop we are going to have the variable num so inside each loop the variable num is going to be referring to first of number zero then one then two then three etc etc and so it's that number which we are comparing in each of these situations and printing out so when we run this code we get first of all we get the first item from range 10 which is zero which goes into variable num which gets compared to this is not greater than five so it goes to this one it is less than five and so it prints out less than five it does the same thing for the rest of them until it gets to number five at which point it's not greater than it's not less than and so it does this block then it carries on through and then all of the rest of the numbers are well are matched by the first block there so this is to show that you can put if statements inside for loops and it all just kind of works how you expect in this block here you can or you can put some blank lines in here you can look at this block of code and even outside of the context of you knowing that you're inside a loop this section of code still makes sense in itself as long as you trust that there's a variable called num that you have access to it doesn't matter to these lines of code that are inside a loop they are just doing the same if else lf and else thing that they always do in our case however we do want to make it clear this inside a loop because that's how we know that we're looping over the numbers you'll use if statements a lot in your code i find it quite uncommon to use elif it said it's a corner case catching tool rather than everyday tool but certainly i use a lot of if statements i use a decent amount of else's and i always use loads of for loops in my code that is one of the most common things that i do in my code is looping over stuff and repeating myself for each item inside a list and can ask there are a lot of variations of if in python write yes there's ifs and lfs and else's and lots of different things you can put here it gets quite complex to allow you to explain to the computer all the different things that you might want to do so i'm going to make i'm going to move on to the next section now so after the loop section i'm behind on the notes on the screen here and after conditional section we move on to dictionaries i'm going to make a new text file again it's always good to make a new text file for each section of code that you're writing and i'm going to call this one dict dot py dict is the usual abbreviation for dictionaries in python and you'll see that cropping up a lot so get used to dict meaning dictionary and now i'm going to explain what a dictionary is so a dictionary is similar to a list lists were containers which held multiple pieces of data they held them in a certain order and we could access them either by looping over the list or by asking for specific items in the square brackets asking item three or zero or minus two a dictionary works in a similar way it's a container which holds multiple pieces of data but rather than holding them simply linearly all in line and giving each of them a number to refer to it we instead each give each item in our dictionary a name or a key which we use to access it you can think of it a bit like a physical dictionary where you have a bunch of words and their definitions you look up the definition of a word by looking by finding the word in there and then the definition is associated with it let's look at an example now to see how it looks in code so i'm going to make a dictionary which contains animal sounds continuing our animal theme from earlier so i'm going to make a dictionary called sounds and inside this i'm going to make a dictionary a dictionary is made using curly brackets now those are different to the square brackets and they're different to the round brackets curly brackets in python almost always mean i am making a dictionary in the same way that square brackets by themselves meant i am making a list so once again inside the curly brackets that's where we put our contents of our list each item in a dictionary is a pair of items in this case so we have here cat and meow those two things together separated by a colon are a single item in the dictionary because each item in the dictionary is a key and the value you can add multiple here for example dog and woof so that should be a codon so here we have one item which is this pair of things and another item which is this pair of things each item in a dictionary is made up of two parts the key which goes before the colon and the value which goes after it i tends to when i write dictionaries out spread them over multiple lines because i find it a little bit easier to read when you've got this many symbols hanging around it can get a bit messy to the eye so one thing you can do with dictionaries and in fact with many things in python is spread them over multiple lines so i'm going to put a new line in there a new line after that comma and a new line before that closing bracket so this is exactly the same thing it works in the same way but it makes the fact that this item and this item are set foot a bit clearer oh sorry so i'm going to ask in the chat i need more than two items for my list because two is a bit boring what other animals and sounds should i put in here we had penguin earlier on what sound does a penguin make because i don't know gareth apparently they go meep and we also had uh fox there we go thanks maddie now what what does the fox say screech that sounds about right had one in the garden the other day and it made a very horrible noise and we've got a duck duck going honk and we have a sheep and what noise does sheep make lots of a's there we go so once we've got our dictionary full of items each of these is an item we have a key and a value associated with each we get things out of the dictionary in the same way that we did with lists so we're going to make a new variable cat sound and we want to get from this dictionary the sound of a cat so we go to sounds we use square brackets but this time inside the square brackets we don't write the number of the position of where the thing we're looking for is we put in the square brackets the key of the item we're looking up for example penguin and it will give us back the associated value which in this case would be leap so if you put cat in there it's going to give us back hopefully yeah let's print it and check cat sound oh no wrong file python dict dot py invalid syntax i've missed a comma there very easy to do cat sound i made a typo tell it's getting late when i'm making any typos it prints out meow it's looked up the key we put in it looks up in here it's giving us meow if we change this to be just sound because we're going to be tweaking what animal this is change it to fox and print that it makes a screech noise so have a go of that yourself um copy in that dictionary try grabbing items out of it and also see what happens if you ask for an item from a dictionary that isn't in there for example if you make a typo here or you ask for an animal which isn't in there at all olivia asks do you have to have a comma after the last key in the dictionary no you don't that works just as well the reason i tend to put a comma after the last item when it's on when each is on its own line is because when i add a new item i only have to worry about this line here i don't have to remember add a new item like fish bubble and then oh i missed the comma there which is exactly what i did wrong earlier so i tend to always put a comma at the end of the line to avoid me making that mistake also when you come to use a version control like git later on it makes your uh differences between files a little bit neater to read if you have a comma at the end of each line but largely it's a matter of style i will sometimes use one or the other gary is asking why does the dictionary use square brackets to search the items not curly brackets so this is where it gets a bit confusing with python and the different kinds of brackets or at least it it does seem confusing at first but i'll try and explain the logic behind it so the reason that we use different types of brackets for creating the different types of objects is to make it unambiguous when python is reading the file what kind of object we're trying to create and that is why right from the start you have to choose when creating something whether it's going to be square brackets to be a list curly brackets to be a dictionary or there are other types of brackets which get used as well as for accessing stuff from lists it's not ambiguous what we're asking for with the square brackets here because each type of object can only be searched in one way so a if this were a list then python knows that square brackets is asking for it by number this is a dictionary here so python knows that square brackets is asking for an item from it by its key that allows python to always use square brackets to mean give me something out of this list and the syntax would always look the same so you can look at the line like this and you don't really care whether sounds is a list or a dictionary or some other exotic type of data all you care about the reader of the code is that we know this is something which must contain stuff because we're asking for something from inside it and we trust that the thing in this case sounds knows how to interpret it the other reason is because there are lots and lots of different kinds of containers beyond lists and dictionaries which have their own syntax here in more advanced python tools there are other tools which don't have their own individual way of creating with special brackets but they are accessible using square brackets and so by allowing square brackets to be the generic syntax for give me something from inside this thing it makes the language nice and extensible now i'm going to move on to the next little section like we did with lists we created our list statically we've created our dictionary statically it's fixed in the file with lists we could add items into it by using append and we also saw the insert function when i was answering someone's question dictionaries have a similar ability so i'm going to get rid of sounds fox if we want to add a new item into a dictionary we do it just by accessing it so can someone in the chat give me another animal and a sound tiger and what sound do tigers make girl okay cool that's scary so you do it by accessing the item as if you're trying to get it out but if it's not in there because we've given it a value on the right hand side it's going to insert that into it and that means we can now do sound equals sounds tiger and this will print out ronald is asking can you modify the name of the key without changing the value so we can change the the um contents of a value by doing the same thing we could have put for example uh duck there and maybe make this a scary duck but if you want to change a key it's a little bit trickier so you would have to for example let's say we've called this let's say we made a typo we called tigger by accident we would have to make a new one sounds tiger equals sounds tigger and then we'd have to delete sounds tigger so it's a two-step process so we put in a incorrect name there we've then renamed it and we can access it by the correct name and that should work so have a go that yourself try adding in a new entry into the dictionary make sure it makes sense when you're doing it try overwriting existing variables and i'll come back in a minute or so to move on to the next section i'm going to cover the last section on dictionaries here before we move on to the last chapter in the course today we're doing well for time i think and it's following the same kind of pattern to what we did with lists with lists we looked making them statically getting items out of them changing items inside them and then we moved on to looping over them and when we loop over a list we repeat each line inside the loop body once for each item in the list and it works in a very similar way with dictionaries so let's put tiger inside here because don't leave any animals behind so when we loop over a dictionary let's find out what happens the nice thing about python looping and for loops is they are very very generic tools you can loop over lots and lots of different types of things in python while the syntax you use basically stays the same it doesn't matter whether you're looping over a list or a string or a dictionary or as you'll see in the next section a file the words that you write on the page look very similar and it's a general way of repeating yourselves depending on the kind of a container that you want to loop over so here we want to some kind of thing in uh sounds and i'm saying thing because when we looked over the list it was unambiguous what we'd get back we had one item at a time here we've got two different things we've got a key and a value or potentially the whole thing all at once and let's pretend we don't yet know which of those we are going to get back and let's print thing and find out what thing is so when we've looped over this dictionary we've said we want to have whatever it is that looping over it gives us and python has decided that for us and it looks like looking at the output that when we loop over a dictionary we get back the keys from that dictionary cat dog penguin fox duck sheep fish tiger those are the keys in our dictionary it hasn't given us access to the values so we now know that this isn't a thing this is a key again the variable name doesn't matter but it's good to use one that's descriptive but better than that actually our key isn't just a key it's an animal and suddenly this loop here is starting to read a little bit more like a human would understand it for each animal in our sounds dictionary print out the name of the animal and that's what we get over here if we want to get access to the values there is a thing we can do where like with the append function to a list we do a dot values after the thing we're looping over and that's going to give us back each of the values so we now change this to sound and sound now it's giving us back each the values one at a time if you want to explicitly loop over the keys from the dictionary like we had before we can do that too by writing keys there and changing this back to animal so if you have sounds by itself it gives you back the keys if you do keys explicitly it gives you back exactly the same thing sometimes it's good to do it this way because it reminds you it's giving you back the keys the final thing that you sometimes want to do with dictionaries is not get back a list of just the keys or just the values sometimes you want to get back both of the things at once as a pair because as you can see in the output over here the keys and the values have been disassociated from each other there's no way to know which sound and which animal are related to each other in order to get them coming out in pairs there is a third thing that we can pass in here called items remember i said earlier that an item in a dictionary is this pair here or this pair here or this pair here that terminology carries across to this function here so when we ask for the items it's going to give us back cat meow etc etc so let's have a look and let's call this thing again and when we print this we see it gives us back these pairs now these pairs work in a very similar way when in fact exactly the same way as we saw with enumerate earlier with enumerate we solved the problem of it printing out it with the brackets and the quotes by putting two variable names in here and we can do the same thing with items so we can write for example for animal because the first thing we're getting back here is the animal comma sound and then our print statement we can say animal goes sound and when we run this it white's cat goes meow dog goes worth etc etc this is a very common thing i do to be able to loop through a dictionary and get access to everything but each iteration of the loop i'm only being given access to one of the items so the key and the value at a time but that's really useful to allow me to go through and do something to each of the things inside the dictionary some questions in the chat there so rosalia's asking my list is printing in a different order is that an issue no it's not so until python three-point something 3.4 or 3.5 the order of a dictionary wasn't preserved so the order you wrote it here could easily end up being different to the order it's printed out at the end as of a recent version of python they've changed it so that it's always con preserving the order but in general it's good to not rely on the order of a dictionary treat it as a bag you throw a bunch of items into and each time you loop over it you stick your hand in and grab them in a random order assume that's the case even if they end up coming out in the order you put them in that's the safest way and amanda asks can i assign more than one value to a key you can so let's look at dog so here we've got dog making a rough noise if we wanted to assign a list of values to it we could say woof and bark and so now when we print it out we get dog goes with bar of course this isn't a very nice way of printing it so we might want to have an if statement in here which has something like if and near merle is equal to dog animal goes [Music] sound zero and sound one a little bit bigger so here we have assigned dog to have two values and so when looping over it we check if we're on dog if we are then we print both of the sounds otherwise we print the one sound and we look at that and we see dog goes woof and bark and the rest are just carrying on so here we're combining together dictionaries loops and if statements all together so have a go at running those things yourself looping over items keys and values check you're happy with it and then i'm going to be moving on to the final section in the course today and that is reading files just take a few minutes there and then i will be moving on salem yes the something going something is a as a strange english-ism which is a bit weird to translate so we could instead write animal makes this noise and then it says penguin makes this noise meep fox makes this noise screech if that's easier for you to read or something then uh go ahead and change it to that and that might make a a little bit easier to understand what's going on emma asks can you make the code detect that the dog has two sounds yes we can there's a few ways we can do it i'm gonna try and not use a way that's too confusing for new uh learners of python but we could for example do if um there's a thing you can do called is instance in python which checks what kind of data something is and we could say if is instance sound list let's see if this works it then does the correct thing for dog so this is saying if sound is an instance of the list type then do this otherwise just print it out on the screen that's one way to do it there is in general lots of ways to do things in most programming languages and part of the job is working out which one's right today the last thing i want to cover here in the dictionary section is right at the bottom of the dictionaries chapter there's a little list of things that you can loop over and i want to draw your attention to that because we're going to be building on this in the next section but to reiterate when you have a for loop the thing that you put in this section between the in and the colon can be a list or a string or a numerate function or we saw range earlier which isn't listed here or dictionaries or the keys of dictionaries or the values of dictionaries or the items and dictionaries all of those things are possible all with exactly the same syntax on this line here the only thing you change is what you're looping over python has a very extensible and generic way of looping over things so much so that we can loop over objects that aren't even part of the language itself for example files so the last section we're going to do today is this last chapter here files so i'm going to make a new file text file i'm going to call it maybe confusingly file.py and then here i'm going to also make a file i'm going to make a second file which i'm gonna move down here and i'm gonna call this data.txt and inside here i'm just gonna write some this is a file so i made two files here a python file and a text file and what we're going to do is write a program which can read this text file and display it on the screen so the way we start this is by writing open and then the name of the file that we want to open the file by default has to be in the directory that you're running your code from in this case it's data.txt in order to handle the file being automatically opened and closed we need to introduce a new thing called a it's called a context manager but they're sometimes called with statements and that's because it starts with with and then has as f and what this is saying is open this file make a new variable called f which is going to be referring to this file it's our handle which we're going to use to you know hold on to to refer to this file and the with part is saying because we've got the code on here everything inside the next block is dealing with it while the file is open and as soon as we finish and unindent the file will be automatically closed the reason to keep track of whether files are open or closed is because a you can only have so many files open at once and b in some operating systems only one program can have a file open at once so it's good to be careful and judicious about closing your files when you finished dealing with them and that's what this with statement handles for us automatically so with the file being open we can do something to it and the most basic thing you can do to a file as we saw as i was alluding to before is loop over it so we can write for line in f print line we then run our script that's it called file.py and it's printed out hello this is a file almost exactly as we saw there the thing that's different between the two is that here we have no blank lines but here we've ended up with blank lines after every single time it's printed the way to fix that or the reason that's happening is because every line in a file has the word h-e-l-l-o and then it actually has an invisible character at the end of every line called a new line or a carriage return which tells it to go move on to the next line now that's fine except that the print statement also always prints something and then does a new line for the next time so we're getting a new line being printed from the file itself and another new line coming from the print statement so we end up with two new lines after every thing that we print to fix that there's a few ways of doing it the easiest way is to tell the print statement not to print its own new line so you can do that by passing a second argument to print the end argument and just give it an empty string by default end is slash n which means print a new line if we tell it to do nothing after printing each line it will then print our file correctly i'm getting something funny happening there so i am going to do ignore me for a second um [Music] there we go so we say hello this is and then we say a file at the end so it's printed it it hasn't printed a new line after a file and then it's printed my prompt straight away afterwards so that looks a bit messy so let's see if there's another way we can do this so maybe instead of suppressing the new line that's coming from the print statement we instead suppress the new line that's coming from the file so the way we can do that is we can take lime and we can assign it to be line but there's a method you can call on all strings called strip and what strip does is remove any spaces or new lines from the beginning and the end of the line that will therefore get rid of the new lines that come from the file and leave us only with those coming from our script here and that means we can move back to here and we can run python file.py now after saving this one and now it does the correct thing so what we've done there we've called the strip method on our each line of our file which has got rid of the extra new lines that are kind of hanging around so have a go at doing that yourself reading in a file doesn't matter what file you put here as long as it's in the same directory as everything else and make sure you're happy being able to read it in stripping the new lines that come with the file and then printing out each line i'll just cover now the last thing i want to talk about in files since we've only got a few minutes left of the session and that is how files deal with data types so i'm going to change my data.txt down here to have some numbers in 42 9013 if we now run this again we get those numbers printed out now we know that we're looping over numbers we want to be able to do something like uh print line times two maybe want to double each of these numbers to get um 62 18 026 so if you want this now see what happens ah we get something strange instead of doubling the numbers to be 84 it's double the string that's very strange the reason it's doing that is because it's reading everything from this data file as if it is text even if it is a number if we know that everything in here is a number then what we need to do is once we've stripped it to also convert it to a number by using for example the integer function this is going to take what was lined with new lines strip off the new line so it's just the digits then convert it to be an actual integer so then we can do mathematical operations on it this is like the difference between two in quotes and two by itself so now when we run this it does the correct thing it's a question there from saleem about uh art of knowing all the things that you need to do as gareth says it just comes with practice the more you do it the more you learn stuff i've been doing python for 10 years or something now so this stuff just becomes second nature the more you do the stuff you shouldn't expect to be an expert at python after a three hour session one afternoon it takes time but there's lots of people learning with you and there's lots of people who can help so that's the last thing in the the file section the very last summary section here has on it a big difficult exercise which i'm leaving as a homework exercise for you if you'd like to carry on with this after the session with that i'm pretty much going to wrap up thank you very much for attending and i'll hopefully see you all soon welcome to this afternoon's course on intermediate python this is a follow on from our beginning python course so this will be assuming knowledge about the basics of python so we'll be assuming you know how to make loops and dictionaries and lists and things like that when working through some slightly more intermediate material as a follow-on from that course now the first page of these notes here go through setting up jupiter lab in the landing page there was a link to some videos which hopefully you've had time to go through to make sure everything's all set up otherwise if you've been to our beginning python courses then you should all be ready to go anyway but i'm going to go through that just in front of the room now to make sure everyone's up to speed give everyone a minute to catch up and then we'll launch into the actual material so the first page of the course notes here are mostly going through getting jupiter lab set up so i'm going to scroll down as far as the setup and there'll be a little bit of demonstration so i've switched tabs now to jupiter lab this was launched through anaconda and i've set it up in the same way as i showed in that video so i've got a text editor on the left hand side in which i've already written a short python script hopefully everyone here is comfortable with that level of python at the very least on the right hand side i have a terminal in that terminal i can run for example python and then space and then the name of the script which in this case is script.py press enter and it runs it and prints out what the script does so throughout the course today whenever we're doing exercises i'll be asking you to do the exercises in a text editor and run them in the terminal that's how you're going to be working the exercises today but in the course today we're introducing a potentially new tool called the python console which i've opened up in a second tab on the right hand side as well so if i click on the python console this is where we can write and run python code interactively it works very similarly to a jupyter notebook if you've used one of those but we can write in here at the bottom print and then put something in the string hello and then run the cell where i shift enter it prints the code and prints the response so when going through the examples in the session today i'll mostly be working in the python console here because it lets me quickly iterate but i might be using both modes at various times so i'm going to give you a minute now to make sure that you can a write a python script and run that python script in the terminal and b that you can load a console and run some python code in there as well so as to your question david um when you're writing code in the console when you want to run it you have to hold down shift and press enter it might also be that control enter works no control enter doesn't work so you have to hold down shift and then press enter and that should run that bit of code it looks like most people have managed to got things working now so i'm going to just do a little bit about how the console works so i imagine that most of you haven't come across the console before so i'm just going to show you some of the features that it provides which i think are quite useful so i'm going to start off by making a list with some animals in it so cat and dog normal python list something we've all seen before can i run that with shift enter so l is now a list type so if i want to do something to this list we learned about in the beginning python course that we can call list.append on lists to add stuff to the end of them so if we do l dot but if we pretend now that we don't know what functions are available on the list if you press the tab key just once just press the tab key on the keyboard then you get popping up a list of all the functions that can be completed so you see there there's the append function that is the one that we want to do in this context to add something to the end as well as clear copy count etc etc this works with more than just lists this will work with any data type in python so you can always use this as a quick way of finding out what you can do with a particular piece of information in our case we want the append function so either click on it or press enter then let's pretend that we don't know how to use the append function we don't know what arguments it takes and we don't know necessarily exactly what it does to the list the python console provides a way of finding help directly inside the console without having to go away and google and to do that if you follow the name of the thing you want to find out about with a question mark and then run that cell and i run that cell with shift enter you'll see it prints out directly in line a bit of information and context about what that function does it does use some somewhat technical jargon and for example it tells you what the append function does by using the word append which is a classic mistake but it tells you that it's a function which takes one argument the slash there's a behind the scenes thing which means doesn't take any more arguments but it tells us that whatever object we pass in it appends that object to the end of the list and that's a useful thing to know exactly what it does it adds it to the end of the list rather than the beginning or somewhere in the middle that means we can go ahead and use it so if you press the up key on the keyboard we can then that brings up the previously one command you can keep on pressing up and you can cycle through up and down cycles through the recent event commands delete the question mark call it like a function and pass in something like course if we now run that with shift enter the list has been affected and it's hopefully added horse to the end if we want to have a look at what is inside the list now of course we can write print l as we've seen previously and we can do with normal python but if you just want to have a quick inspect of what an object looks like you can just type the name of the variable nothing else and then run the cell with shift enter and it will display on the screen the contents of that variable so it will show us what's inside it so if you have a little go with that yourself now using the question mark making sure you can display variable names just by writing the name of the available directly and see if you can get tab completion working so we'll move on to the next section of the notes i'm going to switch back over to the notes the bottom of every page there is a next button if we click on that we end up on the next chapter but let's go through string formatting and see what python can do to help us so if we go over to the console here and let's have a look at some features so these are features which were added in relatively recent versions of python there's always been ways of telling python exactly how you want your strings to be formatted and including variables in your strings as well but they've added several over the years and so i'm going to be showing you today the most up-to-date and most recent way of doing it but first we're going to start with plain old print functions so let's make a variable something called mynum and let's make it the number 42. we can of course print 42 and that shows up correctly does the number 42. no sorry i meant to say we can print my num and we run that and it prints 42. so it's accessing the variable as we expect we can also with print functions put in multiple arguments so we can say something like my num is and there's a second argument pass in the variable and that prints out the first argument you pass in automatically puts in a space and then prints out the second argument you put in so this works perfectly well there's nothing wrong with doing python coding this way the potential issues are that it's automatically put the space in and so maybe you don't want it to put the space in you want to format things a little bit more neatly and the second thing is if you get lots of variables and lots of things going on this can end up being quite a long string and it's hard to see by reading the code what your output is going to look like and so python introduced a way of including your variables directly inside the strings so you can see how it's going to be displayed at the end without having to construct it out of separate pieces so to do that we use a print function and we write a string as we ever did but to enable this special mode you have to write an f just before the opening quotes so you write an f and then the pair of quotes and then inside the quotes we can use the special mode so we write my num is but then instead of doing a comma and then passing a second argument we can directly inside this string use curly brackets now previously we've seen the curly brackets are used for making dictionaries this is a completely different use of curly brackets it's a common problem in programming there's only so many different types of brackets that they do end up being used for the same for different purposes but inside those curly brackets we can just directly write the name of the variable that we want to use so we just write my underscore num inside those curly brackets and when we run that it's going to write this and the space and it sees the curly brackets and it's going to replace those with the value of the variable that's inside them so run that with shift enter and we get the same thing printed out but here we've constructed one string which represents what we want the final product to look like but we put placeholders in for where we want our variables to be replaced you can do more complicated things so we can write answer equals 42 and pi equals 3.14159 probably two variables there and i can then print them out in an f string all in one f string so i write print open closing quotes again putting the f in front of the string and inside here we can write the answer is curly brackets answer the name of the variable and then outside again and pi is curly brackets the name of the variable and so it's going to replace this with the variable answer this with the variable pi and we'll point out the answer is 42 and pi is 3.1415 you can put more complicated things inside the curly brackets you don't have to just put a plain variable name so i'm just going to copy and paste a dictionary in here so here we have a dictionary which has one key which is answer and a value 42 and one key which is pi and the value is three point whatever if we want to print that using an f string we can do our f quote quote thing again and we can copy our thing from before but this time inside the curly brackets we want to get the value out of the dictionary so the dictionary is called my dict we use curly square brackets and then we ask for the key from the dictionary and we do the same thing here my dick square brackets to get the key and we ask for the string pie you'll notice here that i've used single quotes to get the string for the keys to get the keys from the dictionary and that's because i've used double quotes for the outside so you have to make sure you don't use a double quote if you've made your overall string using double quotes otherwise it's going to think you're closing the string and it will get confused but when we run this we see we get the same thing printed out so just spend a minute or so having to go with that and then i'm going to set you the first exercise to make sure that um the first exercise goes smoothly and that's because we're going to use the first exercise throughout the course today build up on it slowly to make sure that everyone's set in the first place but first have a go at doing this f string formatting and make sure that it's working for you so if you go to the bottom of the string formatting chapter and i'll do the same on my screen you'll see a big yellow box with a big long looking exercise in it so inside here there's a big chunk of code which we are going to be working on and improving as we're going through the course today we're going to be using as the primary bit of code to do the exercises on to learn and play around with the techniques that we're learning about luckily this first exercise is nothing more than take this bit of code and run it so i'm going to do the same thing as you will do i'm going to copy this code i'm going to move my text editor i'm going to make a new file a new text file put that over here and i can close script.py i'm going to paste all that code into that text file i'm going to rename it to encode.py because what this is doing it is it's going to take a string and it's going to encode it into morse code now the layout on the screen looks a little bit funky because of the line wrapping but this up here is a dictionary which tells us for each english letter how to convert it to morse code so the first exercise is to take this code paste it into a file called encode.py and to then in the terminal not in the console but in the terminal to run it using python or if you've been told to python3 in code.py and you should get printed out incoming message sos etc and then morse encoded dots and dashes so take a minute now to do that same thing copy that from the example in the notes paste it into a file called code.py and make sure you can run it in the terminal once that's all there then we can move on and start learning about the first of our new techniques for this course i'm seeing a few questions in chat so the first one from bryony your syntax error if it's saying that f strings are an invalid syntax then there's a chance that it's because you're running on python 2. so if you're getting that when you run your code in the terminal instead of writing the word python run the word python followed by a three then a space and the name of the script and that will hopefully give you something that's working f strings were only introduced in a recent version of python so functions are the first in the journey that we're going to be taking today the general theme of the workshop today is learning about how you can take your scripts that you've been writing so far and package them up make them reusable make it so you can share them and make it so that it's harder to make mistakes when writing code and when people are using your code as well that's the general journey we're taking and functions are the first step along that route so functions allow us to reuse code that's their primary purpose one of the biggest uh one of the biggest sources of mistakes in programming is when you copy and paste some code and you make some kind of mistake when you're tweaking your pasted version and it makes a mistake and then your code gets something wrong and it's really hard to spot functions are one of the ways that you can avoid that problem entirely and make it make it so that you can have a single unit of code which can be used in lots and lots of different contexts throughout your code base so i am going to go back to my console over here on the right and we're going to have a look at how we can learn about function so i'm going to start off with an example not using functions and then go through the root of turning it into something which can be reused so let's start off with writing a little bit of code which is going to add together the contents of two lists so we make a variable a which has got some numbers in it one two three and four and a variable b which has got some other numbers in it uninventively five six seven and eight when we run that we've got our two variables ready to go then we're going to write our bit of code which is going to add those two lists together and by add the list together i mean add the first two elements i add one and five put that into a new list then add two and six together and put that into a list add three and seven put it into the list and then add four and eight so we'll be adding the list together pairwise in order to have something to put our results into we have to start off by defining an empty list which i am going to call c then we're going to write a loop which loops over a and b and adds them together and puts a result into c so we write a for loop and i'm going to write a element b elements so a element will represent one two three and four b element will represent five six seven and eight for those two in and i'm gonna use a function called zip here which you can pass multiple lists to and allows you to loop over both lists at the same time so we pass a and b into the zip function this is going to loop over the two lists simultaneously the first time around the loop it will give us the first item from each next time i'm on the loop who gives a second item from each and so on and so on we then append our result into c and we compute our result by adding together alm plus b elem if i can spell we run this code here and it's going to go ahead and do the calculation finally we print c to have a look at what's inside it and we see that c contains the correct result we will be using this bit of code in a few examples today so make sure you understand what's going on here but with that we're going to have a look at how we can turn this into something which is reusable when you're writing code whether it's a small script or something on a much larger scale doing a large piece of scientific analysis perhaps almost all code can effectively be broken down into three main sections the first section is the bit where you read in your data and prepare your inputs this is what we've simulated here by defining these two lists the second chunk and often the largest chunk of your script will be the part where you do some kind of analysis to your data here we're simulating our data analysis by just adding together the two elements this is where you might run some kind of simulation or model or do some kind of numerical analysis to your data the third section that doesn't in most code bases is then doing something with the output of that simulation this is often something like save it to a file it's often a very short part of your code but thinking about your code in these three sections often helps you think about the flow of data and work out how you can construct a pipeline to analyze it in the most effective way because you'll often find yourself doing similar analyses each time you'll often in one week be doing analysis which uses a particular function and then later on you'll be using that same bit of functionality in a different analysis it's useful to be able to package up the middle step into small pieces which can be reused in multiple places and that's what functions allow us to do so we're going to look at this middle part of our analysis here and we're going to wrap that up into something which can be reused and so instead of us having to use three lines of code every time to do that functionality we can instead have a single line of code we run to call the function which implements it so we start a function definition with the def keyword which stands for define because we are defining a function we then give the function a name and now this is the name we are going to use when we call the function in the same way as we call the print function somewhere there is a bit of code that says def print here we are defining an add arrays function then we need to tell it what arguments it should expect to receive and we do that by using round brackets attacks to use when calling a function but here we are just defining it and we say that this function when it's past the arguments is going to refer to them as x and y so this function when called should be given two arguments we then describe the body of the function notice that when i press enter after the colon at the end it automatically indented like with for loops and if statements and so on indented code means it's part of the thing that's above so all of this code is part of the function i'm then going to effectively rewrite this bit of code here inside the function i'll use different variable names to make sure that we don't get confused so i'll make a variable called z which is going to be our accumulator we write 4 x lm y lm in zip x x y z dot append x l plus y l m okay and i've made a typo there append and then last time that was all we had to do because the c variable was left in the same level as everything else so after we'd done our code we could simply print c this time because we're inside a function we've got our own little set of variables which are internal and private to that function so in order for anything to get out of the function to escape to the outside world we have to explicitly return anything that we want the outside world to get access to the thing that we want to return from this calculation is the variable zed and so to return it to the person who called the function we use the return keyword followed by space followed by the name of the variable so when we read this is we are defining a function called add arrays which takes two arguments inside the function we are making a list adding stuff to it based on some kind of calculation and finally returning that new variable which we've just defined if we run this cell nothing has yet happened we haven't executed the code inside all we've done is define the existence of a function but that means that we can now call this function in the same way as we do with print statements print functions sorry we call it by using the name of the function that we just gave it add arrays and we pass it to arguments because that's what we defined it to want it expects lists and so we're going to give it the name of two lists a and b notice that the lists in this top scope are called a and b but inside the function they're called x and y that's perfectly fine they don't have to have the same name at all and in fact it's often better if they don't have the same name to avoid confusion in the outside world we refer to them as a and b the function knows them as x and y they're the same data but they have different names in the different contexts if we run this function call it passing in a and b and run that cell i made a mistake that should be append those added together not add them as two arguments now if we run this function we get printed out the correctly added data so we've got our 6 8 10 and 12 which is the correctly added together a and b lists so amy's asking with zip can you multiply divide etcetera a and b absolutely so if we look at our code here where we're doing our zip function all that zip is doing is providing us with the elements from those two lists so the elements of x and the elements of y are going into these two variables here we are deciding that we want to add them together but we could just as well replace that plus sign with a multiply or divide or absolutely anything else you'd want to do with those two variables in order to process the data as you see fit so there's no more questions we'll move on to the next exercise so go into the notes at the bottom of the functions section it's a chance for you to have a go at writing some functions for yourself so the first exercise here is to take that code from that last section which i'll just show on the screen and to turn all the code from here to here into a function so to move that section of code inside a function by defining a function to finding the arguments in this case it will take one argument which will be the message to be translated and then to call that function and check that it's giving you the same result once you've managed to do that there is a second chunk of code which you can start by copying and pasting into a file called decode.py and then you want to do the same thing to that chunk of code copy everything from the empty list creation to the join and put that into a function as well in the first exercise of the two your function should be called encode and the second exercise your function should be called decode i'm going to give you a few minutes to do this and if any point you want a bit of help either ask in the chat but also note that every exercise has an answer link afterwards so do feel free to have a look at the answer to see if you can learn anything from it or if you want a little bit of clarification lucas is asking whether it matters where you define the function for example in matlab they have to be defined at the end of a script in python you can define a function anywhere at all as long as you've defined it before you call it everything's going to work out absolutely fine there's a general convention to define your functions at the top but as long as they're defined before they're called it will work just fine and luca is asking when you define a function does it remain in python when you run it another time no a function defined will only remain as long as that script is being run we'll see in the next section how you can write functions and have them used in multiple scripts so you uh you've probably predicted in the future a little bit there but on the whole you define a function it exists in that script and you can use it for the rest of that one script i'm going to go through turning encode.py into a function on the screen now in front of everyone so whether you finish this section or whether you're still working on it if you want to watch along to see how i'm doing it feel free otherwise if you're still working on the exercises and you just want to concentrate on that feel free to carry on for a little bit doing that so if we look at encode.py the bits of code that we want to turn into something reproducible are the bits that actually do the conversion these lines at the top of the file are our data reading in they're setting the initial inputs to the whole thing both some data that's used for the conversion as well as the message that we want to convert these lines of code here are the bit that's actually doing the conversion they start by making a list that we are accumulating into they loop over the message and convert each letter in turn into the morse code equivalent and finally once all of those converted letters have been put into the morse list we join together all those morse letters using spaces and put it into this variable morse underscore message so this is the section of code we want to turn into something reproducible this is the thing that we might want to call in different contexts we don't always want to only call it on this particular message we might want to call it on other messages so we want to turn into something reusable we start off by defining a function define encode choosing a name for a function can be a little bit tricky sometimes the main advice i would give you is keep it short and use something which is generally an action or a verb when you call a function you want it to perform some kind of action and so you should name your function in a verb sense so this function is going to encode something it's going to take an argument the argument is going to be called message and we end it with a colon all of the code inside we then want to indent because that is all now part of that function at the end what we need to do is return morse message so the input comes in here gets looped over and then the output is pushed into morse we convert morse into morse message using the join and then we return our final output now that we have our function we still need to call the function if i run this code now we would get an error saying i don't know what morse message is so we need to make sure we call the function i'm going to scroll down a little bit and we call the function by using the name of it in code call it like a function and pass in the argument as message the result of that function which is what's returned here needs to be put into a variable and so let's call that morse message equals we can now save that and run it and we get exactly the same output that we got before the one thing i'd like to point out which i alluded to before with our a's and b's and x's and y's is that this variable here is called message we're also calling the variable inside our function message that's just because that's how the code looked before we functionified it but there's no reason they have to have the same name to make that clear i'm going to change our internal variable names inside our function into something else so i'm going to just call that i'm going to call this m and i'm going to call this m so the message that we want to convert is called m and that's the variable name we pass to the function inside the function it refers to as message and finally returns most message also these two variables don't have to have the same name so to make that clear i'm going to call our outside of the function variable morse underscore m and do the same thing with both of these so outside of the function we just have m and morse m inside of the function that's where we've got our long variable names there's nothing implicitly wrong with using the same names outside and inside but i wanted to show you that they don't have to be the same and to demonstrate it i will then run that bit of code the process for the decode function works in exactly the same way there's a bit of setup at the top which is defining our variables then the bit that does the decode works like this we've turned it into a function by indenting it called it in code finally called it and printed it out i imagine that some of you attending the course today might have done functions before it's always difficult to know what the introductory level of everyone's knowledge is but certainly by the end of the course today we want to make sure that everyone's up to the same level with all the topics that we're covering so i want to try and make sure we're not leaving anyone behind so now we've got our encode and our decode files and i'll show you that python decode.py works as well it prints out sos we have here an iceberg and need help quickly we are ready to move on to the next section so looking at this code so far we've got some information that is being duplicated between these two files so first of all there's this letter to most dictionary at the top which is in decode.py but it's also in encode.py so this is something which is central and core to the process that we're doing it's some kind of static data that's being used as part of our algorithm to do the calculation that we're asking for but there's other text or another code inside this file which is specific to the problem at hand in this case it's the message that we're translating you can imagine this being a stand-in for the data that i want to analyze today or the database that i'm getting data from that i'm looking at today so there's some things in this file here which are usable in all contexts no matter what kind of message i want to convert to or from morse code and there's some code inside these files which is specific to the task at hand that i'm doing today and so the first thing to think about when you're doing when you're when you've got code like this is to start thinking about which parts of your code are today's job and which part of your code are going to be useful in the future when you're working on a similar task a week six months five years down the line think about which parts of your code are generalizable and which part of your code are specific to your job today once you've got in your head how various parts of your code base fall into one category or another you can start making it so those parts of your code which are generally usable can be extracted out and put into something which can be used in multiple different scripts without having to copy and paste them and this is how we're going to carry on our journey towards making our code more reusable and harder to make mistakes with the first step was we've got some lines of code and so we turn it into a function so that we have a defined interface of what goes in and what comes out and so we no longer have to care about what's inside the function we just call it and we expect and assume that the internal is going to work correctly the next step along that path is to take the parts of your code which are reusable and to put them somewhere where all your scripts can find them and that is a feature of python called modules and so to demonstrate it i'm going to show you how we can do it with the add arrays example that we were playing with before i'm going to make this a little bit bigger here so i'm going to make a file called erase.py and inside that i'm going to put that same function that i defined over there on the right hand side so this is a normal python file it is as you've written any other time there's nothing special about it but this is going to turn into a module as we work through this little section here to turn a python script into a module in python requires no work whatsoever we have our vase.py which has inside it a function that means we can go to our console and we can write import arrays you might have seen imports used when you are working with for example numpy or pandas or other third-party modules that you might have used before in python but this allows you to write your own modules which you can put in functionality which you want to use in different contacts contexts yourself when we write import arrays it's going to first of all look in the current directory for a file called arrays dot py and because we've just created one it's going to find this file and it's going to import this module we run this with shift enter we can then do arrays dot because dot is how in python you get access to something that's inside something else we have the arrays module which is this whole file on the left hand side and from that module we want to import the add arrays function now if your tab completion is working you should after typing arrays dot be able to just press tab and it will show up there arrays dot add arrays and it's a function we can then pass in a and b and run it and it gives out our answer now this arrays function is the one coming from the module arrays which is the one that's defined over here and that means as long as this file is in our directory we can restart our computer we can change and restart the console but as long as we write import arrays we should get access to it if you're having any issues with this and i'll give you a chance to have a go with this yourself in just a moment the first thing to check is that you've saved your arrays.py file up here you should see a black x next to the file name in the text editor if you see a little black circle next to the file name make sure you've saved the file by going to file save which control s or command s if you have done that then the next thing to try in the console is if you right click in the console and select restart kernel that will clear everything out after pressing the bid big red restart button you'll scroll down sometimes at which point we can import arrays again define our a and b and then we can call arrays dot add arrays a b so if you're having issues try right click restart kernel run those lines of code again and see if that works gonna give you a minute now to have a go with that yourself then i'll carry on uh with the next little section so katie's getting an error module not found ever as is sofia so the first thing to check there is that as i said when you do import arrays it's going to look for a file called arrays.py in the current directory so inside the box here try running percent ls and then press enter and you should see the same list of files here printed in the console as you do on the left hand side in the file browser if you see a different list of files then you will have to make sure that you're in the same directory between between the two places so check that you're seeing the same list of files first and if you're not then let us know good i'm glad that fixed your problem katie it does get a bit confusing and jupiter lab isn't always very good at keeping everything in sync so that's always worth checking that you're in the right directory in all of the different places that need to be kept in sync and sophia if you're not in the right directory so for example i'm going to put myself in the wrong directory so on my computer my code is sitting in homemat temp slash python i put myself into the wrong directory there we go that's working but if you want to move then percent cd space the name of the directory you want to move into which is python cd stands for change directory run that and then percent ls should work canal points out that if you put in different length arrays then the resultant array has a length of the shortest that's a very good point and later on in the course today we'll be learning how we can check for errors and raise errors in those situations but to demonstrate what you're talking about for everyone else if i make a and b different lengths and then run the function we see that 4 never gets added to anything now there's two reasons for why that happens firstly is that there's no definition of what for added to nothing would be and so it makes sense that it's not able to guess it but the real root cause for it is that the zip function only loops over the arguments that passed in as long as they all have elements left as soon as one of the arguments past a zip runs out the whole thing stops that's something to be aware of when using zip briony you're seeing name error name arrays is not defined make sure that you have run import arrays before you try and call the function so you have to import arrays you have to do that every time you restart the console every time you do a new script in order to make sure that arrays has folder called my code you could run something like my code dot arrays and it would look inside that folder for that module otherwise when you've got much larger collections of code there are places you can put them centrally on your computer where you'll always be able to find them regardless of which directory you're in the next thing i want you to do is the exercise at the bottom of the modules page so in this exercise we're going to take the code from our encode.py and our decode.py and we're going to extract the common core elements and move them into a file called morse.py then we're going to edit our encode and decode to import morse and call the functions that are defined inside the morse module you can test it in the python console if you want to but if you do you will have to do the right-click restart kernel trick otherwise it's not going to import any changes that you make to your modules but spend some time on that make sure you're able to convert your morse code stuff into one module called morse and then import it in the other two files i'll go through the answer to the exercise now with your how we can turn our morse code functions into modules so i'm going to close arrays.py i'm going to make a new file over here and i'm going to call this file rename morse.py so encode and decode are going to represent our tasks we want to do today we're going to pretend that we're a morse code operator we've received a message and we want to turn it into morse code and this script here should just do the task at hand it should define an english message and then it should work out what the most code message is likewise decode.py is just going to define a morse code message it's i'm going to call the decode function to get back the english message all of the stuff that does the conversions we're going to move into more stock py the first thing we want to move in is our two functions so we take decode i'm going to cut it with control x and paste it into more stop ey and i'm going to do the same thing with encode and paste it into morse dot py so you've got our two functions in here but if you look at the encode function for example it's taking in one argument which this function is going to refer to as message so inside this function using message as a variable name is valid we can access that we define the morse variable we loop through message to make a new variable letter this is all fine this is all consistent on line six here we try and access the letter to morse dictionary and from the point of view of this function as it's written it doesn't know what the letter to most dictionary is because it's not inside this module and so we need to make sure we also move in any data that these functions need into the module as well and so that is the letter to most dictionary as well as this bit of code which calculates what the morse two letter dictionary is these four lines of code here invert the dictionary turn keys into values and values into keys so i'm going to take these two variable definitions here i'm going to cut them and i'm going to paste them into our module i'm going to save that file and i'm going to get it out of here because we don't need it in here anymore because it's not used anywhere in here and you see that has massively simplified the encode and decode files they are just dealing with a task at hand defining a message decoding the message and printed printing the decoded message all of the complicated stuff is locked away inside more stock py which the users in code and decode.py don't have to care about they don't have to understand what's inside here they just have to trust that calling those functions is going to work correctly we can do a quick test of this by going over to our console doing import morse morse dot in code and let's encode sos because i know what the answer should be gives us back dot dot dot dash dash dash dot dot dot so this function here is doing the right thing we've got it from the morse module which is more py called the function it's given us the response in order to do the same thing to our encode and decode files there's one other change we have to make and that is we have to explicitly import our module that we've just created imports in scripts always go at the top of the file by convention so import morse and now encode as a bear name doesn't have any meaning by itself it is now inside the morse module and so we have to write morse dots in code importing a module doesn't automatically make everything inside it globally available it keeps it inside what's called a namespace named after the name of the module which we access inside using the dot syntax this means if we go to our terminal down here and we now run python in code dot py it's printing the right thing we can make the same change to decode import morse morse dot decode printing out correctly and so now still our job we're working on today's script is nice and short and to the point we define our data we process our data and we print the result likewise with encode.py to find our data do something with our data and then print the result the complicated stuff's locked away in the module morse.py and once we've written it and it works we don't have to worry about it again we can just import it and use it we saw before how moving our code into functions allowed us to define the interface that they had we would say this function should have these inputs and it should have these outputs we also then saw how moving them into modules allows us to reuse those chunks of code in different contexts in one file or in another or in the console the last thing i wanted to show about what modules and functions provide for us is the ease with which we can test that our code is working how we expect if i make a new file up here i'm going to make a text file called test underscore morse dot py i've got a module called morse.py and alongside that i'm going to make a second file called test underscore more.py so the same name just with test in front this is a convention i like using for every module that i write to write a file alongside it with the same name with test underscore at the beginning in which i put a little bit of code which i can run to check that the module is doing what i expect so in order for that to work we have to start by importing morse and because our code that we've written is an encoder and decoder that allows us to test what's called a round trip we encode a string decode it again and check that we get back what we started with so let's make a message hello everyone we then make a new variable code which is created by doing morse dot encode message i then take the result of that and pass it to morse.decode straight away without doing anything to it and assign that to decoded the last thing i do is then print out checking that message is the same thing as decoded if message which is our starting thing once it's been own coded and decoded is the same as the result then we know everything's working well and so i can run this script anytime i like with python testmores.py and as long as it prints out true i know that everything's working and that allows me to go to my more stop py change stuff around fix bugs try and re-implement it in a different way and as long as my test is returning true i have decent confidence that the code is still working and i haven't broken anything this is something i highly recommend getting the habit of doing this is the first step towards testing your code but there are much more flexible powerful and useful techniques that you can use and we cover those in our other course best practices in software engineering so do check out that course if you're interested in seeing how you can do more with testing but for now in this course today this gets us most of the way there towards checking that our code is working how we expect we're now going to move on to the largest chunk of the course today if i go to the notes and that is classes and objects now classes objects are something that a lot of people struggle with when they first start learning python it does take a bit of a conceptual shift as you're going through but i'm hoping that i can take you on the journey today to show you how they can be useful and how you can use them in your own code they fit in the same kind of story that we've been telling so far we started with functions which allow us to package up chunks of functionality and give that a name and make it so we can reuse it in different places we then packaged up those functions and put them into modules which allowed us to use that function in different scripts or in the console however we saw fit the next step along that journey is to learn how we can package up not just bits of functionality but combine together data and functionality all into one blob which we can reuse in whatever context we see fit and the way that python and most programming languages provide that is using a technique or a feature called classes through the example here we're going to be writing some code which represents a dog and so i'm going to go ahead and start writing some code i'm going to do it in the console because we're going to be interacting with it i'm going to restart the kernel just to clear everything out and i'm going to clear the console cells just so that it's a nice clean slate so let's make a variable called our dog now the dogs that we're representing inside this code here we're gonna have a couple of attributes to them um and of the functionality that we've learned so far either this course or previous courses the obvious thing that we'd reach for would be a dictionary a thing that we can use to bundle together a bunch of different pieces of information so let's go ahead and make a dictionary the key is going to be name and i'm going to get some audience participation here and ask in the chat if anyone has an idea of what we should call our dog i'll go with bruno bruno the other bit of information we're going to store about our dog is it's color now what color is bruno i guess brown would be the obvious answer but uh anyone got any other ideas yeah let's go with brown that's fine and i'll do lowercase b so this is a dictionary which defines our dog his name is bruno and it's a brown dog now this is a perfectly reasonable way to represent data as you're developing code with python you're bound to very often reach for dictionaries as your way of bundling together disparate pieces of information which are related in some way and where you want them to have names which you can assign and use to relate to different parts of that object because once you've got our dog we can obviously do something like our dog square brackets name and it prints out bruno so we can get the data back out again let's imagine imagine however that we've got a function uh def called describe which is going to read in this dictionary and print out a message which summarizes the core essence of this dog so it takes one argument which we're going to call dog and inside the function all it's going to do is return a string in fact it's actually going to return an f string as we saw before which says dog i need the curly brackets name is dog color so it's going to put the name of the dog the word is and then the color of the dog depending on the argument that we pass in we've now got our function defined and usable so we can now call it describe our dog dog who knows brown good that's all working fine the issue comes about when you realize that this function here is making assumptions about the type of data that's passed in a cursive glance of this or a a comment describing this function might say this function describe accepts a dictionary describing the dog and it will print out information about the dog that's a one sentence summary of what this function does but actually it's more than that it requires that the dictionary has the key name and it requires that it has the key color if you pass a dictionary which doesn't have one of those two then it's not going to work it's going to raise an error of some kind and so when we're describing how this function works we would have to tell any users of the function you have to pass in a dictionary which describes a dog and that dictionary must have the keys name and color and if they make a mistake with that they're going to get errors and they're not necessarily going to understand why one of the important things that you can do as you're developing code is make it so that any functions you write have a well-defined interface which makes it hard for people to use them wrong if you write a bit of code which people easily misuse then it's not going to get very widely shared and people aren't going to want to use it they're going to want rigid rules which allow them to avoid mistakes as much as possible and so the main way that we can take something which is a dictionary and create some kind of data type which has a rigidly defined set of parameters or attributes on it is using a class at its core a class is a data type which has a strict list of parameters which must always be there and you can rely on them being there so let's see how we can take our definition of a dog and turn it into a class like with functions which we start by writing def we define a class using the class keyword we then give the class a name the convention in python with classes is that you give the name of the class a capital letter at the beginning as opposed to variable names which use lowercase and underscores this helps when reading your code to easily see what are classes and what are variable names we then follow that with a colon and then we are writing the code inside the class in much the same way as we do with functions or if statements or for loops or anything else the main things that sit inside classes are functions and so we're going to define a function now which is going to describe what a dog looks like so we do that with def now the special function that we have to create to define what a class looks like is called underscore underscore init there are two underscores before and two underscores afterwards this is a special function which is called in order to initialize the class it's going to set it all up so that we know it has the parameters and the attributes that we expect it takes arguments because it's a function all class functions always have to take an argument called self at the beginning of their list followed by any arguments that we want it to accept which in our case are going to be name and color and it's a function so we ended with a colon i'll come back to explain how init and self relate to each other in a little while but for now we're going to make this dog class use it and see how it works inside this function we are going to set some variables and we are going to set those variables we are not going to return them at the end but we are instead going to attach them to this variable self this variable self is representing the dog in question at the moment the dog that we care about the dog that we are currently initializing and so if you do self dot name we are talking about the variable name that's attached to the object self we assign that the argument that was passed in name we do the same thing with color self dot color equals color at this point we have finished defining and describing our class that's all you need for a class that has two attributes on it so we run that cell with shift enter and it gives us back no error so it's worked that class now exists in order to make an instance of that class we call it like it's a function we need the name of the class followed by brackets the arguments that we put inside the brackets are the arguments that are going to be passed into the initialize function not including self because self gets handled automatically it's the other arguments that get passed in so we can pass in bruno and brown we want to assign this to a variable so let's call it our dog again we've run that and that has created an instance of the class dog which we are now calling our dog dog with a capital d is a class this code here defines a class this variable here which was created from the class is what's called an object or an instance now that we've created this thing we can go ahead and use it we can say our dog dot name run that and it tells us that our dog is indeed called bruno so we're accessing attributes on our dog with a dot syntax followed by the name of the parameter that we care about and that is in contrast if i scroll up a moment to our dictionary style where we gave the name of our variable followed by the square brackets syntax so if you've got a dictionary use the square bracket syntax but because we're now inside using a class we can use the dot syntax we can obviously do the same thing with dot color and we get out brown this means we can now adapt our described function so that instead of taking a dictionary and assuming that there's a dictionary being passed in we can have it so that it assumes a dog object is being passed in so here instead of dog dot name sorry instead of dog square bracket's name we do dog dot name and instead of dog square back it's color we do square brackets color we can now call the describe function on our dog and it prints out bruno is brown so take a minute now yourself to write this code copy it in it's all written in the note so feel free to copy and paste it from there or use that as a reference spend a minute writing that code running it making sure you can make instances of your dog you can get the attributes out again and adapt the describe function so that you can pass in a dog object using the dot syntax get the attributes out and describe the dog have it all work correctly one way of thinking about a class in python and in fact in most pokemon languages is that it can be seen as a template with which you can create objects so the code here doesn't represent one single dog it represents all possible dogs we might want to create so here we created one instance of the dog class we call it bruno and we call the variable name our dog but we can create more we can create an other dog and there's been some great examples of dog names in chat and let's go with achilles and what color is achilles achilles is blue a very unusual color for a dog it's probably been mistreated there we go and i'll just run that out dog bruno line again so they're next to each other so here we've called the dog class two different times each time we've called it we've created a different object and so the way that works when you're inside your class is that when we called our dog let me call dog bruno brown dog is then going to look at the dog class because we're calling it with the brackets it's going to create an instance of that class which is going to need to be initialized and so python will automatically call this magic function underscore underscore init sometimes called dunder init for double underscore in it and it's going to pass in the two arguments that we passed in here as name and color the self object here is referring to the same thing as what's going to get passed out at the end so self here refers to the same thing as our dog and so when we do self.name we are effectively doing owldog.name equals which is why our dog dot color our dog.name give the right thing when we call this class a second time when we did other dog this time in the function name and color are obviously different because we're passing in different arguments but self is also pointing at a different object self this other time is pointing at the same thing as other dog and so self is the special argument in any class function which gives you access to the object that you're currently talking about and so because self allows us to talk about various different things about the class we can use it both to put data into the class we can also use it to get data out and so now let's see how we can extend our class here by adding some functionality to it i said at the beginning that classes combine data and functionality so far our class is all data it just defines some data which is held by the class we want to give our class some functionality as well and to that end i come back to our describe function at the beginning i was lamenting the fact that you have to remember not only that the first describe function if i scroll up took a dictionary and you had to remember to pass in my dictionary you had to also make sure the dictionary had the name and the color attributes or keys inside it we improved that a bit by saying that our described function here is going to enforce that as long as you pass in a dog object it will always have name and color because both of those variables are always defined inside the init function but we still have the possibility that we could pass something completely wrong to dog we could do describe hello and when we try and run that of course we get an error because describe function doesn't understand how to describe a string it only understands if it's past a dog object and so what we want to do is make it so that our function can only ever be called on a dog object and it can't be used outside of that context and so i'm going to bring up our definition of dog again and run it so that it's on the screen i'm going to go back up to our definition of our class and i'm going to show you how you can take this function that's currently freestanding and move it so that it's a function inside the class i'm going to copy the code because it's going to be only a short few steps we move it so that it is alongside the init function we paste this code here and make sure that it's indented so that it's at the same levels in it and the internal indentation is all the same the next thing we need to do is like we had with the init function the first argument was self here the first argument of describe is going to be self and as self here was referring to an object of the class dog likewise self here can refer to an object of the class dog in the same way that the dog variable in our freestanding function did that means we can simply rename this variable to be self rename this variable to be self and we name this variable to be self and that's all we have to do we have now moved the describe function inside the class to be a class function class functions are sometimes also called class methods those two bits of terminology are somewhat interchangeable in python now when we define this class it has got two functions on it i can make our bruno dog again we have to recreate our dog because it was still referring to the old version of the class and that means that we can now do our dog dot instead of doing dot name or dot color we can now do dot describe notice that we don't put anything in the brackets of describe and that because in the same way as self in the init function was automatically put in there in the describe function likewise it's automatically passed in where the argument that's passed in as the first parameter to describe is taken from the thing in front of the dot so writing our dog.describe we'll call this function with our dog passed in as self and that means inside the function self.name is doing our dog.name and self.color is doing our dog.color when we call this function here so that means if we run this code it gives us back the description of our dog and there is now no way to call this described function on anything that's not an dog type if we try and make a a list and do l dot describe that's not going to work because the describe function isn't defined on the list type it's only defined on the dog type and this will give an error so we'll ask you to take a moment just now and do the same exercise with your dog class take the describe function move it inside the class so it becomes a class function or a cast method redefine your dog of course you can give it any name and color that you wish and check that calling out dog.describe does the right thing and print something out take a minute or so now to do that and then we'll be moving on harry you're seeing a strange output of bound method dog describe and then a whole lot of hexadecimal and i bet i can reproduce that so i imagine what you've done is type something like al dog dot describe and then haven't passed the um brackets afterwards so if i run that i get a very similar thing and this points to a interesting feature of python which we cover in more detail in our parallel python course and that is functions exist as objects in their own right if you don't put the brackets afterwards it tells you that thing you just told me about that's a function or as it calls it a method that's been bound to the class and so if we put the brackets afterwards it does the right thing so far our dog is a little bit sad really it has a name and a color but it doesn't have any functionality it can't do anything it's entirely static we create it we give it a name and a color and then it exists in that form changeless for eternity or at least until we turn off our computer we want to have the ability to represent data inside our classes which can potentially change over time something which is going to represent the state at one point in your program you call some functions and it keeps track of things and it has a different state another point in your program so we're going to make a new parameter or new attribute of our class which unlike these two uh arguments here were taken from the argument list you can just make static data which is attached to your class so we can make a new variable self dot energy this is going to represent how much energy our dog has now if anyone has had a dog you'll know that they have a lot of energy but we're going to pretend that initially our dog is always created with an initial energy of one in some kind of units undefined so this parameter here this attribute isn't being defined based on an argument being passed in it's just statically defined to any new dog that's created to always have an energy of one so if i create that and i make our bruno dog again in fact let's make a different dog because we've had some good uh examples in chat we had one earlier called corona and i'm going to make corona gold so now ow dot now underscore dog dot energy is number one it's got an energy of one so we've got some information about it so that's all very well we can you know change the energy to be 100 and then it's got an energy of 100 but it's nice to instead of having to dive into a class directly and change its parameters to instead have functions which do the actions which we want this class to perform and keep track of the internal state for us so we're going to be adding some more functions to our class which allow us to exercise our dog so the exercise function is going to reduce the amount of energy that our dog has so we go back to our class here and we're going to add another function def exercise again it takes the self parameter self argument very easy when you're writing functions into classes to accidentally forget the self but you'll quickly notice it when you try and use the code and the exercise function or method is going to print out a message telling us that you take self.name for a walk and then it's going to do self dot energy notice itself here is the argument that's passed in so it's referring to going to be referring to the same thing as this minus equals that's how you decrement or decrease a number by a certain amount one so this function will print out something and then reduce the energy by one so if we make our dog corona again we print out the energy our dog dot exercise it prints out the message we took corona for walk and we look at the energy a second time we see the energy is now zero if we make a second dog to draw home the point that each dog object is an independent thing we make our bruno again uh other other dog other dog energy has not changed so each dog has got his own internal energy attribute which it's keeping track of so have a go at implementing the exercise function now as well as adding in the energy parameter once you've done that have a go at making a fourth function on the class called feed which is going to feed the dog and increase the energy by one i have on a screen here the example of the feed function so the feed function works exactly the same as the exercise function it prints out a message saying that the dog eats the food and then it does cell to energy plus equals one to increase the energy so if we define that class redefine our dog we can now do other dog dot feed and eats the food we can do it again and eat the food again so that if we look at other dog dot energy the energy is now three so it's a dog with an infinite appetite which will accrue an infinite amount of energy but such is life when you're simulating things on the computer there's some text on this chapter here which is um uh explaining how this stuff all works so do have a look at this text later on all these course notes will stay up permanently so do have a look through the text if there's anything you're not clear about but have a look at the exercise at the bottom here which is to edit the morse.py script which is our script which is holding our library code our functions and our static data and to create two classes one class called english message which has the encode function moved inside it and another class called most message which will have the decode function moved inside it and then to change test morse.py or make a new file called testmorse.py which imports those two classes makes a string and an english message from that string this is calling our first class calls the encode function on it to get back the encoded text string then to take that encoded string and make a morse message from it and to call the decode function on that and to check that we get our answer back at the end have a look at this exercise and see if you can create a class for the encode function called english message and a class for the decode function called morse message again there is an answer at the bottom of the section so do feel free to have a look at that answer in case my description of the question isn't good enough or if you want any help along the way and likewise feel free to post in the chat if you have any questions i'll give you a few minutes for this one because it's a little bit trickier as to your question there emma about it not changing the energy i'm going to show you on the exercise here what you've done so you've written return that string in the same way you did in the describe function and that means as soon as the exercise function gets called it's going to return that string and the function is going to end which means it's never going to get to this second line here instead change it so that you write print as a function so that it prints that outputs it and then carries on to the next line and that should then work gnar's asking what if i don't put init in a class so in the example here if i didn't have an init function at all it would still work it would mean that you can only call your function or create an object of your function without any arguments at all which means that none of the parameters or the attributes of it which are assumed to be there will be created you will effectively create an empty class which has no data which only has functions it's a perfectly valid thing to do if you want to bundle up a few of your functions under some kind of namespace but on the whole you usually want to have some data in your class as well as functions to act on that data i'll go through the answer to that exercise on the screen now so you can all see how i can convert a function and some associated data into a class which is encapsulating both of those things the easiest place to start is with the function here and we write class english message this class is going to represent a normal string an english string which is going to have an encode function on it which is going to return to us the morse encoded string we want to indent this and we'll come back to the rest of what we do to that in a moment we have to make an init function and the init function like all functions take self and that is our and message so we're going to make one class which represents a message and that message is going to be wrapped up and saved into the class with self dot message equals message the other thing that we want to save into our class because we want to use it inside our function is the letter to mouse dictionary so i'm just going to copy that i'm actually going to cut it and paste it into here make sure everything's indented nicely and write self dot letter to morse so this is saying that our class is going to be initialized or an object of our class will be initialized with an attribute message which comes in from what's passed in as an argument to the constructor as well as an attribute which is storing our conversion dictionary that means in our function down here where we are going to be converting our message it has to take the self argument now message isn't going to be passed as an argument to self and that's because we don't need to anymore because we can access message through self as self.message and so here instead of writing message we want to write self dot message and here instead of writing letter to morse we want to talk about this letter to morse up here so we write self dot letter to morse and i think that is all the changes we have to make so i'm going to save that file i did when you weren't looking make the same change to the morse message class which has got a self.message a self.morse to letter attribute and then the same decode function which had the same stuff done to it that means that we can come over to our test morse function file which imports english message and morse message it's going to call the english message class and pass in a string that is going to call this init function with message as the string which will then be saved as self.message it calls the message.encode function a message is the object of that class we just created which is going to call this function here do the same stuff we've been doing so far and return our message out at the end that goes into code string we can then stick that back into the morse message class and do the same thing in reverse where we call it make a more message object call code dot decode and then compare the two so now hopefully when i run this over here i will get out true printed so that means we now know that everything is working we could also do uh from morse import english message english message we'll do our sos again m let's call it m and we do m dot encode and we get back our morse code this means we can't accidentally call encode on something which is a morse code message and vice versa it makes sure the type is holding both the data and the things that we can do to it no one's piping up in chat to say they're having any problems or have any questions i will say that classes in python and in any language are often one of the biggest increases in complexity you come across when learning the language hopefully i've done a good enough job explaining how they're used how you can construct them and what their purpose is but they do sometimes take a little bit of experience and practice to get the full value out of them gonal is asking why i wrote from morse import english message there so i could have written exactly as i did before import morse and then done morse dot english message sos that gives us back an object of that class but by writing from morse import english message i've extracted out just the one thing that i care about from the module and so i get access to the class name just by itself without having to write morse in front of it so the last section that we'll cover today as you know before we do the last section today i think about naming i did mention this earlier but classes often have these capital letter names like morse translator or dog whereas variable names and function names use this snake case it looks good snakes it goes on the ground and it's got sort of up and downs to it so snake case in python is used for variables and functions and modules in fact whereas camel case and it's so cool because it's got these humps is used for classes if you stick to this convention then other people reading your code are going to understand what's going on so the last thing i'm going to cover today is on the next section of the notes and that's handling errors so going back to our console let's have a look at what these dog classes can do so let's create our dog again so we know what state it's in and let's make a other dog equals dog bruno brown so we've got our dog defined our dog can do things we saw before we can perform actions on the dog we can call these functions and we can do other dog dot exercise that works fine we can do other dog exercise and we can exercise it and then we can do other dog exercise and it keeps on walking and it keeps on working and everything seems like it's all working well and that is until we ask the dog how it's doing and we look at the energy of the dog and we see that somehow its energy has ended up negative now under very few definitions is a dog with negative energy doing very well and this is because we've allowed our exercise function to arbitrarily keep on reducing the energy attribute by one every time we call it without any checking that it has energy to go for a walk and so let's look at what tools we have at our disposal in order to make sure this stuff is uh being checked as we're going through so the main tool that we've had in our disposal so far for checking stuff is an if statement so let's go ahead and use the tool we already know and see how far that can get us so we only want to exercise if self dot energy is greater than or equal to one if it's zero then the dog can't go for a walk because it's going to end up with negative energy by the end of the walk we are going to allow the dog's energy to get to zero that just means it's very tired so if the energy is greater than one we allow it to happen and so if we run this code to find our dog and make our dog and then exercise it we get the message printed if we try and exercise it a second time we see that no message gets printed we can exercise it as many times as we want but if we go back and check what the energy is the energy has stopped at zero it's never gone below zero because we are checking as we go along that the state of the system that we're looking at is in some kind of sensible situation of course it's very easy to miss that this one printed something and this one didn't and so we really want to when printing these things out to print something else in the situation where the dog is out of energy so let's write else and i'll copy and paste this the dog is tied it doesn't want a walk so if we now run that class and define it make our dog and try and exercise it it works once because we reset the energy to one in our initializer if you try and do it a second time it tells us that it's unhappy that's good we've now got something to do in the good state and something being printed in the bad state but when you're writing code which is going to be used by other people it's still very possible to ignore this line accidentally if for example we do this same line of code multiple times because we think that everything's working fine and then we do afterwards other dog dot describe because we've seen here that we've run this three times we're gonna expect that the energy is going to be three lower than it was now let's look at energy but actually it's silently failing and it's allowing these more exercises to carry on it's allowing us to keep on trying to walk our dog without us being told explicitly and forcing us to make a decision to notice that it is in fact tired and to the way that you can raise an error in python which the person calling your function can't ignore is with a thing called exceptions and so we can call we can raise an exception in our code i'm going to make this a bit bigger but instead of just printing out this message by writing the word and i'll make this wider here as well so everything fits on the screen the raise keyword followed by the type of error that this is one time error in the notes here there's a link to the documentation which lists all the different kinds of errors you can raise in this situation it's an error which has occurred while the program's running and so raising a runtime error makes sense it's up to you to decide what kind of error makes sense for your situation so now when we exercise that code and we make our dog and if we try and exercise it too many times we get an error raised a deliberate error which tells us that a runtime error was raised and the dog is tired if we look at where the lines of code raise the error we see that it's telling us in the exercise function on line 15 we raise the error but that was called from these lines of code here which are these lines of code here and you see it stopped on line two it's trying to exercise it the second time hasn't been able to and so these other two lines of code here will never ever run it's forcing us to make the decision about how we deal with this problem now there's a few ways that you can deal with an error that's being raised like this but one of the most common is using what's called a try accept state a try except block is going to try and run some code but if an exception or error was raised it's going to allow us to run a a different bit of code to kind of deal with the error that we're seeing so we write try and then we put this code in here i'm going to try and do this except if there's a runtime error raised in which case we are going to print something out and see now when we run this it says something went wrong so these code was attempted to be run a runtime error was raised and so this message gets printed out that's good we were able to catch the error and deal with the situation but we've lost the information about why it fails and so what we can do in this situation is take the error that was raised capture it and give it a name by writing as e then if we turn this into an f string we can put e as a parameter inside the f string so now when we run this we get something went wrong with tindex as the dog and then the contents of that error printed out so printing a logging message when you've got an exception raised is a useful thing to do but in some situations you can actually deal with the error and fix it in that situation so for example we have our try block still but here inside the exception handler this accept state here if the dog has run out of energy we can try and feed it and if we feed it then we will be able to exercise it so we've tried to do these lines if any of them fail due to a lack of energy then we end up in this block at which point we can feed it and try and exercise it once more so when we run this we see that one of these lines failed presumably the first because we never saw an exercise line be printed it's gone to this section here where it has fed the dog and then it's able to successfully take it for walk without any further exceptions being raised so for your homework after this session is to take the ideas that we had here with catching errors and raising them and apply them to the encode and decode code that we have inside our morse code translator if you try and translate code in morse code which contains exclamation marks or at signs or pound signs anything like that it's going to fail so the exercise at the bottom of the handling errors chapter i'll leave as homework for you and it's to take this same idea of tri-accepts to catch the errors as they're raised and to practice applying the try accept statement to them to make sure you get a sensible error message printed out on the very last page on the notes there's a link to our summary which goes through what we learned today as well as some links to the notes for some of our future courses so do feel free to have a look at the notes for those and sign up for any of those courses if we're running them in the future with that i'll just say again thank you all very much for being here today really great session and i look forward to seeing hopefully all of you in one of our future courses bye-bye data analysis and python this is a continuation really only from our beginning python course we've tried to keep the requirements for this data analysis course as minimal as possible to make it useful to as wide a range of people at the university as possible we don't want people to feel they have to be an expert in python before they can start using it for making their job and life easier so we only require a relatively small amount of python experience but that said there might be some things in this course which are new to you so as we're going through do feel free to ask in the chat if there's a particular piece of syntax or particular function that we're using that you're not sure how it works please do post a message and ask a bit of clarification and we'll try and help out in order to run the session today in the previous classes we've been using jupiter lab which we've installed through anaconda jupiter lab we've been writing writing text files with our python scripts in and then running them in a terminal in the session today we're going to be using a tool called a jupiter notebook now it's possible some of you have used a jupiter notebook before so just to get a bit of a sense of the room could people post in the chat whether they have or have never used the jupiter notebook before no good so lots of people who haven't used them before so that's going to be the first thing we go through today um those who have used it before this first 10 15 minutes or so feel free to feel free to follow through it as well otherwise since all the material is self-guided um you can just carry on through to the next section without waiting for me to get to it if you so wish so the first thing to make sure you've all got is anaconda anaconda navigator all started up and running so i'm just going to give you all a minute or so or 30 seconds or so to start that up and have it running in the background before i carry on to actually starting the session okay so hopefully you'll have anaconda navigator started up now and running it should look something like what i see on my screen here you might have a slightly different list of apps or you might have slightly different versions of things but hopefully it's a similar kind of idea now i said we were going to be using jupyter notebooks today however i don't want you to immediately click on the jupiter noteback notebook launch button if you do want to use that mode of launching dupes notebooks it's okay it's going to work just fine and if you feel more comfortable there that's okay but for the session today we are going to use jupiter lab as the way to start um the jupiter notebook server i'm going to explain what these terms um mean in a little bit but if you go ahead on the jupiter lab tile which is probably the first one and click on the launch button that should open up in your web browser one that i prepared earlier that looks something like this so i'll just move those to the side so we can see both of them so click on the jupiter lab launch and they'll open up in your web browser something that looks like this i'm going to go ahead and make this window here full screen and extra full screen and the rest of the session we're going to be living inside this window here so jupiter lab is a web-based i say web-based it runs entirely on your local computer but it works in your web browser even though it's entirely local and it gives you an environment where you can run run python code you can write python code one of the things it has built into it is its own jupyter notebook environment so the very top icon you should see on your launcher and if you've been to our previous sessions you might not be seeing the same view just go ahead and you might for example have something that looks like this just go ahead and close all the tabs at the top that's perfectly fine click on the python 3 icon under the notebook header and that will open up a jupiter notebook which should fill the whole screen so having clicked on that jupiter notebook python 3 tile you should get something that looks something like this now this is called a jupiter notebook i'll just give you uh 30 seconds or 20 seconds or so to get this started up and again if any issues post in the chat what we have here is a jupiter notebook a jupiter notebook is a way of writing and running python code in an interactive way and it also provides us with a bunch of extra features which are really useful when we're doing a data analysis kinds of tasks so with the first case what we can do is we click inside the gray area here and we can type any python code we like i'm going to keep it nice and simple we can print hello now if you just press enter at the end of the line it just puts another line inside that cell so it doesn't run the code just by pressing enter if you want to run this cell you either click on the plus button just above it up there or hold down shift and press enter i'll click the plus the play button this time but in this course of this session i'll probably quite regularly be using shift enter or control enter to run the cells so you will sometimes see me running a cell without clicking it and that's what's happening there if i click the play button it runs that cell and prints hello it also gives every cell that's been run a number so you can keep track of which order they've been running because in principle it's possible to rerun cells that are further up the page after having run further cells that are further down the page it can get confusing and we'll try and cover that later so i'm just going to switch over to the notes now and copy and paste a few examples from here so one thing we can do as i said before is have a code over multiple lines um i'll just show that by typing so a equals five b equals seven a plus b if i do shift enter it runs that cell and displays 12 on the screen notice here in the second case i haven't had to write print a plus b i didn't need to do this because the way that jupyter notebooks work is whatever the last thing in a cell is will be displayed onto the screen so here because a and b has the value 12 it's displayed 12 underneath the cell as the output for that cell and you see the output and the input have got corresponding numbers as well as being able to run python code as we have done in the script so far it also gives us an extra ability to do you need to make that small there we go an extra ability to display output from commands in a slightly more visually appeasing way now don't worry about what this bit of code here does we're going to be discovering how these things work throughout the session today i'm just going to make my font a bit bigger um we're going to be discovering how this works well this session today if you run this it's going to print something that looks like a nicely presented table of numbers where you hover over it and the lines get highlighted which is useful when you have larger tables of data the thing is really useful however is the ability to display plots so here again i'm just copying pasting this in we'll be discovering how these things work throughout the course if i run this cell with shift enter we get a plot coming up in line so if you use something like mathematica or i think matlab can do this and r notebooks do this too it lets you run code and get the output from that code right there and then you can carry on further underneath and you know print whatever you want and you've got the code in line at the point where you asked the figure to be shown the final thing that it allows you to do is as well as having cells which contain code at the top of the window there's a drop down menu that says code at the moment and with this cell selected if i change that to say markdown i click on the markdown button on the drop down menu you see the square brackets at the beginning have disappeared and now this won't accept python code and run the python code it will instead accept markdown now markdown is a way of applying uh formatting and design to plain text so you can start headers by doing a hash key and you can write header and then if you run that cell you see it turns the word header into a big word header you can then double click on it to edit it you can write bold text and italic so i could if i could spell correctly and you see it formats it with bold and italics and so what this allows you to do is to interlace with your code blocks of text which describe what your code is doing and the real powerful thing about this is it gives you the ability to write a whole report
Info
Channel: Geek's Lesson
Views: 68,696
Rating: 4.9313307 out of 5
Keywords: python prgramming, python tutorial, python programming course, python for beginners, intermediate python, python full course, python, programming
Id: hl-Y3tLMkbY
Channel Id: undefined
Length: 225min 30sec (13530 seconds)
Published: Fri Oct 16 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.