SlideShare una empresa de Scribd logo
1 de 48
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

Más contenido relacionado

La actualidad más candente

An Intro to Python in 30 minutes
An Intro to Python in 30 minutesAn Intro to Python in 30 minutes
An Intro to Python in 30 minutesSumit Raj
 
Introduction to advanced python
Introduction to advanced pythonIntroduction to advanced python
Introduction to advanced pythonCharles-Axel Dein
 
OpenGurukul : Language : Python
OpenGurukul : Language : PythonOpenGurukul : Language : Python
OpenGurukul : Language : PythonOpen Gurukul
 
PyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and MorePyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and MoreMatt Harrison
 
Python: an introduction for PHP webdevelopers
Python: an introduction for PHP webdevelopersPython: an introduction for PHP webdevelopers
Python: an introduction for PHP webdevelopersGlenn De Backer
 
Why Python (for Statisticians)
Why Python (for Statisticians)Why Python (for Statisticians)
Why Python (for Statisticians)Matt Harrison
 
AmI 2015 - Python basics
AmI 2015 - Python basicsAmI 2015 - Python basics
AmI 2015 - Python basicsLuigi De Russis
 
Meetup di GDG Italia - Leonardo Pirro - Codemotion Rome 2018
Meetup di GDG Italia - Leonardo Pirro -  Codemotion Rome 2018 Meetup di GDG Italia - Leonardo Pirro -  Codemotion Rome 2018
Meetup di GDG Italia - Leonardo Pirro - Codemotion Rome 2018 Codemotion
 
Active Support Core Extensions (1)
Active Support Core Extensions (1)Active Support Core Extensions (1)
Active Support Core Extensions (1)RORLAB
 
Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)Pedro Rodrigues
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayUtkarsh Sengar
 
한국어와 NLTK, Gensim의 만남
한국어와 NLTK, Gensim의 만남한국어와 NLTK, Gensim의 만남
한국어와 NLTK, Gensim의 만남Eunjeong (Lucy) Park
 
The Swift Compiler and Standard Library
The Swift Compiler and Standard LibraryThe Swift Compiler and Standard Library
The Swift Compiler and Standard LibrarySantosh Rajan
 
Basic Python Programming: Part 01 and Part 02
Basic Python Programming: Part 01 and Part 02Basic Python Programming: Part 01 and Part 02
Basic Python Programming: Part 01 and Part 02Fariz Darari
 
Learn python in 20 minutes
Learn python in 20 minutesLearn python in 20 minutes
Learn python in 20 minutesSidharth Nadhan
 

La actualidad más candente (20)

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

Destacado

Y3CDS - Python class 01
Y3CDS - Python class 01 Y3CDS - Python class 01
Y3CDS - Python class 01 Ting-You Xu
 
Lecture 8 strings and characters
Lecture 8  strings and charactersLecture 8  strings and characters
Lecture 8 strings and charactersalvin567
 
Python Programming - III. Controlling the Flow
Python Programming - III. Controlling the FlowPython Programming - III. Controlling the Flow
Python Programming - III. Controlling the FlowRanel Padon
 
Ridge regression, lasso and elastic net
Ridge regression, lasso and elastic netRidge regression, lasso and elastic net
Ridge regression, lasso and elastic netVivian S. Zhang
 

Destacado (7)

Y3CDS - Python class 01
Y3CDS - Python class 01 Y3CDS - Python class 01
Y3CDS - Python class 01
 
Text analysis using python
Text analysis using pythonText analysis using python
Text analysis using python
 
Lecture 8 strings and characters
Lecture 8  strings and charactersLecture 8  strings and characters
Lecture 8 strings and characters
 
Python Programming - III. Controlling the Flow
Python Programming - III. Controlling the FlowPython Programming - III. Controlling the Flow
Python Programming - III. Controlling the Flow
 
Python Introduction
Python IntroductionPython Introduction
Python Introduction
 
Lasso regression
Lasso regressionLasso regression
Lasso regression
 
Ridge regression, lasso and elastic net
Ridge regression, lasso and elastic netRidge regression, lasso and elastic net
Ridge regression, lasso and elastic net
 

Similar a Python introduction

Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...DRVaibhavmeshram1
 
Python introduction towards data science
Python introduction towards data sciencePython introduction towards data science
Python introduction towards data sciencedeepak teja
 
Python utan-stodhjul-motorsag
Python utan-stodhjul-motorsagPython utan-stodhjul-motorsag
Python utan-stodhjul-motorsagniklal
 
Python Course Basic
Python Course BasicPython Course Basic
Python Course BasicNaiyan Noor
 
