Вы находитесь на странице: 1из 9

Python as a Teaching Language

Purpose of this document


Introduction and intended audience
This paper is written to help A-Level centres assess the suitability of Python as a teaching language. It is specifically written for centres that follow the AQA New (2009) specification for Computing A-Level. Python is one of several possible languages for this specification, each of which has advantages and drawbacks. This paper aims to spell out the arguments for and against the adoption of Python as a first language, so that centres can make an informed decision, plan for implementation if appropriate, anticipate and avoid difficulties in deployment.

Background
The paper is based on a single years experience in moving from a Visual Basic environment to Python in an English Comprehensive School. Overall we consider the transition to have been successful and our results at AS back up this view obviously only time will tell whether this preliminary judgement is sound.

Quick overview
The Language
Python is a high level, dynamic, general purpose programming language. It has become popular as a scripting language partly due to the ease with which concepts can be expressed the language is often affectionately described as an executable pseudo-code 1 . In addition to the elegant core of the language, it is straightforward to create extensions; as a result open source or free libraries can be found and freely used for almost any purpose 2 . There is a large and enthusiastic support community on-line, together with extensive support resources and example materials. The language is owned by a software foundation and distributed under an open source licence that makes it free to use3 . Students find that it is simple to install the language on home machines allowing teachers to set practical homework tasks and thus maximise the overall time spent practicing the craft. Python looks different from other programming languages because of the way in which it uses whitespace. Python uses indentation to indicate blocks of code, blocks that in other languages are marked with begin and end statements or braces. Although new students find this easy and intuitive, experienced programmers sometimes find this a little awkward at first. The adjustment, however, is usually rapid.

1 2

Jython - http://hugunin.net/story_of_jython.html Popular Python modules - http://www.catswhocode.com/blog/python-50-modules-for-all-needs 3 Main Python Website - http://www.python.org/

Python names and commands are case sensitive and the language discriminates between equality (==) and assignment (=). These two facts immediately generate a whole slew of novice errors, but the latter avoids, or rather makes manifest, the confusion between assignment and comparison in a similar way to Pascal.

Deployment at School
Implementations of the language are available for a very wide range of platforms, including Unix, Macintosh, web servers and mobile phones. There are a bewildering variety of implementations 4 possible for the standard school Windows XP environment, some of these, although initially attractive turn out to be poorly documented and hard to deploy. The standard implementation of Python with the IDLE editing environment available from python.org, or the Activestate implementation are recommended if for no better reason than that they are well documented and straightforward for technicians to deploy.

Industry and Higher Education


Python occupies the second rank of programming languages 5 (the first rank being restricted to C and Java) in terms of popularity and commercial adoption. Many organisations use it as their principal language 6 and an increasing number of Universities use Python in their Computer Science courses 7 .

Positives
Straighforward syntax, free of clutter allows students to focus on the fundamentals. Console code is simple, with no hidden setup structures and no need to use complex objects for simple tasks. As a result teaching can concentrate on the fundamentals of programming rather than the quirks of a particular language.

Negatives
It is different from the prior generation of languages (C, Pascal, VB). Experienced teachers or programmers making the transition from such languages will have to adjust some of their basic concepts, for example: the absence of explicit data typing and the fact that Python names are labels for objects rather than traditional variables requires a change in teaching approach over traditional languages.

4 5

Python WIKI IDEs - http://wiki.python.org/moin/IntegratedDevelopmentEnvironments Language popularity charts - http://langpop.com/ 16/7/09 6 Commercial adoption - http://www.python.org/about/quotes/ 7 York University - http://www.cs.york.ac.uk/admit/Modules/ipp.html

Positives
Implementations and resources are Open Source, free and can be used at home on a wide variety of platforms including Unix, web servers, Macintosh and some cell phones. On a Windows environment the whole language and supporting programs can be run from a memory stick without any prior installation. Strong, enthusiastic and very active support community which has posted a vast range of examples and created comprehensive set of extensions and libraries. Excellent study guides, documentation and other teaching resources available on the Web for example Think Python, a translation of the classic introductory text How to think like a Computer Scientist and available as a free .pdf for download 8 . For experienced programmers, Dive into Python is a focused introduction to the language, also freely available on-line. 9 A growing body of anecdotal and more formal research experience suggesting that students find Python easier to understand than comparable languages and become independently productive more quickly. 10

