Advertisement
Advertisement

More Related Content

Advertisement

Python story

  1. Python Story 陈小玉 <smallfish.xy@gmail.com> twitter: @nnfish blog: http://chenxiaoyu.org
  2. 历史由来 阿姆斯特丹 1989 年圣诞节期间 Guido van Rossum 为了打发圣诞节的无趣 决心开发一种新的解释性程序 … 不久之后,一个叫 Python 的语言就诞生了! 注: Guido 又称龟叔或者莽爹 :_)
  3. hello, world
  4. 代码截图
  5. 序列切片 >>> a = 'abcd' >>> print a[1:3] bc >>> print a[1:] bcd >>> print a[:] abcd >>> print a[:3] abc >>> print a[:-1] abc >>> print a[-2] c 字符串反转 >>> print a[::-1] dcba a b c d 0 1 2 3 -4 -3 -2 -1
  6. 流程控制 if a == 1: print 'aaa' else : print 'bbb' if b == 1: print '111' elif b == 2: print '222' else : print '000' while a < 10: print a a += 1 for item in (1, 2, 3): print item d = {'name': 'smallfish', 'age': 20} for k in d: print k, d[k] for k, v in d.items(): print k, v 输出 age 20 name smallfish
  7. 函数( 1 ) def hello(name): &quot;hello function, name is param&quot; print &quot;hello&quot;, name >>> print hello.__doc__ hello function, name is param >>> hello('smallfish') hello smallfish # 给函数参数加默认值 def hello(name='smallfish'): # 同上代码 >>> hello() hello smallfish >>> hello('chenxiaoyu') hello chenxiaoyu
  8. 函数( 2 ) # 不定参数 def hello(*args, **kwargs): print args print kwargs >>> hello() () {} >>> hello(1, 2, 3) (1, 2, 3) {} >>> hello(1, 2, 3, a='aa', b='bb') (1, 2, 3) {'a': 'aa', 'b': 'bb'} >>>
  9. 函数( 3 ) lambda 函数 简单函数: def lowercase(x): return x.lower() 其实可以这么写: lowercase = lambda x: x.lower()
  10. 内置函数 >>> help(re) Help on module re: NAME re - Support for regular expressions (RE). FILE d:ython27ibe.py DESCRIPTION … >>> dir(re) ['DEBUG', 'DOTALL', ..., 'sys', 'template'] >>> globals() {'a': 'abcd', '__builtins__': <module '__builtin__' (built-in)> … } locals() vars() 输出基本同上 >>> type(re) <type 'module'> >>> type('aa') <type 'str'>
  11. Class class User: def __init__(self, name): self.name = name def get_name(self): return self.name >>> u = User('smallfish') >>> print u.get_name() smallfish
  12. 模块 # user.py def hello(name): print 'hello', name class User: def __init__(self, name): self.name = name def get_name(self): return self.name >>> import user >>> dir (user) ['User', '__builtins__', '__doc__', '__file__', '__name__', '__package__', 'hello'] >>> user.hello('smallfish') hello smallfish >>> u = user.User('smallfish') >>> print u.get_name() smallfish
  13. 列表推导 L ist C omprehension (适用于 dict , tuple , str ) 过滤数组中除以 2 等于 0 的元素: tmp = [] items = [1, 2, 3, 4, 5] for i in items: if i % 2 == 0: tmp.append(i) # 其实可以这么做: >>> [item for item in items if item % 2 == 0] [2, 4] # 每个元素都乘以 2 >>> [item * 2 for item in items] [2, 4, 6, 8, 10]
  14. 修饰器( 1 ) Decorator , Python2.4 之后新加特性! 问题:想记录每个函数执行多少时间? def hello(): start = time.time() … run code print time.time() – start 是不是太丑了点?用修饰器吧。 先写一个包装函数: def time_wrapper(func): def _wrapper(*args, **kwargs): start = time.time() func(*args, **kwargs) print func.__name__, time.time() – start return _wrapper
  15. 修饰器( 2 ) 测试函数: def hello(n): sum = 0 for i in range(n): sum += I return sum 我们可以这样调用: a = time_wrapper(hello) print a(100) 这个不稀奇,还可以这样写: @time_wrapper def hello(n): … 同上 >>> hello(1000000) hello 0.265000104904
  16. 结束 print &quot; Thanks! &quot; import sys sys.exit(0)
Advertisement