A-level Computing/AQA/Python

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Python is a free multiplatform programming language. Its original purpose and strengths were as a scripting language to automate the administration of Unix systems. Both the core language and the standard library have since been extended for many other purposes, including web, database, interactive and graphical applications.

Why Python?[edit | edit source]

The reference implementation of Python, CPython, runs almost everywhere, and is well supported for Linux, Unix, Mac OS X and Windows. Many free text editors offer syntax highlighting, code completion, class navigation, debugger integration and other tools. Therefore students can pursue their interest easily at home, work or at computer clubs.

The main implementations (CPython, Jython and IronPython) are open source and free-of-charge. CPython is included as standard in most Linux distributions.

As of 2012, Python is one of the two main languages that Google offers, alongside Java, for its customer APIs. Though not as popular in industry as Java and VB, it has been adopted in various fields of employment.

Python is interpreted, so trial-and-error experimentation and debugging can be fast. The standard runtime also includes an interactive console with access to the executing program image.

Python has a wide range of data types, both built-in and in its standard library, though there can be some quirks and inconsistencies among them. For example, some types are mutable and others are immutable.

Python's significant whitespace makes incorrect nesting of block structured statements easy to spot.

Why not Python?[edit | edit source]

CPython's virtual machine is a bytecode interpreter and execution of Python can be much slower than say, execution of Java on its HotSpot virtual machine with just-in-time compilation.

Python's significant whitespace can make it easy to break a working method with the space bar or tab key.

Python uses variables without declaring datatypes, this can be confusing when trying to teach them.

When using classes, python doesn't use the keywords public and private to specify access to attributes and methods. They are possible, but may confuse students.

2.7 or 3.x?[edit | edit source]

When you read about python you will probably learn that there are two different versions out there. AQA allows you to use either but which one is most suitable for you? A quick summary:

2.7 is stable, end-of-life, no further updates, but solid and widely deployed, with a vast array of libraries and learning resources.

3.x is the future - handles multi-lingual text correctly, many improvements, but some libraries not yet converted.

Some tutorials written for python 2.7 won't work in python 3.x as there are small changes in the language such as the print statement in Python 2.x being replaced with the print function in Python 3.x:

print "hello world" #python 2.7
print("hello world") #python 3.x

You can install both and they'll play nicely together, but when choosing what language to pick you might be wise to check out the resources that you have available to your students.

Getting Started[edit | edit source]

You can get a portable version of python that can be run directly from a USB, available in 2.7 and 3.x versions.

If you want to use Microsoft Visual Studio you can get IronPython which is free and integrates python 2.7. Alternatively you can get Python Tools for Visual Studio which is free and works with existing installations of Python 2.x and/or 3.x. This gives you syntax highlighting, Intellisense, debugging, code folding, type hints, and more in much the same way as Visual Studio works for other languages.

Python comes pre installed on the Raspberry Pi with development tools and lots of supporting learning resources.

COMP1[edit | edit source]

Arrays[edit | edit source]

Although some users encourage learners to use Python's built-in list as an array, the standard library includes the array module which some teachers may prefer. See, for example,

Check which array the examiners prefer.

COMP3[edit | edit source]

Object Orientation[edit | edit source]

Since Python doesn't have the concept of explicit declarations, or PRIVATE and PUBLIC keywords, the syntax for defining classes differs quite a lot from the generally accepted AQA syntax, e.g., the VB.NET class

Class MediaFile
        Sub PlayFile
        End Sub
        Function GetTitle As String
            Return Title
        End Function
        Function GetDuration As Single
            Return Duration
        End Function
        Dim Title As String
        Dim Duration As Single

is a reasonable match to the June 2010 COMP3 question:

MediaFile = Class   
        Procedure PlayFile
        Function GetTitle
        Function GetDuration
        Title : String
        Duration : Real

The Python version looks something like this:

class MediaFile
    def __init__(self):
        self.Title = ""
        self.Duration = 0.0

    def PlayFile:

    def GetTitle(self):
        return self.Title

    def GetDuration(self):
        return self.Duration

COMP4[edit | edit source]

Python can be used to build anything from websites to games using pygame (v2.7). Blender has a python scripting engine (3.x) meaning that it is possible to create projects involving 3D animation. Python also has a decent RAD (Rapid - Application - Development) tool in the form of Glade and PyGTK. Python's origins as a scripting language mean that it can be used to glue together Unix programs to make an application.

Open source library code can be imported, as teaching examples or to extend the practical project, from PyPI http://pypi.python.org/pypi , with package management tools built into Python.

To ease the generation of some documentation, students may be introduced to the docstring, a form of embedded documentation supported by Python source code. A number of tools are available for generating readable documents from source code containing docstrings, including pydoc (in the standard library), Epydoc, doxygen and Sphinx.

For website creation, Python is one of the alternative 'P's in LAMP. (Like Perl and PHP, Python integrates well with Linux, Unix, Apache HTTP Server and MySQL.)

Online resources[edit | edit source]

If you're already proficient in another programming language, the official tutorial will get you up to speed at speed. Even if you're not a programmer, this fast-paced, no-frills introduction will get you programming in Python quickly, with a reasonable degree of mental pain!

Other Python books at Wikibooks are listed under Python programming language.

Books[edit | edit source]

Useful printed books are as follows:

Title ISBN Suitable for
Python Programming for the Absolute Beginner (3.1) 978-1435455009 Applications and Games
More Python Programming for the Absolute Beginner (2.7) 978-1435459809 Applications and Games
Dive Into Python (2.7) 978-1590593561
available for free online
Python for Software Design: How to Think Like a Computer Scientist (2.7) 978-0521725965
available for free online
Algorithms and CS thinking
Invent Your Own Computer Games with Python (3.2) 978-0982106013
available for free online