Negatives
Although integrated environments exist for Python, they are not as well developed as the Microsoft or Delphi IDEs. One consequence is that Pythons debugging and error reporting compare unfavourably with other languages. Names and commands are case sensitive. Whitespace errors are possible, though rare. The transition from console programming to GUI development requires the mastery of an additional library and there are no drag-and-drop GUI editors at present for Python. The fit with the Pascal based approach to the AS curriculum is not perfect. Some Pascal concepts, such as procedures and arrays, have only rather artificial Python equivalents. Examples in the AQA textbooks will need to be re-cast to be understood by students.

Python encourages, but does not enforce, a readable coding style. The more adventurous and able, exploring aspects of the language such as lambda functions, can create masterpieces of obscurity. As with any language, good practice can and should be taught from the outset.

By way of summary, Python is free, straightforward and well supported. It allows the teacher to focus on the fundamentals of programming and the structures of algorithms. Using Python in the current Pascal based AS syllabus does require that some aspects of the AS course are sensitively adapted. This is not too difficult, the languages are very similar in many ways, Python shares many of the structures that make Pascal an excellent teaching language. Python is not just a teaching language, however, it has features

Think Python - http://www.greenteapress.com/thinkpython/thinkpython.html Dive into Python, Mark Pilgrim - http://diveintopython.org/ 10 Teaching Scientific Programming using Python, Mark Williams 2002 http://pentangle.net/python/report/publicreport.html
9

which go well beyond Pascal in sophistication, incorporating modern language features such as regular expressions - furthermore, because it plays nice with C, a very wide variety of third-party, Open Source extension modules are available to the Python programmer.

Specific issues for consideration


Object Orientation
Python is an object oriented language. Everything in Python is an object, creating new classes and instantiating objects are straightforward processes. There is considerable debate among those teaching A-level Computing as to the best way and right time to introduce object orientation. It is possible to teach an entire A-level course using Python without using objects or classes at all. Alternatively the concepts can be introduced in the first lesson. Python does not require that the programmer master object techniques in order to be productive - by way of example, the AS level sample program makes no explicit use of objects. Set against this, students who are not introduced to object techniques early are likely to find most on-line Python tutorials and code examples hard to understand.

Graphics
The basic implementation of Python does not include a drag and drop GUI builder and requires the use (and understanding) of additional modules to create graphic objects or user interfaces. On the plus side, these modules are easily obtained. The most commonly used and deployed graphics module is the TKinter library which comes as standard with most implementations. Other options are the WX library and GTK+. None of these modules are particularly straightforward to use and most require some understanding of object orientation to write successful programs with graphics. However this is not essential in order to create a simple GUI, the EasyGUI module 11 allows a very straightforward approach to GUI programming which dispenses with the requirement to master the event-driven paradigm or object orientation. The right approach to the development of applications can therefore be tailored to the student. There is a trade-off however. If the main task is to create a Windows only GUI program, then Visual Basic or Delphi, with their drag and drop GUI builders, have an advantage. The ease with which complex GUI forms can be created does come at a price, the presence, although often hidden, in the program of large amounts of inscrutable machine generated code. Such automatic code can make teaching difficult, the teacher has an unwelcome choice: either try to explain it to pupils who lack the conceptual grounding to understand, or advise the students to ignore it, whilst trying to give clear guidance as to exactly what should be ignored and why. There is more to programming than developing Windows GUIs, extending student work into more challenging application domains is something that the current syllabus strongly
11

EasyGUI Home - http://easygui.sourceforge.net/

encourages. Here Python has a clear advantage over both VB and Delphi, since there is a vast range of open-source extensions and enormous scope for experimentation and genuinely new projects for example the Pybluez 12 library provides a simple interface to a computer or mobile phone Bluetooth stack.

Versions, implementations and support


