Python overview

Swobodin's picture
Submitted by Swobodin on Tue, 2006-05-16 12:33. ::
This week I have been leaning Python in order to set a Wikipedia robot; it differs so much from other C like languages, but it's powerful and its syntax is interesting.
Python is a free interpreted, interactive, object-oriented programming language  that  combines  remarkable power with very clear syntax.
You may start python in interactive mode; however, to create a solid program, it's better to use a script file
First line, like in UNIX script, must indicate the path to the executable, you may specify at the second line the charset (commented)
#!/usr/bin/python
## -*- coding: utf-8 -*-

A simple "Hello world" output is like the following
myvar="Hello world" # Variable declaration
print myvar+"\n" # Concatination is represented by a plus (+) symbol

Instruction is ended either by a semi-colon (;) or by a break line
The amazing thing is the start / end of instructions within "if", "while" and "for" statements, which is represented by a Tab!
Here is an example in both C and Python
C:
int a=5, b=7;
for (int i=0; i < b ; i++) {
        while (1) {
                b+=;   
                if (b == 100) {
                        break; 
                }

Python:
a, b = 5, 7;
for i in range (0, b):
    while (True): #instruction start by a Tab
        b = b+1 # Another tab
        if b == 100:
            break #Another tab!
# No more instruction in loop


Another example, to get the MD5 hash of a string, you have to import the module
import md5
m=md5.new("Your hash")
encoded = m.hexdigest() #hexdigest return void
print encoded

Another intersing feature: You may compile your code and make source unavailable by python itself!
You want to compile the file compiled.py , open another file
import compiler
compiler.compileFile("compiled.py")

Done, you should have obtained a file called compiled.pyc
This is just an overview, to find out more about the powerful language, check out the official site where you can get the program updates, documentation, tutorials, ...
http://python.org