Python Input Output


More about Keyboard Input:

Looking back at the previous examples you may have noticed that there are two specific ways in which Python accepts data from the keyboard. In one of the first examples ( – Fig. 13 on p. 33), the keyword ‘raw_input’ was used. What that function does is it allows input from the keyboard which then gets returned as a String. This is generally used for text input.

The above mentioned keyword is quite versatile and if you are expecting a number to input it is possible for that input to be converted to almost any format required.

The other keyword that allows for data to be input from the keyboard is the ‘input’ keyword. This keyword (used in the example in Fig. 20 on the previous page) is more specific and actually only allows for an integer expression to be input. This keyword should therefore only be used if you are expecting the response to be an integer. If for some reason the wrong input is giver (alphabetic characters), Python will respond with a syntax error. It is, however, possible to validate input and in so doing you can prevent your program from having a runtime error (crashing). We will cover validation techniques at a later stage.

Type Conversion:

As was mentioned in the previous section it is possible to convert from one data type to another. There are a variety of reasons why this is useful but one of the most common situations is where text is read in from the keyboard as a string (using raw_input) and then if a calculation needs to be done on a number it can be converted to either an integer or a float.


In the above example we start off with a variable named ‘tmp1’ that gets instantiated as a string since data is read in from the keyboard as a string using the ‘raw_input’ keyword. As you can see in line four the text (23) is displayed within single quote marks which indicates that the data is stored as a string. The next line shows how that string gets converted to an integer. Please note that if you try to convert anything other than an integer in this way Python complains with a ValueError. In this case I converted the string ‘23’ to an integer, which it is. Notice how the inverted commas are missing now.

In the 7th line I converted the integer value to a floating point number. Notice how the output now includes a decimal. This indicates that tmp1 now is a floating point variable. If for some reason you convert a number containing decimals to an integer, Python will round the number down to the nearest integer. It is also possible to convert numbers to strings.

When converting from strings or numbers (integer or float) to Boolean, there are some interesting facts to bear in mind: In the case of numbers (float or integer), if the number is zero, it gets converted to ‘False’ and any non-zero number gets converted to ‘True’. In the case of strings, if the string is empty, it gets converted to ‘False’ and if it contains any text, it gets converted to ‘True’.

The Return Statement:

The ‘return’ statement has two main uses:

  1. It allows execution in a function to be stopped and then return execution to the calling function. If you are using data validation techniques and an error occurs that you have caught, you can use the ‘return’ statement to return to the calling function allowing the user to retry or select an alternative function / operation.
  2. The ‘return’ statement can also be used in conjunction with data whereby the data can be returned to the calling function as an attribute. This is especially useful when there is a chain of conditional statements and each of them results in a different output. In such a case each of the conditions will have their own return statement.


« Previous || Next »