Chapter 3 The First Python Program

3.1 Text Input and Output

Text input and output (I/O) used to be the dominant user interface before graphical user interface (GUI) was invented in the 1980s. Nowadays, most system administrators, researchers and hackers still use the text console as the main I/O interface. Compared with GUI, text UI is relatively much easier to learn as a beginner. Python allows interaction between a program and a user via text IO, by proving a special type of objects: strings. A Python string can be treated as a sequence of text characters.

Traditionally, the first program written in a new language is called Hello, World! because all it does is display the words, Hello, World! In Python, the source code looks like this.

print("Hello, World!")

This is an example of using the print function, which doesn’t actually print anything on paper. It displays a value on the screen. In this case, the result is the phrase:

Hello, World!

Here is the example in activecode. Give it a try!

The quotation marks in the program mark the beginning and end of the value. They don’t appear in the result.

Some people judge the quality of a programming language by the simplicity of the Hello, World! program. By this standard, Python does about as well as possible.

The print statement is used for text output; it shows the value of an object as a string on the text console. In most cases, the text console is the text terminal (i.e. Terminal in Linux and Mac OS, or Command Line in Windows). In the case of IDLE, the text console is IDLE itself. The syntax of the print statement is print (x), where x is an expression. When print x is executed, the value of x is calculated first, and then converted into a string, before being displayed.

In the last two examples above, the values of the expressions 3 + 5 − 2 ∗ 6 and ′%.2 f ′%3.1415 are evaluated first, before being displayed on the console, respectively. Here the string type is used as a bridge between the value of an expression and the output console-an object is converted into a string before being displayed on the console.

In the example above, the codes show Python’s internal representation of the internal representation of the floating point number and its string conversion, respectively. As can be seen from the example, the floating point number displayed by the print statement is the same as the string conversion, but different from the internal representation of the floating point number itself. This is because the number has gone through a string conversion before being printed.

The string conversion by the print statement is implicit — the str function is not called explicitly in the statement. When custom types are introduced later, a custom string conversion process is discussed, and the implicit string conversion by the print statement is reflected more directly.

The print statement displays a string on the console. This may appear to be a redundant functionality, since IDLE will display the value of an expression anyway. However, the print statement is necessary and important when Python code is executed as a stand-alone program and independent of IDLE. In that case, the value of an expression is not displayed when the expression is a single line of code. In addition, although for integers and floating point numbers, Python’s internal representation is similar to the string conversion, for many other types, the two representations can be very different, and hence what IDLE displays can be rather different from what the print statement shows.

One final note is that when a string is printed, the escaped characters in the string are displayed in their original form.

Because backslach() are used for escaped characters, their literal form in a string is represented by ‘\’.

Input from the text console can be achieved by using the input function, which asks the user to enter a string and returns the content of the string as a Python object. A command line prompt can be specified as an input argument to this function.

In this example, when the first line of code is executed, the input argument “Enter a string:”is displayed, with the program being temporarily stopped to wait for user input. If the user enters the letters ‘a’, ‘b’ and ‘c’, and then presses the Enter key to complete the input. The function call then returns the string “abc” as its value.

Note that input(“Enter a string:”) is a function call. It forms an expression on the right hand side of an assignment statement in the example above, and is evaluated to a string object. The specialness of this function call is its evaluation process: IDLE stops to ask the user for text input, and evaluates the return value of the function call according to the input, rather than according to the input string argument (e.g. “Enter a string:”), which is used for console prompt only. This behavior is rather different from the round, len and int function calls, which are all numerical and do not affect the execution of IDLE. However, it remains true that all function calls are expressions that evaluate to a Python object.

input is the basic mechanism in Python for accepting user input from a text console. The return value of this function is always a string, and therefore type conversion is necessary when the desired object is an integer, a floating point number or other types.

>>> x=input("x=")
x=123
>>> type(x)
<class ’str’>
>>> x
'123'
>>> x=input("x=")
x=12.3
>>> type(x)
<class ’str’>
>>> x
'12.3'
>>> x=input("x=")
x=abc
>>> type(x)
<class ’str’>
>>> x
’abc’

In the example above, the input function is called with the “x=” prompt being displayed.

An integer literal, a floating point number literal and a literal are entered respectively. The results are always strings.

Check your understanding

© Copyright 2024 GS Ng.

Next Section - 3.2 Strings