Upload
shawn-woods
View
243
Download
0
Embed Size (px)
Citation preview
Python tutorial若手幹事
To b e g i n w i t h …
2
minimal python features for reading and extending the python source code on the lecture after that
In this tutorial…
More details…https://docs.python.org/2.7/index.html (English)
http://docs.python.jp/2.7/index.html ( 日本語 )
https://www.google.co.jp/search?q=python (Google 先生 )
C o n t e n t s
3
What is Python? Basic build-in type More control tools Defining Functions Module Class File I/O Using Fortran native code
C o n t e n t s
4
What is Python? Basic build-in type More control tools Defining Functions Module Class File I/O Using Fortran native code
W h a t i s P y t h o n ?
5
Python is an interpreted language, so no compilation and linking is necessary
The high-level data types allow you to express complex operations in a single statement
Statement grouping is done by indentation instead of beginning and ending brackets
No variable or argument declarations are necessary Supports multiple programing paradigms, including
object-oriented, functional programming style Third-party tools, namely site-packages, are extensive
V e r s i o n o f P y t h o n
6
Python 2x (latest: 2.7.10)There are two versions of Python
Which version should I use?Mostly dependent on what you want to get done.
Python 3x (latest: 3.4.3)
Characters of Python 2x and 3x Some parts are not compatible Development of Python 2x has been completed, but some of
the improvements in 3.1 have been backported to 2.7 Python 3x has slightly worse library (site-package) support Some are phasing out Python 2x as preinstalled default
W h a t a r e t h e d i f fe r e n c e s ?
7
The print statement has been replaced with a print() function- Python 2x: print “Hello world”- Python 3x: print(“Hello world”)
Binary data and Unicode has changed- Python 2x: “…” (Byte); u“…” (Unicode)- Python 3x: “…” (Unicode); u“…” (ERROR for
3.0~3.2) The data type “long” renamed “int”
Therefore, there is only one built-in integer type
U s i n g t h e P y t h o n I n t e r p r e t e r ( 1 )
Interactive Mode (i.e., command line)- Windows: Command prompt ( + R “cmd”)⇒- Macintosh: Terminal (Ctrl + Space “terminal”)⇒
$> pythonPython 2.7.10 (May 23 2015, 09:40:32)Type "help", "copyright", "credits" or "license" for more information.
>>> print “Hello world”Hello world>>> 1 + 12>>> quit() # exit python interpreter
U s i n g t h e P y t h o n I n t e r p r e t e r ( 2 )
9
Using python script- Edit python script
- Open terminal and run the script file
$> scriptname.pyHello world2
scriptname.py
1: print “Hello world”2: print 1 + 1 # Need the “print” in order to display
C o n t e n t s
10
What is Python? Basic build-in type More control tools Defining Functions Module Class File I/O Using Fortran native code
B u i l t - i n T y p e s
11
Numeric types – int, float, long, complex
Sequence types – str, list, tuple, etc. Mapping type – dict Iterator types File object (in File I/O) Boolean operations – and, or, not Truth value testing Array (from NumPy) etc.
N u m e r i c t y p e s ( 1 )
12
$> python>>> 1 + 2 * (3 + 4)15>>> 17 / 3 # int / int -> int5>>> 17 / 3.0 # int / float -> float5.666666666666667>>> 17 // 3.0 # explicit floor division5.0>>> 17 % 3 # the “%” operator means modulo operator
2
N u m e r i c t y p e s ( 2 )
13
… Continued from the previous page …>>> 2 ** 5 # 2 to the power of 532>>> width = 20 # no result is displayed>>> height = 2 * 6>>> width * height240>>> 8.0 / 3.02.6666666666666665>>> round(8.0 / 3.0, 2) # result rounded to 2 digit
2.67
N u m e r i c t y p e s ( 3 )
14
… Continued from the previous page …>>> 1j * 1J # numeric literal + ('j' or 'J‘) is complex
(-1+0j)>>> 1j * complex(0, 1)(-1+0j)>>> a = 1.5 + 0.5j>>> a.real1.5>>> a.imag0.5
S e q u e n c e t y p e s ( 1 )
15
>>> var = [1, 2, 3, 4, 5, 6]>>> var[0] # component in position 01 # !!! index starts at 0 !!!>>> var[-2] # second-last component5
Sequence the order that something exists in (ref. Longman) ex.) (1, 2, 3, 4, 5, 6) : sequence of numbers
Features of sequence types Access (getter)
S e q u e n c e t y p e s ( 2 )
16
>>> var = [1, 2, 3, 4, 5, 6]>>> var[3] = 7 # replace the component in position 3
>>> var[1, 2, 3, 7, 5, 6]
Replace (setter)
>>> var = [1, 2, 3, 4, 5, 6]>>> var2 = 2 * var + [7, 8, 9] # repeat and append
>>> var2[1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Append/Repeat (Combine)
S e q u e n c e t y p e s ( 3 )
17
>>> var = [1, 2, 3, 4, 5, 6]>>> var[0:2] # components from position 0 (included)
[1, 2] # to position 2 (excluded)
>>> var[:3] # omitted first index defaults to zero
[1, 2, 3]>>> var[3:] # omitted second index defaults to
[4, 5, 6] # the size of variable>>> var[-2:] # characters from second-last to[5, 6] # the size of variable
Slicing; access
S e q u e n c e t y p e s ( 4 )
18
>>> var = [1, 2, 3, 4, 5, 6]>>> var[2:4] = [7, 8]>>> var[1, 2, 7, 8, 5, 6]>>> var[3,5] = [9]>>> var[1, 2, 7, 9, 6]>>> var[:] = [] # “:” means all component>>> var[]
Slicing; replace
L i s t s ( 1 ) / S e q u e n c e t y p e
19
>>> a = [‘Win’, “Mac”, 7, 8.1] # make a list
>>> a[‘Win’, ‘Mac’, 7, 8.1]>>> a[1] # access‘Mac’>>> a[2:] = [“Linux”] # replace>>> a[‘Win’, ‘Mac’, ‘Linux’]
Lists (≈ variable-length array) Definition: [ var1, var2, var3, …, varn ]
L i s t s ( 2 ) / S e q u e n c e t y p e
20
>>> x += [1, 2] # append (“x += y” means “x = x + y”)
>>> x[‘Win’, ‘Mac’, ‘Linux’, 1, 2]>>> x = [‘a’, ‘b’, ‘c’]>>> y = [a, [1, 2, 3]] # it is possible to nest lists
>>> y[[‘a’, ‘b’, ‘c’], [1, 2, 3]]>>> y[0][‘a’, ‘b’, ‘c’]>>> y[0][1] # since y[0] is also List,‘b’ # y[0][1] access to component 1 of y[0] (= x)
T u p l e ( 1 ) / S e q u e n c e t y p e
21
$> python>>> a = (1, 2, 3, 4) # create a tuple>>> a(1, 2, 3, 4)>>> (1) # this is not tuple but numeric value1>>> (1,) # this is 1 component tuple(1,)
Tuple (≈ immutable List) Definition: ( var1, var2, var3, …, varn )
T u p l e ( 2 ) / S e q u e n c e t y p e
22
>>> a[1] = 6 # tuple cannot replace component(s)Traceback (most recent call last): File “<stdin>”, line 1, in <module>TypeError: ‘tuple’ object does not support item assignment
>>> a_list = list(a) # convert from tuple to list
>>> a_list[1] = 6 # OK>>> a = tuple(a_list) # convert from list to tuple
>>> a(1, 6, 3, 4)>>> b = 5, 6, 7, 8 # parentheses need not necessarily
>>> b # but putting parentheses is recommend
(5, 6, 7, 8)
S e q u e n c e u n p a c k i n g
23
>>> x = [ 1, 2, 3 ]>>> i1 = x[0]>>> j1 = x[1]>>> k1 = x[2]>>> print i1, j1, k11 2 3>>> (i2, j2, k2) = x # sequence unpacking>>> print i2, j2, k21 2 3 # same result>>> i, j, k = x # OK, the most often-used statement
>>> [i, j, k] = x # List is also OK
S t r i n g s ( 1 ) / S e q u e n c e t y p e
24
>>> ‘Hello world’ # single quote‘Hello world’>>> “Hello world” # double quote‘Hello world’>>> ‘I don\’t know’ # use \ to escape the single quote
“I don’t know”>>> “I don’t know” # ... or use double quotes instead
“I don’t know”
Strings Definition: ‘strings’ or “strings”
S t r i n g s ( 2 ) / S e q u e n c e t y p e
25
>>> s = ‘First\nSecond’ # \n means new line
>>> s # without “print”, \n is included in the
‘First\nSecond’ # output (displayed as strings variable)
>>> print s # with “print”, \n produces a new line
FirstSecond>>> s[1] # access to position 1‘i’>>> s * 2 + “Third” # repeat and concatenate
‘First\nSecondFirst\nSecondThird’>>> s[0] = ‘A’ # ERROR: strings is immutable
S t r i n g s ( 3 ) / S e q u e n c e t y p e
26
>>> print ‘C:\work\newdir’ # here \n means new line C:\workewdir>>> print ‘C:\\work\\newdir’ # use \ to escape \
C:\work\newdir>>> print r‘C:\work\newdir’ # ... or appending “r”
C:\work\newdir>>> ‘4 ** 12 is %i’ % 4 ** 12 # format % value
'4 ** 12 is 16777216‘>>> ‘Today is %i/%s/%i’ % (8, “Sep”, 2015)'Today is 8/Sep/2015‘ # need to specify by tuple
S t r i n g s ( 4 ) / S e q u e n c e t y p e
27
>>> print “““\ # print multi line using triple quotes
... Usage: myprogram [OPTIONS]
... -h Display this usage message
... -n Number of dimensions
... ”””Usage: myprogram [OPTIONS] -h Display this usage message -n Number of dimensions
>>>
D i c ti o n a r y / M a p p i n g t y p e ( 1 )
28
>>> tel = {“noda”: 7315, “ishimura”: 7464}>>> tel{‘noda’: 7315, ‘ishimura’: 7464}>>> tel[“noda”] # access to item7315>>> del tel[“noda”] # remove item>>> tel[“nishizawa”] = ???? # append item>>> tel.keys() # print the list of keys[“ishimura”, “nishizawa”]
Dictionary (≈ associative array) Definition: { key1:val1, key2:val2, …, keyn:varn ]
D i c ti o n a r y / M a p p i n g t y p e ( 2 )
29
… Continued from the previous page …>>> “ishimura” in telTrue>>> “noda” in telFalse>>> for k, v in tel.iteritems(): # unpacking
... Print k, “=“, v
...ishimura = 7464nishizawa = ????
A r r a y i n N u m P y ( 1 )
30
Array (≈ normal array in C, Fortran, and so forth) Definition: numpy.array()>>> import numpy as np # explain later>>> np.array(0) # pass a number, create rank 0 array
array(0)>>> np.array([1, 2, 3, 4]) # pass a listarray([1, 2, 3, 4])>>> np.array([[1, 2], [3, 4]]) # pass a nested list
array([[1, 2], [3, 4]])>>> np.array([[1, 2], [3, 4, 5]]) # ERROR
A r r a y i n N u m P y ( 2 )
31
… Continued from the previous page …>>> np.zeros(3) # create new array, filled with zeros
array([0., 0., 0.]) # default is float>>> np.zeros(3, np.int32) # specify the 32bit int
array([0, 0, 0])>>> np.empty((3, 2)) # create new array without clear
# need to pass by “Tuple”
array([[2.46412955e-295, 1.72455919e-295], [2.46412955e-295, 2.46520467e-295], [1.72424561e-295, 2.46780288e-295]])
C o n c l u s i o n s o f t h i s s e c ti o n
32
Numeric types (int, long, float, complex) Lists Ex. [x, y, z]
mutable; can be nested Tuple Ex. (x, y, z)
similar to Lists, but immutable Strings Ex. ‘STRING’ Dictionary Ex. {x:a, y:b}
can be accessed to value with key Array Ex. np.empty((2, 2))
NumPy is used; can be convert from/to Lists or Tuple
Basic build-in data types are…
Sequence type• access print x[0]• replace x[0] = y• slicing x[2:4] = [1, 2]
C o n t e n t s
33
What is Python? Basic build-in type More control tools Defining Functions Module Class File I/O Using Fortran native code
i f s t a t e m e n t s ( 1 )
34
>>> x = 6>>> if x < 3:... print “small” # need indent (some blanks)
... elif 3 <= x < 8: # “elif” is short for “else if”
... print “middle”
... else:
... print “large”
...middle
if statements Definition: if condition: … (elif condition: …) (else: …)
i f s t a t e m e n t s – t r i v i a
35
>>> if x < 3: print “small” # continuable... else:... print “large”
>>> if x < 3: # normal description
... print “small”
... y = x * 2>>> if x < 3: print “small”; y = x * 2 # OK
>>> if x < 3: print “small” # NG
... y = x * 2 # when some statements are continued after
# “if” statement, cannot make a new line
f o r s t a t e m e n t s ( 1 )
36
>>> sum = 0>>> for i in [1, 2, 3, 4, 5]:... sum += i...>>> print sum15
over the items of any sequence, in the order that they appear in the sequence (same as Bourne and C shell)
List: Sequence type
for statements Definition: for loop_variable in sequence:
f o r s t a t e m e n t s ( 2 )
37
>>> for c in “abc”:... print c...ab # in the order that they appear in the sequence
c>>> for i in [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]... sum += i ...>>> print sum120
Strings: Sequence type
What a hassle!
range() function
( x ) r a n g e f u n c ti o n
38
(x)range function>>> range(6) # given end point[0, 1, 2, 3, 4, 5]>>> range(5, 10) # let the range start at another number, # first index is start point, and second index is end point
[5, 6, 7, 8, 9]>>> range(1, 10, 3) # specify a different increment
[1, 4, 7]>>> xrange(1, 10, 3) # similar to range(), but returns an xrange object # xrange() has to create the values when asked for them
L o o p i n g f o r s e q u e n c e u n p a c k i n g ( 1 )
39
>>> x = [[1,2], [3,4], [5,6]] # make a nested list
>>> for y in x: # y is list type variable... print y[0], y[1]...1 23 45 6>>> for i, j in x: # obtain the same result above
... print i, j
...
L o o p i n g f o r s e q u e n c e u n p a c k i n g ( 2 )
40
>>> x = [[1,2], [3,4], [5,6,7]]>>> for i, j in x:... print i, j...1 23 4Traceback (most recent call last): File "<stdin>", line 1, in <module>ValueError: too many values to unpack
CAUTION
Only x[2] has 3 components
b r e a k s t a t e m e n t s
41
break statements breaks out of the smallest enclosing for of while loop
>>> for i in range(5)... if i == 4:... break # if i == 4, breaks out “for”
... print i
...0123
c o n ti n u e s t a t e m e n t s
42
continue statements continues wih the next iteration of the loop
>>> for i in range(10)... if num % 2 == 0:... print “Found an even number”, num... continue... print num # if num % 2 == 0, this line is never evaluated ...
p a s s s t a t e m e n t s
43
pass statements does nothing
>>> def hoge(): # defining functions... # we want to define this later, but blank is declined File "<stdin>", line 2
^IndentationError: expected an indented block>>> def hoge():... pass # OK, does nothing...
C o n c l u s i o n s o f t h i s s e c ti o n
44
If statement if condition: … (elif condition: …) (else: …)
for statement for loop_variable in sequence_variable: … (x)range is powerful functions can be used unpacking sequence (Ex. for i, j in list:)
break statement continue statement pass statement
when nothing to do, put the pass
Python control tools are…
C o n t e n t s
45
What is Python? Basic build-in type More control tools Defining Functions Module Class File I/O Using Fortran native code
D e fi n i n g F u n c ti o n s ( 1 )
46
>>> def fib(n):... a, b = 1, 1 # need indent... while a < n:... print a,... a, b = b, a + b...>>> fib(1000)1 1 2 3 5 8 13 21 34 55 89 144 233 377 618 987
Definition – def function_name(arguments):
D e fi n i n g F u n c ti o n s ( 2 )
47
>>> def fib2(n):... result = [] # make an empty list... a, b = 1, 1... while a < n:... result.append(a) # i.e., result += [a]
... a, b = b, a + b
... return result # return the value
...>>> print fib2(1000)[1,1,2,3,5,8,13,21,34,55,89,144,233,377,618,987]
D o c u m e n t a ti o n S t r i n g s
48
>>> def hoge():... “““Hoge... # blank line makes a indent... Placeholder name... ”””... pass...>>> print hoge.__doc__ # docstrings can be printedHoge
Placeholder name
The first statement can optionally be a string;this string is the function’s documentation string (docstring)
D e f a u l t a r g u m e n t v a l u e s
49
>>> def axbpc(a, b = 1, c = 0): # b, c have default value
... return a * b + c
...>>> axbpc(5) # a = 5, b = 1, c = 05>>> axbpc(5, 6) # a = 5, b = 6, c = 030>>> axbpc(5, 6, 7) # a = 5, b = 6, c = 737
Default argument values and keyword arguments
K e y w o r d a r g u m e n t s
50
… Continued from the previous page …>>> axbpc(5, c = 7) # a = 5, b = 1, c = 712>>> axbpc(b = 6, a = 5) # a = 5, b = 6, c = 030### following calls would be invalid:>>> axbpc() # require arg. “a” missing
>>> axbpc(b = 5) # require arg. “a” missing
>>> axbpc(c = 7, 5) # non-keyword arg. after a keyword arg.
>>> axbpc(5, a = 8) # duplicate value for the same arg.
>>> axbpc(5, d = 9) # unknown keyword arg.
A r b i t r a r y a r g u m e n t l i s t s
51
>>> def hoge(a, *arguments, **keywords):... print a... for arg in arguments:... print “Arb. arg. : “ + arg... keys = sorted(keywords.keys())... for key in keys:... print key, “:”, keywords[key]...>>> hoge(“Hoge”, “Piyo”, “Fuga”, “Hogera”, hoge = “hogevalue”, piyo = “piyovalue”)
Tuple Dictionary
A r b i t r a r y a r g u m e n t l i s t s
52
>>> hoge(“Hoge”, “Piyo”, “Fuga”, “Hogera”, hoge = “hogevalue”, piyo = “piyovalue”)Hoge # required argumentArb. arg. Piyo # arbitrary argument in tupleArb. arg. Fuga # sameArb. arg. Hogera # sameHoge:hogevalue # arbitrary argument in dictionaryPiyo:piyovalue # same>>> hoge(“Hoge”, hoge = “hogevalue”, “Piyo”) # This is error, non-keyword arg. need to put # before keyword arg.
C o n c l u s i o n s o f t h i s s e c ti o n
53
def function_name(argument_list): return statement returns something value
First statement can optionally be a string, this string called by documentation string (docstring)
Default args. can be defined and access by keyword arg. Ex.) define: def hoge(a, b = 1, c = 0): access: hoge(1, c = 2)
Arbitrary argument lists can be used … but used frequency is very low
Summaries of definition functions are …
C o n t e n t s
54
What is Python? Basic build-in type More control tools Defining Functions Module Class File I/O Using Fortran native code
W h a t i s M o d u l e ?
55
ModuleA file which has definitions of functionThese functions are used in a script or in an interactive interpreterA module allows you to logically organize your Python code
Without module With moduleModule Function
M o d u l e ( 1 )
56
C:\work\tutorial.py
1: def hoge():2: “““Print out ‘hoge’””” # docstrings3: print “hoge”4: def piyo():5: print “piyo” # docstrings need not necessarily
1. Make a test module on work directory (= C:\work)
M o d u l e ( 2 )
57
C:\work> python>>> import tutorial # load NOT the names of the functions but the module name “tutorial”
>>> tutorial.hoge() # “x.y” means “x of y”hoge>>> tutorial.piyo()piyo>>> hoge() # ERRORTraceback (most recent call last): File "<stdin>", line 1, in <module>NameError: name 'hoge' is not defined
2. Run python on work directory, and Import test module
M o d u l e ( 3 )
58
… Continued from the previous page …>>> from tutorial import hoge # load the names of “hoge” from “tutorial” module
>>> hoge() # “hoge” has been loadedhoge>>> piyo() # ERRORTraceback (most recent call last): File "<stdin>", line 1, in <module>NameError: name ‘piyo' is not defined
>>> from tutorial import * # imports all functions
>>> piyo()piyo
M o d u l e ( 4 )
59
>>> import tutorial>>> dir(tutorial) # print the defined functions in a module
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'hoge', 'piyo']>>> dir() # Without arg., dir() lists the names loaded currently
['__builtins__', '__doc__', '__name__', '__package__', ‘tutorial']>>> print tutorial.hoge.__doc__ # print docstrings
Print out ‘hoge’>>> print tutorial.piyo.__doc__None # RECOMMEND using docstrings
M o d u l e - t r i v i a
60
>>> import numpy>>> numpy.array(0)array(0)>>> import numpy as np>>> np.array(0)array(0)
C:\work\tutorial.py1: “““docstrings for module”””2: def hoge():3: “““docstrings”””4: pass
other name can be defined
outer docstrings is module’s help
C o n c l u s i o n s o f t h i s s e c ti o n
61
A module is a file containing Python definitions Using the import statement, the name enters in the
symbol tables, user can be accessed only them Ex.) “imoprt numpy” can be accessed numpy “from numpy import array” can be accessed array as numpy.array
dir function can be listed the defined functions Other name can be defined to module name
Ex.) import numpy as np
Summaries of Module are …
C o n t e n t s
62
What is Python? Basic build-in type More control tools Defining Functions Module Class File I/O Using Fortran native code
W h a t i s c l a s s
63
ClassAn extensible template for creating objects, providing member variables and implementations of behavior (method)
Clerk class
priceobj
demand
Customer class
moneybaggage
orderShopA; \500 Noda;\1,500
order
demand
Ishimura;\4,649ShopB; \800
order
demandorder
demand
Clerk Customer
Same communication between clerk and customerwithout attention to shop and person
Instance of Customer classInstance of Clerk class
W i t h o u t c l a s s
64
01: money = {“noda”: 1500, “ishimura”: 4649}02: baggage = {“noda”: [], “ishimura”: []}03: price = {“shopA”: 500, “shopB”: 800}04: obj = {“shopA”: “video”, “shopB”: “game”}05:06: def order(persion, storeName):07: baggage[person].append(obj[storeName])08: def demand(person, price):09: money[person] -= price10: def main():11: order(“noda”, “shopA”)12: demand(“noda”, price[“shopA”])13: order(“noda”, “shopB”)14: demand(“noda”, price[“shopB”])15: order(“ishimura”, “shopB”)16: demand(“ishimura”, price[“shopB”])
God manages data for all person and shop
W i t h c l a s s
65
01: class Clerk:02: def __init__(self, price, obj): # initialize03: self.price = price # define member variable04: self.obj = obj05:06: def demand(self, person):07: person.money -= self.price08:09: class Customer:10: def __init__(self, money, baggage = None):11: self.money = money; self.baggage = baggage12: if (baggage == None): self.baggage = []13:14: def order(self, store):15: self.baggage.append(store.obj)16:
W i t h c l a s s ( c o n ti n u e d … )
66
17: def main():18: noda = Customer(1500) # make an instance19: ishimura = Customer(4649) # __init__ is called here20: shopA = Clerk(500, “video”)21: shopB = Clerk(800, “game”)22:23: noda.order(shopA) # use method “order”24: shopA.demand(noda)25: noda.order(shopB)26: shopB.demand(noda)27: ishimura.order(shopB)28: shopB.demand(ishimura)
E x a m p l e o f m e t h o d s
67
Methods of List>>> x = [1, 2, 3, 4]>>> x.append(5)>>> x[1, 2, 3, 4, 5]>>> x.reverse()>>> x[5, 4, 3, 2, 1]>>> x.insert(1, 6)>>> x[5, 6, 4, 3, 2, 1]
Methods of String>>> s = “Hello world”>>> s.find(“l”)2>>> s.isdigit()False>>> s.rsplit()[‘Hello’, ‘World’]>>> s.rsplit()[‘Hell’, ‘ W’, ‘rld’]
I n h e r i t a n c e o f c l a s s
68
Inheritance of classThe purpose of inheritance is “overriding” and “code reuse”
Form:Button is only acceptable Button A
Button B
Button
text
Show()
Text
text
Edit()
abc
Show message when pushed
BeepButton
text
Show()Beep()
Show message andbeep when pushed
Button C
I n h e r i t a n c e o f c l a s s ( c o n ti n u e d … )
69
Form:Button is only acceptable Button A
Button B
Button
text
Show()
BeepButton
Beep()Button C
Inheritance
Extend features
Have features(Reuse)BeepButton is a Button
H o w t o u s e i n h e r i t a n c e
70
01: class Button:02: def __init__(self, text):03: self.text = text04:05: def show(self):06: print “My text is “ + text07:08: class BeepButton(Button): # inheritance of Button09: def __init__(self, text):10: self.text = text11:12: def beep():13: print “\a”14:
H o w t o u s e i n h e r i t a n c e ( c o n ti n u e d )
71
15: def main():16: buttonA = ButtonA(“buttonA”)17: buttonB = ButtonB(“buttonB”)18:19: buttonA.show()20: buttonB.show()21: buttonB.beep()22: # buttonA.beep() # ERROR: Button class has not beep()23:24: if __name__ == “__main__”:25: main()x01: # class Button 01: class Button(object)x10: # self.text = text 10: super(BeepButton, self).__init__(text)
OR
O v e r r i d e
72
Form:Button is only acceptable Button A
Button B
Button
text
Show()
BeepButton
Button C
Inheritance Have features(Reuse)BeepButton is a Button
Beep when pushedwithout message
override
O v e r r i d e
73
01: class Button(object):02: def __init__(self, text):03: self.text = text04:05: def show(self):06: print “My text is “ + text07:08: class BeepButton(Button): # inheritance of Button09: def __init__(self, text):10: super(BeepButton, self).__init__(text)11:12: def show(): # override !!13: print “\a” # or pass, then add beep() function14:
C o n t e n t s
74
What is Python? Basic build-in type More control tools Defining Functions Module Class File I/O Using Fortran native code
O p e n i n g F i l e s
75
>>> f = open(“test.txt”, “r”) # create file object with opening a file on read mode
>>> print f<open file ‘test.txt’, mode ‘r’ at 80a0960>>>> f.close() # close the file>>> f = open(“test.txt”) # open a file on read mode
>>> f = open(“test.txt”, “w”) # write mode>>> f = open(“test.txt”, “a”) # append mode
>>> f = open(“test.txt”, “rb”) # read mode as binary
R e a d i n g F i l e s
76
>>> f = open(“test.txt”, “r”)>>> f.read() # read the all data in “test.txt”
‘First line\nSecond line\n’>>> f.read() # EOF‘’>>> f.seek(0) # means rewind
test.txt1: First line2: Second line3:
R e a d i n g F i l e s
77
… Continued from the previous page …>>> f.read(3) # read the 3 character from current pos.
‘Fir’>>> f.read(3) # read the 3 character from current pos.
‘st ’>>> f.read() # read the rest of data from current pos.
‘line\nSecond line\n’>>> f.seek(13) # move pointer to pos. 13>>> f.read()‘econd line\n’>>> f.seek(0) # rewind
R e a d i n g F i l e s
78
… Continued from the previous page …>>> f.readline() # read from current pos. to “\n”‘First line\n’>>> f.readline()‘Second line\n’>>> f.seek(0)>>> f.readlines() # return the list of lines[‘First line\n’, ‘Second line\n’]>>> f.seek(0)>>> for line in f: print line,First lineSecond line
W r i ti n g F i l e s
79
>>> f = open(“test.dat”, “w”)>>> f.write(“Hello world\n”)>>> f.write(0) # only strings can be writtenTraceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: expected a character buffer object
>>> s = str(0) # int => strings>>> f.write(s)>>> f.close()
T r e a ti n g t h e L i s t , D i c ti o n a r y , e t c .
80
# easy to read/write the numeric value
>>> f = open(“test.dat”, “w”)>>> f.write(“123”);>>> f.close()>>> f = open(“test.dat”)>>> s = f.read()>>> int(s)123>>> f.close()
T r e a ti n g t h e L i s t , D i c ti o n a r y , e t c .
81
# how to read/write a list
>>> x = [1, 2, 3, 4]>>> f = open(“test.dat”, “w”)>>> s = str(x)>>> f.write(s)>>> f.close()>>> f = open(“test.dat”)>>> s = f.read()>>> s‘[1, 2, 3, 4]’
>>> list(s) # can not convert correctly['[', '1', ',', ' ', '2', ',', ' ', '3', ',', ' ', '4', ']']
T r e a ti n g t h e L i s t , D i c ti o n a r y , e t c .
82
>>> import pickle # import pickle (or json) module
>>> x = [1, 2, 3, 4]>>> f = open(“test.dat”, “w”)>>> pickle.dump(x, f)>>> f.close()>>> f = open(“test.dat”)>>> y = pickle.load(f)>>> y # success‘[1, 2, 3, 4]’ Pickle: allows the serialization of complex object, but it is specific to Python JSON: serialized by the JavaScript Object Notation, and the data is used to
communicate with applications written in other language
C o n c l u s i o n s o f t h i s s e c ti o n
83
Open any file: open(filename, mode) mode: “r” or blank => read, “w” => write, “a” => append
Only “strings” variables can be read/written to a file When read/write complex objects such as List,
pickle (or json) module is useful
Summaries of file I/O are …
C o n t e n t s
84
What is Python? Basic build-in type More control tools Defining Functions Module Class File I/O Using Fortran native code
P e r f o r m a n c e o f P y t h o n
85
Generate Prime number; n = 100,000,000 (in Python)prime_purepy.py01: import math02:03: def generate_prime(n):04: sqrtN = int(math.sqrt(n))05: seed = list(1 for x in xrange(n + 1))06: for i in range(2, sqrtN + 1):07: if seed[i] == 0:08: continue09: for j in xrange(i * i, n + 1, i):10: seed[j] = 011: return [i for (i, x) in enumerate(seed) if x == 1 and i >= 2]
27.159 s
P e r f o r m a n c e o f F o r t r a n
86
Generate Prime number; n = 100,000,000 (in Fortran)prime.f9001: subroutine prime(pList, m, seed, n)02: implicit none03: integer, intent(out) :: pList(n / 2), m, seed(n)04: integer, intent(in) :: n05: integer :: i, sqrtN06: seed(1 : N) = 1; m = 0; sqrtN = int(dsqrt(dble(n)))07: do i = 2, sqrtN08: if (seed(i) == 0) cycle09: seed(i * i : n : i) = 010: end do11: do i = 2, n12: if (seed(i) == 1) then13: m = m + 1; pList(m) = i14: end if15: end do16: return17: end subroutine prime
01.916 s
U s i n g F o r t r a n n a ti v e c o d e
87
To obtain the high-performance, using a Fortran native code
1. Using “ctypes” module (Python 2.5 ~)“ctypes” can load dynamic link libraries (include Fortran)
2. Create the wrapper in C for calling Fortrana. Create it yourself using PyObject data type in Cb. Using the Swig (C/C++ => other language)
Make a C code called Fortran code => Python modulec. Using the f2py in numpy
Make a Fortran code => Python module
Python Fortrancall C FortrancallPython Ccall
U s i n g t h e f 2 p y ( 1 )
88
C:\work> f2py.py -c --fcompiler=gfortran -m prime prime.f90running build module namerunning config_cc...Found executable C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\BIN\link.exeRemoving build directory c:\users\nishi\appdata\local\temp\tmpv4fdweC:\work>
1. Create python module (prime.pyd) using the f2py
U s i n g t h e f 2 p y ( 2 )
89
C:\work> python>>> import prime>>> print prime.__doc__ # print the list of functionsThis module 'prime' is auto-generated with f2py (version:2).Functions plist,m,seed = prime(n) # this function should be used like this.>>> print prime.prime.__doc__ # print the information of this functionplist,m,seed = prime(n)(continue..., parameters, returns, and so forth)
2. Check the features for “prime” module
U s i n g t h e f 2 p y ( 3 )
90
prime_usefort.py01: import prime02:03: def main():04: n = 10000000005: pList, m, work = prime(n)06: # pList = pList[: m] # remove the extra array07: # pList = list(pList) # numpy.ndarray => list08:09: if __name__ == “__main__”:10: main()
01.921 s
3. Use the “prime” module in a python code
M o r e o n f 2 p y : a r g u m e n t p r o p e r t y
91
test_f2py.py01: subroutine test1(a, b, c)02: implicit none03: integer, intent(in) :: a04: integer, intent(out) :: b05: integer, intent(inout) :: c06:07: b = a + 108: c = c * 2 + a09:10: return11: end subroutine test1
A r g u m e n t p r o p e r t y
92
C:\work> python>>> import test_f2py>>> print test_f2py.test1.__doc__b = test1(a,c)
Wrapper for ``test1``.
Parameters # “in” and “inout” are Parameters----------
a : input intc : in/output rank-0 array(int,'i')
Returns-------
b : int # “out” is Returns
Te s t o f a r g u m e n t p r o p e r t y
93
>>> import test_f2py>>> a, c = 5, 6>>> b = test_f2py.test1(a, c)>>> print b, c # b = a + 1 = 6, c = 2 * c + a = 176 6 # ??>>> import numpy as np>>> a, c = 5, np.array(6, np.int32)>>> b = test_f2py.test1(a, c)>>> print b, c6 17 # OK>>> print type(b), type(c)<type ‘int’> <type ‘numpy.ndarray’>
W h a t a b o u t a r r a y ?
94
test_f2py.f9013: subroutine test2(a, b, c, n)14: implicit none15: integer, intent(in) :: a(n)16: integer, intent(out) :: b(n)17: integer, intent(inout) :: c(n)18: integer, intent(in) :: n19:20: b = a + 121: c = c * 2 + a22:23: return24: end subroutine test2
f 2 p y - 1 - d i m e n s i o n a r r a y
95
>>> import test_f2py, random, numpy as np>>> a = [random.randint(1, 10) for i in range(5)]>>> c = [random.randint(1, 10) for i in range(5)]>>> print a, c[4, 8, 5, 7, 6] [4, 9, 10, 2, 7]>>> b = test_f2py.test2(a, c) # passed Lists arg.Traceback (most recent call last): # Lists is denied on “inout” File "<stdin>", line 1, in <module>TypeError: failed to initialize intent(inout|inplace|cache) array, input not an array
>>> c = np.array(c, np.int32) # Lists => array>>> b = test_f2py.test2(a, c) # Lists is acceptable for “in”
>>> print a, b, c[4, 8, 5, 7, 6] [5 9 6 8 7] [12 26 25 11 20]
arrayLists
U s i n g c o m m o n v a r i a b l e s
96
test_f2py.f9026: module commontest27: implicit none28:29: integer :: a # common variable30:31: contains32:33: subroutine input(b)34: implicit none35: a = b36: return37: end subroutine input38: module commontest
f 2 p y - c o m m o n v a r i a b l e
97
>>> import test_f2py>>> print test_f2py.aTraceback (most recent call last): File "<stdin>", line 1, in <module>AttributeError: 'module' object has no attribute 'a'
>>> print test_f2py.commontest.a # need the module name
0>>> test_f2py.commontest.a = 2>>> print test_f2py.commontest.a2>>> test_f2py.commontest.input(3)>>> print test_f2py.commontest.a3
U s i n g c o m m o n v a r i a b l e s f o r F 7 7
98
test_f2py.f 1234567890123456789012345678901234567890123456789012345671: SUBROUTINE DUMMY2: IMPLICIT NONE3: INTEGER A4: COMMON /TEST/ A5: END>>> import test_f2py>>> print test_f2py.test.a # need the block namearray(0) # as a array>>> test_f2py.test.a = 1>>> print test_f2py.test.aarray(1)
U s i n g e x t e r n a l l i b r a r i e s ( f a i l e d … )
99
C:\work> gfortran -c -shared -o prime.o prime.f90$ gfortran -c -shared -fPIC -o prime.so prime.f90
0. Create dynamic link library (or prepare any library)Win
Mac
1. Create Fortran code (call function(s) from library) external_prime.f901: subroutine get(pList, m, work, n)2: implicit none3: integer, intent(out) :: pList(n / 2), m, work(n)4: integer, intent(in) :: n5: call prime(pList, m, work, n)6: return7: end subroutine get
U s i n g e x t e r n a l l i b r a r i e s ( c t y p e s )
100
external.py1: import prime2:3: def main():4: n = 1000000005: pList, m, work = prime(n)6:7: if __name__ == “__main__”:8: main()
2. Create python module (extprime.pyd)C:\work> f2py.py -c --fcompiler=gfortran -L. prime.o-m extprime external_prime.f903. Use the “extprime” module in a python code
C o n c l u s i o n s o f t h i s s e c ti o n
101
Need to make a wrapper for calling Fortran native code f2py (in NumPy) is useful for making wrapper
f2py.py -c --fcompiler=gfortran -m module_name file Variables of “in” and “inout” property are parameters,
that of “out” property is returns inout parameters needs to pass as a array
Summaries of using Fortran code
C o n t e n t s
102
What is Python? Basic build-in type More control tools Defining Functions Module Class File I/O Using Fortran native code Odds and Ends
E x c e p ti o n ( 1 )
103
Two distinguishable kinds of errors Syntax error>>> if x < 0 File "<stdin>", line 1 if x <0 ^SyntaxError: invalid syntax
Exception: statement is syntactically correct, but an error occurs
>>> 10 / 0Traceback (most recent call last): File "<stdin>", line 1, in <module>ZeroDivisionError: integer division or modulo by zero
Loss of “:”
E x c e p ti o n ( 2 )
104
without_exception.py
1: import os.path 2: if not os.path.exist(“test.txt”): 3: print “file not found” 4: quit() 5: f = open(“test.txt”) 6: s = f.readline() 7: if not isdigit(s): 8: print “invalid value” 9: quit()10: i = int(s)
Procedures are contaminatedby error check.
E x c e p ti o n ( 3 )
105
with_exception.py
1: import os.path 2: try: 3: f = open(“test.txt”) 4: s = f.readline() 5: i = int(s) 6: except: 7: print “something error” 8: quit() 9: else: # exception was not thrown10: print “conversion is success”
Error checks are separatedfrom main routine
E x c e p ti o n ( 4 )
106
without_exception.py
1: import util # this program can execute even without this module, # but if “util.py” not found, the program exitswith_exception.py
1: try: # this program works without util module
2: import util3: except:4: print “WARNING:: features are limited”
E n t r y p o i n t o f p y t h o n ( 1 )
107
Entry_without_main.py01: print “Global region: Always print out”02:03: def Hoge():04: print “Function region: Hoge was called”
C:\work> entry_without_main.pyGlobal region: Always print outC:\work> python>>> import entry_without_mainGlobal region: Always print out>>> entry_without_main.Hoge()Function region: Hoge was called>>>
E n t r y p o i n t o f p y t h o n ( 2 )
108
entry_with_main.py01: def main():02: print “Function region: main was called”03: def Hoge():04: print “Function region: Hoge was called”05: if __name__ == “__main__”:06: main()
C:\work> entry_with_main.pyFunction region: main was calledC:\work> python>>> import entry_with_main # print nothing, not thorough main>>> entry_with_main.Hoge()Function region: Hoge was called
C o n c l u s i o n s
109
Practice makes perfect If you have any trouble for module or class, type
print (module or class name).__doc__
If you have any trouble for Python syntax, see https://docs.python.org/2.7/index.html (English)
http://docs.python.jp/2.7/index.html ( 日本語 )
Thank you for your attention
110
ゴミ箱
M o r e C o n t r o l F l o w To o l s : e l s e
111
else Clauses on LoopsLoop statements may have an else clause.It is executed when the loop terminates through exhaustion of the list, but not when the loop is terminated by a break.
>>> for n in range(2, 10)... for x in range(2, n)... if n % x == 0: # not a prime number
... break:
... else:
... print n, “is a print number”...
Same scope
S e q u e n c e t y p e s ( L i s t s : 2 )
112
… Continued from the previous page …>>> squares = [1, 4, 9, 16, 25]>>> squares[1, 4, 9, 16, 25]>>> squares + [36, 49, 64, 81, 100][1, 4, 9, 16, 25, 36, 49, 64, 81, 100]>>> cubes = [1, 8, 27, 65, 125] # something’s wrong
>>> cubes[1, 8, 27, 65, 125]>>> 4**364
S e q u e n c e t y p e s ( L i s t s : 3 )
113
… Continued from the previous page …>>> cubes[3] = 64 # replca the wrong value>>> cubes[1, 8, 27, 64, 125]>>> cubes.append(216) # add the cube of 6>>> cubes.append(7**3) # add the cube of 7>>> cubes[1, 8, 27, 64, 125, 216, 343]
S e q u e n c e t y p e s ( L i s t s : 4 )
114
… Continued from the previous page …>>> letters = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’]>>> letters[‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’]>>> letters[2:4] = [‘C’, ‘D’] # replace some value
>>> letters[‘a’, ‘b’, ‘C’, ‘D’, ‘e’, ‘f’]>>> letter[2:5] = [] # remove some value>>> letters[‘a’, ‘b’, ‘f’]
S e q u e n c e t y p e s ( S t r i n g s : 4 )
115
… Continued from the previous page …>>> word = ‘Help’ + “Me” # strings can be concatenated
>>> word‘HelpMe’>>> ‘!!!’ + word * 5 + ‘!!!’ # ... and repeated
‘!!!HelpMeHelpMeHelpMeHelpMeHelpMe!!!’>>> word[0] # character in position 0‘H’>>> word[-2] # second-last character‘M’
S e q u e n c e t y p e s ( S t r i n g s : 5 )
116
… Continued from the previous page …>>> word[0:2] # characters from pos. 0 (included) to pos. 2 (excluded)
‘He’>>> word[:2] # omitted first index defaults to zero
‘He’>>> word[3:] # omitted second index defaults to the size of string
‘pMe’>>> word[-2:] # characters from second-last to the size of string
‘Me’
D e f a u l t a r g u m e n t o f L i s t s ( 1 )
117
>>> def hoge(a, L = []): # L is initialized by List
... L.append(a)
... return L
...>>> hoge(1) # L is initialized by [][1]>>> hoge(2) # default value is not evaluated,[1, 2] # ... that is, L is not cleared>>> hoge(3)[1, 2, 3]
Important warning: The default value is evaluated only once.
List, Dictionary, Class(Mutable object)
D e f a u l t a r g u m e n t o f L i s t s ( 2 )
118
>>> def hoge(a, L = None):... if L is None:... L = []... L.append(a)... return L...>>> hoge(1) # Since L = None, L is cleared by []
[1]>>> hoge(2) # In this case, L is cleared[2]
Alternative plan
NoneType object (Immutable object)