Every coder’s first program is “Hello World!” and in python it’s done using the print() function to create a print statement.
After the print command are a set of parentheses ( ). Inside these parentheses is what should be printed to the screen. Using parentheses to pass information to a function is standard practice in math, and computer languages.
Math students learn to use parentheses evaluating expressions like this:
sin and cos are functions. Data passed to these functions is inside the parenthesis. What is different in our case is that the information being passed is text.
Notice that there are double quotes around the text to be printed. If a print statement has quotes around text, the computer will print it out just as it is written.
We can also have the print function output the result of a calculation.
Attempting to print text without double quotes will cause a syntax error in Python and you will see a squiggle in your IDE.
Printing Multiple Items
A print statement can output multiple things at once, each item separated by a comma. For example this code will print out Your new score is 1040
If we type this
the code will output Your new score is 1030 + 10
Escape Codes
Because we use “double quotes” at the start and end of a string of characters, that makes it hard to also use them as output. We can use an escape code \ positioned carefully so that we can use quotation marks in our output.
You can see the issue here:
We solve it by using \ right before the item in our print statement that we want the computer to not interpret as python code.
But now what if we actually want a \ in our print statement?
This might not work…
But this will…
Here are some important escape codes to know:
In this class you will find that \n to create a new line comes in handy.
Try them out in this interactive code snippet: