Enough with the pseudocode and keywords that are code but actually don't do anything! After all, you want to learn to program so that your code can do something.
You've already seen Python in action in previous sections of this course. In this lesson, you'll revisit one of the Python functions that you'll use very often: print().
Info: Don't worry about what exactly a function is. It's enough to know that it makes Python do something.
What Does the Print Function Do
The print() function is how you can write anything to your console using Python. You've seen it before, but because it's such an essential and useful tool, it deserves to get some more of your attention.
Throughout this course, you'll use print() to:
- Display messages to yourself and your users
- Show information about your programs
- Inspect your code and find possible errors
You can print anything that has a value in Python. You do so by placing the value you want to see in between the parentheses (()):
print(42)
print("hello world")
The correct way to say this in programming terms is:
I'm passing the value
42as an argument to theprint()function.
You can pass values directly, as you did in the example above, or you can pass a variable that points to a value. You'll learn all about what variables are in the next section.
Print Function Practice Problems
Follow these steps to get familiar with how the print function works:
- Write a script that uses
print()to display some messages to your console. - What happens when you try to print
42with quotation marks? - What happens when you try to print
hello worldwithout the quotation marks? - Can you perform a calculation inside of
print()? - How many different error messages can you manage to get? Are they helpful?
print() is a versatile function that you'll use often in this course. You'll need it when you want your scripts to show you some output---which is very often!
Scripts Vs. Interpreters
You might have noticed that there was no need to use print() to display your output when using the Python interpreter. This is because it's set up to execute line-by-line and show each output automatically.
However, when you are building Python scripts, they'll only give output to the console when you use print().
When you encounter Python code snippets in the course materials moving forward, they'll sometimes be shown without print(), to make them easier readable:
5 + 5 # 10
If you are executing your code with a script, you'll need to pass the code to print() in order to confirm that you get the same output, in this case, 10. If you see a code snippet like the one above, make sure to run it like below in your scripts:
print(5 + 5) # 10
If you execute the code in an interpreter, then you won't have to add print() to see the same output.
Summary: Python Print Function
- The
print()function is a Python function that you can pass any value to that you want to display on your console - It's frequently used to communicate information to users, show the state of your program, and help with debugging your code
- Executed code in the Python console displays the output right away
- Output from running a Python script, requires the use of
print()to view it in the console