SPEAKER 1: Well, hello world! These are CS50's New Year's seminars. So first and foremost, a very happy 2021 to everyone. We hope this finds everyone healthy and well. This is the very first time that we've done something like this on the internet. I teach this course called CS50, which is Harvard University's introduction to the intellectual enterprises of computer science in the arts of programming. And this, as well as a number of other courses taught by some of my colleagues at Harvard, are freely available on edX and beyond. So odds are you've discovered at least one of those classes at some point, or you're here with a friend or family member or colleague, in which case, welcome to CS50's community. The goal of these seminars is to give everyone truly a taste of CS50, and a taste of a number of topics, today's being Python. And so in a bit, I'll introduce you to today's teacher, my dear colleague at Harvard, Brian Yu. So for this first seminar on Python, and for our subsequent seminars on SQL for databases and for Linux on computing, and ultimately for game development, we're pleased and we're thrilled to say that we've had over 10,000 registrants for these and the upcoming seminars. And more amazingly too, we've had to more than 500 volunteers join us today and in the coming days as mentors, who you'll see some of in the chat, some of on ed, the online Q&A platform that we'll use as well. And let me emphasize here, if you are completely new to Python, to programming, to CS50, to Zoom, certainly do not worry if at any point you fall a little bit behind today. If you have any trouble following along, we'll tend to try to keep as many people on the same page as we can. But as you might recall from the real world, it's hard enough to keep 20 students in a physical classroom perhaps on the same page, let alone 1,000. So that's why we have this other platform, ed, so that you can ask questions at your own pace, either during the seminar or after. The chat window will likely get a bit out of control with so many people commenting and asking questions here, so please do feel welcome to tune in via ed after today's seminar, in order to ask your questions as well, and get more comfortable ultimately with the world of computer science. Without further ado, allow me to introduce my colleague, CS50's own Brian Yu, who teaches not only CS50's Introduction to Web Programming, but also CS50's Introduction to Artificial Intelligence here at Harvard, which also is available online. CS50's own Brian Yu. BRIAN YU: Thanks so much, David. It's so good to see everyone here today. Welcome everyone to the very first of CS50's New Year's seminars. As David mentioned today, we'll be introducing the programming language Python. So if you've never programmed before, this will be an opportunity to see what it's all about and to get your hands, get some experimenting with writing your very first programs. So while I'm presenting, definitely feel free to ask any questions in the Zoom chat. If you go down to the bottom of your Zoom window, you'll probably see a button that says Chat, which you can click to say hello to other classmates and ask questions if you have any. We have a fantastic group of mentors that are here in the meeting with us today, that are here to help answer questions too. And in addition to that, we also have ed, which David mentioned is another platform where you can ask your questions. And I'll go ahead and paste that link into the chat now. So feel free to go there in order to ask questions as well. And the staff can definitely be around to help you there, too. Before we get started, as was mentioned in the email but if you haven't already, be sure to sign up for a GitHub account if you don't already have one. You'll use your GitHub account as a means of signing into some of the course's tools. And if you go on to take CS50 after the fact, you'll do a little more work with GitHub itself. So you can go to GitHub.com/join, the link to which I just pasted into the chat, to join GitHub if you don't already have an account. And we're going to be spending most of today inside of an IDE, or Integrated Development Environment, called CS50 IDE. And all that means is it's a tool that lives in the cloud, which will be a place where you can write and run Python programs on the internet. And so we'll get an opportunity to explore that if you've never used it before. But what you can do is go to this URL here that I've just pasted into the chat, ide.cs50.io. And once you go there, you can sign in with your GitHub username. And in a moment, I'll introduce that platform to you, show you how to use it, and we'll go about writing our very first Python programs. And so with that, let's go ahead and dive into a taste of Python. So in a moment, once you go to ide.cs50.io and sign in with your GitHub username, you should ultimately end up seeing an environment that looks something like this. This is CS50 IDE, which is a cloud-based platform where you can write and run code. And that's what we're going to be using today in order to write programs in this programming language called Python. Now, it's possible to run Python programs just on your own computer without using CS50 IDE. But today, with so many students, to make sure everyone's on the same page, we're using CS50 IDE to make sure everyone's in a consistent environment, to make sure everyone's seeing the same thing at the same time. And so over here, on the left hand side of CS50 IDE, this is our file browser. Any of the files that we create and add to our IDE, they will appear here. Right now, if you've never used CS50 IDE before, this is probably empty. But over time today, we're going to be adding some more files, and you'll be adding some more files to this yourself, too. Up in this top panel of CS50 IDE is the text editor. This is where we're actually going to be writing our Python code. And then down below is what we call the terminal window. This is a place where we're going to be writing text-based commands, telling our IDE what we want it to do. So ultimately, we're going to be writing code in this top section here. And then down below, we're going to write commands in order to actually run that Python code as well. And so I see that a couple people, if you're finding that the IDE is taking a bit of time to load, just give it some time to load until you see this screen. But certainly, if you're having any trouble, feel free to make a post on ed, or post in the chat. And the mentors can definitely help out with whatever challenges you might be having. And otherwise, you can feel free to follow along with what I'm doing here. And so to start writing our very first Python program, I'm going to go up to the File menu in CS50 IDE, and click on New File. We're just going to create a new empty text file. And this text file is going to be where we're going to start by writing our very first Python program. And the simplest of Python programs we're going to write is going to look something like this. I can write print, and then ("hello, world"). And I'm going to actually save this file first. I'm going to go to File, Save. And I'm going to save this in a file called hello.py. That py. extension is just a convention. When you're writing programs in Python, they'll very often end with .py. That way we and the computer will know that it is in fact a Python program. So I'm going to name this file hello.py. And I'm going to click on Save. And this, again, is my very first Python program. It is one line of code. And the way to decipher this, the way to read this as you might imagine, this program is going to print, in other words display to the screen, the words hello, world. And the way this works is I've written this word print. This is what we in the programming world would call a function. It is some code that is going to run, that is going to perform some task, and the task that this function print performs, is that it prints or displays and some text to the screen. And in between these parentheses that come after the word print, comes the input to the function. Functions can take input, some information that the functions use. And right now the input that I'm providing to the print function inside of these quotation marks are the words hello, world. These are the words that I want to be displayed to the screen when I run this program. And so once I've written this program, I write it in this top section inside of this file called hello.py I need to be sure to save the file, if you haven't already, by going to File and then Save. Now, I can actually run this program. And the way that you run a program is by going into the terminal window, which is in this bottom half of CS50 IDE. And here I can write commands that my computer, that my CS50 IDE is going to follow. And the command I'm going to use here is a command called python, which is a command that will run a Python program. And then after the word python, I'm going to specify the name of the file that I want to run. And you might recall that the file that I opened from before is a file called hello.py. This is the file where I've been writing this code so far. And so I'm going to write Python, then a space, and then hello.py as the name of the file that I would like to run. And it's going to be a Python program. And once I've written that command, python space hello.py, I can just press the Return button to run this program. And what you'll see, if you've been following what I've done, is you'll see the words hello, world printed to the screen. And so I'll give you all a moment to try and copy what I've done here. All I've done so far is go to ide.cs50.io. I've created a new file by going to File, New File, inside of which I've just written one line of code-- print, and then in parentheses the words hello, world in quotation marks. I've saved that in a file called hello.py. And then down here in the terminal window, I've written the command python hello.py to run that Python program. And that is going to cause the IDE to run my Python code, reading it one line at a time, top to bottom. Right now, there's just one line of code inside of my program. And that one line of code prints the words hello, world to my terminal screen. All right, I'll give everyone a moment to try that for themselves. All right. So, let's try to do something a little bit more interesting now. We've seen now one line of code, one line of Python code that uses this function called print. And print is a function that's built into Python, it's part of Python itself. And so once you start using Python, you'll be able to use this print function that was written by the developers of Python that we can use to display information to the screen. But there are other functions that are part of Python as well that we can use inside of our Python programs. And another function is called the input function. And the input function is a function that asks the user, whoever is running your program, to type in some input that they're going to provide to the program. And so we'll see a look at that in a moment, too. I see a question asked about whether you need a semicolon at the end of the line. Turns out in Python, that's not necessary. If you've programmed a little bit before, maybe seen languages like C or Java for example, you might have put semicolons at the end of all of your lines that are printing things out, for example. In Python, we don't need those semicolons. In Python here, we just have to say the name of the function print, and then in parentheses hello, world. But that's an excellent question, so thank you for asking that. So let me try now creating another program that's going to use this other function called the input function, so that we can see how that input function works, as well. So I'll go ahead and go to the File menu and create another new file. And this file I'm going to save by going to File, Save. I'm going to call this file greet.py. And you'll see why in a moment. But inside of this new program greet.py, I'm going to, instead of using the print function, which would print out some text to the screen, which we've already seen, we're going to use the input function. And here I'm going to have a prompt, prompt asking the user to type something in as input. And the prompt I'm going to use is something like ("What's your name? ") your name? And then I'll include a space in a moment, for reasons you'll see in just a second. And again, this text could be anything you want. You can write whatever text you want in between those quotation marks. In Python, and in programming more general, any text inside of quotation marks is called a string. A string is just a fancy word for some text, like words or sentences or paragraphs. And here this string, which is in quotation marks, is just asking a question, what's your name? And because it's in the input function, that is going to prompt the user to type in some input. In this case, I'm prompting the user to type in their name, for example. And I'm seeing a couple of people in the chat that are having some trouble loading CS50 IDE. If you are having trouble loading CS50 IDE, my recommendation would be to first make sure you're using the latest version of Google Chrome or Firefox. If you're using a different browser or using an older version of one of those browsers, you'll probably have the best luck with CS50 IDE by using the latest version of Chrome or Firefox. And the other thing you'll want to check is make sure that any ad blockers or other browser extensions, if you have those turned on, be sure to disable those for using CS50 IDE, just to maximize the chances that you'll be able to get in without any problems. And if you're still having issues, try reloading the IDE or asking a question in the chat. And the staff and the mentors will be around to try to help you with that, as well if you're struggling. But if you are having problems, you're also welcome to just follow along with what I'm writing in my IDE. And then you can try it in your own IDE after the fact, if you would like to. So, if you are having trouble with the IDE, those might be good suggestions for trying to fix that. But let's turn our attention now back to this program greet.py, where I'm writing this function input, asking the question, what's your name, and let's now try this. This program's not done yet. There's more that I want to do with this program. But one strategy that I use when writing programs is not to try writing many, many lines all at once and then run it, but to build my programs a little bit at a time. And in a moment, I'll give you all an opportunity to try writing some programs of your own. And I'd recommend following that same strategy. Write everything a little bit at a time, and try it as you go. And so here, I'm going to, in my terminal window, write python, and then the new file that I would like to run, which in this case is greet.py. That was the name of the file that I just created. And when I run python greet.py, I see printed to the screen What's your name? And notice that my cursor is now right next to What's your name after a space. This is why I included the space up here on this very first line of my program. And my program is now paused. It's waiting for me, the user of this program, to provide some input to the program. So it's asking me what's your name? So I'll go ahead and type in my name. And you can do the same thing, typing in your name. I'll type in Brian down here at the terminal. And then I'll just go ahead and press Return to send that input to the program. And at that point, what you'll notice is that this dollar sign appears again. This dollar sign is just called the prompt. The prompt is telling you, you can type in another command to the program now. And usually when you see the prompt after running a Python program, that means the Python program is over. You've run the Python program, it's run to completion, and the prompt means the program is now done. And you can now run another program if you would like to, by running Python followed by whatever file name you would like to run. And so this greet.py program now is asking the user to type in some input. And we can type in some input. But we're not yet doing anything with that input. I would like for my program to be able to do something like, when I say my name is Brian, maybe my program can respond to me and say, hello, Brian, for example, using whatever it is that I typed in as input and using that later inside of the program. And so here, we're going to introduce a new idea. We're going to introduce the notion of a variable. A variable in Python is just going to be someplace where we can store information. Oftentimes, our programs are going to want to store information that they're going to use later inside of that program. You might imagine if you're writing a program that's going to be like a calculator that can do math, you might want variables to keep track of what numbers you're adding or multiplying or subtracting. And in this program, where we are asking the user for their name, we probably want some variable where we're going to store whatever it is the user typed in, whatever name the user provided as input. So how do we create a variable in Python? Well, the first thing we need to do is decide on what we're going to call that variable. And we can choose to call the variable whatever we want. But it's usually helpful to give it a descriptive name, some name that's going to be meaningful or something you'll remember later on in your program. And so because I want to use this variable to store the name of whoever is using my program, I'm just going to call this variable name. And so what I'll change in line one is I'm going to say name equals, and then this input line that I had before. So all I've done is add name equals to the beginning of this line. And so now what this line of code is saying is I'm going to ask the user for some input, prompting them to type in their name by asking what's your name, and then waiting for the user to type in their name. And then this equals is what in Python we call assignment. It means we are going to assign whatever was typed in by the user to this variable name. We're going to take whatever the user typed in and store it inside of this variable that we are here calling name. I see a question, do we need to initialize the variable? And so if you're familiar with other programming languages, not to worry if you haven't programmed in other languages before, you might be familiar with having to initially say what type of variable any variable name is. In Python, you don't have to do that. The only thing you need to do to create a new variable is have the name of the variable, choose whatever name you want to give it, then an equal sign saying this is what I want this variable to be equal to. And then what do I want it to be equal to? Well, I want it to be whatever comes back when I ask the user to type in their name. And so now, I can use that variable, that variable called name, inside of my program. And so what I'm going to do is on line 2, we now have a program that has multiple lines of code, and that's OK. What's going to happen is that our computer is going to read these lines of code one after another in sequence. Let's go ahead and print hello and then a comma. And now let me, on another line for now, let me print (name). And notice, name here is not in quotation marks, because I don't want to literally print out the characters N-A-M-E. But I instead want to print out whatever the contents of this variable name is. Whatever is stored inside of that variable, that is what I want to have printed to the terminal. All right. So what I can do now is go ahead and run python greet.py again. I see that it prompts me again for what's my name. I can type in my name, Brian. And now I see printed on line 2, I see hello comma. And then an after that, I see printed the name Brian. And I see a couple of other people that are saying they were having trouble accessing the IDE. If you are you could also try going to sandbox.cs50.io, which has been suggested by a couple of people in the chat. That will open up a very similar programming environment to the one I see here. It's not going to be exactly the same. But you'll notice many similar elements. You'll notice that you'll have a file browser, where you can have some files. You'll notice a place, where you can type your code inside of a file text editor. And you'll also notice a terminal window down below, where you can run these commands. Ultimately, it should behave in exactly the same way. But it just might look a little bit different to what it is that I'm seeing here, but certainly OK to be using sandbox.cs50.io if you would like, in order to follow along here as well. All right. So I can try this again. And I can try typing in a different name. And you'll notice that whatever name I type in, that gets stored inside of the variable. And that's going to get printed out instead. So if I write python greet.py, and instead say What's your name. Let's try a different name, let's try David. Then I see hello comma. And then on the next line I see David. And so one thing I'm wondering as I'm writing this program is, why is there a new line in between hello comma and David? And that's probably not what I would expect. Like, if I was talking to a person and they were saying hello to me, they'd probably just include the whole thing on the same line, just hello comma, and then a space, and then the name instead of a brand new line. And so why might that be? Anyone have a guess as to what might be causing this? And I see a comment from Andrea, who put in the chat that Python is just automatically adding a new line for us. And this is something that Python is trying to do in order to be helpful. And Python just says that normally if you're trying to print something out, like I'm printing the word hello, then at the end of printing out whatever that happens to be, we're just going to automatically break to a new line. And so we're saying hello, then a new line, and then David. So how do I fix that problem? How do I make it so that it's all on one line instead? I have a couple of options for how I could do this. But what I'm going to do now is, if Python is adding a new line at the end of every time I call this print function, every time I use the print function, well then, I'm just going to use the print function once. It turns out that the print function can take multiple inputs. I don't just have to provide one thing for the print function to print. I can provide multiple things for the function to print. So I could say print hello. And then after this quotation mark, after the end of the hello string, I'm going to add a comma. And then I'm going to write name, which is the name of that variable. And so now, what I have happening here is that I now have a line of code that's asking the user to type in their name. We're storing that inside of this variable called name, and then I'm printing hello comma. And then after the comma, we're printing out that name as well. And so again, there were multiple ways you could do this. I see a couple people suggesting other ways you could do that. And that's totally fine, too. Definitely multiple ways you can solve the same problem. And that's true of programming more generally, that when you have something that you're trying to write code to solve a problem, there is not usually going to be just one way to solve that problem. There are going to be multiple ways to solve the problem. And you can think about which way makes more sense to you, or which way you think is going to be better. And so today we'll have an opportunity to explore a couple of different ways that you could solve that problem. I see a lot of people suggesting that you can use plus to combine two different pieces of information in Python. So I could say hello plus name, and so that might work OK too. So a couple of different options for how you can accomplish things like this. But let's try this again now to see how it worked. I'm going to write python greet.py. It's going to prompt me to type in my name. What's my name? I'll type in Brian. Press Return. And now just on one line it says hello Brian, which is what I expect and what I would want it to do. So I'll pause here to give people an opportunity to try to follow along or ask questions if people have any. I see [? Manvir ?] is asking, do I need to write python greet.py every time you want to run the code? And the answer is yes. You need to write that command python greet.py in order to run it. Though there is a little bit of a trick, which is that if you are running the same program multiple times, like here I'm writing python greet.py, then I've run it again a couple of times, the trick is that if you're in your terminal you can usually press the up arrow key on your keyboard, and that will go to the most recent command that you have run in the terminal. So if you just ran python greet.py and you want to run it again, you can go into your terminal, press the up arrow key, and then you can run python greet.py. Again I can try a different name. For example, I can try hello, David. And it says hello, David now as well. How do you open the terminal in the IDE, I see someone is asking. It should be open for you by default already inside of CS50 IDE. But if you've closed it by accident for some reason and you need to get access to a new terminal, not a problem. You should see a green plus button inside of CS50 IDE. And if you click on that button, there will be a button that says a new terminal. And if you click on that option, you'll get a new terminal window where you can run your code there as well. So that is how you could create a new terminal if you wanted to. All right. So now that we have the ability to have variables, storing information, I want to try something out. I want to try something a little more interesting. Maybe one of the simplest things I can think of to try is to try to build a calculator. Try and build a program that can do some math, like take two numbers and add them together the way that a calculator could. So I'm going to try and do just that. I'm going to write a program that is going to accept two numbers as input, maybe 1 and 2. And it's going to add those two numbers together for me and print out what the result is. I see a couple of people, before I get to that, asking me, how do I clear the terminal when I'm done with it, when I want to make it clean again? The simplest way to do it is in your terminal, just type the word clear and press Return. And that'll clear out the terminal for you if you would like to do that. So I'm going to create a new Python program, and feel free to follow along if you'd like. I'll create a new file. I'm going to save it as arithmetic.py, because I'm going to be doing some arithmetic, doing some addition. And what I want to do is, I want to ask the user to provide us input two numbers. And maybe I'll call them x and y as conventions from algebra. And how might I do that? Well, I can do input, and then I provide the prompt, what do I want them to type in. We'll type in x. So just write the letter x and then a colon to signal to the user, here's where I want you to type in the first number, the number x. And I'll add another line, input y, to mean all right, now I want the user to type in a second number, to type in the number y. So I have two lines of code right now. Each line is asking the user to type in some input, and let's try it out. Of course, the program's not done yet, but I just want to test this to make sure that it's working. I'll run python arithmetic.py. It asks me to type an x. I can type in a number, maybe the number 1. Then y, I'll type in another number, the number 2. And the program's over. I see this prompt again that tells me the program is done. And of course, my program didn't do anything. It didn't actually add the numbers together, because I haven't told the program to add the numbers together yet. And my Python program will only do what I tell it to do by writing some programming instructions. So how would I write a program now that is going to accept two numbers and print out what the sum of those two numbers is? Well, the first thing that I'll probably want to do is store whatever came in as input inside of variables. And this is a common idea in programming. Anytime you want to be keeping track of information, you'll often want a variable to keep track of that information. And so I'll call this variable x. So I'll just say x equals whatever that input is. And then on line 2, I'll add a y equals. So I now have two variables, x and y. x is going to be equal to whatever the user typed in on that first line. Then we're going to prompt the user for another number. And we're going to save that inside of y. And then now on this third line, let me try something out. Let me print (x + y). So what I would like for this program to do is ask the user for two numbers, for a number x and a number y, and then print out what is x plus y. So let's try it out. Let's try python arithmetic.py. I'll try typing the number 1. And then I'm going to type the number 2 as x and y. And anyone have a guess what's going to happen when I press Return, when I submit this as input? What am I going to see? All right, I see some people saying 3 and some people saying 12. Yeah, I would hope it's 3. I want the answer to be 3, because that's what 1 plus 2 is. But I do for some reason get 12. Yeah, I see a lot of people in the chat that are suggesting that that was the answer. So you predicted it. And this is maybe the first example of a bug in programming. And once you start programming, inevitably you're going to run into bugs. But that's OK, totally normal. Whenever you encounter a bug, it's time to do some debugging, trying to figure out what is it that went wrong when I was writing this program, and how could I fix it. What is it that I could change to get this program to work? So why am I getting this problem? What should I fix? All right, yeah, I see a bunch of people saying that I'm dealing with strings. Remember that I mentioned earlier that a string is just some text. And that's true. By default when I ask the user to type in some input, Python is going to treat that input as a string. It's just text as characters. And so I'm storing the character 1 and then the character 2. And as far as Python is concerned, to add two sets of characters together, to combine two strings, we're just going to take the first set of characters, the number 1, and then the number 2 and just mash them together. I've got 1 and then 2, so it's going to print out 1, 2. So that's maybe not quite what I wanted for it to do. So is there a solution, something that I can do to solve this problem? All right, and I see a couple of people suggesting that I need to somehow convert these strings, this text, into an integer. And a couple of people that maybe have programmed in Python before know that there is another function called int that will take something like a string and convert it into an int, otherwise known as an integer, which in this case is going to be a number, like 1, 2, 3, 4, 5, or maybe a negative number or maybe 0, but just some whole number or negative number or 0, that I can then manipulate, I can add together, for example. And so how do I do that? Well, I'm going to on line 1, say int, and then an open parenthesis, and the whole of this input line is going to go inside of those parentheses. So the way to read that is that I'm asking for input. I'm asking the user to type in some value x. And that itself is the input to another function called int. And remember the input to that function goes inside of the parentheses, which is why that value is inside of those parentheses. And so this might look a little weird if you've never programmed before, but what I am now doing is asking for input, converting that input into an integer, and then storing that input inside of this variable called x. And now, I'll do the same thing with y. I want to take that and convert it into an integer as well. And now, both x and y are going to be integers. They're going to be numbers. Int is short for integer, for [INAUDIBLE], who I see is asking about that in the chat. And then I'm going to add those two values together, x plus y adding together two integers. And now, let's give the program a try. Let's see what happens. I can try 1 and then 2. And I do see the result that I expect, which in this case is the number 3. All right, so I've been able to add two integers together. And I've been able to resolve the bug that I had before, where the numbers weren't adding in the way that I expected for those two numbers to be added. So I'll pause here to give you an opportunity to try that out and to ask questions if you have any, questions about what it is that we've done here. Oh I see a question. Can you use single quotes instead of double quotes? So here around my strings, I'm using these double quotation marks. It is OK to use single quotation marks, too. Python does allow you to do that. I'm going to be using double quotation marks for today just to be consistent, but it is a matter of personal preference, and you can choose which one you'd like. I see another question. Are these spaces necessary around the equal sign? No, the spaces are not necessary. But it's often good style to include those spaces there. And so style refers to how your code looks. The computer doesn't really care whether there are spaces around things or not. But it's often going to be easier for the human, someone reading your code, to read if your code is well organized, if there's appropriate spacing, for example. And so you'll see me often add spacing, not because the computer requires it, but just because it's helpful to someone who's reading the code to make it a little bit cleaner as well. All right, so that was addition. But Python also allows other arithmetic operators, so to speak, other operations that I can do on numbers. I could use minus to do subtraction, for example. So I could try this out by going to python arithmetic.py. If I take 5 minus 1, for example, I see the answer is 4. That's what I would expect it to be. To do multiplication, you're going to use the asterisk key, which on many keyboards is above the 8. So x multiplied by y, via the asterisk. And so I can try this again. I'll maybe multiply 7 times 4, for example, get the number 28 as a result of multiplying those numbers together as well. And for division, I can use a slash. The slash is how I can divide one number by another. And so I can do this again, python arithmetic.py. I'll try doing 8 divided by 2, and I see that the answer is 4. And so you can use these various different arithmetic operators to perform calculations, by taking numbers and manipulating those numbers. And in a lot of programs, you'll end up needing to do a little bit of math. And the computer is very fast at being able to quickly perform mathematical operations. Oh, I see a great question. Why is it that when I divide, the answer is 4.0 and not just 4? So Python realizes that when I divide one integer by another, I might not get a perfect whole number. If I take 3 divided by 2, for example, well, 3 doesn't go evenly into 2. What I would get is something like 1 and 1/2. And so let's try this actually. I'll try 3 divided by 2, and the answer I get is 1.5. And so Python is assuming when I perform division, that what I might get is something after the decimal. So it's actually given me a different type of data, something called a floating point number. You'll often hear this just called a float when programmers are talking about it. And a floating point number is just a number that might have something after the decimal. An integer is always just going to be a whole number or a negative number or 0, something like negative 1 or 0 or 1 or 2 or 3, whereas a floating point number could have something after the decimal, something like 1.5, in this case. And Python is just being a little bit smart, noting that when I do division on two integers, I might end up getting a floating point number instead. Excellent question there, too. All right, so let's try and explore some other features of Python. In a moment, after we do a couple more exercises, I'll give you an opportunity to try a little bit on your own, too. But what I'm going to introduce now is the notion of a condition. And so, so far, when we have been writing Python programs our programs are just running one line at a time, top to bottom, running every single line of code. But our programs can get a lot more interesting than that. And ideally, what we want our programs to be able to do is to sometimes run some code, and sometimes not run that code, to be able to make a decision about whether to run some code or not. You've probably experienced this on your own, too. Maybe when you're on a website and only if you click on a button, something happens. So if you do something, then some code is going to run, but only if something is actually true. So let's give that a try. How can we introduce this notion of conditions, this notion of if to be able to conditionally perform some action? So I'm going to create a new file. I'm going to call this conditions.py, some new Python program. And here's what we're going to do. I'm going to start the same way I started my arithmetic program, by asking for an integer from the user. So x is going to be an integer. I'm going to ask for input from the user, which is going to just be x. And let's come up with a simple condition now. I'm going to say if x is greater than 0 and then a colon. And then now this is going to be a little bit new. But on a new line, and this line is going to be indented. And if you're using CS50 IDE or CS50 Sandbox, you'll probably find that the IDE is going to automatically indent this line for you, because it's guessing that you want to indent it. I'll explain why in a moment. Let me just print out ("positive"). So what's going on here? Here on line 3, we're asking a question. We're asking, if x is greater than 0, is it true that x, this variable, is greater than 0? And this colon is going to introduce some code that I'm going to run, only if this code is true, only if x is greater than 0, I'm going to print the word positive. And the way to read this is that anything that's indented underneath this line if, is the code that is only going to run if that condition is true. And so because I've indented this line underneath the if, this line of code is only going to run if x is actually greater than 0. And this gives our code the ability to make a decision, to decide whether to run this line of code or not. And so I can give this a try. I'm going to run this program, Python conditions.py, and watch. Let me type in at a positive number. I'll type in the number 2, for example. And it prints out the word positive. Great, exactly what I would expect for it to do. But let me now try to print out a number that's not positive. Let me try negative 8, for example, a negative number. And let's see what happens. And notice nothing gets printed out. The word positive doesn't get printed out, even though I have a line of code that says print positive here. And the reason why is because my Python program asks this question, is x greater than 0? If it's true we print out the word positive. But otherwise, we don't print anything at all. And the program is just over. Yeah, for those who are familiar with programming in C, if you've maybe taken the beginning of CS50, this looks a little bit different than C. We don't have those curly braces to indicate that there is a condition. Python syntax is a little bit different, admittedly. But the idea is still the same idea. The idea is that you need to ask the question, and then inside of the if condition goes whatever it is that we want to print. Is the indentation necessary, I see someone asking. In fact, it is necessary. Watch what happens if I try not to include that indentation. I'll try putting everything just aligned on the left hand side. Let's run the program. And immediately, I get an error. This is the first Python exception, or error, that we've seen so far. And my Python error is that there is an indentation error. We expected an indented block, and Python's even helpful enough to tell me what line that error is coming from. It's coming from line 4 of conditions.py. And so line 4 of conditions.py seems to be having some sort of error. The error is that we were supposed to indent something but it wasn't indented. And so I do, in fact, need to indent this line in order for my code to work correctly. So the indentation is necessary in Python. All right, so let's try and add to this program a little bit. So right now I'm printing something if the number is positive. Let's try and add what happens if it's not positive. So this is what we're going to call an else. So in programming we can often say if something is true, do this, but otherwise else, do something else. So we're going to run one block of code, or we're going to run some other block of code. And so underneath the if condition, not indented, I'm going to write the word else, meaning if that condition wasn't true, here's what I want to do instead. And what I want to do instead is I want to print the word negative instead. And so now the way to read this program is that if x is greater than 0, I'm going to print the word positive. Else if it's not greater than 0, I'm going to print negative. And that way I'll either get one of these blocks of code to run or the other block of code to run, instead. And so I'll try running this I'll try running Python conditions.py. Again, if I provide a positive number, I can type the number 2 and it's going to say positive. But if instead I type a negative number, I'll try typing the number negative 8, and I get negative printed out instead. So I'm never going to get both printed out. If the condition is true, then line 4 is going to run. If it's not true this else case is going to run instead. And so you can add that to handle what's going to happen if the condition is not true, instead. Now maybe there's a slight bug in this program. And I think a couple of people have noticed this bug already, taking a look at the chat. But does anyone else see what the maybe bug is in my program right now? Yeah I see a bunch of people suggesting what's going to happen if I try the number 0? And so this is a case maybe I didn't think about yet. So let's try this out. What happens if I run this program, but I instead type the number 0? All right, well the number is 0. My program somehow thinks is negative, when really 0 isn't really negative, it's just not positive, it's not negative, it's just 0. And so I would like to be able to handle now a situation where I can deal with both positive numbers and negative numbers and 0. And so here I'm going to show you how you could chain together multiple conditions, and the syntax for this is a little bit unusual. But here on line 5, instead of just saying else, meaning in all other cases go ahead and print the word negative, let's go ahead and say elif, which seems a little bit weird. But elif, which is short for else if, x is less than 0. This is me now asking another question. So if x is greater than 0, we're going to print out positive. Otherwise else, if our variable is less than 0, we're going to print out negative. And now at the very end, I'm going to add one more else, where I'm going to print 0, meaning if the number is not positive and it's not negative, the only other option is that it's 0. And so we're going to print out 0 instead. And so this allows me to make multiple different decisions based on questions that I might ask inside of my program. And so I'll run Python conditions.py. Let me try typing a positive number, 2. That's positive, very good. I'll type a negative number, that's negative. And I'll try typing 0, and that's going to be 0. And so now I have these different conditions that can handle these various different situations that might arise as I run this code. All right, give people an opportunity to experiment with that. You do not need parentheses around the if statements, which a couple of people are asking about. It is OK to include them, but not necessary in Python. And so we'll generally not include those parentheses if ultimately they're not necessary. The one thing that is important, I see one person ran into an error with this, is this colon at the end of the if line. That is, in fact, necessary. And that just means whatever comes next, this is going to be part of that condition. All right so conditions are a very powerful part of programming, and they're going to allow us to achieve a lot of very interesting things with our programs that we'll have an opportunity to experiment with as well. But one other thing I want to demonstrate is the idea of loops, and we'll see the idea here in a moment. I'm going to create a new file called loops.py. Oftentimes in a program, you are going to want to run some code multiple times, because often the advantage of a computer is it can do many things very quickly. It can repeat some code again and again and again. And so how might I go about doing this? Well, let's imagine, remember from my very first program I printed out hello, world. Imagine I wanted to print out hello, world 10 times, let's say. How could I do that? Well, one way would be let's take this line hello, world and let me just print("hello, world"), print("hello, world"). And let me just copy paste it 10 times. Just like that, I've printed out ("hello, world") 10 times. And let me now run this program. And I do see hello, world get printed out 10 times, so I have achieved what it is that I wanted to achieve. But why is this maybe not such a great design for a program? And now we're introducing this notion of design. How can you create a better designed program? Why is this not ideal? Yeah, I see [? Marciara ?] is asking what if I had to do this 100 times? Then I'd have to copy this line 100 times. If I wanted to change the number of times, it's not so easy to do. And it's not very concise. I'm repeating myself a lot, so any time you find yourself repeating yourself a lot, writing lengthy code that might be hard to read, maybe there's an opportunity to use something called a loop. And there's multiple ways to use a loop in Python. We'll explore a couple of those ways right now. But here's one way. I'll write the code first and then explain where it comes from. Let me give myself a variable called i. I is just a common variable that's used for keeping track of a loop, but it's just a variable. You could name it whatever you want. And now I'm going to write a line of code that says while i is less than 10, print hello, world, and this is going to be indented. And then i equals i plus 1. So what am I doing here? I'm creating a variable i equals 0. And then this line is what we call a while loop. And a while loop is going to run some code over and over and over again repeatedly, as long as some condition is true, as long as some question is answered with a yes. So as long as this variable i is less than 10, what are we going to do? We're going to run all of these lines that are indented. We're going to print("hello, world"). And then beneath that, I'm running this line i equals i plus 1. And if you've never seen a line like this, it might look a little weird from the world of math, like i is not equal to i plus 1. Those are two different numbers. But this equals sign, remember, stores something inside of a variable. So what I'm doing here is changing what is stored inside of the variable i. What I would want it to be equal to now, well, whatever i plus 1 is equal to. So if I right now the number 0, then i plus 1 is going to be the number 1. And we're going to store that number 1 inside of i. I'm changing the value of a variable. Variables don't just stay the same, they can change, and so I can update the value of i, increasing it by 1, and then try again. Now, i is 1, which is still less than 10, so we print("hello, world"). I increases to 2, and we run it again. And this is going to keep happening. i is going to be 3, 4, 5, 6, 7, every time printing out hello, world, until we get to a point where i is equal to 10. And at that point, i is not less than 10. 10 is not less than 10, and so we're going to exit the loop. The condition is no longer true. And the loop is going to be over. And so if I run this code now, I see hello, world repeated 10 times. And notice, I only needed to write the line of code print hello one time. The loop, the while loop, is taking care of the process for me of counting how many times I've run this so far. And as long as i is less than 10, we're going to keep printing hello, world again and again and again. And this makes my code much more flexible. If I only wanted to print hello, world three times, I could change the 10 to 3. And now, it's only going to print hello, world three times. If I wanted to print hello, world 1,000 times, I could change this number to 1,000. And now, it's going to print hello, world 1,000 times. And so you can change this number to whatever we want it to be to decide how many times we want to print out hello, world. I see a couple of people, including [? Prince ?] in the chat, suggesting that there is a more concise way that I can write this line. It turns out that updating a variable, like increasing a variable by 1 or by some other number, happens so often in programming, we do this so much when writing code, that there is a shorthand, a shorter way of writing the same thing. I can write the line of code i += equals 1, to mean add this value to i. What value do I want to add to i? I want to add the value 1. And that's just a shorter, simpler way of expressing the same idea. My code will run exactly the same way, but this is just a little bit more of a concise way of expressing that same thing. So this is what we in programming call a while loop, and this is useful anytime we want to ask a question and then keep running this code as long as the answer to that question is yes, as long as the condition is true. It turns out there's another type of loop I see a couple of people suggesting called a for loop. A for loop is very helpful if you know exactly how many times you want some code to run, and it's just going to be a little bit more concise for this particular case. And I'll show you what a for loop would look like. A for loop would look like this. I'm going to delete all of this code. I'm instead going to say for i in range 5, print hello, world. And this is going to do exactly the same thing as before. What this is saying is range 5 means get me a range of numbers-- give me a range of five numbers. That's going to give you the number 0, 1, 2, 3, and 4, five different numbers starting at 0 going up to 4. And for each of those values, go ahead and print out hello, world. So five times for all the numbers in the range of five numbers, print out hello, world. And this is going to do the same thing. Five times it's going to print hello, world. And so this is an opportunity now to simplify our code a little bit. We could have used a while loop. That worked perfectly fine. But if I know exactly how many times I want the code to run, I want it to run five times, oftentimes a for loop is just a little bit more concise. I see someone asking in the chat, could we take user input? And the answer is yes we could. In fact, let's try that now. I'm going to add a line of code above my for loops, up at line 1. I've moved the for loop down a little bit. Let me ask for a number. I'll call that number n, but you could call it whatever you want, from the user. I'm going to use int and input again, which we've seen a couple of times now. I'm asking the user to type in a number, and I'm going to save whatever the user types in inside of this variable n. And now watch what I'm going to do. I'm going to replace this number 5 with that variable n. So it's not just going to print hello, world 5 times. It's going to print hello, world n times, where n is whatever it is the user typed in as input. So let's give it a try. We run python loops.py. It's asking me now to type in a number. Let me type in the number 3. And it prints hello, world three times. If instead I tried typing the number 5, well then it's going to print hello, world five times. And so I can now decide how many times something runs. And this is really the power of loops and variables. I can store some input in a variable and use that information to decide how many times my code is ultimately going to run. All right I'll pause here for questions. I see someone asking, why did I put the colon after the number? Is that required too? It's not. This colon on line 3 after the for loop, that is required. Much like an if condition, I need a colon to say everything indented after this line, that's going to be part of the loop. This colon inside these quotation marks is just part of the text, it's part of the user interface. It causes a colon to appear printed out to the user. I just use that as a prompt to the user to let them know that here now is an opportunity for you to type in a number, for example. So now we've seen a bunch of different features of Python. We've seen variables, we've seen printing out values, we've seen conditions, we've seen loops. I'm going to go ahead and give you your first exercise, actually. So I'm going to introduce the exercise. And in a moment I'm going to stop talking, give you an opportunity to try in CS50 IDE, or inside of the Sandbox if you're using that, to try writing a program of your very own. Then we'll come back together. You don't have to leave the meeting. You can stay here, but we'll come back together afterwards, talk about it, move on to talk about some other features of Python as well. And we'll introduce our very first programming exercise for you. So I'll introduce the context for the exercise. I've been doing a little bit of running lately. And I've wanted to keep track of how many miles have I've been running? So maybe I run on 10 different days, and I want to add up how far am I running on each of these days? How far have I run in total? So we're going to try and write a program to help me out with that a little bit. And so what I'm going to ask you to do is follow these instructions here. You're going to write a program called sum. That's going to ask the user to type in 10 integers as input. And then you're going to print out their sum. So the user is going to be prompted 10 times to type in numbers. And then at the end, after the user types in 10 numbers, you're going to print out the sum of all of those 10 integers. And so I am going to go ahead and give you maybe 10 minutes on your own to try writing this program for yourself. You don't have to leave the meeting, you can stay here. But if you do have any questions, if you run into any trouble, feel free to ask a question on ed, the discussion forum where you can post the code that you've been working on, and the mentors who are in ed right now are going to be there to help answer questions. Valerie has just pasted the link to ed inside of the chat, which you can use to ask questions. You can create a new post. You'll go ahead and, inside of ed, what you'll do if you have a question you'll click on New Thread, go ahead and click on Question, and then go ahead and click on Python as the category for the category today, and then you can ask whatever question you want there and the staff will be around to help you out with that too. So we will go ahead and give you 10 minutes or so to work on that program. And so I will go ahead and throw up a timer. You should see the timer here. So we'll give you 10 minutes. I'm going to stop talking, give you 10 minutes to work on this program. And when we come back, we'll talk about it. So we'll see you in 10 minutes, feel free to work on this and ask questions in ed if you have any. All right, welcome back, everyone. Hopefully you enjoyed working on that. I saw a lot of great questions in the ed discussion forum as we were working through that. So let's talk about how we approached it. Does someone want describe to me what approach they took to trying to solve this problem? What kinds of programming techniques did you use? Actually, yeah [? Minwu, ?] why don't you go ahead. How did you approach the problem? AUDIENCE: Oh, so for me, I decided to go with an array, since I personally don't like working with like 10 variables listed at one line. So I decided to make an array called number, and then set the range so that, for i in range 10, I would append each number into the array. And I would use the system of using just adding everything in the array together, then printing out the final number to make the sum some function. BRIAN YU: Yeah, very nice. So we haven't actually seen these yet in today's seminar, but there are this notion of lists in Python that allow you to store a sequence of values. And so, as [? Minwu ?] did, you could have created a sequence of these 10 values, and then added those 10 values together. That was definitely one possible option for what you could have done here. Any other options that people chose to do, other things that you might have tried? Let's go to Alexandrina, if you'd like to offer what you tried. AUDIENCE: So I [INAUDIBLE] a variable called [INAUDIBLE], and I gave it value of 0 in the beginning. Then I have a for loop for i in range of 10. For each iteration of the loop, I get a number from the user using input, and converting it into an integer. And [INAUDIBLE] that I added to the sum. Having [INAUDIBLE] for the for loop. And then after the for loop, I [INAUDIBLE] the sum. BRIAN YU: All right very nice. You could have had a variable where you were continuously keeping track, as you loop through asking for input, adding to those values as well. So that was definitely something you could have tried, too. So multiple approaches you could have used to trying to solve this problem. So I'll just show you one way that you could have done that, and ultimately going to be very similar. And definitely, OK if you didn't manage to totally get through the problem. Programming takes practice, just takes some getting used to. So if you find yourself struggling, not entirely sure what to do, just keep up the practice, keep following along, and over time you'll start to build an intuition for when to use these types of ideas. So like Alexandrina suggested, I'm going to start by having a variable. I'm just going to call it total, because it's going to keep track of my total for each of these 10 times I'm asking for input. And if I want to ask for input 10 times, I could have an input number line, again and again and again, repeated 10 times. But as we've seen before, that's maybe not the best design. Maybe if I'm repeating this over and over, there's an opportunity there to use a loop, for example. And so we've seen that we can use a for loop to say something like, for i in range 10, to repeat something 10 times. And let me now ask for input from the user. That input is going to need to be an integer, so I'll convert it into an integer, and I'll go ahead and save that inside of a variable, just for now. Maybe you didn't do this, but we'll do it just to be clear about what we're doing. We're asking the user for a number n. We're storing that inside of this variable called n. And now what I want to do is add this to the total. The total starts at 0, but I want to start adding numbers to it now. So I could say total equals total plus n. Of course, I could shorten that as we've seen before to total plus equals n. That's just a shorter way of expressing that same idea. And at the very end, I'll go ahead and print out the sum is this total. So I start with a total variable that's set to 0. And now 10 times, I'm going to repeat the same thing. Ask the user to type in a number, store that number inside of the variable n, and then add that to my new running total. Then at the very end after I've run this code 10 times, I'm going to go ahead and print out whatever the sum of all of those numbers happens to be. So we can try running the program now and see what happens. I'll run python sum.py. Let's type in 1, 2, 3, 4, 5, 6, 7, 8, 9, and 10. And I see that the answer, the sum is 55. So I now have the ability to quickly add a whole bunch of numbers together, thanks to our Python program and thanks to some loops. So if you struggle with that, feel free to take a look at this. Feel free to continue to ask questions on ed if there are other questions that you have about how you would go about doing this, or if there are other approaches. And there are definitely other ways you could have solved this problem. It's not just one. We already heard at least two different ways. I'm seeing in the chat, some people that took other approaches as well. So definitely multiple ways you could have used to try to solve this problem. I saw one question in the chat as to when we might want to use a while loop instead of a for loop. So a for loop we've seen is very good if I know that I want to run this code 10 times, for example. When might a while loop come in handy? Well, a while loop is particularly useful if you don't necessarily know in advance how many times some piece of code is going to run, and you want to exit the loop only when something is true, or when something happens, you want to exit or break out of that loop. So I'm going to show you one additional example of that. I'll create a new file. I'm going to call this positive.py. Let's imagine, for example, that you are writing a program, and you want the user to provide you with a positive integer. You don't want to allow negative integers, you don't want to allow 0's. And this might be the case, for instance, if you're writing a program for a bank, and you're writing a program for an ATM to allow people to withdraw money. You probably only want to allow people to withdraw a positive amount of money and not to withdraw a negative amount of money, because it probably wouldn't make a lot of sense. So you can come up with use cases, where you really do want the user to type in a positive integer. So how could I do that? Well, I want to continue to ask the user to type in a positive integer until they give me a positive integer. So I can't really say for i in range 10, to ask the user again and again like I was doing before with the for loop, because I don't necessarily know that it's going to take 10 tries. Maybe it takes fewer tries, maybe it takes more tries. And so I'm going to introduce some logic that is quite common in Python. I'm actually going to have a loop that says while true, which might seem a little unusual. While true is going to run this loop as long as this condition is true, as we've been saying before. And therefore, while true is just always going to run. It's what we'd call an infinite loop, a loop that's going to go again and again and again. And normally, that means something would never stop. But we'll see in a moment how we can still exit or break out of this loop, even though it's running infinitely. I'm going to ask the user to type in a number. We've seen lines like this several times now, but let's ask the user to type in a positive integer. And now, when do I want to exit the loop? I want to exit the loop as soon as the user gives me a positive integer. If they're giving me something else, I need to keep running the loop. But as soon as they give me a positive integer, I would like to exit this loop. So I'm going to say if n is greater than 0, we're going to introduce a new statement called break. Break just means exit the loop, get out of the loop if n is greater than 0. And now at the very end, let's print that variable n. So let's run python positive.py. if I type in a negative number, let's say negative 2, it asks me to type in a positive integer. If I type in another negative integer, it prompts me again. If I type in 0, it prompts me again. Only once I type in a positive number does the loop exit, and then I see that positive number printed out. I see someone asking, does the T need to be capitalized? Yes it does. In Python, these are special keywords. Capital T, true, and capital F, false, are just special keywords in Python, and the T and the F do, in fact, need to be capitalized for that, which might be different from some other programming languages you may have used before, if you've programmed before. All right. So let's go ahead and try something else. Let's try another example of a loop. So it is CS50's New Year's seminars. We just passed a new year. And tradition on New Year's is to count down to the New Year. 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, Happy New Year. So let's try and write a Python program that does just that, counts down to the Happy New Year. We're a few days late, but that's OK. I'll create a new file. We'll call it countdown.py. And if I wanted to count down from 10, what could I do? Well, I could do print(10), then print(9), then print(8), then print(7). And I could do this again and again, printing all the way down to 1. But this, again, probably not the best design. I'm repeating myself a lot. There's lots of different print lines. So I'd like to be able to do this a little bit better. And so how might I do that? How could I solve this problem where I find myself repeating myself? Yeah, I see a bunch of people in the chat suggesting some kind of a loop, where we can use a loop to repeat something again and again. And so if I say for i in range 10, print i. Remember that when I say for i in range 10, range 10 is going to give me a range of 10 numbers. Perfect. That's exactly what I want. I want a range of 10 numbers. And it's going to loop over each one of those numbers, and I'm going to print out that value, i. And so I'll go ahead and do that, printing out all the values in those number range. And at the very end, let's print("Happy New Year!"). So I have a for loop to print out 10 numbers, and then I'm going to print out Happy New Year. What's going to happen if I try to run this program? Anyone have a guess? What am I going to see if I try and run this program? See an error. I'm not going to get an error. It's not going to crash, but it still might not be exactly what I want. Yeah, it's going to start counting up. When we're counting on a for loop, we're generally starting at 0 and moving forward. And so if I say python countdown.py, here's what I get-- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, Happy New Year. Maybe not the way most people ring in the New Year. Why did this happen? Well I got 10 numbers, but by default loops in Python are going to start at 0 and then count up. We get 10 numbers. 0 through 9 is 10 total numbers, from 0 all the way up through 9. So how could I solve this problem? I want for it to count down. And again, there are multiple ways I could solve this problem. But I'd like a way that's not going to involve any new syntax that we've never seen before. How can I maybe add a little bit of math to try and solve this problem? I see some suggestions. And I see Michael with a great suggestion in the chat, which is instead of printing i, I'm going to print 10 minus i. All right, that seems a little weird, but what's going on? Well originally, we printed out the number 0. What's 10 minus 0? Well that's just [? 0. ?] Then our code originally printed out 1. What's 10 minus 1? Well, that's 9. OK so we went 10, 9. Next, we were printing out 2. 10 minus 2 is 8. So we're printing out 8. So on and so forth, all the way down to 9. What's 10 minus 9? Well that's 1, and so at that point we could say 10 through 1, Happy New Year. Let's give that a try. I'll run Python countdown.py, and now we get what we want 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, Happy New Year. So we've counted down from 10. There are other ways of doing this. I see someone solving this with a while loop. I see someone that's adding some additional arguments to range. There are definitely, as always, multiple ways that you could have used to solve this problem. This is just one way that you can do, using subtraction to, instead of counting from 0 up to 9, counting from 10 all the way down to 1. And so this is some pretty useful logic that we've managed to now figure out. We've managed to figure out some code that can count down from 10 all the way down to 1. And in fact, this type of logic we might want to use multiple times. Imagine that maybe we're writing a program that can count down to the New Year in multiple different time zones. So first on the East Coast of the United States, we're counting down saying Happy New Year, then on the West Coast we want to do a countdown and say Happy New Year. This code that we've written, that counts down from 10, is pretty useful. Maybe I would like to be able to use that code multiple times. And so oftentimes, what you'll see in programming is that when there's some code, when there's some logic that you might want to reuse, we can package that code inside of something called a function. And we've seen examples of functions already. We've seen the print function that prints out text to the terminal. We've seen the input function that asks for input from the user. But it turns out that we can create our own functions that we can then use again in a program. We can use that code multiple times. So I'm going to show you now how you could create a function. And so here's what I'm going to do. Let me get rid of the print Happy New Year right now. I'm just focused on how do you write a function that counts down, and here's how you do it. Up at the very top, I'm going to say def countdown. d-e-f is short for define. And it's the keyword we use when we want to define or create a new function. So we're going to define a function called countdown. We're going to include parentheses to indicate that it is a function. Inputs to that function can go in those parentheses. We'll see that in a moment. For now, we'll leave it empty. And now anything inside the function needs to be indented. So I'm going to indent both of these lines inside the function. What you'll notice now is that this print is actually indented twice. It's indented inside the function, and then it's indented again inside of the for loop. But I now have a function called countdown that can loop 10 times and give me a countdown that's counting down from 10 all the way down to 1. And now that I'm introducing functions, I'm going to add one more function actually that's called my main function. In some programming languages, this is required. In Python, it's not. But my main function is just going to be the function that I want to run when I start running my program. So what do I want to happen when I start running my program? Well, I'm going to define my main function, again with that def keyword, to define a new function. And what I'm going to do in my main function is say go ahead and count down, and then print("Happy New Year!"). So my main function is running, otherwise known as calling, two functions. We're first calling the countdown function, which is the function that I wrote that's going to count down from 10. And then we're going to print Happy New Year. And now at the very bottom, after I've defined my functions, let's just run my main function by typing main and then parentheses to actually run that function. So this is now more sophisticated than the code we've seen so far. So we'll go through it again. I've defined two functions. This countdown function is defined with this def keyword to run that code that counts down, much as we've seen before. And then the main function is just going to run the countdown function and then print Happy New Year. And so now, I can run this countdown program. And right now, it does the same thing. I've organized it a little better, I would argue. But the effect is the same. It's going to be 10 all the way down to 1. And then at the very end it says Happy New Year. But the nice thing about functions is that functions have the ability to take input. And that's one of the great advantages of functions. They don't have to do the same thing every time. They can take input and do different things based on that input. For example, the print function takes input, which is this string. And we've been able to make that print function say different things. We made it say hello. We made it say positive or negative. We made it say the sum of a whole bunch of numbers. Now, we've made it say Happy New Year, just based on what that input is. And my countdown function, it can take some input as well. So I'm going to make it take some input. You can give that input a name. I'll call the input n. And now I'm going to want my countdown function not to count down from 10 every time, but to count down from whatever n is. And n might be 10 but it might be some other number instead. And so what I'm going to do is, instead of saying 10 everywhere in my function, I'm going to replace all of my 10's with just this number n. So now, my countdown function is a little bit more flexible. It's a little more adaptable to whatever I want to count down from. I could say countdown(10), providing 10 as the input to the function. And that would give me the same thing I've seen before, counting down from 10, saying Happy New Year. But all I need to do to change the way the function behaves is change the input to the function. I don't have to change the countdown function at all. If I instead want to count down from 5, I just change the 10 to a 5. And now, 5 is the input to my countdown function. And now, we see a 5, 4, 3, 2, 1 and then Happy New Year. And so this now shows some of the flexibility we get with functions, functions that can take input and do something with that input. In this case, my countdown function is deciding what numbers to print, based on whatever the input to that function is. So a lot of new syntax, a little bit more complex than the programs we've see. I'll pause for questions. [? Feddy ?] is asking why do I need main at the very end? The reason why is because defining our functions doesn't actually run those functions. It's just saying this is what the main function is, and this is what the countdown function is, but neither function is going to run yet. If I didn't have this main line at the very end, when I run my countdown program, nothing will happen. I've defined the functions, but I haven't actually run those functions yet. So I need to at the end say main, to say I would like to actually now run this main function, and that is going to have the effect of giving me the countdown. [? Uliana ?] is asking, do I need to define main? Yes, you do, and I did so on line 1 here, again with this def keyword. Anytime I want to define a new function, I use the def keyword, followed by the name of the function. In the parentheses go any inputs to that function, if there are inputs, but they're not necessary. And then the colon, as we saw with conditions and as we saw with loops introduces the block of code that I would like inside of that function. You could have named these functions anything else. I see a couple of people asking about that. They didn't need to be given these names, specifically. Oh I see someone asking, how do you add comments to your program? And so a comment is just some English text, some description of what it is your program is doing. The computer, the program that's running your Python program, it doesn't really care about what those comments actually are. It's just going to be a way for you, the person writing the code, and someone reading the code to be able to better understand it. To add a comment to your code, you use the hash mark, so the hash or the pound symbol. And then you can just write some English text, like count down to the new year. Oftentimes, you use comments every couple of lines just to describe what it is that your code is doing. Again, the computer doesn't care about the comments. It's just going to ignore them But it's good style, good practice for you. And it makes it easier to read someone else's code, if you can read their code and get a sense for what it is that their code is doing. All right. So right now, our program is doing the same thing every time. Every time we run it, it's going to count down from 5 and then say Happy New Year, for example. Sometimes, and we'll see an example of this in just a moment, we might want our programs to have a little bit of randomness, to not always do the same thing every time but to sometimes do one thing and to sometimes do something else. And so I would like maybe now, just for fun, to surprise myself. I don't want always come down from 5, maybe sometimes count them from 5 maybe sometimes come down from 10, maybe sometimes come down from a different number. Let's add, just for fun, a little bit of randomness into this program. Now, how do I get a random number in Python? Well, it turns out Python has a whole bunch of built-in modules, or libraries, things that I can use inside of my code that have already been written, that just give me easy access to some functionality that I might want as a Python programmer. And it turns out that one of those modules is called the random module. And so how do I use the random module? Well, what I need to do at the top, and we haven't seen anything like this yet, is import random. Random is an already existing module in Python. You could write your own module if you wanted to, but it already exists. And I would just like to import it, meaning include it inside of my program so that I have access to that random module. And now, how do I use the random module? And I would only know this by looking up the documentation, in other words, the instructions for how to use the random module. But there is a function called random.randint that gets me a random integer. And lets say I want a random integer between 5 and 15. So it's going to give me a random number between 5 and 15 using this random.randint function. And I'm going to save that random number inside of a variable. I'll call it number. And so now, I have a variable that is some random number between 5 and 15. And now instead of counting down from 5, let me count down from whatever that number is. Whatever that number is, that's what I want to count down from. And so I don't know yet before I run the program what it's going to be. The program is going to surprise me by picking a random number and then counting down to that random number. So let's give it a try, python countdown.py. This time, it counted down from 5. So it just so happened to pick a random number between 5 and 15. It picked 5. But if I try running it again, this time it counted down from 8. It counted down 8, 7, 6, 5, 4, 3, 2, 1, Happy New Year. I'll try one more time, just for fun with 8 again. Sometimes you get to same number. I try it again, this time it's 5 again. So every time I try it, I might get different values. This time it counted down from 15, and so this allows myself to have a little bit of randomness in the program as well. So this is probably the most complex program we've now written so far in today's seminar. I'll pause here for questions about functions, about imports, about variables, random numbers. Multi-line comments? We haven't seen any multi-line comments here. But you can do so by using three quotation marks. It's a little bit strange. But if you have three quotation marks, you can have a multi-line comment here, and that allows you to span multiple lines. Can functions return values in Python? Yes, they can. We'll see some examples of that a little bit later today, in fact. We haven't yet seen examples of that with our own functions. But for example, the randint function is returning a value. It's giving you back a number. And we are then storing that value inside of this variable number. Adam is asking how do I memorize all these libraries? In short, you don't. I definitely don't have all the libraries memorized. I know a couple of them. I know how to use random numbers because I use those kind of often, and a couple of others that I use pretty frequently. Ultimately, if you want to know how to use a library, you can look to the documentation, the instructions for how to use that library. And Python has a pretty extensive documentation that explains exactly how each of those libraries works. So you can use that as a reference material for knowing how to do it. All right. So let's go ahead and try another exercise, an opportunity for you to try something out for a little bit and get some practice with writing some Python code of your own. You'll get some practice with loops and a little bit of practice with some random number generation, too. And so it'll be a little more complex than the last exercise I had you work on. But we're going to have you build a game. And this is going to be a guessing game. What I want you to do is write a program that picks a pseudorandom number, or a random number between 1 and 10, and then give the user three chances to guess the number, each time printing some information, letting the user know whether their guess was correct or not. So your computer is going to pick a random number between 1 and 10, and the user gets three chances to guess the number. And so if the number is 2 for example, you might guess 8. And it tells you you're wrong. And maybe now you guess 4, and you get it wrong. But then if you guess 2, well, then you get it right. And the computer will let you know. If you want a little bit of an extra challenge, maybe give the user a hint. Let them know if their number is a little too big or a little too small, for example. But start by just maybe just letting the user know whether their task was correct or not. You'll probably find that you might want to use it this random.randit function to generate a random number, which you can access by importing random at the top of your program. And maybe you want some kind of loop to let the user guess multiple times as to what that number is. So we'll give you a little more time to work on this. We'll give you maybe 15 minutes to try and write this guessing game for yourself. If you have questions, again, about how to go about building this game or if you get stuck at a particular point in your code, ask a question on the ed discussion forum where you can post the code you've written so far. And again, we have a great team of mentors who were here to help answer your questions and help you out as you go about trying to write this program, too. Valerie, again, has pasted the link to ed in the chat. And in about 15 minutes, we'll reconvene and we'll talk about how it is that you could have built this guessing game program. All right welcome back, everyone. Definitely OK if you didn't manage to finish that entirely. But hopefully, you were able to make at least a little bit of progress. What I thought we'd do now is see if anyone wants to offer their approach to the problem. How did you try to solve the problem? What sorts of programming ideas or constructs were you using? Let's see. Let's go to [? Rami. ?] If you'd like to offer your idea. AUDIENCE: Yeah, hello. As first I used two f, two main functions, main and then as a game. For main at first, I chose a random number between 1 and 100. And I gave a variable named chance and number 3. Then I do a while loop. What are the chances bigger than 0, ask the player to give a input. This is a number. Then if this number is bigger than the number that I chose, then it says if that number is bigger. That's the hint. If it's smaller, it says the number is smaller. And if equal, it says the number is correct. And the other function it goes another game. Here I ask the player if you want to play another game, they put yes, y. If not, they put no. No, it breaks the loop. Yes, it recalls the main function another time. BRIAN YU: All right very nice. So you even add an additional feature that we didn't require, the ability to play again, which maybe is a good feature to add. And this is going to be true of a lot of the programs you might write. You might write a program and realize down the line that maybe you want to add another feature to that program, and you can keep building on top of what you've already built. But I like these big ideas, using a random.randit to try and generate a random number, and then using some kind of a loop to repeatedly prompt the user to give them a certain number of chances to try and get the right answer. So I'll show you one way you could have approached the problem. Again, there were many ways you could have done it and I saw a lot of different ways in the ed discussion forum. So definitely, OK if you didn't do it exactly the way that I did, but here's one way you could have approached it. I know I'm going to need the random module. And I'll go ahead and define a main function and run my main function at the bottom. I'll start to do this more often in the programs, just as the programs get more complex. It can be nice to have a main function to keep things organized. But what I'll do first is give myself a random number, which I can get by via random.randit, and then 1 to 10. So this is going to give me a random number between 1 and 10, which is probably a good place to start. And now that my program has picked a random number, I would like to give my user three opportunities to guess that number, to be able to make some sort of a guess. And I see Hussein's using a for loop for i in range 3. And I'm going to do exactly the same thing, for i in range 3 to repeat some code three times. And this is going to be 3 different guesses that the user is going to have to make a guess. And let's go ahead and get a guess from the user. So I will say, guess is going to be an integer that comes in from the input. We're going to ask the user to type in a guess. The function does not have to be called main, but it is a convention to call your primary function that you're going to run when you run the program the main function. So I'm following that convention here, but you could name it something else. So I'm asking the user for a guess. It's going to be an integer. And now let's ask ourselves some conditions. You might remember from conditions.py we wrote before, him were checking is x greater than 0? Is x less than 0? Otherwise, what do we do. We're going to do the same thing here. But instead of comparing against 0, let's compare it against the number, whatever the random number was that was generated. And so if the guess is greater than the number that was picked by the computer, let's print out the guess is too large, or you could print some other message. elif, meaning otherwise, if the guess is less than the number, print guess is too small. And then else, if the number is not too big or too small, it must be right. I saw some people saying elif guess equals equals number. We haven't seen this yet, but equals equals, with two equal signs, is how you check if two things are equal to each other. You could do that, but I'm also just going to assume that if the number is not too big and the number is not too small, we must have the right number. So go ahead and print out correct. And so now, I could try running the program. I don't know what number it's going to pick, but let's go ahead and try running Python guess.py. Let's make a guess. Let's guess 3. Guess is too small. Let's guess something bigger. We'll guess 7. Guess is too large. We'll guess in between. Let's try 5. Guess it's too small. So it was 6. I didn't get it in three tries, so it stopped me. But let's try again. Maybe I'll get this done. Let's guess 5. Guess is too small. Let's guess 8. Guess is too large. And so it's got to be 6 or 7. I've got a 50/50 shot at this. Let's guess 7. OK, it was too large again. So you could continue to play this game. Maybe you'll have better luck than I do at trying to guess what the right number is. And maybe a slight improvement you could make, which I saw a couple people make, is that once you get the answer right, you probably don't want to let the user keep guessing. And so here, we have a couple options. We could break out of the loop. We could even just say return, say the program's over. We don't need to keep running the main function. Once you're correct, let's just exit the program altogether. Return is how you get out of a function. So when we're done inside of the main function, we can use return to get out of it. Can we pick a number ourselves instead of having the program pick a random one, I see someone asking. Yes you could. Right here on line 4 is where the random number is selected. Right now, we're setting the number equal to some random number between 1 and 10. I could cheat, of course, and just say, let's make the number 4. And now I know exactly what it's going to be. I'll just guess 4 and correct, I got it right. But maybe that's not such a fun game, and it will be a little more fun if the number is indeed a random number. So again, multiple ways you could have approached that problem. This was just one. I've seen a couple of people ask now about return values of functions. So I thought before we move on, I'd do an example of that. So we've been doing a little bit of work with arithmetic earlier. Let me create a new file called-- well, actually, yeah, let me create a new file called maximum.py. So let's imagine I want two numbers, and I want to figure out which one is bigger. So I want to be able to take two numbers and figure out which one is bigger, probably something not too hard for a human to do. But it turns out that quite often we want to, in a computer program, look at two numbers and figure out which of those numbers is bigger. It turns out Python actually has a built in function to do this. We're not going to use that for now, just to demonstrate how you could write this on your own. But let me give myself a main function and get two integers as input. So I'm going to get a value x, and I'm going to get a value y. These are lines we've seen before several times now. And now, what I'd like to do is define a new function that is going to calculate the maximum of two numbers. So let me give myself a new function. I'm going to call it maximum. And it's going to take as input two values. And I can give those values names. I could call them x and y, for consistency. But you can also give them different names. Every function gets its own set of variables. So I could call these a and b, for example, to make clear that this is a different set of names inside of this function. And now, how do I find the maximum value between a and b? Well, if a is greater than b, then the maximum value is just a. And so I would like for this function to produce as output whatever a is. And for a function to produce something as output, this is what return means, in answer to Abdullah's question in the chat right now. Return means exit from a function. And return can also optionally leave a function and also have some output from that function, some data that comes back from the function that can be used by whoever is running that function. So we'll go ahead and return a. And else, otherwise, if a was not the maximum, I can just return b. It's possible that the two were the same, but if the two were the same it doesn't matter which one we return. So we can just return the variable b. And so I now have a function that takes two numbers and calculates whichever the maximum is. And so now, let me say that z, a new variable, is going to be the maximum of whatever x and y are. I'm passing in two inputs now to the maximum function, x and y. And whatever the maximum's return value is, whatever output comes back from the maximum function, that's going to be stored inside of this variable z. And so now, I could print out the maximum is and then z. I need to be sure to call my main function at the very end. But let's now try this out. I'm going to try python maximum, let me try typing 2 and 8. And the maximum of those two numbers is 8. And I did that by writing a function that can calculate maximums that I've used once. But in a more complex program, you can imagine that I would use this function multiple times. And I'm now using that ability to calculate the maximum of two different values. I see a question. How does Python know that these are integers that we're dealing with? Python knows that it's an integer in particular because I converted the value to an integer here, on lines 2 and 3. Whatever the user typed in, I converted it into an integer. And we're saving it inside of these variables. Dawn is asking how is x different from a here? Every function is allowed to have its own set of variables with its own names that are not accessible by other functions. And so the names could overlap. But I'm giving them different names to make clear that they don't need to be the same. That maximum is allowed to have inputs a and b, and the connection here is that inside the main function, where I have these x and y variables, I'm using x and y as the inputs to the maximum function. And maximum is just going to call those values a and b. So every function gets its own set of variables that are not accessible by the other variables. Why did I need to write main at the bottom but I didn't need to write maximum at the bottom, someone's asking. The reason why is that the main function is where my program begins. And while the main function is running, the main function is going to call or run that maximum function. And so I didn't need to say maximum down here. It's possible to have one function call another function, and then that function can call another function as well. So you can have functions that are running other functions, too. I know that functions can be a little bit confusing. I'm going to do one more example of functions, and maybe even two more examples of functions before your next exercise, just so you can get a sense for how it is that these work. One thing that you might want to do, imagine you're playing a game with three players and every player scores a certain number of points. And what I'd like to do is, I'm doing a little bit of very simplistic computer graphics, print out like a bar chart of how many points each player got in the game. So let me do that inside of a new file. I'm going to call this file scores.py. And what I'm going to do inside my main function is first ask for three scores. Score 1 is going to get an integer input. Type in Score 1. score2 is going to be an integer input. Type in another score. And then score3 is going to be an integer input, Score 3. So I'm asking the user to type in three scores. And now, what I'd like to do is print out a horizontal bar chart of those scores. And what I'm going to use is a hash mark, a hash symbol, for each bar, for each piece of that bar. So if score 1 is like 5, then I want to print five hash marks, for example. So how do I do that? If score1 is 5, how do I print five hash marks? Well, I probably want some kind of a loop. So I could say for i in range score1, let's go ahead and print out a hash mark. And for now, I'm actually going to ignore score2 and score3. I'm going to put a hash symbol in front of each to make them a comment. Oftentimes, if I don't want to deal with certain lines of code yet, I can put a comment in front of them to say, let's not run these lines of code yet. I'll add them into the program a little bit later. So right now, all the main function is going to do is it's going to ask for one score and then print a hash mark. So let's try this. I need to be sure to run my main function here at the bottom. But let's run python scores.py. I type in a number. Let's type in 5, for example. And all right, I got 5 hash marks. But I wanted these to be like horizontal, like hash marks in a row instead of hash marks stacked on top of each other. So what did I do wrong? What mistake did I make? In fact it's a mistake I made a little bit earlier today, too. Yeah, the new lines. Python is automatically adding a new line at the end of every print statement. It's automatically creating a new line for me. And that's not really what I want. It turns out there's a way to override what comes at the end of the print, and I'll show you that now. It's new. We haven't seen this yet. But after the string, I'm going to add a comma. And then I'm going to say end equals two quotation marks, two quotation marks just being an empty string, no characters. And what that's saying is that print the hash mark, but after you print the hash mark, don't print a new line, just print nothing. Nothing goes after that hash mark. And so now if I run python scores.py, I type in a number, like the number 5, and now I see five hash marks printed one after the other. And that's what I want. They're all in one row. Now, one slightly annoying thing is that usually this prompt appears on a brand new line for me. This time, the prompt is like stuck on the same line. And the reason is I didn't print a new line at the end of the program, because I overrode that. I said don't print a new line, just print nothing. And so the kind of tricky way to fix this is, at the very end, I can just say print with nothing in it. And that won't print any new characters. But because it is a print, it's going to print a new line at the end of it. And so that will give me a clean new line at the very end. So let me try this program again. I type in the number 5. And I do, in fact, see five bars, which is exactly what I wanted. So that's now one score. Let's do it for the other two. So I'm going to uncomment these lines for score2 and score3. And now, I really want to do the same thing, just printing out that bar chart. So I'm going to copy and paste it, and do the same thing. But instead of score1, let's print it out for score3, and then we'll do it one more time for score3. And so I now have the same logic to print out a bar for score1, for score2, and for score3. And now, I have the ability to print a chart. I can print out scores. Maybe this person got 2 points and 3 points and 5 points. And I get a bar chart with two hashes, eight hashes, and five hashes, which is exactly what I wanted to see. Now, there's something not great about the way that I've written this code, something maybe not the best design. Can anyone point out for me maybe what mistake I've made, what I could improve here? Yeah, there's a lot of repetition. I see a lot of people suggesting that I've repeated myself a lot. Especially here with these lines that are printing out all these hash marks, there's a lot of repetition. And repetition is another place where it can be very helpful to have a function, a function that organizes some of the common code, so that I can use that code multiple times. So let me give myself a function. I'm going to call this function print_scores. And it's going to accept as input the number n, which is going to represent how many hash marks should I print. Now, what is the print scores function going to do? Well, we're going to loop n times, and I can do that for i in range n, as we've seen before. I'm going to print a hash mark with nothing at the end. We've seen that before too. And at the very end, we're going to print a new line. So the exact same logic, I'm just using n instead of score1, score2, or score3. And we're going to organize all of that into this function called print scores. And so now, instead of these nine lines where I'm repeating the same logic over and over again, I can simplify a little bit. I can just say print_scores(score1), and then do the same thing for score2, and then do the same thing for score3. I've simplified and organize my code. I've cut down on the number of lines that I need inside of my program, to take the common code, the code for printing out these bars, and organizing them into a function. And so now, I can run it. It'll do the exact same thing, no different. But I'd argue my code is a little more organized. And you could maybe organize it even more, reduce some repetition with some loops, someone is suggesting. But I really wanted to just point out here the value of having a function, not just for input and output, but also for organizing code into a place where you can reuse. And so functions are very good for code reuse if I want to use some piece of code multiple times. All right, one last thing that I want to show you before I give you one final exercise to work on today is how you might organize loops together in a sequence. And so to introduce this, I'm going to create a new file. I'm going to call it multiplication.py. And I'm going to use this to be a program that's going to print out some multiplication facts. You might have learned your times tables in grade school, where you learn what 2 times 3, and 2 times 4, and 2 times 5, and you learn all your multiplication facts. Let's write a program that's going to print out some of these multiplication facts. And so I'm going to define a main function that I'm going to call down below. And we'll add some code here soon. But now, before I even get to that, I'm going to write a new function called multiply that's going to print out multiplication facts for two numbers, for numbers x and y. And how do I print out that? Well, I first need to calculate, what do I get when I multiply the two numbers together? So I'm going to create a new variable called product, which is just going to be x times y. And now, I'm going to print out that multiplication fact. I'm going to print out x multiplied by y is going to be equal to the product. So this is a rather long print statement that's got a bunch of commas, because I'm printing out multiple things. But I'm printing the variable x comma, the string that is just going to be the asterisk for multiplication, that's in quotation marks, then a comma. I want to print something else, I want to print the number y, the variable y, then a comma. Then in quotation marks equal sign to print out an equal sign, and then after a comma, print out whatever the product is. I'm not returning anything, I just want to print something out to the terminal window. And now I can use that multiply function inside of my main function. I can say, go ahead and multiply 2 and 3, and then multiply 2 and 4, for example. And so now if I do python multiplication, I see these multiplication facts. 2 times 3 is 6. 2 times 4 is 8. Each time, this multiply function is calculating what I get when I multiply those numbers. And it's printing out that multiplication fact for me so that I can see what it is. And I'm running it two times on 2 and 3, and 2 and 4. But now what I'd like to do is do all of the multiplication facts. I want to print out all the multiplication facts for like 0 up through 9, maybe. And how would I do that? Well, I could do this inside of a loop. I could say let's do for i in range 10, and then let's do multiply, well what am I going to multiply? Well we can start with maybe 2. Let's multiply 2 times whatever i is, for example. And so it's going to be 2 times 0 and then 2 times 1 and then 2 times 2, all the way up 10 times. And so now it's inside of a loop, and I get 2 times 0 is 0, 2 times 1 is 2, and I get very nicely printed, very quickly computed by the computer, all of the multiplication facts for the number 2. 2 times everything I now have here. But what if I want to not just do 2 times everything, but 3 times everything and 4 times everything and 5 times everything? How can I add some logic to do that as well? Any ideas? Yeah, Nathan is suggesting I use two loops instead of just one loop. And so we're introducing the concept of what people in the chat are correctly calling a nested loop, where I have one loop inside of another loop. And that's going to cause the loop to happen again and again for every time the outer loop is called. And so you can get loops upon loops happening within each other. And so in here, I'm going to say for j in range 10. And then I'm going to indent one more time, multiply i and j. So what's going to happen? What's the logic for these nested loops? Well, we're going to start with this outer loop, i is going to be 0. And then we're going to run this inner loop. j is going to be 0. So we're going to get 0 time 0, 0 times 1, 0 times 2, so on and so forth, all the way up to 0 times 9, because this loop is happening 10 times. And then we go back to this loop again. i is now going to be 1. And so now we're going to run this inner loop again. We're going to do 1 times 0, 1 times 1, 1 times 2, so on and so forth up to 1 times 9. Then that loop finishes, and we go back to the outer loop again. Now, i is 2, and so we're going to do 2 times 0, 2 times 1, 2 times 2, so on and so forth. And we're going to be 3 and again and again. Let's see what that looks like. I run python multiplication.py. And we'll see exactly what we wanted, which is we got all of our 0 multiplication facts printed out first, then all of our 1's, then 2's, and so on, and so forth, all the way up to 9 times 9. And we did that via nested loops, because we wanted one loop to run multiple times. If you want a loop to happen multiple times, you can put that loop inside of another loop. This is a little tricky at first but in a moment you'll get some practice with just this. [INAUDIBLE] is asking how do you make it start at 1? It turns out range can take multiple arguments. By default range starts at 0. If I wanted it to start at 1, I would say 1 comma 10, and 1 comma 10. And that will have it instead of starting at 0, it's going to start with 1 times 1, 1 times 2, all the way up to 1 times 9, and so forth. [? Fang ?] is asking where do i and j get defined as integers? Because I have a range in my for loop, it's ranging over integer values 1 and 10 here, i is automatically going to be an integer. Python just knows that if I'm looping over a range of integers, then i is going to be an integer each time. If you're asking for these exact lines of code, all of this code is available on the CS50x website under [? this ?] seminar, so you can find all of the code that I've written here today as you're following along too. All right. So now that we have the ability to have nested loops, and we have the ability to be a little bit clever about how we're printing things-- you'll recall in scores.py, we used this n equals empty string to be able to print something without going onto a new line-- I thought we'd try and put these ideas together in one last in-class exercise for today. And then we'll talk about it afterwards before we wrap up. And here is your last exercise. It's inspired by the game Mario, where if you've played the game Super Mario, you might have seen this grid of bricks that's somewhere in one of the levels. And so what you're going to do is you're going to create a grid. You're going to write a program that asks the user to first type in a number, a number n, and then you're going to print an n by n grid of hash marks. So if the user types in 5, then you're going to get this-- a 5 by 5 grid of hashes with five rows and five columns. If the user typed in 4, it would be a 4 by 4 grid. If the user typed in 10 you'd, print a 10 by 10 grid. So this is what you're now going to try to do. It's not too many lines of code, but it is a little bit tricky. You're going to have to maybe use the end trick at the end of print that we talked about. You might have to use some nested loops that we've talked about. So we'll give you about 10 minutes to try and write this. If you have questions about any of it, ask questions in the ed discussion forum as I see many of you have been already. The mentors are here to help you answer your questions as you try and figure out how to write this program. But in about 10 minutes, we'll reconvene and we'll talk about how it is that you might have created this grid. All right, welcome back, everyone. So would anyone like to share how they approached building this grid? What did your code look like? What kind of techniques did you use? Let's go to Michael, go ahead. AUDIENCE: So basically, what I did is I defined the main function. And I declared height as an int input, and then height, so that you could input height. And then I called a function which is draw. And I added height as the argument. And then below that, I defined draw, h as the argument. And I put for i in range h, and then inside of that I nested for j in range h, and then inside of that print and then one hash and then comma end, and then nothing. And then I've got print with nothing, so that I just print in a new line. And then I just called main. BRIAN YU: All right, very nice. So you're using for i in range, for j in range, using some kind of nested loop, using some printing, you even added an additional function, which you didn't have to do but it was an option to create an additional function if you wanted to. [? Nashihad, ?] did you have another approach? What did you do? AUDIENCE: I kind of did the same thing. BRIAN YU: All right, so let's take a look at that approach. Using the approach of using nested loops and how we might have gone about creating this grid. And so the first thing we might want to do is ask the user for how tall the pyramid should be or how tall the grid should be, getting ahead of myself. We'll ask for input, for maybe the height of the grid. And now we want some kind of nested loop, because we want to repeat row after row. And each row is going to be repeated hash after hash. So for i in range height, and for j in range height, to create this nested loop. I'm going to print a hash mark. And I don't want anything at the end of it. So end equals empty string. So let's give this a try, python grid.py. Let's type in 3 as a height, for example. And let's press Return. OK, that doesn't look much like a grid. So it seems like I'm forgetting something. Can anyone help me out with what I might be forgetting or what I might be missing in the program? Yeah, it's the new line, very good to those people who are suggesting it in the chat. I don't want new lines after every hash mark. But I do want it at the end of the row, before I go to the start of the next row inside of this loop. And so at the very end, I can just say print to say go ahead and give me a new line. That way I move on to the next line before I start the next row. So now if I say 5, I do in fact get a 5 by 5 grid. If I instead say 7, I now get a 7 by 7 grid. So it gives me a little bit of an ability to customize what I want that grid to look like. All right. So as we wrap up today's session in Python, we do have some homework for you, if you want to try your hand at writing some more Python programs yourself. They're taken from CS50's problem sets. And so your first homework assignment, similar to what we just did, also inspired by the game Mario. At the end of one of the levels, you might recall this pyramid. So your first homework assignment is to write a program in Python to build this pyramid. You're going to ask the user for a number, for how tall the pyramid should be. And you're going to print a pyramid of that height. Notice that this is not a 5 by 5 grid, but a 5 by 5 pyramid, right aligned with everything lined up on the right hand side. But the first row has just one hash, the second row has two, the third one has three, so on, and so forth. And so that's your first homework problem. And your second homework problem is a problem from CS50 called Cash. You're going to write a program that asks the user to type in some amount of money. And you're going to print out how many US coins would be required to make change for that amount of money. So in the United States, the coins have values of $0.25 $0.10 $0.05, and $0.01. So if I told you to make change for let's say $0.31, for example, you would use one quarter for $0.25, you would use one nickel for five additional cents, and then one penny. And you would figure out what is the fewest number of coins required to make that amount of change, and write a Python program to figure that out. And as a hint, as you're trying to figure out which coin to use next, you usually want to use the biggest one. If I want to make change for $0.31, I want to start with a $0.25 coin rather than use like 31 one cent coins, which might take up quite a number of coins. And so you're welcome to work on that. You're welcome to continue to post on the ed discussion forum if you have questions about any of these problems as you go about trying to work on it. And we've only just scratched the surface of Python, really. If you're interested in learning more about Python and Python programming, I'd encourage you to take CS50 on edX, if you haven't already, where you'll learn even more about programming in Python and other programming languages. And if you choose to do that, that's where you'll submit these problems, Mario and Cash, that you'll be working on here. And I'd also note there are other seminars that are coming up later this month and next. There's one on SQL that I will be leading, one on Linux that Kareem will be leading, and one on game development that Colton will be leading. So if any of these topics are topics that you're interested in exploring, we hope to see you at some of those, too. But before we wrap up, we would love to get your feedback on today's session. And so I'm going to go ahead and put a link into the chat. And I'll share my screen as well with it. But if you could go to this URL, cs50.ly/feedback, this will just help us to improve future sessions like these. This, again, is the very first of these types of sessions that we're doing. And we would love to hear what you think of that. So it shouldn't take too long to fill out. But if you could, take a moment to go ahead and fill out that feedback form at this URL here, cs50.ly/feedback. And once you filled out that feedback form, that wraps up today's session on introducing Python. Thank you all so much for being here. Thank you to the mentors for all of your help with students today. And we hope to see you at some seminars in the future. Thanks, everyone.