• Save
  • Download
  • Clear Output
  • Runtime
  • Run All Cells
  • Difficulty Rating

Loading Runtime

The print() Function

If we ever want to display the value that has been stored inside of a variable we can use something called the "print function" –sometimes also called the "print statement."

To demonstrate this, let's create a variable called power_level and store to it the value 9001. And then we'll use the print() function to display the variable's value.

We do this by writing the variable's name inside of the parentheses of the print statement. Python will try to output anything that is written inside of these parentheses.

So, after running the cell you should see the number 9001 displayed in the output section of the code cell.

This is kind of like opening our labeled storage box and peeking inside to confirm to ourselves that it actually holds what we think it does.

9001

If we try and print out a variable that hasn't been created yet, we'll see an error message that says NameError: name '[variable_name]' is not defined.

In this lesson we haven't created a variable called my_age yet, so let's try and print out it's value to trigger the error message on purpose.

File "<exec>", line 1, in <module> NameError: name 'my_age' is not defined

This error message is a common one because sometimes you forget to create the variable, or you forget to run a code cell, or you just have a typo in how you've spelled out the variable's name, so I want to make sure that you're familiar with it.

The importance of `print()' debugging in Python

The print() function will be very useful in the future. When we're writing programs, the values stored to our variables will often change in expected and intentional ways. When our code doesn't seem to be running right, we can use the print() statement to look at our variable's values to help figure out what might be going wrong. We'll look at some examples of this soon when we start writing more complex code, but we need to cover some other basic concepts first.

Use the print() function to display multiple variables in the same code cell.

In a previous lesson I showed you how if we write a variable name as the last piece of code in a code cell then the variable's value will be automatically displayed in the output section.

And this is fine, but only works if the variable is written as the last line of code in a code cell. This means that this method is a little bit limited. For example, we can't use this method for printing out multiple values in the same code cell. Let me show you.

Here's the same code that we wrote in a previous lesson, where we declared the variable my_age, assigned it the value of 30 and then displayed its value.

30

However, if we try and display two different variables in this same way, for example my_age and also the power_level variable that we just created, you'll notice that only the last one is actually displayed.

9001

If I reverse the order, again, we'll only see the value from the last line of code in the cell.

30

This behavior of automatically displaying the last variable's value is something that is specific to writing code in notebooks. In more traditional forms of Python programming the only way to view a variable's value is to use the print function.

The print() function will always display its contents in the output section of the code cell, even if it's not the last line of code in the code cell.

Let's try it. This time we'll print out my_age and power_level.

900130

Nice, now we can see both –that looks like the result that we were after.

If you want, you can combine these two notebook behaviors by letting the last value be output automatically, but printing out anything that comes before it.

900130

But don't get it mixed up and think that it's either-or. Remember, the variable's value will only be automatically displayed if it's the last line in the code cell. I've probably said that a lot now.

9001

Even though this is a small thing I wanted to take the time to show this to you –because it's something that I've noticed can be confusing to beginners if nobody takes the time to spell it out for you.

I personally like to use the print() as often as I can instead of letting the notebook output values for me, just because I know that it's going to work 100% of the time–even if it is a little bit more typing.