4.4.3 Python's peculiarities

It should not have any unnecessarily complicated constructs whose use is required to undertake common programming tasks...

In general, the trial demonstrated that Python's way of doing common tasks was suited to educational use; there are few complicated constructs.

Perhaps the most clumsy thing in Python is formatted input, such as reading in a data file with many entries on the same line. In Pascal this is done using readln(x, y) (or readln(fin, x, y) where fin is a file pointer). x and y are then the first two whitespace separated numbers on the line. The program will fail at runtime if it reads in something other than numerical values. It is not much more complicated in C, although it does involve pointers, which is a topic a one-day introductory course would probably avoid:

    scanf("%d %d\n", &x, &y);         /* from stdin */
    fscanf(fin, "%d %d\n", &x, &y);   /* from file  */

In Python the same task is achieved by doing:

    linestring = raw_input()                  # Read in a line of input 
                                              # from stdin.
    linelist = string.split(linestring)       # Split the line on
                                              # whitespace, returning a
                                              # list of strings.
    x = int(linelist[0])                      # Assign the coerced strings
    y = int(linelist[1])                      # to the required variables.

If input is to be taken from file the first line is replaced with:

    linestring = fin.readline()

Here fin is a file object. This method is objectively convoluted in comparison to Pascal and C. It did not appear to be a problem for many of the trial students though. This was required by the easiest problem (CO11) which was attempted by the least able students. Encouragingly, few of them found it a problem. Those that did had usually incorrectly coerced the strings returned by the split function which is not a problem in Pascal as it is not dynamically typed. The questionnaire suggested a large minority found reading from files a problem (21%), but it is worth bearing in mind that reading and writing files is always one of the things students struggle with.