C4Swimmers Newsletter  

(Tutorial) Introduction to Python - Part 1

Tutorial : Introduction to Python - Part 1

Python is a very handy tool whenever you need to put together a small script that manipulates some files in a few minutes. Moreover, it is also useful for bigger projects, as you get all the power you from data structures, modularization, object orientation, unit testing, profiling, and the huge API.

Python has connection with almost everything. You have very advanced string and regular expression handling, you have threading, networking (with many protocols built-in), compression, cryptography, you can build GUIs with Tcl/Tk, and these are just a few of the built-in features.
If you look around on the Internet, you’ll be surprised how many applications and libraries have Python bindings: MySQL, ImageMagick, SVN, Qt, libXML, and so on.
There are applications that provide plug-in interface through Python, like Blender and GIMP.
In case this is not enough for you, you can write extension modules for Python in C or C++ using the Python/C API, or in the reverse case, you can use the Python interpreter as a module of your native application, that is, you can embed Python into your software.

Python is widely used by serious people for serious purposes. I just list some of the more well-known names here: NASA, Industrial Light and Magic, Philips, Honeywell, TTTech.
You can find several other success stories on Python’s website.

Python is available for any platform you can imagine: Linux, Solaris, Windows, Mac, AIX, BeOS, OS/2, DOS, QNX, or PlayStation, for example. This is very good, since one you have a running code in Python, since once you need the same on another platform, you don’t even have to touch it, and it just runs. And believe me, this is a very important aspect whenever you use it in you work. You just never know when you’ll be asked whether you code runs on the X platform too.

If I succeeded in convincing you that learning Python is a good investment, go on, I will be happy to introduce it for you in fast and straightforward way.

In the following, I assume that you already know at least one programming language, so you know what a variable and a loop is. That is, it is intended for people who would like to learn Python as a second, third, etc. programming language.

For testing the examples, I will use Python version 2.5. But in case you have a somewhat older version, don’t worry, the examples are going to run on older versions too (2.3, 2.4), or if not, I will remind you beforehand.

Setting up your programming environment

To start Python programming, all you need is a text editor (if it has syntax highlighting for Python, that’s even better), and the Python interpreter, which is usually installed with today’s Linux distributions. If not, you just grab one from www.python.org or look for it in your package manager.

Of course, you can have more than a simple text editor. We all know that a handy Integrated Development Environment (IDE) can make life a lot easier. You can use PyDEV for Eclipse, Emacs, Komodo (a full Python IDE from ActiveState), or Vim, just to name a few.

Getting started

As I don’t want to take your time with theory, syntax or anything like that, let’s get started with some practice right away. Some people say that once you can code hello world in a programming language, you are very close to be able to do anything you want. So here it goes:

#!/bin/python
print "Hello, world!"

Just enter these lines to a file, give the file a chmod +x, and run!
The first line specifies the interpreter to use to run the following code, as usual. In the second line, we send our greetings, and since nothing else follows, we just quit the program afterwards.

Staying for a second with print, I show you a very useful little feature, multi-line printing, which can be used for printing file headers, how-to-use texts, or any kind of multi-line blocks. You start the block with triple quotes, and finish it the same way.

[Read more..]

Courtesy : Linuxforums.org