Cognitionis
The little I know

Python Basic Kit


Python is a interpreted, general-purpose, high-level programming language. Its design philosophy emphasises programmer productivity and code readability. Python’s core syntax and semantics are minimalistic, while the standard library is large and comprehensive. Python supports multiple programming paradigms (primarily object oriented, imperative, and functional) and features a fully dynamic type system and automatic memory management, similar to Perl or Ruby. Like other dynamic languages, Python is often used as a scripting language. Python was first released by Guido van Rossum in 1991. The language has an open, community-based development model managed by the non-profit Python Software Foundation, which also maintains the de facto standard definition of the language in CPython, the reference implementation. The name comes from Monty Python.

Version history

2.5, 2.6 Old but most used versions.

3.0 Breaks backward compatibility to solve many language problems including treatment of unicode (string, ustring). This version is released with a 10% lower performance but will be improved in next releases.

lxml is a Pythonic binding for the libxml2 and libxslt libraries.

Advantages: easy to learn, short to write, easy to read code.

Disadvantages: low performance

Example script

#!/usr/bin/python

# import needed paquets, clases or modules (its all the same for python)
# in this example system and regular expresion modules are imported

import sys,re

print “Hello World!”

# read standard input into a string
s=sys.stdin.read()

# compile a regular expression

p = re.compile(r’\b[0-9]+([.,][0-9]+)*\seuro[s]?\b’, re.I)

# the first r is for raw read of the expression (use regular esacpe)
# \b is for whole word only recognition
# \s is equivalent to POSIX [[:space:]]
# re.I is to ignore case

# Pattern search and replace (substitution)
a=p.sub(r”\g<1>”,s)

To create separate modules, paquets or classes you only need to write another file containing whatever.

Then the name of the module to import is the name of the file without .py extension.

(import samplemodule)

To use functions or variables of the importet module just write: samplemodule.function() or samplemodule.var