 In this video, I'm going to introduce you to the programming language called Python. Now there's the term language in there. It's much much like a spoken or written human language. Now you don't learn a new language by picking up a dictionary and memorizing all the words in the dictionary. You start using it and the same applies to Python. You've got to start using it. And we're going to start using it not too long from now. But there are a few basic concepts that you have to know about. So I'm going to take roughly about an hour and just introduce you to some of it. Now don't run away. Some of it's going to look difficult and some is a bit odd and strange and unfamiliar. You don't have to memorize anything and you don't have to understand everything in this hour. Look at some of the concepts. Try and just familiarize yourself with them. It actually is quite easy and it's quite a bit of fun. Open that Google Collab notebook and let's get started. And we are back here in Google Collab and Python in a few minutes. As you can see is the name of my notebook there. Remember you can just click on there and the name at least on the left side of this dot for the IP YNB file. You can just hop over there, change it whatever you like. It's clinical research. You see there the three logos. Don't worry. I'll show you how to do those logos before this course is over. But it's brought to you by myself, my research group at the University of Cape Town. Although this is for everyone it does follow the same chapters as we have in the Coursera course on understanding clinical research. So Python in a few minutes. Can it be done? So remember this is not learning the whole language. We're not sitting with a dictionary when we try to learn a new human spoken language and just learn all the words from the dictionary and then we can speak the language. No, we're going to start using it. And as we start using it, everything becomes clear and really relatively easy. But there are a few basic concepts that we have to understand I think before we go. So this is going to be very simple, simple introduction, but it's going to give you enough so that we can just start doing our statistical analysis once we get going. So let's just start with a simple arithmetic. Now we see on the right top side there my Python 3 Google Compute Engine back end is already running. We see the RAM and the disk size there versus what is available to us. So it's already running. So I've done a new code cell here, remember. So I'm just going to hover there and hit the code cell and we're going to start entering some text. And what I've done here is two space plus space two. I've seen this before two plus two. We know that it's four. Now remember our little hashtag symbol here when we write a line of code, that means tells Python everything after this, please ignore that. So we can write it here at the end of our code because the two plus two is still going to be seen by Python, it's still going to be executed, but everything after this hashtag is going to, both things are going to ignore that. So hold shift and enter as you can see there on a PC and Linux machine or return on a Mac to execute that cell. Remember we can also just hit this little play button. So we're going to play it and get sent to Google. Google's massive computers executes that for me and sends me the answer back in displays right here in my browser and the answer is four. And of course we needn't just stop with just the two numbers. It's just a massive calculator is what Python is here for us two plus two plus 10. And of course the answer is going to be 14 subtraction just as easy. That's the minus symbol on a keyboard. And if I shift into that shift return that or hit the play button seven minus four three. We have looked at this before. Remember there is no multiplication symbol on a keyboard. Most keyboards that I'm aware of anyway. So we use the star symbol that shift and eight on most keyboards that we shift and eight on my keyboard. So if I want to do multiplication three times four, that is going to be 12 division is the fourth slash key again. There's no division symbol at least not the one that you learned about at school with a line in the dot on top of him in the bottom. There's nothing like that on the keyboard. So we use the forward slash key. So if I want to divide three into 14 and otherwise 14 divided by three 14 is the numerator above the line. So three is the denominator below the line 14 divide by three. And we're going to get this numeric representation of the solution. This is just a representation of the solution. This is not an exact solution. The exact solution is 14 over three. If you keep it in that fraction, that's the exact solution. But we get this numerical approximation. So it's going to truncate this long string of sixes with a seven, which means it's four and two thirds. And that if we would write 14 and two thirds, that would be the exact solution. But here we get this numerical approximation. We also get integer division. Remember, that's the whole number that you want to have. So that's that four that we can see there. So for that, I'm going to use two forward slash symbols. That means three goes into 14 four whole times. And there's going to be a bit of a remainder to get from three times force 12 to get to 14. There's a remainder of two and to get to that remainder. If you're interested, I'm just showing you some of these mathematical symbols. And that's the percentage. If we just use the percent symbol there, that's the remainder. So three times force 12 plus another two is 14. And there you get this whole whole concept of division right here inside of Python. Powers, sometimes we want to take something to the power again. There's no specific way to write powers. You know, the small little superscript there. So we just use in Python two asterisks, two little star symbols. So three star star two means three to the power two. And of course three times three is nine. And so if we execute that, we should see a nine. And indeed we see a nine. The order of execution, that is something that you must remember from school. Remember, some of these operators should be done before others. So multiplication and division come, they come before addition and subtraction. So if I were to write three plus four times two, it's not going to go from left to right. So it's not going to say three plus four, seven and seven times two is 14. That would not be right. Multiplication comes before addition here. So it's going to say four times two, which is eight. And if we add three to that, we should get 11. Let's see. And indeed we get 11. If my intention was to do the addition first, I just use as you did at school. We use the parentheses. So I'm saying the parentheses come before multiplication in that order of execution. So it's going to be three plus four, seven times two is 14. And I'm going to get the desired 14, which might have been my original intention. So here's a little exercise for you. Six plus four times two divided by 10. And my intention is to do the addition first. So if you want to pause the video, just do this little solution. If not, just I'm going to show it to you right now. So there's our solution. Remember, if I double click on that, it's three little hashtag symbols, meaning it's the third small from the larger size three down to down to get to the third one. And our hidden cells there, I can just toggle on that and it hides that for me and it opens it for me again. And there's the solution six plus four. I put that inside of a set of parentheses. So that gives me 10, then it's going to multiply by the two. But all of that is inside of another set of parentheses so that I can do this whole numerator first. And then I divide by the denominator, which is 10, and that should give me two. And it's a 2.0. So that's a floating point number. So in basic terms, that is the mathematics that would be on any calculator. Now, let's just, I'm going to say a quick word on functions. Most of what we do in Python is based on functions, these keywords that are built into the language. And if we import other modules or libraries, we get even more. And that's fantastic, that ecosystem. And that's how we're going to do our statistics, by the way. But everything is based on these functions. A function is something, it's a keyword is protected. So you can't use it to do anything else. You shouldn't, you can, but you shouldn't. It's built into that. And you're going to pass this function something, and it's going to do something with this input that you gave it. And remember, we call these inputs arguments, some other languages call them parameters. The in Python, we call them arguments. Suppose you can call them parameters if you wanted to. But we pass this to the function, the function knows, based on what you give it, the type of thing that you give it, and it'll do something to it. So here we have the print function, just as the single example, just to belabor the point and to tell you about these definitions like arguments. It's going to take an argument, and remember those arguments are always going to go inside of a set of parentheses. And in this case, this argument that I'm giving it as a certain type, and the print function likes that we give it a specific type. And this type is a string, and string go inside of quotation marks. We've heard this before. So I put that, this is easy, full stop, and I put that in single quotes. So that makes it a string. The type of that thing that I'm passing to print, the type of the argument is a string, and print likes those type of arguments, and it can do something. And the thing that it's going to do, it's going to print whatever I pass it to the screen. So there we can see this is easy. You see it's stripped away my quotation mark, my single quotes there. So it's just printing that string, that sentence back to the screen. So that's a function and an argument. So anyone I'm going to show you because, as I said, this is not reading the dictionary. You're going to hear the word dictionary again, by the way. But let's hang on before we get there. You're not reading a dictionary to learn the new language. I'm just telling you these are the concepts and we're going to see plenty of functions as we go. So I mentioned this thing that we passed this argument as a certain type of thing. It is a string. And certainly there are many data types inside of Python. And Python is fantastic in this way. It has many data types and it knows what to do with certain data types. And it is all about this thing. And we're going to call it a thing for now, this kind of thing that we're passing. And we have this function called type. And if we pass something to this function type, it's going to tell us what type this thing is that we passed to it. So this thing is an int, short for integer, a whole number. And if I write 3.0 and I take the type of that, for us human beings 3 and 3.0 is exactly the same thing, but for computers, now that's a floating point number, which is just another term for a decimal, a decimal, a value with decimal points. So 3.0, that's a floating point number. And remember I said something like put inside of quotation marks that is a string. Let's see what that's called str. That's the type of it. So if you're ever stuck in something that's not working for you because perhaps it is the wrong kind of type that you are passing, just look at the type function. Remember the type function is very quickly going to tell you what type of thing you are dealing with in this instance. We're dealing with a string because it's inside of these quotation marks. Now I mentioned before very quickly we're talking about libraries and modules, and that's a way for Python to extend itself. So the base of Python, we're standing at the three series of Python and 3.8 is out. When I recorded this, it contains a bunch of functions. But we want to do more than what is system-based Python, and so there's this whole community around Python that just develops extra libraries and that just adds a bunch of new functionality including functions to base Python. But we've got to import them first. By the way, as Python is an open source, it's free of charge, and people work on a free of charge. So people take some of the free time and develop these modules. Some people do work for companies that do support them in the development of these, but this is a fantastic community. Let's carry on. The math is a module or a library that we can add to Python and we add it by this import function. So you're going to see import quite a bit. So import math. Math is the name of the library. It contains a bunch of functionality and functions, extra functions. So we just say import math. And when we do that, we have access to everything that's built inside of math. So one of the first things to look at is those weird and wonderful irrational numbers. The first one is Pi. You can see the beautiful Pi symbol there. If you want to know how I got that Pi symbol there, let me just lift the veil on that. It's a text cell and you see these little dollar symbols on either side. That is something called LaTeX. LaTeX, but call it LaTeX. And that is a type of code. And it is for type setting and very useful for mathematical type setting. So if we want to write mathematics on a document, if you want to send it for publication and you want nice mathematical symbols and formulas and equations and expressions in there, use LaTeX. So inside of Python, in the notebook here, we use these dollar symbols to indicate that we are going to now pass some LaTeX code. And Pi is a specific symbol inside of LaTeX. And these specific symbols have forward slashes in front of them. That's going to be forward slash Pi Pi. And if I execute that, we just get Pi. So that's not part of Python. I'm just showing you that that is possible. What we are interested in, of course, is the value of Python itself. So there's a Pi keyword inside of the math. And to get to these extra ones, because they're not part of Python itself, we have to reference the library or module from which it came. So we have to say math and then a dot. And then this new functionality that we want that is built inside of this library or module. So math dot Pi, I can't just say Pi. There's a way just to say Pi, but we won't get there yet. So I'm going to say math dot Pi. And there I see the value for Pi, at least to a couple of decimal places. Oilers number E, you might know it off, math dot E. None of these absolutely phenomenal irrational numbers, just like Pi, absolutely incredible values these. I just have to say math dot E and I get the representation of math dot E. Now we can also use the exponent function. EXP, you'll see that a lot more than you'll see the dot E there. Exponent is EXP. Exponent is the E itself. Oilers number and we're just raising it to the power one, because any number of mathematics that you raise to the number one just stays that number. So we're just saying exponent one E to the power one as I've written for you there. Let me just lift the veil on this little one because LaTeX is sometimes it's fun as E. And then we get this shift six carrot symbol on my keyboard and then one. And that carrot means write a little superscript in LaTeX. So I get this very beautiful E to the power one there. If I say math dot EXP and I pass the argument one, I get exactly the same thing, the Oilers number. Trigonometric functions. We're not going to use them inside of data analysis much, but it's just math dot sine. S-I-N, the mathematical abbreviation that we always use for the sine function. And I want to do, as you can see here, sine of Pi over two. Let me just open that for you can see how that happens. So that's backslash S-I-N that tells LaTeX it's sine. And then I have the set of curly braces on the outside because I'm going to do something after the sine function and the forward slash means fractions. And then two sets of curly braces, the numerator and the denominator and the numerator. I'm putting a Pi in the denominator. I'm putting a two. This is all just fun. It's not part of Python, but I just want to show you it's fun. So it's math dot Pi for the Python. Remember division is the forward slash so Pi divided by two. And if I take the sine of that, of course, the sine of a half Pi is just one. These are done in radians, by the way, if you remember that from school, not in degrees. So cosine is math dot cos. And you see a very funny looking answer here, 6.123e negative 17. So that's 6.123 times 10 to the power negative 17. If you remember anything from scientific notation, that is all the zero comma all these zeros until the 17th place, we have a 6. So 0.00000. This is rounding errors that are made for very specific reasons and within very specific constraints. And because of these constraints, that we have this representation and you can just think of it 0.00000. That's basically zero. That's what Python is just trying to tell us there. It's just zero. That number is just zero. And Sydney, the cosine of 90 degrees Pi over 2 is just zero. Logarithms, don't worry about those too much. Sydney, those kind of round off errors never going to bother us in our statistical analysis. At least not what we're going to do here. So if I want to take the natural logarithm, if you remember what logarithms are, sometimes some schools dropped the teaching of logarithms at school. Some still have it. You might know what logarithms are. We certainly can deal with logarithms inside of statistics. It's much better, I suppose, today to use what we call nonparametric tests instead of these transformations, log transformations. If I'm talking weird words here, don't worry, we'll get to that. But I can take the natural logarithm of E and, of course, the logarithm just says whatever the base is, I'm dealing with a base of E here. It asks e to the power what gives me 1. Well, or gives me the natural logarithm E is just 1. e to the power 1. If I want the natural logarithm base 10, it's asking there, 10 to the power what gives me a thousand? Well, 10 to the power 3 gives me a thousand. So log base 10 of a thousand should be 3. Let's see. Oh, 3. It is indeed. Square root, just as simple. It's the sqrt function. So math.sqrt, and I pass a value of 256. So what's the square root of 256? It's 16. So a very simple little exercise for you now. Maybe it's also time for you to go get something to drink or whatever. This is going to be a slightly long video. I want the tangent of pi over 4. Okay. Let's have a look at the solution. I can just pull that down. And yes, I'm sure you would have guessed this is math.tan tan. And I'm doing math.pi divided by 4. And let's see what the tangent is of pi over 4. And you again see this numerical representation 0.9999. That's 1. Python is just trying to tell you that it's 1. Later on, we'll have a look at how to do a round off. So if I were to round that to two decimal places, of course, the answer was going to be 1. Good. Let's move on to something very, very important. So we've done with a basic calculator that you have on your phone and used to have in physical form. I still have a very fancy calculator in physical form. I just love the thing. I still love to use it. Computer algebra system calculator. Really still fun to use. Anyway, a computer variable. Now a computer variable, you can describe this in different ways. The way I want you to think about it is this name that you give to a space in your computer's memory. You're going to name that little physical space inside of your computer's memory. You're going to give it a name. And you give it a name because then it sort of gets locked down and you can start putting stuff into it. Meaning you can assign things to live inside that piece of memory so that you can use it over and over again. And I've been using the term things all along. That is a bit cringe-worthy. What we actually put inside of that piece of memory is called an object. Everything is basically an object. Even these arguments that we pass as an object and what we get back as an object and these objects have types and we've looked at the types before. So the type of just three was integer. But that three was an object. More correctly, it was an instance of a class, of a certain class but not to worry about that. It's an object. And I place this object inside of this piece of physical memory in my computer and I give it a name. And that's much easier for us to do than writing the whole physical address in all sorts of weird and wonderful code in real computer speak. I just give it a name and that makes things very easy. Now there are some constraints to these names that you can give and it's best to start off a name with a lower case letter. No numbers, no uppercase letters, etc. You can use underscores, etc. But just start with a lower case number, stick to the basics. They make it very descriptive. You're going to view your code later on. So you want to give a variable name something that's descriptive. So just looking at the variable name tells you what you stored inside of it. And the most common thing to do is just to string a bunch of words together. So my computer variable name is my variable name and you see I put underscores there because spaces would be illegal characters. We can't use illegal characters. Space and Python mean something very different or can mean something very different. So I have to concatenate my words together but my variable name is very descriptive to me. So if I say mean of group one and I just put these underscores in between that that's easy for me to, when I look at the code or give it to someone else, mean of group one or group A or mean of placebo group. If you name your variable something like that everyone knows what you're talking about. Now that's called snake case because we're just trading this little snake of words and then the less popular is camel case. We also teach languages like the Wolfram language which I also teach is uses camel case and that is where we put the words right up bunched up against each other. We save some typing by not having to put the underscores. We start with a lower case but every subsequent word is uppercase just so that when you look at it as a human being you can still kind of see that these are different words. So you would write my new variable like that but sitting in Python it's much more common to use the snake case and please, please don't use the Python functions the built-in things don't call your don't call your variable print or type. That is a reserved keyword a reserved function inside of Python so don't do that. So let's get a few done. Here I have my value my underscore value and I'm using this equal sign now the equal sign you have got to get used to that's called the assignment operator that's not the equal sign from school or from math definitely not and we're going to say my value equals 3 that actually reads create a piece of memory just allocate a piece of memory call it my underscore value and assign an object to it so I'm actually just putting this inside that bucket I'm physically assigning what is on the right-hand side to what is on the left-hand side of that assignment operator that's what it is an assignment operator so I'm saying pass this object 3.0 this object is of a certain type remember it is a float I'm going to pass this assign this float to this piece of my memory and I'm going to call this memory my underscore value and that my underscore value that is what we refer to as the computer as a computer variable to some name so let's assign that there we go and now there's a little piece of the memory inside Google's computer not mine that has this an instance of integer object and this object has a value 3.0 not integer I should say float and it lives inside of this piece of memory now I can just recall it by just using the name so if I just type my value it's going to go look in a piece of memory and whatever object is stored in there it's just going to give back to me and we can look at the type of my value just as we looked at the type of 3.0 and it says my value that computer variable is storing a float object lovely stuff now one more thing about this assignment operator that you must be aware of which is one of these quintessential things let's say that is what this video is all about I'm just showing you these basics we are attempting just to read some words in the dictionary but later on we're just going to use it and these things will really become second nature because they're really easy I'm going to create this computer variable just called I and I'm free to do that because that's not part of Python it's not nothing that is kept by Python I equals 0 so I'm using the term equal but that's not what it says it says assign whatever is on the right-hand side of me to what's on the left-hand side of me to the right-hand side of me is this integer 0 and this is a computer variable name so I'm assigning the 0 to I and I can just on the new line type I that's just recalling it and this is going to print 0 that's what is inside of it now I want to give I another value but it's kind of an update value and if you read that sentence I equals I plus 1 that line of code there this one here let me highlight it I equals I plus 1 this is increase the size I think it's a face size that makes no sense because in algebra if I take the I to the left-hand side that becomes I minus I and that's 0 equals 1 and that's nothing not true because remember as I said this is not an equal sign this is an assignment operator so what is on the right-hand side on the right-hand side is I plus 1 what is inside of I at the moment while the value 0 and if I add 1 to 0 I get 1 so on the right-hand side I actually have 1 and now I assign it to what's on the left-hand side on the left-hand side is the computer variable I it already holds something but that's not cast in stone in your computer you can rewrite over stuff over you can delete something and store another document delete a document create another document it's not there it's not chiseled in stone inside of the silicone no we can just write over it so remember on the right-hand side I now have 0 plus 1 that's 1 I'm assigning that new value that new object I'm assigning to the computer variable on the left-hand side which is I now I equals 1 okay so that's a beautiful example of this not being an equal sign and just to show you people are lazy and there's all these little shortcuts instead of writing I equals I plus 1 we also just write I plus equals 1 that's just short-handed you'll see it everywhere and I just wanted to show it to you in case you saw it somewhere and didn't know what it meant now that I plus equals 1 is the same as I equals I plus 1 I at the moment is holding a value 1 I add another 1 to that's 2 and now I'm reassigning that to I so I is now 2 as simple as that so another little exercise time for a break but you can certainly attempt this pause come back to the video whatever you want to do before we move on to list objects which is another one of those important things so we're going to create a computer I want you to create a computer available to hold the string this is easy recall the computer available and confirm that the computer available indeed holds an object that is a string off you go so here's the solution simple to all that open you could have given it any name you want I called it my underscore string that's descriptive enough for me and I'm assigning to it what is on the right hand side here and that is a string and it says this is easy full stop and so I now have that if I want to recall it I'll just use the variable name and it's going to return to me whatever is in there see now the quotation marks are still there I'm not using it inside of a print function although I could also now have said print parentheses my underscore string then I would have gotten back this is easy full stop and the type of my string if we were to do that it's just the string it's a string object STR now let's move on to objects collections and we first we're going to look at is list objects there so a list object is this something that we can do to store more than one element before we've just used single values 3 3.0 one single string this is easy full stop now we're passing more than one thing and we the first of the most common thing we have in Python is called a list and we indicate the fact there were more than one way to do it but the common way is just to use square brackets square brackets 1, 2, 3, 4, 5 so I'm passing 5 elements creating 5 elements I should say inside of these set of square brackets and that is just a list and there we go we have a list and if I just type it in like that this is going to return that list is not saved in memory under in a computer variable nothing it's just that now I needn't just stick to a bunch of integers there yeah I'm mixing to an integer a string and a floating point value all in the same and if I asked what is the type of this thing and I'm still using the square brackets there so what is the type of this whole thing not what's inside of it but the type of the whole thing it's a list object as simple as that so let's get a list object but this time around we're going to store it in a computer variable and very descriptive there very original I'm calling it my underscore list and I'm parsing these four I have these four elements inside of this list that I create and I assign that to the computer variable my list and let's recall my list and it's just that list object that we get back with elements 1 2 3 4 now another useful function is just the len function len and that's short for length so it'll tell me how many elements is inside of my object my object is a list the length knows what to do it's actually short-hand for something a bit deeper but that's not what we're interested in here it's a function len we passed it an argument that argument is this object and it's called my list and it counts there are four elements in the beautiful now we're going to get something called properties or attributes and methods so if we've created an object an object will have certain things about it and things you can do with it it's built into this object that you created or an instance of this class and one of them is a method and we get to that method by the dot notation so first I'm going to use my computer variable my list which now contains this list object and then I say dot count and count is now called a method but it looks very much like a function because it is really just a function behind the scenes so it's my underscore list dot count and because it's a function but we now call it a method because we're using the dot notation after an object we pass an argument to a day which is three and the count function is going to look at my list and it's just going to count how many times whatever I pass to it occurs in my list so I'm asking there how many times does the value three occur in my list and it just occurred once because remember the elements were one two three four so three just occurred once great stuff so that is a method and we're going to see a bunch of methods most of what we're going to do are going to be methods but just think of them as functions that you put at the end instead of at the front we'll see some attributes later or properties an attribute just tells you something about that object it's not it doesn't have a lot of parentheses and you don't pass arguments to it it's just some property that we can elicit and we'll have a look at some of those a bit later so let's create another one that's a bit more complex I'm creating my other list very original there and it's a list object and I have these three elements in it and two are integers and one is a string if we look at the types though it's going to look at the whole thing and just tell me that it is a list slightly more complex again lists as elements of a list and that's called a nested list so I've got this outside set of square brackets but inside I have two sets of square brackets and each of those are lists so this is a nested list I've got list inside of a list and that's completely allowed no problem there whatsoever if I now do pass my nested list which my computer variable that I created by the way if I pass that to the length function what are we going to get to because it takes it from the outside and it's seeing those nested lists inside of it as a single element so I've got these two nested lists inside of there are ways that you can drill down and we'll get to that now we can certainly add to a list remember my list was just one two three four if I want to add to the end of it I use the dot append and what is that append it is a method so it's like a function and I can pass an argument to it so I'm just saying append which is good English for saying just add something so I'm going to add a five to the end of it and if I now look at my list lo and behold the five is now going to be added to it just a fun little function is the pop function and the pop function is going to pop off an element and it's going to pop off the last element and when I just do it like this it is going to show me the element that is popping off removing it's the five because if I now look at my list again I am only back to the one two three four excellent now we come to indexing and slicing indexing is a very important part of python and if you can think of we have a large data set full of health care information we only want to extract certain patients or certain data we don't want all the data when we analyze it we are after making groups analyzing subgroups etc so slicing or indexing let's use the term indexing very important so let's just recall what is held in my other list and we see it was one two three one two three but the two was a string and the thing about a list is that every element inside of a list has an address just like you have an address and we can reference those addresses if we want to get to those values those the values of those elements one of the things about python that you must learn is that python starts counting at zero zero based so the first element is actually the zero with element it has an address of zero the second one has an address of one the third one has an address of two and it's all a bit of fun so the three addresses there would be address zero address one and address two for those three elements and we use addressing by square brackets as well this is nothing to do with a list to this now square brackets means something completely different because I write the computer variable there my other list there we go square bracket zero it says give me the value that is an address number zero and because python starts counting at one that's going to be the first element inside of my other list and that is the one so if we want that element I have to pass the address one and that is the string two beautiful now instead of listing all the ones that I want I can also create this little range and this range says give me values from zero to two now we've got to be very careful here see the colon symbol there that indicates to python that we are asking for a whole range of values and from the zero value the value there is exclusive that's one way to look at it so it's going to go up to address number two but it's not going to give me address number two the other way to look at it well let's just stick to this I think that's the easiest way to have a look at it so it's actually going to be zero and one the two is not included the zero and one the zero element and the first element in our little list my other list it was the one and the string two so you can ask for this range I don't want you to try and memorize this now I just want for you to be aware of the fact that this exists and as we start using it you'll see how much sense it makes and actually how easy it is to use there's a little short hand that I want you to know about and that's the minus one that just indicates give me the minus one means the end element now it's easy to see here because we've only got three elements here but if you have an enormous data set you just want the last value it's very easy not to have to then know what its address is just type minus one and that's going to give you the last one inside of the list now remember my nested list it had two nested elements in it and so we can use these two sets of square brackets so the first one says give me the first the first element one with an address of one that means it was the second nested list and then give me the first value or the zero with the value in that so if I scroll back up to my nested list well what we can do let's just recall it here so I'm going to insert a new code cell I'm going to leave myself a comment to say recall recall my nested list and then just type my nested list there we go and if we recall it was a nested list of two so the first one the one two three was the zero and the first was the three four five so I'm saying give me that second one the three four five and in that the first element which was the three as simple as that now we can also use indexing to change the values so I'm saying the last one change it to four please remember I said it's not chiseled in silicone stone we can change the values and now the last one is one two four dot oh and you see I passed a little dot there I don't have to put the zero in python I can just say four dot it's exactly the same thing but now I've changed that value one other thing I want you to be aware of is the double colons and that just says up to number two but two again is excluded so let's say start at the beginning and give me up to two but exclude two so that's the the one the one in the four as we've rewritten my other list now for the other way around one till so that's going to give me the second element till the last element the two and the four great stuff an exercise that I want you to do is the following create a list with ten elements from zero to nine if you can look it up there's a very easy way to do that you don't have to type all the numbers but for now we're going to type all those numbers use indexing to print the first five elements of the list and use anything to print every second element now that I haven't shown you but maybe you can figure it out let's have a look at the solution so there's the solution my list and I've had to type all of them there as I said there's an easier way how to do that if I want the first five that's going to be zero to six remember because the six was excluded and the one I didn't show you if you wanted to look it up you would know how to do this but here's the solution you can use another colon and that colon is going to be two in this instance because it says start at zero and skip give me every second one and that's as simple as that now I'm going to get the zero the two the four the six and the eight now that was lists I've introduced you to lists and we are going to see lists again let's move on to tuples or tuples which way you want to pronounce it perhaps tuple because two sounds like tuple and it sounds like two but tuples are actually n tuples in where n stands for any number you want you get a three tuple and a four tuple that will be four elements anyway here we are creating a three tuple or then a tuple and what we tell python that it is a tuple or tuple by putting it inside of parentheses so here we have three five and the string two and there we go three five and two now these certainly have addresses but the beautiful thing about tuple is the fact that I can give each of those elements a name a computer variable name so I'm going to say one comma two comma three because I'm just separating those three computer variables assign my tuple to those so every element inside there is now going to have this very descriptive computer variable name so if I were to type one it's going to give me back that first element which was three and the same for two was going to give me back the five and three was going to give me back the string two so I can name tuples now I can also pass tuples to the length and that's going to give me the length of three I can use the same indexing one thing I'm not showing you there but I really want to tell you about it tuples are cast in stone they are what we call immutable whereas lists are mutable we can change the values of the elements tuples are immutable you cannot change them when you've created them here that value is going to stay the way that it is and there's good reason to have this functionality now we're going to move on to dictionaries now dictionaries are going to become very useful to us we're going to use them in a slightly different way it's actually just going to be what underlies what we're going to use but it's good to know just what a dictionary is and where we had square brackets for lists we had these parentheses for tuples we have curly braces for dictionaries and dictionaries are just pairs of values the first one of the pair is the key or the name of it and the second one is the value so these are key value pairs and we separate the key and the value by this colon symbol so on the left is the key on the right is the value so my key is language and it's a string key and my value is python and it's also a string value comma is the next one my next key is version and it's a string and my value is 3 which is an integer then I have another string as a key and another string as a value and you can mix these up of any types or almost any types so there's my underscore dict for my dictionary and I can get to the keys by using the keys method remember the method is like a function so I'm just saying my dict my underscore dict dot keys and it's going to give me back the 3 keys that I had the language, the version and the environment if I want the values I use the dot values method and then I see the 3 if I want the whole item back I just get dict dot items and I get language python version 3 environment notebook as this list I mean that makes it a list and this makes it little tuples so key value pair actually little tuples now if I just want to get the value back of one of the keys so the value of the version key was 3 so I just have to save my underscore dict dot get and I'm going to get that back there is the alternative syntax remember I said right in the beginning there are other ways to create lists instead of using square brackets it was just short hand notation and certainly here you see with dictionaries that those curly braces were just short hand notation it underlies or it will behind the scenes convert it to this format dict function so everything is going to go inside of sort of parentheses open and closing parentheses there you can see them and what we are passing to dict is a list and it's a list of tuples and the tuples are the key value pair so that's actually what's happening behind the scenes just if you're interested in that and there we can see what we've created next up by the way remember you can always hit pause and come back this is a long video and I said it's just a few minutes in the end it's going to be a long few minutes but still it is just a few minutes sometimes we want to iterate over code just repeat the execution of that code a couple of times that becomes very useful and the two ways to loop through things are these two concepts that I'm going to show you in Python the for loop and the while loop so let's start with the for loop it really reads like English I'm going to use this little loop to loop and it reads 0 to 9 now inside of it let's just break it down a little bit before we get to the proper structure there's this range function it's a function inside of Python and I'm just passing one single value to it if I pass a single value to it it's going to start at 0 and go up in steps of 1 so it's going to be 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 it's going to stop at 9 it's not going to include 10 but if you think of it as 0 you are getting 10 elements back so that's the other way to think about this that indexing I said before but let's look at the whole for loop so it's a for i in a range 10 print i now for means we're going to loop through this i is just some placeholder I'm using now that's got nothing to do this is internal to the for loop so it's just a placeholder saying for i in range 10 and remember range 10 is going to create this list for me which is 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 it's going to loop over each of them and then print that one to the screen let me show you what it looks like it'll make more sense and then we get the 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 everyone is printed as we loop through those 10 elements so another way to make it very useful if I say my underscore other underscore list remember that was 1, 2 and 4 I'm going to loop through these elements in my other list and print them so for element now element is this some word I came up with is this some placeholder inside of this for loop outside of this for loop nothing is this this placeholder I could have put i there as well or some other word I'm saying for element in my other list print element I'm going to show you what it looks like before we get deeper into the construction of this for loop and there you see it's just going to loop through print element and each element is just a separate one as I say that element word has got nothing to do with it I could use any other word in there the loop counter basically but look at the structure of the for loop I'm saying for and in those are very important so I've got to have the for and I've got to have the in I've got to have some counter name just some name that you choose and then the thing that you that contains many things inside of it so my other list contains three it's a list and contains three things in it then I've got to put a colon and if you put that colon and hit enter you're going to get this white space here see that white space the P is not under the F that's very important in Python this is set by default the number of spaces it puts there in other IDEs that's development environments programs in which you can write the code you can set how many spaces are in white space this is all done by default here in our Google colab but it really means something to Python that white space I can't delete that white space then things are not going to work for us okay something else I could loop over are the keys in a dictionary that is something that has elements in it and I can always loop over elements so for key again that's just a word I chose for key in my the keys that's going to be the three keys print the key so that would be another way just to get back those three keys okay the while loop is going to execute something while something while something holds some constraint holds so I'm going to create a computer variable called I remember that has nothing to do with that name that I choose to loop over the I there so I'm going to create a computer variable I and I'm going to assign to it the integer 0 then I'm going to use the while loop and then some condition because I want to I'm going to keep on doing this loop until until this condition is no longer met so while I is less than 5 print I and then I equals I plus 1 so let me just show you what it looks like so 0 1 2 3 4 so at the beginning at 0 is 0 less than 5 yes it is again the colon and see the white space it will be created automatically for you print that I at that moment I is 0 so I'm printing 0 and then increment I by 1 so I is 0 add 1 2 it is 1 reassign it to I now I is 2 now we loop back is 2 less than 5 yes it is I can continue with this loop until I get to 5 is 5 less than 5 no it's not Python is going to execute that while loop and it's done it's done so here's your exercise I want you to loop over the following create a computer called my string and assign the string object this is easy full stop to it and then I want you to loop over each character in the string and print it to the screen but I want to give you some tips remember the range function we just said range 10 and then gave us 1 2 3 4 0 1 2 3 4 5 6 7 8 9 10 if I say put 2 numbers in there it's going to go from the 0 to the 5 but remember the 5 is not included so it's going to be 0 1 2 3 4 so if I pass that to the type function see that it's a range object but I can loop over this range object just as we did with the 10 so for n in range 0 to 5 print n that's going to print for me the 0 1 2 3 4 that's 5 elements but it you know it excludes that 5 because it starts counting at 0 so that's one tip for you the other tip for you is remember if I were to write the word Python inside of quotation marks it makes it a string I can index that string and so I can say give me the last one back that's just the n so the solution I want you to come up with must include these tips please so let's have a look at the solution so I'm going to create this string object my string let the computer variable my string pass to it assign to it whatever is on the right hand side on the right hand side is the string this is easy full stop we're able to hold the length the length of that so this is easy let's do this let's just type my string length as well so we can just see how much how many it counted my string length so if I do that I'm going to get 13 there's 13 characters inside of that string so now I can just loop over each of those remember when I showed you the negative 1 it means each of those characters outside of the string actually has its own little address so I can say for char char is just the word I came up with you can have used i again you could have said for i in range and that's 0 to my string length and remember that's 13 so it's going to go 0, 1, 2, 3, 4, 5, 6, 7, 8 12 up to 12 the 13 is excluded but if you start counting at 0 that's the 13 and then I want you to print my string and then address each of the characters so if you print that out we're going to say this is easy full stop with the spaces in between all of them great stuff now in the while loop we used conditional so I just want to show you something about conditionals and these are if and else is the one that I want to show you again this is just to show you that this exists if any of this lecture is just too strange for you don't worry about it as we start using it it's going to be easy if you learn a new language and I just take some random value or word I should say from the dictionary it doesn't make sense and until you've used it a couple of times it's difficult so this is just introducing you to some concepts so I'm going to create a computer variable called i and I'm going to assign the integer 23 to it and now I have this if else concept going on here so the if i is less than 20 colon because if I hit enter I'm going to get this white space it means something to python so it's going to look at this conditional is i less than 23 what is i it's 23 is 23 less than 20 no it's not under those circumstances what comes here will be ignored and then it'll jump to the else statement and this else if I hit enter here I'm going to get that white space so I can't start typing else there you physically have to remove that white space because the E and the I the E and the I the E of else and the I of if have to be they have to be right under each other so if i is less than 20 print smaller than 20 else colon so that there's white space very important to look at the structure come back to it and look at it often print equal to or greater than 20 so indeed that is what is going to happen equal to or greater than 20 that smaller than 20 was not going to execute because the conditional was false 23 is not less than 20 now you can keep on with these you can add more conditionals as you go and in between we put in this L if it's short for other languages which use the full else if but in python it's just L if so let's make a equal to 2 assign 2 to the computer variable a we assign 1 to the computer variable b now I'm going to say if b is greater than a colon print b is greater than a do you think that's going to happen is one greater than 2 no L if a equal equal b now that's a new conditional we've never seen that one before so the double equal symbol there that is a conditional it says is what is on the right hand side to me equal to what is on the left hand side of me and it returns a true or false all these conditionals return Boolean values we haven't discussed those but it says true or false so is on whatever is on the left of me 2 equal to what's on the right of me 1 says 2 no it's not but if it was is what's going to print a and b I equal and then in the end of all of these things fail you know do something else and that is print a is greater than b is 2 greater than 1 yes it is I guess which one is going to be printed to the screen a is greater than b and it's a beautiful very simple example for you to figure out how if L if an L statements work so an exercise for you it's a computer and I've put in example exercise there for you because these last two things I just want to make some something fun it's a famous computer task and if people go for interviews they might be asked just to create this just to check if they understand the language and but it's a fun little thing so it's a task we're going to say take the numbers 0 through 21 in this case and for each number if it is divisible by the integer say 3 and divisible means there's no remainder it'll go into it completely then print the word foo f o o and if it's if it's not if there's a remainder print bar and this is the way one way to go about it I'm just showing you one way to go about it for I in range 22 so that's going to be 0 1 2 3 4 5 up till 21 which is exactly what we want if I percentage 3 equals equals 0 so remember the percentage means that I just want to just want a remainder and then something is divisible by the remainder is going to be 0 so it's going to start at 0 and it's going to say is 0 divided by 3 does that have a remainder of 0 if so print I that number that we are at the moment and and then print foo now you've seen something new with a print function here as well I can concatenate stuff inside of the print function so here I'm printing the value that's an I at the moment it'll start at 0 then go to 2 then go to 3 then go to 4 and then a comma and then a string so I can with commas I can concatenate lots of things in a string and I'm concatenating it by a little space a colon symbol another space and foo just so the prints nicely to the screen and if the remainder is not 0 so if it's not divisible print the value and then print bar so it's going to run down all of those and just do the foo bar thing for us so foo remember we said if it has a remainder of 0 and otherwise bar so 0 1 or 2 won't be 3 is divisible by 3 with a remainder of 0 6 is divisible by 3 9 is divisible by 3 so simple as that the last little fun thing I just want to show you something called list comprehension and list comprehension is like a little foo loop but we put it inside our square brackets this is the sum advanced stuff just some fun for you to have if you're interested in it my underscore set equals i for i in range 51 so i for i in range 51 inside of square brackets that's going to be a list object for me and I'm just going to print those first 5 elements 0 1 2 3 and 4 I could say i for i in my set if i percentage 3 equals equals 0 now I'm getting the same sort of thing in a much shorter sentence now if you look at that that is a bit advanced that is like some hectic syntax or grammar inside of spoken human language but I want you to look at and see if you can dissect what is going on there and as I'm just showing you this for for a bit of fun and let's do the following print the square of the number if it is divisible by 5 so i squared for i in my set if i percentage 5 equals equals 0 that's how I would construct that so now it's going to print it's going to first select only the values that are divisible by 5 and then it's going to square each of them I hope you find that as interesting as I find it but as it's not part of this course I just want to show you some of the power of Python and how you can construct something very complex basically so take all these values see if they are divisible by 5 print them to the screen but square them at the same time that's a short line of code and once you get the hang of it it's actually quite easy so that's my couple of minutes introduction to Python again maybe I have thrown the dictionary at you but that's not the idea you don't have to remember any of this because it's only when we start using it that it starts making sense that it'll get into your head and you'll see just how easy it was for me just to write this little notebook it's from a long time of usage not from looking at it for the first time so don't despair all of this is easy and as we start using it to do what we actually here to do and let's do some statistical analysis on healthcare data you'll see just how easy all of this really is