You'll learn how to become a competent Python programmer by learning the fundamentals of the language in detail. You'll learn how to navigate complex data structures and accumulate results from them. And you'll learn how to convert data into a format that can be used by other programs. At the end of the specialization, you'll be able to write Python programs of a few hundred lines. You'll be able to use and integrate Python modules into your code. You'll be able to use external tools like APIs by reading their documentation as well. We start from the beginning and we don't assume any prior knowledge, but we do go deep into the fundamentals of Python to be sure that you understand every aspect of code. So, you want to say something about what's our runestone interactive environment? Yeah, so the runestone interactive textbook allows you to interleave learning materials with active code assessments that will allow you to actually write code. And we find that writing code is really important because even though you can learn how a concept works in theory, so you might know how some particular feature of Python works, it's really important to actually write code to gain more of a working understanding and to know how to actually apply those concepts in practice. So, there's also the way of the programmer segments. So, most of the course is about how to use Python and learning about Python features. The way of the programmer segment is more about how programmers can and should work. Programming is a little bit more of an art than a science. There's lots of correct ways to do things, but there are best practices. So, there are things like how to write programs incrementally. In the way of the programmer segments, you'll also learn about how to write good automated test cases. So, that's going to come in course four. Until then, we're going to write those test cases for you. Lauren has created a whole lot of assessments where not only can you run the code in the browser, but it'll tell you whether you got it right or not. And you get that immediate feedback and you can try it as many times as you want. In fact, we've set up the assessments so that you have to get everything right, 100% in order to pass the assessment. And the reason for that is we really want you to build mastery so that you don't go on to the later stuff until you've got the early material really solid. You'll also notice that in all of the projects that you do, you'll find ways of translating the concepts that you learn in the courses and throughout the specialization into your real life. For example, different ways of building programs that might be fun in your job, or your school, or your work, or whatever it is that you do. So, one of the things that I really like as I've watched you all put this together is in Python for Everybody, and you kind of already said this, in Python for Everybody, I really focus on the program. If you get the program, it's like you win, you know, you get the gold star. And we didn't have the time or the luxury to really understand what was going on inside the program. We're just like, we got the program done and we got to move on to the next thing. But with some of the stuff that you have in Runestone, you get to say, what's really going on inside of the program and how does this really work? And that's part of the mastery is so that if you can't, as a programmer, kind of put yourself inside the program and understand how the program is actually functioning, it is difficult to write more sophisticated programs. And so, that's where, even though this technically is a beginning course, I think it's really important for people to take more than one beginning course because you have to go over the same material over and over with, in a sense, deeper understanding each time you go through it. Yeah, we have this great code lens tool that I think you're referring to that lets you visualize what's happening in the execution of the program one line at a time and you can go forwards and back and see what actually was the value of that variable and when did my list change what its contents were? And so, it gives you a way of thinking about it. It's really great for debugging so that you don't have to just do trial and error. Let me change something in the code. You can really think through what is a program. So, another thing that the students always ask me is what next? And I think that it's kind of cool that you've built into this specialization kind of a step into what they're going to do after this, Chris. Yeah, so one of the things that we've added to this course at the very end is a project course and that's really to focus people on how to take other APIs that might be out there or packages and use them and do something novel with them outside of just learning. And it gets to this repeated practice comment that you made. And for that, we're actually doing it within the Jupyter environment. So, just like you need repeated practice with APIs and with Python fundamentals, there's so many different places that you can write Python code and Runestones one of them and the tools you use in Python for everyone are one of those. Jupyter is one that's quite common and we teach that in the data science specialization that students can follow this with. And there's other environments too and so we're trying to really showcase a diversity of learning environments and production environments for Python. Programming is not one environment, right? It's not like you have this one thing and you type this stuff in and that's all the programming. When you're out in the real world, each job often has different kinds of environments. Yeah, and practice is so important in the context of programming. I think Lauren has written some great examples of practice problems for you to work on in the course as well. And we have this great practice tool that you'll get to see where it represents to you for review some questions that you've already seen in the past. And it keeps presenting them to you more frequently if you're having trouble, less frequently if you're showing mastery of them and it's a way to really reinforce what you've got. So, look for that practice tool. It also has these fun fireworks that don't have your practice problems for the day. So, as you can tell, we're all really excited to share this material with you and we hope you have a lot of fun and wish you a lot of luck. Here at the University of Michigan our school colors are maize and blue. You might think of them as yellow and blue, but we call it maize and blue. And if I travel anywhere and I have a Michigan logo thing on, someone will come up to me in the airport and say, go blue. So, on three, two, three, go blue. Hi everyone. I'm excited to show you some useful features of the free interactive textbook that will be available to you as part of this specialization. Content in the first four courses all track pretty closely to the textbook content. So, whichever course you're starting with, you'll want to go through this video to see the important interactive features. You can skip it if you've already seen it in a previous course. The runestone interactive textbook environment by my friend Brad Miller. I've made a few contributions to both the software environment and especially the textbook over the past four years, but Brad deserves almost all the credit. Let's take a look. The first thing you'll need to do before accessing any of the textbook pages is to log in from Coursera. So I just click on this open tool and I'm automatically logged in. You've already logged into Coursera and Coursera is passing the credentials to Runestone, so you'll be automatically logged in here. Once you're logged in, all of your work will be saved and we've deliberately disabled any other ways to log in except by doing it through Coursera. So when you first log in following that link, you'll be taken to this practice page in the textbook. It's our way of encouraging you to use the practice feature every day, but we'll come back to that later. Once you're logged in, you'll be able to click on any of the links for the readings and you'll be taken directly to the pages in the textbook for those readings. So here's a link to the Runestone page for variables and I'll click on it and now I'm on a textbook page. In the textbook you'll find text and images, diagrams but you'll also find some interactive elements. For example, here's what we call an active code window. It's got some code in it and I can click save and run. It'll run and print something out over here in an output window. I can change that code and I can run it and all of your code versions when you save and run them will be saved. I have this little scrubber here and I can move it and see all of my old versions and they're not just saved while this page is open they're saved permanently. For example, let's reload this page. When the page loads we're back to the original window contents but I can click load history and then I get the scrubber and it shows me my last code run. Now if I rerun a previous version it won't it won't show in the scrubber but it will show in the latest version. But if I change it instead of 17 I do 18 now it becomes the latest version in the history. Show in code lens is a really useful feature of active code windows. This is an amazing tool developed by Philip Guo, a professor at UC San Diego. It lets you step through the execution of a program one line at a time. I can click forward and show me what happens after one line is executed in the next can print out just the first message and so on. That's not such a big deal now but it will be really useful for you when you start to do more complicated programs with conditional execution and iteration and defining your own functions. Part of our educational philosophy in this specialization is to reveal all the magic. We want to give you a way to reason about how your programs are executing because that's the foundation for being able to debug your code through understanding rather than through trial and error. Code lens really helps with that. Now sometimes these code lens examples are built right into the textbook but you can always get to code lens by hitting the show code lens or hide code lens for any active code. Here are some that are built in on the textbook page. There are also other interactive features. Here's a multiple choice question. You can answer those and get immediate feedback by clicking on check me. I've actually already answered this one but suppose I said Thursday as the thing that would print out here because day is set to Thursday. I click check me and it gives me some feedback. That's true. Thursday is the value of day but it gets overwritten later so the correct answer is 19. Now when you get to the bottom of the page I suggest that you click on mark as completed. If you haven't clicked on it, this is what it will look like initially. If you click on mark as completed a couple good things will happen. One is you get the satisfaction of it says yay completed well done but you get a couple other things too. First some of the multiple choice questions or other activities on the page get added to the practice tool which I'm going to show you in a minute. That practice tool will help you review things so that you don't forget them. Sort of like vocabulary flash cards when you're learning a foreign language. Second the pages that you've marked as completed will be marked in the table of contents. So you can keep track of the textbook of what you've read and what you haven't. Here's the table of contents and you can see these orange dots indicate things that I've completed and I've marked as complete and the check boxes the check marks indicate things that I've opened but I haven't marked as complete. So this completed button at the bottom of the page is separate from marking a reading as complete in Coursera. You may want to do both of those things. In Coursera we'll generally provide you with links to particular pages and so you can just read that one page but if you want to you can navigate through the textbook once you're on the Runestone site. We have these forward and back buttons. This goes to the next page in the book. Back to the previous page. If you click on the textbook title as I showed you a second ago you'll get to a table of contents that's very detailed with every single page and sometimes subsections within the pages. If you want a more overview look at it. You can click on this chapters and it'll show you the different chapters and you can just see the detail for one chapter at a time. Now notice that the orange dots aren't shown on this detailed view of just a single chapter. That's a little unfortunate and now that I've noticed it I'll try to add that feature at some point. Finally, there's a search option so I can search for variable and it'll tell me lots of pages in the textbook where the word variable shows up. There's also an index I want to look for various things and I can click on them and it'll take me to where they are in the textbook. Normally if you log in from Coursera you'll be taken directly to the practice feature but you can also get there from within the book working on practice. What this practice feature does is it represents to you questions on topics that you've marked as already completed. That thing at the bottom of the page where you mark the pages completed. When you're here in the practice feature you get to answer it again and if you get it right you'll remember that and it won't ask you that same topic again for a long time if you get it wrong I might ask you again tomorrow. So this practice tool is the brain child of my doctoral student, Iman Yakisare. He just implemented it last year and in the first semester where we made it available to students in our on-campus classes those students who used it in the first semester where we made it available to students in our on-campus classes those students who used it more did a lot better on the course exams than those who didn't. This is a striking result for me because I'd been monitoring for several years to see whether just spending more time in the textbook had a similar effect on student performance and it didn't. In my on-campus classes use of this practice tool is now required and earns a few points towards the final grade. For the Coursera courses it's not required but based on the results I've seen with our on-campus students I strongly encourage you our on-campus students love the fireworks that they get so here I'm going to answer a couple of questions I have only two left to practice for today and I'm going to say done ask me another question and it gives me one more it says hang in there last question for today and what's going to print out oh this is a review the one we just looked at I say check me and then I done and I get these fireworks which are a little little fun when you finish all the questions for the day. For those of you who are taking this course for a certificate you'll also see links to graded assignments usually at the end of each lesson or set of lessons. In the first four courses the assessments and projects are in the runestone textbook and they're all auto graded there. You'll only be able to see these in Coursera if you're paying to take the course for a certificate. If you're not paying you can find similar questions in the end of chapter assessment pages in the runestone textbook. So let's follow the link for this first assessment and this assessment just has two questions I've actually already answered one of them correctly before that was a multiple choice question and they want me to write some code the answer to this one is I'll save and run it and I get some immediate feedback an automatic test in here and it's telling me that I got the right output. If I said hello word instead I would get feedback saying that I had failed. I would actually when I tell it to grade me it'll use the best answer I've ever given so if I ever manage to pass the test I will pass this. We've set up the assessments so that you have to get usually that you have to get 100% in order to pass the assessment but you can keep trying and keep getting feedback until you get that 100%. We've done that because we think it's really important to master the early material because things keep building on each other. So I click grade me and it comes back you can see now that it's updated the score to 1 instead of 0. I've gotten a total of 2 out of 2 for this assessment and if I go back on Coursera and I refresh it it'll tell me instead of trying again it's going to tell me that I've passed. Passed with 100%. That's the runestone environment. It's been a labor of love for all of us who've worked on it as an open source project over the last few years especially Brad Miller who started the project. I hope you'll find it really helpful to you as you master the fundamentals of Python. I usually end my on-camera segments with a little joke so here's a bit of humorous advice. Procrastinate today always today. Don't put it off until tomorrow. Okay then. Don't listen to my advice. Don't procrastinate today. Go get started with the first lesson in this course. I'll see you next time. In this course you're going to learn to write computer programs in the Python programming language. But before we get into that we need to know exactly what programming is. Fundamentally programming is giving a list of instructions for a computer to follow. In the context of programming these instructions are sometimes called algorithms. Now computers are really good at following these instructions very reliably and very quickly but not very creatively. And we can't just give computer's instructions in English or any other natural language that we normally speak because natural language has ambiguity. Words or sentences can have multiple meanings which computers can't figure out. Instead we need to give computers instructions in a programming language which is a kind of language that computers can understand because they have a formal syntax or a set of rules. There are many programming languages that exist including JavaScript, C++, Java, Haskell and many many more. But in this course we're going to learn to use the Python programming language. The process of learning programming is more than just learning the rules of the Python programming language it's also about how to break down and solve problems regardless of the programming language. And I like to think of programming as a translation process where the programmer translates their goals from natural language into a programming language. For example, if I want to write my own version of the game Wheel of Fortune then I need to translate the rules of the game into an unambiguous set of instructions written in the Python programming language that tell the computer how to run the game. And as you become a programmer you'll learn how to come up with strategies or algorithms for solving problems and how to translate these strategies into Python code that a computer can execute. Now all that said the best way to learn programming is through practice so let's get started. In this course you'll be able to write Python codes right in your browser. You'll see what are called active code windows that look like this in your textbook. Now most programming courses start off with what's called a Hello World program or a program that prints out Hello World on the screen when you run it. In Python a program to print Hello World looks like this. In order to actually run this code we need to click the Save and Run button. When we do that we'll see an output window show up to the right here. Now to break down what all of this shows we have our Python source code on the left and we have our output on the right. Now when we click Save and Run what happens is that there's a hidden Python interpreter and it looks at what's in our source code and prints out whatever any relevant output is. And remember that this source code is a set of instructions. In this case the source code is an instruction to print out whatever is in quotation marks here in this case Hello World. If we wanted to change the set of instructions so for example I want to change it to print out Hello Michigan then I need to put it through the interpreter by clicking Save and Run again. And now when I do that you'll see that my output changed from Hello World to Hello Michigan.  Now the Python interpreter typically tries to run all of the source code that we put in this window but it can sometimes be helpful to leave natural language notes or explanations for ourselves or for other programmers who are looking at the source code. Now the Python interpreter typically tries to run all of the source code that we put into our source code window but sometimes it can be helpful to leave natural language notes or explanation for ourselves or for other programmers looking at the source code. In order to do that we have what are called Python Interpreters. In Python we write a comment by using the pound symbol and writing what we want after it. When we write a comment then Python ignores everything that comes after the pound symbol meaning that we can write whatever we want here. And on the next line Python will start running the code again so if we wanted to write a comment we would need to add a pound symbol. And what comments do are they tell the Python interpreter to ignore these portions of the source code and only to run what's not commented. In our case the only code that actually runs here is print hello world. Another thing that's worth noting is that the Python interpreter is not very forgiving so recall that when you write source code you're giving the computer a set of instructions and these instructions need to be unambiguous to follow the rules of the Python programming language. One of the rules of Python is that when we have an open parentheses then we are going to need to have a close parentheses. So let's suppose that I forget that rule and I delete this close parentheses and I try to run my program. What happens is that I get what's called a syntax error. A syntax error is Python saying that it doesn't understand the rules of what you wrote so it doesn't try to actually execute what's in the source code window. In other words a syntactic error or a syntax error is when you're not following the rules of the Python programming language. In this case we're not following the rule that this open parentheses has to be followed by a close parentheses. Throughout this course we're going to run into syntax errors and other kinds of errors including runtime errors and semantic errors. So when we get a syntax error as we will many times throughout this course we can fix it by editing the source code to obey the syntactic rules of the Python programming language. In this case I'm going to add a close parentheses to match the opening parentheses that starts out here. Now when I click save and run again then you'll see that the syntax error disappears and my program runs again. With that you're already on your way to becoming a programmer. With more practice we'll better understand how the Python interpreter works and we'll be able to write larger and more complex programs. See you next time. In this lesson we're going to go over some of the basics of writing Python programs. Part of the reason that I really like Python as a first language is that it doesn't take much to actually write a simple Python program. So when we write a Python program we are going to write our Python code in this window and the code that we write is going to go into a Python interpreter which is a program that's running behind the scenes but it takes what we write in our code window and interprets it and then depending on what we write it might send something to the output. So again this is the source code this is the output and the Python interpreter looks at our source code. We've already seen a Hello World Python program but I'm going to write a program that's even simpler. This is a valid Python program so if I click save and run then you aren't going to actually see anything in the output program but Python did actually interpret and run this program correctly. So remember that there's this hidden Python interpreter and when we actually ran this code the Python interpreter looked at our source code ran it and saw that there was no output for it. So when we write code we're actually writing what are called expressions. Learning the program is in part learning the right expression to write for what we actually want to compute. So 100 is an expression and when we write expressions what the Python interpreter does is it computes the value of those expressions. So we have expressions, every expression has a value. So in the case of the expression 100 the value of the expression 100 is just 100 and then every value also has what's called a type. A type is like a category of data so the type of 100 is an integer an integer is just a round number so I'm going to add on to this program by writing another expression. So here's another expression 3.14 Again when I run my program nothing shows up but what's happening is that behind the scenes the Python interpreter is looking at my code and deciding what to output. So here we have two expressions the first expression is 100 the second expression is 3.14 and these expressions have a value so the value of 100 is 100 the value of 3.14 is 3.14 and both of those values have types. So the type of 100 is an integer the type of 3.14 because it has a decimal place is something called a float short for floating point number. So we want to compute all of the values that we want and Python will do these computations behind the scenes again with this Python interpreter but typically we want our programs to actually give us some feedback we want it to do something and then give us back some value in the output window in other words we don't just want our source code to go to the Python interpreter and get no output we usually actually want some output or some feedback from our programs and in order to do that what we need to do is we need to use what are called print statements so a print statement is a special kind of expression that tells something to show up in our output window so I'm going to modify my source code to say rather than just computing the value to also print out the value that I do that is I say print open parentheses and then the value of whatever expression I want to print out and then close parentheses so if I only say print 100 and I run my program then I'll see that in my output window I get 100 because I specified that I want to print out the value of this expression so notice here that print 100 prints out 100 but when I say just the expression 3.14 then this doesn't lead to any output in order to actually print out the value of this expression I also would need to add a print statement here and now when I run my code then you'll see that the value of both of these expressions is printed so so far we've seen two kinds of types we've seen integers which are round numbers like 100 and floats numbers like 3.14 another type that we've actually seen in an earlier lecture was a string so when we said print hello world then this expression hello world is a string a string is a sequence of characters and if I run this program then you'll see that hello world is what actually gets printed out when I run line 2 so so far we've seen the types integers like 100 strings like hello world and floats short for floating point number like 3.14 and in all of these cases these 100 the string hello world or 3.14 are all expressions now expressions are like the building blocks of programming we can combine and reuse expressions to get more and more complicated programs so the expressions that we've written so far are what are called literal expressions where the value is the same as the expression itself so the value of the expression 100 is 100 the value of the expression 3.14 is 3.14 let's move on to operators to see how we can combine expressions to get more complicated expressions so far we've seen simple expressions like 100 hello world or 3.14 and these are all literal expressions where the value is the same as the expression itself but expressions are like building blocks that can be combined to form larger and more complicated expressions operators are one way to combine expressions the first operator that we'll learn is the addition operator in order to use it we write something like print out 100 plus 200 when I run this code note that I get the value 300 so when we execute a print statement then rather than printing out the expression itself the expression here being 100 plus 200 then we print out the value of that expression so this expression has a value of 300 now this expression uses the addition operator the addition operator which is done using the plus sign expects one expression to the left and one expression to the right and what it does is it computes the value of the expression to the left and the value of the expression to the right it adds up these two values and then the value of this overall expression is the value of this expression plus the value of this expression so in the case of 100 plus 200 we add the value 100 plus the value 200 and the value of this overall expression ends up being 300 and remember that whenever we call print we print out the value not the expression itself and so 300 gets printed in our output beyond the addition operator python includes many other kinds of operators so there's subtraction so when I print out the value of 5 minus 2 then we get 3 if we reset our code here there's also a multiplication multiplication is done using the star symbol so when we print out the value of the expression 2 times 4 then we get 8 there's also division so when I print out 10 divided by 3 so division is done using the slash symbol and when I print out the value of this expression then it's 10 divided by 3 which is 3.3 repeating and in addition to addition multiplication division and subtraction there's also some other kinds of operators so you'll notice here that when we take 10 divided by 3 we had 2 integers 10 and 3 and we got a float as a result sometimes we don't actually want that remainder so python also includes truncated division which leaves out the remainder so that we get 3 back and it also includes the modulo operator so if I say 10 and then percent sign 3 this is the modulo operator so it gives me the remainder of 10 divided by 3 which is 1 python also includes exponentiation so if I print 4 star star 2 then this expression is like saying 4 to the power of 2 or 4 squared and so the value of this expression is 4 squared which is 16 when executing code python follows the normal order of operations that you might be used to in math so at highest precedence is whatever is in parentheses and then below that is exponentiation so that would be something like 4 to the power of 2 below that there's multiplication and division so that would be something like 2 times 4 and then below that there's addition and subtraction so what this means is that suppose that we wanted to compute the average of 2 numbers 10 and 20 so if we wanted to compute the average of these 2 which should be 15 then we might intuitively write something like 10 plus 20 divided by 2 so if I run this code you'll note that I get 20.0 rather than 15 which is the actual average and the reason is that python because division is higher in precedence than addition when computing the value of this expression then python first computes the value of 20 divided by 2 and gets 10.0 and then it adds 10.0 and gets the value 20.0 if instead we wanted to first add 10 and 20 and then divide by 2 then we could add parentheses around 10 and 20 so I would say print out 10 plus 20 in parentheses divided by 2 and now what python is going to do is it's first going to add 10 plus 20 to get 30 and divide by 2 and get 10.0 and you can see that now we're correctly getting the average of 10 and 20 see you next time welcome back one type of expression that you might write in python is what's called a function call expression so you can think of functions as boxes that contain some machinery inside of them so I'm going to visually represent them here just using this gray box and you can think of a function as some inputs so here the inputs feed into the top here and then these inputs go inside of this machinery this machinery does something with the inputs and then after it done its work with the inputs it spits out an output so visually again you can think of it as working something like this so here we have a function it takes in 3 inputs or arguments and then it does some work and then it spits out an output or a return value so let's watch that again so again we have the function and it takes in 3 inputs here or arguments then it does some work and then it spits out an output or a return value now let's translate this visual metaphor into actual working python code here's an example of how we would actually write a function call expression in python suppose that we have a function named square and our square function takes in one argument which is going to be a number and then it returns or outputs whatever that argument is squared so for example if we say square and then open parenthesis and we put our argument in between the parenthesis here so this is called calling the square function with 4 as an argument so this is how we actually write that in python again we say square or whatever the name of the function is then inside of parenthesis immediately after the name of the function we pass in our arguments so the arguments go here and then the value of this overall expression so whatever the value of this expression is is going to be the return value and so in the case of square if square squares whatever input or arguments you give it the return value is going to be 16 and this is what it looks like when we translate this python code into that visual representation that I just mentioned so we have the square function again represented as a box and the square function in this case we suppose that it takes in one input or argument and if we pass in 4 it does some work and it spits out an output or return value of 16 to watch that again we pass in the square function does some work and it computes an output or return value in this case of 16 and again this is how we write that in python code we say the name of the function, square then open parenthesis to say that we want to call that function and then in between parenthesis then we pass in whatever arguments we want our functions to take now python functions can take one, two or any number of arguments but there's always exactly one output so for example here this function takes in three inputs or arguments but it spits out one return value so again functions always take any number of arguments but there's always one value of that function call so let's look at a little bit more python code and calling functions so here on the first line we're actually calling two functions technically because here print is a function that we're calling and we're passing in as an argument to print an expression which just so happens to be another function call and this function call uses the square function that I just described so again the square function that I'm describing this isn't a built in with python this is something that we define for the purpose of explaining function calls but we defined it in a way that it takes in one argument in this case square three and then it spits out that number squared so if we say print out the value of square three then this is going to print out the integer nine so let's see that in action so I'm going to comment out the rest of these lines and run my code and I can see that the value of this expression square of three is nine if we call a function but we don't have a print statement so let's suppose that I say square five the value of this expression is going to be ten but you'll notice when I save and run my code it's not going to actually affect the output and that's because we never actually printed the value of square five we just computed the value of square five excuse me it's not ten it's twenty five of course so here the value of this is twenty five but we never print out its value now let's suppose that we have another function called sub so sub takes in two arguments so I can represent that visually like this so it takes in one and two arguments and just like every other function it's going to spit out one return value now let's suppose that sub subtracts the second argument from the first argument so in other words suppose if I passed in six if I passed in six and four then my sub function is going to spit out two because six minus four is two so we would call the sub function by saying print out and then sub and then here you'll notice that we have two arguments and we separate those arguments by a comma so we print out the value of sub six and four and we see that the value of this expression sub six and four is two if we call that again so if we call sub with arguments five and nine then this is going to give us negative four because five minus nine is negative four we can also combine function calls with other operators including function calls so here if we print out the value of square three plus two then python is going to first compute the value of this overall expression by evaluating the value of square of three and then it's going to add the result of that to two so python is going to compute the value of square of three and it's going to get nine and then it's going to compute the value of this expression which has the value two because it's a literal expression and then it's going to add these numbers together to give us 11. Now, this expression on line two is much more complicated. So here we can see that we're calling the sub function. We are calling it with two arguments. So the first argument is square of three. So this is arg one. The second argument is square of one plus one. So this is arg two. Now whenever Python calls a function call expression, it needs to figure out what the values of the arguments are. So Python kind of computes the values of a function call expression from the inside out. So it's going to go from the value of one plus one to the value of square one plus one, and then the value of square three. And then after it has the values for square three and square one plus one, it's going to subtract those two values by calling the sub function. So again, we're kind of going from the inside out to the next layer, and then out to the next layer beyond that. So when Python computes this, it's going to first compute the value of square one plus one, or two. So it's going to compute the value of square two, and that's going to give us four. It's going to compute the value of square three, and that's going to give us nine. And then it's going to compute the value of sub when called on nine and four, and that's going to give us five. So if I run my code, you'll see that we get 11 from line one and five from line two. Just to run through the exact steps that Python takes when computing the code online to Python first computes what's the value of square three, it gets nine. Then it computes what's one plus one, it gets two. Then it computes what's square of two, and it gets four. Then it subtracts nine and four, and gets five. And that's what it ultimately prints out. Now one thing to note is that functions are objects in Python. So if I print out what's the value of square, then Python is going to tell me that square is a function. So if I run my code, you can see that line one prints out function square. In order to actually call the function or run that machinery, we need to have the name of the function, and then open parentheses and pass in our arguments inside of those parentheses. So in other words, we need to have open parentheses right after the name of the function in order to say that we want to call the function. We don't want to just reference the function object itself. Let's offer now until next time. If you're not sure what type a given value has, then Python has a nice function called type. And what type does is it tells us what the type of a given value is. So for example, if we print out the value of type when called with the string hello world as an argument, then Python is going to tell us that this is a string. It'll tell us that this is an int. And then here, if we don't call the type function, we get the value itself. But if we type print out the type of 3.2, then we get that this is a float. So let's run our code. And what you'll see is that when we print out the type of hello world, then Python has a way of telling us that this is a string, print out the type of 17. Python's telling us this is an integer. This is just us printing out the string hello world itself. But if we print out the type of 3.2, then Python tells us that this is a float. And this can be especially helpful in situations where you're not necessarily sure what type something has. So for example, here we have a value 17. And even though this is a number, because it's in quotation marks, the value of this expression is going to be a string. And so the type of this expression is going to print out class str. Same thing with this. So even though 3.2 is a float, because these are in quotation marks, this overall expression is a string. So if I run my code, then I'll see that both of these are strings. Now there are a few other things to note when working with Python expressions. First, when creating a string, as we mentioned, there are multiple ways of creating a string literal. One is using a single quotes mark. Another is using double quotation marks. Another is using three double quotation marks or three single quotation marks. Now the important thing to note is that even though all of these expressions create a string in a different way, if we ask what's the type of each of these, then Python is going to tell us that these are all strings. So even though we create strings in four different ways here, all of these boil down to the same type in Python, which is a string. So another thing to note when creating strings is that sometimes you want to include single or double quotation marks inside of a string. So for example, I might want to print out the string Bruce's beard. Now here you'll note that I have an apostrophe in Bruce's and when I try to run my code, then Python gives me a syntax error and that's because to Python it thinks that this is the string because it thinks that this quotation mark starts the string and that the apostrophe is supposed to end it. One way to get around this is by using double quotation marks if we know that we're going to have an apostrophe inside of our string. So here this works. Conversely, if we had a string that we knew would contain double quotation marks, again, if I run this code, then I get a syntax error because Python thinks the string starts here and ends here. But one way around it is by using single quotation marks to create the string. If I have a string that might contain single and double quotation marks, then one way around that is by using triple quotation marks. So I might say something like, oh no, she's claimed Ben's bike is broken. So now I can use both double and single quotation marks inside of the string. And again, triple quotation marks allow us to create strings that span several lines. Another thing to note when creating an integer is that in the US, we often use commas every third digit in larger numbers. So for example, we might indicate 42,500 using a comma like this. But here if Python sees a comma inside of the integer, it thinks that these are two separate values. So it thinks that we're printing out 42 and then 500 as separate values. So when we're creating large numbers in Python, we shouldn't ever use commas to separate out every three digits. We need to write the integer like this. And now we have the number 42,500 in one value. And one more thing to note along those lines is that we can call print with any number of arguments. And when we do print decides to take every argument and print out its value separated by spaces instead of commas. So here we're printing out all of these values on one line, and then all of these values on the next line. So let's do a few multiple choice. How can you determine the type of a variable? Well, one way to determine the type of the variable is by using the type function. Next question is what is the data type of this is what kind of data. So in other words, if we called type on this expression, then what would then what would we get? Well, Python would tell us that this is a string because it starts and ends with a single quotation bar. That's all for now. Until next time. Welcome back. Sometimes it's necessary or convenient to be able to convert values from one type into another in Python. And for that, Python has type conversion functions. So the type conversion functions that we'll learn right now will be int to convert to an integer float to convert to a floating point number and string str to convert to a string. And each of these type conversion functions expects an argument. And usually we want to pass in arguments. That's another type. So for example, if we're calling in to convert to an integer, then we'll usually pass in an argument. That's something else either a float or a string. So let's look at that in code. So here we print out the value 3.14. And then we print out the value of in when called with 3.14 as an input. And the value of this overall expression is going to be 3.14 a float cast or converted into an integer. So the value of that expression is going to be three. Now when Python converts a float to a string, it always just does that by discarding or kind of cutting off the decimal point and everything after it. So 3.14 becomes three. What that also means is that 3.99999 also becomes three. So that's how Python chooses to cast a float to an integer. So this will make sense sometimes if we do 3.0. But other times it might end up being a little bit strange. So casting negative 3.99999 from a float to an integer ends up being negative three. So in addition to converting floats to integers, we can also convert strings to integers, which actually comes in handy fairly often. So here we have a string 2345. And let's say that we wanted to convert that string into the integer 2345. We do that by calling the int function and using the string 2345 as input. Another note, if we take in something that's already an integer and call the int function on it, then we're going to get that same value back. So if we call int 17, then we just get the integer 17 back. Now we can call int on any string. So for example, if we call int on this string, but Python isn't necessarily going to be able to convert every string into an integer. So for example, here this string starts out with 23, but it also contains bottles. So when Python sees that we're trying to convert this string, which isn't quite an integer into an integer, then Python gives us an error. So here we're told that on line eight, there's an invalid literal for it. So it's telling us that this string can't be converted into an integer. Now the float function works just like the int function, except rather than converting to an integer, it converts it to a float. So if I call float on the string 123 period 45, then that converts it into a float 123.45. And if we print out the type of this expression float 12345, then we're going to get that its type float. So you can see that the value of this expression was the float 123.45. And the value of this expression, which computed the type of this expression is a float. Now in addition to in float, we can also convert into a string. So for example, we might print out what's the value of the string 17 of string 123.45. And then if we print out what's the type of this expression, then we're going to get that this is a string. So let's run our code to be sure that this is the case. So we can see that when we print out string 17, it prints out the string 17. It just so happens that the output actually matches up with if we print it out the integer, but this is a string. This is also a string because we converted it again using the str method. And then the type of this expression is a string. So that's telling us that when we call str on this number, then we get a string back. So one reason that we might want to use casting is when we want to concatenate or combine values. So let's suppose that we have an integer val, which is set to 55, and we want to print out the value is val. We get an error saying type error, val must be a string not an integer. So we have to add a conversion to convert val from an integer to a string by calling the str function. So we print out the value is, and then if this is an integer, we need to add the str to convert it to a string. What value is printed when the following statement executes? So again, when we call the int function to convert or cast this float into an integer, Python does that by just cutting off everything that comes after the decimal place. And so even though this number rounds up to 54, we're going to instead get 53 as our answer. So the answer here is b. That's all for now until next time. Welcome back. If you write an expression to compute a value, then you probably want to store that value somewhere. Variables are useful because one, they allow us to store the values of expressions to be used later on in our programs. And two, because they allow us to give us human readable names for values that we can then reuse later on. So for example, in math class, if you ever have done geometry, then you might be familiar with pi. And when you use pi, you usually don't write out 3.14159, etc. Instead, you just use the symbol pi. So pi is almost like a variable. It has a name and a value. And Python has an equivalent to variables in its programming language. So every variable has a name and a value. So in this program, we create three variables. The first variable is called message, and it's created on line one. So message has the value WhatsApp doc, which is a string.  And then we create a third variable whose name is pi and whose value is 3.14159. Now the first three lines are setting variables. And then we reference or use those variables in the last three lines. So if I run my code, then you'll see that I get three things printed out from lines 5, 6, and 7. So when we print out message, then we print out WhatsApp doc. When we print out n, then we print out 17. When we print out pi, then we print out the value 3.14159.  And in order to do that, we have what's called a variables values diagram or a reference diagram.  So we start at the top, line 1, and then go down. And for every line, we're going to say what variable is set to what value. A reference diagram has two columns. It has variables and values. So what we do is we go line by line from the top of the program to the bottom. And we're going to say what variable is set to what value. So in this program, on line 1, then we set the variable message to the value, the string WhatsApp doc. So in our variables values diagram, we say the variable message is set to the value WhatsApp doc. On line 2, we set the variable n to the value 17. On line 3, we set the variable pi to the value 3.14159. And now by the time we get to line 5, when we say print out message, then this expression is an expression whose value evaluates to whatever message is pointing to. So when Python says print message, it looks at this and it says this is an expression, specifically it's an expression that references the variable message. And so the Python interpreter looks in this variables values diagram and it asks what's the value of message. We see a message here and we see that messages value is WhatsApp doc. And so what that means is that when we print out message, then we print out WhatsApp doc. And same thing for when we actually look up the value of n. So when we say print out the value of n, then Python is going to first look in this variables values diagram, and it sees that the value of n is 17. So what that means is that when we print out the value of n, we print out 17. And same thing for when we print out the value of pi. So Python looks in this reference diagram sees that pi is 3.14159. And when we print out pi, then we print out 3.14159. Make this green just to be slightly clear. Okay, so you might ask what happens if we actually overwrite the value of a variable? So what happens if we actually have a variable that I'll just call x and reset it to 5 and then I'll say print x and then I'm going to later on reset x to be 10 and I'll say print x again. So in order to determine what's going to happen here, then I'm going to write out my variables values diagram. So line one sets the value of the variable x to the value 5 and then on line two, when we print out the value of x, then we're going to print out 5. On line four, we set the value of x to 10. And so what that does is it says x no longer points to 5, so I'm going to erase this line and I'm going to cross out 5. x now instead points to 10. So x now points to 10. And here when we print out x, then we actually are going to print out 10 rather than 5. And you can see this if we run our program. So again here the first line, or the first print statement prints out 5, the second print statement prints out 10. So every variable has a name and a value. Even though the value of a variable can be anything, the name has some restrictions. So every variable name has to start out with a letter and variable names can only contain letters and numbers. So for example, x is a valid variable name because it starts out with the letter and only contains letters and numbers. Same thing with my variable. I can also have capital letters. So I can say x, y, z, capital X, y, z equals 100. And these are all valid variable names. So if I print out the value of x, y, z, then it prints out 100. One thing to note is that Python is what's called case-sensitive. And what that means is that the variable name x, y, z in all capitals is different from the variable name x, y, z in lower case. So if I print out x, y, z in all capitals, I get 100. If I print out lower case x, y, z, then I get this other variable value. If I print out x, capital Y, z, then there's no variable that's called x, capital Y, z, and so I get an error saying that this variable is not defined. So variable names have to follow these two rules. Start with a letter and it can only contain letters and numbers. And what that means is that a variable name that starts out with a number, so if I say 2x equals 50, this is not a valid variable name because it starts with a number rather than starting with a letter. And if I try to save and run my program, then I'm going to get a syntax error because I have a bad variable name here. I can have a variable called x2 because that starts with a letter, but I can't have a variable name 2x. One thing to note here is that Python also treats underscores like characters. So what that means is that I can have a variable my variable and I can set it to 5.5. And if I print out the value of my variable, then I'll get 5.5. So because Python treats this underscore character as a letter, people often use underscores as a substitute for spaces in their variable names. So variables are a useful tool that we'll use to, one, remember an expression's value to use later on, and two, to give a nice name to the value of an expression so that it's more human readable. Until next time. Welcome back. So far we've talked a little bit about expressions, which are the building blocks of programs, values, which are what Python evaluates an expression to, and then types, and in Python every value has a type. So there's one more concept which is called a statement. A statement is a complete instruction that the Python program executes. So the only statement that we've seen so far has been the assignment statement, where you say something like x equals 1 plus 2 plus 3. So here, this is a complete statement. And in this case, it's an assignment statement. But this part is an expression. And so when Python is computing the value of this expression, it breaks it down into smaller parts. So it first adds 1 plus 2, takes the result, and then adds 3 to that, and then it gets 6 as the value of this expression. And then the statement says, assign the value 6 into the value for the variable x. Now let's look at just a few other examples of expressions. So here, the expression 500 is what's called a literal expression, because the value 500 is going to be the same as the expression itself. And this has type integer. Another literal expression with a float might be 3.14, which has value 3.14, and has type float. And again, we can have slightly more complex expressions as well. So we might have an expression that adds two numbers. So 200 plus 300 is an expression. When Python sees this expression, it computes its value, which is 500, and an integer. And we can have expressions, of course, with floats as well. So we can have 10.0 plus 5.0, an expression that has the value 15.0, which is a float. Now whenever we call a function, including the print function, Python first evaluates the value of the expression that we're actually printing. So here, on line 1, we're calling the print function, and Python sees that the argument to the print function is this expression. And so what Python does is it doesn't print out the expression itself. It instead first computes the value of this expression, and it computes the value of this expression by breaking it down into smaller sub-expressions. So here, Python is going to compute 1 plus 1 and get 2. And then it's going to compute the value of this sub-expression, 2 times 3, and it gets 6. And then we add 2 plus 6 to get 8. And 8 is the final value that we actually print out. Here, when we print out the value of this expression, then Python first figures out what's the value of this expression. Here it's a literal expression. So Python figures out it's a string that has H-E-L-L-O as its characters. And then when we call the lend function on that string, then we see that there are 1, 2, 3, 4, 5 characters. So the value of this overall expression is going to be 5, and that's what gets printed out. So again, Python does the work of computing the value of the expression that we're printing out, and it prints out the value of the expression and not the expression itself. So you'll see when we run our code that the first line prints out 8 because that's the value of this expression, the second line prints out 5 because that's the value of this expression. And the same thing goes when we're actually assigning variable values. So if we say y equals 3.14, then that assigns the variable y to the value the float 3.14. But when we say x equals the lend of hello, then as Python is evaluating line 2, it crosses this out and computes the value of the actual expression. So the value of that expression is 5. And so that value of 5 gets assigned to x. So in our variables values diagram, x points to 5. Now when we print out x, we're going to print out 5. When we print out y, we're going to print out 3.14. Now we can combine different kinds of expressions into larger expressions as well. So here, this print statement prints out the value of one large expression. This expression combines literal expressions with function call expressions and also arithmetic operators. Now when Python is computing the value of this more complicated expression, then again, it kind of breaks down every expression. So here, Python goes from kind of the inside out and it says, okay, what's the lend of hello? And it crosses this out and replaces it with 5. What's the lend of goodbye? We get 1, 2, 3, 4, 5, 6, 7. So it crosses this out and it gets 7. And then it knows we're printing out the value of 2 times 5 plus 7. So 2 times 5 plus 7. Then Python computes the value of this sub expression, 2 times 5, and it gets 10. And it adds 7 to that. And then it gets 17 as the value of the overall expression. So if I run this code, it's just going to print out 17. So in this code, we first assign x to be 2. So in our variables, values table, we say that x points to 2. Then we assign y to be 1. So we assign y to be 1. Now again, when Python sees a complex expression like square when called with y plus 3, it evaluates kind of from the inside out. So it first figures out what's the value of y and then it sees that y is 1. And then we add 1 plus 3 and that gives us 4. And so that means that we're taking the square of 4 and that gives us 16. And that means that the value of this overall expression is going to be 16. Here on line 4, we're printing out square of y plus square of x. So Python, again, you can think of it as competing from the inside out. So Python is going to replace x with 2. So that means that square of 2, the value of this expression is going to be 4. When we add y whose value is 1 to 4, then we get 5. So that's, you're taking the square of 1 plus 4 or the square of 5. And then when we take the square of 5, then the value of this overall expression is going to be 25. In this third example, we're taking a sub of square of y and square of x. So Python first asks, what's the value of y? For y, it gets 1. And then square of 1 is going to be 1. That's what's the value of x. That's 2. It takes square of 2 to get 4. And then so we're calling sub with 1 as the first argument and 4 as the second argument. And that's going to give us 1 minus 4 or negative 3. So you'll see that's what gets printed out for this line. And 25 gets printed out here. And 16 gets printed out for line 3. That's all for now. Until next time. Welcome back. Now for this video, I want to be a little bit more precise about how Python evaluates expressions in the order that Python evaluates different expression parts in. Now when we're doing a function call, the first thing that Python does is it evaluates the function that we're calling itself. So it evaluates the function itself. So in other words, let's suppose that we're calling this add function. The first thing that Python is going to do is it's going to look up what is the value of this expression. And it's going to hopefully see that add as a function. Now the function call expression might be something more complicated than just a variable name as long as it evaluates to a function. Then the next thing that Python is going to do is it's going to evaluate each one of the arguments in order. So it evaluates the arguments from left to right. And so in this example, the first argument is square. So Python is going to compute the value of this expression. And then it's going to compute the value of this expression. And then finally, after Python knows what function it's calling and what the value of every argument is, then Python is going to finally call the function. And then the value that that function calls returns is going to be the value of this overall expression. So let's see that in action. So here we assign x to 5 and y to 7. And the first thing that Python does is it evaluates this add function. So it sees that add as a function. And so the next thing that it does is it evaluates each one of its arguments. So first it evaluates this sub expression and it sees that square itself is a function. And so it's going to repeat that process and it looks up square and finds that it's a function. Then it repeats that process, looks up y, finds that its value is 7. And then it's going to call square on 7 and get 49. Then it's going to look up the value of this square function and it's going to see that it's a function. It's going to compute the value of each one of its arguments. So it's going to see that x is 5. And then it's going to call square on 5 to get 25. So, so far what Python did was it found what's the value of add and then it computed the value of its two arguments. And then it ended up getting 49 as the first argument and 25 as the second argument. And so now that we have the function itself and all of the arguments, then we can finally call the function add on 49 and 25. And as a result, we're going to get 74 as the value of this overall expression. Now suppose that we have this complex expression, square of x plus sub of square of y and two times x. And we're asked to figure out what order is Python actually going to evaluate these arguments in. Now, bear with me for a bit as I drag each one of these blocks in the correct order. Okay, so the first thing that Python is going to do is it's going to try to look up the square function. And so if we see, yeah, look up the variable square to get the function object. And then it needs to compute the value of each one of its arguments. So here there's only one argument. And so the first thing that Python is going to do when figuring out what's the value of x plus sub of, you know, this function, is it's first going to look up the value of x because that's leftmost. So it's going to be look up the value of variable x. So I'm going to get look up the value of variable x to get two. And then before we can add x plus this expression, then we need to compute the value of this expression. So when computing the value of this expression, then we need to in turn look up the value of the sub of the sub function. So we need to look up the variable sub to get that function object. And then we're computing of the value of its arguments. So the first argument is square of y. So in order to do this, we need to look up what's the square function. So we look up the variable square again to get the function object. And then we need to compute the value of its argument. So we need to look up the value of y. So we look up the value of y to get three. And then we back up and we need to compute the value of the second argument to sub. And in order to do that, we need to look up x again to get two. And we need to multiply two by two to get four. So now we have the value of both of these expressions. Oh, excuse me, there is one more thing here. So after we look up the value of variable y to get three, we actually call the square function. So here after we look up y to get three, we have to run the square function. Okay, so now we have the value of this sub expression or this argument and this argument. And so now we can finally call this sub function. So we run the sub function on inputs four and nine, giving us the value five.  So that's going to give us two plus five. And we get seven. And that means that we're calling square with seven as an argument. And so we run the square function again on the input seven, giving us the final value of 49. That's all for now until next time. Welcome back. So we've seen how to assign variables to different values. For example, on line one here, we have code that assigns the variable x to the value six. And what that does is you can imagine a variables values table that says x now points to six. And so what that means is that after we've assigned x to six on line one, if we print out the value of x on line two, then this should print out six. Because Python asks, what's the value of x? It looks in this variables values table. It sees that x points at six and it gets six. Now, what happens if we say something like on line three, x equals x plus one. Now, on first blush, this might look almost like a contradiction. But the way that Python evaluates this expression is it first computes the value of this expression. And then it takes the value of this expression and assigns it to the new value for x. So in other words, what Python is first going to do is it's going to ask, what's the value of x and Python is going to get six. And then it takes that and adds one to it. So the value of this expression x plus one is going to be seven. And so all Python does is it computes a value of x plus one to which we get seven. And then it puts that as the new value for x. So x is no longer six, x is now seven. So now when we print x on line four, then this prints out seven. So if we run our code, you'll see that when we print out the value of x on line two, we get six. But then after reassigning x to be x plus one on line three, then when we print out x on line four, then we print out seven. Now this kind of operation where we say x equals x plus one, or whatever x is, or whatever x's previous value was, we want to add one to it and reassign that to x. That's actually really common. And for that reason, Python includes a shortcut for incrementing and decrementing like that. So let's run through this code. So on line one, we assign x to have the value six. And then we print out the value of x, which is going to print out six. On line three, we have this special syntax where we say x plus equals three. x plus equals three is a shortcut for saying x equals x plus three. Now it allows us to just say the name of the variable x only once here. So it's a little bit shorter and less verbose, especially if we have much longer variable names. So again, this increments x by three and reassigns it. So if we have our variables values table here, on line one, we've assigned x to be six, then we print out that. On line three, we say x equals x plus three by saying x plus equals three. So x is no longer six, x instead becomes nine. Then when we print out x, this prints out nine. And then we can do the same thing with subtraction. So we can say x minus equals one as a shortcut for saying x equals x minus one. And so that's going to take x from nine and it's going to reassign it to the value eight. And now when we print out the value of x, then we're going to print out eight. So you can see x goes from six to nine to eight. So we can do reassignment as many times as we want. So we can say s equals one and then we can add two to it. Then we can add three to that and four to that, et cetera. If I run this code, you'll see the different values of s. So s starts out as one. When we add two to that, it becomes three. When we add three to that, it becomes six. When we add four to that, it becomes 10 and so on. Later on in this course, you'll find an even a much shorter way to actually do something like this. Now let's run through some multiple choice questions. What gets printed out when the following statements execute? So here we assign x to be 12. So x has the value 12. And then we say x equals x minus one. So the value of this expression, we replace x with 12 and we subtract one from 12 to get 11. And we take 11 and that's x is new value. So x is no longer 12, x is now 11. So when we print out x, we're going to print out 11 or c. Next question, what gets printed when the following statements execute? So here we first assign x to be 12. Then we say x equals its previous value minus three since x goes from 12 to nine. Then we say its previous value plus five. So x goes from nine to 14. So then we say x equals x plus one. So it goes from 14 to 15. And so x ends up with the value 15 or c. Next question, construct code that will result in the value 134 being printed. So the first thing that we want to do is we want to assign an initial value for bank balance. If I try to put this first, then Python when trying to evaluate the value of this expression we say that it didn't find a variable named myBankBalance. So I know that this line can't come first. Here I can do the assignment first so I can assign myBankBalance to 100 and then I can reassign it like I do here. So this looks valid and by the end of running these two lines then myBankBalance is going to have the value 134. And then if we print out the value of myBankBalance on line three then we're going to print out 134. Next question, which of the following statements are equivalent? So I see this statement reassigns x to its previous value plus y. This statement assigns y instead of x. So I know that this can't be equivalent to statement a. x plus equals x plus y looks like it's similar to statement a but what that's really saying because we have plus equals here is x equals x plus x plus y. So here we have that extra x plus so that's not equivalent to statement a but statement d is and then statement e just wouldn't work. So statements a and d are equivalent. That's all for now until next time. The last thing I want to talk about in this lesson is hard coding and before I get to that I'm just going to tell a kind of motivating story from my childhood. So I have three older siblings and when they were doing algebra then if you remember algebra then you often get problems of the type solve for x solve for y solve for z. And one day I overheard my sister talking on the phone with a friend about a math problem that they had and they were trying to solve for the value of x and I overheard my sister saying the value of x is three and when I heard that I thought that I had found out some hidden secret about algebra x is three. I could take every single problem that involved x and just substitute three in I had cracked the code and I didn't have to learn algebra anymore. I could just skip that grade. Of course if you have actually taken algebra then you know that x isn't three. You know that you have to solve for the value of x every single time. So what I was thinking of doing there was a form of hard coding or thinking that x is three for every single problem. So in the context of solving Python programs what hard coding is is it's writing down the answer without actually going through the work of competing the answer. So let's suppose that I want to write a program to print out the value of x and y averaged. So if I have x equal to 10 and y equal to 20 then I could hard code my answer by saying average equals 15.0 and I let's say print out that average and so yes in this case I happen to get the right answer that the average of x and y is 15 but my answer is hard coded because I said that the average was 15 and what that means is that if I change the value of x here to be 30 the average should be 25.0 but instead I still get 15 because my answer was hard coded. So what I'll do is instead of writing out average equals 15 I want to actually write out the expression that's going to reference x and y to compute the average. So I'll say average equals x plus y divided by 2 and so even though I still get the same answer of 15 then because this answer is no longer hard coded if I change the value of x to 30 then my answer is still going to be correct. Same thing if I change the value of my answer y to 40. So in your assignments we'll often say don't hard code your answer and what that means is that you have to write expressions that actually reference the variables and rather than just jumping straight to the answer you actually have to write expressions and statements that will actually compute the answer in your code. Until next time. Welcome back. So Python gets a lot more interesting once we learn how to write code that doesn't actually execute or output the same thing every single time that we run it. Now the first way that we'll learn how to add some variability into what our code executes is by getting input from the user. So we'll be able to have different outputs from our code even with the same program by asking the user for some input and then doing something with that input. And the way that we'll do that is with the naturally named input function. So input asks the user to enter something. So for example in this code on line one we ask the user to input their name by calling the input function and the input function accepts a prompt as its argument and so here we prompt the user please enter your name. Now the value of this expression of input is going to be whatever the user entered. So if the user enters Steve then the value of this expression is going to be the string Steve. The value of input is always going to be a string no matter what the user enters. So if the user for whatever reason enters 500 then the value of this expression would be the string 500. And now n gets assigned either Steve, 500, or whatever string the user entered. And so when we print out hello n then we print out hello and whatever the user entered. So let's run this code into what we get. So when I hit save and run you'll see that I have this prompt here and it says please enter your name and that's the exact prompt that we passed into input. So if I type in Steve as my name then you'll see that my code then prints out hello Steve. If I save and run my code and I enter something different so let's say that I enter Paul then my code is going to output hello Paul. I can enter absolutely anything that I wanted here so I can enter 9999 and my code is going to print out hello 9999. So again input always gives back a string but let's suppose that we wanted a number instead of a string. Well the way that we would do that is by casting or calling one of the functions to convert strings to a number. So for example let's suppose that we wanted to write some code that was going to convert a number of seconds into a number of hours and a number of minutes and we want to also compute the remainder when we actually divide together the number of hours and minutes. So here we prompt the user please enter the number of seconds that you want to convert and then we put that into this variable str seconds. The next thing that we do is we cast that variable str seconds because again input always gives us back a string we cast that to be an integer and we assign that to be total seconds. We then compute the number of hours by taking total seconds and then doing division without remainder by 3600 to convert seconds into hours and then we find out how many seconds are still remaining by taking the remainder of total seconds when it's divided by 3600. Then we take those seconds that didn't divide evenly into the number of hours and convert them into minutes by again taking the remainder I'm sorry by again dividing without remainder by 60 and then we figure out how many seconds are still remaining at the end of this by taking the remainder when we divide seconds still remaining by 60 and then the end we get the number of hours, minutes and seconds for whatever number of seconds the user input. So if I save and run my code and then let's suppose that I just want to convert 60 seconds then I'll see that this is zero hours, one minute and zero seconds if I convert 6000 seconds then I'll see that this is one hour and 40 minutes if I convert 999 seconds then I'll see that 16 minutes and 39 seconds, etc. So again the point of this is we can use whatever the user entered as an integer by converting it to an integer by calling the int function or if we wanted to make it a floating point we might call the floating point function. Now you might ask what if the user enters something other than an integer so if we're asked to enter the number of seconds that I want to convert and I type in let's say a name and I run then you'll see that Python wasn't able to convert Steve into an integer and so we get a runtime error. Now let's look at a multiple choice question. So this asks what gets printed when the following statements execute. So let's suppose that we ask the user please enter your age and then the user enters 18. So here n gets assigned to the string 18 because remember input always gives us back a string. It doesn't matter what the user actually entered even if they enter 18 then n gets assigned to the string 18 and when we print out the type of n then we're going to get that n is a string. That's all for now until next time. There's lots of minutia to master when you just get started programming and it can be kind of tedious and not so rewarding. In today's lesson we're going to give you a chance to be creative do something a little fun. You're going to write programs that will instruct an on-screen turtle to draw a picture. Now I have with me some bobblehead turtles handcrafted in Mexico to give us a little inspiration. In our live classes sometimes I use them as prizes for especially good pictures. So you can be writing programs that will instruct your on-screen turtle to draw a picture but there's also a hidden agenda in today's lesson which is to introduce some Python capabilities particular object notation, methods that act on objects and how to repeat a set of commands many times. At the end of this lesson you should be able to write a multi-line program or program with many lines of code. You should be able to distinguish between instances, attributes and methods. You should be able to invoke methods and set attributes using the dot notation and you should be able to use a loop, a for loop to execute commands repeatedly. And stick around at the end for a turtle joke. See at the end. Let's learn how to draw some pictures. Our metaphor here is that there's a virtual turtle. It starts out in the middle of the screen. In the turtle's tail there's a pen. So when the turtle moves around it leaves a drawing on the screen. In this code we've created a turtle named Alex and we're telling it to move forward and turn left and go forward again. Let's see what its picture looks like. So in this case our virtual turtle is really represented by just an arrow. It doesn't even look like a turtle. But he started over here, moved forward by 150 pixels. That's what happened on this line of code. Told him to start here and go forward by 150 pixels. He then sort of turned left by 90 degrees. That's what the line 5 told him to do. And then he moved forward by 75. Let's go through in a little more detail what each of these lines of code does. The first line just says to make our turtle commands be available. The second line says make a screen. So that's what line 2 does. And then any turtle things that we have in the rest of the program will happen on that screen. Line 3 makes a turtle. That's what creates our little turtle guy. And it assigns that turtle object to the variable called Alex. On line 4 we are invoking a method, the forward method. So this is our hidden agenda. And this lesson is to teach you about objects in this dot notation. So Alex is the name of the turtle. If you have an object and then a dot, it will look up attributes or methods of that object. So Alex dot forward says find the forward method, which is just like the functions that you've seen before. It takes an action. In this case it says take the forward action on Alex. And we pass in the value 150. That says how much Alex should move forward. So we have object dot method. And in parentheses we pass a value. So on line 5 we're telling Alex to turn left by 90 degrees. Line 6 says move forward by 75 degrees. And because Alex has a pen in his tail, we get to see the lines that show where he went. So a little vocabulary we've gotten here. We've gotten the idea of an instance. Alex is an instance of the turtle class. WN is an instance of the screen class. So turtle is a class. Alex is an instance of that class. Screen is a class. WN is an instance of that class. And we've gotten the idea of a method like forward or left. Now in addition to these things, we can also set and read attributes. In this turtle drawing we don't really need to do it. But I want to show you how setting attributes works for object instances. So Alex is an instance. And if I want to set any attribute, let's say Alex is salary, because he should really be paid a lot for doing all of this drawing work for us. And we'll set his salary, let's say, to $50,000. If I do that, Alex will store in its salary attribute the value $50,000. And I can then refer to it just like we would other variables. So Alex refers to the instance and we can print out his salary. As before, when we print things, they're going to show up in our output window. So this is going to redraw that whole thing. And at the end of it, it's going to print out the $50,000. That's because we printed it here. So we're not going to do much with these attributes of instances for a while, until much later in this course sequence. But we wanted to give you a hint of it before we went too far with Python. I want to make a little more interesting picture. Let's see if we can finish making a rectangle here. So instead of doing this stuff with Alex's salary, let me get rid of that. And let me have Alex do a little bit more. I'm going to have him turn to the left by 90 degrees again. And what that should do is turn him that way so that when he goes forward, he'll be going in that direction. Now, how far should I make him go if I want it to be the same distance as this one was? Well, that one came from here. So I have to have him go forward by 150. Let's see what happens when we run that. It's always a good idea to run things when you write some code. Great, that seems to have worked. Let's have him turn and finish the rectangle. This time I have to go just 75 the shorter length. Sure enough, we've got a rectangle there. Welcome back. We can have more than one instance of a class. And each one will have its own attributes, its own internal state. In this code, we'll create two turtles, Alex and Tess.  Alex will draw a square with a pink pen. So let's go through the code. First we'll run it. That's test drawing there. And now Alex is drawing. So let's go through the code. As before, we need to import the turtle module and create the screen. That creates the place for us to do the drawings. In this case, we get that instance down to the variable WN. And we're calling the BGColor method on it to make it be light green, as you're seeing here. We create a turtle Tess and we set its pen size to be five. That's what makes Tess have this wider pen instead of just the narrow pen that's the default. We create a second turtle called Alex and we set Alex's color to be hot pink. After that, we have a whole segment where we have the movement commands for Tess. That's where she does her drawing. And then below that, we have some movement commands for Alex. And finally, there's this little WN.exit on click. That just says that when somebody clicks on the window, it disappears. Let me make a couple comments about the coding style here. One thing to notice is that we've chunked the code using some white space. And that just makes it easier to read. That segment that I just marked, lines 12 to 17, is what causes Tess to make her triangle. And then there's a break with a blank line on line 18 and then lines 19 and 20 have her make her extra line that goes off to the left at the end of the drawing. And there's another blank space and we get from lines 22 to 29 Alex's movements. We don't have to do this chunking in order to make the picture come out right. We do this chunking because it makes it easier for people to read the programs. You can sort of look at it and say, oh, there's a chunk of code from 22 to 29 and that's drawing a square. There's a chunk of code from 12 to 17 and that's drawing a triangle. One final thing to notice and I'm going to rerun this so that you'll get to see it again. You might notice that on line 17 Tess turns to the left by 120 degrees then on 19 she turns to the right by 180 degrees. We're following a little convention that lines 12 to 17 are drawing a triangle and putting Tess back into the same orientation that she started in. It just makes it easier for a programmer to think about it if the net impact of all of the movements is to leave Tess back in her original position in the same orientation. Similarly on line 29 you'll see that after we've completed the square we make Alex turn to the left for 90 degrees even though we're not going to make him move again. That's again just trying to get him back into the same orientation he was in before. This would be especially useful if we were going to take the whole block of code and reuse it and that's something that we'll see later in the course where we define functions. We could make a function to draw a square and wherever he starts the turtle would create that square and leave the turtle in the same spot that he was in before. So let's go on and do one of the Parsons problems. At the bottom of this page you'll find more than one Parsons problem. I'm just going to go through one of them. Here we're going to create a program that has one turtle Jamal draw that capital L in blue and then Tina is going to draw that orange line. So this is a mixed up code problem. We have all of the code that we need to have Jamal draw his blue L and Tina draw her orange but your job is to drag the parts over and drop them off in the right order. The first thing to remember about this is that the very first thing in any turtle program is you have to import the turtle module so that we can have all of those turtle commands available and you have to create an instance of the screen so that there's a place for the turtles to draw. In the instructions it said that all the stuff with Jamal should happen first so those things are going to come before the Tina stuff. But we do have a little decision to make. Should we make the left and the forward commands first or should we have this larger block first? And the clue here is that you can't have Jamal turn left before you create Jamal. So we need to have this block first because it has the line of code Jamal equals turtle dot turtle that creates the instance Jamal. Tina stuff happens after Jamal. Oops, got to get that a little lower. And finally, we have our exit on click so that when the user clicks on the drawing it disappears. Let's check it and make sure I got it right. Perfect. All right, see you next time. Suppose we have a line of code or a bunch of lines of code that we'd like to make run more than once. You could just write those lines of code and copy and paste them so that you add them again but that would start to give you long programs that were hard to understand. So Python gives us a special command that says do some lines of code multiple times. It's called a for loop or iteration. We begin with a special word for then we have some variable name or in this case since we're not really using it we just have underscore and we have a special word in and in this case we say do it three times. And then we have a colon all of the lines of code that are indented farther in this case by a few spaces. All of those lines of code are a block and that block of code will be executed three times in this case. It's always a good idea to work on your understanding of code before you run it to try to predict what it'll do. In this case you might imagine that it's going to print in our output window this will execute first and then it'll say this will execute three times this line will execute three times this line will execute three times and then this line will also execute three times this line will also execute three times. But in fact it doesn't do that it doesn't print this line three times and then this one three times. What it does is it executes line four and then line five and then four and then five and then four and then five. So the whole block is executed and then the whole block is executed again and the whole block is executed a third time. Let's see how that works. So you can see in our output window the whole block executes once and the whole block executes a second time and the whole block executes a third time. Let's go look at the next example. In this next example we're going to be able to draw a more interesting turtle program by having a set of commands get executed a bunch of times. In this case we're creating a turtle named Elon and we're going to make Elon do some commands 10 times for I underscore in range of 10 do this block from lines 8 through 10 do that 10 times. So Elon's going to go forward by 50 and then turn right 90 remember from the previous chapter what distance equals distance plus 10 does we had the variable distance and it was initially bound to the value 50. We look up 50 plus 10 that equals 60 and we replace the 50 with 60. The next time that we execute line 10 we're going to replace it with 70. And you can see that that distance variable is used here to determine how far Elon will move. So each time Elon moves forward he's going to go forward a little bit farther 50 pixels the first time then 60 then 70. So before I run this you might want to pause the recording and see if you can envision in your mind what is this going to look like when I run it. Here we go let's see. So it's getting a little bit longer each time and that means instead of just making a square it ends up making a spiral. So how many times does it do this block? It does it 10 times because it says range of 10 there. Now what if I made the angle increase every time too? Suppose instead of turning right by 90 degrees I turned right by whatever the current value of the angle was and it started at 90. If I do this I get just exactly the same picture that I have right now but suppose that I change the angle every time. Maybe I'll make it a little bit smaller. So again you might want to pause the video and see if you can predict what this is going to look like. Here we go the angle is getting a little smaller each time and so we end up with this even more interesting spiral. Now if I wanted the spiral to go even farther I could increase this range. This means it's going to go forward and turn more times. Means a little off the screen there and now he's back maybe he's gone forever now. So that's making the turtle do a set of commands a bunch of times so that we can have a relatively short program but have it do a lot of drawing on the screen. Let's see a few more interesting things we can do with turtles. We've got another little spiral in this case we're not seeing the whole line. Tess is moving around and only leaving her imprint occasionally. So how did that happen? First as before we've seen things where Tess is getting a color in this case blue. We also changed her shape so instead of being that little abstract triangle representing a turtle now Tess really looks like a turtle. On line 9 we introduced a new method called up. What that does is lift the pen up off the paper so that when Tess moves she doesn't leave a line behind her. As before we have a for loop. So 30 times we're going to execute that whole block of code. On line 11 we have a new method called stamp which basically stamps Tess's shape at the current location where Tess is. The rest of the things are things that you've seen before. He's moving forward, the distance is increasing a little bit. Each time that's what makes it a spiral. In this page in the textbook there are some more mixed up code problems, Parsons problems, and I really encourage you to take a look at those because they will help you reinforce what order these things have to happen in and the fact that you have to indent the block of code under the line that says for. See you next time. Welcome back. In all our turtle examples I kind of glossed over the import statement. In this specialization we're trying to minimize the amount of magic. We want you to be able to read code and reason about it. So this video provides some details about that import statement. There are lots of functions and methods available to you in Python that other people have already written. And we can just invoke them, we don't have to write them ourselves. Some of those functions are built into the core of Python, like the LEN function. You can just invoke it and it's always there. Lots of other functions are not built into the core of Python but are readily available. They're part of a standard library of modules. A module is a code file that defines some functions that can be used by other programs. Before you can invoke a function that's from a standard library module, you need to tell the Python interpreter to make it available. And that's what the import statement does. Turtle is one of those standard library modules. But we'll come back to that in a minute. Let's look at a simpler module first. There's a module called random and you just have to import it in order to make all of its functions available. One of its functions is called randrange. You can see this on line 6. We've imported the random module and then on line 6 we ask for randrange which says give me an integer between 1 up to but not including 7. So 1, 2, 3, 4, 5, or 6. There's another function called random. I'll talk in a minute about this weird thing of random.random. We have this random function that returns a floating point number between 0 and 1. In this case we got 0.72 and then we get a die roll of 1, 2, 3, 4, 5, or 6 from the randrange function. If I do it again, I may get different values. I get 0.039 and 3. Run it a third time I'll get yet again different values. There are two possible syntaxes for importing and referring to stuff from modules. The first makes the stuff available but doesn't give it any special alias. That's what this version that I'm showing in the current code does. On line 1 we make everything from the random module available but every time we want to refer to it we have to say random.random. It doesn't give us any alias. So I say random. And then the name of the function that's defined within the random module like randrange or random. Notice that this is another use of the dot notation. Before we've seen something like test.forward where test was a turtle object and dot forward and we would say how many pixels to go forward. Forward is a method that can be applied to test the turtle object. Here we're saying random.randrange. It's a little bit analogous. We're getting something from the random module and that something is the randrange function but it's not exactly the same. Random is a module, not the kind of python object we've been seeing so far. The way to tell the difference between these two different uses of the dot notation is just by the context. If we said import random as we did on line 1 then random.something will find the something object inside that module. Anyway, that explains the funny random.random on line 3. Random is the name of the module. That gets us the first random. And inside that module there's a definition of a function also called random. And that gives us the second random. Similarly, random.randrange. Random is the name of the module. randrange is a function inside that module. One thing we can't say is randrange.random. That is going to fail. Don't do that. The second approach to importing stuff from other modules puts the functions directly into our local namespace so we don't have to keep referring to the module they came from. Let me show you how that works. Instead of saying import random we're going to say from random import randrange. And we can also import the random function while we're at it. Now when we refer to them we no longer have to say what module they came from. Those names become available in our local namespace. And this will work just like the other one did. We got a new value there when we ran it again. Either of these two different ways works. Either the commented out version that I have on line 1, import random or the other version on line 2 from random import randrange and random. But you have to refer to the things a little differently depending on which one you did. So if we say from random import randrange then we'll just refer to random. If we did the other one we'll have to say random dot random on line 4. With that understanding of module importing let's take a look back at how we imported the turtle module. Let's look at lines 1 through 3 here. On line 1 we say import turtle make everything from the turtle module available to us. And then on lines 2 and 3 we're doing that turtle dot thing. On line 2 we do turtle dot screen. Now we haven't explicitly talked about classes yet and you won't talk about that until much later in the specialization. But this is creating a new instance of a class. The class capital screen is defined in the turtle module. Similarly the class turtle is defined. It's like we're invoking a function but it's a function that creates a new turtle object. On line 2 it's creating a new screen object. So this is just an example of the same syntax we were seeing with import random previously. We're importing the turtle module and then we're referring to something screen that is defined inside of the turtle module. By the way, we only have a few of the standard library modules that would normally be available in Python. Only a few of those are available in this runestone textbook environment. We have the random module, the turtle module, and the math module. But there are a lot more that are part of the standard distribution if you just install Python on your own machine. They can do lots of stuff for you so you don't have to write very much code yourself. You just have to find the right module. And then even beyond the standard library there's lots of other modules that you can install if you've installed Python on your local machine. By the way, if you haven't seen XKCD before there's lots of great geeky humor there and I highly recommend it. Here's one of my favorites. A friend is talking to his other friend who's up in the sky. He says, you're flying, how? His friend says Python, which I hope you're also experiencing. You're using Python and you're flying high. He says, I learned it last night. Everything is so simple. Hello World is just print Hello World. Of course, in the newer version of Python we have these days we would have to have parentheses. But it's still very simple. And his friend says, I don't know about Python. I've heard it's got these weird things like dynamic typing and white space matters. And his friend who's up in the sky says, come join us. Programming is fun again. It's a whole new world up here. But how are you flying? Well, I just typed import anti-gravity. That's it. Well, I also sampled everything in the medicine cabinet for comparison. But I'm pretty sure it's the Python. So Python has lots of great modules. Just import them. I don't know about the anti-gravity module though. We'll see you next time. As a reward for sticking around this far, here's a little joke. A snail was out on the beach taking a walk and he looked back. There were turtles behind him wearing leather jackets. Made him a little worried. So he picked up his pace. And he's inching along. About four hours later, he turns around again. And sure enough, they're still following him and they're getting closer. He's getting very worried. He moves forward, keeps going. Two hours later, he sneaks another look back. They're still following him and they're really close. So he starts sprinting as fast as he can. But an hour later, the turtles caught him. They mugged him. Took his money, his car keys. Took him all the way till the next morning before he managed to get to the parking lot to a pay phone. He calls the police. He says, I got mugged by three turtles wearing leather jackets. Can you get down here as fast as you can because they're going to get away? The officer asks, sir, can you describe the turtles in a little more detail? No, I can't. It all happened too fast. Happy drawing. I'll see you for the next lesson. Most weeks, our last lesson is going to be titled The Way of the Programmer. These segments are not about how Python works but about how you should work. Style tips for your codes, strategies for solving programming problems, habits, mindsets that will make it so that you can have fun even though it's hard work and sometimes frustrating. This week, we'll focus on debugging. Your programs are not going to always work the first time. In fact, they rarely will. You have to debug them.  So our learning objectives for this lesson, the end of it, you should be able to distinguish between three types of errors, syntax errors, runtime errors, and semantic errors. You should develop a habit that when you get an error, you'll look at the error messages and you should start to build a skill of interpreting those error messages. You will internalize a mantra of get something working and keep it working. Don't write big swaths of code before you test it. And you'll be able to recognize some common errors and how to fix them. The first kind of error is called a syntax error. Just like in English where we have syntax is an important part of grammar. A syntax error in Python means that the statements just aren't well formed. The interpreter can't parse it. In this case, we have the word print. We have the open parentheses and then something to print. But we're missing the closing parentheses. And the Python interpreter tries to keep looking farther on. Well, there's no closed parentheses here, but maybe it'll be here or down here. And it keeps going to look for it. It can't find it. And it just says, this is an error. I don't know what the programmer had in mind here. It doesn't try to guess. It says this is a syntax error. Let's see what that looks like. We actually get an error message and it tells us syntax error. And then it tells us something about what's going on. It says EOF. That's an acronym. EOF end of file. Of course, we don't really have a file. We just have a little window, but it thinks of the contents of that window as being a file that has commands in it. It got to the end of that file. It was in the middle of a multi-line statement. The statement started on line one and it was waiting for that statement to end with that closed parentheses and it never got to the end. It got to line three, the end of the file without ever seeing a closed parenthesis. Now, in the textbook, when you're executing Python, we've actually tried to translate some of the error messages that you would get in a normal Python interpreter and make them easier to understand for someone who's just getting started programming. So believe it or not, this is actually already a little bit more human-friendly than you might get normally. And we've tried to give you a little description and some suggestions of what you should do to fix it. The other things that are suggesting aren't quite what we need. What we really needed was a closed parenthesis. The other thing to notice here is that it's telling us line three. There isn't even a line three, but certainly the place where we ought to add the parenthesis is on line one, not on line three, is just that the problem started on line one and the interpreter wasn't really sure it was a problem until it got to the end of the file, without getting the closed parenthesis. So if I put this here, now it'll run just fine. An alternative, I could have put it here, and that also would be fine. Normally, there would be no reason to do that. It would be much easier to read if you put it there. Now here I'm going to get another error. It says a bad input on line three because I have this extra closed parenthesis. These are all syntax errors that make it impossible for the interpreter to figure out what the program was supposed to do. The second type of error are runtime errors. These occur when the interpreter is able to parse the program, but during the running of the program, some illegal operation happens. For example, let me do one of those. Suppose I print three divided by zero. What is three divided by zero? Infinity may be. It's just not a legal operation. Doesn't make sense to divide by zero, and if I try to do that, I get this zero division error where it says the integer division or modulo by zero on line one. So that's an example of a runtime error. It can figure out what you're trying to do, but it's not a legal operation. The third type of error is called a semantic error. That's when the Python interpreter is able to parse the code. It's actually able to run it, but it doesn't produce the thing that the programmer intended. So, for example, suppose I try to print something that explains a conversion of a fraction to a percentage, but I do it a little bit wrong. So I say one-half as a percentage is, and then I say one over two. I'd like it to say 50%, but it's really going to say 0.5. What I should have done here is multiplied by 100 to turn it into a percentage. So one-half as a percentage is 50%. I apologize for the line break there, but you can figure out what it means, 50%. So when I left off the 100, and I just said one over two, that was a semantic error. It was a programmer's error that produced the wrong thing, but the program was able to run all the way to completion. It just didn't do what I wanted it to do. Those are our three types of errors, syntax, runtime, and semantic. In this multiple-choice question, you'll get a version of on each of the pages for the three different error types. We've got one example of each. So attempting to divide by zero, that is a runtime error. Forgetting a semicolon at the end of a statement where one is required, that would make it impossible to parse what you've written, and that would be a syntax error. If you forget to divide by 100 or multiply by 100 when you're printing a percentage amount, that would be a semantic error. It's really valuable when you get an error to look at the error message. It can tell you a lot. The first thing you notice is that there's that pink box and you got an error. You may be tempted to just go look at your code and try to figure out what's wrong, but it's really worth investing the time to look at the error message. It'll tell you a line number, which will be a good clue, to learn to decode the specific messages. They will also be really helpful. So let's go through an example here. Here's a program that's going to ask for a couple of inputs. It's going to ask what time we want to start, how long we want to wait, and at the end it's supposed to print. So if I enter three o'clock, or three is our start time, then I want to wait four hours. It should tell us that that's seven o'clock. Let's see what actually happens. What's the current time in hours? Let's say three. How many hours do you want to wait? Four. And it should print out seven, but instead we get that pink error box. Let's go see what it's telling us. It says, first of all, that line five is a good place to look. It says the type of problem that we have is a name error, and in particular, wait time int is not defined. So let's go look at line five. This means that it actually executed one through four. Everything was going fine until it got to line five. Whenever you see an assignment statement like we have on line five, what happens is we first evaluate the right hand side, the stuff after the equal sign, and then we'll assign it to the variable wait time int. So it's going to get a value, but it only gets that value after we manage to evaluate the right side. So the right side says to look up the current value of wait time int, but it is not defined. So by the time we get to line five, wait time int has not previously been set. So when you try to look it up on line five, we get this error. So that should give us a clue. Now we have to go back to what we were trying to do and figure out what's going on and say, oh, wait a minute. We were trying to take this input that we got that was going to be a string that was maybe four hours, but this isn't the name of the variable. Wait time str is the variable. So if I do that, I have a better chance of it working. Let's see if that does. I start at three. I say that I want to wait four hours, and now I get the seven that I was expecting to get. See you next time. You can reduce your headaches as a programmer. By writing your programs incrementally, get something working and keep it working. That's going to be our mantra. Or start small and keep it working. Here's a program where we're trying to draw a house, a rectangle with a triangle on the top. And we've done the first part for you. Get something working. But I'm now going to try to add to it in a way that says draw more and more of the house and when we make mistakes to catch them before there's too much code. So we've started with something that's working. Bob is going down and turning left. Let's complete that square. That should be pretty easy to do. Just make him turn left again and go forward. You might be tempted to do more lines of code than that, but I really recommend testing after you write every little bit of code. Let's turn him again and we'll test that. Then we're going to get to the slightly harder part. Now I want to make him turn this way so that he can finish the house like that. Let me do that. Instead of turning left, I'm going to want to have him turn to the right. Instead of 90 degrees, let's just try going 45 degrees. We'll go halfway and then we'll have him go forward by our 50 again. Oops! This is why it's a good idea to do things a little bit at a time because I'm going to get to realize already that I am going the wrong direction. I wanted him to turn to here so that he would go that way, but he didn't go far enough. He turned 45 degrees, but I really needed him to go all the way around to there, so another 90. So I need him to go 135 degrees. That looks a lot better. Now I'll just have him turn 90 degrees, go forward, and we will have our house. Let's see. Oh, we don't get a house that way. So it turns out that we need to make him not go quite as far. So instead of going 50, he's got to go a little bit less. Maybe he should go 40 each time. Almost, but not quite. You can try just doing trial and error until you get something that's pretty close, or sometimes it really helps to step back and do a calculation. In this case, the Pythagorean theorem, remember that, can really help us. We want to make something that is 50 here and is going to be a right triangle there. If we have some length a, we need a squared plus a squared to equal 50 squared. So you could do this in a calculator, or you could have the Python interpreter work it out for you. There is a square root function. It's a little tricky. You have to import the math module, and then we're going to say distance is the square root of 50 times 50 divided by 2. And then we will have Bob go forward by that distance each time. Let's try it and see if we get a nice roof on the house this time. Yay. So the main point that I wanted you to take away from this was that you're unlikely to get it all right the first time if you just write a really long program. It's much better to start with something that's working, add a line or two, test it, and make sure that you continue to have a program that is working. So build up and always keep your program working. Let's go through some common errors. These are typical mistakes that programmers make. So it would be helpful just to have seen some of the error messages and how to recognize the problems and how to fix them. So one that we've already seen is forgetting a parenthesis. Here it tells us there's a syntax error. It doesn't really know exactly what's wrong. It tells us there's a syntax error on line 6. Whenever you see a syntax error, you should always look on the line number that it says if everything looks right there, you should really look on the lines above it. In this case, we forgot to close the parenthesis on line 5. Even though it says line 6, it's line 6 where the Python interpreter realized there was a problem, but it's on line 5 where you really have the problem and where you should correct it. We should add that parenthesis and now it works. Our next set of errors involve variable names. The first thing to realize is that capitalization really matters. In this case, we got to line 3. And on line 3, it tries to do capital turtle dot small turtle. First thing to know is that capital turtle and small turtle are two completely different variable names. And small turtle is the name of this module. In that module, you can find the turtle class. So we really needed to have small turtle dot capital turtle. So another example of a variable naming problem occurs here. And sometimes you got to watch out when we have a big window to draw in that there may be an error message down below. In this case, the name right is not defined on line 6. You might look at this and say, this is all just fine. We're telling June to go right by 90 degrees. That's not the correct way to say it. We first have to mention the object and then the dot and then the method. So June should go right by 90, not right dot June. So let's fix that. And now we get it without the error message. Here's another variable naming problem. We create a turtle named June and then we tell June to move right by 90. Again, the error message is down off the screen that it's saying June is not defined on line 6. So we're telling June to go right. There's no binding for the variable June. There is a binding for the variable small j June. This is a situation where small j June, capital j June, not the same variable. And so we got that error. Fix that. No more error there. Another common type of error is when we invoke a function but don't pass in the right values, the right arguments that it's expecting. So here we've got a program where June is supposed to move forward and turn right and do that repeatedly. But we didn't tell it how many times to do it. So we invoke the range function but we didn't pass in an argument saying how many times to do it. We get an error and it tells us that on line 5 we tried to invoke the range function. It wanted to have at least one argument but we didn't give it any. There are no values in the parentheses and it wants at least one. So we should have said something like do this three times. Let's give it an argument of three and now it works just fine. So those are some kinds of common errors that you're likely to see as you start writing your own programs. We'll see you next time. Congratulations.  Remember your programs aren't going to work the first time and you shouldn't expect it but with the debugging skills you've started to pick up that won't be a problem for you. At this point you should be able to distinguish between syntax, runtime, and semantic errors. You should be well on your way to developing a habit of looking at the error messages and developing a skill of interpreting them. Learn to love your error messages. You should be internalizing a mantra. Get something working and keep it working. Celebrate the small wins and in order to celebrate the small wins you first have to have those small wins. And finally you should now be recognizing some common errors and how to fix them. So today's joke. A manager, a mechanical engineer, and a programmer they're driving back from a convention going over some mountain roads and suddenly the brakes fail. They fly, they're careening down the mountain. They're scraping against the guard rails and fortunately they finally stop in a ditch to their great relief. All three of them they get out and they start to assess the damage. The manager says let's collaborate, let's share our ideas, work together on this and we can solve this issue. The mechanical engineer says well let's disassemble the whole car and we'll analyze each part for failure and figure out what went wrong. The programmer says hey why don't we just push the car back up the hill and go back down and see if it does it again. Now don't be that kind of programmer. Be more like the mechanical engineer. Read the error messages. Figure out what's wrong. Your life as a programmer will be a lot more pleasant. We'll see you next time. Welcome back. Sometimes you'll want to change an object after you've created it. You create a list. You want to keep adding items on to the end of the list. Create a turtle. Change its pen color. There are two ways to change the value of an object after you've created it. One, you can make a modified copy of the object and the other is to modify the original. We'll call that mutation and it's useful but sometimes confusing. So today's lesson, how a mutation of existing objects works, recognizing the potential confusion that that can cause. At the end of today's lesson you should be able to identify whether an object is mutable or immutable. You should be able to identify whether a method mutates an object or creates a modified copy of it. And you should be able to recognize when two different variables are aliases for the same object and predict whether an operation on one of those variables is going to cause an impact on the contents of the other one. Welcome back. Recall that types are like categories of data and that different types are good for storing different kinds of things. So for example, we've seen integers which are good for storing round numbers and we've seen floats which are good for storing floating points or decimals. In this lesson we're going to learn three new kinds of types, strings, lists, and tuples. All three of these types are sequences. A sequence is an ordered collection meaning that it's a collection of items and it has an order meaning that it has a first item, a second item, a third item, and so on. And it also has a length. So let's start by talking about strings. The simplest way to create a string is with a string literal expression. So for example here, we're creating a string and storing its value in the variable s. I create a string by starting with either double quotes or as I'll talk about in a little bit, single quotes and then inside of those quotation marks then I put the content of the string. The content of the string is a sequence of characters or letters. So in the case of this string, the first character is capital H, the second character is lowercase e, and so on. And in this string, we have exactly 1, 2, 3, 4, 5, 6. Yes, the space counts. 7, 8, 9, 10, 11 characters. So I would say that the string s is 11 characters long. We can also create strings using single quotes. So if I replace these double quotes with single quotes, then s is still a valid string. If we wanted to create a multi-line string, we can use triple quotes. So now if I print out the value of s and I get hello world, if I print out the value of m, then I get this multi-line string. For multi-line string, we can make these triple quotes either double quotes or single quotes. Either is valid. So we can use either double quotes or single quotes as long as we're consistent about which kind of quotation we're using. So for example, I can't start out this string s with double quotes and end it with a single quote. If I try to do that, then I get a syntax error on line one. Instead, I need to either use double quotes for both the start and end, so I could do this. Or I need to use single quotes for both the start and end. And the same is true for multi-line strings. We can either use three double quotes or three single quotes. So I'm going to modify this string s a little bit. So first, here's another valid string, s equals capital M. And so if I print it out, then I get m. So what this is doing is it's creating a string that's one character long and that first character is capital M. Here's another slightly more confusing string. So here, s is a string that has zero characters in it. So s has a length of zero. And that's not to be confused with a string that has one character, but that character is a space. Here, the length of s is one. Whereas here, if I delete this space, the length of s is zero. One more note for strings. I can have a string that looks a lot like an integer. So for example, I can say s equals five, and when I print out the value of s, then I get five. To Python, this is entirely different than saying something like i equals the integer i. So even though when I print these two things out, they look almost exactly the same in their representation, there are things that I can do with s or things that I can do with i that I can't do with the other because s is a string and i is an integer. Just to show that these are different types, I'm going to print out the type of s and I'm going to print out the type of i. So when I run this code, I see that i and s have different types, but this actually has consequences for what I can do with i and s. So for example, let's suppose that I wanted to print out what ten plus i was. I can say print out i plus ten. And now when I print out s, then I get five, and when I print out i plus ten, then I get fifteen. What if I tried to add ten to s, so I printed out s plus ten? Well, if I try to do this, then Python is actually going to give me a runtime error, and it's going to say that it can't concatenate string and integer objects on line four. Just to break that down, what it's saying is that s is a string and I can't add an integer to a string because even though s is a string whose first character happens to be five, to Python, that's still a string, but even though to us s looks like a number, to Python, s is just a sequence of characters, and so we can't add a number to a sequence of characters. To Python, that doesn't make any sense to do, and so we get a runtime error. If we did want to add five to something like s, then we would need to cast it first. So I could say print int s, and this call to the function int is going to cast the string s to be an integer, so the value of this overall expression is the integer five, and so now when I run my code, then I can see that I can add ten to the integer value of s. Alternatively, I could cast it to be a floating point number, so I could say float s plus ten, and then I would get 15.0 because the value of this expression is the string s or five casted to a float, and so the value of this overall expression is 5.0, and when we add 5.0 plus ten, then the value of this overall expression is 15.0. So one thing to note when we cast strings like s into numbers by calling something like float s is that when we call float or int to cast the string, then the string needs to be a valid number. What that means is that if I have something other than numbers in the string, so for instance let's suppose that I say 500, when I try to cast s into a float, then I get a runtime error, and what this is saying is that the string 500 is not a number. In other words, Python doesn't know how to convert this string into a float, and the same goes for integers. Welcome back. So where strings like s are sequences of characters, lists are a new kind of Python type, which are sequences of any type of value, which can include other strings or lists. So lists are created by using square brackets. So I start creating a list by using a left square bracket, and then I put my content separated by commas. So here I'll make the first item actually be a string. I'll make the second item be an integer, and I'll make the third item be another string. So when I print out my list, then I get back the items that I actually put into the list. Now, just like strings, lists have an order and a length. So in the case of this list, my list, then I actually have three items. The first item is the string 1, and then separating out every item is a comma. The second item is the integer 2, separated by a comma again, and the third item is the string 3. So I would say that this list has length