SlideShare a Scribd company logo
1 of 87
Download to read offline
Programming with Python
        Basic Topics

          Mosky




                          1
Mosky:
●   The examples and the PDF version are available at:
    –   j.mp/mosky-programming-with-python.
●   It is welcome to give me any advice of this slide or
    ask me the answers of the challenges.
    –   mosky.tw




                                                           2
Mosky
●   Projects               ●   Pinkoi staff
                               pinkoi.com
    –   MoSQL
        mosql.mosky.tw

    –   Clime              ●   PyCon JP '12 Speaker
        clime.mosky.tw
                               pycon.jp
    –   Apt-Pool
        Apt-Add
                           ●   PyCon TW '12 Speaker
                               pycon.tw
        …




                                                      3
Advertisement




●   PyCon Taiwan 2013               ●   COSCUP 2013
    –   pycon.tw                        –   coscup.org
    –   5/25-26 @ Sinica
                                        –   8/3-4 @ TICC
                                        –   coscup-gereral@googlegroups.co
    –   pythontw@googlegroups.com           m


                                                                             4
Topics
●   Basic Topics            ●   Adv. Topics
    –   Python 2 or 3?          –   Module and Package
    –   Environment             –   Typing
    –   hello.py                –   Comprehension
    –   Common Types            –   Functional Technique
    –   Flow Control            –   Object-oriented Prog.
    –   File I/O                –   Useful Libraries
    –   Documentation       ●   Final Project
    –   Scope                   –   A Blog System
                                                            5
An Investigation
Do you know _________ ?
–   any other programming language
–   Object-oriented
–   Static Typing; Strong and Weak Typing
–   Dynamic Typing
–   Functor; Closure
–   Functional Programming
–   Web development


                                            6
Python 2 or 3?
    in short.




                 7
Python 2 or 3?
●   Python 2.x                                                 ●   Python 3.x
    –   status quo                                                 –   present & future
    –   2.7 is end-of-life release                                 –   under active development
    –   harder for newcomers                                       –   easier for newcomers
    –   more third-party lib.                                      –   less third-party lib.
    –   2to3.py                                                    –   3to2.py
    –   backported features:                                       –   new features:
         ●   What's News in Python 2.6                                  ●   What's News in Python 3.0
             docs.python.org/release/2.6.4/whatsnew/2.6.html                docs.python.org/py3k/whatsnew/3.0.html

         ●   What's News in Python 2.7
             docs.python.org/dev/whatsnew/2.7.html



                                                                                                                     8
Python 2 or 3? (cont.)
●   Use Python 3 if you can.
●   Decide Python 2 or 3 by the libraries you will use.
●   Today, we will go ahead with Python 2.
    And introduce you to the changes in Python3.




                                                          9
Environment
Is a python in your computer?




                                10
On Linux or Mac
●   Python is built-in on Linux or Mac.
●   All you have to do is check the version.
    Type "python" in any terminal.

    Python 2.7.3 (default, Sep 26 2012, 21:51:14)
    [GCC 4.7.2] on linux2
    Type "help", "copyright", "credits" or "license"
    for more information.
    >>>

                                                       11
On Windows
●   Download the installer from:
    "http://python.org/download"
●   Install it.
●   Add the Python's PATH.
    –   Computer → System Properties → Advanced system
        settings → Advanced tab → Environment Variables →
        System Variables → find PATH.
    –   "...;C:Python27"


                                                            12
Editor / IDE
●   The Editors                    ●   The IDE
    –   Sublime Text 2                 –   IDLE
        www.sublimetext.com
                                            ●   Debian-base:
    –   VIM                                     sudo apt-get install idle
        wiki.python.org/moin/Vim            ●   Windows:
    –   Gnome Text Editor                       Use the Start Menu to
        (gedit)                                 search "IDLE"

    –   Notepad++
                                   ●   The others:
        notepad-plus-plus.org          –   wiki.python.org/moin/PythonEditors
    –   ...

                                                                                13
The Python Shell
●   Type "python" in terminal.
    –   >>>
    –   ...
●   Leaving a shell:
    –   exit()
    –   Linux or Mac: Ctrl+D
    –   Windows: Ctrl+Z<Enter>



                                     14
The python Command
●   Enter Python shell without arguments.
●   python hello.py
●   python -c 'print "Hello, World!"'
●   python -m SimpleHTTPServer




                                            15
hello.py
Say hello to Python.




                       16
hello.py
#!/usr/bin/env python
                                  ●   #! the shebang.
# -*- coding: utf-8 -*-           ●   # -*- defines the encoding
# file: hello.py                      of this file.
                                  ●   # means the comments.
def hello(name=None):


   if name:n                     ●   : starts a block.
        return 'Hello, %s!' %
name
   else:                          ●   A block uses 4-space indent.
        return 'Hello, Python!'   ●   A statement ends with n.


                                                                     17
hello.py (cont.)
if __name__ == '__main__':
                             ●   __name__, the name of
                                 module.
    import sys


    if len(sys.argv) >= 2:   ●   import is important.
        print                    The usage:
hello(sys.argv[1])
    else:
                             ●   import sys
        print hello()        ●   from sys import argv
                             ●   … as alias


                                                         18
19
The print Statement
print   'End with a new line char.'
print   'Print', 'multiple', 'strings.'
print   'End with a space.',
print   # print a new line char




                                          20
The print function in Python 3
print('End with a new line   char.')
print('Print', 'multiple',   'strings.')
print('End with a space.',   end=' ')
print() # print a new line   char

print('End with a space.', end='')
print('a', 'b', 'c', seq=',')


                                           21
Common Types
Without it we can do noting




                              22
Common Types
●   Numeric                   ●   Sequence
    –   Integer 100               –   String ""
    –   Float 10.0                –   Unicode u""
    –   Long 100L                 –   List [,]
    –   Complex 1+1j              –   Tuple (,)
    –   Boolean True, False




                                                    23
Common Types (cont.)
●   Mapping
    –   Dictionary {:}
●   Set
    –   Set {,}
    –   Frozen Set
        forzenset(...)




                                         24
Integer, Float and Long
●   3+3                ●   divmod(5, 2)
                           → tuple (not numeric)
●   3-3                ●   5/2
●   3*3                    → int (truncated)
                           5.0/2
    3/3
                       ●
●
                           → float
●   6/2*(1+2)              (as double in C)
                       ●   5.0//2
    → int                  → float (floored)
    (as long in C)     ●   2**1000
                           → long (∞ precision)


                                                   25
Integer and Float in Python 3
●   3+3               ●   divmod(5, 2)
                          → tuple (not numeric)
●   3-3               ●   5/2
●   3*3                   → float
                          5.0/2
    3/3
                      ●
●
                          → float
●   6/2*(1+2)             (as double in C)
                      ●   5.0//2
    → int                 → float (floored)
    (∞ precision)     ●   2**1000
                          → int (∞ precision)


                                                  26
Note: The Variables
●   x = 1              ●   x < y
●   x + 1                  → True
    → 2
●   y = 2              ●   bin(y)
●   x + y                  → '0b101'
    → 3                ●   bin(y | 0b011)
    y += 3
                           → '0b111'
●


    → 5


                                            27
A Trap of the Integer
●   weight = 49
●   height = 163
●   bmi = weight / (height / 100) ** 2
●   bmi
    → 49

●   (height / 100)
    → 1

                                         28
A Trap of the Integer (cont.)
●   weight = 49.0
●   height = 163.0
●   bmi = weight / (height / 100) ** 2
●   bmi
    → 18.442545824080696




                                         29
Complex
●   1j * 1J                ●   a = 3.0+4.0j
●   1j * complex(0,1)
                           ●   float(a)
                               → TypeError
●   3 + 1j*3               ●   a.real
●   (3+1j)*3                   → 3.0
●   (1+2j)/(1+1j)          ●   a.imag
    → complex                  → 4.0
                           ●   abs(a)
                               # = sqrt(a.real**2 +
                               a.imag**2)
                               → 5


                                                      30
Boolean
●   not False            Comparison:
●   True and True        –   10 < 100
●   False or True        –   10 < 10.0
                         –   10 <= 10.0
                         –   10 == 10.0
●   False +1
    → 1
                         –   10 != 10.0
                         –   x is y
●   True +1
    → 2

                                          31
String and Unicode
'...' is equal to "..."      Functions
                             –   ord( 'A')
String (immutable seq.)      –   chr(65)
–     ' 中文 '                 –   ord(u' 中 ')
–     ' 嗨, nPython ! '      –   unichr(20013); chr(20013)
–   r' 嗨, nPython ! '       Decoding (String → Unicode)
                             –    ' 中文 '.decode('utf-8')
–    ''' ... '''
                             –   unicode( ' 中文 ', 'utf-8')
Unicode (immutable seq.)
                             Encoding (Unicode → String)
–   u' 嗨, nPython ! '       –   u' 中文 '.encode('utf-8')
–   ur' 嗨, nPython ! '      –   str(u' 中文 ')
–    u''' ... '''            –   str(u' 中文 ', 'utf-8')



                                                             32
Bytes and String in Python 3
'...' is equal to "..."   Functions
                          –   ord(b'A')
