introduccio a python

16
ntroducció a Python Copyleft 2012 Santi Camps #bbmnk Betabeers Menorca Copieu I compartiu aquesta presentació tant com vulgueu .. per favor

Post on 14-Sep-2014

691 views

Category:

Technology


3 download

DESCRIPTION

Breu introducció a python per a betabeers Menorca

TRANSCRIPT

Page 1: Introduccio a python

Introducció a PythonCopyleft 2012 Santi Camps

#bbmnk Betabeers Menorca

Copieu I compartiu aquesta presentació tant com vulgueu .. per favor

Page 2: Introduccio a python

Llenguatge de programació creat per Guido Van Rossum

És software lliure

És multiplataforma (quasi totalment)

És interpretat o pseudo-compilat (psyco)

Combina el millor d'un llenguatge de script amb el millor de l'orientació a objectes

Aconsegueix productivitats 5-10 superiors a altres llenguatges

Page 3: Introduccio a python

Usos: the glue language

Scripts de sistema

Serveis i pàgines web

Software de baix nivell

Daemons

Protocols d'intercanvi

Inteligència Artificial

Software d'escriptori

Absolutament de tot !!

Page 4: Introduccio a python

Diverses entorns

cPythonjPythonIronPython...DjangoGoogle Applications DevelopmentWeb.pyPyramidTryton

Page 5: Introduccio a python

Casos d'èxit

YoutubeDropboxInstagramPinterestMozilla FundationRedditOpenERPGoogle te 3 llenguages recomanats: C++, Java i python !!

Page 6: Introduccio a python

Sintaxi i altres rareses

Indentació per llei:barca = 5

madrid = 1

if (barca > madrid):

print("Barca guanya de %i" % (barca - madrid))

Tipus de dades bàsics:int / long / float

string: suport unicode i implementat com a class

booleans

classes

Page 7: Introduccio a python

Tipus de dades extranys

Llistes: l = [1, 4, 9, 4]

print l[2]

Tuples: t = (3, 4, "beer")

print t[2]

Diccionaris:

d = {'uid': 2, 'name': 'betabeers menorca'}

print d['name']

Sets:print set(l)

Page 8: Introduccio a python

Classes i Objectes

class beer(object):

def __init__(self, alc, name):

self.alc = alc

self.name = name

def __str__(self):

return "%s with %0.2f %% of alcohol" % (self.name, self.alc)

b1 = beer(0.0, "San Miguel 0 0")

b2 = beer(5.4, "Sant Climent Brown Ale")

print "What do you prefer, %s or %s ?" % (str(b1), str(b2))

Page 9: Introduccio a python

Herència

class damm(beer):

base_name = " Damm"

def __init__(self, alc, name=''):

self.alc = alc

self.name = name + self.__class__.base_name

b1 = damm(4.8, "Estrella")

b2 = beer(5.4, "Sant Climent Brown Ale")

print "What do you prefer, %s or %s ?" % (str(b1), str(b2))

Page 10: Introduccio a python

Control d'Errors

# Sense control

b3 = beer("Volldamm", 6.5)

str(b3)

# Amb control d'errors

try:

b3 = beer("Volldamm", 6.5)

str(b3)

except TypeError:

print "Invalid type"

import pdb; pdb.set_trace()

Page 11: Introduccio a python

Dinamisme

def producer(self):

if hasattr(self.__class__, 'base_name'):

return self.name + ' is produced by ' + self.__class__.base_name

else:

return self.name + ' producer is unknown'

beer.producer = producer

print b1.producer()

print b2.producer()

Page 12: Introduccio a python

Velocitatimport random, time

llist = [random.random()*100000 for i in range(1000000)]

t1 = time.time()

maxitem = 0

for item in llist:

if item > maxitem:

maxitem = item

print "Found %i in %0.4f secs" % (maxitem, time.time() - t1)

t1 = time.time()

maxitem = max(llist)

print "Found %i in %0.4f secs" % (maxitem, time.time() - t1)

Page 13: Introduccio a python

Mòduls SINTAXI D'IMPORTACIOSINTAXI D'IMPORTACIO

import nom_modul

from nom_modul import funcio, classe, submodul

from nom_modul.submodul import funcio, classe, submodul

SINTAXI DE DEFINICIO: fitxer __init__.py

PYTHONPATH

MODULS DISPONIBLES: os, xml, json, datetime, md5, urllib, calendar, locale, pickle ... més tot un món instalables

Page 14: Introduccio a python

Documentació

http://docs.python.org/release/2.6.5/

http://docs.python.org/release/2.6.5/modindex.html

http://www.diveintopython.net/

https://docs.djangoproject.com/en/1.4/

Page 15: Introduccio a python

Cas pràctic

Crear un servidor que escolti pel port 6666 i que converteixi a majúscules el que rebi i li afegeixi el nom de cadascú

Crear un client que li envïi peticions al servidor

Cridar-nos entre noltros

Page 16: Introduccio a python

Life is Short(You Need Python)