1 00:00:04,400 --> 00:00:08,733 Today we are going to learn how to print in Python, it is really straightforward. 2 00:00:08,733 --> 00:00:17,533 All you need is the print command. And you can just go ahead and type that in at this IDLE prompt right here. I'm typing in print. Follow 3 00:00:17,533 --> 00:00:23,333 that by parentheses and a double quote. One of these is a double quote, we're going to need two of them. 4 00:00:23,333 --> 00:00:29,533 Then type what you want to print out on the screen. I'm going to print out "Hello World." 5 00:00:29,533 --> 00:00:34,233 Close your double quote, close the parenthesis, and at that point you hit enter, 6 00:00:34,233 --> 00:00:38,233 and the computer will print out exactly what you've got in between those quotes. 7 00:00:38,466 --> 00:00:43,066 Now, how exactly does this work? Right here, 8 00:00:43,066 --> 00:00:49,266 this print is what is called our "function name." 9 00:00:49,266 --> 00:00:55,766 It is really similar to what you've already used before in mathematics. In mathematics you've probably used sine, 10 00:00:55,766 --> 00:00:57,932 or cosine 11 00:00:57,933 --> 00:01:02,066 in this we are using a function called "print." 12 00:01:02,066 --> 00:01:05,066 and just line sine, cosine, we follow it 13 00:01:05,066 --> 00:01:16,366 with parenthesis. Inside those parentheses goes the data are giving the function. We might give sine and cosine pi over four. 14 00:01:16,366 --> 00:01:27,166 Close parenthesis. In this case we're giving the print function a set of text: "Hello World" 15 00:01:27,166 --> 00:01:28,899 and that 16 00:01:28,900 --> 00:01:35,466 is right here, on what we've typed in. Unlike with mathematics, with computers we can easily give functions 17 00:01:35,466 --> 00:01:39,566 several sets of parameters that go inside those parentheses. 18 00:01:39,566 --> 00:01:45,399 And it doesn't even have to be numbers, it can also be text. Or even an entire customer record or 19 00:01:45,400 --> 00:01:49,366 a 3D object that we wanna have displayed on our screen. 20 00:01:49,366 --> 00:01:54,232 Now inside of this IDLE prompt I'm going to hit the up arrow two times. 21 00:01:54,233 --> 00:01:57,566 so that I've got the cursor right on the print line. 22 00:01:57,566 --> 00:02:04,366 I'm going to hit enter and it'll copy what I've had before, down here, so I can type again without having to retype the whole thing. 23 00:02:04,366 --> 00:02:11,166 And I can go ahead and say 24 00:02:11,166 --> 00:02:16,999 "Learning Python is great," because if I say that whole of lot of times maybe I'll believe it. It'll go ahead and print that out whether or not the 25 00:02:17,000 --> 00:02:20,900 computer knows that its true or not. I can get the computer to say anything. 26 00:02:20,900 --> 00:02:27,233 Now why are these quotes so important? Let's do another example here. I'm going to take the up arrow a couple times, 27 00:02:27,233 --> 00:02:30,833 and I'm going to go ahead and print out 28 00:02:30,833 --> 00:02:34,599 10 + 10 inside of quotes. 29 00:02:34,600 --> 00:02:39,133 Like before, the computer prints out exactly what I had between the quotes. 10+10. 30 00:02:39,133 --> 00:02:45,233 If I were to go up here, do this line again, but this time take out the double quotes 31 00:02:45,233 --> 00:02:48,566 and the number of spaces I have doesn't really matter in this case 32 00:02:48,566 --> 00:02:58,232 particularly if you are outside the quotes. What's going to happen is that it is going to evaluate that particular item that as a mathematical expression 33 00:02:58,233 --> 00:03:03,066 and then output the result. 10 plus 10 inside of quotes 34 00:03:03,066 --> 00:03:14,066 prints out exactly that, 10+10. 10+10 with no quotes is going to go ahead and evaluate it as a mathematical equation and give you the result. 35 00:03:14,066 --> 00:03:20,899 We can do that a lot more terms. 36 00:03:20,900 --> 00:03:28,633 In this case I've got 10+10 -5 times 20 and it'll go ahead and give me a -80. Order of operations applies 37 00:03:28,633 --> 00:03:33,566 We'll talk about that in a later lesson. But the -5 times 20 happens first. 38 00:03:33,566 --> 00:03:36,532 and I've got the result of my mathematical expression. 39 00:03:36,533 --> 00:03:46,266 Now that's great but if I come up here and I select "hello world" and I take out the quotes for the "hello world" 40 00:03:46,266 --> 00:03:49,499 when I go ahead and hit enter, the computer gives me an error 41 00:03:49,500 --> 00:03:57,433 "hello world" it says a syntax error: invalid syntax. That may not give me a whole lot to go on that something's wrong 42 00:03:57,433 --> 00:04:04,533 But what the computer is saying, is that it is trying to evaluate "hello world" as a mathematical expression and has no 43 00:04:04,533 --> 00:04:08,433 idea what "hello world" is as a mathematical expression 44 00:04:08,433 --> 00:04:11,266 and says the syntax that expression is wrong. 45 00:04:11,266 --> 00:04:17,766 Which is true, it's not a mathematical expression. There's nothing for it to evaluate and I can't print it out. 46 00:04:17,800 --> 00:04:24,800 As somebody starts to learn how to program sometimes they can get confused what goes inside quotes what doesn't go inside quotes 47 00:04:24,800 --> 00:04:30,900 If it is a mathematical expression then it does not go inside quotes and 48 00:04:30,900 --> 00:04:34,033 what comes out, that will be the answer that expression 49 00:04:34,033 --> 00:04:39,933 If it goes inside quotes that's exactly what's going to print out: what's inside the quotes. 50 00:04:39,933 --> 00:04:47,899 We can actually combine these. I'm going to go up a few lines do this 10 plus 10 line. 51 00:04:47,900 --> 00:04:51,266 the answer to 10 plus 10 52 00:04:51,266 --> 00:04:57,066 If I hit enter it's going to, just like before print out exactly what I had before. 53 00:04:57,066 --> 00:05:04,832 but I can also print multiple things. In this case the key to doing this is right after the double quotes, not inside, 54 00:05:04,833 --> 00:05:09,366 I'm going to hit a comma, and then do 10 plus 10 55 00:05:09,366 --> 00:05:17,032 When I do that and hit enter the computer prints both the first term, which is right here 56 00:05:17,033 --> 00:05:22,766 the comma will translate into a space and it will print out the second term. This is one, 57 00:05:22,766 --> 00:05:29,366 this is two, this is one, and this is two. The whole 10 plus 10 is the second 58 00:05:29,366 --> 00:05:31,999 parameter that is going into the function. 59 00:05:32,000 --> 00:05:42,200 Remember then, you can list several different things and them a print out. 60 00:05:42,200 --> 00:05:49,733 In this case I've got five different things, I hit the print, and it prints out those five numbers. The comma doesn't print out if it 61 00:05:49,733 --> 00:05:55,533 is outside the double quotes. You need to have those commas in there though, if I print 62 00:05:55,533 --> 00:06:00,699 three and 63 00:06:00,700 --> 00:06:03,100 four 64 00:06:03,100 --> 00:06:10,533 the computer doesn't know if you want to add them, subtract them, or what you want to do with them. 65 00:06:10,533 --> 00:06:19,033 A common mistake is to do something like this. 66 00:06:19,033 --> 00:06:20,933 In this case I get the syntax error. 67 00:06:20,933 --> 00:06:28,966 I do have a comma, but that comma is inside the double quotes and the computer interprets it as something to a print out, not something that 68 00:06:28,966 --> 00:06:33,432 separates all the different things I want to print out. I need to have that comma 69 00:06:33,433 --> 00:06:35,499 outside the double quotes 70 00:06:35,500 --> 00:06:36,433 71 00:06:36,433 --> 00:06:39,499 In this case it prints out "The answer is," 72 00:06:39,500 --> 00:06:42,166 then the second comma 73 00:06:42,166 --> 00:06:48,166 counts as what is separating the different things that I'm printing out, and then I finally get my answer. 74 00:06:48,166 --> 00:06:53,066 All right, just to summarize then. What we've got is the print command, this is our function. 75 00:06:53,066 --> 00:06:56,099 All functions are followed by parentheses. 76 00:06:56,100 --> 00:07:00,400 Inside of those parentheses goes the data that we want to have print out. 77 00:07:00,400 --> 00:07:07,433 If we include, inside of those parentheses double quotes, 78 00:07:07,433 --> 00:07:16,399 the computer will print out exactly what we've got between double quotes. If we have multiple things that we want to print out, 79 00:07:16,400 --> 00:07:21,733 we can separate them by commas, and in this case I've got 80 00:07:21,733 --> 00:07:25,566 four different things that are going to be printed out. This is my function name 81 00:07:25,566 --> 00:07:30,399 I've got the parentheses here, this is item number one, 82 00:07:30,400 --> 00:07:39,200 item number two, item number three, item number four, no commas will print out because no commas are inside those parenthesis. 83 00:07:39,200 --> 00:07:45,133 and I've got my output. Those commas will always add an extra space in there, later on we'll show you how to format 84 00:07:45,133 --> 00:07:50,033 output so you don't get those extra spaces, but that is a little beyond scope of this tutorial.