SlideShare a Scribd company logo
An introduction to Python
                             The language



                               Marcelo Araujo
                               Jul 2012, Taipei
                               marcelo@qnap.com
Monday, July 16, 12
{ Goals of this talk?




      ! A brief introduction of Python language.
       ! Get you interested in learning Python.
        ! Shows that it is a powerful language.




                                             And of course.....
Monday, July 16, 12
...make you comfortable with PYTHON, like this guy!



Monday, July 16, 12
Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>>    What we gonna see?

     ! What is Python?
     ! Who uses Python?
     ! Interactive prompt, memory, basic syntax, data types, strings,
     flow control statements, functions, classes, exceptions, importing,
     file I/O, list, tuple, dictionary, cast, help, dir.
     ! CPython, Pypy, Jython and IronPython.
     ! Python Frameworks.
     ! Your first Python program.


Monday, July 16, 12
{ What is Python?
      Python is a remarkably powerful dynamic programming
      languate that is used in a wide variety of application
      domains.


      ! Very clear, readable syntax.
       ! Strong introspection capabilities.
        ! Intuitive object orientation.
         ! Natural expression of procedural code.
          ! Full modularity, supporting hierarchical packages.
           ! Exception-based error handling.
            ! Very high level dynamic data types.
             ! Extensive standard libraries and third party modules.
              ! Extensions and modules easily written in C, C++ or (Java or .NET).
               ! Embeddable within applications as a scripting interface.

Monday, July 16, 12
{ Who created Python?

   ! Guido van Rossum.
    ! Python was released early of 1990s.
     ! Based on: ABC, C, Bourne Shell, Modula-3,
      Perl, Haskell and Lisp.
      ! Currently he works for Google.
       ! ... Half of his working time is to improve
        Python.


    ! Python is not related with the snake, but yes with a British show
     called Monty Python’s Flying Circus.


Monday, July 16, 12
{ Who uses Python?




Monday, July 16, 12
{ The interactive python

     ! Python has an interactive prompt that able you write code.
      ! You can obtain help.
       ! Have access to the docs.
        ! Test your code and ideas any time.




Monday, July 16, 12
{ The interactive python

       Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05)
       [GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
       Type "help", "copyright", "credits" or "license" for more information.
       >>> import            this


                                                   The ZEN of Python by Tim Peters.
                                                              PEP - 020.
                                                  It is the principles of Python




   *PEP - Python Enhancement Proposal.
   http://www.python.org/dev/peps/
Monday, July 16, 12
{ The interactive python

    Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05)
    [GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> name = 'Marcelo'
    >>> fname = “Araujo”
    >>> age = 31
    >>> print 'My name is ', name, fname, ', I'm %i years old' % (age)
    My name is Marcelo Araujo , I'm 31 years old
    >>> print “I’d like be %i years old” % (age - 9)
    I’d like be 22 years old
    >>> type(name)
    <type ‘str’>
    >>> type(fname)
    <type ‘str’>
    >>> type(age)
    <type ‘int’>




Monday, July 16, 12
{ Reserved words.

                      and        del       from     not      while
                      as         elif      global   or       with
                      assert     else      if       pass     yield
                      break      except    import   print
                      class      exec      in       raise
                      continue   finally   is       return
                      def        for       lambda   try




                                       Like in any other computer
                                       language.



Monday, July 16, 12
{ The interactive python - Variable
       ! The first assignment to a variable creates it.
        ! Variable types don’t need to be declared.
         ! Python figures out the variable types on its own.
          ! Python has a garbage collector.

       >>> name = ‘Marcelo’
       >>> type(name), id(name)
       (<type 'str'>, 4483079264)
       >>> memoryview(name)
       <memory at 0x10b3d3f28>
       >>> del name
       >>> print name
       Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
       NameError: name 'name' is not defined




Monday, July 16, 12
{ The interactive python - Memory
     >>> a = ‘marcelo’
     >>> b = a                                     a = ‘marcelo’
     >>> id(a), id(b)
     (4482266376, 4482266376)                name                  obj
     >>> memoryview(a), memoryview(b)
     (<memory at 0x10b3d3f28>, <memory at 0x10b3d3e90>)
     >>> a = 'araujo'
     >>> id(a), id(b)
     (4483897624, 4482266376)
     >>> memoryview(a), memoryview(b)
     (<memory at 0x10b3d3f28>, <memory at 0x10b3d3e90>)




                                                             }
                                           buffer method()

                                        0x10b3d3f28
                                        0x10b928050                Mem
                                        0x10b3d3e90

Monday, July 16, 12
{ The interactive python - Memory
     >>> a = ‘marcelo’
     >>> b = a                                     a = ‘marcelo’
     >>> id(a), id(b)
     (4482266376, 4482266376)                name                  obj
     >>> memoryview(a), memoryview(b)
     (<memory at 0x10b3d3f28>, <memory at 0x10b3d3e90>)
     >>> a = 'araujo'
     >>> id(a), id(b)
     (4483897624, 4482266376)
     >>> memoryview(a), memoryview(b)
     (<memory at 0x10b3d3f28>, <memory at 0x10b3d3e90>)




                                                             }
                      OID                  buffer method()

         a                              0x10b3d3f28
                      OID               0x10b928050                Mem
                                        0x10b3d3e90
          b
Monday, July 16, 12
{ Basic syntax
      1 #!/bin/env python
      2 # -*- coding: utf-8 -*-
      3
      4 class Name:                              Class Name
      5
      6 def __init__(self, name=None):           Method and attribute
      7       self.name = name                   Attribute
      8
      9
     10 class FName(Name):


                                      }
     11
     12 def __init__(self, fname=None):
                                                 Indentation Style
     13        self.fname = fname
     14
     15 def output(self):
     16        print self.name, self.fname
     17


                                             }
     18 if __name__ == '__main__':
     19 inst_name = FName(fname='Araujo')         main()
     20 inst_name.name = 'Marcelo'
     21 inst_name.output()


Monday, July 16, 12
{ Basic syntax - Indentation
    ! Indentation is important.
     ! No brackets. {}
      ! No dot and comma. ;
           >>> a = 1
           >>> b = 2
           >>> if a > b:
           ... print 'a is bigger than b'
           ... else:
           ... print 'b is bigger than a'
           ...
           b is bigger than a




                      Code readable and more elegant.


Monday, July 16, 12
{ Basic data types

            Numbers: int, long, float, complex.
            Strings: str and unicode.
            List and Tuples: list and tuple.
            Dictionary: dict
            Files: file
            Boolean: bool(True, False)
            Sets: set, frozenset
            Null: None


  Note: int usually 32bits, long all your memory,
        float usually 32bits.


Monday, July 16, 12
{ Basic data types

       ! It is possible to cast types.
                                             >>> a = 1      >>> a = float(a)
                                             >>> type(a)    >>> type(a)
                                             <type 'int'>   <type 'float'>

       Operators
           ! Arithmetic.                       ! Logical.
               ! +, -, *, /, %, ** and //          ! and, or, not
            ! Comparison.                       ! Membership.
                ! ==, !=, <>, >, <, >=, <=          ! in, not in
             ! Assignment.                       ! Identity.
                 ! =, +=, -=, *=, /=, %=,            ! is, is not
                  **= , //=
              ! Bitwise.
                  ! &, |, ^, ~, <<, >>
Monday, July 16, 12
{ Basic data types - Strings

       ! String is powerful in Python. (Immutable)
              >>> a = 'Marcelo'
              >>> print a[0]
              'M'
              >>> a[0] = 'm'
              Traceback (most recent call last):
               File "<stdin>", line 1, in <module>
              TypeError: 'str' object does not support item assignment

       ! String has a set of METHODS.
              >>> print a.upper(), a.lower(), a.partition('c')
              MARCELO marcelo ('Mar', 'c', 'elo')
              >>> a.replace('c', 'C')
              'MarCelo'

       ! How change the string?
              >>> a = 'Marcelo'         >>> a = 'm' + a[1:]
              >>> id(a)                 >>> print a
              4483079264                marcelo
                                        >>> id(a)
                                        4483500336
Monday, July 16, 12
{ Basic data types - Special Types.
       ! Lists. (Mutable)
              >>> a = [1, 2 3, 4, 5]
              >>> print a
              [1, 2, 3, 4, 5]
              >>> a[0] = 0
              >>> print a
              [0, 2, 3, 4, 5]

       ! Tuples. (Immutable)
              >>> a = (1, 2, 3, 4, 5, 6)
              >>> a[0] = 0
              Traceback (most recent call last):
               File "<stdin>", line 1, in <module>
              TypeError: 'tuple' object does not support item assignment


       ! Dictionary. (Mutable only the ITEM)
              >>> a = {'Name' : 'Marcelo', ‘Age’ : 31}
              >>> print a['Name']
              Marcelo

Monday, July 16, 12
{ Flow control statement

   if guess == number:         while True:
       # do something             # do something
   elif guess < number:           # break when done
       # do something else        break
   else:                       else:
       # do something else        # do something in the end

   for i in range(1, 10):       for i in range(1, 10):
      # do something               # do something
      print i                      if i == 5:
   else:                                pass
      # do something else in       else:
      # the end                         print i

Monday, July 16, 12
{ More about List.
       ! List sort using “for”.               Methods
                1 a = [1, 3, 5, 4, 2]
                2 b = [0 for i in range(5)]    a.append()
                3 size_a = len(a)
                4                              a.pop()
                5 for i in range(size_a):      a.remove(<value>)
                6 print i, a[i]
                7 b[a[i] - 1] = a[i]           a.index(<value>)
                8                              a.count(<value>)
                9 print a
               10 print b

       ! Sort the list in a python way.       dir() and help()
               >>> a = [1, 3, 5, 2, 4]
               >>> a.sort()                     >>> dir(list)
               >>> print a                      >>> help(list)
               [1, 2, 3, 4, 5]




Monday, July 16, 12
{ More about List.
       ! Different than string, list, tuple and dictionary point to the
        same OBID.
                                             >>> import copy
              >>> a = [1, 2, 3]
                                             >>> a = [1, 2, 3]
              >>> b = a
                                             >>> b = copy.copy(a)
              >>> a.append(4)
                                             >>> a.append(4)
              >>> print a, b
                                             >>> print a, b
              [1, 2, 3, 4] [1, 2, 3, 4]
                                             [1, 2, 3, 4] [1, 2, 3]
              >>> id(a), id(b)
                                             >>> id(a), id(b)
              (4529798896, 4529798896)
                                             (4528943688, 4529135344)



       ! List full fill.                      ! List comprehension.
              >>> a = []                        >>> a = [i for i in range(1,6)]
              >>> for i in range(1,6):          >>> print a
              ... a.append(i)                   [1, 2, 3, 4, 5]
              ...
              >>> print a
              [1, 2, 3, 4, 5]



Monday, July 16, 12
{ More about Dictionary.
       ! Dictionary is useful, we have a key for a specific item.
              >>> register = {'Name' : 'Marcelo', 'Age' : 31, 'Dep' : 'R&D'}
              >>> print register
              {'Dep': 'R&D', 'Age': 31, 'Name': 'Marcelo'}


       ! Access the dictionary.
              >>> for key, item in register.iteritems():
              ... print key + ':' + 't' + str(item)
              ...
              Dep:	

 R&D
              Age:	

 31
              Name:	

 Marcelo




              Note: Key is an unique value and immutable.


Monday, July 16, 12
{ Function/Method
     ! Function is a block of organized, reusable code, that perform
      a single related action.
      ! Better modularity for your application.
       ! Python gives you many built-in functions like print().



           Ex: 1                                  Ex: 2
            >>> def say_hello(name=None):         >>> def say_bye(name):
            ... if name == None:                  ... print 'Good bye %s' % (name)
            ...       print 'Hello nobody!'       ...
            ... else:                             >>> say_bye()
            ...       print 'Hello %s' % (name)   Traceback (most recent call last):
            ...                                      File "<stdin>", line 1, in <module>
            >>> name = 'Marcelo'                  TypeError: say_bye() takes exactly 1
            >>> say_hello(name)                   argument (0 given)
            Hello Marcelo
            >>> say_hello()
            Hello nobody!

Monday, July 16, 12
{ Function/Method
     ! How pass an undefined number of args to a function?
            >>> def hello_all(say=None, *names):
            ... if say == None:
            ...        say = 'Hello'
            ... elif say == 'Morning':
            ...        say = 'Good morning'
            ... else:
            ...        say = 'Aloha'
            ... for name in names:
            ...        print say + ' ' + name
            ...
            >>> name = 'Marcelo'
            >>> name1 = 'Bob'
            >>> name2 = 'Kevin'
            >>> hello_all(‘Aloha’, name, name1, name2)
            Aloha Marcelo
            Aloha Bob
            Aloha Kevin




Monday, July 16, 12
{ Function/Method
     ! We could use return.
           >>> def big_number(n1=0, n2=0):
           ... bigone = None
           ... if n1 > n2:
           ...        bigone = n1
           ... elif n1 < n2:
           ...        bigone = n2
           ... else:
           ...        bigone = 'same'
           ... return bigone
           ...
           >>> answer = big_number(10, 20)
           >>> print answer
           20


         Note: Alway a function return something, if not
         defined, it will return None.

Monday, July 16, 12
{ Classes/Objects - OOP Terminology
   ! Class: A user-defined prototype for an object that defines a
    set of attributes.
    ! Class variable: A variable that is shared by all instances of a
     class.
     ! Function overloading: The assignment of more than one
      behavior to a particular function/method.
      ! Instance variable: A variable that is defined inside a method
       and belongs only to the current instance of a class.
       ! Inheritance: The transfer of the characteristics of a class to
        other classes.
        ! Instance: An individual object of a certain class.
         ! Instantiantion: The creation of an instance of a class.
          ! Method: A special kind of function that is defined in a class.
           ! Object: An unique instance of a data structure.

Monday, July 16, 12
{ Class example
       1 class Employee:                                                           Class Name
       2 """ Common base class for all employees"""
       3 empCount = 0                                                          Global Value of Class
       4
       5 def __init__(self, name, position):                                    Class constructor
       6      self.name = name
       7      self.position = position
       8      Employee.empCount += 1
       9
      10 def displayEmployee(self):                                             NEXT SLIDE (><)
      11       print "Name: ", self.name, "ttPosition: ", self.position
      12
      13 if __name__ == '__main__':
      14 emp1 = Employee("Marcelo", "R&D")                                    Instantiation the class
      15 emp2 = Employee("Bob", "Q&A")
      16
      17 emp1.displayEmployee()
      18 emp2.displayEmployee()
      19
      20 print 'Total Employee: %d' % (Employee.empCount)                   Instance the empCount Obj



Monday, July 16, 12
{ self, self, self, self.....?
     ! “self” is a polemic decision on the project.
      ! It is part of PEP-8.
       ! Do you remember the Python ZEN?
         ! BETTER EXPLICIT THAN IMPLICIT

         Ex: 1                              Ex: 2
                         ↶
          >>> class Person:                 >>> class Person:
          ... def set_name(person, name):   ... def set_name(self, name):
          ...       person.name = name      ...       self.name = name
          ...                               ...
          >>> woman = Person()              >>> woman = Person()
          >>> woman.set_name('Janny')       >>> woman.set_name('Janny')
          >>> print woman.name              >>> print woman.name
          Janny                             Janny




Monday, July 16, 12
{ Class inheritance

         1 class Employee_salary:                                   New Class
         2 def salary(self, value=0):
         3              self.value = value
         4              print "[Salary: %s]" % (self.value)
         5
         6 class Employee(Employee_salary):                          Inheritance
        < ..............code snipped...................>
        18 if __name__ == '__main__':
        19 emp1 = Employee("Marcelo", "R&D")                  Instantiation the class
        20 emp2 = Employee("Bob", "Q&A")
        21
        22 emp1.displayEmployee()
        23 emp1.salary(100)                                   Call method salary()
        24 emp2.displayEmployee()
        25 emp2.salary(200)
        26
        27 print 'Total Employee: %d' % (Employee.empCount)




Monday, July 16, 12
{ How about exception?
     ! It help us to handle situations that disrupts the normal flow
      of the program.
            try:                          >>> try:
                                          ... 10 / 0
               # do something             ... except ZeroDivisionError:
            except:                       ... print "Ooops, invalid."
                                          ... else:
               # do exception             ... print "We're good, no exception!"
            else:                         ... finally:
               # do something else        ... print "We're done with that."
                                          ...
            finally:                       Ooops, invalid.
              # just do                   We're done with that.




Monday, July 16, 12
{ More about exception.

     ! We can check multiples exceptions.

            >>> try:
            ... a / 0
            ... except ZeroDivisionError:
            ... print “Not possible make the division!”
            ... except TypeError:
            ... print “Unsupported type.”
            ... else:
            ... print 'We pass in all exceptions!'
            ... finally:
            ... print 'Do something'
            ...
            Unsupported type.
            Do something




        Built-in Exceptions list:
        http://docs.python.org/library/exceptions.html
Monday, July 16, 12
{ More exception with raise.

     ! Use raise to catch problems.
              >>> def verify(value):
              ... if value == 10:
              ...          print "Wow you have: %s" % (value)
              ...
              >>> try:
              ... verify()
              ... except:
              ... raise
              ...
              Traceback (most recent call last):
                 File "<stdin>", line 2, in <module>
              TypeError: verify() takes exactly 1 argument (0 given)




Monday, July 16, 12
{ More exception with raise.

     ! We can change the error message.
           >>> def verify(value):
           ... if not value.isdigit():
           ...        raise ValueError, "My useful message!"
           ... else:
           ...       return value
           >>> try:
           ... verify('A')
           ... except ValueError:
           ... raise
           ...
           Traceback (most recent call last):
              File "<stdin>", line 2, in <module>
              File "<stdin>", line 3, in verify
           ValueError: My useful message!




Monday, July 16, 12
{ Import/Modules
     ! Every Python code is a module by default.
      ! Modules help you to organize your software.
       ! Python comes with batteries. Powerful standard library.


      STDL:
                      socket, select, SocketServer, BaseHTTPServer, asyncore,
                      asynchat, xmlrpclib, SimpleXMLRPCServer, urllib, httplib,
                      ftplib, smtpd, smtplib, poplib, impalib, json, getopt,
                      optparse, argparse, fileinput, cmd, readline, subprocess,
                      threading, multiprocessing, Queue, anydbm, pickle,
                      shelve, sqlite3 ...... there are more



Monday, July 16, 12
{ Import/Modules

      What I did?
      ! I moved all classes to another file called cemployee.py.
                 1 class Employee_salary:
                 2 def salary(self, value=0):
                 3       self.value = value
                 4       print "[Salary: %s]" % (self.value)
                 5
                 6 class Employee(Employee_salary):
                 7 """ Common base class for all employees"""
                 8 empCount = 0
                 9
                10 def __init__(self, name, position):
                11        self.name = name
                12        self.position = position
                13        Employee.empCount += 1
                14
                15 def displayEmployee(self):
                16        print "Name: ", self.name, "ttPosition: ", self.position


Monday, July 16, 12
{ Import/Modules

      What I did?
      ! Now my employee.py looks like.
             1 import cemployee
             2
             3 if __name__ == '__main__':
             4 emp1 = cemployee.Employee("Marcelo", "R&D")
             5 emp2 = cemployee.Employee("Bob", "Q&A")
             6
             7 emp1.displayEmployee()
             8 emp1.salary(100)
             9 emp2.displayEmployee()
            10 emp2.salary(200)
            11
            12 print 'Total Employee: %d' % (cemployee.Employee.empCount)




Monday, July 16, 12
{ Import/Modules
      ! I can give a friendly name for the module.
           >>> import cemployee as myclass
           >>> emp1 = myclass.Employee("Marcelo", "R&D")
           >>> emp1.displayEmployee()
           Name: Marcelo 	

 	

    Position: R&D

      ! I also can import only some classes from a module.
            >>> from cemployee import Employee_salary as salary
            >>> a = salary()
            >>> a.salary(100)
            [Salary: 100]

      ! More one example.
            >>> import os
            >>> os.system('uname -m')
            x86_64
            0
            >>> from os import system as sys
            >>> sys('uname -m')
            x86_64
            0
Monday, July 16, 12
{ CPython, Jython, IronPython and PyPy?
    Cpython
           ! Used to binding C/C++ code.
                 >>> from ctypes import *
                 >>> libc = CDLL("libc.so")
                 >>> size = c_uint(0)
                 >>> buf = create_string_buffer(size.value)
                 >>> libc.sysctlbyname("kern.hostname", buf, byref(size), None, 0)
                 0
                 >>> print buf.value
                 controllerA.qnap.com

       Jython
           ! Used to binding Java.
            ! You can pack a jar to run over the JVM.
             ! Swing and any other JAVA library can be used.



Monday, July 16, 12
{ CPython, Jython, IronPython and PyPy?
IronPython
           ! Used to binding .NET.
            ! Microsoft have interesting on IronPython.

 PyPy
            ! It is a compliant alternative implementation of the Python
             2.7.2
             ! They claim be faster than CPython.

             More info at: http://speed.pypy.org/




Monday, July 16, 12
{ Python Frameworks?
   Django
           ! High-level Python Web framework.
            ! Rapid development and clean, pragmatic design.
             ! Automatic admin interface.
              ! Cache system.
               ! Template system.
                ! Internationalization by default.
                 ! MTV (Model, Template,View).




Monday, July 16, 12
{ Python Frameworks?
 Web2Py
           ! Inspired by Ruby on Rails.
            ! Focuses on rapid development.
             ! MVC.
              ! Support packed applications.

TurboGears
           ! Scale it out.
            ! SQLAlchemy - Object Relational Mapper

 Twisted Matrix
           ! It is an event-driven networking engine.
            ! It supports many network protocols like: SMTP, POP3, IMAP,
             SSHv2, DNS and so on.

Monday, July 16, 12
{ Python Frameworks?
 Example web-server using Cherrypy:
         1 #!/usr/bin/env python
         2 # -*- coding: utf-8 -*-
         3 import cherrypy
         4
         5 class HelloWorld(object):
         6
         7 def index(self):
         8       return "Hello World!"
         9 index.exposed = True
        10
        11 if __name__ == '__main__':
        12 cherrypy.quickstart(HelloWorld())




Monday, July 16, 12
{ Your first python code.
  The output:



                      sh-3.2# python first.py
                      Our first python code!
                      The result is: 5



Monday, July 16, 12
{ Your first python code.
  Now is time to play:

       a = 10                       print ‘Impossible do that!’

                        finally:         result = a / b

         b=2                                result = None
                              except:
                                                           % (result)
                      print ‘Our first python code!’

         print ‘The result is: %s’                  try:

Monday, July 16, 12
{ Your first python code.
  How it supposed to be?
                       1 a = 10
                       2b=2
                       3 result = None
                       4
                       5 try:
                       6 result = a / b
                       7 except:
                       8 print 'Impossible do that!'
                       9 finally:
                      10 print 'Our first python code!'
                      11
                      12 print 'The result is: %s' % (result)

Monday, July 16, 12
{ Thank you.




              Thanks, have a good weekend guys!




                                        marcelo@qnap.com
Monday, July 16, 12

More Related Content

PDF
Python for text processing
PDF
Python idiomatico
PDF
Python Part 1
PDF
Python Part 2
PDF
Os Goodger
PDF
Python Tutorial
PDF
Learn 90% of Python in 90 Minutes
PDF
An introduction to Python for absolute beginners
Python for text processing
Python idiomatico
Python Part 1
Python Part 2
Os Goodger
Python Tutorial
Learn 90% of Python in 90 Minutes
An introduction to Python for absolute beginners

What's hot (20)

ODP
An Intro to Python in 30 minutes
PDF
Python in 90 minutes
PDF
Introduction to advanced python
PPTX
ODP
OpenGurukul : Language : Python
PDF
PyCon 2013 : Scripting to PyPi to GitHub and More
PDF
Python: an introduction for PHP webdevelopers
PDF
Why Python (for Statisticians)
PDF
AmI 2015 - Python basics
PDF
Meetup di GDG Italia - Leonardo Pirro - Codemotion Rome 2018
PDF
Active Support Core Extensions (1)
PPTX
Python programming
PPTX
Introduction to the basics of Python programming (part 1)
PPTX
Python Workshop - Learn Python the Hard Way
PDF
한국어와 NLTK, Gensim의 만남
PPT
python.ppt
PPT
Python ppt
PDF
The Swift Compiler and Standard Library
PPTX
Basic Python Programming: Part 01 and Part 02
PPTX
Learn python in 20 minutes
An Intro to Python in 30 minutes
Python in 90 minutes
Introduction to advanced python
OpenGurukul : Language : Python
PyCon 2013 : Scripting to PyPi to GitHub and More
Python: an introduction for PHP webdevelopers
Why Python (for Statisticians)
AmI 2015 - Python basics
Meetup di GDG Italia - Leonardo Pirro - Codemotion Rome 2018
Active Support Core Extensions (1)
Python programming
Introduction to the basics of Python programming (part 1)
Python Workshop - Learn Python the Hard Way
한국어와 NLTK, Gensim의 만남
python.ppt
Python ppt
The Swift Compiler and Standard Library
Basic Python Programming: Part 01 and Part 02
Learn python in 20 minutes
Ad

Viewers also liked (7)

PPTX
Y3CDS - Python class 01
PDF
Text analysis using python
PDF
Lecture 8 strings and characters
PDF
Python Programming - III. Controlling the Flow
PPT
Python Introduction
PPTX
Lasso regression
PDF
Ridge regression, lasso and elastic net
Y3CDS - Python class 01
Text analysis using python
Lecture 8 strings and characters
Python Programming - III. Controlling the Flow
Python Introduction
Lasso regression
Ridge regression, lasso and elastic net
Ad

Similar to Python introduction (20)

PPTX
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
PPTX
Python introduction towards data science
PDF
Python utan-stodhjul-motorsag
PDF
What is Python?
PDF
bv-python-einfuehrung aplication learn.pdf
PPTX
Python knowledge ,......................
PPTX
Chapter1 python introduction syntax general
PDF
Intro to Python
PPTX
Python Course Basic
PDF
Python_Fundamentals_for_Everyone_Usefull
PPTX
unit (1)INTRODUCTION TO PYTHON course.pptx
ODP
Programming Under Linux In Python
PDF
Don't do this
PDF
Class 1: Welcome to programming
PPTX
UNIT 1 PYTHON introduction and basic level
PDF
Porting to Python 3
PDF
Funky file formats - 31c3
PPT
Python_library_data_handelling by dr vingari
PPT
Python for data analysis using matplotlib library.ppt
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Python introduction towards data science
Python utan-stodhjul-motorsag
What is Python?
bv-python-einfuehrung aplication learn.pdf
Python knowledge ,......................
Chapter1 python introduction syntax general
Intro to Python
Python Course Basic
Python_Fundamentals_for_Everyone_Usefull
unit (1)INTRODUCTION TO PYTHON course.pptx
Programming Under Linux In Python
Don't do this
Class 1: Welcome to programming
UNIT 1 PYTHON introduction and basic level
Porting to Python 3
Funky file formats - 31c3
Python_library_data_handelling by dr vingari
Python for data analysis using matplotlib library.ppt

Recently uploaded (20)

PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
Cloud computing and distributed systems.
PDF
Modernizing your data center with Dell and AMD
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
KodekX | Application Modernization Development
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPT
Teaching material agriculture food technology
Spectral efficient network and resource selection model in 5G networks
Review of recent advances in non-invasive hemoglobin estimation
Understanding_Digital_Forensics_Presentation.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Cloud computing and distributed systems.
Modernizing your data center with Dell and AMD
Advanced methodologies resolving dimensionality complications for autism neur...
Dropbox Q2 2025 Financial Results & Investor Presentation
Chapter 3 Spatial Domain Image Processing.pdf
Big Data Technologies - Introduction.pptx
KodekX | Application Modernization Development
Building Integrated photovoltaic BIPV_UPV.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
“AI and Expert System Decision Support & Business Intelligence Systems”
Teaching material agriculture food technology

Python introduction

  • 1. An introduction to Python The language Marcelo Araujo Jul 2012, Taipei marcelo@qnap.com Monday, July 16, 12
  • 2. { Goals of this talk? ! A brief introduction of Python language. ! Get you interested in learning Python. ! Shows that it is a powerful language. And of course..... Monday, July 16, 12
  • 3. ...make you comfortable with PYTHON, like this guy! Monday, July 16, 12
  • 4. Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05) [GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> What we gonna see? ! What is Python? ! Who uses Python? ! Interactive prompt, memory, basic syntax, data types, strings, flow control statements, functions, classes, exceptions, importing, file I/O, list, tuple, dictionary, cast, help, dir. ! CPython, Pypy, Jython and IronPython. ! Python Frameworks. ! Your first Python program. Monday, July 16, 12
  • 5. { What is Python? Python is a remarkably powerful dynamic programming languate that is used in a wide variety of application domains. ! Very clear, readable syntax. ! Strong introspection capabilities. ! Intuitive object orientation. ! Natural expression of procedural code. ! Full modularity, supporting hierarchical packages. ! Exception-based error handling. ! Very high level dynamic data types. ! Extensive standard libraries and third party modules. ! Extensions and modules easily written in C, C++ or (Java or .NET). ! Embeddable within applications as a scripting interface. Monday, July 16, 12
  • 6. { Who created Python? ! Guido van Rossum. ! Python was released early of 1990s. ! Based on: ABC, C, Bourne Shell, Modula-3, Perl, Haskell and Lisp. ! Currently he works for Google. ! ... Half of his working time is to improve Python. ! Python is not related with the snake, but yes with a British show called Monty Python’s Flying Circus. Monday, July 16, 12
  • 7. { Who uses Python? Monday, July 16, 12
  • 8. { The interactive python ! Python has an interactive prompt that able you write code. ! You can obtain help. ! Have access to the docs. ! Test your code and ideas any time. Monday, July 16, 12
  • 9. { The interactive python Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05) [GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import this The ZEN of Python by Tim Peters. PEP - 020. It is the principles of Python *PEP - Python Enhancement Proposal. http://www.python.org/dev/peps/ Monday, July 16, 12
  • 10. { The interactive python Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05) [GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> name = 'Marcelo' >>> fname = “Araujo” >>> age = 31 >>> print 'My name is ', name, fname, ', I'm %i years old' % (age) My name is Marcelo Araujo , I'm 31 years old >>> print “I’d like be %i years old” % (age - 9) I’d like be 22 years old >>> type(name) <type ‘str’> >>> type(fname) <type ‘str’> >>> type(age) <type ‘int’> Monday, July 16, 12
  • 11. { Reserved words. and del from not while as elif global or with assert else if pass yield break except import print class exec in raise continue finally is return def for lambda try Like in any other computer language. Monday, July 16, 12
  • 12. { The interactive python - Variable ! The first assignment to a variable creates it. ! Variable types don’t need to be declared. ! Python figures out the variable types on its own. ! Python has a garbage collector. >>> name = ‘Marcelo’ >>> type(name), id(name) (<type 'str'>, 4483079264) >>> memoryview(name) <memory at 0x10b3d3f28> >>> del name >>> print name Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'name' is not defined Monday, July 16, 12
  • 13. { The interactive python - Memory >>> a = ‘marcelo’ >>> b = a a = ‘marcelo’ >>> id(a), id(b) (4482266376, 4482266376) name obj >>> memoryview(a), memoryview(b) (<memory at 0x10b3d3f28>, <memory at 0x10b3d3e90>) >>> a = 'araujo' >>> id(a), id(b) (4483897624, 4482266376) >>> memoryview(a), memoryview(b) (<memory at 0x10b3d3f28>, <memory at 0x10b3d3e90>) } buffer method() 0x10b3d3f28 0x10b928050 Mem 0x10b3d3e90 Monday, July 16, 12
  • 14. { The interactive python - Memory >>> a = ‘marcelo’ >>> b = a a = ‘marcelo’ >>> id(a), id(b) (4482266376, 4482266376) name obj >>> memoryview(a), memoryview(b) (<memory at 0x10b3d3f28>, <memory at 0x10b3d3e90>) >>> a = 'araujo' >>> id(a), id(b) (4483897624, 4482266376) >>> memoryview(a), memoryview(b) (<memory at 0x10b3d3f28>, <memory at 0x10b3d3e90>) } OID buffer method() a 0x10b3d3f28 OID 0x10b928050 Mem 0x10b3d3e90 b Monday, July 16, 12
  • 15. { Basic syntax 1 #!/bin/env python 2 # -*- coding: utf-8 -*- 3 4 class Name: Class Name 5 6 def __init__(self, name=None): Method and attribute 7 self.name = name Attribute 8 9 10 class FName(Name): } 11 12 def __init__(self, fname=None): Indentation Style 13 self.fname = fname 14 15 def output(self): 16 print self.name, self.fname 17 } 18 if __name__ == '__main__': 19 inst_name = FName(fname='Araujo') main() 20 inst_name.name = 'Marcelo' 21 inst_name.output() Monday, July 16, 12
  • 16. { Basic syntax - Indentation ! Indentation is important. ! No brackets. {} ! No dot and comma. ; >>> a = 1 >>> b = 2 >>> if a > b: ... print 'a is bigger than b' ... else: ... print 'b is bigger than a' ... b is bigger than a Code readable and more elegant. Monday, July 16, 12
  • 17. { Basic data types Numbers: int, long, float, complex. Strings: str and unicode. List and Tuples: list and tuple. Dictionary: dict Files: file Boolean: bool(True, False) Sets: set, frozenset Null: None Note: int usually 32bits, long all your memory, float usually 32bits. Monday, July 16, 12
  • 18. { Basic data types ! It is possible to cast types. >>> a = 1 >>> a = float(a) >>> type(a) >>> type(a) <type 'int'> <type 'float'> Operators ! Arithmetic. ! Logical. ! +, -, *, /, %, ** and // ! and, or, not ! Comparison. ! Membership. ! ==, !=, <>, >, <, >=, <= ! in, not in ! Assignment. ! Identity. ! =, +=, -=, *=, /=, %=, ! is, is not **= , //= ! Bitwise. ! &, |, ^, ~, <<, >> Monday, July 16, 12
  • 19. { Basic data types - Strings ! String is powerful in Python. (Immutable) >>> a = 'Marcelo' >>> print a[0] 'M' >>> a[0] = 'm' Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'str' object does not support item assignment ! String has a set of METHODS. >>> print a.upper(), a.lower(), a.partition('c') MARCELO marcelo ('Mar', 'c', 'elo') >>> a.replace('c', 'C') 'MarCelo' ! How change the string? >>> a = 'Marcelo' >>> a = 'm' + a[1:] >>> id(a) >>> print a 4483079264 marcelo >>> id(a) 4483500336 Monday, July 16, 12
  • 20. { Basic data types - Special Types. ! Lists. (Mutable) >>> a = [1, 2 3, 4, 5] >>> print a [1, 2, 3, 4, 5] >>> a[0] = 0 >>> print a [0, 2, 3, 4, 5] ! Tuples. (Immutable) >>> a = (1, 2, 3, 4, 5, 6) >>> a[0] = 0 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'tuple' object does not support item assignment ! Dictionary. (Mutable only the ITEM) >>> a = {'Name' : 'Marcelo', ‘Age’ : 31} >>> print a['Name'] Marcelo Monday, July 16, 12
  • 21. { Flow control statement if guess == number: while True: # do something # do something elif guess < number: # break when done # do something else break else: else: # do something else # do something in the end for i in range(1, 10): for i in range(1, 10): # do something # do something print i if i == 5: else: pass # do something else in else: # the end print i Monday, July 16, 12
  • 22. { More about List. ! List sort using “for”. Methods 1 a = [1, 3, 5, 4, 2] 2 b = [0 for i in range(5)] a.append() 3 size_a = len(a) 4 a.pop() 5 for i in range(size_a): a.remove(<value>) 6 print i, a[i] 7 b[a[i] - 1] = a[i] a.index(<value>) 8 a.count(<value>) 9 print a 10 print b ! Sort the list in a python way. dir() and help() >>> a = [1, 3, 5, 2, 4] >>> a.sort() >>> dir(list) >>> print a >>> help(list) [1, 2, 3, 4, 5] Monday, July 16, 12
  • 23. { More about List. ! Different than string, list, tuple and dictionary point to the same OBID. >>> import copy >>> a = [1, 2, 3] >>> a = [1, 2, 3] >>> b = a >>> b = copy.copy(a) >>> a.append(4) >>> a.append(4) >>> print a, b >>> print a, b [1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3] >>> id(a), id(b) >>> id(a), id(b) (4529798896, 4529798896) (4528943688, 4529135344) ! List full fill. ! List comprehension. >>> a = [] >>> a = [i for i in range(1,6)] >>> for i in range(1,6): >>> print a ... a.append(i) [1, 2, 3, 4, 5] ... >>> print a [1, 2, 3, 4, 5] Monday, July 16, 12
  • 24. { More about Dictionary. ! Dictionary is useful, we have a key for a specific item. >>> register = {'Name' : 'Marcelo', 'Age' : 31, 'Dep' : 'R&D'} >>> print register {'Dep': 'R&D', 'Age': 31, 'Name': 'Marcelo'} ! Access the dictionary. >>> for key, item in register.iteritems(): ... print key + ':' + 't' + str(item) ... Dep: R&D Age: 31 Name: Marcelo Note: Key is an unique value and immutable. Monday, July 16, 12
  • 25. { Function/Method ! Function is a block of organized, reusable code, that perform a single related action. ! Better modularity for your application. ! Python gives you many built-in functions like print(). Ex: 1 Ex: 2 >>> def say_hello(name=None): >>> def say_bye(name): ... if name == None: ... print 'Good bye %s' % (name) ... print 'Hello nobody!' ... ... else: >>> say_bye() ... print 'Hello %s' % (name) Traceback (most recent call last): ... File "<stdin>", line 1, in <module> >>> name = 'Marcelo' TypeError: say_bye() takes exactly 1 >>> say_hello(name) argument (0 given) Hello Marcelo >>> say_hello() Hello nobody! Monday, July 16, 12
  • 26. { Function/Method ! How pass an undefined number of args to a function? >>> def hello_all(say=None, *names): ... if say == None: ... say = 'Hello' ... elif say == 'Morning': ... say = 'Good morning' ... else: ... say = 'Aloha' ... for name in names: ... print say + ' ' + name ... >>> name = 'Marcelo' >>> name1 = 'Bob' >>> name2 = 'Kevin' >>> hello_all(‘Aloha’, name, name1, name2) Aloha Marcelo Aloha Bob Aloha Kevin Monday, July 16, 12
  • 27. { Function/Method ! We could use return. >>> def big_number(n1=0, n2=0): ... bigone = None ... if n1 > n2: ... bigone = n1 ... elif n1 < n2: ... bigone = n2 ... else: ... bigone = 'same' ... return bigone ... >>> answer = big_number(10, 20) >>> print answer 20 Note: Alway a function return something, if not defined, it will return None. Monday, July 16, 12
  • 28. { Classes/Objects - OOP Terminology ! Class: A user-defined prototype for an object that defines a set of attributes. ! Class variable: A variable that is shared by all instances of a class. ! Function overloading: The assignment of more than one behavior to a particular function/method. ! Instance variable: A variable that is defined inside a method and belongs only to the current instance of a class. ! Inheritance: The transfer of the characteristics of a class to other classes. ! Instance: An individual object of a certain class. ! Instantiantion: The creation of an instance of a class. ! Method: A special kind of function that is defined in a class. ! Object: An unique instance of a data structure. Monday, July 16, 12
  • 29. { Class example 1 class Employee: Class Name 2 """ Common base class for all employees""" 3 empCount = 0 Global Value of Class 4 5 def __init__(self, name, position): Class constructor 6 self.name = name 7 self.position = position 8 Employee.empCount += 1 9 10 def displayEmployee(self): NEXT SLIDE (><) 11 print "Name: ", self.name, "ttPosition: ", self.position 12 13 if __name__ == '__main__': 14 emp1 = Employee("Marcelo", "R&D") Instantiation the class 15 emp2 = Employee("Bob", "Q&A") 16 17 emp1.displayEmployee() 18 emp2.displayEmployee() 19 20 print 'Total Employee: %d' % (Employee.empCount) Instance the empCount Obj Monday, July 16, 12
  • 30. { self, self, self, self.....? ! “self” is a polemic decision on the project. ! It is part of PEP-8. ! Do you remember the Python ZEN? ! BETTER EXPLICIT THAN IMPLICIT Ex: 1 Ex: 2 ↶ >>> class Person: >>> class Person: ... def set_name(person, name): ... def set_name(self, name): ... person.name = name ... self.name = name ... ... >>> woman = Person() >>> woman = Person() >>> woman.set_name('Janny') >>> woman.set_name('Janny') >>> print woman.name >>> print woman.name Janny Janny Monday, July 16, 12
  • 31. { Class inheritance 1 class Employee_salary: New Class 2 def salary(self, value=0): 3 self.value = value 4 print "[Salary: %s]" % (self.value) 5 6 class Employee(Employee_salary): Inheritance < ..............code snipped...................> 18 if __name__ == '__main__': 19 emp1 = Employee("Marcelo", "R&D") Instantiation the class 20 emp2 = Employee("Bob", "Q&A") 21 22 emp1.displayEmployee() 23 emp1.salary(100) Call method salary() 24 emp2.displayEmployee() 25 emp2.salary(200) 26 27 print 'Total Employee: %d' % (Employee.empCount) Monday, July 16, 12
  • 32. { How about exception? ! It help us to handle situations that disrupts the normal flow of the program. try: >>> try: ... 10 / 0 # do something ... except ZeroDivisionError: except: ... print "Ooops, invalid." ... else: # do exception ... print "We're good, no exception!" else: ... finally: # do something else ... print "We're done with that." ... finally: Ooops, invalid. # just do We're done with that. Monday, July 16, 12
  • 33. { More about exception. ! We can check multiples exceptions. >>> try: ... a / 0 ... except ZeroDivisionError: ... print “Not possible make the division!” ... except TypeError: ... print “Unsupported type.” ... else: ... print 'We pass in all exceptions!' ... finally: ... print 'Do something' ... Unsupported type. Do something Built-in Exceptions list: http://docs.python.org/library/exceptions.html Monday, July 16, 12
  • 34. { More exception with raise. ! Use raise to catch problems. >>> def verify(value): ... if value == 10: ... print "Wow you have: %s" % (value) ... >>> try: ... verify() ... except: ... raise ... Traceback (most recent call last): File "<stdin>", line 2, in <module> TypeError: verify() takes exactly 1 argument (0 given) Monday, July 16, 12
  • 35. { More exception with raise. ! We can change the error message. >>> def verify(value): ... if not value.isdigit(): ... raise ValueError, "My useful message!" ... else: ... return value >>> try: ... verify('A') ... except ValueError: ... raise ... Traceback (most recent call last): File "<stdin>", line 2, in <module> File "<stdin>", line 3, in verify ValueError: My useful message! Monday, July 16, 12
  • 36. { Import/Modules ! Every Python code is a module by default. ! Modules help you to organize your software. ! Python comes with batteries. Powerful standard library. STDL: socket, select, SocketServer, BaseHTTPServer, asyncore, asynchat, xmlrpclib, SimpleXMLRPCServer, urllib, httplib, ftplib, smtpd, smtplib, poplib, impalib, json, getopt, optparse, argparse, fileinput, cmd, readline, subprocess, threading, multiprocessing, Queue, anydbm, pickle, shelve, sqlite3 ...... there are more Monday, July 16, 12
  • 37. { Import/Modules What I did? ! I moved all classes to another file called cemployee.py. 1 class Employee_salary: 2 def salary(self, value=0): 3 self.value = value 4 print "[Salary: %s]" % (self.value) 5 6 class Employee(Employee_salary): 7 """ Common base class for all employees""" 8 empCount = 0 9 10 def __init__(self, name, position): 11 self.name = name 12 self.position = position 13 Employee.empCount += 1 14 15 def displayEmployee(self): 16 print "Name: ", self.name, "ttPosition: ", self.position Monday, July 16, 12
  • 38. { Import/Modules What I did? ! Now my employee.py looks like. 1 import cemployee 2 3 if __name__ == '__main__': 4 emp1 = cemployee.Employee("Marcelo", "R&D") 5 emp2 = cemployee.Employee("Bob", "Q&A") 6 7 emp1.displayEmployee() 8 emp1.salary(100) 9 emp2.displayEmployee() 10 emp2.salary(200) 11 12 print 'Total Employee: %d' % (cemployee.Employee.empCount) Monday, July 16, 12
  • 39. { Import/Modules ! I can give a friendly name for the module. >>> import cemployee as myclass >>> emp1 = myclass.Employee("Marcelo", "R&D") >>> emp1.displayEmployee() Name: Marcelo Position: R&D ! I also can import only some classes from a module. >>> from cemployee import Employee_salary as salary >>> a = salary() >>> a.salary(100) [Salary: 100] ! More one example. >>> import os >>> os.system('uname -m') x86_64 0 >>> from os import system as sys >>> sys('uname -m') x86_64 0 Monday, July 16, 12
  • 40. { CPython, Jython, IronPython and PyPy? Cpython ! Used to binding C/C++ code. >>> from ctypes import * >>> libc = CDLL("libc.so") >>> size = c_uint(0) >>> buf = create_string_buffer(size.value) >>> libc.sysctlbyname("kern.hostname", buf, byref(size), None, 0) 0 >>> print buf.value controllerA.qnap.com Jython ! Used to binding Java. ! You can pack a jar to run over the JVM. ! Swing and any other JAVA library can be used. Monday, July 16, 12
  • 41. { CPython, Jython, IronPython and PyPy? IronPython ! Used to binding .NET. ! Microsoft have interesting on IronPython. PyPy ! It is a compliant alternative implementation of the Python 2.7.2 ! They claim be faster than CPython. More info at: http://speed.pypy.org/ Monday, July 16, 12
  • 42. { Python Frameworks? Django ! High-level Python Web framework. ! Rapid development and clean, pragmatic design. ! Automatic admin interface. ! Cache system. ! Template system. ! Internationalization by default. ! MTV (Model, Template,View). Monday, July 16, 12
  • 43. { Python Frameworks? Web2Py ! Inspired by Ruby on Rails. ! Focuses on rapid development. ! MVC. ! Support packed applications. TurboGears ! Scale it out. ! SQLAlchemy - Object Relational Mapper Twisted Matrix ! It is an event-driven networking engine. ! It supports many network protocols like: SMTP, POP3, IMAP, SSHv2, DNS and so on. Monday, July 16, 12
  • 44. { Python Frameworks? Example web-server using Cherrypy: 1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 import cherrypy 4 5 class HelloWorld(object): 6 7 def index(self): 8 return "Hello World!" 9 index.exposed = True 10 11 if __name__ == '__main__': 12 cherrypy.quickstart(HelloWorld()) Monday, July 16, 12
  • 45. { Your first python code. The output: sh-3.2# python first.py Our first python code! The result is: 5 Monday, July 16, 12
  • 46. { Your first python code. Now is time to play: a = 10 print ‘Impossible do that!’ finally: result = a / b b=2 result = None except: % (result) print ‘Our first python code!’ print ‘The result is: %s’ try: Monday, July 16, 12
  • 47. { Your first python code. How it supposed to be? 1 a = 10 2b=2 3 result = None 4 5 try: 6 result = a / b 7 except: 8 print 'Impossible do that!' 9 finally: 10 print 'Our first python code!' 11 12 print 'The result is: %s' % (result) Monday, July 16, 12
  • 48. { Thank you. Thanks, have a good weekend guys! marcelo@qnap.com Monday, July 16, 12