unit (1)INTRODUCTION TO PYTHON course.pptx
unit (1)INTRODUCTION TO PYTHON course.pptxunit (1)INTRODUCTION TO PYTHON course.pptx
unit (1)INTRODUCTION TO PYTHON course.pptxusvirat1805
 
Programming Under Linux In Python
Programming Under Linux In PythonProgramming Under Linux In Python
Programming Under Linux In PythonMarwan Osman
 
Class 1: Welcome to programming
Class 1: Welcome to programmingClass 1: Welcome to programming
Class 1: Welcome to programmingMarc Gouw
 
Funky file formats - 31c3
Funky file formats - 31c3Funky file formats - 31c3
Funky file formats - 31c3Ange Albertini
 
Python PPT by Sushil Sir.pptx
Python PPT by Sushil Sir.pptxPython PPT by Sushil Sir.pptx
Python PPT by Sushil Sir.pptxsushil155005
 
What is Python? (Silicon Valley CodeCamp 2014)
What is Python? (Silicon Valley CodeCamp 2014)What is Python? (Silicon Valley CodeCamp 2014)
What is Python? (Silicon Valley CodeCamp 2014)wesley chun
 
PythonBrasil[8] - CPython for dummies
PythonBrasil[8] - CPython for dummiesPythonBrasil[8] - CPython for dummies
PythonBrasil[8] - CPython for dummiesTatiana Al-Chueyr
 
python-160403194316.pdf
python-160403194316.pdfpython-160403194316.pdf
python-160403194316.pdfgmadhu8
 
Introduction to Python and Matplotlib
Introduction to Python and MatplotlibIntroduction to Python and Matplotlib
Introduction to Python and MatplotlibFrançois Bianco
 
Everybody be cool, this is a ROPpery
Everybody be cool, this is a ROPperyEverybody be cool, this is a ROPpery
Everybody be cool, this is a ROPperyVincenzo Iozzo
 

Similar a Python introduction (20)

Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
 
Python introduction towards data science
Python introduction towards data sciencePython introduction towards data science
Python introduction towards data science
 
Python utan-stodhjul-motorsag
Python utan-stodhjul-motorsagPython utan-stodhjul-motorsag
Python utan-stodhjul-motorsag
 
What is Python?
What is Python?What is Python?
What is Python?
 
Intro to Python
Intro to PythonIntro to Python
Intro to Python
 
Python Course Basic
Python Course BasicPython Course Basic
Python Course Basic
 
unit (1)INTRODUCTION TO PYTHON course.pptx
unit (1)INTRODUCTION TO PYTHON course.pptxunit (1)INTRODUCTION TO PYTHON course.pptx
unit (1)INTRODUCTION TO PYTHON course.pptx
 
Programming Under Linux In Python
Programming Under Linux In PythonProgramming Under Linux In Python
Programming Under Linux In Python
 
Intro
IntroIntro
Intro
 
Don't do this
Don't do thisDon't do this
Don't do this
 
Class 1: Welcome to programming
Class 1: Welcome to programmingClass 1: Welcome to programming
Class 1: Welcome to programming
 
Porting to Python 3
Porting to Python 3Porting to Python 3
Porting to Python 3
 
Funky file formats - 31c3
Funky file formats - 31c3Funky file formats - 31c3
Funky file formats - 31c3
 
Python PPT by Sushil Sir.pptx
Python PPT by Sushil Sir.pptxPython PPT by Sushil Sir.pptx
Python PPT by Sushil Sir.pptx
 
What is Python? (Silicon Valley CodeCamp 2014)
What is Python? (Silicon Valley CodeCamp 2014)What is Python? (Silicon Valley CodeCamp 2014)
What is Python? (Silicon Valley CodeCamp 2014)
 
PythonBrasil[8] - CPython for dummies
PythonBrasil[8] - CPython for dummiesPythonBrasil[8] - CPython for dummies
PythonBrasil[8] - CPython for dummies
 
Porting to Python 3
Porting to Python 3Porting to Python 3
Porting to Python 3
 
python-160403194316.pdf
python-160403194316.pdfpython-160403194316.pdf
python-160403194316.pdf
 
Introduction to Python and Matplotlib
Introduction to Python and MatplotlibIntroduction to Python and Matplotlib
Introduction to Python and Matplotlib
 
Everybody be cool, this is a ROPpery
Everybody be cool, this is a ROPperyEverybody be cool, this is a ROPpery
Everybody be cool, this is a ROPpery
 

Último

Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 

Último (20)

Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 

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