SlideShare a Scribd company logo
Introducció a Python
Copyleft 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
cPython
jPython
IronPython
...
Django
Google Applications Development
Web.py
Pyramid
Tryton
Casos d'èxit
 Youtube
 Dropbox
 Instagram
 Pinterest
 Mozilla Fundation
 Reddit
 OpenERP
 Google 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()
Velocitat
import 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'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)

More Related Content

Viewers also liked

Python and MongoDB
Python and MongoDB Python and MongoDB
Python and MongoDB
Norberto Leite
 
A Folksonomy of styles, aka: other stylists also said and Subjective Influenc...
A Folksonomy of styles, aka: other stylists also said and Subjective Influenc...A Folksonomy of styles, aka: other stylists also said and Subjective Influenc...
A Folksonomy of styles, aka: other stylists also said and Subjective Influenc...
Natalia Díaz Rodríguez
 
Python 101
Python 101Python 101
Python 101
Julián Perelli
 
Volunteering assistance to online geocoding services through a distributed kn...
Volunteering assistance to online geocoding services through a distributed kn...Volunteering assistance to online geocoding services through a distributed kn...
Volunteering assistance to online geocoding services through a distributed kn...
José Pablo Gómez Barrón S.
 
Python Dominicana 059: Django Migrations
Python Dominicana 059: Django MigrationsPython Dominicana 059: Django Migrations
Python Dominicana 059: Django Migrations
Rafael Belliard
 
Madrid SPARQL handson
Madrid SPARQL handsonMadrid SPARQL handson
Madrid SPARQL handson
Victor de Boer
 
BDD - Test Academy Barcelona 2017
BDD - Test Academy Barcelona 2017BDD - Test Academy Barcelona 2017
BDD - Test Academy Barcelona 2017
Carlos Ble
 
TDD in the Web with Python and Django
TDD in the Web with Python and DjangoTDD in the Web with Python and Django
TDD in the Web with Python and Django
Carlos Ble
 
Zotero
ZoteroZotero
El arte oscuro de estimar v3
El arte oscuro de estimar v3El arte oscuro de estimar v3
El arte oscuro de estimar v3Leonardo Soto
 
Presentacion scraping
Presentacion scrapingPresentacion scraping
Presentacion scraping
Jose Mussach Gomez
 
PyQgis gpul-lab Univerisity of A Coruña 20160413
PyQgis gpul-lab Univerisity of A Coruña 20160413PyQgis gpul-lab Univerisity of A Coruña 20160413
PyQgis gpul-lab Univerisity of A Coruña 20160413
Luigi Pirelli
 
Charla mspba
Charla mspbaCharla mspba
Charla mspba
Julián Perelli
 
Guía de Python
Guía de Python Guía de Python
Guía de Python
Lennys Camargo
 
Geospatial and MongoDB
Geospatial and MongoDBGeospatial and MongoDB
Geospatial and MongoDB
Norberto Leite
 
Bucles con Scratch
Bucles con ScratchBucles con Scratch
Bucles con Scratch
Fco Javier Lucena
 

Viewers also liked (19)

Python and MongoDB
Python and MongoDB Python and MongoDB
Python and MongoDB
 
A Folksonomy of styles, aka: other stylists also said and Subjective Influenc...
A Folksonomy of styles, aka: other stylists also said and Subjective Influenc...A Folksonomy of styles, aka: other stylists also said and Subjective Influenc...
A Folksonomy of styles, aka: other stylists also said and Subjective Influenc...
 
Tidy vews, decorator and presenter
Tidy vews, decorator and presenterTidy vews, decorator and presenter
Tidy vews, decorator and presenter
 
Python 101
Python 101Python 101
Python 101
 
Volunteering assistance to online geocoding services through a distributed kn...
Volunteering assistance to online geocoding services through a distributed kn...Volunteering assistance to online geocoding services through a distributed kn...
Volunteering assistance to online geocoding services through a distributed kn...
 
Python Dominicana 059: Django Migrations
Python Dominicana 059: Django MigrationsPython Dominicana 059: Django Migrations
Python Dominicana 059: Django Migrations
 
Madrid SPARQL handson
Madrid SPARQL handsonMadrid SPARQL handson
Madrid SPARQL handson
 
BDD - Test Academy Barcelona 2017
BDD - Test Academy Barcelona 2017BDD - Test Academy Barcelona 2017
BDD - Test Academy Barcelona 2017
 
TDD in the Web with Python and Django
TDD in the Web with Python and DjangoTDD in the Web with Python and Django
TDD in the Web with Python and Django
 
Zotero
ZoteroZotero
Zotero
 
El arte oscuro de estimar v3
El arte oscuro de estimar v3El arte oscuro de estimar v3
El arte oscuro de estimar v3
 
The emerging world of mongo db csp
The emerging world of mongo db   cspThe emerging world of mongo db   csp
The emerging world of mongo db csp
 
Presentacion scraping
Presentacion scrapingPresentacion scraping
Presentacion scraping
 
Grunt.js introduction
Grunt.js introductionGrunt.js introduction
Grunt.js introduction
 
PyQgis gpul-lab Univerisity of A Coruña 20160413
PyQgis gpul-lab Univerisity of A Coruña 20160413PyQgis gpul-lab Univerisity of A Coruña 20160413
PyQgis gpul-lab Univerisity of A Coruña 20160413
 
Charla mspba
Charla mspbaCharla mspba
Charla mspba
 
Guía de Python
Guía de Python Guía de Python
Guía de Python
 
Geospatial and MongoDB
Geospatial and MongoDBGeospatial and MongoDB
Geospatial and MongoDB
 
Bucles con Scratch
Bucles con ScratchBucles con Scratch
Bucles con Scratch
 

Introduccio a python

  • 1. Introducció a Python Copyleft 2012 Santi Camps #bbmnk Betabeers Menorca Copieu I compartiu aquesta presentació tant com vulgueu .. per favor
  • 2. 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
  • 3. 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 !!
  • 5. Casos d'èxit Youtube Dropbox Instagram Pinterest Mozilla Fundation Reddit OpenERP Google te 3 llenguages recomanats: C+ +, Java i python !!
  • 6. 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
  • 7. 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)
  • 8. 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))
  • 9. 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))
  • 10. 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()
  • 11. 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()
  • 12. Velocitat import 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)
  • 13. Mòduls SINTAXI 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
  • 15. 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
  • 16. Life is Short (You Need Python)