introduccio a python
Post on 14-Sep-2014
692 Views
Preview:
DESCRIPTION
TRANSCRIPT
Introducció a PythonCopyleft 2012 Santi Camps
#bbmnk Betabeers Menorca
Copieu I compartiu aquesta presentació tant com vulgueu .. per favor
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
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 !!
Diverses entorns
cPythonjPythonIronPython...DjangoGoogle Applications DevelopmentWeb.pyPyramidTryton
Casos d'èxit
YoutubeDropboxInstagramPinterestMozilla FundationRedditOpenERPGoogle te 3 llenguages recomanats: C++, Java i 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
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)
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))
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))
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()
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()
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)
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
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/
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
Life is Short(You Need Python)
top related