|
|
|||
![]() |
Department of Engineering |
| University of Cambridge > Engineering Department > computing help > Languages |
Python is a free, interpreted language with support for Object-Oriented work. If you know matlab, perl or Java you'll have met most of the concepts used by Python. On the teaching system you can run it by opening an XTerminal window and typing python. The prompt at the start of the command lines is >>>
This document aims to give you a feel for the language by giving you some sample commands to type in. Without knowledge of perl, Java or matlab, some of the underlying concept might be difficult for you: at the end of this document are some sources of further information.
You needn't create them and specify their type before assigning values. The following commands work
You can use the print command to see a variable's value. Python has ways of collecting elements together. The elements needn't always be all of the same type
Here x is a list, so it's "mutable" and can be changed.
a == b will tell you whether the 2 variables have the same value. a is b will tell you whether the variables refer to the same object.
Multiple assignments are possible, with the elements of one list being assigned to another. That makes the following possible
All the usual mathematical operations are possible, and much more besides.
The range command creates a list
Slicing operations extract elements from sequences. You give the index of the first element you want (indexing starts at 0) and the index of the first element you don't want. For example
You can set alternate elements to particular values. Try
You can remove slices. Try
print 4+2 works, so does print "x" + "2", but
won't work - you'll get something like
You need to convert one of the objects by doing (for example)
The list command converts a string/tuple into a list - useful because a list is mutable. The following converts a string into a list (so that the characters can be sorted in-place), then converts the list back into a string using a member function (to find out what a member function is, see later)
So far we've been working from the command line. If on a unix machine you put commands into a file, make the file executable (by typing chmod u+x filename) and put
as the first line of the file, you can run the file (if the python program exists at /usr/bin/python). Comments in a script begin with a #. For some of the longer examples that follow, it's assumed that you'll write a script rather that enter the lines interactively, so the command-line prompt won't always be shown.
python "add-ons" are called modules. You need to "import" a module. For example,
The sys module lets you provide arguments to scripts. sys module lets you access these arguments. In a script, the following lines will print the first argument given to the script
The objects so far created are objects which have member functions. Here are some examples of use
Dictionary objects have several specialised routines
You can also define your own classes, and inherit from existing classes. The following creates classes called 'foo' and 'fooplus'. __init__ is the constructor (as in C++ and Java). Default values can be set similar to the way they are in C++ - here, num is 3 by default. Note that member functions need to explicitly state which object they're acting upon, but when you call a member function you don't mention the object as a parameter
If a variable is within a class but outside all member functions, then it is shared by all objects of that class.
Python has all the usual control flow options - again, indentation is significant
The file-handling commands are similar to other languages
The os module lets you find out about the computer
Like several other languages (C++, Java, etc), Python lets you use exceptions to deal with errors. The following code works
producing
(Python's print command is behaving rather like C's printf) but if you try
you'll get
If you run the following, you'll see that the error caused by the print command has been detected and a message of your choice has been displayed.
More technically, the print command raised an exception of type TypeError, which the except handler dealt with.
| | Python | Languages | computing help | |