Bytes (immutable seq.)    –   chr(65)
–   b' 中文 '               –   ord( ' 中 ')
–   b' 嗨, nPython ! '    –   unichr(20013); chr(20013)
–   br' 嗨, nPython ! '   Decoding (Bytes → String)
                          –   b' 中文 '.decode('utf-8')
–    b''' ... '''
                          –   str(b' 中文 ', 'utf-8')
String (immutable seq.)
                          Encoding (String → Bytes)
–     ' 嗨, nPython ! '   –    ' 中文 '.encode('utf-8')
–   r' 嗨, nPython ! '    –   bytes( ' 中文 ')
–    ''' ... '''          –   bytes( ' 中文 ', 'utf-8')



                                                          33
Unicode Does Matter!
●   b = ' 中文 '
●   len(b)
    → 6
●   len(b.decode('utf-8'))
    → 2




                                  34
String and Unicode (cont.)
●   They have a lot of methods:
    capitalize center count decode encode endswith
    expandtabs find rfind format index rindex isalnum
    isalpha isdigit islower isspace istitle isupper
    join ljust rjust lower partition rpartition
    replace split rsplit splitlines startswith rstrip
    strip lstrip swapcase title translate upper zfill
●   ref:
    docs.python.org/2/library/stdtypes.html#string-methods



                                                             35
String and Unicode (cont.)
String formatting:
–   % (modulo)
     ●   ref: docs.python.org/2/library/stdtypes.html#string-formatting-operations
–   str.format
     ●   ref: docs.python.org/2/library/string.html#formatstrings




                                                                                     36
List and Tuple
List (mutable seq.)          Tuple (seq.)
–   []                       –   tuple()
–   ['item']                 –   ('item', )
–   ['s', 100, u'unicode']   –   ('s', 100, u'unicode')
–   list('abc')              –   tuple('abc')
–   'a b c'.split(' ')
–   'n'.join(['spam',       –   'n'.join(('spam',
    'eggs'])                     'eggs'))
–   x, y = [1, 2]            –   x, y = (1, 2)
–   x, y = [y, x]            –   x, y = (y, x)


                                                          37
Sequence
Sequence                    Mutable Seq.
                            –   s[i] = x
–   x in s # performance?   –   s[i:j] = t
–   x not in s              –   del s[i:j]
–   s + t                   –   s[i:j:k] = t
–   s * n, n * s            –   s.append(x)
                            –   s.insert(i, x)
–   s[i]                    –   s.pop([i])
–   s[i:j]                  –   s.remove(x) # performance?
–   s[i:j:k]                –   s.extend(t)
                                in-place
–   len(s)
                            –   s.sort([cmp[, key[, reverse]]])
–   s.index(x)              –   s.sort([key[, reverse]]) # Py 3
–   s.count(x)              –   s.reverse()



                                                                  38
Sequence Comparison
●   (0, 0, 0) < (0, 0, 1)
●   [0, 0, 0] < [0, 0, 1]
●   (0, ) < (0, 0)
●   'ABC' < 'C' < 'Pascal' < 'Python'
●   (1, 2, 3) == (1.0, 2.0, 3.0)

●   'A' == 'A'
●   'A' > 65
●   'A' > 66
●   ('A', ) > (66, )


                                        39
Sequence Comparison in Python 3
●   (0, 0, 0) < (0, 0, 1)
●   [0, 0, 0] < [0, 0, 1]
●   (0, ) < (0, 0)
●   'ABC' < 'C' < 'Pascal' < 'Python'
●   (1, 2, 3) == (1.0, 2.0, 3.0)

●   'A' == 'A'
●   'A' > 65 → TypeError
●   'A' > 66 → TypeError
●   ('A', ) > (66, ) → TypeError


                                        40
Sequence (cont.)
Slicing and Slice object:
–   s = range(10)           –   s = 'I am a str.'
–   t = s                   –   s[:-3]
–   t[0] = 'A'
–   print s                 –   s.reverse()
–   t is s                      → TypeError
                            –   s[::-1]
–   t = s[:]                –   ''.join(reversed(s))
–   t is s
                            –   slice(None, None, -1)


                                                        41
Mapping
Dict. (mutable map.)
                                –   len(d)
–   {}
                                –   d[k]
–   {'A ': 1, 'B': 2, 'C': 3}   –   d[k] = v
–   dict({...})                 –   del d[k]
–   dict(A=1, B=2, C=3)         –   k in d, k not in d
                                –   d.copy()
                                –   d.get(key[, default])
