Printing to the screen
Contents
Printing to the screen#
Before we go any further, it’s convenient to have a more powerful way of displaying information from Python.
So far, you have been getting output to the screen by writing the name of a variable at the end of a cell. This is often not flexible enough, so Python provides a print
function for displaying output to the screen at any point. This allows you to display multiple values from one cell.
a = 3
b = 4
print(a + b)
print(a - b)
7
-1
It is considered bad practice to display values on the screen without any context, as in the above code cell. In order to print out a specific bit of text, we have to enclose the text in "
marks, like so:
print("Hello there!")
Hello there!
The print
function is unlike any you have seen yet: it can take any number of arguments, separated by commas, and will display them all on one line (if possible). This allows us to display values along with a description of the value, which is better than just the value by itself!
import numpy as np
print("The value of sin(pi/6) is", np.sin(np.pi/6))
The value of sin(pi/6) is 0.49999999999999994
Notice that the value displayed should be \(0.5\); the difference is due to the rounding errors that occur when we represent decimals on computers using floating point numbers.
Note: especially in Jupyter notebooks, it is usually unnecessary to print
output to the screen. It is annoying for users of your code if you print
large amounts of information or print
values without context. On the other hand, print
can be a very valuable tool for debugging: by displaying suitable messages, you can understand what is going on inside your program. Just be sure to remove the print
statements you added for debugging after you’re done!
We call pieces of text enclosed in "
marks like above strings (meaning letters or characters strung together). You can store strings in variables just like numbers, and manipulate them in many ways using Python. We will learn more about strings throughout future worksheets.
place = "Moria"
"Welcome to " + place + ", enjoy your stay!"
'Welcome to Moria, enjoy your stay!'
Optional: Formatted strings#
In the future, you will often come across places where you want to print multiple values in the same print
statement, perhaps like so:
x = np.pi/6
print("For x =", x,
"the value of sin(x) is", np.sin(x),
"and the value of cos(x) is", np.cos(x))
For x = 0.5235987755982988 the value of sin(x) is 0.49999999999999994 and the value of cos(x) is 0.8660254037844387
It’s a little annoying to keep swapping in and out of strings, so Python provides an alternative called f-strings. These allow you to insert Python expressions into strings using braces ({ }
) like so:
x = np.pi/6
print(f"For x = {x}, the value of sin(x) is {np.sin(x)} and the value of cos(x) is {np.cos(x)}")
For x = 0.5235987755982988, the value of sin(x) is 0.49999999999999994 and the value of cos(x) is 0.8660254037844387
There are lots of options available to customise how the values from expressions are displayed. Perhaps the most commonly used is the “number of decimal places” specifier, which is used like so:
print(f"For x = {x}, the value of sin(x) is {np.sin(x):.1f} and the value of cos(x) is {np.cos(x):.10f}")
For x = 0.5235987755982988, the value of sin(x) is 0.5 and the value of cos(x) is 0.8660254038
You can read :.1f
as “as a 1-decimal-place floating point number”. Further information on formatting strings can be found [here](https://docs.python.org/3/tutorial/inputoutput.html.