• No results found

Producing Output

In document OO Programming in Python (Page 88-91)

Getting Started in Python

2.9 Using a File for Source Code

2.9.1 Producing Output

An interactive session in the Python interpreter is very much like a conversation between a software developer and the interpreter. The developer issues commands to the interpreter and the interpreter at times displays information to be seen by the developer. For example, here is a simple session indicative of those from earlier in the chapter.

>>> waitlist = ['Kim', 'Eric', 'Andrew', 'Nell']

>>> waitlist.pop(0) 'Kim'

>>> waitlist

['Eric', 'Andrew', 'Nell']

Notice that when we issue a call to a function that provides a return value, the return value is displayed by the interpreter. When we type the identifierwaitlistby itself, the interpreter responds by displaying a representation of that list. If, however, we were to type the very same commands into a separate file, as follows,

waitlist = ['Kim','Eric','Andrew','Nell'] waitlist.pop(0)

waitlist

and execute the source code, no output is generated. Although these responses are shown by the interpreter during an interactive session, they are not automatically displayed to the user. Theprintcommand allows the programmer to control what information is displayed to the user. For example, when issuing thepopcommand, had we wanted the return value to be displayed to the user, we must write

print waitlist.pop(0)

In this case, the following output is explicitly displayed to the user:

Kim

Notice that when a string is displayed with print, the quotation marks are not actually printed; those were only shown within the interpreter to clearly designate that information as astrinstance.

Of course, displaying such a name without any further explanation is not very help-ful; remember that the user does not know that this output was generated as a result of being popped from a waiting list. We should probably compose more informative output, perhaps with the command

print waitlist.pop(0),'your table is ready.' In this case, the user would see the output

Kim your table is ready.

Here, the syntax we use for theprintstatement has two arguments, separated by commas.

The first argument is the value returned from thepopand the second is the string literal 'your table is ready.'. When multiple arguments are outputted with a single printcommand, they appear on the same line with a single space automatically inserted between each pair. That is why a space appears between 'Kim' and 'your' in this example. Notice that the comma is not displayed as part of the output; that syntax is used in the source code to separate the arguments. If we want the comma to appear within the output it must be included as a character within the string. We might try the following, with the comma and space within the second argument:

print waitlist.pop(0),', your table is ready.'

Unfortunately this does not quote produce the desired format. It literally outputs the following:

Kim , your table is ready.

Python still inserts a space between the two arguments, in this case lying between the name and the displayed comma. An alternative approach to get the desired output is to use string concatenation to compose a single argument, as follows:

print waitlist.pop(0) +', your table is ready.'

By using the+operator, the two strings are concatenated into a single string without any additional space. Then that composite string is printed.

If two separateprintstatements are used, then by default, the user sees two separate lines of output. For example, if the above had been programmed as

print waitlist.pop(0) +', your table is ready.' print'Please come this way.'

the user would see

Kim, your table is ready.

Please come this way.

The default newline can be suppressed and replaced with a single space by putting a final comma at the end of the firstprintstatement. Thus the code

print waitlist.pop(0) +', your table is ready.', print'Please come this way.'

produces the output

Kim, your table is ready. Please come this way.

Printing nonstring data types

In these first examples, the arguments to theprintstatement happen to be string instances.

However, we can also useprintwith arguments from other types. In this case, those argu-ments are automatically converted to a string representation by Python. For example, it is perfectly acceptable to issue a command, as follows

print'There are', waitlist.index('Nell'),'people ahead of Nell.' If this command were executed at the end of our program, the output would appear as There are 2 people ahead of Nell.

We specified three arguments to theprintcommand, the second of which is formally the intinstance returned by the call toindex. When faced with a nonstring object, theprint command automatically casts it to a string, using the implicit syntaxstr(x)for argumentx. Mixing different arguments of different types is okay; each is independently cast to a string.

Finally we note that a singleprintcommand may cause multiple lines of output if newline characters are contained within any of the arguments. For example, we could print the entirely waiting list in a more user-friendly manner with the single command

print'Current list\n---\n'+'\n'.join(waitlist)

Although strings can be concatenated with the + operator, they cannot be directly concatenated to other data types. For example, the expression 'The number of guests is '+ len(guests) is illegal because the first operand is a string and the second is an integer. Instead, the integer must be cast to a string, as in 'The number of guests is '+ str(len(guests)).

This results in the following output, given our sample waiting list:

Current list ---Eric

Andrew Nell

In document OO Programming in Python (Page 88-91)