7. 什么是Python? If programming languages are cars.. Python is a great beginner’s car; you can drive it without a license. Unless you want to drive really fast or on really treacherous terrain, you may never need another car.
15. 计算阶乘 n! JavaScript function factorial(n) { return n != 1 ? n * factorial(n - 1) : 1; } factorial(n); Python reduce(lambda x, y: x * y, range(1, n + 1))
16. 找出一个数组中的所有偶数 JavaScript var a = [12, 5, 2, 60, 55, 6, 33, 9], b = []; for (vari = 0; i < a.length; i ++) { if (a[i] % 2 == 0) b.push(a[i]); } Python a = [12, 5, 2, 60, 55, 6, 33, 9] b = [i for i in a if i % 2 == 0]
33. 一点点语法 函数 闭包 >>> def f(a): ... b = a * 2 ... def g(): ... c = b * 2 ... return c ... return g() ... >>> f(3) 12
34. 一点点语法 函数 闭包 >>> def f(a): ... b = a * 2 ... def g(): ... b = b * 2 ... return b ... return g() ... >>> f(3) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 6, in f File "<stdin>", line 4, in g UnboundLocalError: local variable 'b' referenced before assignment
41. Pythonic Python is powerful... and fast Python plays well with others Python runs everywhere Python is friendly... and easy to learn Python is Open
42. Pythonic >>> import this The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!