Python Speed Learning 1 / 76
Python 2 1 $ python 1 >>> 1 + 2 2 3 2 / 76
print : 1 print : ( ) 3 / 76
print : 1 print 1 2 print hello 3 print 1+2 4 print 7/3 5 print abs(-5*4) 4 / 76
print : 1 print 1 2 print hello 3 print 1+2 4 print 7/3 5 print abs(-5*4) : 1 1 2 hello 3 3 4 2 5 20 4 / 76
,, (/) 7 / 3 2 7 / 3.0 2.3333333333333335 7 / float(3) 2.3333333333333335 5 / 76
: 1 = : ( ; variable), 6 / 76
:?, python? 1 a = 1 + 2 2 print a 3 b = a + 3 4 print b 5 a = a + 4 6 print a 7 / 76
:?, python? 1 a = 1 + 2 2 print a 3 b = a + 3 4 print b 5 a = a + 4 6 print a : 1 3 2 6 3 10 7 / 76
... : 1 a = 1 + 2 2 print a 3 print b 8 / 76
... : 1 a = 1 + 2 2 print a 3 print b : 1 3 2 Traceback (most recent call last): 3 File "a.py", line 3, in <module> 4 print b 5 NameError: name b is not defined 8 / 76
:.,, ( ),., 1. : x, y, tanaka, Kouji Uehara, World Cup 2013 : J-League, hungry?, 4WD 9 / 76
: 1 def (,,...): 2 3 4... : 10 / 76
: 1 # 2 def f(x, y): 3 a = x + y 4 return a * a 5 6 # ( ) 7 print f(10, 20) + 30 11 / 76
: 1 # 2 def f(x, y): 3 a = x + y 4 return a * a 5 6 # ( ) 7 print f(10, 20) + 30 : 1 930 11 / 76
python, 1 def f(x, y): 2 a = x + y 3 return a * a 4 print f(10, 20) + 30 1 f ( ) 2 print. 1,f(10, 20) + 30. 2, f(10, 20) x=10, y=20 f 3 return : 1 return,, 12 / 76
:?, python? 1 def f(x, y): 2 print 1 3 print hello 13 / 76
:?, python? 1 def f(x, y): 2 print 1 3 print hello! 13 / 76
:? 1 def f(x, y): 2 print x 3 print y 4 return x + y 5 6 print f(10, 20) 14 / 76
:? 1 def f(x, y): 2 print x 3 print y 4 return x + y 5 6 print f(10, 20) 1 10 2 20 3 30 14 / 76
:? : 1 def f(x, y): 2 print x 3 print y 4 x + y 5 6 print f(10, 20) 15 / 76
:? : 1 def f(x, y): 2 print x 3 print y 4 x + y 5 6 print f(10, 20) : 1 10 2 20 3 None 15 / 76
:? : 1 def f(x, y): 2 print x 3 print y 4 x + y 5 6 print f(10, 20) : 1 10 2 20 3 None (return ). return,,, None 15 / 76
:? : 1 def f(x, y): 2 print x 3 print y 4 x + y 5 6 print f(10, 20) + 30 16 / 76
:? : 1 def f(x, y): 2 print x 3 print y 4 x + y 5 6 print f(10, 20) + 30 : 1 10 2 20 3 Traceback (most recent call last): 4 File "a.py", line 6, in <module> 5 TypeError: unsupported operand type(s) for +: NoneType and int 16 / 76
:? : 1 def f(x, y): 2 print x 3 print y 4 x + y 5 6 print f(10, 20) + 30 None, 30, : 1 10 2 20 3 Traceback (most recent call last): 4 File "a.py", line 6, in <module> 5 TypeError: unsupported operand type(s) for +: NoneType and int 16 / 76
1 def f(x, y): 2 print hello 3 print bye 4 print 10 5 f(30, 40) 17 / 76
1 def f(x, y): 2 print hello 3 print bye 4 print 10 5 f(30, 40) 1 10 2 hello 3 bye print hello, print bye f ( ) print 10 f f(30,40), print hello, print bye 17 / 76
Python 1 def f(x): 2 print x 3 print x+1 1 File "a.py", line 3 2 print x+1 3 ^ 4 IndentationError: unexpected indent 18 / 76
Python 1 print 10 2 def f(x): 3 print x 4 5 print 20 1 File "a.py", line 5 2 print 20 3 ^ 4 IndentationError: unindent does not match any outer indentation level 19 / 76
Emacs Emacs Python (xxx.py), Tab ( C-i), Tab, 20 / 76
1 def f(x): 2 print x 3 y = x + 1 4 return y * y 5 def g(x): 6 y = f(x + 1) 7 print x 8 return x + y 9 print g(10) 21 / 76
1 def f(x): 2 print x 3 y = x + 1 4 return y * y 5 def g(x): 6 y = f(x + 1) 7 print x 8 return x + y 9 print g(10) 1 11 2 10 3 154 21 / 76
,. : 1 def f(x, y): 2 return x - y 3 4 print f(2, 3) # x=2, y=3, x, y 1 print f(y=10, x=20), 22 / 76
,, ( ) 1 def g(x, y=1): 2 return x - y 3 4 print g(2) # x=2, y=1 5 print g(x=2) # x=2, y=1 6 print g(2, 3) # x=2, y=3 7 print g(x=2, y=3) # x=2, y=3 8 print g(y=3) # NG x Visual Python, numpy, 23 / 76
: 1 def f(x): 2 print x 3 y = x + 1 4 return y * y 5 def g(x): 6 y = f(x + 1) 7 print x 8 return x + y 9 print g(10), f x (y) g x (y),?. ( ) ( ) 24 / 76
1 def f(x): 2 y = x + 1 3 f(10) 4 print y : 1 Traceback (most recent call last): 2 File "a.py", line 5, in <module> 3 print y 4 NameError: name y is not defined 25 / 76
( ), 1 z = 10 2 def f(x): 3 return x + z 4 print f(20) 5 z = 30 6 print f(20) 26 / 76
( ), 1 z = 10 2 def f(x): 3 return x + z 4 print f(20) 5 z = 30 6 print f(20) : 1 30 2 50, ( ) 26 / 76
import : 1 from import * : 27 / 76
import : 1 print cos(0.1) : 1 Traceback (most recent call last): 2 File "a.py", line 1, in <module> 3 print cos(0.1) 4 NameError: name cos is not defined : 1 from math import * # math import 2 print cos(0.1) : 1 0.995004165278 28 / 76
import : 1 from visual import * 2 sphere() : 29 / 76
import : 1 import : from import *,,. 1 import visual 2 visual.sphere(), OK, 30 / 76
Python Python Visual Python, Matplotlib, numpy? ( )? python, dir( ) 1 from visual import * 2 print dir(visual) 1 [ ALLOW_THREADS, BUFSIZE,... 2... sphere,... ] 31 / 76
print (print ) ( = ) (def...) return (return ) import (from import *) 32 / 76
, ( ) 33 / 76
for (1) ( ): 1 for in range(, ): 2 3 4... : 1 for i in range(a, b): 2... 3..., i = a, a + 1,..., b 1,... : range( ) range(0, ) 34 / 76
for (1) : 1 1 for x in range(3, 7): 2 print x * x 35 / 76
for (1) : 1 1 for x in range(3, 7): 2 print x * x 1 9 2 16 3 25 4 36 35 / 76
for (1) : 2 1 def hatena(n): 2 a = 1 3 for i in range(0, n): 4 a = 2 * a + 1 5 return a 6 print hatena(5) 36 / 76
for (1) : 2 1 def hatena(n): 2 a = 1 3 for i in range(0, n): 4 a = 2 * a + 1 5 return a 6 print hatena(5) 1 63 36 / 76
for (1) : 3 1 def hatena(n): 2 s = 0 3 for i in range(0, n): 4 s = s + i * i 5 return s 6 print hatena(8) 0 2 + 1 2 + + (n 1) 2 s = s + i i... 37 / 76
for (1) : 3 1 def hatena(n): 2 s = 0 3 for i in range(0, n): 4 s = s + i * i 5 return s 6 print hatena(8) 1 140 0 2 + 1 2 + + (n 1) 2 s = s + i i... 37 / 76
if : 1 if 1: 2 3... 4 elif: 2: 5 6... 7... 8 else: 9 10... elif. elif, else: ( ) : 1 (0) ( 1...) 2 (0) 2 ( 2...) 38 / 76
if (1) 1 def one_two_many(x): 2 if x == 1: 3 return ikko 4 elif x == 2: 5 return niko 6 else: 7 return takusan 8 print one_two_many(2) 9 print one_two_many(3) 39 / 76
if (1) 1 def one_two_many(x): 2 if x == 1: 3 return ikko 4 elif x == 2: 5 return niko 6 else: 7 return takusan 8 print one_two_many(2) 9 print one_two_many(3) 1 niko 2 takusan 39 / 76
if (2) 1 def nabetsune(): 2 for i in range(1, 41): 3 if i % 3 == 0 or i % 10 == 3 or 30 < i < 40: 4 print i, "!!!!!" 5 else: 6 print i 7 8 nabetsune() 40 / 76
if (2) 1 def nabetsune(): 2 for i in range(1, 41): 3 if i % 3 == 0 or i % 10 == 3 or 30 < i < 40: 4 print i, "!!!!!" 5 else: 6 print i 7 8 nabetsune() 1 1 2 2 3 3!!! 4... 40 / 76
,, (e.g.,,,,,,... ),, ( ) 41 / 76
: 1 [,,..., ] : ( ) 42 / 76
: 1 from math import * 2 x = 20 3 a = [ 1, 1+2, cos(0.0), x ] 4 print a 5 print len(a) # 6 print a[2] # 7 b = a + [ 1.2, 3.4 ] # 2 8 print b 9 c = [ a, a, a ] # 10 print c[1][2] # c 1 ( ) 2 1 [1, 3, 1.0, 20] 2 4 3 1.0 4 [1, 3, 1.0, 20, 1.2, 3.4] 5 1.0 43 / 76
:,,,,,, 1 def f(l): 2 return l[0] 3 4 print f([1,2,3]) 44 / 76
,, 1 a = [ 2, 3, 5 ] 2 a.append(7) # 3 a[2] = 50 4 del a[1] # a[1]. 5 b = [ a, a, a ] 6 print b 7 a[2] = 30 # 8 print b 1 [[2, 50, 7], [2, 50, 7], [2, 50, 7]] 2 [[2, 30, 7], [2, 30, 7], [2, 30, 7]] 45 / 76
for : 1 [ for in range(, ) ], for ( ) : ( for in ) 46 / 76
: 1 >>> [ x * x for x in range(0, 5) ] 2 -> [0, 1, 4, 9, 16] 1 >>> sum([ x * x for x in range(0, 5) ]) 2 -> 30 1 s = [] 2 for x in range(0, 5): 3 s.append(x * x) 1 s = [] 2 for x in range(0, 5): 3 s += x * x ( : s += E s = s + E ) 47 / 76
: 1,,...,, 1 (,,..., ) :!, len(...) ( )...[...] ( )... +... ( ) 48 / 76
,,, 1 a = (1.1, 2.2, 3.3) 2 a.append(4.4) # NG 3 del a[1] # NG 4 a[1] = 22 # NG 5 a = (4.4, 5.5) # OK 6 a = a + (6.6, 7.7) # OK 2, 49 / 76
1 def polar(x, y): 2 r = sqrt(x * x + y * y) 3 theta = atan2(y, x) 4 return (r,theta) 1 r,theta = polor(3, 4)? OK 1 [a0,a1,a2] = range(3,6) # a0=3, a1=4, a2=5 50 / 76
: 1 "... " 2... 3 """... """ 4....? ",. a = he greeted, "hi" b = "Obama s lecture" 3 (""", ),, len(...)...[...]... +... 51 / 76
: ( ) x, 1 print x,, 1 print "x = %s" % x 1 from math import * 2 y = cos(3.14) 3 print "cos(3.14) = %s" % y 52 / 76
: 2 2, % 1 from math import * 2 x = 2.3 3 print "cos(%s) = %s" % (x, exp(x)) 53 / 76
:, 1 1 % 2, 1, 1 i %s, 2 i %s,, %s... %d : %9d :. 9 9 %f : %.3f :., 3 54 / 76
(1) Python,,, (visual vector, numpy ), Python 5 55 / 76
(2) Python,,, (, ),,, +, len, [...] numpy Visual Python vector, 56 / 76
for (2) : 1 for in : 2 3 4...,, range(a, b),,,,, range(a, b) [ a, a + 1,...b 1 ] 57 / 76
for (2) : 1 for x in [ 2, 3, 5 ]: 2 print x 3 for x in "hello": 4 print x 1 2 2 3 3 5 4 h 5 e 6 l 7 l 8 o 58 / 76
for (2) 1 A = [ (0,1), (2,3), (4, 5) ] 2 for x,y in A: 3 print x + y 1 1 2 5 3 9 59 / 76
for (2) : zip zip(x, Y ) for 1 X = [ 1, 2, 3 ] 2 Y = [ 1, 4, 9 ] 3 for x,y in zip(x, Y): 4 print x + y 1 2 2 6 3 12 60 / 76
for (2) : enumerate enumerate(l) ( ; 0, 1, 2,... ) 1 def find_space(): 2 for i,c in enumerate("hello world"): 3 if c == : 4 return i 5 return -1 1 5 61 / 76
while : (for ) break : for, while continue : for, while ( ), Python 4 62 / 76
: Python,, etc. 63 / 76
: 1 class : 2 3 4... : ( ), 64 / 76
: 1 class nothing: 2 pass # 3 4 # nothing 5 m = nothing() 6 # ( ) 7 m.x = 10 8 m.y = 20 9 # 10 print m.x + m.y 11 # 12 13 def take_nothing(n): 14 n.x = n.x + 100 15 16 take_nothing(m) 17 print m.x 65 / 76
: init, ( ) ( self ) 1 class baseball_team: 2 # 3 def init (self, name, manager, players): 4 self.name = name 5 self.manager = manager 6 self.players = players 7 # 8 def add_player(self, p): 9 self.players.append(p) 66 / 76
: 1 # -> init 2 r = baseball team("nipponham", "Kuriyama", 3 [ "Inaba", "Saito", "Takeda" ]) 4 print r.players 5 # 6 r.add player("ohtani") 7 print r.players 67 / 76
: 1 # -> init 2 r = baseball team("nipponham", "Kuriyama", 3 [ "Inaba", "Saito", "Takeda" ]) 4 print r.players 5 # 6 r.add player("ohtani") 7 print r.players 1 [ "Inaba", "Saito", "Takeda" ] 2 [ "Inaba", "Saito", "Takeda", "Ohtani" ] 67 / 76
,,, Visual Python, numpy,,, (, ) (vector, sphere, + ) 1 center = vector(2, 3, 4) 2 s = sphere(pos=center) 3 s.pos = center + vector(1, 1, 1) 68 / 76
add (self, o), (+) x + y, x. add (y) 1 class vector: 2 def init (self, x, y, z): 3 self.x = x 4 self.y = y 5 self.z = z 6 def add (self, o): 7 return vector(self.x + o.x, self.y + o.y, 8 self.z + o.z) 9 10 u = vector(1,2,3) 11 v = u + vector(4, 5, 6) # u. add (vector(4, 5, 6)) 12 print v, sub, mul, div 69 / 76
Python, +, -, *,, Python, ( ), Visual Python vector ( ), numpy array, mat,,, vector, + 70 / 76
Visual Python 1 from visual import * 2 s1 = sphere(color=color.red) # 3 s2 = sphere(pos=vector(1.0, 2.0, 3.0), radius=0.1) 4 # s1, s2 5 rate(0.3) # 3.3 (0.3 / ) 6 # 7 s2.color = color.green # 8 rate(0.3) # 3.3 (0.3 / ) 9 s2.pos = s2.pos + 0.5 * vector(-1.0,-1.0,-1.0) # Visual Python sphere, (color, radius, pos ),,, OK (, s1.vel = ) 71 / 76
Visual Python 1 from visual import * 2 s1 = sphere(color=color.red) # 3 s2 = sphere(pos=vector(1.0, 2.0, 3.0), radius=0.1) 4 # s1, s2 5 rate(0.3) # 3.3 (0.3 / ) 6 # 7 s2.color = color.green # 8 rate(0.3) # 3.3 (0.3 / ) 9 s2.pos = s2.pos + 0.5 * vector(-1.0,-1.0,-1.0) # Visual Python vector 3 +, * 72 / 76
Visual Python vector 1 >>> from visual import * 2 >>> v = vector(1,2,3) 3 >>> v 4 vector(1, 2, 3) 5 >>> v[0] 6 1.0 7 >>> v.x 8 1.0 9 >>> 2 * v 10 vector(2, 4, 6) 11 >>> v + v 12 vector(2, 4, 6) 13 >>> dir(v) 14 [..., astuple, clear, comp, cross, diff_angle, dot, mag, mag2, norm, proj, rotate, x, y, z ] 73 / 76
Visual Python vector 1 >>> v.mag 2 3.7416573867739413 3 >>> v.mag2 4 14.0 5 >>> v.dot(v) # 6 14.0 7 >>> v.norm() 8 vector(0.267261241912424, 0.534522483824849, 0.801783725737273) 9 >>> v.mag * v.norm() 10 vector(1, 2, 3) 11 >>> v.cross(vector(1,1,1)) # 12 vector(-1, 2, -1) 74 / 76
Visual Python 1 from visual import * 2 s1 = sphere(color=color.red) # 3 s2 = sphere(pos=vector(1.0, 2.0, 3.0), radius=0.1) 4 # s1, s2 5 rate(0.3) # 3.3 (0.3 / ) 6 # 7 s2.color = color.green # 8 rate(0.3) # 3.3 (0.3 / ) 9 s2.pos = s2.pos + 0.5 * vector(-1.0,-1.0,-1.0) # Visual Python rate(f),, f / ( 1/f ) rate, ( ) 75 / 76
( ), Python, # 1 def main(): 2 # location of the pointmass 3 p = vector(1,2,3) 4 # the pointmass 5 s = sphere(pos=p) 6 # the spring 7 sp = helix(pos=p) 8..., 1 1 #! -*- coding: utf-8 -*- 76 / 76