Python 快速介绍

limodou , Zoom.Quiet

[any material that should appear in print but not on the slide] limodou@gmail.com ;

开场白

[any material that should appear in print but not on the slide]

大纲

[any material that should appear in print but not on the slide]

Python怎么读?

[any material that should appear in print but not on the slide]

Python在历史洪流中的位置

http://www.oreilly.com/pub/a/oreilly/news/languageposter_0504.html http://www.levenez.com/lang/

Python的诞生


创始人
Guido van Rossum
  • 1989年圣诞节期间,在阿姆斯特丹
  • Guido为了打发圣诞节的无趣,决心开发一个新的脚本解释程序,做为ABC语言的一种继承
    • 实际上,第一个Python实现是运行在Mac机
    • 使用Python作为语言的名字,因为是英国幽默剧团:"Monty Python飞行马戏团"的fans
    • ABC是由Guido参加设计的一种教学语言非常优美和强大,是专门为非专业程序员设计的
  • 原在荷兰,现在
  • 因为Python,Guido 02-02-16 荣获FSF颁发的第四届自由软件进步大奖
http://blog.csdn.net/gvanrossum/archive/2006/06/23/824656.aspx http://www.gnu.org/press/2002-02-16-FSF-Award.html

Python 的江湖地位

Python 的流行程度

SoureForge.net2005-11 统计:

TIOBE Programming历史统计趋势图

小结 - 历史

[any material that should appear in print but not on the slide]

为什么学习Python?

[any material that should appear in print but not on the slide]

现实中Python的应用领域

  • 桌面GUI软件开发,网络应用开发
  • 2/3D图形处理,游戏开发
  • 文档处理,Web应用开发
  • 科学计算,移动设备应用开发
  • 嵌入其它应用开发...
  • 可以说除了OS和驱动程序,Python 可以干一切!
官网首页成功实例推荐

桌面GUI软件开发

[any material that should appear in print but not on the slide]

网络应用

[any material that should appear in print but not on the slide]

2/3D 图形处理

  • 3D
    • 大象之梦核心动力
    • , ,LightWave, Nendo, Off, Radiosity, Raw Triangle, TrueSpace and Wings 3d
      ...主流3D建模/渲染引擎都有Python接口
    • Panda3D Disney 开发
    • ...
3D 'blender-home.png' blender.org - Python http://www.blender.org/tutorials-help/python/ MaYa http://www.autodesk.com.cn/adsk/servlet/index?siteID=1170359&id=8821472

游戏/媒体应用开发

国际工作机会 http://www.opensourcexperts.com/Index/index_html/Python/index.html 《EVE》首次将前沿编程应用于网游_网络游戏EVE_新浪游戏_新浪网 http://games.sina.com.cn/o/n/2006-03-18/1039145594.shtml Stackless Python http://www-128.ibm.com/developerworks/cn/linux/sdk/python/python-7/ [any material that should appear in print but not on the slide]

文档处理

[any material that should appear in print but not on the slide] TeX http://www.woodpecker.org.cn:9081/graspOnline/learn.tsinghua.edu.cn/homepage/2001315450/tex/tex_start.html

Web应用开发

宽广的其它领域应用...

[any material that should appear in print but not on the slide] Python for Mobile Devices http://www.awaretek.com/pymo.html Python for S60 http://www.forum.nokia.com/info/sw.nokia.com/id/ee447e84-2851-471a-8387-3434345f2eb0/Python_for_S60.html

Python的影响

[any material that should appear in print but not on the slide]

小结 - 现实

[any material that should appear in print but not on the slide]

到底什么是Python?

Python是一种语法简洁优美的, 面向对象的, 内置高级数据结构, 支持模块和包,支持多种平台, 可扩展的解释型通用编程语言。
[any material that should appear in print but not on the slide]
代码演示顺序... help() dir()
l=range(10)
N=100
l[:-2:2]

99表:
seed=range(1,10,1)
for i in seed: [i*j for j in seed[:i]]

[item for item in l if item > bound ] 

In [154]: a=7
In [156]: [d for d in range(2,a-1)]
Out[156]: [2, 3, 4, 5]
In [155]: [a%d for d in range(2,a-1)]
Out[155]: [1, 1, 3, 2]

In [162]: a=8
In [163]: 0 not in [a%d for d in range(2,a-1)]
Out[163]: False

单行素数扫描!
In [167]: N=10
In [168]: [p for p in range(2,N) if  0 not in [p%d for d in range(2,p-1)]]
Out[168]: [2, 3, 5, 7]

参数:
工厂模式...
In [174]: def doit(do,i,j):
   .....:     done = "%s %s %s" % (i,do,j)
   .....:     print "want do::",done
   .....:     return eval(done)

In [125]: def myfun(*args):
   .....:     '''try function def
   .....:     '''
   .....:     print len(args),args
   .....:     return args[0]+args[1]

