pycrashcourse4.0 pdfjam
DESCRIPTION
Introducing python with short example and a nice evolutive game from Land of LispTRANSCRIPT
Outline
Very (very )* short introduction to Python
Some small examples
Evolutionary game
2
Parlando del futuro...We will perhaps eventually be writing only small modules that are identified by name as they are used to build larger ones, so that devices like indentation, rather than delimiters, might become feasible for expressing local structure in the source language.
Donald E. Knuth, Structured Programming with go to Statements, 1974
3
Introduzione
Python è concepito da Guido van Rossum alla fine degli anni ‘80 per Amoeba
Pubblico 1991, stabile 1994.
Linguaggio di alto livello ed orientato agli oggetti.
Utilizzato per programmazione di sistema e di rete, e calcolo scientifico, applicazioni desktop, integrazione di videogiochi, MMORPG backend, ...
Si impone in ambito web/enterprise, con soluzioni come Zope/Plone, Django,Twisted, GAE, OpenERP.
4
READABILITYCOUNTS
Zen of Python
5
Oggetti in Python
In Python tutto è un oggetto:
Un numero, una stringa sono oggetti
Gli oggetti sono oggetti (ehm...)
Una funzione è un oggetto
Una classe è un oggetto
Gli oggetti sono cittadini di prima classe, possiamo manipolarli riccamente e comodamente (introspezione, etc.)
Possiamo fare, in definitiva, tutto
6
Tipizzazione in Python
Python è un linguaggio ad oggetti a tipizzazione dinamica e forte
Tipizzazione forte:
Gli errori di tipo sono sempre generati. Es. Stringhe non diventano interi e viceversa
Ogni oggetto ha una classe, questa non cambia
Tipizzazione dinamica
Gli errori di tipo sono generati a runtime
Duck typing
7
Hello, world!
print “Hello, world!”
8
Dettagli implementativi
Tipicamente Python viene compilato a byte-code e questo viene interpretato da una macchina virtuale (come Java)
Diversamente da Java la compilazione è trasparente per l’utente
Possiamo anche usare l’interprete interattivo
$ cat hello.py #!/usr/bin/python
print "Hello, world!"
$ python hello.py Hello, world!$ chmod 755 hello.py $ ./hello.py Hello, world!$ python Python 2.5.1 (...) ...>>> print "Hello, world"Hello, world
9
Interprete interattivo
L’interprete interattivo ufficiale ha come prompt >>>
Scriviamo comandi (statements) che vengono byte-compilati ed eseguiti
Se il comando valuta in un espressione (es. un expression statement), l’espressione viene stampata
>>> import os>>> print “foo”foo>>> os.getcwd()“/Users/enric/pycourse”>>> import sys>>> sys.stdout.write(“ciao\n”)ciao>>> def f(a): ... sys.stdout.write(a)... return a... >>> f(“ciao\n”)ciao“ciao\n”
10
Esempio 01:System Scripting
import osimport shutil
for fname in os.listdir(os.getcwd()): if fname.endswith(('pyc', 'pyo')): os.remove(fname) elif fname.endswith('py'): shutil.copy(fname, fname + '.bak')
11
Esempio 2:“semplice wget” (GvR)
import sys import urllib import os
def hook(*a): print '%s: %s' % (fn, a)
for url in sys.argv[1:]: fn = os.path.basename(url) print url, "->", fn urllib.urlretrieve(url, fn, hook)
12
Evolution!
https://github.com/rik0/isle
13
Animals
0 1 2
7
6 5
M 3
4
+ move() : void+ turn() : void+ eat(plants : cell[0..]) : void+ reproduce() : Animal[0..1]
+ x : int+ y : int+ energy : int+ dir : int+ genes : int[8]
Animal
01234567
11
1011111
Dir Gene
https://github.com/rik0/isle
14
UIhttps://github.com/rik0/isle 15