The canonical implementations of Python are available from the website python.org. At time of writing there are two, versions 2.6.2 and 3.1. Both of these versions are free downloads and include the IDLE integrated development environment. Python 3.x represents a significant upgrade of the language which is quite consciously not backwards compatible with earlier versions of the language 13 . This is obviously a potential problem. For A-level purposes however the changes between the two versions are fairly straightforward: changing the way printing works; replacing raw_input with input and dropping the <> comparison (use != instead). There are, of course, many more significant changes but these are unlikely to affect novice coding. By way of example the author has edited the Python 2.x skeleton program issued for the 2009 COMP1 exam to make it run under Python 3.1, the work was largely mechanical and took less than ten minutes. Other noteworthy implementations of Python are: The Activestate package which includes its own integrated development environment and a set of additional modules specifically for the Windows environment. Portable Python, a lightweight implementation which can run from a USB stick Jython: an implementation of Python in Java, using the Java Virtual Machine IronPython, a Microsoft project that targets .NET and Silverlight.

Support
There is an active newsgroup, comp.lang.python on usenet There is also an extremely active mailing list, called Tutor 14 which is specifically for those learning the language. Typically there will be upwards of 10 messages per day on this list. The mailing list is perhaps the clearest example of why Python is recommended for novice programmers, it is an extremely respectful, resourceful and helpful support environment.

Moving to Python from Visual Basic


An experienced Visual Basic Programmer may struggle initially with the unfamiliar look of Python. As with the majority of modern languages, Python is case sensitive,

12 13

Pybluez library - http://code.google.com/p/pybluez/ Comparison between 2 and 3 - http://docs.python.org/3.0/whatsnew/3.0.html 14 Tutor information page: http://mail.python.org/mailman/listinfo/tutor

something that will doubtless cause irritation if VB has been the language of choice. A further frustration is that the Integrated Development and Debugging environment is less mature than Visual Studio. Set against this, Python has an interactive interpreter which means that simple commands can be tested immediately, without a requirement to create, save, compile and run a file. When creating a program in Python there is no need to interface with a complex set of object structures and no requirement to generate a large amount of inscrutable, if hidden, supporting code to develop a simple application. Python is in many ways a more sophisticated language than Basic, with built-in support for exotic data structures and methods such as regular expressions. The language feels much richer, with a much wider variety of support available on-line. There are more ways to approach problems in Python, more sense of exploration and discovery. The language benefits from being part of the University-driven hive of activity around Unix platforms and Open-Source software. There are no arrays in Python. Alternatively there are four different types of array in Python and a further type from an external module. These four types, particularly Lists, can simulate multi-dimensional arrays but the process is clumsy and requires care 15 . The way in which Python uses whitespace is appealing, but it does require discipline to manage properly. A Python purist would say that tabs should not be used for indentation, four spaces is the recommended method. Using tab is a difficult habit to break. Mixing tabs and spaces can cause syntax errors. Most modern editors avoid any problems by automatically replacing tabs with a set number of spaces. There is much to admire in the language, for example the way in which Python handles global variables, requiring that they are explicitly identified as such within functions, is particularly elegant and effective. The main issue for those moving from Visual Basic is likely to be the difficulty in writing Windows graphic user interface driven applications. With Visual Basic these are the first and often the only kind of application that ever gets written. Python has no built-in GUI builder, the student must master a GUI toolkit in order to create such an interface.

Moving to Python from Pascal or Delphi


The points made in the previous section apply equally to those moving from a Delphi environment. Since the AQA specification and supporting materials are very heavily weighted toward Pascal or Delphi, a move to Python will require care in adapting some of the existing teaching material to a new language. Although a version of Delphi is available from Embarcadero without charge, it is not a trivial matter to install on a Windows machine. In contrast the Python implementations

15

http://www.python.org/doc/faq/programming/#how-do-i-create-a-multidimensional-list

represent the complete product and can, if required, be installed on a portable USB memory device. Python does remove the requirement to declare constants, variables and functions in a specific order. This allows related components to be grouped together in the program body. The use of global variables is discouraged in Python, although they are available if required (and rather sensibly implemented). Making the move from Pascal to Python is a process of discovering alternative, safer and clearer ways to accomplish what is conventionally achieved using globals in Pascal.

Four concluding quotations