In [139]: def myfun(**kvar):
   .....:     print kvar
   .....:     for k in kvar:
   .....:         kvar[k]+=2
   .....:     print kvar
In [140]: myfun(k1=12,k2=23)
{'k2': 23, 'k1': 12}
{'k2': 25, 'k1': 14}

重载+
In [119]: class nplus:
   .....:     def __init__(self,init):
   .....:         self.one = init
   .....:         print "creat n plus obj"
   .....:     def __add__(self,two):
   .....:         return self.one*two


自省!!
In [89]: def repself(item):
   ....:     print "ID    :",id(item)
   ....:     print "TYPE  :",type(item)
   ....:     print "VALUE :",repr(item)
   ....:     if hasattr(item,"__name__"):print "NAME::",item.__name__
   ....:     if hasattr(item,"__class__"):print "CLASS::",item.__class__.__name__

import traceback
In [200]: def callme():
   .....:     s = traceback.extract_stack()
   .....:     print "i'd call by %s !" % s[-2][2]

In [201]: fun1()
i'd call by fun1 !

MixIn:
In [202]: class A:
   .....:     def __init__(self):
   .....:         self.name = "class A"
   .....:     def do(self):
   .....:         print self.name

In [203]: class B:
   .....:     def __init__(self):
   .....:         self.name = "class B"
   .....:     def add(self,a,b):
   .....:         return a+b
In [212]: A.__bases__ +=(B,)

In [213]: A.
A.__doc__     A.__init__    A.__module__  A.add         A.do

Pluin:::

def p2(self,*arg):print "in %s !!!! %s"%(self.name,arg)
setattr(A,"plugin",p2)

对象序列化:
In [234]: lis=[1,2,"as",[1,2],(2,3,3),{}]
In [235]: pickle.dump(lis,open("lisA.dump","w"))

In [237]: li2 = pickle.load(open("lisA.dump"))
In [238]: li2
Out[238]: [1, 2, 'as', [1, 2], (2, 3, 3), {}]

编译字节文件:
def myfunc(alist):
    return len(alist)
the following command can be used to get the disassembly of myfunc():
import dis
>>> dis.dis(myfunc)
  2           0 LOAD_GLOBAL              0 (len)
              3 LOAD_FAST                0 (alist)
              6 CALL_FUNCTION            1
              9 RETURN_VALUE        
             10 LOAD_CONST               0 (None)
             13 RETURN_VALUE
			 
函式编程
map(func, list, …)
相当于[func(l) for l in list]
In [241]: map((lambda x:x*x),range(10))
Out[241]: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
In [254]: map((lambda x,y:x+y),range(0,10,1),range(10,0,-1))
Out[254]: [10, 10, 10, 10, 10, 10, 10, 10, 10, 10]

In [257]: a=range(1,10,2)
In [258]: a
Out[258]: [1, 3, 5, 7, 9]

In [259]: b=range(10,0,-2)
In [260]: b
Out[260]: [10, 8, 6, 4, 2]

In [264]: zip(a,b)
Out[264]: [(1, 10), (3, 8), (5, 6), (7, 4), (9, 2)]

In [101]: [(lambda x,y : x+y)(i,j) for i,j in zip(a,b)]
Out[101]: [11, 11, 11, 11, 11]

In [102]: ["%s-%s "%(i,j) for i,j in zip(a,b)]
Out[102]: ['1-10 ', '3-8 ', '5-6 ', '7-4 ', '9-2 ']

蟒学!

[any material that should appear in print but not on the slide]

Zen of Python

蟒之禅 其它译本
优美胜过丑陋 明确胜过含蓄
简单胜过复杂 复杂胜过难懂
扁平胜过嵌套 稀疏胜过密集
易读亦有价
尽管实用会击败纯洁 特例也不能特殊到打破规则
除非明确地使其沉默 错误永远不应默默地溜掉
面对着不确定,要拒绝猜测的诱惑
应该有一个--宁肯只有一个--明显的实现方法
也许这个方法开始不是很明显,除非你是荷兰人
尽管不做通常好过立刻做 但现在做也要胜过不去做
如果实现很难解释,那它就是一个坏想法
如果实现容易解释,那它可能就是一个好想法
名字空间是一个响亮的出色想法--就让我们多加利用吧
[any material that should appear in print but not on the slide]

小结 - 沉迷吧!

[any material that should appear in print but not on the slide]

如何学习Python

[any material that should appear in print but not on the slide]

Python在中国

[any material that should appear in print but not on the slide]

woodpecker.org.cn

CPUG - China Python User Group

[any material that should appear in print but not on the slide]

学习Python的一些资源

[any material that should appear in print but not on the slide]

问题时间

谢谢!
[any material that should appear in print but not on the slide]

关于...

作者