Getting Started with Python
Written by Alex Armstrong   
Wednesday, 08 February 2012
Article Index
Getting Started with Python
Control structures
Functions
Inheritance

So you want to get up to speed with Python. Here's a lightening tour for the beginning to intermediate programmer who is already familiar with some fundamental programming ideas.

And now for something completely different – Python

Well, if you are going to venture into Python you might as well get used to the Monty Python quotes, and expect references to spam (a type of meatloaf and an acquired taste), Brian and grail (references to two classic films).

This is a brief introduction to Python for the beginning to intermediate programmer. It is assumed that you know enough about programming not to be confused by ideas such as variables, constants, loops and conditionals.

Python is a free to use open source language. Developed by Guido van Rossum in 1990, Python now has hundreds of thousands of users.

Although it is copyrighted by van Rossum, you can use it and distribute it without charge, even for commercial use. You can download a Windows/Linux/Mac version complete with a visual development environment for free and the good news is that your Python application will often run irrespective of which platform you developed it on i.e. Python is largely platform independent.

The first question to answer is why bother?

Python may be a high quality language but so is Java! Why bother with a strangely named language that you probably haven’t heard very much about. One good reason is that it is an interactive object-oriented language that has managed to take many of the good ideas from many other languages and meld them together into something workable.

It is also a good excuse to become nostalgic about Monty Python and, yes, there are lots of Monty Python references – for example the Python IDE is called IDLE after Eric Idle.

Getting a Python

If you want to try out Python then it couldn't be easier. Just go to www.python.org and download the version of Python you want to use.

This is our first problem.

Python comes in two distinct versions - version 2.7 and version 3.2.2 (at the time of writing). In the case of most languages you would be well advised to use the very latest version, but the split between version 2 and 3 of Python is so big that it isn't quite such a simple choice. As most of the Python programs that you will encounter are written in version 2 there is a certain logic in starting with this, so here we are using 2.7.

After installation is complete you can try Python out either by using the command line interpreter or the IDLE visual IDE which is installed automatically. 

 

If you start IDLE you can try out your first Python program by simply typing in:

2+2

and you will see 4 displayed..

Python is an interpreted language and whatever you type in is obeyed at once. Python is dynamic and variables can be assigned to without any need to declare them first.

Built in variable types include integers, floating point, complex numbers, unlimited precision integers and strings.

Notice that already we are moving into some unusual areas – most languages don’t bother providing complex numbers or integers with as many digits as you like.

What is more, Python decides the type of variable needed according to what you try to store in it. For example,

x=2+3j

is complex,

x=1

is integer and

x=123456578901234567891234567890123456789

is a long integer.

You also get all of the standard arithmetic operations and comparisons, plus some more interesting binary operations such as >> shift and xor but you can learn about these as you go along.

Strings

The fundamental way of working with text in Python is to use a string.

Strings work much like they used to in Basic in that there is no equivalent to a fundamental Char type – if you want to work with a single character just use a one-character string.

Strings support concatenation and  slicing.

For example:

s = “Spam” + “Spam”

gives “SpamSpam” and

s[i:j] 

is a slice of the string from offset i to offset j. Negative offsets are from the right hand end of the string.

You can also use a repeat operator

s="Spam"
s=s*3

is three times the amount of Spam.

You can also make use of formatting operations very similar to the C formatting codes. For example, %%d formats a decimal integer into a string.

At this stage suffice it to say that Python has sufficient string handling to make most tasks relatively easy compared to the tricks you have to perform when using C or Java. The ability to handle text easily is one of Python's strong points.

Data structures

After discovering the basic variable types the next thing you should be interested in are the data structures that are available.

In Python there are no arrays but this doesn't matter because it has the list.

Lists are in general more powerful and easier to use than arrays. You can create a list simply by writing constants within square brackets.

For example,

x=[1,2,3,4]

is a list of numbers.

The real power of a list is that it can contain any type of data including any Python objects and other lists. The items within a list can be of mixed type.

You can access list members using an index notation and you can insert and delete items in the same way.

For example, if

x=["spam","brian","grail"]

then x[0] is “spam” and

x[2] =“holy grail” 

replaces the last item “grail” by “holy grail”.

Notice that, unlike other fundamental Python data types, the list is a “mutable” object, i.e. it can be changed without making a new copy.

In this mode of use lists look like arrays but they can be used in more sophisticated ways. There are functions which append to a list, sort it, reverse it count it and so on.

Of course, as already mentioned,  a list may contain a list and so you are free to build data structures which correspond to trees or anything else that can be made of nested lists - i.e. everything.

There are also some more advanced or specialized data structures that you need to be aware are available for use.

A special type of list, the tuple, is also supported and you can think of this as a generalisation of a co-ordinate pair such as (2,3). Even more sophisticated is the dictionary, which you can think of as an associative array – that is you can look things up using non-numeric keys. For example:

x={"meat":"spam",0:"brian","holy":6}

and x[0] is "brian" and x["meat"] is "spam". Both keys and values can be strings or numeric.

<ASIN:1933988495>

<ASIN:0596007973>

<ASIN:0596100469>



Last Updated ( Wednesday, 08 February 2012 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.