With the final release of Python 2.5 we thought it was about time Builder AU gave our readers an overview of the popular programming language. Builder AU's Nick Gibson has stepped up to the plate to write this introductory article for beginners.
Python is a high level, dynamic, object-oriented programming language similiar in some ways to both Java and Perl, which runs on a variety of platforms including Windows, Linux, Unix and mobile devices. Created over 15 years ago as an application specific scripting language, Python is now a serious choice for scripting and dynamic content frameworks. In fact it is being used by some of the world's dynamic programming shops including NASA and Google, among others. Python is also the language behind Web development frameworks Zope and Django. With a healthy growth rate now could be the perfect time to add Python to your tool belt. This quickstart guide will give you an overview of the basics of Python, from variables and control flow statements to exceptions and file input and output. In subsequent articles I'll build upon this foundation and offer more complex and specific code and advice for using Python in real world development.
Why learn Python?
- It's versatile: Python can be used as a scripting language, the "glue" that sticks other components together in a software system, much in the same way as Perl. It can be used as an applications development language, just like Java or C#. It can be used as a Web development language, similiarly to how you'd use PHP. Whatever you need to do, chances are you can use Python.
- It's free: Python is fully open source, meaning that its free to download and completely free to use, throw in a range of free tools and IDE's and you can get started in Python development on a shoestring.
- It's stable: Python has been around for more than fifteen years now, which makes it older than Java, and despite regular development it has only just has reached version 2.5. Any code you write now will work with future versions of Python for a long time.
- It plays well with others: It's easy to integrate Python code with C or Java code, through SWIG for C and Jython for Java, which allows Python code to call C or Java functions and vice versa. This means that you can incorporate Python in current projects, or embed C into your Python projects whenever you need a little extra speed.
- It's easy to learn and use: Python's syntax closely resembles pseudo code, meaning that writing Python code is straightforward. This also makes Python a great choice for rapid application development and prototyping, due to the decrease in development time.
- It's easy to read: It's a simple concept, a language that is easy to write should also be easy to read, which makes it easier for Python developers to work together.
Okay, enough already, I'm sold. Where do I get it?
Easy, in fact if you're running Mac or Unix, chances you've already got it, just pull up a terminal and type "python" to load up the interpreter. If you don't have it, or you're looking to upgrade to the latest version head on over to the download page.
Alternatively you could install ActivePython, a binary python distribution that smooths out many of the hassles. There is a graphical installer under most platforms, all that you need to do is click through the dialogues, setting the install path and components. In Windows, you can then start up the interpreter by browsing to its entry in the start menu, or on any system simply by typing 'python' in a terminal. While ActivePython is generally easier to set up, it tends to lag behind official Python releases, and at the time of writing is only available for Python 2.4.
Interactive Mode
Now it's time to load up the interpreter in interactive mode, this gives you a prompt, similiar to a command line where you can run Python expressions. This lets you run simple expressions without having to write a Python program every time, let's try this out:
Python 2.5 (r25:51908, Oct 6 2006, 15:24:43) [GCC 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> print "Hello World" Hello World >>>
The first two lines are the Python environment information, and are specific to my install, so your mileage may vary. Interactive mode is useful for more than just friendly greetings however, it also makes a handy calculator in a pinch, and being part of a programming language allows you to use intermediate variables for more complicated calculations.
>>> 2+2 4 >>> 2.223213 * 653.9232 1453.8105592415998 >>> x,y = 5,20 >>> x + y 25 >>> tax = 52000 * (8.5/100) >>> print tax 4420.0 >>> "hello" + "world" 'helloworld' >>> "ring " * 7 'ring ring ring ring ring ring ring '
Another Python type that is useful to know is the list; a sequence of other types in order. Lists can be added and multiplied like strings, and can also be indexed and cut into sublists, called slices:
>>> x = [1,2,3,4,5,6,7,8,9,10] >>> x [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> x + [11] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] >>> x + [12] * 2 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 12] >>> x[0], x[1], x[9] (1, 2, 10) >>> x[1:3], x[4:], x[2:-2] ([2, 3], [5, 6, 7, 8, 9, 10], [3, 4, 5, 6, 7, 8]) >>> x[:] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
The interactive mode is good for quick and dirty calculations, but once you start writing anything longer than a couple lines, or you would like to save your programs to use again later, it's time to let it go and start writing python programs. Thankfully this is easy, just type your commands into a .py file and run it with the same command.
Control Flow
Just like your favourite programming language, Python has all the usual ways of controlling the execution of programs through if, while and for statements. In Python, an if statement looks like this:
if a > b:
print a
else:
print b
You'll see that unlike languages such as C or Java, Python does not use braces to group statements together. Instead statements are grouped together by how far they are indented from the margin, the body of a statement extends for as long as there are lines below it with the same or greater indentation.
x = 3
y = 4
if x > y:
x = x + 1
y = y + 1
For example, in the above code x = 3 and y = 4 at the end of the program.
x = 3
y = 4
if x > y:
x = x + 1
y = y + 1
In the second example, however, y finishes equal to 5.
Python also has loops, both of the while and for variety. While loops are straightforward:
while a > b:
a = a + 1
For loops work a little differently from what you might be used to from programming in other languages, rather than increasing a counter they iterate over sucessive items in a sequence, similiar to a Perl or PHP foreach. If you do need to have that counter its no problem, you can use the built-in range function to generate a list of numbers like so:
for i in range(10):
print i
If you need it, you can have finer control over your loops in Python by using either break or continue statements. A continue statement jumps execution to the top of the loop, whilst a break statement finishes the loop prematurely.
for x in range(10):
if x % 2 == 0:
continue
if x > 6:
break;
print x
This code will produce the following output:
1 3 5
Do you need help with Python? 





1
Supun - 02/11/06
How to use Datagrid 6.0 with checkbox
» Report offensive content
2
No ads! - 15/11/06
Ian,
Cut out the ads!
» Report offensive content