–   k = 'ABC'                   –   d.setdefault(key[, default])
–   v = [1, 2, 3]               –   d.items(), d.keys(), d.values()
–   pairs = zip(k, v)           –   d.pop(key[, default)
–   dict(pairs)                 –   d.update([other])
                                    ...



                                                                      42
Set
Set (mutable set)
                                  –   len(s)
–   set()                         –   x in s, x not in s
–   {'A', 'B', 'C'} # Py3         –   s.copy()
                                  –   s.add(elem)
–   set('ABC')                    –   s.discard(elem)
–   set(['A','B','C'])            –   s.pop()
                                  –   s |= other
                                  –   s &= other
                                  –   s | other | ...
                                  –   s & other & ...
                                  –   s < | <= | == | > = | > other
                                      ...



                                                                      43
Flow Control
in Python is grace and easy to learn.




                                        44
The if Statement
if [condition 1]:
    …
elif [condition 2]:
    …
elif [condition 3]:
    …
else:
    …

[exp. if true] if [condition] else [exp. if false]


                                                     45
Truth Value Testing
They are same as False in a boolean context:
–   None
–   False
–   Zeros (ex. 0, 0.0, 0L, 0j)
–   Empty containers (ex. '', [], {})
–   __nonzero__() or __len__() returns 0 or False




                                                    46
Truth Value Testing (cont.)
●   if   not None: ...
●   if   not []: ...
●   if   [0]: ...
●   if   [[]]: ...
●   if   "": ...
●   if   {}: ...
●   if   not {0: False}: …
    …

                                         47
The for Statement
for [item] in [iterable]: for i in [0, 1, 2]:
    …                         print i



for i in range(3):      for i in xrange(3):
    print i                 print i




                                                48
The for Statement in Python 3
for [item] in [iterable]: for i in [0, 1, 2]:
    …                         print i



for i in range(3):      for i in xrange(3):
    print i                 print i




                                                49
The for Statement (cont.)
for i in range(1, 3): for i in range(3, -1, -1):
    print i               print i




s = [1, 2, 3]            s = [...]

t = 'xyz'                for i, item in enumerate(s):
                             print i, item
for i, j in zip(s, t):
    print i, j

                                                        50
The for Statement (cont.)
●   It is like for … each in other language.
    –   Note: Python hasn't other for loop.
●   It can iterate all of iterable object.
    –   In other words, the object which defined __iter__.
    –   ex. sequence, mapping, set, ...




                                                             51
Challenge 1: A Pyramid
●   Use for loop to build a               *
    pyramid on right.                    ***
    –   without limit.                  *****
    –   limit: in two lines            *******
         ●   hint: string formatting




                                                 52
Challenge 2-1: Count the Chars
●   Use for loop to count    "Please count the
    the sentence on right.   characters here."
    –   without limit.
    –   limit: without if
         ●   hint: use get
                             {'P': 1, ...}




                                                 53
Challenge 2-2: Collect the Chars
●   Use for loop to collect "Here are UPPERCASE
    the chars.              and lowercase chars."
    –   limit: use setdefault


                                {'c': ['C', 'c',
                                'c'], ...}




                                                    54
The while Statement
tasks = [...]
while tasks:                  while 1:
    …                             …

●   It leaves the loop once   ●   A infinite loop.
    the tasks is empty.       ●   It is better to use block
                                  mechanism in a loop.
                                  –   ex. I/O block

                                                              55
The break, continue Statement
loop …:                     loop …:
    if …: break                 if …: continue



●   It terminates a loop.   ●   It continues with the
                                next iteration.




                                                        56
The break, continue Statement (cont.)
●   They do the same thing in both C and Python.
●   Using break or continue is encouraged.
    –   take the place of the complicated condition in a while.
    –   faster, because Python is interpreted.
●   Just use them.




                                                                  57
The pass Statement
●   Do nothing.




                                       58
The else Clause on Loops
       loop …:
           …
       else:
           …
●   No a clause on the if statement!
●   If the loop isn't broken by any break statement, the
    else block is executed.
●   It replaces the flags we usually used.

                                                           59
Challenge 3-1: The Primes
●   Try to filter the primes     [2,   3, 5, 7, 11, 13,
    from [2, 100).               17,   19, 23, 29, 31,
                                 37,   41, 43, 47, 53,
    –   without limit.           59,   61, 67, 71, 73,
    –   limit: use loop's else   79,   83, 89, 97]




                                                          60
The try Statement
try:
    …
except LookupError, e:
    …
except (IndexError, KeyError), e:
    …
else:
    …
finally:
    …


                                    61
The try Statement in Python 3
try:
    …
except LookupError as e:
    …
except (IndexError, KeyError) as e:
    …
else:
    …
finally:
    …


                                      62
The try Statement (cont.)
●   For avoiding to catch the exception we don't expect, you should:
    –   reduce your code in try block.
        ●
           move them to else block.
    –   make the exception precise in except statement.
        ●   Avoid using Exception.
        ●   ref: docs.python.org/2/library/exceptions.html#exception-hierarchy
●   Release the resource in finally block.
    –   or use context manager
    – ex. file, socket, …
●   raise SomeError



                                                                                 63
The def Statement
def f(x, y):            def f(x, y=2):
    return (x, y)           return (x, y)



f(1, 2)                 f(1)
f(y=2, x=1)             f(x=1)
f(*(1, 2))              f(*(1, ))
f(**{'y': 2, 'x': 1})   f(**{'x': 1})

                                            64
The def Statement (cont.)
def f(*args):                 def f(**kargs):
    return args                   return kargs


f(1, 2, 3)                    # f(1, 2) # → TypeError
                              f(x=1, y=2, z=3)
# f(y=2, x=1) # → TypeError
                              # f(*(1, 2)) # → TypeError
f(*(1, 2, 3, 4))              f(**{'x': 1, 'y': 2, 'z': 3})
# f(**{'y': 2 ,'x': 1})
# → TypeError


                                                              65
The def Statement (cont.)
def f(x, *args):              def f(x, **kargs):
    return x, args                return kargs


f(1, 2, 3)                    # f(1, 2) # → TypeError
                              f(x=1, y=2, z=3)
# f(y=2, x=1) # → TypeError
                              # f(*(1, 2)) # → TypeError
f(*(1, 2, 3, 4))              f(**{'x': 1, 'y': 2, 'z': 3})
# f(**{'y': 2, 'x': 1})
# → TypeError


                                                              66
The def Statement (cont.)
def f(*args, y):     def f(*args, **kargs):
    return kargs         return args, kargs




→ SyntaxError        f(1, 2, 3)
                     f(y=2, x=1)
                     f(*(1, 2, 3, 4))
                     f(**{'y': 2, 'x': 1})


                                              67
The def Statement in Python 3
def f(*args, k):              def f(*args, k, **kargs):
    return kargs                  return args, kargs




F(1, 2, 3)                    f(1, 2, 3)
# f(x=1, k=2) # → TypeError   f(x=1, k=2)
f(*(1, 2, 3, 4))
                              f(*(1, 2, 3, 4))
# f(**{'x': 1, 'k': 2})
# → TypeError                 f(**{'x': 1, 'k': 2})


                                                          68
The def Statement (cont.)
        def f(): pass
        def g(): pass
        d = {'x': f, 'y': g}
        d['x']()
●   Python functions are first-class functions.
    –   It means you can pass functions as arguments, and
        assign functions to variables.
    –   It is like the function pointers in C.


                                                            69
An Example of Using while, try and def.

# file: ex_try.py                                  $ python ex_try.py
def take_int(prompt='Give me a int: '):
                                                   Give me a int: str
    while 1:
        try:
                                                   It is not a int!
             user_input = int(raw_input(prompt))   Give me a int: abc
        except ValueError, e:
             print 'It is not a int!'              It is not a int!
        else:
             return user_input                     Give me a int: 100
if __name__ == '__main__':
                                                   I got a int from user:
    x = take_int()                                 100
    print 'I got a int from user: %d' % x
                                                   $

                                                                      70
A Trap of the Default Value
# file: ex_defval_trap.py      ●   Because the list is
                                   created when the
def f(items=[]):                   function is defined.
    items.append(1)
    return items
                               ●   Avoid to use the
                                   mutable types as the
if __name__ == '__main__':         default value.
    print f() # -> [1]
    print f() # -> [1, 1]
    print f() # -> [1, 1, 1]




                                                          71
Challenge 4: A BMI Calculator
●   BMI: Body Mass Index                  Enter your height (M):
    –   BMI = weight (KG) ÷ height (M)2   1.63
    –   < 18.5 → Underweight
                                          Enter your weight (KG):
    –   [18.5, 25) → Normal weight
    –   [25, 30) → Overweight             49
    –   >= 30 → Obesity                   ---
●   Write a BMI calculator.
                                          Your BMI is:
    –   without limit.
    –   limit: only one if
                                          18.44 (Underweight)
         ●   hint: use loop               Ideal weight is between:
                                          49.15 ~ 66.42

                                                                     72
File I/O
Open anything with the open.




                               73
The file Object
f = open('input.txt')   f =
print f.read()          open('output.txt',
                        'w')
f.seek(0)
                        f.write('a line.n')
for line in f:
                        f.close()
    print line,
f.close()



                                               74
The Context Manager
        with open('input.txt') as f:
            for line in f:
                print line,
        f.close()
●   Python 2.5↑
    –   Python 2.5.x: from __future__ import with_statement
    –   Python 2.6↑: It is mandatory.



                                                              75
Challenge 2: Count the Chars (cont.)
–   limit 3: with the files   The path of input:
                              input.txt
                              The path of output:
                              output.txt
                              ---
                              The result was
                              written.


                                                    76
The csv Moudle
#!/usr/bin/env python           1, apple
# -*- coding: utf-8 -*-         2, orange
# file: ex_csv.py
                                3, watermelon
import csv
                                ['1', ' apple']
with open('ex_csv.csv') as f:
                                ['2', ' orange']
    for row in csv.reader(f):
        print row               ['3', ' watermelon']


                                                       77
The os.path Moudle
# file: ex_os_path.py                                $ python ex_os_path.py
from os import walk
from os.path import join
                                                     It requires a path as
def list_files(path):
    paths = []
                                                     argument.
    for root, dir_names, file_names in walk(path):
        for file_name in file_names:
            paths.append(join(root, file_name))
                                                     $ python ex_os_path.py .
    return paths
                                                     …/1
if __name__ == '__main__':

   import sys
                                                     …/b/4
   from os.path import abspath, dirname

   if len(sys.argv) == 2:
                                                     …/a/2
       path = abspath(dirname(sys.argv[1]))
       for path in list_files(path):                 …/a/3
           print path
   else:
       print 'It requires a path as argument.'




                                                                                78
Documentation
The documentation is everywhere.




                                   79
The help Function
●   In Python shell:           ●   In terminal:
    –   help(open)                 –   $ pydoc SimpleHTTPServer
    –   dir(open)                  –   $ pydoc csv
                                   –   $ pydoc os.path
    –   'n'.join(dir(open))




                                                                  80
Your Documentation
                                    $ pydoc ex_doc
# file: ex_doc.py                   Help on module ex_doc:


'''module-level doc.'''             NAME
                                           ex_doc - module-level doc.

def f(x):                           FILE
    '''A short sentence describes
this function.                      /home/mosky/programming-with-python/ex_doc.py

                                    FUNCTIONS
    About the parameters, return        f(x)
value or any other detail ...                A short sentence describes this
    '''                             function.

    pass
                                            About the parameters, return value or
                                    any other detail ...




                                                                                    81
Scope
Where is the x?




                  82
Scope
# file: ex_scope.py            $ python ex_scope.py
                               global
x = 'global'
                               local
def f():
                               $
    if 1:
         x = 'local'
    return x
                               ●   Scopes are decided by
                                   functions.
if __name__ == '__main__':
    print x
    print f()




                                                           83
The LEGB Rule
# file: ex_LEGB.py                             ●   return …
global_var = 100                                   –   Local (in function)
def f():
    enclosed_var = 10
                                                   –   Enclosed
    def g():
                                                   –   Global
        local_var = 1
        return sum([local_var, enclosed_var,
                                                   –   Built-in
global_var])

    return g()

if __name__ == '__main__':
    print f() # -> 111




                                                                             84
Challenge 3-2: The Primes (cont.)
–   limit 1: Sieve of   [2,   3, 5, 7, 11, 13,
    Eratosthenes.       17,   19, 23, 29, 31,
–   limit 2: use set.   37,   41, 43, 47, 53,
                        59,   61, 67, 71, 73,
                        79,   83, 89, 97]




                                                 85
Challenge 5: Mix All
●   You have many             $ python mix.py pyramid 10
    functions now.            …
                              $ python mix.py primes 100
    Try to write a CLI
                              …
    program to trigger your
                              $ python mix.py bmi 1.63 49
    functions.
                              …
    –   without limit
                              $ python mix.py blah blah
    –   limit: without if.    Please check your args.




                                                            86
Adv. Topics
There is another slide.




                          87

More Related Content

What's hot

What is Python? An overview of Python for science.
What is Python? An overview of Python for science.What is Python? An overview of Python for science.
What is Python? An overview of Python for science.Nicholas Pringle
 
Writing Fast Code (JP) - PyCon JP 2015
Writing Fast Code (JP) - PyCon JP 2015Writing Fast Code (JP) - PyCon JP 2015
Writing Fast Code (JP) - PyCon JP 2015Younggun Kim
 
Python Foundation – A programmer's introduction to Python concepts & style
Python Foundation – A programmer's introduction to Python concepts & stylePython Foundation – A programmer's introduction to Python concepts & style
Python Foundation – A programmer's introduction to Python concepts & styleKevlin Henney
 
Introduction to go language programming
Introduction to go language programmingIntroduction to go language programming
Introduction to go language programmingMahmoud Masih Tehrani
 
Python - Introduction
Python - IntroductionPython - Introduction
Python - Introductionstn_tkiller
 
Chapter 0 Python Overview (Python Programming Lecture)
Chapter 0 Python Overview (Python Programming Lecture)Chapter 0 Python Overview (Python Programming Lecture)
Chapter 0 Python Overview (Python Programming Lecture)IoT Code Lab
 
Go Language Hands-on Workshop Material
Go Language Hands-on Workshop MaterialGo Language Hands-on Workshop Material
Go Language Hands-on Workshop MaterialRomin Irani
 
Introduction To Python | Edureka
Introduction To Python | EdurekaIntroduction To Python | Edureka
Introduction To Python | EdurekaEdureka!
 
Open source projects with python
Open source projects with pythonOpen source projects with python
Open source projects with pythonroskakori
 
Introduction to Programming in Go
Introduction to Programming in GoIntroduction to Programming in Go
Introduction to Programming in GoAmr Hassan
 

What's hot (18)

Python Presentation
Python PresentationPython Presentation
Python Presentation
 
What is Python? An overview of Python for science.
What is Python? An overview of Python for science.What is Python? An overview of Python for science.
What is Python? An overview of Python for science.
 
Writing Fast Code (JP) - PyCon JP 2015
Writing Fast Code (JP) - PyCon JP 2015Writing Fast Code (JP) - PyCon JP 2015
Writing Fast Code (JP) - PyCon JP 2015
 
Python basics
Python basicsPython basics
Python basics
 
Python Foundation – A programmer's introduction to Python concepts & style
Python Foundation – A programmer's introduction to Python concepts & stylePython Foundation – A programmer's introduction to Python concepts & style
Python Foundation – A programmer's introduction to Python concepts & style
 
Python ppt
Python pptPython ppt
Python ppt
 
Python Tutorial
Python TutorialPython Tutorial
Python Tutorial
 
Introduction to go language programming
Introduction to go language programmingIntroduction to go language programming
Introduction to go language programming
 
Python - Introduction
Python - IntroductionPython - Introduction
Python - Introduction
 
Chapter 0 Python Overview (Python Programming Lecture)
Chapter 0 Python Overview (Python Programming Lecture)Chapter 0 Python Overview (Python Programming Lecture)
Chapter 0 Python Overview (Python Programming Lecture)
 
Go Language Hands-on Workshop Material
Go Language Hands-on Workshop MaterialGo Language Hands-on Workshop Material
Go Language Hands-on Workshop Material
 
Python by Rj
Python by RjPython by Rj
Python by Rj
 
Introduction To Python | Edureka
Introduction To Python | EdurekaIntroduction To Python | Edureka
Introduction To Python | Edureka
 
Python - the basics
Python - the basicsPython - the basics
Python - the basics
 
Python tutorial
Python tutorialPython tutorial
Python tutorial
 
Open source projects with python
Open source projects with pythonOpen source projects with python
Open source projects with python
 
Introduction to Programming in Go
Introduction to Programming in GoIntroduction to Programming in Go
Introduction to Programming in Go
 
Python final ppt
Python final pptPython final ppt
Python final ppt
 

Viewers also liked

Learning Git with Workflows
Learning Git with WorkflowsLearning Git with Workflows
Learning Git with WorkflowsMosky Liu
 
Boost Maintainability
Boost MaintainabilityBoost Maintainability
Boost MaintainabilityMosky Liu
 
Socket Programming In Python
Socket Programming In PythonSocket Programming In Python
Socket Programming In Pythondidip
 
Beyond the Style Guides
Beyond the Style GuidesBeyond the Style Guides
Beyond the Style GuidesMosky Liu
 
Programming with Python and PostgreSQL
Programming with Python and PostgreSQLProgramming with Python and PostgreSQL
Programming with Python and PostgreSQLPeter Eisentraut
 
Learning Python from Data
Learning Python from DataLearning Python from Data
Learning Python from DataMosky Liu
 
Functional programming in Python
Functional programming in PythonFunctional programming in Python
Functional programming in PythonColin Su
 
Learn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesLearn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesMatt Harrison
 
Golang iran - tutorial go programming language - Preliminary
Golang iran - tutorial  go programming language - PreliminaryGolang iran - tutorial  go programming language - Preliminary
Golang iran - tutorial go programming language - Preliminarygo-lang
 
Golang #5: To Go or not to Go
Golang #5: To Go or not to GoGolang #5: To Go or not to Go
Golang #5: To Go or not to GoOliver N
 
Concurrency in Python
Concurrency in PythonConcurrency in Python
Concurrency in PythonMosky Liu
 
Minimal MVC in JavaScript
Minimal MVC in JavaScriptMinimal MVC in JavaScript
Minimal MVC in JavaScriptMosky Liu
 
Introduction to Clime
Introduction to ClimeIntroduction to Clime
Introduction to ClimeMosky Liu
 
Working With WordPress Widgets
Working With WordPress WidgetsWorking With WordPress Widgets
Working With WordPress WidgetsKathy Gill
 
Automating and Accelerating Application Deployments to IBM WebSphere without ...
Automating and Accelerating Application Deployments to IBM WebSphere without ...Automating and Accelerating Application Deployments to IBM WebSphere without ...
Automating and Accelerating Application Deployments to IBM WebSphere without ...XebiaLabs
 
ZIPCodeTW: Find Taiwan ZIP Code by Address Fuzzily
ZIPCodeTW: Find Taiwan ZIP Code by Address FuzzilyZIPCodeTW: Find Taiwan ZIP Code by Address Fuzzily
ZIPCodeTW: Find Taiwan ZIP Code by Address FuzzilyMosky Liu
 
Graph-Tool in Practice
Graph-Tool in PracticeGraph-Tool in Practice
Graph-Tool in PracticeMosky Liu
 

Viewers also liked (19)

Learning Git with Workflows
Learning Git with WorkflowsLearning Git with Workflows
Learning Git with Workflows
 
Boost Maintainability
Boost MaintainabilityBoost Maintainability
Boost Maintainability
 
Socket Programming In Python
Socket Programming In PythonSocket Programming In Python
Socket Programming In Python
 
Beyond the Style Guides
Beyond the Style GuidesBeyond the Style Guides
Beyond the Style Guides
 
Programming with Python and PostgreSQL
Programming with Python and PostgreSQLProgramming with Python and PostgreSQL
Programming with Python and PostgreSQL
 
Learning Python from Data
Learning Python from DataLearning Python from Data
Learning Python from Data
 
Functional programming in Python
Functional programming in PythonFunctional programming in Python
Functional programming in Python
 
Programming with Python
Programming with PythonProgramming with Python
Programming with Python
 
Learn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesLearn 90% of Python in 90 Minutes
Learn 90% of Python in 90 Minutes
 
Golang iran - tutorial go programming language - Preliminary
Golang iran - tutorial  go programming language - PreliminaryGolang iran - tutorial  go programming language - Preliminary
Golang iran - tutorial go programming language - Preliminary
 
Golang #5: To Go or not to Go
Golang #5: To Go or not to GoGolang #5: To Go or not to Go
Golang #5: To Go or not to Go
 
Concurrency in Python
Concurrency in PythonConcurrency in Python
Concurrency in Python
 
Minimal MVC in JavaScript
Minimal MVC in JavaScriptMinimal MVC in JavaScript
Minimal MVC in JavaScript
 
Introduction to Clime
Introduction to ClimeIntroduction to Clime
Introduction to Clime
 
Working With WordPress Widgets
Working With WordPress WidgetsWorking With WordPress Widgets
Working With WordPress Widgets
 
Automating and Accelerating Application Deployments to IBM WebSphere without ...
Automating and Accelerating Application Deployments to IBM WebSphere without ...Automating and Accelerating Application Deployments to IBM WebSphere without ...
Automating and Accelerating Application Deployments to IBM WebSphere without ...
 
ZIPCodeTW: Find Taiwan ZIP Code by Address Fuzzily
ZIPCodeTW: Find Taiwan ZIP Code by Address FuzzilyZIPCodeTW: Find Taiwan ZIP Code by Address Fuzzily
ZIPCodeTW: Find Taiwan ZIP Code by Address Fuzzily
 
Graph-Tool in Practice
Graph-Tool in PracticeGraph-Tool in Practice
Graph-Tool in Practice
 
Python
PythonPython
Python
 

Similar to Programming with Python - Basic

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
 
PyCon2022 - Building Python Extensions
PyCon2022 - Building Python ExtensionsPyCon2022 - Building Python Extensions
PyCon2022 - Building Python ExtensionsHenry Schreiner
 
Practicing Python 3
Practicing Python 3Practicing Python 3
Practicing Python 3Mosky Liu
 
Python_Session
Python_SessionPython_Session
Python_Sessionsiva ram
 
Python and Pytorch tutorial and walkthrough
Python and Pytorch tutorial and walkthroughPython and Pytorch tutorial and walkthrough
Python and Pytorch tutorial and walkthroughgabriellekuruvilla
 
Beginning python programming
Beginning python programmingBeginning python programming
Beginning python programmingkanteshraj
 
Conda: A Cross-Platform Package Manager for Any Binary Distribution (SciPy 2014)
Conda: A Cross-Platform Package Manager for Any Binary Distribution (SciPy 2014)Conda: A Cross-Platform Package Manager for Any Binary Distribution (SciPy 2014)
Conda: A Cross-Platform Package Manager for Any Binary Distribution (SciPy 2014)Aaron Meurer
 
PyCon Taiwan 2013 Tutorial
PyCon Taiwan 2013 TutorialPyCon Taiwan 2013 Tutorial
PyCon Taiwan 2013 TutorialJustin Lin
 
Python 3.5: An agile, general-purpose development language.
Python 3.5: An agile, general-purpose development language.Python 3.5: An agile, general-purpose development language.
Python 3.5: An agile, general-purpose development language.Carlos Miguel Ferreira
 
OpenGurukul : Language : Python
OpenGurukul : Language : PythonOpenGurukul : Language : Python
OpenGurukul : Language : PythonOpen Gurukul
 
First python project
First python projectFirst python project
First python projectNeetu Jain
 
Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.
Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.
Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.Niraj Bharambe
 
The Python Book_ The ultimate guide to coding with Python ( PDFDrive ).pdf
The Python Book_ The ultimate guide to coding with Python ( PDFDrive ).pdfThe Python Book_ The ultimate guide to coding with Python ( PDFDrive ).pdf
The Python Book_ The ultimate guide to coding with Python ( PDFDrive ).pdfssuser8b3cdd
 
Build and deploy scientific Python Applications
Build and deploy scientific Python Applications  Build and deploy scientific Python Applications
Build and deploy scientific Python Applications Ramakrishna Reddy
 

Similar to Programming with Python - Basic (20)

05 python.pdf
05 python.pdf05 python.pdf
05 python.pdf
 
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
 
PyCon2022 - Building Python Extensions
PyCon2022 - Building Python ExtensionsPyCon2022 - Building Python Extensions
PyCon2022 - Building Python Extensions
 
Practicing Python 3
Practicing Python 3Practicing Python 3
Practicing Python 3
 
Python_Session
Python_SessionPython_Session
Python_Session
 
Python and Pytorch tutorial and walkthrough
Python and Pytorch tutorial and walkthroughPython and Pytorch tutorial and walkthrough
Python and Pytorch tutorial and walkthrough
 
Beginning python programming
Beginning python programmingBeginning python programming
Beginning python programming
 
A Python Tutorial
A Python TutorialA Python Tutorial
A Python Tutorial
 
Conda: A Cross-Platform Package Manager for Any Binary Distribution (SciPy 2014)
Conda: A Cross-Platform Package Manager for Any Binary Distribution (SciPy 2014)Conda: A Cross-Platform Package Manager for Any Binary Distribution (SciPy 2014)
Conda: A Cross-Platform Package Manager for Any Binary Distribution (SciPy 2014)
 
Hello World! with Python
Hello World! with PythonHello World! with Python
Hello World! with Python
 
PyCon Taiwan 2013 Tutorial
PyCon Taiwan 2013 TutorialPyCon Taiwan 2013 Tutorial
PyCon Taiwan 2013 Tutorial
 
Python 3.5: An agile, general-purpose development language.
Python 3.5: An agile, general-purpose development language.Python 3.5: An agile, general-purpose development language.
Python 3.5: An agile, general-purpose development language.
 
OpenGurukul : Language : Python
OpenGurukul : Language : PythonOpenGurukul : Language : Python
OpenGurukul : Language : Python
 
First python project
First python projectFirst python project
First python project
 
Why learn python in 2017?
Why learn python in 2017?Why learn python in 2017?
Why learn python in 2017?
 
Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.
Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.
Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.
 
The Python Book_ The ultimate guide to coding with Python ( PDFDrive ).pdf
The Python Book_ The ultimate guide to coding with Python ( PDFDrive ).pdfThe Python Book_ The ultimate guide to coding with Python ( PDFDrive ).pdf
The Python Book_ The ultimate guide to coding with Python ( PDFDrive ).pdf
 
week1.ppt
week1.pptweek1.ppt
week1.ppt
 
Build and deploy scientific Python Applications
Build and deploy scientific Python Applications  Build and deploy scientific Python Applications
Build and deploy scientific Python Applications
 
Python Projects at Neova
Python Projects at NeovaPython Projects at Neova
Python Projects at Neova
 

More from Mosky Liu

Statistical Regression With Python
Statistical Regression With PythonStatistical Regression With Python
Statistical Regression With PythonMosky Liu
 
Data Science With Python
Data Science With PythonData Science With Python
Data Science With PythonMosky Liu
 
Hypothesis Testing With Python
Hypothesis Testing With PythonHypothesis Testing With Python
Hypothesis Testing With PythonMosky Liu
 
Elegant concurrency
Elegant concurrencyElegant concurrency
Elegant concurrencyMosky Liu
 
Simple Belief - Mosky @ TEDxNTUST 2015
Simple Belief - Mosky @ TEDxNTUST 2015Simple Belief - Mosky @ TEDxNTUST 2015
Simple Belief - Mosky @ TEDxNTUST 2015Mosky Liu
 
Dive into Pinkoi 2013
Dive into Pinkoi 2013Dive into Pinkoi 2013
Dive into Pinkoi 2013Mosky Liu
 
MoSQL: More than SQL, but Less than ORM @ PyCon APAC 2013
MoSQL: More than SQL, but Less than ORM @ PyCon APAC 2013MoSQL: More than SQL, but Less than ORM @ PyCon APAC 2013
MoSQL: More than SQL, but Less than ORM @ PyCon APAC 2013Mosky Liu
 
MoSQL: More than SQL, but less than ORM
MoSQL: More than SQL, but less than ORMMoSQL: More than SQL, but less than ORM
MoSQL: More than SQL, but less than ORMMosky Liu
 

More from Mosky Liu (8)

Statistical Regression With Python
Statistical Regression With PythonStatistical Regression With Python
Statistical Regression With Python
 
Data Science With Python
Data Science With PythonData Science With Python
Data Science With Python
 
Hypothesis Testing With Python
Hypothesis Testing With PythonHypothesis Testing With Python
Hypothesis Testing With Python
 
Elegant concurrency
Elegant concurrencyElegant concurrency
Elegant concurrency
 
Simple Belief - Mosky @ TEDxNTUST 2015
Simple Belief - Mosky @ TEDxNTUST 2015Simple Belief - Mosky @ TEDxNTUST 2015
Simple Belief - Mosky @ TEDxNTUST 2015
 
Dive into Pinkoi 2013
Dive into Pinkoi 2013Dive into Pinkoi 2013
Dive into Pinkoi 2013
 
MoSQL: More than SQL, but Less than ORM @ PyCon APAC 2013
MoSQL: More than SQL, but Less than ORM @ PyCon APAC 2013MoSQL: More than SQL, but Less than ORM @ PyCon APAC 2013
MoSQL: More than SQL, but Less than ORM @ PyCon APAC 2013
 
MoSQL: More than SQL, but less than ORM
MoSQL: More than SQL, but less than ORMMoSQL: More than SQL, but less than ORM
MoSQL: More than SQL, but less than ORM
 

Recently uploaded

Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024MulesoftMunichMeetup
 
Lessons Learned from Building a Serverless Notifications System.pdf
Lessons Learned from Building a Serverless Notifications System.pdfLessons Learned from Building a Serverless Notifications System.pdf
Lessons Learned from Building a Serverless Notifications System.pdfSrushith Repakula
 
Workshop - Architecting Innovative Graph Applications- GraphSummit Milan
Workshop -  Architecting Innovative Graph Applications- GraphSummit MilanWorkshop -  Architecting Innovative Graph Applications- GraphSummit Milan
Workshop - Architecting Innovative Graph Applications- GraphSummit MilanNeo4j
 
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...drm1699
 
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypseTomasz Kowalczewski
 
GraphSummit Milan & Stockholm - Neo4j: The Art of the Possible with Graph
GraphSummit Milan & Stockholm - Neo4j: The Art of the Possible with GraphGraphSummit Milan & Stockholm - Neo4j: The Art of the Possible with Graph
GraphSummit Milan & Stockholm - Neo4j: The Art of the Possible with GraphNeo4j
 
Encryption Recap: A Refresher on Key Concepts
Encryption Recap: A Refresher on Key ConceptsEncryption Recap: A Refresher on Key Concepts
Encryption Recap: A Refresher on Key Conceptsthomashtkim
 
^Clinic ^%[+27788225528*Abortion Pills For Sale In witbank
^Clinic ^%[+27788225528*Abortion Pills For Sale In witbank^Clinic ^%[+27788225528*Abortion Pills For Sale In witbank
^Clinic ^%[+27788225528*Abortion Pills For Sale In witbankkasambamuno
 
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...Abortion Clinic
 
BusinessGPT - Security and Governance for Generative AI
BusinessGPT  - Security and Governance for Generative AIBusinessGPT  - Security and Governance for Generative AI
BusinessGPT - Security and Governance for Generative AIAGATSoftware
 
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdfThe Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdfkalichargn70th171
 
Weeding your micro service landscape.pdf
Weeding your micro service landscape.pdfWeeding your micro service landscape.pdf
Weeding your micro service landscape.pdftimtebeek1
 
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...Lisi Hocke
 
^Clinic ^%[+27788225528*Abortion Pills For Sale In harare
^Clinic ^%[+27788225528*Abortion Pills For Sale In harare^Clinic ^%[+27788225528*Abortion Pills For Sale In harare
^Clinic ^%[+27788225528*Abortion Pills For Sale In hararekasambamuno
 
Microsoft365_Dev_Security_2024_05_16.pdf
Microsoft365_Dev_Security_2024_05_16.pdfMicrosoft365_Dev_Security_2024_05_16.pdf
Microsoft365_Dev_Security_2024_05_16.pdfMarkus Moeller
 
Evolving Data Governance for the Real-time Streaming and AI Era
Evolving Data Governance for the Real-time Streaming and AI EraEvolving Data Governance for the Real-time Streaming and AI Era
Evolving Data Governance for the Real-time Streaming and AI Eraconfluent
 
GraphSummit Milan - Visione e roadmap del prodotto Neo4j
GraphSummit Milan - Visione e roadmap del prodotto Neo4jGraphSummit Milan - Visione e roadmap del prodotto Neo4j
GraphSummit Milan - Visione e roadmap del prodotto Neo4jNeo4j
 
^Clinic ^%[+27788225528*Abortion Pills For Sale In soweto
^Clinic ^%[+27788225528*Abortion Pills For Sale In soweto^Clinic ^%[+27788225528*Abortion Pills For Sale In soweto
^Clinic ^%[+27788225528*Abortion Pills For Sale In sowetokasambamuno
 
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...naitiksharma1124
 
Effective Strategies for Wix's Scaling challenges - GeeCon
Effective Strategies for Wix's Scaling challenges - GeeConEffective Strategies for Wix's Scaling challenges - GeeCon
Effective Strategies for Wix's Scaling challenges - GeeConNatan Silnitsky
 

Recently uploaded (20)

Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024
 
Lessons Learned from Building a Serverless Notifications System.pdf
Lessons Learned from Building a Serverless Notifications System.pdfLessons Learned from Building a Serverless Notifications System.pdf
Lessons Learned from Building a Serverless Notifications System.pdf
 
Workshop - Architecting Innovative Graph Applications- GraphSummit Milan
Workshop -  Architecting Innovative Graph Applications- GraphSummit MilanWorkshop -  Architecting Innovative Graph Applications- GraphSummit Milan
Workshop - Architecting Innovative Graph Applications- GraphSummit Milan
 
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
 
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
 
GraphSummit Milan & Stockholm - Neo4j: The Art of the Possible with Graph
GraphSummit Milan & Stockholm - Neo4j: The Art of the Possible with GraphGraphSummit Milan & Stockholm - Neo4j: The Art of the Possible with Graph
GraphSummit Milan & Stockholm - Neo4j: The Art of the Possible with Graph
 
Encryption Recap: A Refresher on Key Concepts
Encryption Recap: A Refresher on Key ConceptsEncryption Recap: A Refresher on Key Concepts
Encryption Recap: A Refresher on Key Concepts
 
^Clinic ^%[+27788225528*Abortion Pills For Sale In witbank
^Clinic ^%[+27788225528*Abortion Pills For Sale In witbank^Clinic ^%[+27788225528*Abortion Pills For Sale In witbank
^Clinic ^%[+27788225528*Abortion Pills For Sale In witbank
 
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
 
BusinessGPT - Security and Governance for Generative AI
BusinessGPT  - Security and Governance for Generative AIBusinessGPT  - Security and Governance for Generative AI
BusinessGPT - Security and Governance for Generative AI
 
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdfThe Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
 
Weeding your micro service landscape.pdf
Weeding your micro service landscape.pdfWeeding your micro service landscape.pdf
Weeding your micro service landscape.pdf
 
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
 
^Clinic ^%[+27788225528*Abortion Pills For Sale In harare
^Clinic ^%[+27788225528*Abortion Pills For Sale In harare^Clinic ^%[+27788225528*Abortion Pills For Sale In harare
^Clinic ^%[+27788225528*Abortion Pills For Sale In harare
 
Microsoft365_Dev_Security_2024_05_16.pdf
Microsoft365_Dev_Security_2024_05_16.pdfMicrosoft365_Dev_Security_2024_05_16.pdf
Microsoft365_Dev_Security_2024_05_16.pdf
 
Evolving Data Governance for the Real-time Streaming and AI Era
Evolving Data Governance for the Real-time Streaming and AI EraEvolving Data Governance for the Real-time Streaming and AI Era
Evolving Data Governance for the Real-time Streaming and AI Era
 
GraphSummit Milan - Visione e roadmap del prodotto Neo4j
GraphSummit Milan - Visione e roadmap del prodotto Neo4jGraphSummit Milan - Visione e roadmap del prodotto Neo4j
GraphSummit Milan - Visione e roadmap del prodotto Neo4j
 
^Clinic ^%[+27788225528*Abortion Pills For Sale In soweto
^Clinic ^%[+27788225528*Abortion Pills For Sale In soweto^Clinic ^%[+27788225528*Abortion Pills For Sale In soweto
^Clinic ^%[+27788225528*Abortion Pills For Sale In soweto
 
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
 
Effective Strategies for Wix's Scaling challenges - GeeCon
Effective Strategies for Wix's Scaling challenges - GeeConEffective Strategies for Wix's Scaling challenges - GeeCon
Effective Strategies for Wix's Scaling challenges - GeeCon
 

Programming with Python - Basic

  • 1. Programming with Python Basic Topics Mosky 1
  • 2. Mosky: ● The examples and the PDF version are available at: – j.mp/mosky-programming-with-python. ● It is welcome to give me any advice of this slide or ask me the answers of the challenges. – mosky.tw 2
  • 3. Mosky ● Projects ● Pinkoi staff pinkoi.com – MoSQL mosql.mosky.tw – Clime ● PyCon JP '12 Speaker clime.mosky.tw pycon.jp – Apt-Pool Apt-Add ● PyCon TW '12 Speaker pycon.tw … 3
  • 4. Advertisement ● PyCon Taiwan 2013 ● COSCUP 2013 – pycon.tw – coscup.org – 5/25-26 @ Sinica – 8/3-4 @ TICC – coscup-gereral@googlegroups.co – pythontw@googlegroups.com m 4
  • 5. Topics ● Basic Topics ● Adv. Topics – Python 2 or 3? – Module and Package – Environment – Typing – hello.py – Comprehension – Common Types – Functional Technique – Flow Control – Object-oriented Prog. – File I/O – Useful Libraries – Documentation ● Final Project – Scope – A Blog System 5
  • 6. An Investigation Do you know _________ ? – any other programming language – Object-oriented – Static Typing; Strong and Weak Typing – Dynamic Typing – Functor; Closure – Functional Programming – Web development 6
  • 7. Python 2 or 3? in short. 7
  • 8. Python 2 or 3? ● Python 2.x ● Python 3.x – status quo – present & future – 2.7 is end-of-life release – under active development – harder for newcomers – easier for newcomers – more third-party lib. – less third-party lib. – 2to3.py – 3to2.py – backported features: – new features: ● What's News in Python 2.6 ● What's News in Python 3.0 docs.python.org/release/2.6.4/whatsnew/2.6.html docs.python.org/py3k/whatsnew/3.0.html ● What's News in Python 2.7 docs.python.org/dev/whatsnew/2.7.html 8
  • 9. Python 2 or 3? (cont.) ● Use Python 3 if you can. ● Decide Python 2 or 3 by the libraries you will use. ● Today, we will go ahead with Python 2. And introduce you to the changes in Python3. 9
  • 10. Environment Is a python in your computer? 10
  • 11. On Linux or Mac ● Python is built-in on Linux or Mac. ● All you have to do is check the version. Type "python" in any terminal. Python 2.7.3 (default, Sep 26 2012, 21:51:14) [GCC 4.7.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> 11
  • 12. On Windows ● Download the installer from: "http://python.org/download" ● Install it. ● Add the Python's PATH. – Computer → System Properties → Advanced system settings → Advanced tab → Environment Variables → System Variables → find PATH. – "...;C:Python27" 12
  • 13. Editor / IDE ● The Editors ● The IDE – Sublime Text 2 – IDLE www.sublimetext.com ● Debian-base: – VIM sudo apt-get install idle wiki.python.org/moin/Vim ● Windows: – Gnome Text Editor Use the Start Menu to (gedit) search "IDLE" – Notepad++ ● The others: notepad-plus-plus.org – wiki.python.org/moin/PythonEditors – ... 13
  • 14. The Python Shell ● Type "python" in terminal. – >>> – ... ● Leaving a shell: – exit() – Linux or Mac: Ctrl+D – Windows: Ctrl+Z<Enter> 14
  • 15. The python Command ● Enter Python shell without arguments. ● python hello.py ● python -c 'print "Hello, World!"' ● python -m SimpleHTTPServer 15
  • 16. hello.py Say hello to Python. 16
  • 17. hello.py #!/usr/bin/env python ● #! the shebang. # -*- coding: utf-8 -*- ● # -*- defines the encoding # file: hello.py of this file. ● # means the comments. def hello(name=None): if name:n ● : starts a block. return 'Hello, %s!' % name else: ● A block uses 4-space indent. return 'Hello, Python!' ● A statement ends with n. 17
  • 18. hello.py (cont.) if __name__ == '__main__': ● __name__, the name of module. import sys if len(sys.argv) >= 2: ● import is important. print The usage: hello(sys.argv[1]) else: ● import sys print hello() ● from sys import argv ● … as alias 18
  • 19. 19
  • 20. The print Statement print 'End with a new line char.' print 'Print', 'multiple', 'strings.' print 'End with a space.', print # print a new line char 20
  • 21. The print function in Python 3 print('End with a new line char.') print('Print', 'multiple', 'strings.') print('End with a space.', end=' ') print() # print a new line char print('End with a space.', end='') print('a', 'b', 'c', seq=',') 21
  • 22. Common Types Without it we can do noting 22
  • 23. Common Types ● Numeric ● Sequence – Integer 100 – String "" – Float 10.0 – Unicode u"" – Long 100L – List [,] – Complex 1+1j – Tuple (,) – Boolean True, False 23
  • 24. Common Types (cont.) ● Mapping – Dictionary {:} ● Set – Set {,} – Frozen Set forzenset(...) 24
  • 25. Integer, Float and Long ● 3+3 ● divmod(5, 2) → tuple (not numeric) ● 3-3 ● 5/2 ● 3*3 → int (truncated) 5.0/2 3/3 ● ● → float ● 6/2*(1+2) (as double in C) ● 5.0//2 → int → float (floored) (as long in C) ● 2**1000 → long (∞ precision) 25
  • 26. Integer and Float in Python 3 ● 3+3 ● divmod(5, 2) → tuple (not numeric) ● 3-3 ● 5/2 ● 3*3 → float 5.0/2 3/3 ● ● → float ● 6/2*(1+2) (as double in C) ● 5.0//2 → int → float (floored) (∞ precision) ● 2**1000 → int (∞ precision) 26
  • 27. Note: The Variables ● x = 1 ● x < y ● x + 1 → True → 2 ● y = 2 ● bin(y) ● x + y → '0b101' → 3 ● bin(y | 0b011) y += 3 → '0b111' ● → 5 27
  • 28. A Trap of the Integer ● weight = 49 ● height = 163 ● bmi = weight / (height / 100) ** 2 ● bmi → 49 ● (height / 100) → 1 28
  • 29. A Trap of the Integer (cont.) ● weight = 49.0 ● height = 163.0 ● bmi = weight / (height / 100) ** 2 ● bmi → 18.442545824080696 29
  • 30. Complex ● 1j * 1J ● a = 3.0+4.0j ● 1j * complex(0,1) ● float(a) → TypeError ● 3 + 1j*3 ● a.real ● (3+1j)*3 → 3.0 ● (1+2j)/(1+1j) ● a.imag → complex → 4.0 ● abs(a) # = sqrt(a.real**2 + a.imag**2) → 5 30
  • 31. Boolean ● not False Comparison: ● True and True – 10 < 100 ● False or True – 10 < 10.0 – 10 <= 10.0 – 10 == 10.0 ● False +1 → 1 – 10 != 10.0 – x is y ● True +1 → 2 31
  • 32. String and Unicode '...' is equal to "..." Functions – ord( 'A') String (immutable seq.) – chr(65) – ' 中文 ' – ord(u' 中 ') – ' 嗨, nPython ! ' – unichr(20013); chr(20013) – r' 嗨, nPython ! ' Decoding (String → Unicode) – ' 中文 '.decode('utf-8') – ''' ... ''' – unicode( ' 中文 ', 'utf-8') Unicode (immutable seq.) Encoding (Unicode → String) – u' 嗨, nPython ! ' – u' 中文 '.encode('utf-8') – ur' 嗨, nPython ! ' – str(u' 中文 ') – u''' ... ''' – str(u' 中文 ', 'utf-8') 32
  • 33. Bytes and String in Python 3 '...' is equal to "..." Functions – ord(b'A') Bytes (immutable seq.) – chr(65) – b' 中文 ' – ord( ' 中 ') – b' 嗨, nPython ! ' – unichr(20013); chr(20013) – br' 嗨, nPython ! ' Decoding (Bytes → String) – b' 中文 '.decode('utf-8') – b''' ... ''' – str(b' 中文 ', 'utf-8') String (immutable seq.) Encoding (String → Bytes) – ' 嗨, nPython ! ' – ' 中文 '.encode('utf-8') – r' 嗨, nPython ! ' – bytes( ' 中文 ') – ''' ... ''' – bytes( ' 中文 ', 'utf-8') 33
  • 34. Unicode Does Matter! ● b = ' 中文 ' ● len(b) → 6 ● len(b.decode('utf-8')) → 2 34
  • 35. String and Unicode (cont.) ● They have a lot of methods: capitalize center count decode encode endswith expandtabs find rfind format index rindex isalnum isalpha isdigit islower isspace istitle isupper join ljust rjust lower partition rpartition replace split rsplit splitlines startswith rstrip strip lstrip swapcase title translate upper zfill ● ref: docs.python.org/2/library/stdtypes.html#string-methods 35
  • 36. String and Unicode (cont.) String formatting: – % (modulo) ● ref: docs.python.org/2/library/stdtypes.html#string-formatting-operations – str.format ● ref: docs.python.org/2/library/string.html#formatstrings 36
  • 37. List and Tuple List (mutable seq.) Tuple (seq.) – [] – tuple() – ['item'] – ('item', ) – ['s', 100, u'unicode'] – ('s', 100, u'unicode') – list('abc') – tuple('abc') – 'a b c'.split(' ') – 'n'.join(['spam', – 'n'.join(('spam', 'eggs']) 'eggs')) – x, y = [1, 2] – x, y = (1, 2) – x, y = [y, x] – x, y = (y, x) 37
  • 38. Sequence Sequence Mutable Seq. – s[i] = x – x in s # performance? – s[i:j] = t – x not in s – del s[i:j] – s + t – s[i:j:k] = t – s * n, n * s – s.append(x) – s.insert(i, x) – s[i] – s.pop([i]) – s[i:j] – s.remove(x) # performance? – s[i:j:k] – s.extend(t) in-place – len(s) – s.sort([cmp[, key[, reverse]]]) – s.index(x) – s.sort([key[, reverse]]) # Py 3 – s.count(x) – s.reverse() 38
  • 39. Sequence Comparison ● (0, 0, 0) < (0, 0, 1) ● [0, 0, 0] < [0, 0, 1] ● (0, ) < (0, 0) ● 'ABC' < 'C' < 'Pascal' < 'Python' ● (1, 2, 3) == (1.0, 2.0, 3.0) ● 'A' == 'A' ● 'A' > 65 ● 'A' > 66 ● ('A', ) > (66, ) 39
  • 40. Sequence Comparison in Python 3 ● (0, 0, 0) < (0, 0, 1) ● [0, 0, 0] < [0, 0, 1] ● (0, ) < (0, 0) ● 'ABC' < 'C' < 'Pascal' < 'Python' ● (1, 2, 3) == (1.0, 2.0, 3.0) ● 'A' == 'A' ● 'A' > 65 → TypeError ● 'A' > 66 → TypeError ● ('A', ) > (66, ) → TypeError 40
  • 41. Sequence (cont.) Slicing and Slice object: – s = range(10) – s = 'I am a str.' – t = s – s[:-3] – t[0] = 'A' – print s – s.reverse() – t is s → TypeError – s[::-1] – t = s[:] – ''.join(reversed(s)) – t is s – slice(None, None, -1) 41
  • 42. Mapping Dict. (mutable map.) – len(d) – {} – d[k] – {'A ': 1, 'B': 2, 'C': 3} – d[k] = v – dict({...}) – del d[k] – dict(A=1, B=2, C=3) – k in d, k not in d – d.copy() – d.get(key[, default]) – k = 'ABC' – d.setdefault(key[, default]) – v = [1, 2, 3] – d.items(), d.keys(), d.values() – pairs = zip(k, v) – d.pop(key[, default) – dict(pairs) – d.update([other]) ... 42
  • 43. Set Set (mutable set) – len(s) – set() – x in s, x not in s – {'A', 'B', 'C'} # Py3 – s.copy() – s.add(elem) – set('ABC') – s.discard(elem) – set(['A','B','C']) – s.pop() – s |= other – s &= other – s | other | ... – s & other & ... – s < | <= | == | > = | > other ... 43
  • 44. Flow Control in Python is grace and easy to learn. 44
  • 45. The if Statement if [condition 1]: … elif [condition 2]: … elif [condition 3]: … else: … [exp. if true] if [condition] else [exp. if false] 45
  • 46. Truth Value Testing They are same as False in a boolean context: – None – False – Zeros (ex. 0, 0.0, 0L, 0j) – Empty containers (ex. '', [], {}) – __nonzero__() or __len__() returns 0 or False 46
  • 47. Truth Value Testing (cont.) ● if not None: ... ● if not []: ... ● if [0]: ... ● if [[]]: ... ● if "": ... ● if {}: ... ● if not {0: False}: … … 47
  • 48. The for Statement for [item] in [iterable]: for i in [0, 1, 2]: … print i for i in range(3): for i in xrange(3): print i print i 48
  • 49. The for Statement in Python 3 for [item] in [iterable]: for i in [0, 1, 2]: … print i for i in range(3): for i in xrange(3): print i print i 49
  • 50. The for Statement (cont.) for i in range(1, 3): for i in range(3, -1, -1): print i print i s = [1, 2, 3] s = [...] t = 'xyz' for i, item in enumerate(s): print i, item for i, j in zip(s, t): print i, j 50
  • 51. The for Statement (cont.) ● It is like for … each in other language. – Note: Python hasn't other for loop. ● It can iterate all of iterable object. – In other words, the object which defined __iter__. – ex. sequence, mapping, set, ... 51
  • 52. Challenge 1: A Pyramid ● Use for loop to build a * pyramid on right. *** – without limit. ***** – limit: in two lines ******* ● hint: string formatting 52
  • 53. Challenge 2-1: Count the Chars ● Use for loop to count "Please count the the sentence on right. characters here." – without limit. – limit: without if ● hint: use get {'P': 1, ...} 53
  • 54. Challenge 2-2: Collect the Chars ● Use for loop to collect "Here are UPPERCASE the chars. and lowercase chars." – limit: use setdefault {'c': ['C', 'c', 'c'], ...} 54
  • 55. The while Statement tasks = [...] while tasks: while 1: … … ● It leaves the loop once ● A infinite loop. the tasks is empty. ● It is better to use block mechanism in a loop. – ex. I/O block 55
  • 56. The break, continue Statement loop …: loop …: if …: break if …: continue ● It terminates a loop. ● It continues with the next iteration. 56
  • 57. The break, continue Statement (cont.) ● They do the same thing in both C and Python. ● Using break or continue is encouraged. – take the place of the complicated condition in a while. – faster, because Python is interpreted. ● Just use them. 57
  • 58. The pass Statement ● Do nothing. 58
  • 59. The else Clause on Loops loop …: … else: … ● No a clause on the if statement! ● If the loop isn't broken by any break statement, the else block is executed. ● It replaces the flags we usually used. 59
  • 60. Challenge 3-1: The Primes ● Try to filter the primes [2, 3, 5, 7, 11, 13, from [2, 100). 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, – without limit. 59, 61, 67, 71, 73, – limit: use loop's else 79, 83, 89, 97] 60
  • 61. The try Statement try: … except LookupError, e: … except (IndexError, KeyError), e: … else: … finally: … 61
  • 62. The try Statement in Python 3 try: … except LookupError as e: … except (IndexError, KeyError) as e: … else: … finally: … 62
  • 63. The try Statement (cont.) ● For avoiding to catch the exception we don't expect, you should: – reduce your code in try block. ● move them to else block. – make the exception precise in except statement. ● Avoid using Exception. ● ref: docs.python.org/2/library/exceptions.html#exception-hierarchy ● Release the resource in finally block. – or use context manager – ex. file, socket, … ● raise SomeError 63
  • 64. The def Statement def f(x, y): def f(x, y=2): return (x, y) return (x, y) f(1, 2) f(1) f(y=2, x=1) f(x=1) f(*(1, 2)) f(*(1, )) f(**{'y': 2, 'x': 1}) f(**{'x': 1}) 64
  • 65. The def Statement (cont.) def f(*args): def f(**kargs): return args return kargs f(1, 2, 3) # f(1, 2) # → TypeError f(x=1, y=2, z=3) # f(y=2, x=1) # → TypeError # f(*(1, 2)) # → TypeError f(*(1, 2, 3, 4)) f(**{'x': 1, 'y': 2, 'z': 3}) # f(**{'y': 2 ,'x': 1}) # → TypeError 65
  • 66. The def Statement (cont.) def f(x, *args): def f(x, **kargs): return x, args return kargs f(1, 2, 3) # f(1, 2) # → TypeError f(x=1, y=2, z=3) # f(y=2, x=1) # → TypeError # f(*(1, 2)) # → TypeError f(*(1, 2, 3, 4)) f(**{'x': 1, 'y': 2, 'z': 3}) # f(**{'y': 2, 'x': 1}) # → TypeError 66
  • 67. The def Statement (cont.) def f(*args, y): def f(*args, **kargs): return kargs return args, kargs → SyntaxError f(1, 2, 3) f(y=2, x=1) f(*(1, 2, 3, 4)) f(**{'y': 2, 'x': 1}) 67
  • 68. The def Statement in Python 3 def f(*args, k): def f(*args, k, **kargs): return kargs return args, kargs F(1, 2, 3) f(1, 2, 3) # f(x=1, k=2) # → TypeError f(x=1, k=2) f(*(1, 2, 3, 4)) f(*(1, 2, 3, 4)) # f(**{'x': 1, 'k': 2}) # → TypeError f(**{'x': 1, 'k': 2}) 68
  • 69. The def Statement (cont.) def f(): pass def g(): pass d = {'x': f, 'y': g} d['x']() ● Python functions are first-class functions. – It means you can pass functions as arguments, and assign functions to variables. – It is like the function pointers in C. 69
  • 70. An Example of Using while, try and def. # file: ex_try.py $ python ex_try.py def take_int(prompt='Give me a int: '): Give me a int: str while 1: try: It is not a int! user_input = int(raw_input(prompt)) Give me a int: abc except ValueError, e: print 'It is not a int!' It is not a int! else: return user_input Give me a int: 100 if __name__ == '__main__': I got a int from user: x = take_int() 100 print 'I got a int from user: %d' % x $ 70
  • 71. A Trap of the Default Value # file: ex_defval_trap.py ● Because the list is created when the def f(items=[]): function is defined. items.append(1) return items ● Avoid to use the mutable types as the if __name__ == '__main__': default value. print f() # -> [1] print f() # -> [1, 1] print f() # -> [1, 1, 1] 71
  • 72. Challenge 4: A BMI Calculator ● BMI: Body Mass Index Enter your height (M): – BMI = weight (KG) ÷ height (M)2 1.63 – < 18.5 → Underweight Enter your weight (KG): – [18.5, 25) → Normal weight – [25, 30) → Overweight 49 – >= 30 → Obesity --- ● Write a BMI calculator. Your BMI is: – without limit. – limit: only one if 18.44 (Underweight) ● hint: use loop Ideal weight is between: 49.15 ~ 66.42 72
  • 73. File I/O Open anything with the open. 73
  • 74. The file Object f = open('input.txt') f = print f.read() open('output.txt', 'w') f.seek(0) f.write('a line.n') for line in f: f.close() print line, f.close() 74
  • 75. The Context Manager with open('input.txt') as f: for line in f: print line, f.close() ● Python 2.5↑ – Python 2.5.x: from __future__ import with_statement – Python 2.6↑: It is mandatory. 75
  • 76. Challenge 2: Count the Chars (cont.) – limit 3: with the files The path of input: input.txt The path of output: output.txt --- The result was written. 76
  • 77. The csv Moudle #!/usr/bin/env python 1, apple # -*- coding: utf-8 -*- 2, orange # file: ex_csv.py 3, watermelon import csv ['1', ' apple'] with open('ex_csv.csv') as f: ['2', ' orange'] for row in csv.reader(f): print row ['3', ' watermelon'] 77
  • 78. The os.path Moudle # file: ex_os_path.py $ python ex_os_path.py from os import walk from os.path import join It requires a path as def list_files(path): paths = [] argument. for root, dir_names, file_names in walk(path): for file_name in file_names: paths.append(join(root, file_name)) $ python ex_os_path.py . return paths …/1 if __name__ == '__main__': import sys …/b/4 from os.path import abspath, dirname if len(sys.argv) == 2: …/a/2 path = abspath(dirname(sys.argv[1])) for path in list_files(path): …/a/3 print path else: print 'It requires a path as argument.' 78
  • 80. The help Function ● In Python shell: ● In terminal: – help(open) – $ pydoc SimpleHTTPServer – dir(open) – $ pydoc csv – $ pydoc os.path – 'n'.join(dir(open)) 80
  • 81. Your Documentation $ pydoc ex_doc # file: ex_doc.py Help on module ex_doc: '''module-level doc.''' NAME ex_doc - module-level doc. def f(x): FILE '''A short sentence describes this function. /home/mosky/programming-with-python/ex_doc.py FUNCTIONS About the parameters, return f(x) value or any other detail ... A short sentence describes this ''' function. pass About the parameters, return value or any other detail ... 81
  • 83. Scope # file: ex_scope.py $ python ex_scope.py global x = 'global' local def f(): $ if 1: x = 'local' return x ● Scopes are decided by functions. if __name__ == '__main__': print x print f() 83
  • 84. The LEGB Rule # file: ex_LEGB.py ● return … global_var = 100 – Local (in function) def f(): enclosed_var = 10 – Enclosed def g(): – Global local_var = 1 return sum([local_var, enclosed_var, – Built-in global_var]) return g() if __name__ == '__main__': print f() # -> 111 84
  • 85. Challenge 3-2: The Primes (cont.) – limit 1: Sieve of [2, 3, 5, 7, 11, 13, Eratosthenes. 17, 19, 23, 29, 31, – limit 2: use set. 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] 85
  • 86. Challenge 5: Mix All ● You have many $ python mix.py pyramid 10 functions now. … $ python mix.py primes 100 Try to write a CLI … program to trigger your $ python mix.py bmi 1.63 49 functions. … – without limit $ python mix.py blah blah – limit: without if. Please check your args. 86
  • 87. Adv. Topics There is another slide. 87