Skip to main content

Section 3.12 Output

So far, we have already been using the print function to display values in the output window. Even though print may feel familiar by now, it plays a critical role in how programs communicate results to users.
In earlier examples, print allowed us to see the results of expressions and function calls. However, printing fixed values or single expressions is often not enough. As programs become more useful and flexible, they need to produce output that reflects the results of computation based on variables, expressions, and user input.
In Python, the primary way to produce output is through the built-in print function.
print("Hello, world!")
print(42)
print(3 + 5)
As we’ve seen before, Python first evaluates any expressions inside the parentheses, producing a single value. That value is then passed to the print function, which displays it in the output window.
We’ve also seen how the print function can display the value of a variable just as easily as a literal value. For example:
Here, the value stored in name is sent to print and displayed. Printing a variable does not change its value, it only shows it.
Even after printing x + 5, the value of x remains unchanged.

Subsection 3.12.1 Printing Expressions

The argument passed to print does not need to be a simple value. It can be any expression that produces a value, including expressions that involve operators or function calls.
In this example, we compute the number of hours and remaining minutes from a total number of minutes. The expressions minutes // 60 and minutes % 60 are evaluated first, and their results are printed. So Python evaluates each expression first, then prints the resulting value. This allows print to serve as a window into the program’s computation.

Subsection 3.12.2 Functions and Different Types of Parameters

Each function expects a particular number and type of input values-sometimes none at all-and is written to operate on those values in specific ways. The print function is a good example of this flexibility:
In each case, print performs the same overall task which is displaying output but adapts its behavior based on the types of values passed to it. Numbers are displayed as numbers, strings are displayed as text, and expressions are evaluated before being printed.
This does not mean that all functions accept all types of values. Some functions are more restrictive and require inputs of a specific type. For example, a function designed to perform arithmetic may only accept numbers, while a function designed to work with text may only accept strings.
So far, we have used print with a single value, but the print function can take multiple input values, separated by commas.
In this example, print is called with several arguments. Each argument can be a different type of value, such as a string or an integer. The print function handles each value appropriately and displays them together in a readable way.
When multiple values are provided, print displays them in the order they are given, inserting a space between each value by default. This allows us to combine text and numbers in a single line of output without converting values to strings ourselves.
We could produce the same output using string concatenation, but it would require more work:
print("Time: " + str(hours) + " hours and " + str(minutes) + " minutes")
Here, the + operator joins strings together, but because hours and minutes are integers, they must first be converted to strings using str(). While this approach works, it is more verbose and easier to get wrong.
Using multiple arguments with print is often simpler and more readable, especially when mixing text and numeric values. Later in the course, you will also see other ways to format output, but for now, passing multiple values to print is a clear and effective approach.

Remark 3.12.1.

Explore More
The behavior of a function depends on:
With print, these differences are mostly handled for you. Other functions may respond differently or even produce errors if they receive inputs they do not expect. Now that you understand that a function’s behavior depends on its inputs, you can use the documentation to see how it works, trace what the program is doing, and figure out what went wrong when there is an error. Take a look at the documentation for the print function and explore some of the different ways it can be used.
You have attempted of activities on this page.