The paper by Jeffrey Elkner, from which the following extract is taken, describes an American High Schools experience in moving first from Pascal to C++ and subsequently to Python. It is worth reading in full. It is a thoughtful exploration of the teaching and learning issues that make the subject of programming so difficult to teach well. Another example of how Python aids in the teaching and learning of programming is in its syntax for functions. Of all the things that I learned by using Python this year, the way in which the right tool could help in explaining functions was the most exciting. My students have always had a great deal of difficulty understanding functions. The main problem centers around the difference between a function definition and a function call, and the related distinction between a parameter and an argument. Python comes to the rescue with syntax that is nothing short of beautiful. Function definitions begin with the key word def, so I simply tell my students, "when you define a function, begin with def, followed by the name of the function that you are defining, when you call a function, simply call (type) out its name." Parameters go with definitions, arguments go with calls. There are no return types or parameter types or reference and value parameters to get in the way, so I was able to teach functions this year in less then half the time that it usually took me, with what appears to be better comprehension. 16 The second quote is the conclusion of a detailed paper written by Michael Williams at Oxford University. The paper documents an extensive consultation exercise with students and can be explored in detail on-line. He concludes: I believe the trial has shown conclusively that it is both possible and desirable to use Python as the principal teaching language:

it is Free (as in both cost and source code).

16 Into the Classroom: Preliminary Thoughts on Python as a High School Programming Language, Jeffrey Elkner

http://www.python.org/workshops/2000-01/proceedings/papers/elkner/pyYHS.html

it is trivial to install on a Windows PC allowing students to take their interest further. For many the hurdle of installing a Pascal or C compiler on a Windows machine is either too expensive or too complicated; it is a flexible tool that allows both the teaching of traditional procedural programming and modern OOP; It can be used to teach a large number of transferable skills; it is a real-world programming language that can be and is used in academia and the commercial world; it appears to be quicker to learn and, in combination with its many libraries, this offers the possibility of more rapid student development allowing the course to be made more challenging and varied; and most importantly, its clean syntax offers increased understanding and enjoyment for students;

Python should be used as the first year teaching language. If used it will be possible to teach students more programming and less of the peculiarities of a particular language. 17 The third quote is from Roger Davies, a Computing Teacher at Queen Elizabeth School in Cumbria. He has been teaching the new AQA syllabus using Python: The main advantage for me is the clarity of syntax. This is a huge issue with beginner programming, particularly as many pupils cant write grammatically correct English. Perpetual syntax errors are soul destroying Python helps maintain enthusiasm because you get programs working fairly quickly. Much easier therefore to build up pupil knowledge. The final quote is from Steve Holden, Chairman of the Python Software Foundation, whose help in preparing this paper is much appreciated. The Python Software Foundation, the body charged with the promotion and development of the Python language, is keen to promote Pythons use as a teaching language. This is not because of partisan concerns but because of a belief in Pythons fitness for the purpose. Unusually, Python was designed to be easy to write. This acknowledges that programming languages are an important means of communicating algorithms. Early computer science education benefits from a focus on algorithmic design, and sample Python code is valuable in educational contexts in ways that other code might not be. The dual nature of the language (with good features for both procedural and object-oriented programming) makes it adaptable to a variety of pedagogical approaches.

Teaching scientific programming using Python, Michael Willliams, Oxford University http://pentangle.net/python/report/publicreport.html

17

This adaptability, coupled with the existing support for a large range of application domains (from numerical computation and bioinformatics to networking), means that Python is a great learning tool that will stand students in good stead as their programming skill increases.

Further Reading and Resources


Languages and Tools Principal Python Website ActiveState Python http://www.python.org http://www.activestate.com/activepython/

Textbooks and teaching resources Think Python Dive into Python http://www.greenteapress.com/thinkpython/thinkpython.html http://www.diveintopython.org/toc/index.html

Libraries and other add-ons PyGame EasyGui Python SQL Support communities Python Newsgroup Tutor Mailing List Research Papers Teaching using Python Python in the Classroom http://pentangle.net/python/report/publicreport.html
http://www.python.org/workshops/200001/proceedings/papers/elkner/pyYHS.html

http://www.pygame.org/news.html http://easygui.sourceforge.net/ http://oss.itsystementwicklung.de/trac/pysqlite

http://groups.google.com/group/comp.lang.python/topics http://mail.python.org/mailman/listinfo/tutor

Graham Macleod 22nd July 2009

www.ictlesson.com