In this lecture, we shall be focusing on Python's print statement. As the word ‘print’ may suggest, this a command in Python that, err, prints to the console whatever you fed it as input. The syntax is:
print({argument})
Please take care to note that "print" is all lower-case (Python is case-sensitive). And don't forget the curly brackets! "()"
As a quick side-node, if you see python code on the internet you may occassionaly see print statements without brackets:
print "hello"
print ("hello")
The most likely explanation of this difference is that the print statement used to work differently in Python 2. In Python 3, you need the brackets.
Before moving on, I’d like to make quick note about how I talk about syntax in Python. In the lectures where I chose to talk about syntax I use the format above. With the exception of the “{}” symbols, you should assume that all the the symbols/words are deliberate. In other words if you see a symbol such as comma (“,”), a colon (“:”) or whatever its there because Python needs it to be there!
The exception to this rule is text sandwiched between the “{}” brackets. This is usually 'place-holder' text that you would need to change.
In this particular case you should remove {argument} and replace with whatever you want to print. Lets run through a few examples:
In [5]:
print("hi")
print(4 + 4)
print([1, 2, 3])
In the above case don't worry about what is going on, rather, I just want you to focus on the how we did it. The steps we took were:
When learning programming languages a long-standing tradition has it that the first program you write is "hello world" and who am I to break tradition?
In Python text is handled by something we call a “string”. Strings are an important data-type in Python and later on in this course we shall devote a good deal of time to understanding how they work. But for now, let's just focus on the syntax and how we can print hello word. The Syntax:
"{argument}"
In [6]:
"THIS IS A STRING"
Out[6]:
Yep, thats right, strings have a very simple syntax; to convert normal english text into a Python string all we have to do is surround it in quotation marks (""). Okay we have all the tools we need, now lets give 'hello word' a go!
In [7]:
print("hello World")
In later lectures I’ll go into a lot more detail about error messages. But for now I just want to point out that the computer is ultimately stupid; it cannot figure out what your intentions are and as a result the computer will interpret everything 100% literally and will complain whenever you get anything (even very small things) wrong.
As you learn to program you will see error messages a lot. But, on the brightside error messages usually tell you what is wrong. Thus, a key skill to develop is understanding what various errors mean and how to fix them.
For example:
In [1]:
Print("this is a string!")
In the example above we tried to print a string and we got the following error message:
Why did this happen? Well, Python is complaining here because “Print” and “print” are not the same thing. Remember what I said about the computer being stupid? The computer cannot figure out our intentions nor can it figure out what is meant via contextual cues. Basically, everything we type must be exact and Python is case-sensitive.
Okay, lets look at one more type of error we may encounter:
In [2]:
print("this is a string"
In [3]:
print("this is a string)
In [4]:
print("this is a string"))
So what happened here then? Well in the first case we are missing a closing bracket and in the second example we are missing a quotation mark, and in the third example we have one too many closing brackets. The result is the following errors:
Notice that Python has a little "^" icon pointing out where it thinks the mistake is.
When you see errors like this don’t give up! Most of the time when you see errors like this your mistake was that you missed a comma, or added an extra bracket or something. Basically, you probably just have a typo somewhere in your code.
Basically, try not to get too fustrated when you see errors; its a normal part of the learning process and more often than not its something really simple. So if you are stuck I recommend taking a quick teabreak, a fresh pair of eyes will often spot the mistake a pair of tired eyes couldn't!
For your homework, I want you to print the following phrase into the box below:
MY HUMAN UNDERSTANDS ME.
If you try to print that statement and it doesn't work remember about what I said about error messages; Python needs everything to be 100% correct, not 99% or 99.98% correct. So yeah, if you receive an error message what one did you get? Is it one of the ones I mention above? If so, can you figure out what the fix might be?
In [8]:
# ENTER YOUR CODE BELOW THIS LINE AND HIT THE 'RUN CELL' BUTTON ABOVE.