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.
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.
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.
Subsection3.12.2Functions 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.
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.
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.
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.