we are going to be writing quite a lot of neat code today and that's because we're going to be learning the core features of the Python programming language now I've mostly used python for data structures and algorithms because it's pretty much the simplest language out there but it has quite a lot of applications especially in machine learning AI data science and even like backend web development so there's quite a lot of reasons to Learn Python and it honestly is easier than you would expect even if you're just learning it for coding interviews I think it's well worth your time now you're going to learn a lot from watching this video but you're going to learn even more if you follow along whether you want to do that on NE code. with these kind of exercises that I've created it's kind of like an interactive textbook the way I would think about it but you can also follow along in your own local code editor a quick rundown of this environment here at the top left you can click the course name it'll take you to the menu you can see at the moment there's 882 lessons I might add a few more but at this drop down you can go through all of the chapters and here you can navigate between lessons you can also go back and forth between lessons with these arrows on the right you can change the theme ah okay we're going to go back to Dark theme but if you prefer light theme go ahead you can go full screen if you want to you can change a few settings you can reset to the original code like the starter code here there's a console we'll kind of get more into that as well as the submit button and on the left you can see the question it's itself as well as the solution now let's get into it hello world this is traditionally how most people learn a new language the first exercise they ever do is print hello world to the console you can pretty much do that in every language and it's a long held tradition so I think that's kind of a good place to start python makes it very very easy for us python provides a built-in function called print and we can pass in some text using double quotes we can also use single quotes but we'll get into that a bit later but in those double quotes we can pass in some text so whatever we want to print would go in between those we can see we already have that at the top right and in this exercise we want to print this text exactly now Capital matters punctuation spaces matter so we want it to be exactly like this you could copy it but you could also type it out from scratch so that's what I'm going to do just going to go ahead and type hello world no capital on the second word with an exclamation point and then I'm going to click submit you could also do control enter if you want to do the keyboard shortcut and we can see we have successfully passed this lesson and we can go to the next lesson but if you want to learn a little bit more if you want to learn a little bit more about this course you can kind of go to this bottom drop down it'll go through a little bit of the basics I talked about you can also be a bit more Curious and say well what does this exactly mean what does print mean what is this parenthesis the quotation marks and you can get a little bit of the background and I think one thing that's definitely worth mentioning here is the output your output is what was printed by your program this is the expected output what we expected for you to actually print and since the output matches the expected output you have passed the lesson so that's the way we're going to be testing this your output is basically what would be printed to the console if you were executing this code locally the console is just a place for output and debugging information and things of that nature when you're ready go ahead and click the next button here or the next Arrow up above and go to the next exercise what is python well python is an interpreted programming language if you're curious what exactly that means if you're a beginner you might not be super interested and that's perfectly okay but if you have some programming experience you might be a bit more Curious and you can go ahead and scroll down to read more about that but python is actually an old language believe it or not it was created before JavaScript people don't expect that because the syntax of python is very modern it's very easy to use a lot of languages have borrowed a lot from python I love python because it's very very easy to read and simple to use it's definitely one of the most popular languages in the world and it is continuing to grow in popularity largely because of like Ai and machine learning in this course we're going to be talking about the Core Concepts it's perfect for you if you're a beginner but also if you have experience there are going to be little things like kind of the things I have below here that you're going to learn and be able to compare to other languages that you might already know if you're familiar with Java this will teach you a very different way of programming compared to Java now programming is very very powerful you can see the code here on the right you don't have to understand it yet but just skimming through it there's a lot of code that's going on and what it's doing is Computing the digits of pi obviously there's a lot of math here that we're not going to be learning about but this is very very powerful if I submit this code right now you're going to find our code computed the first 19 digits of pi after the decimal point but the expected output was actually we wanted the 20 digits after the decimal point so how do we get 20 digits well we don't have to rewrite this entire program up above actually there's one parameter that we can change we just have to find this line of code Nal 19 we can read through the code if we want to Define that line it is right down here but it's common in programming to have like a bunch of complex code and if you know what you're looking for you can actually click into the code editor press crlf and then search exactly for the string that we want so Nal 19 is what we want that's how we're Computing the 19 digits of pi we want to change that to 20 we want 20 digits of pi I changed that now I'm going to resubmit this code all we did was change one line and now you can see we get the perfect output that we wanted so this is just meant to illustrate the power of programming you don't have to understand all of this math all of this syntax that's going on just yet but by the end of this course you will understand most of it believe it or not in most programming languages code is executed in order from top to bottom so on the right you can see we have four lines the last line is empty but we have three lines that are non-empty and as you would expect these lines are going to execute in order and that's true for most programming languages python is no exception so on the left you can see if we had these three print statements this is what the output would be the first one would be printed first second and then third each of them will be printed on a new line in Python so in our exercise we want to print fourth fifth and sixth there's only one problem on the right you can see the order doesn't quite mat we first have fifth fourth and then sixth so we just want to reorder this so I'm going to click this line I'm going to control X to cut that line I'm going to go up top go to the first line enter and now we have this now if I put some spaces or empty lines between these lines of code that's going to be fine too like empty lines don't do anything they're pretty much ignored by python so I'm going to run this code now and you can see we did get the expected output so let's go to the next lesson now we talked about printing we've printed a few times now we printed hello world and you probably notice that we did put the text inside of Double quotes in most programming languages including python this is called a string most languages will use double quotes to define a string but some languages like Python and JavaScript actually allow you to also use single quotes if you want to so like on the right here you can see me printing hello with single quotes so this will work just the same as if we had double quotes that's not always true in every language but in Python it is true now what exactly is a string well you can just think of it as a sequence of characters that is placed in between opening and closing quotes now that gets into the question of what if we want to print the quote character itself so down here there's kind of a problem because we said characters in between an opening quote and a closing quote Define a string so it looks like we have a string here right these are the characters inside of that string and then after that we have some text that's hello world now this while it kind of looks like it's in between an opening and closing quote it's not this was the opening and closing quote this is just some text outside of some quotes and then we have a couple more quotes over here so this is going to give us an error just to prove it to you I'm going to run the code on the right which is having a very similar error to the one that we have on the left so running this you see we get an error there is some invalid syntax our job here is to fix this error one possible way in Python and most languages is to use the backslash character which is the escape character so that will tell python that we don't want to treat this quote as a closing quote we don't want to treat it as that we want the character quote itself so look here I'm going to add a backslash and now it's telling python we want to treat this guy as a closing quote now this turned red so we actually still have an error here and that's because we have one opening quote now and we have two closing quotes this is not a quote anymore it's just considered a regular character but for us to fix this we are also going to change this character into a regular quote so we're going to put a backslash in front of that as well so now this code the way python interprets it is this is an opening quote for defining a string this is just the quote character inside of a string this is also just the quote character inside of a string and this is the closing quote that closes the entire string it's a little bit confusing when you first learn about it you probably won't need to use this very often but it's definitely worth knowing the Escape character is very very important in programming so now if you were to actually read what this is saying my favorite quote is to be or not to be and now it will be printed with quote characters around the quote itself so that basically was the error that we were trying to fix this is what we were trying to print before we had a syntax error so now if we submit this you can see it worked just as you would expect it there actually is another solution this solution is nice because you can use it in most languages but there is a solution that python actually allows for and it kind of has to do with what I mentioned earlier that we can also use single quotes to define a string so if we were to do that we can actually take the string here on uh the left and using what I talk about here we can Define the entire outer string with single quotes a single quote on the left a single quote on the right and this itself will work if I submit this you will see it does work but we can also now inside of single quotes we can use double quotes as normal so if I get rid of that and I get rid of that because python first sees the single quote here it scans through here when it sees a double quote it doesn't assume that this is going to close that string because only a single quote can close this string it sees another double quote here that's fine but then it sees the single quote and that closes the opening one so the order that we put these in definitely does matter now not only do we need closing quotes we also need closing parentheses so that's what we're talking about here we're talking about code errors we saw one earlier which looked kind of like this invalid syntax and if we run the code on the right we're going to get a similar error a syntax error because python expects the code to have a certain syntax kind of like how when we use normal language we don't really want to have like spelling errors in our documents or grammar errors and things like that Python and programming languages are the same way but programming languages are a lot more strict they're not very forgiving if you have an error your program might just not work at all and when you have an error you might get a pretty helpful error message like this like this one tells us exactly what went wrong it tells us we had a opening parentheses but error the opening parentheses was never closed so that's kind of a clue of what we should do if you look at the code we do have an opening parentheses but there's not a closing one so sometimes error messages like this are very very helpful sometimes they're not but it's always worth looking at the error message if you can't figure out the solution to the problem so all we want to do in this exercise is print this text can someone please add a closing parentheses so if you weren't able to figure out the solution hopefully that serves as a hint as well we're just going to add a closing parentheses close or go ahead and submit this and you can see we passed it there are other types of errors that are not syntax errors there are runtime errors and logical errors you can read more about those on the left if you are interested comments are a way of adding context to code without actually writing code itself so in Python the easy easiest way to create a comment is to use the pound character or the hashtag character as many people know it as and if you were to do that so on the right just play around with it just try it out put a pound you'll see it turns green so this means it's a comment in most like code editors when it's green like this and now we can just type whatever we want we can add code even we can add a print statement here print hello and it's not going to do anything this code is not going to execute so just to prove it to you I'm going to take all of this and the shortcut to turn this into a comment is control slash at least on Windows on Mac I think it might be command slash but I'm going to turn that whole block into a comment now when I run this you're going to see our output is actually empty there isn't any output this was the expected output but our output does not show up cuz nothing happened here so comments are ignored comments don't do anything in programming and this is how we can define a single line comment by using the pound character now if we were to put that pound after like we can add a comment to this line without affecting this line we can actually say here this prints hello so we have the line of code it's going to execute and we have the comment after it which is not going to do anything it's not going to affect this line if we remove the pound now we have kind of an error I think this is going to be a syntax error yep there it is so I think it's worth playing around with this but our exercise is going to be specific L we want to print this text I belong here I also belong here me too we have five lines of code on the right if you were to reset the code you can reset the code like this by the way so this is the starter code now we want to print this without deleting any of these lines of code how can we do that of course using comments which one do we want to remove this one stays the same why am I here I don't see that in the output so let's go ahead and get rid of that by adding a comment get me out of here I don't see that over here either let's go ahead and get rid of that as well and these two stay now I'm going to go ahead and submit the code and we can see that we successfully passed so this first section was just the introduction now it's time to actually get into learning about python so we've printed a few strings and we've realized that we can pass a string into the print function but if we want to print the same string we kind of have to type it out from scratch every single time is it possible for us to be able to reuse certain strings or just data types in general and the answer is yes we can do that with variables so if we were to save this string again with double quotes in a variable called message we can do that with the equal operator we're assigning this variable to this string so now if we want to print that same string again we pass that variable into the print function rather than the raw string itself we can do this multiple times so instead of typing out the string we reuse the variable and the output would be just as you would expect the string would be printed twice so we don't have to keep typing out the string from scratch so as an introduction you can think of variables as containers that can hold values in this case it's storing a string it can store other data types as well which we will learn about our challenge is to update the code on the right so that it prints this string twice let me kind of make this bigger this string is stored in a variable that should be printed twice and we want to do it without modifying anything below this comment so how do you think we can do it well if we print this right now we're actually going to get an error and that's because message is not defined yet we haven't really created a variable called message just yet so we definitely want to do that we want to fix that and if we want to print this string twice let me just go ahead and copy it and then put it in between these double quotes and then when we print we can see we get the expected output now keep in mind that we didn't put double quotes around the variable itself if we put double quotes here the string that this is going to print is going to be the string message we know it's a variable because there aren't double quotes around it so that's a important point to know now when it comes to variables there are some rules we used a variable called message that works and as you might imagine we can't have multiple variables with the exact same name so if we had a variable called message we can't create a separate variable that also has the same name we'll get into what happens when we try to do that later on but for now we can't have multiple variables with the same name also we want generally our variable names to be descriptive so if we wanted to store for example the name or the string London and maybe we want to reference it as the capital of UK so we're just storing a bunch of capitals and we want it to be descriptive you can notice we are allowed to have underscore characters in a a variable name now there are some things that we can't have we can have letters we can have numbers and we can have underscores we cannot start a variable with a number though so that's very important variable names also can't have spaces because you know if we have a space like message one you know underscore that's allowed but message one like this is two separate things so that's why we can't have variables with white spaces in them and generally you don't want variable names that have the same name as a keyword in Python we'll learn more about keywords later but just know that there are certain special words that we can't generally use so knowing that we have a challenge we have some code on the right that's given to us we want the output to be this these three capitals so it looks like we're already printing the capital of France and Germany so without modifying these lines let's assign those to variables I'm going to copy and paste that capital of France you may know is Paris and the capital of Germany is you may know Berlin so we have the three variables so we want to print the capital of Spain which is already given to us here first so we could do this we could copy and paste this thing in here but you might notice a bug when we try to run this it might not work as expected and the error message is somewhat helpful it does point us to the character that's causing the bug but if you look at the rules you'll see variable names can't start with a number that's very important so let's get rid of the number in that name and then when we're printing the variable let's not reference that number anymore so this is the expected code and we get the expected output let's go to the next lesson now naming conventions aren't like hard rules that are usually going to cause bugs it's mostly about styling and just readability so there are many different ways to name variables a common way is camel case where the first letter of every word is capitalized except for the first word so example my variable name the first word is NOT capitalized the second and third words start with a capital snake case is where the words are lowercase but they are separated by underscores we kind of saw that in the previous lesson and the other one is Pascal case where every word starts with a capital character so this is similar to camel case except the first word also starts with a capital now python uses generally snake case so that's what we're going to stick with in this course it's not the end of the world obviously if you use a different naming convention you might be coming from a language like Java or JavaScript that have different naming conventions so our challenge here is to rename the variables to follow the snake case naming convention there's some code given to us the variables are being printed as snake case but they're not defined as snake Cas so if we want to update the variables all we have to do is rename them and we can leave the code below as is so this is going to go ahead and be like that and like that fixing that as well we get the uh snake case names and the output as expected now we mentioned two variables can't have the same name so what do you think this code over here on the left is going to do we have message assigned to the first message then it's assigned to the second message and then we print it so the output turns out to be the second message why is that well it's because when we had this first line message is assigned to the first message when we execute the second line it gets re assigned to a different value we didn't create a second message variable we created an assignment a reassignment so that the value of message was updated so messages or variables can be reassigned in Python now the order that we put these statements in definitely does matter though if we had reordered the statements like this if we printed it before we assigned it to the second message as you would expect the print would have printed the first message cuz it was executed before the reassignment on this line so the order definitely does matter now our challenge is can we add a single line of code to this starter code over here such that we print the first message the second message and the third message well let's just read the code reading code is a very very important skill to learn it can be even more helpful than writing code so we have a message the first message if printed then we print again as of now you can guess what this code is going to do it's going to print the first message twice then it's going to reassign it to the third message and print the third message so obviously the bug is that instead of printing the first message twice we want to print the second message where do we put that line of code probably before the second print so over here I'm going to reassign it to the second message rerunning the code we can see the output is correct now python also allows you like most languages to assign multiple variables in a single line and the syntax is a little bit different than other languages we want to have the variables all on the left side of the equal sign and they're going to be separated by commas in this case we have two variables message one message two on the right side of the equal sign we're going to have two values they're both strings in this case and the first one is hello the second one is world as you would expect message one gets assigned to Hello message two gets assigned to world so this code is equivalent to the code down below it's just that this one is a bit more concise also as you would expect we want the number of variables on the left to match the number of values on the right otherwise we're going to get an error it's pretty neat in Python that you can actually use this idea to swap values so here the first line of code is going to assign message one to hello second message is going to be world now we can reassign these in a single line as well we can set message one to message two and message two can be set to message one and other languages you might need like a temporary variable to do this but python allows you to do it without a temporary variable so this is very concise syntax to swap these two variables around so now our challenge is to update the code on the right so that it prints the following hello world my name is and if we want to do this with multiple assignments we can get away with doing this with just a couple lines of code so let's see what the code that's given to us we have message one printed message 2 3 four and five printed in that order we're not allowed to change the code down below we're also not allowed to change the code up above this line so what we have to do is kind of swap these variables around message one should be hello like that's what we want in the output on the left so how do we do that well we could just say message 1 is equal to hello or we could set message 1 equal to message 2 we might also then want message two to be equal to world so you could say Obviously message 2 is equal to world or you could set message 2 equal to message one but actually this is a bug look very carefully at what we're doing message one gets assigned to message 2 which is hello so now both message one and message two are hello so if we set message two equal to message one well message one is hello right now right this line executes before this line so this is kind of a bug but if we swap these two variables on the same line of code we can avoid this bug so we can set it like this now these will be swapped out at the exact same time so message one will be Hello message two will be world and we want to do similar stuff with this line here we want message three to be my message four to be name and message five to be is so U message three is going to be assigned to a message five which is currently my we want to then swap the other two on the same line as well so message 4 is going to be be assigned to name which is stored in message 3 so that's going to be over here and lastly uh message 5 needs to be is so message five is going to be assigned to this one which is message 4 this is kind of challenging if you're a beginner or if you're just new to swapping variables like this in Python in general now let's run it and you can see it does work now so far we've only looked at strings but variables in Python can actually have many different data types including integers as you can see here so we have a variable assigned to an integer value 25 we also then have another variable temperature assigned to a decimal value in Python these are called floating Point numbers because they have a point that uh floats and there's uh more reasoning behind it but we won't really get into that um we can also have have booleans and these are very very important in programming we're going to learn more about them later on and some Boolean logic along with that but for now Boolean values are kind of binary values they can either be true or false this one is assigned to true we also have strings which are defined with an opening and closing quote and we also have lists actually so these are like a sequence of values this one specifically is a sequence of integers we'll learn more about these later on as well now if we want to get the type of a value in Python there's actually a builtin function for us to do that it's called type so we can pass in 10 into the type function and then print whatever the result of that happens to be and the output in that case is going to be integer right now we want the output of this code on the right to be type int float buol string and list how do we do that well it looks like we're getting the types of variables that aren't even defined and the names of these are going to kind of give us hints so obviously we want integer type to be assigned to an integer and we kind of see how that's done over here so let's just go ahead and do that integer type is going to be assigned to a value it could be 25 but it could be any integer so there's a hint down here telling you that the values don't matter only the type matters so just to prove that to you I'm going to set this actually to zero and I'm going to have another float type and that's going to be set to a float we know that this is what a float looks like but it just needs to have a decimal point so to prove that to I'm going to put 0.0 that's technically distinct from Z the integer this is a different type next we're going to have uh the buol type so that should obviously be a Boolean it could either be true or it could be false and to define false there was like some wording down here so it does need to be capitalized this can be true or it can be false it needs to be Capital if I get rid of that you can see the color changes that's a hint to us that true and false are important words in Python and they need to be defined with a capital next let's do the string type we're pretty familiar with this so the variable name by the way I am just getting from what is printed down here so this is going to be a string I'm going to make it an empty string because an empty string is still technically a string next I'm going to have a list it's called list type over here I'm going to make it an empty list it's defined with an opening and closing square bracket as mentioned over here so we can put some values in I guess I'll put one or we could make it empty it'll run the same way cuz the type is still a list now let's go ahead and run this this code and you can see the output is what you'd expect it's an INT a float a bu string and list that is what the expected was and that's what we ended up printing obviously if I were to make this a different type if I made it a string we would have gotten the type of this printed as a string that's not what we wanted we wanted it to be a list so learning a little bit about different data types in Python in programming there is this concept of dynamic typing in Python the type of a single variable can actually change we initially assign this variable to 10 it's an integer then we reassign it to hello it's a string and then third we assign it to a list of numbers and this is perfectly okay in Python if you're coming from Java or C++ it's not okay in those languages right so those are statically typed languages python is a dynamically typed language the type of a variable can change if you look at this Java code it will throw an error in Java and um some other languages you cannot change the type of a variable it's an integer initially and then we try to assign it to a string it throws an error so in this challenge what we want is to again print a lot of types but this time we have a single variable and for us obviously to print its type and have it be this sequence of types we should probably reassign the variable so initially we want it to be an integer I'm going to assign variable to be 10 and then we want it to be a float so uh before the second print I'm going to assign it to 10.0 or I could do you know 3.14 just make it a decimal next we want it to be a Boolean I'm going to assign this this time to be true next we want it to be a string so I'm going to assign it to be an empty string or maybe ABC for fun and lastly we want want it to be a list so let's set it to this time not a list of numbers just for fun I'm actually going to make a list of strings so that's also allowed that's technically still a list in Python so printing all of these we should get the corresponding types which we do so let's move to the next lesson but actually before that is dynamic typing a good idea do you think generally speaking it's not if you can avoid it because in large code bases it's kind of unpredictable what the type of a variable is if it's going to be changing from an integer to a Boolean to a string then it becomes unpredictable the type and it becomes unpredictable about how we should use it without having an error occur and we're going to learn more about that later so you know those languages like Java and C++ that have static typing there is definitely a benefit to static typing it can make the code more safe less error prone but it's also a little less flexible so these are just tradeoffs between programming languages now we can actually convert one data type into another type in Python so for example if we have a variable that's 10.9 it's obviously a float we can actually convert that into an integer by passing it into the integer function so we pass it into integer and then we print the result and we get 10 when we take a floating Point number and pass it into the integer function we're basically stripping everything past the floating point so in a sense you could say we're rounding the number down into an integer 10.9 will become 10 10.1 would also become 10 in this challenge we want to update the code on the right using typ casting to print three and to print two we can't modify these two lines and we can't modify these two lines we see we have a variable Pi it's 3.14 right now it's going to print 3.14 in those decimals but we want it to print three we have square root of 8 it's this decimal number 2.8 we want it to print two that's what we want the output to be now you can guess how we can do this by taking Pi reassigning it to the integer version of it so rounding it down so this will be three now and same thing with square < TK 8 we want it to be rounded down we'll assign it to the integer version of that number and then we'll submit the code and we see the output is what you would expect now even though we can convert some types like we can convert decimals into integers we can't convert everything into an integer as you might expect for example if we have a string called hello like that's the value it's assigned to if we try to convert it into into an integer how is python supposed to do that there might be some built-in rule but in this case there's not so python is going to cause a type error we can't convert a string like this one into an integer so right now the code that's given to us is going to throw an error let's run it just to confirm that Yep looks like we have an error value error can't convert this into an integer so we want the output of this to be 10 and then it's going to print the typee of a string and then it's going to print 10 and then it's going to print the type of an integer so we're not allowed to modify the code below this line what is it doing it's printing variable and then the type this might seem confusing at first right how are we supposed to print 10 and the type of that variable is also going to be a string well why can't we put 10 inside of the string this is kind of challenging if you're a beginner if we were to do this it's still going to print the string 10 but the type is also going to be a string when we then convert that variable into an integer this time it's actually going to work so this lesson is designed to show you that we can convert strings into integers only if the string itself represents a number so now that we've changed it to 10 the conversion here will turn it into an integer and then we'll print the integer it'll still be 10 but the type of it will this time be an integer now let's go ahead and run this code and you can see we get the expected result this is pretty challenging but it's just designed to show you like if you're not able to come up with this by yourself that's okay I just think you know learning it this way is a little bit easier than if I just kind of explicitly tell you because now you're probably going to remember it a lot better there is also an empty variable in Python in many languages it's called null and I kind of prefer that uh let me type it out it's called null n in Python though it's not called that it has a different name it's called nun capitalized n o n e so you can see that this is a key word it turns blue and this is used basically to indicate the absence of a value so it's not a data type of integer or float or string or any of that this is an absence of a value a variable that doesn't have any value assigned to it so in Python if we try to just have this code like we have a variable that's not assigned to anything it's actually going to give us an error so if I run this code you're going to see we get an error this variable is not defined but we don't want to assign it to anything so the way to do that is to assign it equal to none so now we still haven't really assigned it to anything other than you know this itself none but this will not give us an error so if I submit it we see we got the wrong answer but it didn't throw an error for us so this is the expected output you can kind of guess based on that what we want to do we want to print the type of this none type so we have this variable let's print the type of this variable and I'm going to go ahead and do that and submit it so we kind of passed this without even reading what the challenge was but that is indeed the solution to this one of the core things that we can do in programming is arithmetic so that's kind of why they're called computers they compute numbers and they do it really fast like no human is going to be able to compete with a computer when it comes to arithmetic we have a lot of the core math operations adding two numbers together with the plus sign subtracting the second number from the First with the subtraction sign and multiplying two numbers not with the X character but with the star character on my keyboard I can type this character with shift 8 on yours it might be a little different division is just the forward slash and the interesting thing about division is that the result is always going to be a float so taking a look at the example here we have two numbers X is three Y is 6 add them together and print the result we get nine subtract y from X and we get 3 - 6 that's a negative number that's -3 negative numbers are fine in programming and we multip mly these two 3 * 6 we're going to get 18 we divide 3 by 6 we get 0.5 as you would expect with decimal Division if we were to do the reverse though if we were to say y divided by X that's 6 / 3 the result will be 2.0 not 2 most of the time this isn't a big deal but it's worth knowing that the division operator will always give us a float as the result now for the other arithmetic operators the result is going to be an integer if both of the operands is an integer so for these three we had integers as the result now if one of these like if this was 3.0 or Y was 6.0 the result of these three operations would have been a float if one of the operand is a float the result will always be a float this is a pretty subtle point that I'm making most of the time it's not really super important but it's definitely worth knowing now you might be wondering about the order of operations if we have like a really big math equation that we put on a single line what would be the order of operations the good news is that it's pretty similar to the math concepts that you're already familiar with in the US the acronym is pemus to be honest I never really learned this but I think it's pretty popular but if you don't really care about that this is the order we can put operations in parentheses those will always evaluate first we can have have exponents we'll talk about these in a bit and then we can have multiplication and division these will execute from left to right and then we can have addition and subtraction they have the same precedence and they will also execute from left to right so if you look at this expression really quickly 2 + 3 * 4 the multiplication goes first and then the addition 3 * 4 is 12 + 2 is 14 um if we want the addition to go first we can put it inside of parentheses 2 + 3 that's 5 * 4 is 20 this is definitely important if you're not sure about the order of operations it's just best to use parentheses that usually makes the math more readable anyway so parenthesis is kind of the easy way out now for our challenge we're going to do a few things we want to print the following the sum of all of the values given to us that's pretty easy to do before I even read the rest of this I'm just going to go ahead and do that because that sounds pretty easy um let's do a plus b plus c those are the variables given to us there are a couple integers and one float number two we want to do 0 minus the sum of the values hm well that can be done pretty easily let's say print0 minus all of these A + B + C this works now there is a hint we're free to decare variables if we want and variables are going to be pretty useful so this is kind of the power of of a variable it's looks like we're using the sum of all of these and we're printing it and it looks like we're using that sum once again in the second print statement so let's just put that in a variable I'm going to call it sum if you try to do that you see that this is actually a key word we'll learn a bit about that towards the end of this course so I don't want it to conflict with that so I'm actually going to call this total sum and I'm going to assign it to these three values added together and then then we'll print the total sum in the first statement and then we'll print 0 minus the total sum in the second statement now let's look at the other ones we want to third print the product of all three values let's do that once again so down here let's do a * B * C that's the product and fourth we want the sum of the values divided by the product of the values so it looks like we're using the product a couple times it's probably good to have this in a variable as well so that's what I'm going to do I'm going to have total product up here and then we're going to print that for the third statement and fourth we're going to take the sum of the values total sum divide that by the total product so now we have this and this is some pretty clean readable code right the fact that we used variables made the code pretty nice and easy to read so let's run this now and we get the expected result you can verify all of these if you want like using these numbers if you're a big math person if not you can go ahead and move to the next lesson now there's a few more math operators in Python because there's a few more math operations that are pretty common to do so one is floor division in a lot of languages this is the default way to divide numbers Python's a little bit different so what we mean by floor division is for example that's the best thing to usually do just kind of read the example down here so that's what I'm going to do we have seven and we have two if we take seven floor divided 2 we get three if we did Regular division it would have been 3.5 with floor division we get 7 / 2 is 3.5 and then we round it down so double slash is floor division it rounds the result down the result will always be an integer with floor division not a float third we have the modulus operator usually people call it the mod operator for sure it's the percent sign it will take a number s divide it by two so 7 / 2 is three with a remainder of one so modulus just gets us the remainder of division so 7 / 2 leaves us with a remainder of one if we did 8 / 2 the remainder is zero and that's what the modulus operator would return in that case and third we have the exponent operator in Python the easiest way to do it is double star so this will take down here x to the power of Y so x with an exponent of Y in this case we have 7 raised to the power of two that's just 7^ squar the result of that is going to be 49 now we're given three variables here and we want to once again print a bunch of computation and quickly what's the Precedence of these operators that we talked about well again it's pretty similar to just math rules that you might be familiar with the highest precedence is the exponent and then floor Division and mod are going to go after that and these two basically have the same precedence as multiplication and division but again if you're ever confused it's best to just use parentheses even I do that and honestly most people do that because it makes the math more readable now let's get into the challenge first we want to print the product of A and B floor divided by C we may or may not need a variable I think we might so I'm going to go ahead and just start with that product a b so I'm going to get a * B and then we're going to print the product a floor divided by C so double slash divided C next we want the remainder so we want uh to use the modulus operator we want to mod it so uh take the product a b moded by C third we want the result of raising a to the power of B that's pretty easy to do a double star to the power of B fourth the result of raising B to the power of C once again pretty easy B power of C just to note this will not print what you expect a carat B is not a to the power of B just keep that in mind that's not that like a lot of people make that mistake so that's not going to give you the result you would expect this is how we do exponents in Python now let's go ahead and run it and we get our expected result there are some Shand operators in Python so if you look at the following code count is assigned to zero and then we add count with one and assign it to count so this will just increment count by one so now count will be one then we will set count + 2 equal to count so now we're adding two to the result so if it's one then the total now is going to be three when we print it the value is three so we are allowed to use the same variable in its own assignment calculation but there's actually a shorter way to write this code in Python and this is similar in other languages we can use plus equal so count is assigned to zero and then we say plus equal 1 this basically means we're adding one to count and then we do plus equal 2 this just means we're adding two to whatever count already happens to be so if it's zero we add one and then add two the result is going to be three so this code is exactly equivalent to the code up above this isn't just true for addition the shorthand operation for subtracting is minus equal for multiplying is times equal for dividing is slash equal and you can do that with with modding floor Division and exponents as well now these are not required so if they're confusing to you as a beginner that's okay you're not required to use them and most of the time the only common ones that you'll see are plus equal and minus equal those are the most important ones anyway we'll at least practice using those so our challenge is there's a variable given to us number we want to use the shorthand operators to add five to number and then print the result so that's not not super crazy let's just review the syntax plus equal so number plus equal 5 and then print it that we can do pretty easily like this okay Step One is complete step two subtract two from the number and print the result we have minus equal for that so we're going to do number minus equal what did they want us to do I think it was two so subtract two from number and then print the result third add number to itself and print the result whoa that's kind of interesting so to number we set number equal to number plus number right like that's what we mean we're adding the number to itself and then assigning that to the result this is perfectly valid and the shorthand version of this is also valid plus equal to number that's adding number to itself and then print the result well run it and you can see we do indeed get the correct result now let's learn a little bit about Boolean algebra it's a branch of mathematics we're not going to be going super in depth into it but we're going to learn what we need to know for programming in programming there is something called The Logical or operation it takes a lot from Boolean algebra and the idea is very very simple so suppose we have two variables one is assigned to true so by the way Boolean algebra deals a lot with binary it deals a lot with Boolean values as you might imagine so true and false so if we have one variable assigned a true a second assigned to false if we say A or B logic or we're basically asking either a is true or B is true and if one of those is true then the out outut is going to be true so in this case that is the case one of these is true so the output is true if both of them were false the output would be false if both of them are true the output would be true it's similar to English so first I'm just going to kind of explain like the English way of saying it like how human language works if we say Alice likes ice cream and we say Bob does not like ice cream then we say Alice or Bob likes ice cream that statement is true because at least one of them likes ice cream if both of them like ice cream it's still true to say Alice or Bob likes ice cream even though maybe the more technical way to say is Alice and or Bob like ice cream now the more mathematical way to look at it is with something called a truth table these are all possible combinations of A and B and the result of them the logic or result of them so if both of these are false the result is false if a is true and B is false the result is true if a is false and B is true the result is once again true if both of them are true the result is also true it's just asking at least one of these is true the result will be true that's all it's saying here we can even have three of them if we have a b and c the first two are false the third is true and we chain these together A or B or C the result is true because at least one of these is true in this case we have a few variables four of them exactly given to us and we want to print the following things we want to print the logic or of A and B we can do that with the keyword or as we saw so we can say print A or B we could have stored this in a variable if we wanted to this time I'm not going to do that but you're free to do that if you want to next we want to do the logic or of B and C so B or C third we want to do logic or of c and d so C or D and lastly we want to do the logic or of all of them and we are going to do A or B or C or D the result of the first two should probably be false the result of the second two should be true the result of the last two should also be true and then all of them together should be true cuz at least one of them is true so in the output we'll get false and then three trues and it looks like that's exactly what we get and so we've completed this challenge um if you're curious what is a keyword CU we haven't explicitly talked about that keywords are basically words that are reserved in a programming language so in Python the word or is reserved because it has a special meaning it's going to take the logic or of the two operand false is a keyword and true is a keyword we can't assign a variable to true because this is already reserved for the value true so if I submit this we're going to get an error right we're not allowed to do this we can say the same for or we can't use that as a variable name so we did talk about this in the variable naming lesson I think but now I hope it makes a bit more sense now in Python there's also a keyword called and it's used to perform the logic and of two operand so this is similar to or but it's kind of the opposite so if we have a assigned to True B assigned to false if we say a logic and B we're asking are both of the operands true in this case they're not both true so the output is false and like the human language way we would say like Alice likes ice cream Bob does not like ice cream then the statement Alice and Bob like ice cream is false because both of them don't like ice cream they both need to like ice cream for that statement to be true and they don't so it's not true now the truth table for and is going to look something like this when A and B are both false of course A and B are going to be false like the value is going to be false if a is true but B is false the output is false if the other one is true but a is false then the output is again false it's only true when both of them are true to summarize the and operation returns true if all of the operand are true so here if we have three operand that are all true and we take A and B and C the output is going to be true in the code editor we're going to have a very similar exercise so we're given four variables we're going to print the logic and of A and B so A and B and then we're going to do B and c and then we're going to do c and d and lastly we're going to do a and b and c and d we probably expect this last one to be false cuz not all of them are true this one should be true cuz these two are true and the first two are going to be false cuz A and B they're both false and these two one of them is false so we should get I think false false true and then false and that is exactly what we get so we're done here now last one for math there's an easier Boolean operation actually the easiest of them all uses the keyword not and this is used to perform The Logical not operation also known as negation so it basically takes the opposite of a Boolean value so if we had a is true and we take print not a it's going to take the opposite of a it's true therefore we're going to get false as the output with B it's already false so we take not b and we get true as the output so very very simple just take the opposite of that I'm sure you can think of some human language ways of saying this like if I say I don't like ice cream that obviously means I don't like ice cream and if I said I don't not like ice cream that's kind of the opposite of that even though it gets really confusing with human language we can also so a combine operators so if we were to take this A and B that's obviously going to be false with these two operand but then if we take the negation of that we take the negation of false we get true in the output since we had parentheses here the and operation was performed first which gave us false and then we got true as the result so now let's take a look at these and do them so so the negation of a pretty easy print not a the negation of C not C uh the negation of the logic and of A and B we're going to get first let's do the logic and of A and B A and B now we want the negation of that so let's say not and then wrap this in parentheses fourth the negation of the logic or of B and C so print B or C and then wrap those in parentheses and then let's do not in front of that we did all we wanted to Let's print it and the output is correct very quickly what's the order of operations of these you can briefly read about them here but in my opinion it's best to use parentheses just so you don't have to like remember these and you don't end up confusing yourself or another developer who has to go and read your code so we learned that values can be stored in variables and then those variables can be reused later in the program well functions are kind of an extension of this because they can store blocks of code that can be reused multiple times so if you look at the code down here this is how we Define a function in Python we use this keyword defa it stands for Define I think I'm not actually 100% sure then we give the name of the function so we probably don't want to have a variable with the same name as the name of a function but after that we put a couple parentheses and then a colon so this is the function header it defines the function and then the code below that belongs to this function this time we only have a single line of code it just prints hello world functions obviously can have more than just a single line of code if we wanted they could have like two print statements the important thing is though that the indentation here matters this belongs to this function only because it's indented now it's common to have two spaces or four spaces in Python this time we are going to be using four spaces and we're going to use that throughout the course that's the most common amount to indent in Python now this code itself is actually not going to print anything by default this just creates the function if we actually want to invoke the function call the function we do it like this greet followed by the parentheses so this is calling the function and it's going to execute that print statement and print hello world so it's not that different from how we used the print function earlier right this is a builtin function print is builtin but we defined our own function called greet which is only going to print hello world the challenge for us is this right now we're we're calling two functions we're not allowed to modify this code we're calling greet and we're calling say goodbye now the problem is the Greet function has a bug it should print hello world but it looks like there's a bug can you spot the bug it looks like the indentation it can be fixed just by pressing the tab character by default it will space by four spaces so that's what I'm going to use now we have fixed it now this will print hello world that's good but there is a second bug when we invoke say goodbye this function isn't even defined anywhere so we have to fix that we have to Define this function and it should print goodby world if you don't remember how to define a function the good thing is there's a function right here for us to follow so we're going to define the function it's going to be called say goodbye followed by parentheses and a colon when you press enter you see it actually already tabs by default that's the power of a code editor now we want to print good by comma capital W world and then exclamation point so now both of these functions should execute and that is indeed the case now variables have to be defined before they can actually be used and the same is true for a function so here you can see we call print and pass in N but n is equal to 10 and it's not assigned until after this print statement so this is going to give us an error because n isn't defined yet down here we're going to get a similar error because we're calling print number it's a function that we Define but that function hasn't been defined yet so we want the print statement to go after the function definition so we're going to go ahead and fix a bug related to that so our challenge is to print 10 and 20 you might not know exactly what's going on here you might not know why we have an n in these parentheses that's okay we can still fix the bug we see that the function is being called two times before the function is defined so we want to take these lines and move it after this function but look if we do this this is a problem we don't want these print statements to go inside of the function cuz then they belong to the function we want the to be outside so we don't want them to be indented so these are going to be like this the function is defined and then the function is called twice printing 10 and 20 so let's run this and you can see we get the expected result now you might have noticed in the previous lesson as well as when we're calling print we always put values and variables inside of the parentheses that's because functions can be defined with something called parameters so let's look at an example of that this is a function it's called greet it takes a variable as a parameter it's called name and then inside of the function we create a variable called message we take hello which is a string add it with name this is an example of string concatenation we're going to learn a lot more about this but for now it's pretty simple to just assume that it's going to take two strings and combine those Str strings together and then it's going to print that string so when we call greet and we pass in a string called with an value of Alice it's going to then inside of here it's going to take hello comma space plus Alice and then print that so this will print hello Alice the power of this is that we can take one function and reuse it for multiple purposes this function can be used to greet not only Alice it can be used to greet a lot of different people all we do is pass in a different variable or value in the parameter position a subtle thing to mention is that this variable is called name it's the parameter when we pass in a value for that parameter it's referred to as an argument so it's not super important to know it's just something that you'll hear a lot so I think it is worth mentioning like what's the difference between a parameter and an argument but again it's not a big deal a lot of people use those words interchangeably so our challenge here is to define a function called farewell it takes a single parameter and it prints goodbye comma space followed by that parameter then we want to use the function so that the output is this so right now we have some code it calls farewell even though the function isn't defined our job is going to be to Define that function up above we want it to take an argument a parameter so I'm going to call it name because that seems to make sense in this context and then we want to create a message similar to what we did over here message is going to be equal to goodbye comma space and then the name itself so adding it with the argument or parameter and then here we're going to print the message we're not allowed to modify any of this code but we still want to print goodbye Bob and Goodbye Charlie the easiest way to do that is just to invoke the functions passing in Bob for the first time and passing in Charlie for the second time that we call the function so now let's run this and you can see we get the expected result functions actually can take more than just one parameter so down here you can see that we Define a function called greet it takes two parameters one called name and another called greeting and it's going to take greeting add a space to it and then add name to it and then that's going to be assigned to message and then that's going to be printed so just another example of string concatenation the plus operator is going to combine strings together so down here if we call greet pass in Alice for the name and hello for the greeting the result we're going to get is hello space Alice so one thing to note the order that we pass these in matters name is defined first so name is going to be assigned to Alice greeting is defined second so that's going to be assigned to hello our challenge here is to define a few functions one is called two sum and another is called thre sum the two sum function should take two parameters and print their sum the thre sum function should take three parameters and print their sum so we can see of how these functions are going to be called uh 10 + 9 and then 5 + 14 + 6 so let's go ahead and Define them the name is going to matter here obviously it needs to be called to sum and I'm going to call the parameters I guess num one num two but you could call them whatever you want all we want to do is print the sum so print num 1+ num 2 and similarly three sum num one num 2 and num three and then print the sum of all of those so far so good you might think you're done here but there's actually one more ask for us in the challenge we want to finally call to some passing in seven and 10 as the arguments so I'm going to do that here because we're not allowed to modify anything below this line so here is where I'm going to put two sum passing in seven and N now the order that we pass in these actually doesn't matter I think I misspoke it's 7 and 10 sorry about that the order that we put these in doesn't matter in this case uh sometimes it could matter it's not going to affect the result of the sum of these but depending on what we were doing in here it could matter here we're going to do three sum passing in 35 and 8 as the argument so 3 five 8 and running the code we can see we get the expected result now another core part about functions is the return statement functions are actually a bit more extendable than you might think we can do more than just print the value we can return the value so here add is going to take X and Y and not print the sum but it's going to return the sum so now we can actually take that function pass in three and five and it's going to return eight so then when we set result equal to the result of this function it's going to be assigned to a so we can take that value and now reuse it we don't just print it a single time we can now reuse that value throughout our code and of course the first thing we're going to do is print it and the result is going to be eight so this allows us to basically use a value outside of the function it was created in so our challenge is to create a function called product that takes two parameters and Returns the product of them this is the test code we're not allowed to modify below this line so you can see we're calling product on two and four 8 and two and a bunch of others and then we're printing the result of that we should not put a print statement inside of the function so we want this product pass and I'm just going to call it a and b for short and then I'm going to return a * B here just like that so I'm going to run this and we get the expected result now there's another component to python called type hints these are pretty much optional you don't need to use them but they can make our code a lot more readable it's basically a way of kind of adding a comment to a function to say that the parameter that X should be is an integer so you can see the syntax is the parameter Name colon and then the type which in this case is an INT and then comma y colon in so the variables are still separated by a comma but we have a type hint for the parameters and we actually have a return type as well so after the closing parentheses and before the colon here we have Arrow like Dash greater than sign I guess but this kind of represents an arrow and then the return value is going to be an integer so this adds a lot of information to the ad function making it pretty clear that we expect to have integers as parameters and the return value should be an integer in Python though these are not like mandatory in that if you were to pass in a Boolean or a string for X and Y it's perfectly allowed python is not going to throw an error in that case in a lot of languages the parameter types are enforced python does not enforce them it's just for uh developers to kind of know it's possible that functions don't return anything so a function like this one down here where we're just printing something and we don't return anything the return type is actually going to be none because we're not returning anything so that represents the absence of a value and none represents exactly that so if you want to confirm this let's do that with the next challenge let's call the function greet here which doesn't return anything and that's what the type annotation represents the return type so let's call greet and let's pass in a string it doesn't really matter what string we pass in actually it does it looks like uh here they say call the function with the argument neat code so let's do exactly that uh neat code and then let's get the return type so we can get that with the type function that we used earlier in the course then let's print the result of that so this is going to give us probably the nun type let's just confirm so at prints hello world we expect that CU we did call the function and then it prints none type which again we expect now it's possible that we can actually change this code like we can from the function itself return none so let's see what happens when we do that is that going to change the output it's actually not going to we have the exact same output we could have also just had an empty return statement which doesn't return anything and that's the exact same as returning none so there's three different ways that a function can return none by not having any return statement by returning none and just by having an empty return statement uh that's kind of what is mentioned over here as well one of the difficult things for beginners to understand is this concept of scope and I'll do my best to explain it but it might take a bit of time to wrap your head around let's look at the following code we have a variable n assigned to 10 if we print it of course we get 10 in the output then we Define a function called print number the name of the parameter is also n this is kind of confusing CU we're not allowed to have the same variable defined multiple times right so what's this code going to do well this function is just going to print that number and we're going to have this function called with the value 11 which is going to print 11 as you would expect then we try to print n one more time and the output is 10 when we print this value we're printing this variable up here basically n was never reassigned to 11 even though when we printed it from here it was 11 so this is kind of confusing I'm going to try to explain it visually so this is all about the concept of scope in Python so when we originally defined n equal to 10 it's a variable we defined it in something called the global scope so this represents the global scope that is kind of the scope that's everything like that encompasses the entire scope of the program then then we had a function so I'm going to use a different color for that we had our function I forgot what it was called I think it was called print number but this is the scope of that function so I'm going to try to make it as clear as possible this is just a single functions scope now you can see the scope of that is inside here but that function accepted a parameter called n so the scope of this is separate from the global scope it had another parameter which was called n and it was assigned the value of 11 so there's a conflict there's two different n's there's an n in the global scope and there's an n in the scope of this function so if there's a conflict the function will use its own scope so whether it took a parameter of n or the function maybe defined its own variable n it's going to use its own variable so there's a conflict it's going to use its own that's the idea I'm trying to get across n was not reassigned this function did not touch this n it created its own n so in a sense there were multiple copies of the same variable like there are multiple copies of n basically it comes from the consequences of scope so that's what I'm getting at this is what happened so when we called print n inside of the function function we printed 11 but then when the function was done when the function was finished executing this function goes away and then we called print out here we called print n in the global scope and that's not going to print the value from the function it's going to print the value n in the scope of like the current scope the global scope so then we print 10 so knowing that this code makes a bit more sense right the original n10 was not modified so going down here I kind of explained the same thing with words in our case the challenge for us is to just prove that n inside of the function is a different one outside so let's look at the following code add one it takes a parameter called n it adds one to n and then it prints n so we have n equals 10 this is the global scope and then we call add one with n it's going to add one to 10 and then print it which is going to give us 11 and then we're going to print n what do you think n is going to be here given what I talked about in terms of scope this one is not going to change so it's going to be 10 and if you don't believe me we're given the exact same code here and all we have to do is just run it to prove it so I'm going to click submit and let's just look at the output we got 11 first and then 10 and again if this is confusing take a look at the description down here it should make a bit more sense of why exactly that's happening so let's learn even more about scope because it's a very very important concept and it's also kind of difficult to understand so let's look at this code here we have a function declare variable it doesn't return anything all it does is declare a variable inside function only it's assigned to 10 and then it returns so it doesn't really do anything we call the function it didn't do anything and then we call Print on this variable and it's going to cause a name error it says this variable was never defined but we did Define it in the function and once again this is a consequence of scope in Python this variable is only usable within the function it's not usable within the outer scope outside of the function aka the global scope but when we try to do this code down here n = 10 and we don't pass a parameter into this function we don't declare a variable in this function we try to print n which is only defined out here this does not throw an error it does not cause an error and we call the function and it actually prints the value 10 this is again a consequence of scope it has to do with global scope versus local scope let me explain that visually this entire scope remember is the global scope and then we have the function scope suppose this function declares a variable like we called it inside function only but let's say the variable is called X right we set x equal to 10 and then we don't do anything and then the function is finished and then we try to call print out here print X it's not going to work because the global scope can't access a variable inside of a function scope after it's done it can't do that this is not allowed but this function is just a subset like the scope of this function is just a subset of the global scope so if we had a variable xal 10 here and there's no X defined here if there was an X1 here of course we would use that but it's not so now we try to print X inside of this function print X it doesn't know where to find it so it's going to look at the global scope and see there is an X out there go ahead and use that one and print it so this is allowed so this kind of explains why those two pieces of code worked the way that they did kind of try to think about it visually if it's confusing for you so knowing all of that let's just briefly review the differences between Global and local scope variables declared outside of any function or other scope are considered part of the global scope they can be accessed from anywhere in the program including other functions the local scope though is a bit more specific a variable that was declared inside of a function whether it's a parameter or it was explicitly declared inside of that function it's part of that local scope to that function it's only usable in that function the local variables are created when the function is called and they're destroyed when the function is finished executing so there you can't access them obviously when the function is finished now knowing all of that we have a challenge there's a bug on the code in the right if we try to run it we'll see a name error but we want it to print 100 and then 100 let's take a look at the code I'm going to run it just for fun let's see yep we do indeed get a name error we're calling print on num but the number is not defined so what's going on there we have Nal 100 we have our function which takes in a parameter and then prints it and then we call Print local number with n which is 100 so this should probably print 100 I'm just taking a note for myself prints 100 and then here we call print with num well num is only usable in the scope of this function we have n outside of the function so we're not actually trying to print num we're trying to print n or you could just replace this with 100 I'm going to do n now this should also print 100 num should not be accessible outside of this function so that's why we were getting an error so now running it we see we get 100 and 100 in the output python allows you to have default Arguments for a function so let's look at this code block we have a function called greet and it takes a parameter that is called name but there's an equal sign after it and then it's equal to a string called world so let's look when we call greet then without passing in a parameter Well normally we'd expect it to throw an error because if there's a parameter we should pass in an argument for that parameter but this time we don't and right now it prints hello world if we pass in Bob it prints hello Bob so why did it print world the first time that's because because name has a default value of world if we don't pass in an argument for this parameter it's going to by default be the string world so it's going to print hello world if we do pass in a string it's going to be assigned to that string so this is a default argument we can have multiple variables actually so for the multiple parameters we can have greeting and then like name and the default value for each of these is going to be uh hello and then world so so we don't have to specify both of these if we don't want to we can also have just one of them be a default value so the first one doesn't have a default value that one is required we have to pass in a parameter or an argument for that the second one is not required it has a default value of world the third way is not valid this is an invalid way of doing it if the first one is optional as you see then the second one must be also opt optional we can't have the first one be optional and then the second one be required because how would we even call this function like in this case what would we do we'd say okay call greet don't pass anything in for the first one and then pass something in for the second one this syntax is very strange and that's why this version is not allowed if you're going to have default values they have to go at the end of the parameter list you don't really have to memorize this it's not something that's going to come up super often but it is worth knowing our challenge is in in the code editor we want to update this function so that the second parameter has a default value of this string exclamation mark so right now if we try to run this code we're going to get an error because we're missing one required argument punctuation hm that's kind of a clue we called it the first time it worked but the second time on line five so let's go to line five here we didn't pass in the second parameter so we should pass something in here or we should make this optional so that's what I'm going to do I'm going to make it optional I'm going to make this have a default value of exclamation mark and now it should work and it does so we're good to go here so now let's learn about comparison so this is going to be kind of a precursor to what we learn about next conditional statements but the idea is that we can compare values for example we have X and Y 3 and 5 respectively and we compare them not with a single equal sign because we know that's reserved for assignment but we compare them with double equal sign so x equal to Y like we're asking a question with double equal sign is x equal to Y so the result is either going to be true or false this case three is not equal to five so the result is false this is one example of a comparison there are several others so we do not equal with exclamation mark equal sign so we're then asking these two are not equal to each other we can compare less than with like these are kind of similar to math like less than with this and then greater than with this and we do less than or equal with the carrot equal and we do greater than or equal with that and so our challenge is basically to implement the operators up above so we're going to get a very clear look at how these work starting with the first one check equal we kind of don't even need to uh you know read the description here because I think the functions are pretty self-descriptive so here check if these are equal we want to know is x equal to Y and then we want to return the result so we can do result equal x equal to Y so this will either be true or false it'll be assigned to this and then we return that so that's one way to do it a more concise way though is just to return the result of this so return X is equal to Y and the result is going to be true or false that's the easiest one now let's check if they're not equal is these two parameters not equal let's return X not equal to Y again it's going to be a Boolean now let's check less than so this one might not be super clear but we're going to be checking is X less than y so we could have been doing the opposite is y less than x but in this case we're checking is X less than y so we're going to return X less than y so the result of that will either be true or false and then we'll check is X greater than y is X greater than y and then the last two are a bit more interesting is X less than or equal to Y so return X less than or or equal to Y and the opposite is X greater than or equal to Y so there's a bunch of test cases here quite a lot of them actually I'll skip a lot of the simple ones that you'll kind of be able to determine on your own but the last two are pretty interesting three is less than or equal to three do you think that's going to be true or do you think that's going to be false well less than or equal so they're allowed to be equal to each other so of course that's going to be true is four less than or equal to three well not to me four is greater than three so that's going to be false and then these three is greater than or equal to three that's also true if they're equal it is true because they're allowed to be equal and is two greater than or equal to three that's false 2 is not greater than or equal to three let's just confirm in the output you can see we do get the expected result there's a lot going on so I tried to add some text so if you get any of these wrong pay attention to what your result was compared to the expected result now let's finally get into some pretty interesting code some conditional statements starting with if statements if we want to run some piece of code based on some condition we can do that with an if statement conceptually it's pretty simple so if we have let's say an account balance which is negative 100 and then we check is the account balance less than zero because if it is we want to print some text we want to say your account is overdrawn you have like a negative balance that's not good so with this this print statement will only execute if this condition is true in this case it is so it's going to execute now this print statement is outside of the if statement and we know that because it's not indented so similar to functions this code belongs to this conditional statement which is followed by a colon only if it's indented so uh this will always be printed regardless of whether this condition was true or false it's possible though to have conditional statements inside of functions as well so this kind of gets into how we want to indent our code and the idea is that any code that's indented once will belong to this function now if we want code to belong to the conditional statement it needs to be indented relative to the conditional statement so you can see this is indented once and twice so that means this print statement belongs to this condition so our challenge is going to be simple we're given a function here is balance low if the balance is less than or equal to 100 it should print warning low balance otherwise it doesn't print anything let's check if that's how an if statement starts the balance is less than or equal to 100 so we do add that equal sign then colon and if that's true let's print exactly warning colon low balance period and if it's not then we don't do anything so that's we don't really have to put anything for that so let's run this code it should print low balance twice because it was called with 99 and then 100 and 101 this one won't print anything so um it looks like I actually did have the wrong output and I think that's because mine is it's kind of subtle but mine had a capital B but we expected a lowercase B I probably should have just copi and pasted this on the left but it's a good error to have there's a lot of Errors like this in programming where you're just kind of off by one character so here we corrected that and now we get the the correct output so now let's go to the next lesson unlike functions in Python if statements do not create a new scope that means that any variables that are defined inside of an if statement are actually accessible outside of the if statement this is different if you're coming from Java or C++ this is different from those languages so it's worth knowing we have here an if statement the condition is true so this if statement is always going to execute and then we create a variable message is equal to hello and then we try to print that message and it works perfectly fine because a conditional statement does not create its own scope so here we can also use conditional statements to update variables that are outside of that conditional statement so balance is declared up above it's in the global scope and then we execute this if statement if balance is less than a zero uh which it is it's going to be set to zero so this will evaluate this will execute and then down here balance will be zero there's only one balance we don't have multiple copies of balance and it was assigned to zero so then we print zero again if statements have the same scope as wherever they are defined so if it's in the global scope it will share that scope if it's a part of a function it'll have the same scope as the function so uh this is an example of that we have a function is balance low we check is the balance less than or equal to 100 if it is assign this to this string and then print the string when we call this function once with 50 it's going to print warning low balance but when then we here try to print message well message was defined in this function it was technically defined in the if statement but either way that variable belongs to the function therefore it's not accessible outside of the function so this line here will cause an error this is nothing really new it's just that functions have different scope than Global so if statements don't really change any of that now we are going to in the code editor Implement a function called pay bill it's going to take two parameters the balance and the bill the balance is the account balance and Bill is the amount of money that needs to be paid if the balance is greater than or equal to the bill so let's just start coding uh before we even read all of that which sometimes isn't a good idea but in this case if balance is greater than or equal to the bill then we want to do something what do we want to do the function should return the New Balance after subtracting the bill from the balance so it should return let's call it new balance is equal to balance minus the bill and then we want to return that return New Balance otherwise the function should return the balance without making any changes to it so down here maybe we can return the original balance this gets a little bit into what return does so if we want to ignore that for now another way of writing this code is before the if statement we could also have a variable called New Balance which originally is set to the original balance and then down here we could also uh return that New Balance so now there's two cases either this if statement is not going to execute in which case we return the original balance if it does execute we could actually get rid of this return statement we'll update the new balance and then return it and in that case it will have subtracted the bill from the balance I'm going to go ahead and run this and you can see we get the expected output you may or may not already know that when we return we stop the execution of a function we will talk about that more in a bit now let's get into if else statements because it's actually common to want to run either one block of code or the other block of code and we can do that with an if else statement so here look at the code without an else statement we have balance is 100 if it's less than zero print account is overdrawn if it's greater than or equal to zero account is in good standing so only one of these is going to execute right because if it Balan is less than zero that's the opposite of saying the balance is greater than or equal to zero it's guaranteed that one of these is going to execute and only one of these is going to execute but it's possible to rewrite this code which is slightly more readable we can say balance is equal to 100 if the balance is less than zero then print this otherwise print the account is in good standing so again it's guaranteed that only one of these is going to execute and exactly one of them either the first one executes or the second one executes else means only execute this if the other one one did not execute so this was false balance is not less than zero therefore execute the code inside the else statement again you can see the code is indented therefore it implies it belongs to this block and now like I promised what does the return statement mean the good thing about conditional statements is that they can allow us to have multiple return statements within a single function but only one of those return statements is ever going to execute take a look at this code here we have a function called get max if a is greater than b a is larger than b so we want to return the maximum of these two integers therefore return a if that's not true then return B because either both of them are equal or B is greater than a in which case we want to return B regardless so the thing about this is that obviously we learned only one of these blocks is going to execute but also when a return statement executes it stops the function even if we had some code outside of this IFL statement it would never execute because either this is going to return or this is going to return and then the function will immediately stop that's just a very important thing to know about functions because that comes up quite a lot knowing all of this let's implement the function get min it's going to return the minimum of the two parameters which are integers so we want to also return an integer if they're equal it doesn't really matter which one we return because they're both the minimum right so let's implement it it's going to be pretty similar to the max function that we had so if a is less than b return a return the smaller value else return B because otherwise they're equal or B is smaller I'll prove to you that this works so let's just run this this does work alternatively we could have put an equal sign here that would have worked the same if a is less than or equal to B it's still technically the minimum so that works as well also we could do this we could get rid of the else statement actually and here either this will execute in which case we found the minimum and returned it otherwise we're going to return this it's going to stop the execution and return regardless so this also works there's multiple ways to write the same code generally we prefer to make it as readable as possible so the lse statement is fine but you know all of these are pretty simple to understand so it doesn't matter which way you write it in my opinion there is yet another variation of a conditional statement it's called the else if statement and in Python it's defined with this key word I think it's kind of weird but it's l if that stands for else if and this allows us to actually check multiple conditions in order suppose we have something like this the balance is a th000 first we check is the balance negative um it's not so this one is skipped then we check is the balance zero it's not so we skip this as well else if is kind of like an else block it only executes if the prior statement did not execute but else if has a condition that's where the if comes from so this will only execute if this is true and the previous one did not execute so this one is also skipped though third one is the balance less than a th000 or 100 nope so this is also skipped lastly we have an else block this one only executes if all the previous ones did not execute so this one is true in this case because these ones didn't execute so this is what we will print in a chain like this with if else if else if and else only one of the conditions is ever going to execute that's very important to know even if multiple of these conditions were true as soon as it finds one that's true it's going to execute it and then stop checking the rest of them for example if the balance was 10 it would have printed uh this one and then the other ones would not have been checked and the other ones as well so if the balance was Zero it would have executed this and not the other ones knowing all of that our challenge is using IF if and else implement the function called check range if the number is less than zero return this string negative if the number is zero return the string zero if the number is greater than zero and less than 10 return this string and lastly if the number is greater than or equal to 10 return this string so we're going to do those in order with an if else chain we're going to say first off if the parameter number is less than zero return this string negative otherwise else if the number is equal to zero num equal to zero return the string zero otherwise uh else if the number is greater than zero so num is greater than 0o and num is less than 10 we're going to return this return positive single digit lastly the number is greater than or equal to 10 so I guess we could also put an else if here we'd say else if number is greater than or equal to 10 return positive multi- digigit that's the string we want to return so uh you can see some of the test cases down below but let's go ahead and run this and we get the correct output now I didn't have an else block here but technically this last part of the code could have been an else Block it's equivalent because if the number is not negative it's not zero and it's not a singled digigit number then it has to be a multi-digit number right that's logic right it has to be a positive multi-digit number it has to be greater than 10 otherwise one of these three would have executed they're mutually exclusive so this could have been an L state if we really wanted it to be that's technically a little bit more readable and here you can see that works just the same and in a way I actually got a little bit ahead of myself by using this condition here I actually used a logical operator in a condition and we could have Rewritten this as well because if we know that the number is not negative and the number is not zero do we really have to check if the number is greater than zero if it's not zero and it's not negative that's kind of implied right like that's just math so we could actually get rid of this as well and just check the number is less than 10 um and that will work the exact same so that also works now let's look at logic conditions kind of what I was going over right now we can use logical operators in a conditional statement we know that a conditional like this one balance is greater than zero and the balance is less than zero this is going to evaluate to true or false and we use true or false to execute conditional blocks so in this case 500 is definitely greater than zero and less than a th so this will execute so we can use or and and not operators in a conditional statement looking at this challenge we want to implement the discount applies function it takes in the age of somebody it's an integer and returns true or false so we should return true if the age is less than 18 or the age is greater than or equal to 65 otherwise it should return false so just like that I'm going to say if age is less than 18 or age is greater than or equal to 65 the equal is very very important we're going to return true a discount applies it's maybe a senior discount or you know a young person discount otherwise we were going to return false we can do it like this or we could just put this outside of the uh condition either one will work I'm going to run this and we get the expected output now there is this interesting Concept in Python and some other languages it's called true Tru Y and falsy it's actually possible to use non Boolean values to execute conditional statements for example we have a string message it's empty we can actually do if message this is not going to throw an error python is going to ask is the string nonempty if it's nonempty then this condition evaluates to true well I guess the justification is below we'll get into that but this time the string is empty so it's not going to execute then we assign the string to a non-empty string it's hello world but it doesn't really matter what it is it's non-empty therefore this is going to evaluate to true and this string will be printed so this is called truthy and falsy non Boolean data types can be converted to Boolean values in a Boolean context that's kind of a fancy way of saying that like a conditional block block is a Boolean context right like the condition is either true or false therefore a data type that's not a Boolean is going to be converted to true or false in that conditional statement so an example of falsy values is first of all the value false itself that evaluates to false in a conditional block none the absence of a value is also falsy it's going to be false if we try to use it in a conditional statement zero whether it's an integer or a float is also considered false an empty string is considered false an empty list in Python is considered false an empty collections of other types which we'll learn about later are also considered false truthy values are basically you know the Boolean value true itself as you might guess any integer that's not zero any float and any decimal value that's not 0.0 any string that's not empty and any collection any list or other collection that has at least one element so they're kind of opposites of each other right if a value is falsy then the opposite of that is going to be truthy can you kind of guess what this code is going to do if we have x equal to 10 do you think X is truthy in this context yes it is right it's not zero so this is going to be printed that's equivalent to saying X is not equal to zero so if you prefer to be more explicit it's okay to do that just check X is not equal to zero and then execute the conditional block the only reason I'm teaching you about this is because it's very very common it's a pythonic way of writing code it's very likely you're going to see code that looks like this I don't want you to get confused so if you prefer to write out your code like this to be more explicit that's perfectly fine but you're definitely going to see code that looks like this at some point you can learn a bit more about Boolean context down below here if you want to there are several others but we're not going to get into that right now let's implement the function is truthy given a value which could be of multiple types I guess we want to return if the value is truthy and otherwise return that it's not truthy so if it's truthy we're returning the string if it's not we return this string and the parameter can be of any type so let's get into this as you might guess we can check if this value is truthy just by using it in a Boolean context the simple one that we know of is an if statement so if value let's return truthy otherwise let's return falsey we could have put that in an else statement or not I'm going to choose not to do that now we're going to see Zero do you think that's going to be truthy like all these values let's just run it to find out but it's going to match what we saw earlier 0 is falsy 10 is truthy 0.0 is false 10.0 is true empty string is false non-empty string is true this is one way to write the solution and we are finished here suppose we had some piece of code that we wanted to execute multiple times like we really really love Python and we want the world to know five times we could obviously print it five times we could even store this string in a variable and reuse it but we'd still need to have this code written five times even if it's a part of a function we'd have to call that function five times so we can get around this with something called a loop specifically this time we're going to be looking at while Loops take a look at this code we have a variable we're calling it I and it's set to zero and we check while I is less than 5 execute this code so it's kind of like an if statement I is definitely less than five there is a condition here that's going to be true or false so this is definitely a Boolean context and we have a colon and then the code that belongs to the while loop is indented and then we print this and then we increment I by one we add one to I this is very important because if this line did not exist I is less than five go ahead and print it and then the loop actually will execute again it's not an if statement which only executes once this will execute multiple times and if we didn't increment I it would just go infinitely long that's an error we want to avoid so we increment I so then I is still less than five and it'll keep going it'll go exactly five times because I will be zero 1 2 3 four once it's equal to five the condition is false and it won't execute anymore it'll stop now it's worth knowing that Loops also don't Define their own scope just like uh conditional statements but now for our challenge we want to print I know how to use while Loops exactly 12 times there's many ways to do this with a while loop but we're going to kind of follow the semantics here I'm going to set I equal to zero while I is less than 12 we're going to print that string I'm just going to copy and paste it so that we don't uh run into any issue so there I know how to use while Loops I'm going to run this just to show you what the error we're going to have is it gives us an error saying the file was too long because we printed like look at this we kept printing kept printing to the point where we just couldn't print anymore the output was too much we only wanted it 12 times but we did it an infinite amount of times to the point that our program crashed so let's fix this by incrementing I by One each time so this will print exactly 12 times another way to have written it would be I is set to one and while I is less than 13 that'll still print exactly 12 times from 1 to 12 once it's equal to 13 it'll stop so just to prove it to you this indeed also works we can use while Loops to print digits not just printing like text for example let's consider this challenge to us in the code editor can we use a while loop to print the digits from 0 to 9 in that order including zero and N so our Loop should execute 10 times and just to give you the syntax this is the overall structure of what a loop looks like let's use that to solve this problem so I'm going to have a variable I'm going to call it N I guess you could call it I whatever you want I'm going to initially set it to zero so we want to print print n 10 times and we want to stop once it equals 10 so we could do this multiple ways we could say while n is less than or equal to 9 go ahead and print it let's indent this and then increment it by one so it'll go from zero up until 9 it'll execute true when it's N9 when it's 10 it'll stop so run this and it works alternatively we could have said while this is less than 10 because as soon as the condition is false the loop will stop this will work the exact same and there you go now another challenge in the code editor can you use a loop to print multiples of 10 from 10 up until 90 including 90 and in that order so what do you think we should do now I mean the overall structure was something like this while n is less than or equal to 9 let's print 9 and then increment it by one this was from 0 to 9 but we want multiples of 10 so obviously we want to start at 10 so n is equal to 10 and the condition uh we don't want to stop at 9 we want to stop at 90 so let's change this to 90 this is not going to do exactly what we want let's print it just to see what we get and what the expected output is we're going from 10 up until 90 but we're incrementing by one that's not what we want we want multiples of 10 so to fix this we're going to increment n by 10 not by one so this will give us multiples of 10 and we get the exact output that we're looking for now let's look at another way of writing Loops these are called for Loops these these are going to be defined with the keyword four so just to look at it really quickly with this Loop we print numbers from 0 to 9 inclusive and this is done with a while loop by having a variable outside of the loop and incrementing it by one each time this Loop is doing the exact same thing up above the only thing is the variable was defined here on this line right after the keyword for and then we use the keyword in this range range is a function when we pass in 10 into this function it creates a range from 0 to 9 inclusive not including 10 it goes from zero up until 9 this number minus one so then it'll print this number and we don't increment it inside of the loop because this is going through the sequence from 0 up until 9 so obviously this is is a shorter way of writing the same code this is more explicit this is less explicit so you might prefer the first way of writing it but this way is generally more common so you can read over this to review the exact syntax of the loop but the best way to really really have it sync in is to actually type it out so that's what we're going to do we're going to write a for Loop to print the numbers from 0 through 12 including 12 in that order we could do it with a while loop but let's try it with a four Loop so just following the syntax here 4 I in range up until 13 right if we put 12 here what's this going to do and then we print I we know we don't have to increment I in this this is going to go from zero through 11 it's not going to include 12 right so if I run this it's going to go from 0 to 11 as you can see here stopping at at 11 and the output that we wanted was stopping at 12 so if that's what we want let's go ahead and add 13 so now it's going to print 0 through 12 which is what we wanted I know this is a little bit confusing CU we have an extra like 13 is not going to be printed that's when we're stopping so now we have the correct output here but for Loops can actually be even more interesting than that it would be pretty limiting for a for Loop if we had to start at zero every single time but we actually don't need to do that the range function can take two arguments let's take a look at that so we have a while loop here starting at two going up until five so this Loop will print two 3 and four it'll stop when we get to five so it won't print five the for Loop down here will do exactly the same thing it's going to start I at two it's going to stop once we reach five and it's not going to print five it's not going to execute that time so it'll go from 2 3 to 4 and it'll increment by one each time so these two Loops are equivalent this is the four loop it's obviously shorter so our challenge now is to use a for Loop to print the numbers from 10 to 20 in that order including 20 so just following the syntax here we want to start at 10 so I'm going to say for I in range 10 as the first parameter if I try 20 as the second parameter and then print I this is not going to work this is going to go from 10 to 19 it's not going to print 20 you can run it just to prove it to yourself it didn't print 20 it stopped at 19 so if we want to print 20 that's the expected output let's change this to 21 now I run it and this time does print 20 we get accepted now again it would be kind of limiting if we had to increment by one every single time in four Loops we actually don't need to do that so look at the code below um this is a while loop I starts at zero while I is less than 10 print it and this time add two to it so this Loop will print 0 2 4 6 and8 once it gets to 10 it'll stop the loop it's not going to print 10 cuz 10 is not less than 10 it'll stop at that point this for Loop down here does the exact same thing remember the first parameter into range is the starting point I is going to start at zero just like up here I is going to stop once it reaches 10 and not include 10 the third parameter though here the first time we're using it is the step that's what we're going to be incing by when we don't Supply a third parameter the step is going to be one by default but when we do Supply it it could be anything we want it to be in this case it's two so this Loop will go print zero then two then four then six and then eight once it's 10 it'll stop so both those Loops are equivalent now let's create a for Loop which is going to print all multiples of 10 from zero up until 100 including 100 in that order uh let's just have the taxs here to the left just to make sure we get this right so for I in range we want to start at zero we want to go up until 100 initially I can put 100 here um but we know it's not going to include 100 even though we that's what we want to print but the Step is important the step is going to be 10 because we want multiples of 10 so here we'll go ahead and print I now if we run this of course we're not going to include 10 we're missing that one so you could put 110 here that will actually work right so this does work but you could actually put any number that is greater than 100 and less than or equal to 110 and I think we'll learn about that in the next lesson so we can also use for Loops to actually iterate in reverse order and this is a little bit confusing so consider this while loop to start off with we start at 10 we want to check is I greater than zero and then we want to print it and then we're actually going to decrement cuz we want to go in reverse order so we'll go 10 then 9 then 8 and all the way up until 1 and once I is equal to zero it'll stop it's not going to print zero so it'll print from 10 to 1 in reverse order this for Loop is going to do something similar it's going to start at 10 and stop at zero but not include 0 and it's going to decrement by one each time so the step here is negative by default it would be positive 1 if we supplied one but this time it's not it's negative 1 and we're going to print I each time it's going to do the exact same thing print from 10 through one knowing that our challenge is to use a for Loop to print the numbers from 20 through 10 including 10 in reverse order let's do that so for I in range starting at 20 stopping at 10 and -1 is the step we're going to print I we actually do want to include 10 this will stop at 10 but we want to print 10 so I'm just going to take the 1 minus that which is 9 this is the solution this is one way to get the solution with a for Loop and as you can see here we match the output there is definitely a more readable way to do this cuz this is kind of confusing to read even for people who are familiar with python so I want to show you a slightly more readable way to do this and here you can see that a shortcut for reversing take a look at this here for I in range 10 remember that that's going to go from zero up until 9 if we take that range and pass it into a function that's called reversed it's going to do 0 through 9 in reverse order so it will go from 9 down to zero it just takes a range and goes through it in reverse order so let's use this to our advantage we want the numbers 20 through 10 well let's consider 10 through 20 we can do that pretty easily like this this will be 10 through 20 but we want to include 20 so we put 21 so this will give us 10 through 20 but it'll do it in ascending order we want to go in reverse order order so let's wrap this entire thing around the Reversed function and it'll do 20 through 10 now in like reverse order so printing this whoops I had a typo I had said reverse the name matters there's a d at the end of it so let me add that D and try this again and now you can see we got the exact same result that we wanted many people prefer doing it this way now to look at nested Loops a very very important concept they come up very often suppose we have a set of integers 1 2 3 we want to get all possible pairs of this set a pair is two values so we want like this pair one two this pair two three and in this case the order of the pairs does matter so we consider two one to be a different pair than one two so those pairs would look something like this 1 one 1 2 1 3 2 1 22 2 3 etc etc and you can see this pair is different from this one cuz the order is different so it's kind of like a 3X3 table right we have three values we take like the square of that and we get a 3 by3 table we get nine values in the output this can actually be accomplished pretty easily with nested loops and the code is very very simple take a look at this Loop we have one loop on the outside for I in range from 1 to 4 not including four so we're going to get 1 2 3 now we have a second Loop we have a loop inside of a loop right so the indentation does matter here we have this Loop J in range from 1 to 4 not including four so once again 1 2 3 and and every time we're printing the pair of numbers this is called a nested Loop and this is what it's going to do first I is going to be one then it's going to execute the code inside so J is going to run from one to three and then stop at four so you can see I is printed first and then J the output is going to be this I is 1 J is one I is 1 J is two i is one J is three so that's one iteration of the outer loop next this Loop stops and then we go back here to the outer loop I is now going to be two and then J is going to be one it's going to be two it's going to be three those are going to be printed you can see that down here 2 1 2 two 2 3 and then finally the third time I is three and then this Loop is going to go through one 2 3 while I stays as three so this way we get those pairs that we wanted in total how many times is this print going to run exactly nine times cuz the outer loop runs three times the inner loop runs three times for like every time the outer loop runs so it's 3 * 3 we get nine total executions nine total uh prints so knowing this let's print all pairs of numbers from this set from 3 4 five where the order of each pair does matter so it's going to be kind of the same as this so we're going to have one Loop I'm going to call 4 I in range from 3 to five but we want to include five so we're going to set this to six six is not included and then for J in range um 3 to six again and then we're just going to print those numbers print I and J so let's go ahead and run this and we got the output that we expected so now to finish off Loops let's look at control flow this is kind of optional you don't need to know it but it's worth knowing because it can make code more readable and it's common to see stuff like this out in the wild so there are a few keywords that can control the execution of a loop from inside of the loop there's one called break when this line of code executes the loop will immediately stop there's another keyword called continue when this executes it will skip the remaining code on that iteration of the the loop and then move to the next iteration I'll show you what I mean in a second but the third one is the most simple pass it literally doesn't do anything and this keyword actually can be used outside of a loop the idea is that like we can't have an empty Loop like you see over here the loop doesn't do anything we're not allowed to have an empty Loop so pass acts as a placeholder it basically does nothing but we won't get an error if we try to remove this we will get an error we don't want that so we leave the word pass same thing with an if statement we can't have an empty if statement or an empty function so the keyword pass will fix that it will not give us an error if we have pass so here's like an example none of this code does anything but it won't throw an error either now the other keywords are a bit more useful this code here we have a loop it's going from I from one through 8 so if I is equal to 3 it's going to continue if I is equal to 6 it's going to break otherwise it's going to print I now the output of this Loop is going to look like this it's not going to print one 23 up until 7 it's going to skip three why did it skip three well because if I is equal to 3 we execute continue it stops everything here it won't even execute this code down here it'll stop and it'll go to the next cycle of the loop the next iteration so if I was three well stop and then go to ials 4 and then continue from there so four is printed five is printed but six isn't printed and seven isn't printed either why is that well when I is equal to 6 we break out of the loop we stop the loop all together not just this iteration but we stop the entire Loop so as soon as I equals 6 Stop the Loop don't print six and don't print anything after that so seven isn't printed either so this is an example of these two keywords these two control flow statements being used now there's nothing we have to do in this challenge we're just going to submit the code to prove that it doesn't do anything and also there's no error cause so just skimming through the code you can see we use pass a few times here we have a loop that's going to print I but we have a break statement before that so the loop is actually not going to do anything it's going to immediately stop same thing down here we have a continue statement before the print so it's always is going to skip every iteration of the loop lastly down here we have an actual print statement this part is going to execute so let's run this and you can see we get the expected output so this is kind of an advanced technique you don't have to be super good at it as a beginner but it can make your code more readable when you uh learn how to use it correctly so now let's talk a lot more about strings CU they're a pretty important data type and we're actually going to start with the length L function so if we're given a string like this for example neat code we can actually call a pretty important builtin function it's Len that's uh what it's typed as but it stands for length so here in this example you can see we're given the string we can call Len on it length and the return value of that is going to be eight in this case because the string has eight characters so let's practice with this a bit we're told to implement the get longer word function so we're given two words in this function word one and word two they're both strings we want to return which one of them is longer now an edge case of course is what if they both have the same length well in that case there is some details for us if the words have the same length return the first word in this case let's check exactly that how do we figure out which word is longer probably by getting the length of it right so let's say length of word one if it's greater than the length of word two obviously we return word one if else then we would return word two but we haven't handled the case where they're equal well I guess we have if they're equal we're going to end up returning word to in this case because the else is going to execute that's not what we want if it's greater than or equal we want to return word one otherwise we return word two so the equal is very important here and of course we don't need the else um this will work but we didn't need the else we could have put this outside of the if statement and this would have worked just the same as you can see here that's that for just getting the length but there is some more that we can do with strings we know that strings are a sequence of characters if we want to access individual characters from a given string we can actually do that like this and it looks a little bit strange at us cuz we have a string hello it has five characters in it we can access the first character by having square brackets after the string name and then putting a zero inside of it that gives us the first character we can get the second character by square brackets and a one we get e and then the third and the fourth and the fifth so the last character actually goes at index 4 even though the length of the string is five so that's strange that's definitely different than you might expect why doesn't it start at one well that's just how programming kind of is you can think of the index as like the offset from the beginning so zero is an offset of zero we're at the beginning and we don't offset at all but one is adding one to the beginning so an offset of one that's one way to think about it but here just for visual purposes first character is at index zero second is at 1 2 3 and four so five even though that's the length of the string is an invalid index by the way this is called zerob based indexing it's pretty common in most programming languages so again like I said if we try to access index 5 we actually get an index error we're trying to access an index that doesn't exist in this string so be very careful about that the length of the string is five but we cannot access the length as an index we'll get a chance to practice this with a few functions so we're going to implement print the first character we're just given a word a string we're going to assume it's nonempty and we're going to print the first character pretty easy to do I'm just going to do that like this we know the first character is not at index one it's at index zero to get the second character we're going to print index one because it's zerob based indexing the second character will be at index one so last ly though this one's more interesting print the last character in a word well that's going to require some information we kind of need to know what the length of the string is so we get to practice taking the length of a string once again remember if we have a word of length five the last character is going to be at index 4 if we had a word of length four the last index is going to be at index three so we can get the last index like this take the length of the word and subtract one and then we print the word the character at the last index just like this so of course we could have put this here we don't need a separate variable for this but just kind of for readability that's what I did so let's run this just to confirm and you can see it indeed gives us the expected output one thing we kind of assumed was that we were going to at least have a first character and a second character and that assumption was okay because that information is provided to us so now let's get into looping now you're seeing kind of a lot of the concepts we already learned coming up once again so looping through a string is very interesting if we wanted to go through every character in this string you might already know how we can do that because take a look at this if we get the length of a string this one has a length of 13 that means the indexes for all the characters are going to be from 0 through 12 so if we create a loop like this for I in range and this is going to be the value 13 will be able to access the index of every single character in the string Now you kind of see why python makes the range function like that if we pass 13 here into the range function we're going to get values from 0 through 12 that's going to be useful that's intentional by python it does that on purpose with this we'll be able to print every every single character in order and each character will be printed separately so it will go on a separate line now let's look at the challenge we're going to implement this function print string characters we're given a string it takes a parameter and we want to print each character of the string separately so kind of just implementing what we just saw earlier so let's see if we can do it from memory for I in range length of the string this will give us all the valid indexes of the string and we just want to print a character we can access the character like this my string at index I and then to print it it's just this simple just pass it into the print function now every character from this string and this string should be printed on a separate line let's just confirm yep hello world good job exclamation mark So Perfect now there's actually a shorthand way so taking a look at this code we have a string here we iterate for I in the range of the length of the string we print I and the character at index I in the same print statement so this in Python will actually print both of those values with a space in between them if we have multiple parameters into the print function we will separate them with a comma and they will be printed with a space in between them so we get the index and the corresponding character at that index but but it's possible we actually don't even need the index if the only thing we cared about was the character itself and just looping through the characters in order there is a shorter way to do it in Python just like this for character in my string we don't even need the range function I think this might be the first time we're seeing the for Loop without the range function so this will iterate through every character in the string and then print that character the variable name in this case is Char it could have been something else it could have just been the character C it could have been I if we wanted it doesn't matter we need the keyword here we need a variable here keyword in and then the string itself so that's kind of the syntax now knowing this we're going to implement a function that's going to print the characters from the two parameters that are given to us so it's going to print all the characters from word one and then all the characters from word two and let's try to do it using the shorthand method so here on the right we're going to do this for character for C in word one print that character and then as soon as we're done with that go through every character in word two and then print that character it's very simple we're not returning anything we just want to print all the characters from this word and this word we don't need to take the length of either word in order to accomplish this so we should be able to get this output with each character on a separate line and that's that's exactly what we get now we have string concatenation we actually learned a little bit about this earlier I think it's so simple that it was fine to introduce it earlier but now let's get a more concrete look at what we're doing concatenation is just a process of combining two or more strings into a single one python allows us to do this with the plus operator some languages allow you to do it with plus sometimes you need a function to do it but keep in mind that when we use the Plus operator we're actually creating a new string combining these two strings so here pretty simple we have hello comma space and then world exclamation mark we add these two and then print the result we're going to get what you'd expect hello world now we are given this function to implement we get two parameters string one string two and we want to return a new string that is the concatenation of both of them but if the length of the string after concatenating them is greater than 10 we are told to return this string instead to long for example if we concatenate these two strings we're going to return good job because the length is not greater than 10 but if we concatenate these two strings it's obviously greater than 10 I think we have something like 14 15 characters something like that in that case we want to return this special string as you might be able to guess we want to obviously concatenate these strings I'm going to call it S3 I guess S1 plus S2 now if the length of S3 is greater than 10 let's instead return that special string to Long exclamation mark otherwise let's return the new string S3 that means it was less than or equal to length 10 so this is the logic let's give it a go and here you can see we did get the expect output great now let's get into a pretty interesting and useful topic called slicing it actually applies to more than just strings kind of like what we've talked about earlier as well we're going to learn all about that later on so I won't get into that just yet but if we have a string like this one and we only wanted to access a portion of that string we can accomplish this with slicing it allows us to get a substring from a string by specifying a range of the indices so for example here we have the start index as one the ending index as five so the syntax is this we have the string we specify the start and then a colon and then the end so we're not accessing A Single Character we're accessing a range of characters from this index up until this one but not including that one so what do I mean well index one is this character the E index 2 is this index 3 is is this index 4 is this and index 5 is this one so by specifying 1 through five as the range we start at one and we go up until five but not including five therefore this range will only give us four characters at index 1 2 3 and four so the output that it prints is going to be l no comma with that in mind we are told to implement this function get substring we're giv given an input string as I'll kind of show on the right but we're given an input string and we're given a start index which is an integer and an ending index which is also an integer it takes a string and two integers and we want to return the substring from the starting index to the ending index but not including the ending index so at first you might think it's very simple all you do is take return input string from start to end right that's it but there's a little bit of a catch so down here it's possible that the ending index is an invalid one it might not be valid it could be out of bounds so if that's the case we want to return an empty string so in that case we'd return an empty string here now they tell us we can assume that start will always be a valid index and that end is always going to be greater than or equal to start but it's possible that end might not be in bounds so given that how do we know if the ending index is out of bounds well the last index would be the length of the input string minus one this is the last valid index length minus one so if the ending integer given to us is greater than the last valid index so if n is greater than length minus one then we know it's invalid when it's invalid we want to do this we want to return the empty string so I'm going to put that inside the if statement now if this doesn't execute we know that the ending index is valid we can assume that so in that case we execute this code so let's give this a go actually it turns out that we had the wrong answer and I think this is a very easy mistake to make so I'm actually kind of glad that I made it so now I get a chance to actually explain why this is actually not quite correct so for example suppose we have the string hello right we have hello like this the index of each of these characters is going to be 0 1 2 3 and four suppose the range that we're given so we're given start is equal to zero and we're given end is equal to four so if we were to try to return that substring we're going to start here at H and we're going to end here at index 4 at o but we know that the second index the ending is non-inclusive so this would give us this substring I didn't mean to make it a square word sorry that's a coincidence but it would give us this substring if we wanted the entire string we'd actually pass in an ending index of five so remember the last index is non-inclusive this will actually not give us an index out of bounds error partially because it will start here and go up until index 4 not index 5 it'll give us every character from 0 through 4 so this is actually valid so what I'm getting at here is that this calculation that we had if end is greater than length minus one which is the last index we actually don't want to check if it's greater than length minus one we want to check if it's greater than the length cuz that is the last valid index we could have for the ending Range Five in this example is valid for the last anything greater than five though doesn't make sense so here we will change this to that so we'll get rid of all these notes and now we're going to change it to this and now it should work and yep there you go it does now there's a bit more to slicing than you might expect so it's actually not necessary for us to pass in the Stars so here we have a string neat code and we don't specify the starting index but we have a colon and then we specify the ending index kind of like with the range function when we have a for Loop it will imply that we're starting at the beginning so if we don't specify anything we're starting at index zero going up until index 3 which is going to be over here but not including index 3 so this is the substring that we get the first three characters and and so that's equivalent to if we specified the starting index as zero now it's also not required for us to specify the ending index and as you might be able to guess if we do that we're going to be starting here at index 4 I think that is here at C and we'll be going up until the end of the string so we'll be getting this part of the string code that's the same as if we were to do this 4 colon 8 so now the last lesson might make a bit more sense because this is index zero 1 2 3 4 5 6 7 so the last index is at index 7 but we pass eight here so we're going up from four to the end of the string right including the seventh index but not including the eighth index so this would also give us code these two are equivalent um if we don't specify either of them if we just have a colon that's the same as just getting the entire string itself the only thing is this would create a temporary copy of that string knowing that our challenge here is to print the first n characters of a given string s and n is going to be a parameter and we're going to return the first n characters so we're not printing it actually we're just returning it so it looks like we can assume that the length of s is going to be greater than or equal to n so we don't have to worry about like an index out of bounds error I guess so how do we get the first n character characters from a string well if you were to look carefully at this example if we had a colon three we obviously get the first three characters and that pattern is going to hold if we had a colon four we'd get the first four characters not including index 4 so here we can say return s from the beginning up until index n that's going to give us the first n characters uh the second one's going to be more interesting actually the last n characters the fact that these are challenging believe me is a good thing you wouldn't want me to give you the easy trivial problems and then when you go out to actually write some real code you don't know what to do so if these are challenging for you that's fine that's a good thing make sure that you kind of understand what's going on here given a string s we want the last n characters from the string and we can assume that the length of s is going to be greater than or equal to n this is kind of challenging it's not quite what you'd expect it's not going to be this it's not going to be return s starting at n colon cuz this will start us at index n but we don't necessarily know that there's going to be n characters in the remaining portion of the string there's no guarantee about that so there is a little hint over here that's going to help us if we want the last character from a string s we would start at length minus one right that would be the starting point because we have one character left in the string from that point if we want the last two characters we do length minus 2 that would be the starting index so if we want n characters our index should be this index equal length of s minus n that's the starting index cuz then we guarantee that there are exactly n characters left in the string starting from this position so now let's give this a go and you will see that we do get the expected output now let's look at reversing a string there's a pretty easy way to do this with slicing the idea is that just like with a range function how we can have a step we can specify the beginning the end and we can have a step we can actually do the same thing with slicing so we have two colons this time and we don't put anything before the first colon or before the second colon but then we put a negative one and this negative 1 is the step so this means we want the entire string cuz we didn't put anything for the start and the end and we want a step of negative 1 so we want the entire string in reverse order so the output of printing this is going to be the string in reverse order and this might make a bit more sense to you if we look at this example here we have start at index one end at index 4 so here through here but not including index 4 so we'll get this l and a step of one this time we don't specify the step because by default the step is one we could have done it we could have had a second colon and then one it would have been the same as this but if we change the step to a negative one then we get the same substring but in reverse order so the step is the third value after both colons knowing that let's implement this function reverse string we're given an input string and we just want to return the Reversed version of that string we want the entire string so all we do here is return input string and colon colon1 just reverse the entire string let's give that a go and you can see it works as expected now it's very important to know that strings in Python are immutable and this is usually actually true for most languages but that means we cannot modify an existing string so look look here on the left we're given a string I will never change if we try to access the character at index0 and try to reassign it from I to X it's not going to work this is going to cause an error because strings are immutable it's going to give us a type error I think so we cannot change individual characters we can only create new strings whenever we're slicing a string that's also true we're not ever modifying the under line string we're only creating a new string with only the portion that we sliced if we did want to create a new string only replacing one character we'd have to build that string ourselves from scratch for example delete the character like the second character which is at index one which is this space character we can get all the characters before that index like this uh colon one it'll stop at one and not include that character and then we can start at index X2 from this W and get every character after that so we'll get this and this string and then we'll concatenate those together to get the new string so that would basically be us deleting this character from this string knowing that let's Implement a function remove the fourth character from a given word and then return the new string and we can assume that the length of the string is always going to be greater than four so we don't have to worry about any index out of bounds error so how are we going to do that it's going to be similar to what we have here so let's get the first part which is I'm just going to call that we're going to get everything before index 4 so we'll take the string get a colon everything up until index 4 but not including index 4 to get the second part I'm going to do word starting not at index 4 at index 5 cuz this is inclusive so we're skipping index 4 we're starting at index 5 and going up until the end of the string then we concatenate these together first part plus second part add them together and return the result so for this example we should return uh let's see actually removes the fourth character so I actually I think I messed up so we're removing the fourth character but not the character at index for so actually I did kind of mess up so before we even run it uh good thing I caught that so here we're going to actually stop at index 3 because uh the fourth character is at index 3 and then we're going to start at index 4 skipping the character at index 3 and then those are the parts and then we're going to add those together here and then return the result so for the first example we should return or delete this T so it should print KN code and in the second example we should delete this character and we'll get hello with just a single L let's see if that is what we get and it looks like it is so we've passed this one now for a little bit about string formatting we saw that we can concatenate strings together but this can be kind of annoying if we have a lot of strings to concatenate there is a more elegant solution in Python and it uses the format method so here uh let's make this bigger so we have a string Alice and we have another variable actually which is an integer if we want to add these together into a string in this format we can do so so this is the string hello comma with these curly braces you are this many years old again some curly braces so the curly braces serve as placeholders and from this string we call Dot format on the string so this function is being called on the string itself and we pass in a couple variables the name which is Alice and the age which is 25 so the first parameter will fill in the first placeholder the second parameter the second argument will fill in the second placeholder so the string that we get is hello Alice you are 25 years old so that's what's going to be assigned to the message over here and then printed this is one way of formatting strings we can be a bit more specific if we wanted to we can put a number inside of each placeholder so zero would tell us that this first parameter is going to go in this spot the second parameter is going to go in this spot if you wanted to do do it that way otherwise it'll just fill them in in order that they appear so this one would go here this one would go here um I prefer doing it this way but if for some reason you wanted to have them in a particular order you can do that as well a third way that some people prefer is called F string so you don't even need to call the do format function and you can do so uh with f so starting with f so this is a special syntax we can start the string with f then have the quotation marks and inside of that string we can have curly braces and directly use the variable name in The Curly braces so obviously Alice would go here the age which is 25 would go here so this is even more concise I think than the previous ones so knowing these three ways of doing it let's Implement a function called say goodbye that is going to return a string in this format goodbye name see you again at hour o' so the name is going to go here the hour is going to go here let return a string that does that and we can do it multiple ways I'm going to use f strings because I think it's the most readable so return F quotation marks and now I'm just going to actually copy this string just so we don't have to type it out but you could type it if you want to and now I'm going to replace the name with well I'm going to wrap it in some curly braces because it does match the parameter name already and then here hour it also matches the parameter name so I'm going to wrap it in a couple curly braces this is the entire code let's run it and you can see we get what we would expect so there's a lot we learned about strings and there's a lot more to learn there's a lot more built-in functions but these are kind of the core ones that I would think you should definitely have a little bit of experience with you don't have to memorize every detail but just kind of knowing that we can do a lot with strings is pretty important so now let's finally talk about lists so these are an interesting data type and the good news is that they have a lot of overlap with strings but they do have a few key differences that we will be going over to begin with a list is just a collection of items that are in a specific order so at first it sounds like a string but item is a general term so here you can see we have a list of numbers not just characters but numbers we can get the length of a list just like we do with a string by calling the Len function and in this case we have three items so the output is going to be three we can index a list just like we can index a string so we get the first element with index zero second with index one third with index 2 so far it's similar to a list now down here you can see we have a list with these numbers a difference between lists and strings are that lists actually are mutable we can modify the values in a list so this is a very key difference so index0 initially was one we reassign it to 10 and then we print it and it works perfectly fine the output we get is 10 and obviously lists can store uh not just uh numbers but they can store other things they can store strings so in the first index we have a string I in the second index we have a string M so this is a list of strings we are printing each word as we Index this list and we can even mix and match types in a single list so we can have one hello 3.14 and true though it's not recommended because it can become kind of unpredictable what data type is in each position so generally this is not recommended but it is possible in Python so we have a challenge we're given a list like this we want to print the following things each on a separate line so first we want to print the second element in the list so recognize that the second element is going to be at index one so that's what we do then we want to print the third element that's going to be at index 2 so just like this third we want to print the first element in the list that's going to be at index zero and then we want to print the length of the list just like this the name of the list is my list so then we print it so those are the four things this is a pretty nice and easy warm-up question nothing super crazy here and we get the correct output this was a list of numbers now to get a bit more interesting there are some operations that we can do on a list now first what would happen when we try to use a list in a Boolean context well here we see we get the length of the list check if it's greater than zero that means the list is not empty of course otherwise we assume the list is empty if the length is zero it's probably an empty list right that's pretty safe to assume um down here though we do the same exact thing except a little bit differently we check if my list so using a list in a Boolean context will be true if the list is non-empty if it's empty then it evaluates to false and we would execute the lse statement so basically this code is equivalent to this code this is what happens when we use a list in a Boolean context if we want to know if a list contains any particular element we can do that with the in operator we say if two is in my list then this is obviously going to evaluate to true and we're going to print two is in the list otherwise we know that two is not in the list and we would print two is not in the list so this is going to evaluate to a true or false value and then we're going to use that to execute one of these conditions so in this case two belonged to the list we can also do the opposite if we want to and that's done in a pretty readable way in Python we don't put the not before the condition we say if for not in my list if four is not in the list this will be true and this will execute if it is in the list then the else block will execute so this is just a way of determining if a list contains a particular element or not in the code editor we're going to implement a couple functions um check if the list is empty if it's empty return true otherwise return false so let's start with that one it's pretty easy if the list is empty so if a length of my list which is the parameter is equal to zero let's return true otherwise let's return false the second function check element in list we're given a list and we're given a specific element we want to return true if the list contains that element and false if it doesn't so let's just check if element in my list return true otherwise return false so this isn't super crazy just kind of applying what we learned here let's execute this and you can see we get the expected output there is a way to actually make this a bit more concise if we wanted to if the element is in the list this itself will be true and that's what we would want to return true if the element was not in the list this itself would be false and that's what we would want to return so can't we just do this return the result of this expression element in list return true or false right so this is the exact same as what I showed earlier similar here if the length is equal to zero we want to return true and this will evaluate to true in that case if it's not equal to zero it'll evaluate to false and that's what we would want to return false so let's just return the result of this expression this Boolean expression if length is equal to zero return true otherwise return false just return the result of the expression this is a nice concise way to do it and we get the exact same output now looping through a list is very similar to looping through a string so here you can see that we can one way do this by getting the length of a list and then iterating for I in the range of that list it will give us index0 through index 4 and we'll be able to print each element we can also Al do it similarly U with a string as this element in my list it'll go through every element and this way we don't really get access to the index of each element but maybe we don't need the index of each element so this would be sufficient if you don't need the index usually it's preferred to do it this way so our challenge this time is to count X we're given a list of nums and we're given an integer X for this function we want to count how many times does X appear in this input uh list and we want to return the result of how many times it appears in that list just to quickly mention here we have a type annotation here where we have nums and it's a list of integers that's the type annotation and we have an import statement up here the only thing this is doing is allowing us to add the type annotation because if I get rid of this then we get an error we're not allowed to use this type annotation so that's just how python is so you don't have to you know know too much about for now but we're given this list of numbers we want to count how many times does this appear it probably makes sense to Loop over every element in nums I'm going to say for n in nums and we want to check if n is equal to X for every time this occurs we should probably increment the count by one I'm going to call it result you could call it count you could call it whatever you want this is counting how many times x occurs and that's what we want to return after we Loop over the entire input so the only thing left for us to do is actually count how many times it occurs so in this if statement if we find An Occurrence of X let's increment the result variable by one let's just quickly look at the examples five how many times is it going to appear it's going to appear twice here five appears zero times in this seven appears looks like three times in there so we should get 2 0 3 let's see if that's the expected output 20 03 yep and we got it so we're good to go here there are some pretty neat functions that we can call on a list in Python some pretty nice built-in functions these are kind of the important ones these work on Integer list specifically so we have a list of numbers here we can call a built-in function called sum it's going to compute the sum of all elements in this list which is 15 we also have Min it's going to calculate the minimum of all elements in this list in this case it's one we have another function Max it's going to get the max of all of them in this case it's five so pretty self-explanatory we are actually going to implement these functions from scratch ourselves we're given three functions get some which is as you can imagine we're going to compute the sum of elements in this list we're going to get minimum get the minimum from this list and get Max get the maximum element from that list we can assume that every list is going to have at least one element and also if we want a challenge let's try to do it without using the builtin functions let's try to Loop over the elements and manually keep track of the sum the minimum and the maximum also recall that since these are built-in functions let's try not to use variable names that conflict with these built-in functions it's generally not wise to do that so Let's uh try to code this up now so to get the sum this is probably the easiest one let's just call it total or you could call it total sum I'm initially going to set it to zero and then I'm going to go through every value in the input to Total I'm going to add every value in the input and then I'm going to return it so this is just going to sum every value and then return the result next we have get minimum and the variable name I'm going to use in this case is current minimum and if we try to assign it to zero to start off with this is actually G to cause a bug let me just fill out the rest of the function so I can explain it to you obviously we're going to go through every element in the input if n is less than what our current minimum is let's reassign it now we found a smaller element let's set current minimum equal to n this seems like it works but consider the example down here get minimum from these four elements if we initialize current minimum as zero none of these elements are smaller than zero so the result we end up returning is zero but zero doesn't even show up in the input so this is kind of a bug what initial value can we possibly set this element to well the good news is we don't actually have to come up with that there are ways to set this to like an infinitely large number which would make sense sense but the best and most simple solution to this is just initially set this as the first element from nums so either the first element in nums is going to be the smallest value in the input so on this example it's seven that's not the smallest value in the input but that's okay because we iterate over every element in nums we'll go to seven we'll go to three and we'll find that three is smaller than seven so we'll execute the if statement and current minimum will be assigned to three if there's a smaller element it'll be assigned to that as well but three is the smallest in this case so that's what we end up returning so it doesn't really matter what we Set current minimum to as long as it's a value that's actually present in the input array and as you can guess get Max is going to be very very similar I'm going to call it current Max I'm going to initialize it to just arbitrarily the first element in the list and this is what we're going to try to return current maximum and then we're going to go through every number in nums if n is greater than our current maximum then assign current Max equal to n we're trying to get the largest value and this is how we do that so now we've completed these functions let's go ahead and run this and we got the expected result now there's a lot more that we can do on a list we don't just want to sometimes change individual elements and reassign them in a list we can also add elements to the end of a list lists are mutable in Python we can modify an existing list so we can add to the end of a list with the append function this is not a separate function this function is called on the list itself we call it with append for so we had a list initially 1 2 3 we append for goes at the end of the list like this so couple clarifying things when we call a function on a variable itself it's called a method this is more related to objectoriented programming which we're not going to cover in this course but we will be covering in the object-oriented programming course of python so it's not super important to know it's just if you're wondering why do sometimes we call functions with a period at the end of a variable versus why do we sometimes call like the function and then pass in the variable this is kind of the reason why a list is considered an object in Python A String is also considered an object but again we're not getting into that for now but also notice that the length of the list initially was three after adding an element now we have four elements so the length also increased so we are given this function to implement we're given a list and a bunch of elements in a second list it should take every element from the second list and add it to the end of the first list and then return the new list uh let's do exactly that we're not just appending one element to this list we're going to appen multiple elements so how do we get those elements probably with a loop let's say for n or you could call it element or whatever you want for n in elements let's take this element and append it to my list my list. append this element and after we've done that let's return the new list my list technically we are actually modifying this list by doing this we are modifying this list we're not creating a new one so that's a subtle point but it is kind of important the output here you can see is what you would expect not only can we add elements to the end of a list we can also remove elements from the end of a list so given a list like this 1 2 3 we call pop on it it will remove the last element in the list which was three so we're left left with one two by default pop is going to remove the last element but we could remove an element from a specific index if we wanted to so here we have 1 2 3 we call Pop zero this does not remove the element zero it removes the element at index Zer so that's very important to know the element at index0 is one so it's going to remove it and the result is going to be 23 and then we're going to call Pop again so what's the element at index0 now well it looks like this is the array we have this is the list that we have and the element at index0 is two so removing that leaves us with a list of a single element which is three so knowing that the challenge is remove from my list the element at this index and that's all it's going to do and then it's going to return the result so let's just get started with that here we can go ahead and just call my list.pop at this index and then return the result return my list so that's pretty easy to do now the second one here is Pop N from my list so what this means is pop the last n elements from the list and return the modified result so if n is one we just pop from the list once if n is two we pop from the list twice etc etc so for this it's probably best to use a loop so let's do something like this while n is greater than zero then call my list. pop so pop and then decrement n by one so if n is two for example this Loop will execute twice we pop from the list twice and let's not forget to return the resulting list my list that's all we want to do and just some clarifying notes Here we can always assume that the index that we're removing from is going to be valid and we can assume that the list that we're popping from is going to have at least n elements now let's go ahead and run this and we get the expected result now let's try to find elements in a list and thankfully there's a built-in function to do that as well so we know that we can check if a list contains a specific element with the in op operator that's pretty easy but what if we want the index of that element so for example we're given a list like this 1 2 3 4 5 3 we call index 3 this will tell us the index of the element three it occurs at index 2 so that's going to be the output but there's actually two copies of three in this list why did it give us index 2 and why didn't it give us index 5 cuz that's the index that this three occurs at well this function is always going to give us the first occurrence of three the first one from left to right is here it's at index 2 so that's the return value that we get it's possible that we call do index and the element doesn't even exist in that case we would get an error a value error so keep that in mind given this we want to implement this function find index we're given a list of numbers and a target number number we want to know what index does this number occur at it should return the first occurrence of the Target number in the list so similar to the index function and we can assume that the target will always be present in the list obviously the easiest way to implement this function would be to do something like this return nums do index of the Target and since we can assume that it's always going to be present we never get an error so running this code you see we get the expected result but for a challenge let's see if we can implement it ourselves without calling the built-in index function let's try to do that we want to Loop over every number in nums and if we find it if n is equal to the Target we want to return the index we know for sure that we will find the number the target definitely exists but the way I wrote this Loop how do we get the index well I didn't do it correctly we used a for Loop without the index So This Is An Occurrence where we actually need the index so I'm going to rewrite this Loop I'm going to say for I in range of the length of nums and then to get an individual element at this index I'm going to say n is equal to the nums at this index so now we still have n down here but if this is true we're not returning n we're returning the index we're returning I and and so we went through the numbers from left to right so we will find the first occurrence and then return its index so this will also work let's run it and you can see it works now the good news is that we can also slice lists and slicing them is exactly the same as slicing a string so we have colon we have the starting index and the ending index so index one to index 3 but not including index 3 so we get 2 three but not including this one so this is the subarray that we would get same thing here starting from the beginning going up until index 3 but not including index 3 so we get that 1 2 3 from here starting at index 2 going up until the end of the list we will get 345 and here going from the beginning to the end in reverse order this is the negative step we'll get the entire list in reverse order that's exactly what we have here so pretty similar to with the string but there's one thing about slicing or indexing that we didn't talk about what if we use a negative Index this is actually not invalid it's not going to give us an error because python allows negative indexes it can sometimes be helpful because if we want to access the last element in a list we just pass in negative-1 it'll give us the last element if we want the second to last element we pass in -2 the third to last element at index -3 so so this works like this it's kind of equivalent to doing it like this if we wanted the last element without using negative indexes we would have to take the length minus one that would give us the last index here and it would give us the last element and the second to last element would be length minus 2 third to last element would be length minus 3 so these are equivalent to what we have up above obviously up above is a bit more concise but it's also kind of confusing if you're new to negative indexes it's very common though to see code that looks like this with python so now our challenge is to get the last three elements in this given list it should return a list containing the last three elements and we can assume that the length of the list will always be at least three so how do we get the last three elements well if we wanted the last element we would do something like this my list starting from the length of my list -1 this is the last index in the input we can do length minus 3 and this is the third to last element and then we can put a colon after it and then this will give us from the third to last element up until the ending of the list and we can return this and it'll work just like you would expect it'll give us the last three elements from each of those lists but there is an easier way as we kind of saw with negative indexing we can do the same exact thing we can get the third to last element with -3 as the starting index and then go up until the end of that list so this is equivalent to doing it like this so both of those work the exact same lastly we're going to briefly talk about tupal the good thing about tupal is that they are very very similar to lists except these are immutable so kind of like strings these are immutable we cannot change a tupal tupal are declared with your rounded parentheses not square brackets lists are square brackets uh tupal are round parentheses but these are still ordered sequence of elements they could be numbers they could be strings they could be whatever type so we can print it and this is the output we would get we can index a tupal we can get individual elements from a tupal just like this just like with a string just like with the list we can also slice a tupal because when we slice we're not modifying the existing tupal we're creating a new tupal with that slice so the slicing rules are the exact same but again we can't modify a tupal if we try to change the element at index zero we get an error just like we would with a string so we can't do that and if we can't modify a tupal you can imagine we can't call a pend on it and we definitely can't call po on it either since these would modify the tupal but we can call sum Max and Min on a tuple because these don't modify the Tuple they just calculate some value based on the values in the Tuple tupal are pretty common to store related data especially if you don't want to modify that data we are given a function to implement create pair we're given a string name and an integer age we want to combine these two into a tupal and return it the T it should contain the name first and the age second so just like with lists we can combine data types in a tupal we can have multiple data types in a single tupal and you can see this is the type hint for a tupal and we needed an import for that so this tells us we're returning a tupal where the first value is a string the second value is an integer so here we're going to return a tupal tupal are with round parentheses and we're going to put the name first the a AG second and then return this so let's go ahead and run that and we get the expected result sets are another really important data type in Python they're pretty similar to lists but they have a few pretty key differences one sets are not ordered so that means that you can add a bunch of elements to a set but the order of those elements is not guaranteed so if you were to try to iterate through the elements in that set the order could be predictable so don't rely on the order of elements within a set another important point is that sets can only contain unique elements you cannot have duplicate elements in a set so let's just briefly look over some code for a set here you can see that the set is declared with curly braces not square brackets that's how lists are declared but a set has curly braces we have 1 2 3 but again the order of these elements is not necessarily guaranteed so here you print the set and you might get something like this and we have the same set in let's say reverse order and you might get something like this so again no guarantee about the order so here we declare an empty set and we actually do that with the keyword set and then a couple parentheses so we're calling a function here to create an empty set we're not doing it with empty curly braces and the reason for that is that empty curly braces actually create a different data type which we'll be talking about pretty short but here we first add one to the set then we add two to the set and then we try to add one again that's not going to do anything one already exists in the set we cannot add duplicates so our set will look something like this when we print it just one two so now our challenge is given a list of numbers create a set from those numbers so the idea is the easiest way that we might know how to do it is just to go through every number in the list of numbers and then add it to a set well first let's create an empty set let's just call it my set and we'll create an empty one like this and now to add to a set we call the function not aend we do that with a list but with a set we do add not a pen so uh just be very careful about that so now we're going to say my set add this number and then after that we're going to return my set so let's just run this and it works as you would expect there are a few more operations we can do on a set so removing is pretty important and we can remove based on a value so we're given a set like this we try to remove two and then we're left with a set like this so one three two has been removed if we try to remove a value that doesn't exist we're going to get an error so try to be careful about that now how would we know that four doesn't exist in the set before we try to remove it well there is a way and I think we are kind of cover that towards the end of this part but just like with lists we can iterate over a set the order that we iterate over them is not necessarily guaranteed so again the order could be unpredictable but we can still iterate over a set with a for Loop similar to how we would with a list we can also actually convert a list into a set very easily so this would have been helpful to know for the last uh challenge but we can just call set and then pass in a list and it'll convert it into a set so if we had a list like this it's going to turn into a set like this and that's because the duplicates have been removed a set cannot have duplicates and if you're curious about why that is I would check out the data structures and algorithms for beginners course because these sets are implemented with hash sets under the hood we can also check with a set if an element is contained in that set so we have a set like this cat dog Mouse we check is this string in the set it evaluates a true cat exists in the set what about lion is it in the set nope it doesn't show up so this evaluates to false so now our challenge is to implement a function count the number of unique words in a given list and it's possible that the list could be empty in which case we would return zero so how are we going to do this well we didn't really learn yet how to take the length of a set but we actually don't need to know that because the easiest way to do this would just be to eliminate the duplicates so we can do this just like this we can convert the words into a set like this word set and pass in the list of words in here and then I'm going to declare a variable let's call it count so initially it's going to be zero and now we're going to Loop over the word set so I'm going to say w inword set and I'm going to increment the count by by one we don't even really use the word so the word set will not have duplicates so that's why I'm doing this how many times we Loop over is however many unique words are left over and then we can return the count so here we can return the count and so this will solve the problem for us and there actually is a easier way we learned about the length function earlier we can call length on a string we can call it on a list so why wouldn't we be able to call it on a set we can actually do exactly that so you can convert this list of words into a set and then just return the length of that word set so that's another way of solving this now to just practice with some sets so we're given a function called contains duplicate this might look familiar to you if you're solving some leak code problems lately but we're given a list of words and we want to return true if that list of words contains some duplicates otherwise we want to return false there's many different ways to solve this problem so let's try to get creative so does the list of words have any duplicates well one way to solve this would be to create let's say a set my set and initially it's empty and then let's go through every word in words we check is this word already been seen before well I guess before we even do that we want to take every word and add it to the set so let's say my set. add this word before we add it though we want to check is this word already been seen before have we already added it to the set before and if we have then we have duplicates so at that point we know the result of this function is going to be true so let's just return true in that case otherwise we go through the rest of the words and then if we never see a duplicate out here we can return false so this code will work as you can see here but there's a slightly easier and more elegant solution why not just take all the work and throw them into a set cuz that's going to remove all possible duplicates so let's do something like this so now we know that there's a duplicate if the length of the set is smaller than the length of the list so we can check is my set the length of it less than the length of words if it is we can return true so in other words we can just return the result of this expression if it's less than there are duplicates if they're equal then it's going to return false so running this you can see it also works so now let's learn about one of the single most common data types in Python and one of the single most useful they're called dictionaries and they are used to store key value pairs so the simplest way to think about it is in terms of a table so we have a mapping from a key to a value so in this case the key is going to be a string and in this row it's Alice and Alice maps to the value 25 Bob maps to the value 30 Charlie maps to the value 35 pretty simple conceptually right you might be familiar already with maps or hash maps and a dictionary in Python is pretty much just a map or a hash map so that's another way of calling these so you might hear them called Maps or hash Maps it's kind of the same thing as a dictionary so here is how we can declare a hashmap in Python notice that it looks similar to a set we still have curly braces and the values are separated by commas but the thing itself like each row in that table is going to be this it's going to be a pair and the pair is separated by a colon the part that's to the left of the colon is going to be the key the value to the right of the colon is going to be the actual value so you know that can be thought of as like this visual representation and down here this is just another way of writing the exact same thing this is just formatted on multiple lines maybe this is a bit more readable for you so feel free to format it like this if you would like another way of creating a dictionary is like this so now you see we have empty curly braces empty curly braces in Python are empty dictionaries that's why we couldn't use that for an empty set so this is an empty dictionary and to insert a value into a dictionary is actually not going to be with a function like add or anything like that the easiest way to do it is like this to take square brackets and then put the key in those square brackets so this time we have a string Alice we're mapping that to the value 25 Bob is mapped to the value 30 Charlie is mapped to the value 35 so it's kind of like an array except you know this is not an index this is kind of the key now our challenge is to implement two functions one is create dict so create a dictionary dict is short for dictionary I'm not making that up so we're going to take a name String and an age integer and we're going to create a dictionary an empty dictionary mapping the name to the age and then returning the dictionary so let's start with that cuz it sounds pretty simple so create a dictionary the easiest way I guess to do it is calling it something like this mapping the name to the age so we're taking the string that's a name mapping it to the age separating them by a colon and then we're going to return that obviously another way of doing it would have been to create uh the my dictionary make it empty and then map the name to the age like this so that's another way of doing it you can do it however you'd like but I'm going to leave it like this so the next one is list to dictionary so we're given a list of words and we want to convert it into a dictionary by mapping each string to its index in the list and then returning the resulting dictionary so the mapping the key is going to be the word the value is going to be the index you know you can think of it like this this string might map to Value zero this would be value one this would be value to these are going to be the indexes so let's do exactly that how do we do it well first we should iterate over the words so one way to do that is like this remember we're going to need the index of each word so we want to kind of keep that so I'm going to do it like this I in range length of words now to get an individual word at indexi let's just do this W is words at indexi okay now we have the word and we have the index so let's create a dictionary up here I'm going to make it empty first of all and then I'm going to map my dictionary I'm going to take the word and map it to the index and I'm going to do that for every single word in here and then I'm going to return it so down here I'm going to write return my dictionary so now let's run this and you can see it works as expected now just like sets dictionaries cannot contain duplicate keys so think of a dictionary like this A1 B2 C3 if we print a we're going to get the value one so this is how we can access a value from a dictionary we can read the value that belongs to this key now if we try to assign that key to a new value like four and then we print it we're going to get the value for it's going to overwrite this value we don't have two keys with the key a so it's overwriting that we cannot have duplicate keys in a dictionary now the values themselves can actually be of pretty much any type in a dictionary so here you can see a dictionary where a maps to a list and here you can see B maps to a set and here you can see uh c maps to another dictionary here so it's kind of hard to see but here c maps to a dictionary we can have nested dictionary so that's kind of interesting we won't get super into that but just know that it is possible in Python now even though the keys have to be unique in a dictionary the values don't necessarily have to be unique so a could map to one B could map to one and C could map to one one they can all map to one that's perfectly fine we can also check if a key exists in a dictionary by using the keyword in just like we do with sets just like we do with lists knowing all of that let's implement the challenge so we want to perform the following operations on the given dictionary print the dictionary itself okay that's pretty easy I'm going to start with that so here let's just print your dictionary next we want to print the value of the key a how do we get that that's pretty easy Your Dictionary at Key a so this is going to give us some value whatever it is let's print it so just like that third print true or false depending on whether the key D is in the dictionary so how do we know if the key D is in the dictionary well just check is D in your dictionary and we want to print the result of that so let's do this now fourth reassign the value of the key a to four so right now it's 10 let's reassign it to four so this time we're not even printing anything so this key is going to be mapped to four now and now print the dictionary again that's the last step so let's go ahead and do that print your dictionary not super crazy stuff just getting kind of familiar with dictionaries but we can see that we get the expected result now we can use the length function just like we could previously to get the length of a dictionary now this specifically is going to give us the number of key value pairs as you would expect this dictionary has three key value pairs so that's the result that we're going to get when we try to calculate the length of it just like with sets the length itself isn't really going to help us Loop over the dictionary if we want to Loop over the dictionary the easiest way to do that is with the keyword in on the dictionary so we create a variable here we call it key we could have called it anything we wanted to but regardless of what we call it it's always going to be the keys in the dictionary so when We're looping over the dictionary like this we're going to get all of the keys a b c and with the key itself we can of course get the value so with the dictionary we could just say my dictionary with the key and that'll give us the value so we can still get both the key value Pair by iterating over it like this now there is a slightly more concise way to do it with the items function we call items on the dictionary itself and then we try to Loop over it that will actually give us both the key and the value at the same time and then we can print it so it's kind of the same as what we did up above it's just a bit more concise because we don't need this line we can automatically get the value in a particular variable but it's pretty optional you don't really have to know this it won't really save you that much time now given this challenge let's see what we got to do get the dictionary keys from a dictionary so we're given a dictionary and we want to just get all of the keys from that dictionary and then return them in the form of a list so the keys in this case are going to be a list of names so let's start with that so we're given this dictionary we probably want to Loop over it right we want to say for key or we could call it name or whatever I guess I'll call it name for name in ag dict and we want to take this name and add it to a list I'm going to call that list the result and then I'm going to say result. append this name and we do that with every single key then we go ahead and return the list of keys so great now let's do the second part get the dictionary values from a dictionary that looks kind of similar to the previous one so this time instead of getting the keys we want to get the values so it's going to be very similar to what we have here I'll still write it out from scratch though so here we're going to go over for every let's say name in age dict and then we want the value we want the age so we can say age dick given the name we should get the corresponding age we also want to have a list here let's make it empty and then to this list we want to append not the name but this time we want to append the age and then after all of that we can go ahead and return that list so let's go ahead and run this and you can see we get the expected Result One very common use case of a dictionary is to count the occurrences of elements in a list so for example we're given a list like this with a bunch of numbers some of them are repeating we want to count the quantity of each element so if we were to do that for this list it would look like this we have three occurrences of one three occurrences of two and three occurrences of three so let's Implement a function that's going to do do this well similar to this the function is actually going to count the characters in a given string and then return a dictionary which counts the occurrences of each character now the only challenging part about this well it might be challenging in general but an even more challenging part about this is going to be the fact that if we try to access a key that doesn't exist in a dictionary it's going to give us an error so we have to be able to handle this case and I'm going to show you how we can do that so let's first just start off looping over every character in this string so we can do that like this for C or for character in word we're going through every character we want to have a dictionary I'm going to call it count it's going to initially be empty we know this is what we're going to return so I'm going to put that return statement down here now we just have to count the occurrences of every single character how do we do that well normally we'd say something like this for the count of this character increment it by one the easiest way to do that is kind of like this right now it's possible that the key the character hasn't been inserted into the dictionary just yet so it's going to give us a key error because this is the same as writing it like this it's the same as saying count of the character plus 1 so obviously how can we access the count if we haven't even inserted the character yet so before we can do this let's check if the character is not in the dictionary it's not there let's just initialize it to zero for example so now it's set to zero so this way we'll guarantee that we'll never get a key out of the dictionary error we'll never get that type of error and it'll always be set to zero so either we'll set it to one here or um maybe the key already existed and then this won't execute and we'll just end up incrementing whatever value was already stored there so let's go ahead and run this and you can see we get the expected result another way of writing this code that might make more sense to you is something like this instead of having an if statement by itself let's also have an lse statement this would mean that if a character doesn't already exist and this is the first time we're seeing the character for this hashmap let's set it to one if it already exists let's increment it with this code so so this might be a bit more simple if you're a beginner so I just want to show you that this also works perfectly fine as you can see we can also remove pairs from a dictionary we can remove them based on the key so we can call the pop method something like this we're given a dictionary that looks like this we call do pop on this key it'll remove this key value pair as you would expect now if we try to call pop on a key that doesn't exist we're going to get a key error so keep that in mind if you don't want to worry about handling the key error case there actually is a builtin way to do that so here we have a dictionary with keys a b c we try to remove the key D but we actually pass in a second parameter we pass in zero so what this is going to do is say try to remove this key if the key doesn't exist just return zero and don't throw an error this time even though the key doesn't exist no error is going to occur now another way of deleting from a dictionary is by using the delete keyword so we have a key like this we can delete that key with this keyword knowing that let's try to take on the next challenge we want to implement the remove Keys function we're going to be given a dictionary and we're going to be given a list of keys we want to remove all of these keys from the dictionary now it's possible that some of those keys don't even exist in the dictionary in that case we want to ignore them we won't want to throw an error we don't want an error to occur so there are different ways that we can handle this and I'll try to show you a couple of them so let's start by iterating over every key in the list of keys so we want to remove the key from my dictionary so first let's just check that the key exists in the dictionary we can do that like this for a key in my dictionary if it's in the dictionary this will evaluate to true in that case we can remove it we can say my dictionary. pop this key after all that's done we can go ahead and return the dictionary after removing all of those keys let's run this to make sure that it works and you can see it does now there is another way to do this without the if statement by using the second parameter of pop so it'll try to remove this key but if it doesn't exist we don't want an error to occur so we're just going to pass in a second parameter in this case I'm going to do zero you could do one you could do 100 if you want I'll just prove it to you I'm going to do 100 this is going to be the return value from this function if the key doesn't exist nothing else is going to happen let me just run it and you can see it works as you would expect another way of iterating over a dictionary is by using values if we only care about the values in a dictionary we can skip the keys and do something like this take a dictionary like this call all do values on it and iterate over that so this will only print the values 1 2 3 it'll ignore the keys so this is useful if you only wanted the values but it's even more useful if you want to take the values from a dictionary and then convert them into a list because we can say my dictionary do values and then call list on that and then it'll convert one 2 three into a list that we have here in this variable so knowing this let's take on the last challenge in the dictionary section once again let's Implement get dictionary values from this dictionary so it'll be a little bit easier for us now that we know what we know cuz we're going to take this dictionary just get the values from it convert them into a list and then return that so it's pretty easy to do age dictionary we don't have to Loop over it we can just say age dictionary. values and then call list on this and then we can return that so I'm going to go ahead and run this and you can see it works as you'd expect now let's learn about reading input we spent a lot of time printing output to the console but we can actually read input from the console as well if you're running programs locally you can actually type manually the input into the console and then your program will read that input once the program is running but to simulate it in this course we're actually going to be read some predefined standard input so input can come from a stream called standard input again this can be user data like that user is typing in or it can be predefined input that we are going to supply in this example and throughout the course how we can do that in Python is actually pretty easy we call the input function so this is going to do a couple things input takes in this parameter in this case this argument this string enter some text so this is going to be printed to the output and then one line of input is going to be red and then this function is going to return that line and then store it in this variable and then we're just going to print that line so that's what this code would do whether you run it in this console here like in this code editor or if you ran it locally it's going to do the same thing it's going to read one line of input and then print it and also print this part too the interesting thing though is that this part is not going to be printed on a separate line so this is going to be printed as you can see down here enter some text and suppose the input was some text then this would be printed on the same line that's at least going to happen in our environment where the input is predefined if you were typing out the input then you might uh see it twice but here knowing all of this let's now take on the current challenge read and print input this function should read a single line of input from the console and print it to the console The Prompt that we should provide is going to be this reading one line of text into a string let's just start with that we know we can read a line of input like this calling input and then passing in the string they tell us that this is the string that we want to pass in so I'm just going to copy uh and paste it whoops there we go so now I've printed it so what's the other thing that they want us to do okay so this is going to be red into a variable uh and then we want to print whatever line that we read so let me just store this in a variable called line and then here I'm going to print that line and now we've created the function and they also want us to call this function exactly twice um so I'm going to do exactly that read and print input once and then I'm going to copy that line and then write it twice so now let's go ahead and run this to see what exactly happens so this is the first time that the output is a bit more interesting we actually have input here so this is the hardcoded or predefined input that we are going to supply to your program so here it's one line This is the first line neat code provided you and this is the second line neat code provided you so the output of our program though was this we called input twice so it makes sense that we have reading one line of text into a string and then we printed that line This is the first line n code provided you and then we entered that prompt again reading one line of text and then we printed that line this is the second line n code provided you and that's exactly the expected output so this is definitely more interesting than just printing to the output we're actually reading input this time now one common thing to do with reading input is actually type conversions because whenever we read input we are always going to get a string so let's learn about that so first of all here we can see that when we call input we actually don't need to pass a string into this function it's an optional parameter if we don't pass a string in nothing is going to be printed but we're still going to read a single line and then store it in this variable and then we're going to print that line This is perfectly acceptable like I mentioned whenever we read input using this function input it's always going to be returned in the form of a string but if we're expecting a different data type like if we wanted somebody to enter their age we're expecting it to be an integer so we can get that input and then we can call Int on that input on that string and then convert it into an integer and then it'll be stored in that variable as an integer if we wanted it to be we could do the exact same thing with a float if we wanted to like for example with temperature now you have to be careful about this because if the user does not enter a integer if they entered some string and we try to convert it into an integer we're going to get an error so that's why you have to be careful and we'll actually learn a way of handling that case um in the next section so for now let's implement the challenge we're going to read an integer this should read an integer from the user and return it the prompt should be empty so read integer we're going to obviously call input The Prompt is empty and we're expecting it to be an integer so we're going to assume that it's going to be an integer so we're going to convert that input into an integer and then we're just going to return it so just like this pretty easy we could have stored this in a variable if we wanted to maybe that makes things a bit more readable so let's say the integer I guess I'll call it that and then here I'll return that integer so maybe this is more readable for you um but now let's do the second one read float so this is going to be very similar we also want the prompt to be empty so here let's call input and then convert it into a float and then return that so I'm going to call this my float because I don't want it to conflict with the keyword float and then here we'll just return my float so now let's run this to see what happens we can see that this is the user input an integer an integer and integer and a float a float and a float so this is the input to our program and then our output is the same because we're just taking in the input and then printing it looks like things worked well for us now sometimes it's not that easy sometimes we have to do more than just convert from one type into another this is actually called parsing in programming it's a very very common thing you're going to come across it a lot so it's definitely worth knowing so consider this suppose we read a line of input that looks something like this a string 1 2 3 where the values are separated by commas but we want to convert this into a list of strings so we want it to be a list of 1 2 3 well that's very easy to do actually in Python we can take a given string and then call a builtin function on it on that string we can called do split and we can pass in the string a comma in this case this is called the delimiter that means that this is what's going to be used to split this string it's going to split it here and it's going to split it here and the pieces left over are going to be 1 2 and three those are going to be converted into a list of strings and then that's the result that we're going to get from this do split and now we print it and that's what we have so knowing this let's Implement a pretty interesting function read integers It should read aign from standard in without printing anything and return a list of integers we can assume that every line is going to be in the following format 1 2 3 4 5 where the values are separated by a comma so we want to return a list of integers in this case interesting so the first thing first we should do is read a line we want this function only to read a single line let's call input and we're not going to print anything I guess to the output so let's store this in a variable right now it's just a string it's line so we want to split this right so let's call Dot split on it with the comma so this will give us the pieces you could say this will give us the list of strings in this case I'll call it so it'll be a list of strings where the strings represent numbers so now we want to convert this into a list of integers and I'm going to create a variable for that here it's going to be a list now I'm going to go through every string in the list of strings and I'm going to convert each one into an integer so we're going to call Int on every single string and then we're going to take this and append it to list of integers so do append passing in this integer if we did this without calling integer on the string we'd get a list of strings again that's not what we want we want to convert every string into an integer and then return that so let's do return list of int so now let's run this and see what we get we can see that we got the expected result this was the input if you're curious and our output is going to be this so these are lists of integers not lists of strings now for a little bit of practice let's practice what we learned we're going to implement a function add to numbers it doesn't take any parameters because we're going to be reading the input from standard in so so the function should read one line from standard in which is going to contain two integers separated by a comma and we're going to take those two integers compute the sum of them and then return that so we can assume that the standard in is going to look something like this every line is two integers separated by a comma so this function is going to read one line of input from standard in without printing anything and then let's call line. Spit with the comma so now this is going to give us an array of numbers so let's assume that these numbers are obviously in the form of strings right now and we want to convert each one there's only going to be two of them we're going to convert each one into a number let's get the first one like this and then let's get the second one like this at index one now we can't really if we add these together it's going to be string so it's going to be string concatenation that's not what we want we want to take this number convert it into an integer not a string and same thing with the second one convert it into an integer and then return the result of this so this is one way to do it you could have obviously put these on separate lines and stored them in variables if you wanted to but this is the core solution to this problem um let's see and it does look like it works so we read this input and we compute the sums of each Pairs and then return those now let's get into exception handling it's a very important part of programming in general so when an error occurs in a program it usually causes the program to crash that's not good and python is no exception no pun intended this happens in pretty much every language so if you had something like this 10 / 0 well the program doesn't know what to do computers don't know how to handle that because 10 divided 0 is pretty ambiguous we don't know what to do so the program stops it crashes so means here that any code after that is not going to execute we have a print statement after that it's never going to execute because this is going to crash the program but there is a way to handle this in Python and in other languages as well in Python it's done by using the try and accept blocks so anytime we have some code that might potentially cause an error we want to put it in a tri block so this kind of looks like an if else statement in terms of the syntax right so we put the code in the tri block so this in this case is going to cause an error if we had 10 / 1 it would not cause an error but we have 10 ided 0 it's going to cause an error and that means that the code in the accept block is going to execute so this is our way of handling this error it's saying if this causes an error we stop executing that code and instead execute this code so we would print an error occurred maybe we could say you tried to to divide by 0 don't do that that would be a bit more descriptive but then here we have the try accept blocks and then code outside of the TR accept blocks this is always going to execute whether there was an exception or not an exception basically means an error occurred so if this didn't cause an error like if we had 10 divided 1 the accept block would never execute so keep that in mind this whole concept is called exception handling and again it's used in most programming languages most modern programming languages our challenge here is to implement this function divide numbers we're going to divide a by B and print the result we're not returning anything we're just going to print the result but if an error occurs we're going to print an error occurred instead of printing the result so let's keep that in mind let's uh implement this here so we know we're going to try to do result is let's say equal to a ID B obviously this could cause an error so let's put this in a tri block we can declare that like this try and then let's make sure to indent this and then here let's put the accept block so we're going to try to do this and then we're going to try to print the result if we can but if an error occurs let's print that string that they wanted to an error occurred so here let's do that print error occurred this is the code let's give it a run and you can see it works as expected so this is just a brief intro to exception handling now we can actually be a bit more specific like I kind of mentioned earlier when an error occurs in a tri block it could be useful to know exactly what error occurred and we can do that like this in the accept block we can say exception as error so this is a very generic term exception could mean any type of error occurred and we're saying as error because we're storing whatever exception occurred in this variable and it's going to give us information about the error and then we can print that information just like this we're printing this string and then we're printing whatever the error message happened to be so in this case this is the information it's going to spit out it's going to say error because that's what we printed and it's going to say division by zero so it actually tells us what error occurred obviously you can tell this would be very useful to debug code to figure out what is causing the issue so knowing this let's implement the function divide numbers we're given a string a and a string B so these are strings but we want to attempt to convert them into integers and then divide the first number a by the second number B now if an error occurs we want to print an error occurred followed by the error message so we're probably going to need to do something like this so let's try that there's going to be a variety of errors that could occur this time actually we might not just get divide by zero so let's see what happens let's do try and then in here let's compute the result um but first let's convert them into integers so let's say a int I'm going to convert a into an integer I'm going to say B int and convert B into an integer then I'm going to try to divide a by B and store it in the result and then I'm going to try to print it so print the result now if an exception occurs let's try to catch that exception let's do the syntax as on the left exception as error and then let's print it so they told us to print this message an error occurred so I'm just going to copy and paste it here and I'm also going to print the error message which is stored in that variable so just like this so let's see the output that we get let's print it uh whoops I actually had the wrong variable name so I had error when we want the actual variable name error so sorry about that let's give this a go and we can see we did get the correct output so the first test case case 10 / 2 that gave us five next time we got an error division by zero it makes sense cuz we're trying to divide 12 by zero next we got an error invalid literal for INT base 10 not a number hm we tried to convert this string into an integer and it didn't work so that gave us a different exception which we handled here again this is the power of exception handling it can tell us what exactly the error was that occurred now instead of having just a single block to catch an error we can actually be even more specific so I told you that this was the generic case exception will catch any type of error that occurs but we might want to handle them differently so we can actually do that so with this we have three different exception blocks we have this one which is only going to catch value errors and we have this one which is only going to catch zero division errors and it'll print like a specific message for each case and then this one will catch every other type of error so the generic case of errors that didn't match these two you might be wondering like how do you know what type of error could occur how do you know all the different types of Errors there is a link for that down here but you don't have to memorize it just kind of know that there's all types of errors and if you need to know which ones they are you can always kind of research that but it's generally a good idea to always have at least one generic block which will catch any type of error if you don't match the specific ones you'd only want to have a block here for zero Division and for Value error if you wanted to handle those separately if you wanted to give like a very specific message in those cases so like the way this will execute is if a value error occurs it'll execute this block and it'll skip the rest of them if a value error doesn't occur it'll try this one if a zero division error occurs it'll execute this and skip the rest of them if neither of these occur it'll execute this third one for sure unless like an exception never happened in the first place in that case none of these would execute obviously so knowing all of that let's implement this challenge divide numbers it's going to take two strings as arguments just like before it's going to try to divide the first one by the second one and then print the result so just like before except we want to have specific blocks here we want to have a accept block for Value errors and accept block for zero Division and an accept block for all other types of Errors so let's do pretty much the same code as before I'm going to put that in the tri block I'm going to have a in I'm going to convert a into an integer like this I'm going to have B int and convert B into an integer like this I'm going to try to compute the result take a inide by B int and then try to print the result now there's a few types of cases we have to handle maybe an except occurs for Value error and let's just make sure we have the syntax correctly so value error we don't really need to catch the error specifically we can just print this string over here on the left error invalid value so I'll just copy and paste that here we have the other case where uh we have zero division error I'm going to copy and paste that here and in that case we want to print this string error division by Zer so let's do that and lastly we have the generic case which we can handle like this except exception and let's see what we want to print in that case we just want to print an error occurred followed by the error message so let's turn this into a variable accept as error and then print an error occurred followed by the error message itself so very similar to the previous code now we just have different blocks for different types of exceptions so let's run this now and and we get the expected output