Screen I/O
Python provides a full spectrum of input/output operations to programmers. The elementary routines are convenient to use; more powerful capabilities are there for when you need them.
We will start by performing simple I/O to the console. This will allow you to do the mini-exercises in this material (including the one below) without constantly having to re-enter statements at the keyboard. More advanced types of I/O will be considered later.
Screen input
In Python 3, a key Python function for user input is the input('prompt')
function. It prints the string prompt which asks the user for input, waits for the user to respond, then returns the user's input as a separate string. In the following example, the prompt 'What is your name? ' is printed first. The interpreter then waits for input. Here, the input 'Joe' is received by input()
, returned to the calling program, and stored in string variable x
.
Note: input()
is an example of one of the potential "gotcha" differences between Python 2 and Python 3. In Python 3, input()
returns a string associated with the text input by the user. In Python 2, this functionality was provided by a function named raw_input()
; a separate function named input()
attempted to evaluate the input text as a Python expression and return the result of that evaluation. Python provides a built-in function named eval()
that evaluates a string passed as an argument, so in Python 3 one can explicitly evalulate the string returned by input via eval(input('prompt'))
. Alternatively, if one wants to convert the string input to a known type, one could call the constructor for the desired type, e.g., int(input('prompt'))
to attempt to convert the input string to an integer.
Screen output
The key Python function for screen display is the print()
function, which takes an arbitrary number of arguments, converts them to strings, and then prints them to the screen (or, optionally, some other stream). Note that while print
produces output on the screen, the function does not return anything (or more exactly, it returns an object of type None
). The print
function attempts to convert its input arguments to a string representation; if one is printing out a string, that string can be constructed within the function call itself (e.g., by comparing different variables into a formatted string). The quotes that would normally surround a string variable are removed from the final output displayed by print()
.
Note: print()
provides another example of an inconsistency between Python 2 and 3. In Python 3, print is a built-in function that prints out a textual string representation of its arguments. In Python 2, print was a reserved keyword that printed out what followed, but without requiring parentheses as would be the case for a function call, e.g., print 'a/b =', a/b
. In Python 2, in order to get the print-as-function functionality of Python 3, one can look to the future with the following statement:
In the interactive interpreter, this import statement can be executed at any time. Within a source code file that is imported, however, it must be provided before any further import statements.