 It is time to get to know Python a little bit. Of course, this is a single lecture. I can't show you everything that there's about the language, but I want to show you the basics so that you're familiar with at least some way of the structuring of the language so that we can continue with this course. Here we are in our next notebook, the Python language for data science. Now, there are, as we mentioned before, many languages for data science, but Python really has taken the lead. There are many versions of Python. New versions are released every time. In the time of this specific recording, version 3.10 is getting ready for release. Now, there is a lot of version 3s that will work for us. There wasn't all the version 2, 2.7, which until very recently was still being actively developed because so many companies still have legacy Python software, but that has now lapsed. So certainly, there's no further development or support for any version 2 of Python. So everything is about version 3. And we already, as I mentioned, going for version 3.10. In the first video lecture, we've spoken a little bit about Python and where it comes from, some of the other languages, and then the fact that there's so many packages that we can install and then import to expand what Python can do for us. We also spoke about some of the tools for Python and that we have settled here on the Google Colab so that we don't have to install anything on our local system. We can all run it in the Google Cloud. And of course, by the access to the internet, it really doesn't cost us anything. Now this Google Colab or Jupyter notebooks then on which it is based is by far not the only coding environments that we can use for Python. And there are many others, PyCharm, Spyder, Microsoft Visual Studio Code, specifically VS Code, has become very popular for the development of Python apps, programs, and of course, even data science. What we like about this notebook format though, is that we have the ability to have these cells and we can just write normal sentences. In between and we can format it very nicely as you can see here with the table of contents on the left being picked up by these cells that have these hashtag symbols or pound symbols that allow us to determine how large that text is going to be. I remind you, in your Google Drive, if you don't see Google Colab, just go to this website, colab.research.google.com, once you've signed into your Google account and then the Colab will be available to you alongside Google Docs, Google Sheets, and all the other Google apps. Now one way to get familiar with a language is usually to write a little program usually called Hello World programs. But of course, we want to do data science. So that's not really going to suffice for us. What I like to do is just to introduce Python as a very large and complicated calculator, the calculator or physical calculator calculated on your phone. We can also do that right here in Python. And I think that gives us a very good introduction to the language, and especially looking forward to the way that we do use Python in data science. So we're going to start with some simple arithmetic. And you can see a little table there, table one with all our expressions, the operator and example and what the results are going to be. And first of all, this is a Google Colab notebook. So you might wonder how did I render that? So let's double click on that cell and you can see exactly what code went in there. And it's very easy. I wrote table one, I put a space. And then I use these little dividers. So look for these symbols on your keyboard, as long as you line everything up like this, it's going to be rendered to the screen as a nice little table. So you get additions, subtraction, multiplication, division, integer, division, remainder, exponentiation. And those are the little operators for that plus minus. Of course, we don't have multiplication on a keyboard. So we use star. That's a shift eight on most keyboards. We don't have divide by. So we are going to just use the fourth slash four division into the division. If we divide a number into another number, we just want the whole that remains. So 10 divided by three, a whole is three, three times three is nine. And then for the remainder, if we just want the remainder return, which will be one in this instance, we sense a symbol. And then to take a power three to the two to the power four, that's two star symbols in Python. So let's just do a little bit of addition right there. I've created a cell there, you know, just in between click on code, a code cell will open and I've typed in two plus two. So let's do a brand new one. So I'm going to say code. And I'm saying two plus two. Now here I've put no oops, two. There we go. I've put no spaces, but just putting these spaces just makes it easier for us to see what to do. And that's all I did to space plus space to two plus two. And now what you'll see in this code here is a little hashtag symbol. Now that hashtag or pound symbol has nothing to do with the one that we get when we do text cells in a text cell that is going to determine how big our text is, when it's inside of code in many languages, it is used for a comment. So in every line of code, Python will look for a hash tag symbol or pound symbol like that. And anything after that on that line, it will ignore. So why do we put that there? Well, it's very useful to leave little code comments. There's a law that states that you read your own code six months later, you'll recognize it as your own code. And especially when we share our code with others, leave those little comments there to tell others and yourself your future self why you wrote that line of code. So anything after a pound symbol or hashtag symbol will be ignored by the Python interpreter. So we've had two by two then and what our code comments says, hold on the shift key and hit enter on a PC or Linux machine or return on a Mac. So I'm going to do just that, hold down shift, hit enter, and I'm going to see the result. Now of course what we haven't done here, remember they are up there, we have to connect to the Google cloud so that it spins up an instance of Python and it runs Python. You can see that the compute engine back in Python three has been connected. Now it's connected. Now this code cell will execute and we see two plus two, we see the result below that is four. Remember I don't only, I can do something other than holding shift and enter or shift and return, I can hit that little play button there and we're going to get exactly the same thing. So that plus symbol for addition and we can of course add more things two plus two plus 10 and shift enter, shift return and that's going to give me 14. And of course there is the subtraction that's just a minus key, so it's seven minus four and of course that's going to give us three or indeed if we have 10 minus three minus four, of course that's going to give us another three. So let's move to multiplication. So multiplication as I said before that's the star symbol because we don't have a multiplication key on our keyboard. So three star four, shift and eight on most keyboards, have a look at where it is on your keyboard. I bet it's at above the eight key and three times four is 12 and as with addition we can do more than just two numbers three times four is 12 times two is 24. Division once again that's the fourth slash so I'm going to say 10 divided by two, so 10 isn't the numerator, two isn't the denominator. So there we go, 10 divided by two is five but you see something different there, you see a five point zero, not just a five, so there we say three times four times two that's 24. There's a difference between that whole number the integer and this decimal value here called a floating point number and that is the internal representation of numbers that most computer languages have and as soon as Python sees a division it's going to give you a decimal value. So let's do 10 divided by eight, we know it does not go into 20 without a remainder, so we're going to see the 2.5 there. If we were interested just in the two the whole number part of this division we can use two fourth slashes and that's just going to return for us the two and if we're concerned about what the remainder is, remember eight times two is 16 to get to 20 there's a remainder of four, we can just use the percent symbol, so 20 percent eight is going to give us the four. Power is very easy, remember two to the power three that means two times two times two three times and that's obviously gives us eight and in Python that's the double asterisk double star symbol two, shift eight, shift eight and three, two to the power three and that gives me eight. One thing I want you just to cast your mind back at school we had the order of mathematical operations so division and multiplication came before subtraction in addition so just remember those things it's something very different depending on what gets executed first if we have three times three plus four times two if we add a three and four together that's going to give us seven times two is 14 but that's of course not the way that it works multiplication comes before addition so it's actually the four times two first which is eight plus three is 11 so let's have a look at that if we just type that into Python and Python's going to obey that order of mathematical operations so it's four times two is eight plus three is 11 of course if I wanted to force the fact that I wanted three and four to be added first I put those in a set of parentheses so I'm not going to put too many exercises in this course but just the end of the beginning I might put one or two and what I want you to do this do six plus four first times ten and then divided by two how would you write that in code and in case you are you know you can just add it there I've left a little cell for you there to enter the code but if you're curious you just toggle open that solution you'll see how it's done next up is the comparison operators and we're going to use them quite a lot when we tease out certain subsets of our data we might only be interested in values you know observations where a certain a certain variable has a value more than some set value think of age where you might want to work with participants that are younger than a certain age or older than a certain age and we're certainly going to use the comparison operators quite a bit and you can see all of them there less than so two minus four that'll be two two is less than four four less than two that's going to return a false that is certainly not true you get the greater than less than equal to greater than equal to so those two also allow for equality is equal to is a double equal sign because the equal sign in most computer languages is not the equal sign that you think about when you think about mathematics it's actually what we call an assignment operator so if we want to know if the left hand side equals the right hand side we put two double equal signs so we'll see four equals equals four dot zero one is an integer one is a is a decimal value but they both the same so that's going to return to but four equals equals two that's certainly going to return a false there and they're not equal to is exclamation mark equal that's not equal to so four not equal to two certainly that's true four not equal to four well that's false so let's have a look at it let's see if two is less than four so we're going to type two less than symbol four and what these comparison operators do they're always going to return some Boolean value and that refers to it being true or false that's the only thing we can get back and the keyword for true in and python as an uppercase t true and if we say four is greater than two yes it is greater than two we see that as well as four less than or equal to four so equality is included there so that's going to return a two for us it's four greater than equal to four well that better return true as well because four is indeed equal to four and here we have that double equal symbol I'm saying I'm asking is that the comparison is the left hand side equal to the right hand side and it's going to return true for me as well as four not equal to two well yes of course it's not equal to two and we're going to return true the four less than two that's going to be false two greater than four that's going to be false four less than or equal to two that's going to be false and you can just set up these and play around with them to your heart's content four of course is not equal to two and four is equal to four so that's going to return a false so please keep in mind these we're going to use them as inside of conditional statements quite a bit so functions are keywords in python they're built into the language or into one of the packages that we're going to import and we can just use them and they take an input that we call an argument and that argument always goes inside of a set of parentheses so there's the print function it's a keyword in python it exists there we don't have to create it or anything we because it's a function immediately followed by a set of parentheses and then inside of those parentheses go an argument the arguments tell the function it gives the something function the function something to work on and the function is designed so that it'll have an output and when I say print and then put this this is easy and you can see I've put those inside of single quotation marks now in python we can use double quotes or single quotes we usually stick to single quotes although you'll see double quotes quite a bit and the interchangeability of the two means that we can use one set within another set if we want that to be printed out what it means though it signifies that those letters words this is easy full stop as a string so we get whole numbers which are integers we get decimal values that are floats and then we get these letters or words or paragraphs inside of quotation marks those are called strings so you can well imagine it's just going to print to the screen this is easy great data types so most of what we work with in python is a certain type and we can actually use the type function there is a type function so I'm just gonna write type and then I'm passing an argument get used to to that vocabulary type I'm going to add or I'm going to pass the argument three to that type and it's going to tell me what representation python has what data type python holds for that symbol three and it says int and it's short for integer so three just on its own is an integer if I pass the value three dot o as argument to the type function and the type function takes that argument it knows what to do with it and it's going to output that this is a float floating point number that means in computer language speak that means a value with decimal a number of decimal values so here we go I'm going to put letters inside of quotation marks and you'll see that that is going to be returned as a str which is short for string so everything really in python or most things in python python has a type so what if I do eight and I put it inside of quotation marks well that is still going to be a string which means I can't do any mathematics with that eight because it is not the number eight it is just a character a string eight one thing we can do with strings is we can concatenate them with a plus symbol so look at the string python and then a space plus is and then a space plus a and a space plus powerful in a space plus language in a space plus a full stop so if we concatenate all those together we're going to get this whole sentence python is a powerful language it's simple as that that's how we do what of course what we term string concatenation which means I won't use this too often but you could do this have a string this the solution to one plus one so that's my string and I'm going to concatenate to that one plus one that I'm passing this one plus one to a function called str and that's going to take what I pass to it and change it into a string and then plus the full stop so I'm using string concatenation there but in the middle of all of that I I do a true calculation one plus one the result is two and I'm going to convert that into a string so that that it's part of this concatenation and then you can say the solution to one plus one is two as you would expect so the math module let's have a quick look at that now the math module also called the math package fortunately for us it's not something we have to download and install and this is part of python but we have to import it into the base into core python and there's a bunch of functions new functions inside of the math module that is not available in just basic python and the way that we import any package is we use the import command or keyword so it's import space and then math and once we do that all the functionality that someone set in design inside of this package is now available to us as simple as that the only problem though is we have to refer to the functions inside of that differently than we would refer to functions that's inside of this python I could just use the print function I could just use the str or string function but here I have to refer to the package that it came from so this the the keyword pi pi and as you can imagine that's going to hold the value for pi to some decimal value but it's it's a it's an object inside of the math package I have to say math and then in python we use dot notation so it's math dot pi it tells us pi lives inside of the math package and I have to say math dot pi and that's purely because how I imported it I just imported math and therefore I have to do this math dot pi and if we do that we'll see the value there to at least the some decimal approximation and then E Euler's number so math dot E gives me Euler's number there now there's also the exponent reminder if we if we take anything to the power one it just remains that one thing so exp the exp function if I pass the element one to that that's also going to give me Euler's number just just of a little bit of interest now there's a bunch of trigonometric functions as well in the in the math module math package so I'm going to say math dot sin for sine and then I'm passing math dot pi divided by two so that's exactly what we have here the sine of pi over two so what is pi over two remember that's 90 degrees what is the sine of 90 degrees while the sine is opposite divided by hypotenuse and the result of that should be one and we see indeed the result of 1.0 what is the cosine of 90 degrees or pi over two so we'll say math dot cos for cosine and we pass the argument math dot pi divided by two that really is as simple as that other than the result that we see because the cosine of 90 degrees is zero but we see a result that's not quite zero well this has to do with how correct a computer language can be or your computer's CPU can be as far as decimal places are concerned because remember it can't have an infinite number of decimal places and pi has an infinite number of decimal places so somewhere along the line is going to be these truncation errors but if we look at the number we see it's 6.123 blah blah blah e minus 17 that means 6.1 times 10 to the power negative 17 so that's going to be a zero comma and then 16 zeros six and something as small as that what python's trying to tell us that this is zero so something with you know so many zeros after the decimal place it's just a truncation error so the way computer languages work and you've got to get used to that and we see there that this really means zero so i want you to do the exercise of the tangent of pi over four and the tangent that's just going to be math dot tan math dot 10 but if you want to have a look at the solution you can see it there logarithms and just you know if you had log logarithms at school remember log is always got a base a and b and that equals c so if you were to say a thousand is 10 to the power three that's what it means log base 10 thousand what that means just this little bit log base 10 of a thousand it asks 10 to the power what gives me a thousand well 10 to the power three gives me a thousand so log base 10 of a thousand is three and the function in the math package is math dot log 10 so if i said math dot log 10 a thousand i'm going to get back 3.0 because 10 to the power three is a thousand we also get that natural logarithm that's where the basis oil is number e so the function for that is just log so math dot log and i pass math dot e to that and of course that's one e to anything to the power one is that something roots the square root is probably what we would use most often and that's also in the math package that's math dot sqrt and i'm passing 256 to that and as you can see it's the square root of 256 which is 16 so if i say math dot square root sqrt pass the integer 256 as argument i get the value back 16 now i want to show you some alternatives and this is what we call namespace abbreviation because all the functionality that come inside of a package that's under that packages namespace inside of python i don't want to get too technical about that but we can give that namespace and abbreviation so i can say import math as and give it an abbreviation such as m that means i don't have to say math dot sqrt i can just say m dot sqrt so i'm using an abbreviation and that's quite commonly in python so we don't have to write out the whole word the name of that package to get to its functionality there's one other alternative if i really don't want to use that namespace full name or namespace abbreviation i can import the functions that that are inside of that inside of that package quite directly so instead of saying import math i say from math import sine cosine and pi and i put commas in between those so those are the keywords and functions that we've seen so i can import them directly this means i don't have to refer to that namespace anymore i can just say the sine of pi over two i don't have to say m dot or math dot sine and math dot pi i can import the functions that are inside of that module so you've got to be a little bit careful when you do that because sometimes when these packages are developed and the developers will use a keyword that's a function that already exists inside of python and that's going to override that function and it can lead to a bit of difficulty so this is something i don't ever want to use so from math import star and that star says import everything inside of the math modules module so that i can use it directly so now i could just say once we've run that cell at least i can just say log 10 of 100 so 10 to the what gives me 100 worlds 2 so that star but once again i try to be more precise either just import the namespace with an abbreviation or the nameless specific functions that you want to import so that brings us to this concept of a computer variable now what a computer variable is it is a name that we give to something it is our choice to give that name to something we call that a computer variable name once we have decided on a name and we type that name what python will do is it'll reserve a little space in memory call that little space in memory give it a name and that's the name we give it and then it can store something inside that little piece of memory now we as i said we come up with that name and we just got to be careful what we name things though there's a few conventions that we try to stick to and you can see the bullet points there user name that starts with a lowercase letter make the name very descriptive and use the most popular form is using these underscores and using those underscores turns this name into something that we refer to as snake case so i'm i'm going to use a computer variable name my underscore variable underscore name that's very descriptive of what it is and there's no spaces in these spaces are illegal characters remember python sees a space as something very specific in the language so that a snake case an alternative with would be a camel case and that's where we don't do underscores we concatenate the whole thing together and except for the first one every subsequent word starts with an uppercase but that's not so common or popular in python python this snake case is much more popular and then one thing absolutely not to do is to use a python function or python keyword so what i've done here is this little line of code that we can see here and as you can see i've left you lots of code comment there so i have this value 3.0 and this value 3.0 it's in what we would refer to as an object that object has a type and we've already seen 3.0 would be a type integer and i'm going to assign the value of an object i'm going to assign this object to a computer variable that's how it works and that's what this equal symbol is in the computer language it's an assignment operator not an equal equal symbol so i'm saying take 3.0 and assign it to a little space in memory and this 3.0 this object that is of type integer is stored in this little part of memory that is called my underscore value and as i said my underscore value that's your own choice what you call it and we do this assignment now once we've named this little part of memory it now contains an object of a certain type and value so our object is going to be a type integer type float and it's going to hold the value three i can just recall whatever's inside of that memory by just referring to the computer variable name so i can just type my underscore value and it's going to look in a piece of memory and bring out what is inside of that 3.0 and if we look at the type of my value we'll see it's an object of type float and 3.0 indeed is a decimal value or floating point value what we can do with computer variables is continuously update them so i'm going to create this computer variable that i just call i i it's very common if you want to iterate over something i'm going to assign uh integer to it and that integer it's an object and it has the value zero and i'm going to assign that to the computer variable i and then on the next line i just call that piece of memory call i and the result is going to come back a zero now look at this next line of code which might be a bit confusing i equals i plus one so the mathematics that's not going to make sense that's because that equal symbol is an assignment operator it says whatever is to my right assign that to whatever is to my left so it's going to look towards its right first and that's i plus one at the moment i holds the value zero so it's going to add one to zero which now gives me one and now it assigns it to that same little piece of memory which means it has to overwrite what was in there before so in in that piece of memory before was a zero i add one to that that's one and it's now going to overwrite that value inside of that same piece of of memory called i and if i call i now zero plus one is one and i'm going to get the one back now we usually use a shorthand notation instead of saying i equals i plus one we use this little shorthand i plus equals one it's just what we call syntactic sugar it's just a little shorthand notation i plus equals one that's exactly the same as i equals i plus one can you guess what the result is going to be well at the moment inside of i is the value one it's the right of this assignment operator we have plus one so one plus one is two i pass that to this piece of memory overwrite this piece of memory and the result is now going to be two it's a little exercise that you can run through if if you if you want to and you can see the solution there next up we're going to talk about collections because up till now we've had a single string okay of course we've concatenated some of them but all our values our integers our floating point values have been single values but what if we want to store more than one value well the most common object in python is called a list object and a list object just goes inside of a set of square brackets so yeah i only have a single value 10 so i might as well just have written the 10 but it it now signifies that this is part of a list it goes inside of a list and how we separate the elements inside of a list is sort of common so yeah we're passing five elements to this list object and now if i were to execute that code i see this list object one two three four five now it needn't all be the same type i mean one is an integer two is an integer up to five is an integer i can also have a list and it has different types one is an integer two as we've written it out there's a string three dot or three dot o we don't have to put the o in python by the way you can omit that but that's still is very different that is a floating point value but i can pass them all to a list now let's see if we pass this list to the type function what it returns and it's going to return that that object is a list object at the moment it doesn't care what the individual elements are the object as a whole is a list so i've taken this list object and and and we're just printing it to the screen it's still a list object but i can assign this list object to a computer variable and very neatly i'm going to give it the computer variable name my list and i'm going to assign to that this list object and this list object has four elements separated by commas and they're all integers if i now call my list looks in the memory sees there's a list object in there with certain values and it prints it out to the screen and that means we have this little function len for links and that's going to return for us how many elements are in my list so if i pass my list as an argument to the len len function it's going to return four because there are four elements inside of there that brings us to something called a method now up till now we've only seen functions so i'm just write the word the keyword put a set of parentheses and off we go we pass an argument to that function it does something with that argument and spits out something but we also have methods and attributes now some there's a little bit confusion about what is what and people tend to use the terms interchangeably it becomes quite technical and i want to get into that for the sake of this course anything that gets a function that gets passed after a computer variable i'm going to call a method so this is the counter method but you can see it still has a set of parentheses and i'm still passing an argument to it but it is after a dot and after some object that i've already created given a computer variable name i'm going to say my list dot count three so i'm going to call this a method instead of a function it's just going to be the convention just to keep things very simple and three a fast three there now what this count method does it counts how many times the three occurs in my list and only occurred once my elements were one two three four so it's only going to occur once so just put the one there now as far as lists are concerned we can also have nested lists or some languages we refer to it as list a list of lists what you can see is the outer set of square brackets to note this is a list but inside of it i have two elements and both of them are lists so both of them are sub lists of the outside lists and they contain elements one two and three and the second one three four and five i'm going to assign that to the computer variable named my underscore nested underscore list and you can see that now if i pass that to the length function what do you think it's going to say what is going to say two there are two elements inside of this whole object my nested list is a single object and it contains two elements and that's what we can see there now we can look at some more methods the next one is the append method so i'm saying my list remember that was just the elements one two three four and i want to add a value to that and that's going to be the append method so i'm saying my list first a dot append it is like a function though so parentheses and i'm passing five to it so append meaning add to my list if i now call my list we're going to see that there's five elements now it's appended the element five to my list so now i have elements one two three four and five what the pop it does the pop method so my list my underscore list dot pop it's going to remove the last element and return that element that it removed so it's going to look what is last it's the five it's going to return that it's going to pop it off and i see the five there that's what gets returned look at my list though if i look at my list i see now one two three four the five was removed so let's just look at something called indexing or an address so i see three elements of my list 10 20 and 30 and what we're going to see is they each have an address called an index so let's call it my other list 10 20 and 30 and i print that out 10 20 and 30 now each of those as i say have an index now one thing you must get used to in python is that python is what we call zero indexed python starts counting at zero so my first element 10 there is actually my zero with element carries an index of zero 20 is my second element but it carries an index of one it's the first element and 30 is my third element but it carries an index of two which is my second element so i have a zero with the first and the second element there so really it starts counting at zero now the way that indexing works instead of a set of parentheses we use a set of square brackets so i'm saying my other list we know it's got three elements in it and i put inside of square brackets i want the zero with all the first element so if i do that it's going to return the 10 for me if i put a one in there it's going to go for the second element which is 20 and with the third one if i put in two there that's going to be my third element my last one and that gives me the 30 so very important to remember that python is zero indexed and there's good reason for that although other languages say for instance julia is one indexed there's a little shorthand if you have a very long list and you don't know how many elements are in there you couldn't be bothered to get the length of that the length using the length function you can just say minus one the minus one refers to the last whatever it is and the last one of course is 30 now if we have indexing we can also do what we call slicing so instead of just asking for one element back i can ask for a couple of elements back and if i want a range of them one after the other i use the colon symbol notation there so zero colon two she might think that means the zero the one and the two that's what you might think the step size is always one so zero colon two give me element zero give me element one give me element two so you think it's going to give you all three of the elements but you'd be wrong because in python this last value is not included so that actually only says the zero and the first in other words the first on the second element that's not going to include that two it says up till two but not including two so let's just go back to my nested list and so i've got two subsets so if i want to get to one of the values in there i've got to use this notation here i've got two sets of square brackets that first one says one that means it's actually the second remember so it's going to be the second sublist the three four and the five and inside of there the zero with the first one which is the three so this should give us back the element three just as we said now we can overwrite elements inside of a list so my element and my other list i'm going to index the last element and i'm going to assign to that the value negative 40 remember equal is assignment so it's going to overwrite that last value which was 30 now becomes negative 40 last little thing that i want to show you here it's just the double colon symbols i can say two till the end two colon colon or one colon colon as we have here and remember one would be the second element so it's going to give me from the second element to the last element 20 and the negative 40 so indexing a little bit difficult in the beginning but if you've used it a couple of times it really becomes second nature and i've left a little exercise for you there the second type of collection is called a tuple instead of going inside of a set of square brackets a tuple goes inside of parentheses so i'm passing three elements to this tuple three five and then the string two and there i go assigning that to the computer variable my underscore tuple we want to do indexing we can do that of course we can ask for the last one which will be the string two now the nice thing about tuples is that they are immutable immutable means i can't overwrite them i can't say my tuple minus one equals four now it's just not going to work tuples are immutable once you've created them you cannot change the actual values another nice thing about them is we can we can name the elements not only do they have an index value so the three would have an index zero five index one and the two they index two i can actually name them and here i've given three computer variable names named one two and three equals my tuple what i've done there is i've just named each individual one so if i now call one this is going to give me back that first value which was three so from time to time uh we do use tuples and they become very useful the last type of of collection inside of python are dictionaries the last type we're going to talk about anyway are dictionaries and they go inside of a set of curly braces and they are key value pairs so there's always going to be a pair of elements and they're separated by a colon the first part is the key and the second part is the value so i'm going to assign that to a computer variable called my underscore dict and then inside of the set of curly braces i've got three elements so you can see the comma there there's a comma and there's a comma so i've got three elements and they're all key value pairs so the key is language in this instance it's a string and its value is also a string the value is python a version also string colon three so the value is three the key is version uh environment colon colab both of them are strings so i have these key value pairs so i have this uh method called keys so i say my underscore dict dot keys it's a function so it has the parentheses it gives me the three keys language version environment and if i want the three values i use the values uh the values uh method there and if i want uh both of the things the keys and the values i can just say use the items uh the items method there so i see language and python version three environment and colab and it's giving me back those almost as little tuples as you can see there so if i want one of the the values there i can say my underscore dict dot get and then i'm going to pass the key to it as argument so it's going to get me back the value of that key value pair version was the key it's a value was three so let's just have a look at another one just to show you that uh you know we can do uh there's another way to construct these dictionaries and instead of curly braces we can now do this use the dict function and what that is going to take as an argument is a whole long list and it's going to be a list of tuples so i can say language comma python inside of parentheses making it a tuple version comma three point eight making that a tuple an environment comma and i've made a list as the value so you can see we can get quite creative as as far as what we do there now we want to control the flow of how the language is executed python is an interpretive language so it's just going to go line by line and execute our code so let's create a little loop so that the uh the code can run over and over again i just wanted to show you this little function quite useful the range function and i'm going to pass a single argument to a 10 now what the range function does it's going to count up to a certain value not excluding that value as usual in python and with the range range function if i don't tell it anything else it's always going to start at zero and it's always going to increment in steps of one so that range 10 is going to give me back this range object starting at zero ending in 10 but 10 is not included let me show you this little for loop i'm going to say for so my keyword in python space i my i is just a placeholder counter for i in range 10 now range holds the values 0 1 2 3 4 5 6 7 8 9 so for i in that range so the i is just going to iterate through all of those 10 values 0 1 2 3 and then a colon and as soon as you put that colon and hit enter return python will put in that little white space for you there white space very important that counts as code in python print i and what we're going to do there is see a new line printed 0 1 2 3 4 5 6 7 8 9 so i think you can see there what a for loop does it's very expressive and you can almost guess at what that code is going to do just by looking at so that's a for loop now let's just recall my other list what was inside of there 10 20 and negative 40 so that i was just a name i decided on i but you can use anything else so this time instead of i i'm using just a word any word it's not a keyword in python and so for element in my other list so i could also just have said for i in my other list print i but it's more expressive here to say element because it sort of tells you what it's going to do print element so it's going to print first to the screen the 10 the 20 and the negative 40 we can also do something like this for key in my other dict dot keys print the key so the my underscore other underscore dict dot using the method keys it's going to give me the three keys and then this for loop i'm just going to print each of those other than a for loop we also get a while loop and what we do here was we start a counter again i've chosen i you can choose a word that that makes sense to i'm sitting at i and i'm saying while i is less than five so this is a conditional i'm asking something to be returned true or false and it's going to continue looping as long as it as this conditional is true so we're using a comparison at the moment at zero is zero less than five yesterday's it's going to run through that loop so what it does in that loop it prints i to the screen then it increments i remember i equals i plus one so now i is going to be one goes through the loop again is one less than five it's true it goes through the next loop by the time it gets to five is five less than five that returns a false so the condition now becomes false and it's not going to loop anymore so if we run that we're going to see zero one two three four it's not going to print five because it's going to escape that little loop very expressive again so we can just use this these conditionals inside of if else statements so look at see how this works and we say i equals 23 and now let's put a little space there just to separate it out i say if i is less than 20 colon print this following text the following string smaller than 20 if that was not true it was false if that conditional that comparison was false else colon print now you'll see those little colons there very important to put them because that's going to give us this white space into python that white space means something but again this reads very much like a normal english sentence if i is less than 20 remember i's 23 so 23 minus 20 that's false that's going to return that conditional at the moment is going to turn a false for me then this gets ignored and it jumps to the else and what do we do else while we print equal to greater than 20 so let's run that and it's going to return equal to or greater than 20 23 is equal to or greater than 20 and as much as it's greater than 20 so if else allows us to decide what code gets executed and as you can see here it gets a bit more interesting because there's an l if function as well by the way see these are printed a little bit differently let's open up that code cell if you put them inside of these little tick marks that means it's going to print to the screen this tick cell looking like code and that's how you get that so let's let's do this one using an l if that's a short for else if so i can embed if statements inside of other if statements using l if so i'm going to sign a 2 to the variable a 1 to the variable b so i'm going to say if b is greater than a do this l if a equals equals b do this and if both of those are false just do this last thing and as you can imagine of course it's going to say a is greater than b 2 is greater than 1 so this one here executed a is greater a is greater than b because this one asked if b was greater than a and 1 is not greater so that was false so this line was ignored then it went to the next one are they equal to each other no they're not so ignore this what else remains to help this print this to the screen so you can well imagine how you can do that to help control what you're doing next on the list is a little bit more of an advanced topic and we're going to make use of it every now and again and that's called list comprehension it's kind of a weird word because they don't really say what it does but I can generate a list object very quickly so I'm going to use this computer variable called my underscore set and I'm going to assign to that the following and you see inside of a set of square brackets so it's going to be a list and I'm just putting a little for loop in there I'm going to say i for i in range 51 so range is going to start at zero it's going to count in increments of one up till 51 but not including 51 so we zero one two three four five so again very expressive i for i in range and it's just suddenly going to do that and it's going to run through that for loop 50 times and just create those values zero one two three four five and what I'm doing here I'm using indexing so I'm saying give me values zero through five that's not the fifth not the five is not included so zero one two three and four those first five elements and if we print them all out we see zero one two three four if we print the whole odd out that will be right till the end so there are two little more fun examples for you here list comprehension i for i in my set if i pertain percentage three equals equals one so maybe I have the value zero to zero through 49 and zero is through 50 I should say because I went there to 51 for two I was not included so what's going to happen here it's only going to return those values that are divisible by three with that sentence again this comprehension i for i in my set so for i in my set that's going to go zero then the one then the two then the three so i for i in my set if i percentage three remember that returns a remainder if that remainder equals zero so that's a very quick way just to return all the values in my in my underscore set that are divisible by three so it's a very fun way and sometimes people get asked this question when they go for interviews can you quickly just show me all the values that are divisible by this on this interval just do it with less comprehension very easy and then this more complicated one we square each value in the list if that value is divisible by five so i'm going to say do i squared for i in my set so it's going to go to zero one two three four first is going to see if is it divisible by five if so it's going to square that value so all the values that are divisible by five are going to be squared so zero zero squared zero is divisible at least you can say zero divided by five that is zero and it has a remainder of zero so that one's included but zero squared is zero the next one's going to be five five is divisible by five without the remainder and five squared is 25 and so you go you're going to get to 10 10 squared is 100 etc it's a very easy next up is just the numpy library now it stands for numerical python and it's probably going to be one of the libraries that we use most often now one thing about the interpretive language since this python is quite slow it's going to go line by line and execute that code and that's one of the problems with python compared to other compiled languages such as c such as julia it's quite slow now along came numpy then you may call python library and what that is it used c code it was developed in c and it was just wrapped in python so it's got a python wrapper over c code so all we see is the python code but under the hood that's executing c code very quickly so it allows us to to allows us to have python code that's a lot faster now instead of lists and other collections numpy has what we call arrays so first of all we're going to import numpy as np very common namespace abbreviation to use quite standard you can do you can call it just in if you want it to but np is the sister convention so now that we've imported numpy all of numpy which is a very big package is going to be available to us and the way that we construct an array is to use the array function so it's np dot remember I imported it as an abbreviation so i've got to use that namespace abbreviation to get to all the functions that are in there so np dot array it's a function so a set of parentheses and inside of that i put a python list so i'm passing an argument my argument is a python list and if we now look at my array we see it's an array of these elements 1 2 0 1 2 and 3 and very quickly there if i wanted to do range 10 remember that 0 1 2 3 4 5 6 9 i pass that as as an argument to the array function i'm going to get an array 1 0 1 2 3 4 and the thing is we want to do arithmetic on those arrays it's much quicker that can happen much quicker than just with more python so let's just do some arithmetic on those numpy arrays so i'm going to use this dot sum method so i'm saying my other array dot sum open close parentheses we're not passing an argument to that we're using it as a just as is and it's going to very quickly say what is 0 plus 1 plus 2 plus 3 plus 4 plus 5 plus 6 plus 7 plus 8 plus 9 well that's 45 and if we we can time this there's some code that you can do to time these and if you have a very large set you'll see a quite a big difference between working with an array versus working with a normal list so let's do that let's do the following thing i'm going to calculate the mean remember the mean or the average i add all the values and i divide by how many they are so we can definitely do this my underscore other dot underscore array dot sum divided by the length of my other array now my uh a length will also take an umpy array as uh as an input as an argument and this is going to return how many they are so we can very quickly see these values 0 through 9 what is the average if we add all of them and divide by how many they are well they're 10 values 45 divided by 10 is 4.5 but we need to be so verbose we can just use the dot mean method so i'm going to say my other array so my underscore other underscore array dot mean and if i do that it's also going to return 4.5 for me i think you can start seeing the power here as far as doing some data science we can be easily have an array of millions of values and i can very quickly see what the average of all of those values are so that was a very brief introduction to python you've met python now um so of course very difficult to remember all these things a computer language is just the same as a spoken language you can't just read it once and then speak the language it comes through use and this is an introduction you've met now in sort of know what it is about and we can start using the language and it's only by using the language that will get familiar with it and you'll be able to speak python