def 函数名(参数列表): 表达式 |
def square(x): return x**2 def helloworld(): print('Hello World') |
def a(): pass |
函数名(参数列表) 变量 = 函数名(参数列表) >>> def square(x): return x**2 print(x**2) >>> square(5) 25 >>> b = square(6) >>> b 36 |
>>> def f(a=1,b=2,c=3): print(a,b,c) >>> f() 1 2 3 >>> f(4,5,6) 4 5 6 >>> f(4,6) 4 6 3 >>> f (100,c=300) 100 2 300 |
>>> def f4(a, L=[]): L.append(a) print(L) >>> f4(1) [1] >>> f4(2) [1, 2] >>> f4(3) [1, 2, 3] >>> f4(5,[3,6]) [3, 6, 5] |
>>> def f1(*x): if len(x) == 0: print (None) else: print (x) >>> f1() None >>> f1(1) (1,) >>> f1('') ('',) |
>>> def f2(**x): if len(x) == 0: print (None) else: print (x) >>> f2(4) Traceback (most recent call last): File "<pyshell#20>", line 1, in <module> f2(4) TypeError: f2() takes 0 positional arguments but 1 was given >>> f2(a=1,b=2) {'a': 1, 'b': 2} |
>>> def f3(a=1,b=2,*x,**y): print (a,b,x,y) >>> f3(x=1,y=2) 1 2 () {'y': 2, 'x': 1} >>> f3(1,2) 1 2 () {} >>> f3(1,2,3,4) 1 2 (3, 4) {} >>> f3(1,2,3,4,x=1,y=2) 1 2 (3, 4) {'y': 2, 'x': 1} >>> f3(1,2,x=1) 1 2 () {'x': 1} >>> f3(1,2,af=1) 1 2 () {'af': 1} |
>>> def add(): def add1(x): return x + x return add1 >>> a = add() >>> a <function add.<locals>.add1 at 0x00000000020E2840> >>> a(19) 38 |
>>> a = 1 >>> b = 2 >>> def x(): def y(): c = b**(a+b) print(c) return y() >>> x() 8 |
>>> def x(): def y(): a = b**(a+b) print(c) return y() >>> x() Traceback (most recent call last): File "<pyshell#31>", line 1, in <module> x() File "<pyshell#30>", line 5, in x return y() File "<pyshell#30>", line 3, in y a = b**(a+b) UnboundLocalError: local variable 'a' referenced before assignment >>> |
>>> a = 1 >>> b = 2 >>> def z(): c = 3 def za(): global a,b nonlocal c a = a**2 b = b**2 c = c**2 print(a,b,c) return za() >>> z() 1 4 9 >>> |
>>> def add(a,b): return a+b >>> def test(fun,a,b): print(fun(a,b)) >>> >>> c = add >>> test(c,1,3) 4 >>> |
lambda 参数列表 : 表达式 |
lambda x: print (x) |
a = lambda x:x**2 print(a(2)) L = [lambda x:x**2,lambda x:x**3] print(L) print(L[0](10)) >>> ================================ RESTART================================ >>> 4 [<function <lambda> at 0x00000000020E2840>, <function <lambda> at0x0000000003336510>] 100 |
>>> a = lambda x: ((x/2 == int(x/2)) and "偶数" or "奇数") >>> a(5) '奇数' >>> a(4) '偶数' |
>>> F=lambda x: map((lambda y: y**2), x) >>> L1 = list(range(10)) >>> print(list(F(L1))) [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] |
def sortkey(x): return str(x) a.sort(key = sortkey) |
a.sort(key = key=lambda x:str(x)) |
def adddots(n): Range = range(-300,300) xs=random.sample(Range,n) ys=random.sample(Range,n) dots = [] for i in range(n): pu() goto(xs[i],ys[i]) pd() dot(10,'black') dots.append([xs[i],ys[i]]) return dots |
pt_unpick = adddots(N) pt_unpick.sort() pt0 = pt_unpick[0] pt_unpick.remove(pt0) pt_unpick.append(pt0) pt_pick = [pt0] pt_next = pt0 |
def vectorcross(pt,pts): pt_choose = pts[0] for i in range(1,len(pts)): vec1 = [pt_choose[0]-pt[0],pt_choose[1]-pt[1]] vec2 = [pts[i][0]-pt[0],pts[i][1]-pt[1]] veccross = vec1[0]*vec2[1]-vec1[1]*vec2[0] if veccross < 0: pt_choose = pts[i] return pt_choose |
while True: pt_next = vectorcross(pt_next,pt_unpick) pt_unpick.remove(pt_next) pt_pick.append(pt_next) if pt_next == pt0: pt_pick.append(pt0) break |
from turtle import * from turtle import Screen, mainloop import sys import random N = int(numinput("请输入生成点的个数", 100, 10, 8)) def adddots(n): Range = range(-300,300) xs=random.sample(Range,n) ys=random.sample(Range,n) dots = [] for i in range(n): pu() goto(xs[i],ys[i]) pd() dot(10,'black') dots.append([xs[i],ys[i]]) return dots def vectorcross(pt,pts): pt_choose = pts[0] for i in range(1,len(pts)): vec1 = [pt_choose[0]-pt[0],pt_choose[1]-pt[1]] vec2 = [pts[i][0]-pt[0],pts[i][1]-pt[1]] veccross = vec1[0]*vec2[1]-vec1[1]*vec2[0] if veccross < 0: pt_choose = pts[i] return pt_choose def main(): pt_unpick = adddots(N) print(pt_unpick) pt_unpick.sort() print(pt_unpick) pt0 = pt_unpick[0] pt_unpick.remove(pt0) pt_unpick.append(pt0) pt_pick = [pt0] pt_next = pt0 while True: pt_next = vectorcross(pt_next,pt_unpick) pt_unpick.remove(pt_next) pt_pick.append(pt_next) if pt_next == pt0: pt_pick.append(pt0) break shape('circle') shapesize(0.01,0.01,0.01) pu() pensize(2) pencolor('blue') goto(pt_pick[0][0],pt_pick[0][1]) pd() for i in range(1,len(pt_pick)): goto(pt_pick[i][0],pt_pick[i][1]) screen = Screen() screen.delay(0) main() mainloop() |
class Dots(Turtle): def __init__(self, x, y, i): screen = Screen() screen.delay(0) Turtle.__init__(self) self.pu() self.shape("circle") self.shapesize(0.6,0.6,1) self.speed(0) self.setpos((x,y)) self.ondrag(self.drag) self.onrelease(self.dynamic) self.i = i def drag(self, x, y): self.color('red') self.sety(y) self.setx(x) def dynamic(self,x,y): self.color('black') global dots dots[self.i] = [x,y,self.i] main() |
月之眼 发表于 2013-10-1 01:40
大川哥的博客上不去了。。。
欢迎光临 NCF参数化建筑论坛 (http://bbs.ncf-china.com/) | Powered by Discuz! X3.2 |