I117 II I117 PROGRAMMING PRACTICE II OTHER LANGUAGES Research Center for Advanced Computing Infrastructure (RCACI) / Yasuhiro Ohara yasu@jaist.ac.jp
/ SCHEDULE 1. 2011/06/07(Tue) / Basic of Programming 2. 2011/06/09(Thu) / Fundamental data structures 3. 2011/06/14(Tue) / Memory Management 1 4. 2011/06/16(Thu) / Memory Management 2 5. 2011/06/21(Tue) / Debugging 6. 2011/06/23(Thu) / Software Development Env. 1 7. 2011/06/28(Tue) / Software Development Env. 2 8. 2011/06/30(Thu) / Data Structure : Tree 9. 2011/07/05(Tue) / Data Structure: Hash 10. 2011/07/07(Thu) / Understanding Programs 1 11. 2011/07/12(Tue) / Understanding Programs 2 12. 2011/07/14(Thu) / Script Language 1 13. 2011/07/19(Tue) / Script Language 2 14. 2011/07/21(Thu) / Other Languages 15. 2011/07/26(Tue) / Examination
/ FINAL REPORT free topic : The deadline: 7/26(Tue) Submit the source code, and the report. by e-mail: i117report@jaist.ac.jp by 7/26(Tue) 23:59. / Sound file converter / web crawler (CalDAV) calendar program new shell, editor, window manager rsync encrypt/decrypt, rsync markov-chain program The final report must have deep consideration
/ TODAY'S INDEX python
PYTHON
PYTHON C sh Java Ruby Python C++ Perl Erlang LISP Scheme ML OCaml Haskel
PYTHON (very-high-level language) BBC Monty Python s Flying Circus C
PYTHON Python 2.x 2.7 2.xPython Python 3.x Python 2.x Python 3.x
PYTHON CPython C PythonCPython JPython Java Java VM PyPy PythonPython IronPython C#.NET Unladen Swallow CPython LLVMJIT Stackless Python Python Eve OnlineSecond LifeMMORPGCisco IronPort
HELLO WORLD! print python $ python >>> print Hello World Hello World! Ctrl+D $ cat hello.py print Hello World $ python hello.py Hello World! Python.py
HELLO WORLD! IN PYTHON 3.X Python 3.x print() >>> print( Hello World ) Hello World!
PYTHON Shell1 #! $ cat hello.py #!/usr/bin/env python print Hello World $ chmod +x hello.py $./hello.py Hello World!
Python ASCII # -*- coding: encoding -*- UTF-8 $ cat morning.py #!/usr/bin/env python # -*- coding: utf-8 -*- print u $./morning.py
PYTHON Unicode
Hello World! Hello World! \ \ \ \n \t\\\ >>> print Hello \ World!\ Hello World! >>> print Hello\\World! Hello\World! >>> print Hello\nWorld! Hello World
+ >>> Hello + World! HelloWorld! * >>> Hello * 5 'HelloHelloHelloHelloHello' len() >>> len( Hello ) 5
[] 0 : -1-2 >>> word = Hello >>> word[1] # 1 e >>> word[1:4] # 13 ell >>> word[:3] # 02 hel # >>> word[1:] # 1 ello # >>> word[:-2] # 3 hel
UNICODE Python 2.x str unicode u hello u unicode type >>> type('hello') <type 'str'> >>> type(u'') <type 'unicode'> str unicode >>> [0] # 1 '\xe3' >>> u [0] # 1 u'\u3053 >>> len('') 15 # 15 >>> len(u'') 5 # 5
UNICODE IN PYTHON 3.X Python 3.x Unicode str (2.x) bytes (3.x) hello b hello Python 2.6 b hello Unicode unicode (2.x) str (3.x) u hello hello
UNICODESTR unicodeencode() >>> u.encode( utf-8 ) # UTF-8 '\xe3\x81\x93\xe3\x82\x93\xe3\x81\xab \xe3\x81\xa1\xe3\x81\xaf >>> u.encode( sjis ) # Shift-JIS '\x82\xb1\x82\xf1\x82\xc9\x82\xbf\x82\xcd' unicodeunicode() >>> unicode('\x82\xb1\x82\xf1\x82\xc9\x82\xbf \x82\xcd', 'sjis') u'\u3053\u3093\u306b\u3061\u306f' >>> print unicode('\x82\xb1\x82\xf1\x82\xc9\x82\xbf \x82\xcd', 'sjis')
PYTHON list deque set tuple dict
LIST C [] >>> foo = ['apple', 'orange', 100, 200] >>> foo ['apple', 'orange', 100, 200] [] >>> foo[2] 100 [:] >>> foo[1:3] ['orange', 100] >>> foo[:2] ['apple', 'orange']
LIST + >>> foo = ['apple', 'orange', 100, 200] >>> foo + ['banana', 500] ['apple', 'orange', 100, 200, 'banana', 500] >>> foo ['apple', 'orange', 100, 200, 'banana', 500] >>> foo[1] = 'cherry' >>> foo ['apple', 'cherry', 100, 200, 'banana', 500] >>> foo[2:3] = [300, 400] >>> foo ['apple', 'cherry', 300, 400, 200, 'banana', 500]
LIST >>> a = [66.25, 333, 333, 1, 1234.5] >>> print a.count(333), a.count(66.25) # 2 1 >>> a.insert(2, -1) # 2-1 >>> a [66.25, 333, -1, 333, 1, 1234.5] >>> a.index(333) # 333 1 >>> a.remove(333) # 333 >>> a [66.25, -1, 333, 1, 1234.5] >>> a.reverse() # >>> a [1234.5, 1, 333, -1, 66.25] >>> a.sort() # >>> a [-1, 1, 66.25, 333, 1234.5] >>> len(a) # 5
LIST appendpop >>> stack = [3, 4, 5] >>> stack.append(6) # 6 >>> stack.append(7) # 7 >>> stack [3, 4, 5, 6, 7] >>> stack.pop() # 7 >>> stack [3, 4, 5, 6] >>> stack.pop() 6 >>> stack.pop() 5 >>> stack [3, 4]
DEQUE deque >>> import collections # collections >>> queue = collections.deque([1, 2, 3]) # >>> queue deque([1, 2, 3]) >>> queue.append(4) # 4 >>> queue.append(5) # 5 >>> queue deque([1, 2, 3, 4, 5]) >>> queue.popleft() # >>> queue.popleft() >>> queue deque([3, 4, 5])
SET >>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana ] >>> fruit = set(basket) # set >>> fruit set(['orange', 'pear', 'apple', 'banana']) >>> orange in fruit # orange True # >>> 'crabgrass' in fruit False >>> fruit.add( peach ) # peach >>> fruit set(['apple', 'banana', 'orange', 'peach', 'pear']) >>> fruit.remove( orange ) # orange set(['apple', 'banana', 'peach', 'pear'])
TUPLE list tuple tuplec >>> point = 10, 30 # tuple >>> point (10, 30) >>> point[0] # 0 10 >>> x, y = point # x, y tuple >>> x 10 >>> y 30
DICT KeyValue Key >>> fruit = { orange : 100, apple : 200} # >>> fruit[ orange ] # key orange 100 >>> fruit[ orange ] = 300 # orange >>> fruit {'orange': 300, 'apple': 200} >>> fruit[ banana ] = 500 # banana >>> fruit {'orange': 300, 'apple': 200, 'banana': 500} >>> del fruit[ apple ] # apple >>> fruit {'orange': 300, 'banana': 500}
PYTHON if for range() continuebreak pass
Python 4 CPython C if (a == b) { a = b * 2; printf( %d\n, a); } Python if a == b: a = b * 2 print a CK&RGNUBSD
IF Cif >>> x = 3 >>> if x < 0:... print x is less than zero... elif x == 0:... print zero... elif x == 1:... print single... else... print more... more
FOR forcfor >>> animal = [ dog, cat, bird ] >>> for a in animal: # animala... print a... dog cat bird
FORDICT dictkeys()key >>> fruit = {'orange': 100, 'apple': 200, 'banana': 400} >>> for f in fruit.keys():... print f, :, fruit[f]... orange : 100 apple : 200 banana : 400 iteritems()3.xitems() >>> fruit = {'orange': 100, 'apple': 200, 'banana': 400} >>> for k, v in fruit.iteritems():... print k, :, v... orange : 100 apple : 200 banana : 400
RANGE() range() >>> range(10) # 09 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> range(3, 10) # 39 [3, 4, 5, 6, 7, 8, 9] >>> range(3, 20, 4) # 320 [3, 7, 11, 15, 19] # 4
CFOR range() >>> month = [ April, May, June, July ] >>> for i in range(len(month)):... print i, month[i]... 0 April 1 May 2 June 3 July xrange() xrange()
CONTINUEBREAK break continue >>> for n in range(2, 10):... for x in range(2, n):... if n % x == 0:... print n, 'equals', x, '*', n/x... break... else: # Cforelse... # else... print n, 'is a prime number'... 2 is a prime number 3 is a prime number 4 equals 2 * 2 5 is a prime number 6 equals 2 * 3 7 is a prime number 8 equals 2 * 4 9 equals 3 * 3
PASS pass >>> while is_func(): # is_func()... pass #
PYTHON def lambda C++ f : X Y Z 2f(x, y)gf f(x, y) = g(x)(y) g : X (Y Z)
def return >>> def twice(x): # def,... return x * 2 # return... >>> twice(100) # 200 >>> twice(45) 90 >>> x # xtwice Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name x is not defined
C >>> def n_times(x, n = 2): # n... return x * n... >>> n_times(40) # n n = 2 80 >>> n_times(40, 3) # n 120
>>> def person(name, age):... print 'name = ' + name... print 'age = ' + str(age)... >>> person( Alice, 16) # name = Alice age = 16 >>> person(age = 18, name = Bob ) # name = Bob age = 18
** * >>> def var_args(*args, **kargs):... print args... print kargs... >>> var_args(1, 2, 3, 'foo', 'bar', name = 'Alice', age = 20) (1, 2, 3, foo, bar ) # { age : 20, name : Alice } #
* ** >>> range(2, 7) # [2, 3, 4, 5, 6] >>> args = [2, 7] >>> range(*args) # [2, 3, 4, 5, 6] >>> def n_times(x, n):... return x * n... >>> args = {'n': 100, 'x': 3} >>> n_times(**args) # 300
LAMBDA lambda n_times() >>> f = lambda x, n: x * n >>> f(10, 4) 40 lambda defreturn
n >>> def n_times_f(n): # n... return lambda x: n_times(x, n) # n... >>> f1 = n_times_f(2) # 2 >>> f1(10) 20 >>> f1(15) 30 >>> f2 = n_times_f(5) # 5 >>> f2(10) 50 >>> f2(15) 75
2x, ysort() x < y, x == y, x > y -1, 0, 1 >>> people = [('Bob', 20), ('Carol', 15), ('Alice', 17)] >>> people [('Bob', 20), ('Carol', 15), ('Alice', 17)] >>> people.sort() # >>> people # [('Alice', 17), ('Bob', 20), ('Carol', 15)] >>> people.sort(lambda x, y: x[1] - y[1]) # >>> people # [('Carol', 15), ('Alice', 17), ('Bob', 20)]
PYTHON filter map reduce
FILTER filter(func, list) func list 23 >>> def f(x):... return x % 2!= 0 and x % 3!= 0... >>> filter(f, range(2, 25)) [5, 7, 11, 13, 17, 19, 23]
MAP map(func, list) func list 3 >>> def cube(x):... return x**3 # **... >>> map(cube, range(1, 11)) [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
REDUCE 2 reduce(func, list, init) func list init >>> reduce(lambda x, y: x + y,... [[1, 2, 3], [4, 5], [6, 7, 8]],... []) [1, 2, 3, 4, 5, 6, 7, 8] step 1: [1, 2, 3] + [4, 5] [6, 7, 8] + [] step 2: [1, 2, 3, 4, 5] + [6, 7, 8] result: [1, 2, 3, 4, 5, 6, 7, 8]
{ x * x x L} >>> [x * x for x in range(10)] [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] { x * x x * x > 20, x L} >>> [x * x for x in range(10) if x * x > 20] [25, 36, 49, 64, 81]
PYTHON dir()
Python11 my_echo.pyecho() $ cat my_echo.py def echo(word): print word my_echoecho $ python >>> import my_echo # >>> my_echo.echo( Hello World! ) # Hello World!
/ SCHEDULE 1. 2011/06/07(Tue) / Basic of Programming 2. 2011/06/09(Thu) / Fundamental data structures 3. 2011/06/14(Tue) / Memory Management 1 4. 2011/06/16(Thu) / Memory Management 2 5. 2011/06/21(Tue) / Debugging 6. 2011/06/23(Thu) / Software Development Env. 1 7. 2011/06/28(Tue) / Software Development Env. 2 8. 2011/06/30(Thu) / Data Structure : Tree 9. 2011/07/05(Tue) / Data Structure: Hash 10. 2011/07/07(Thu) / Understanding Programs 1 11. 2011/07/12(Tue) / Understanding Programs 2 12. 2011/07/14(Thu) / Script Language 1 13. 2011/07/19(Tue) / Script Language 2 14. 2011/07/21(Thu) / Other Languages 15. 2011/07/26(Tue) / Examination
/ FINAL REPORT free topic : The deadline: 7/26(Tue) Submit the source code, and the report. by e-mail: i117report@jaist.ac.jp by 7/26(Tue) 23:59. / Sound file converter / web crawler (CalDAV) calendar program new shell, editor, window manager rsync encrypt/decrypt, rsync markov-chain program The final report must have deep consideration