Today we are going to learn how to print in Python, it is really straightforward.
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
that by parentheses and a double quote. One of these is a double quote, we're going to need two of them.
Then type what you want to print out on the screen. I'm going to print out "Hello World."
Close your double quote, close the parenthesis, and at that point you hit enter,
and the computer will print out exactly what you've got in between those quotes.
Now, how exactly does this work? Right here,
this print is what is called our "function name."
It is really similar to what you've already used before in mathematics. In mathematics you've probably used sine,
or cosine
in this we are using a function called "print."
and just line sine, cosine, we follow it
with parenthesis. Inside those parentheses goes the data are giving the function. We might give sine and cosine pi over four.
Close parenthesis. In this case we're giving the print function a set of text: "Hello World"
and that
is right here, on what we've typed in. Unlike with mathematics, with computers we can easily give functions
several sets of parameters that go inside those parentheses.
And it doesn't even have to be numbers, it can also be text. Or even an entire customer record or
a 3D object that we wanna have displayed on our screen.
Now inside of this IDLE prompt I'm going to hit the up arrow two times.
so that I've got the cursor right on the print line.
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.
And I can go ahead and say
"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
computer knows that its true or not. I can get the computer to say anything.
Now why are these quotes so important? Let's do another example here. I'm going to take the up arrow a couple times,
and I'm going to go ahead and print out
10 + 10 inside of quotes.
Like before, the computer prints out exactly what I had between the quotes. 10+10.
If I were to go up here, do this line again, but this time take out the double quotes
and the number of spaces I have doesn't really matter in this case
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
and then output the result. 10 plus 10 inside of quotes
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.
We can do that a lot more terms.
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
We'll talk about that in a later lesson. But the -5 times 20 happens first.
and I've got the result of my mathematical expression.
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"
when I go ahead and hit enter, the computer gives me an error
"hello world" it says a syntax error: invalid syntax. That may not give me a whole lot to go on that something's wrong
But what the computer is saying, is that it is trying to evaluate "hello world" as a mathematical expression and has no
idea what "hello world" is as a mathematical expression
and says the syntax that expression is wrong.
Which is true, it's not a mathematical expression. There's nothing for it to evaluate and I can't print it out.
As somebody starts to learn how to program sometimes they can get confused what goes inside quotes what doesn't go inside quotes
If it is a mathematical expression then it does not go inside quotes and
what comes out, that will be the answer that expression
If it goes inside quotes that's exactly what's going to print out: what's inside the quotes.
We can actually combine these. I'm going to go up a few lines do this 10 plus 10 line.
the answer to 10 plus 10
If I hit enter it's going to, just like before print out exactly what I had before.
but I can also print multiple things. In this case the key to doing this is right after the double quotes, not inside,
I'm going to hit a comma, and then do 10 plus 10
When I do that and hit enter the computer prints both the first term, which is right here
the comma will translate into a space and it will print out the second term. This is one,
this is two, this is one, and this is two. The whole 10 plus 10 is the second
parameter that is going into the function.
Remember then, you can list several different things and them a print out.
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
is outside the double quotes. You need to have those commas in there though, if I print
three and
four
the computer doesn't know if you want to add them, subtract them, or what you want to do with them.
A common mistake is to do something like this.
In this case I get the syntax error.
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
separates all the different things I want to print out. I need to have that comma
outside the double quotes
In this case it prints out "The answer is,"
then the second comma
counts as what is separating the different things that I'm printing out, and then I finally get my answer.
All right, just to summarize then. What we've got is the print command, this is our function.
All functions are followed by parentheses.
Inside of those parentheses goes the data that we want to have print out.
If we include, inside of those parentheses double quotes,
the computer will print out exactly what we've got between double quotes. If we have multiple things that we want to print out,
we can separate them by commas, and in this case I've got
four different things that are going to be printed out. This is my function name
I've got the parentheses here, this is item number one,
item number two, item number three, item number four, no commas will print out because no commas are inside those parenthesis.
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
output so you don't get those extra spaces, but that is a little beyond scope of this tutorial.