Python first glance

Posted on November 13, 2011

0


Python is a high level interpreted language designed to be very simple to use. It is object oriented and since it is interpreted it is also said to be multiplatform (as long as the python interpreter exists for any given platform of course). The phylosophy is to always have a clean syntax in favor of a readable code.

This language was written by Guido van Rossum in the Cetrum Wiskunde & Informatica (CWI). It is managed by the Python Software Foundation and it has an open source lincense calle Python Software Foundation License which is compatible with the GPL since the 2.1.1 version.

About the data types it has the typical elemental types like int, float, bool and long, and then for strings the str and unicode. What makes it a little bit different is the existence of some more compound types like list, tuple and dictionaries.

So looking a bit of the interactive mode of phython (for which I would recommend bpython just to try it for a while), it has what all programming languages have for example
>>> print “Hello World”
Hello World

>>> 2 + 2
4

it has some singular things about the syntax like the quotes and double quotes that are the same, and in general there is some more syntactic sugar and that’s what makes it well known.
>>> “double quote” == ’double quote’
True

The operand use to concatenate is the + so
>>> “Hello” + “World”
’HelloWorld’

then we have the lists that are a sequences of homogeneous structures so for example we have the following
>>> a = [66.25, 333, 333, 1, 1234.5]
>>> print a.count(333), a.count(66.25), a.count(‘x’)
2 1 0
>>> a.insert(2, -1)
>>> a.append(333)
>>> a
[66.25, 333, -1, 333, 1, 1234.5, 333]
>>> a.index(333)
1
>>> a.remove(333)
>>> a
[66.25, -1, 333, 1, 1234.5, 333]
>>> a.reverse()
>>> a
[333, 1234.5, 1, 333, -1, 66.25]
>>> a.sort()
>>> a
[-1, 1, 66.25, 333, 333, 1234.5]

there also are the tuples which are heterogeneous data structures but unlike lists they are immutable. This data structure could be used for example for coordinate pairs, or for records from a database.
>>> t = (1,2)
>>> t[0]
1

>>> mytuple = ‘Brian’, a, 14,
>>> len(mytuple)
3

And finaly there are the dictionaries data type which are like kind of a mapping table or maybe more like a hash table from other programming languages, but the dictionaries are indexed by keys which could be of any type. So you can think of a dictionary as set of keys where the keys are unique. For example we have

>>> rooms = { ’sara’ : 16, ’anthony’ : 18 }
>>> rooms[’sara’]
120
>>> rooms.keys()
[’sara’, ’anthony’]
>>> del rooms[‘sara’]

Posted in: Uncategorized