pycrashcourse4.0 pdfjam

15
Introducing Python by Example Enrico Franchi [email protected] 1

Upload: rik0

Post on 31-May-2015

398 views

Category:

Technology


0 download

DESCRIPTION

Introducing python with short example and a nice evolutive game from Land of Lisp

TRANSCRIPT

Page 1: Pycrashcourse4.0 pdfjam

Introducing Python by Example

Enrico [email protected]

1

Page 2: Pycrashcourse4.0 pdfjam

Outline

Very (very )* short introduction to Python

Some small examples

Evolutionary game

2

Page 3: Pycrashcourse4.0 pdfjam

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

Page 4: Pycrashcourse4.0 pdfjam

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

Page 5: Pycrashcourse4.0 pdfjam

READABILITYCOUNTS

Zen of Python

5

Page 6: Pycrashcourse4.0 pdfjam

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

Page 7: Pycrashcourse4.0 pdfjam

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

Page 8: Pycrashcourse4.0 pdfjam

Hello, world!

print  “Hello,  world!”

8

Page 9: Pycrashcourse4.0 pdfjam

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

Page 10: Pycrashcourse4.0 pdfjam

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

Page 11: Pycrashcourse4.0 pdfjam

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

Page 12: Pycrashcourse4.0 pdfjam

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

Page 13: Pycrashcourse4.0 pdfjam

Evolution!

https://github.com/rik0/isle

13

Page 14: Pycrashcourse4.0 pdfjam

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

Page 15: Pycrashcourse4.0 pdfjam

UIhttps://github.com/rik0/isle 15