SlideShare a Scribd company logo
1 of 45
Python入门 fengxing <annidy@gmail.com>
目录 Python简介 Python环境搭建 Python基本语法 高级主题 Q&A
Python简介
Python起源 创始人Guido van Rossum 1989年12月,Guido为了打发圣诞节假期,决定开发一种新的脚本语言,作为ABC语言后后代。 蒙提·派森的飞行马戏团的狂热爱好者,选择了 Python 作为项目的标题
Python与脚本 Python通常被认为是“脚本语言”,但它完全有能力开发大型项目 又称之为“胶水语言”,因为通常在性能要求极高的部份会用其它语言(比如C/C++)实现
Python特点 简洁 高级语言 代码可读性高 可移植性好 高效
简洁 The Zen of Python 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! Python之禅 优美胜于丑陋 明了胜于晦涩 简洁胜于复杂 复杂胜于凌乱 扁平胜于嵌套 间隔胜于紧凑 可读性很重要 即便假借特例的实用性之名,也不可违背这些规则 不要包容所有错误,除非你确定需要这样做当存在多种可能,不要尝试去猜测 而是尽量找一种,最好是唯一一种明显的解决方案 虽然这并不容易,因为你不是 Python 之父 做也许好过不做,但不假思索就动手还不如不做 如果你无法向人描述你的方案,那肯定不是一个好方案;反之亦然 命名空间是一种绝妙的理念,我们应当多加利用
示例:统计the在abc.txt出现次数 print  sum([line.count(‘the') for line inopen('abc.txt','r') ])
高级语言 类 内存自动管理 内建list和dict
代码可读性高 强制缩进 没有 { } ;    有效避免太深嵌套 社区中有统一的编码规范 (PEP8) 简洁
可移植性好 各个平台都有解释器实现 Python通常不用于系统级开发 调用C/C++库非常方便
高效 号称有脚本语言中最丰富和最强大的类库 解释性和编译性
Python环境搭建
Python版本的选择 2.x VS 3.x
Python的发行版 www.python.org ActivePython Cygwin Python Python(x,y) ……
Python的IDE IDLE PythonWin ulipad Komodo Wing IDE Eclipse+Pydev ……
我的Python Active Python 2.6 ipython gVim
Hello World print “Hello world”
Python语法
基本类型
基本类型 整形 (-2147483648 ~ 2147483647   32bit) >>> a = 12 >>> b = -1234 >>> type(a) <type 'int'>
基本类型 长整形 (-∞ ~ +∞) >>> a = 123L >>> type(a) <type 'long'> >>> b = 2147483647 + 1 >>> type(b) <type ‘long'>
基本类型 浮点 >>> a = 12.3 >>> type(a) <type ‘float'> >>> b = 3.14e-10 >>> type(b) <type ‘float'>
基本类型 str >>> a = ‘ This is’ + ‘apple’ >>> print a This is apple >>> b = “Ha” * 3 >>> print b HaHaHa
基本类型 list >>> a =[1, 2, 3, 5, ‘a’, ‘b’] >>> a[0] 1 >>> a[-1] b >>> a[2:5] [3, 5, ‘a’] >>>  a[-1:0:-1]        #逆序 [‘b’, ‘a’, 5, 3, 2, 1]
基本类型 dict >>> a = {‘a’: 12,  ‘f’: 5,  ‘bs’: 23} >>> a[‘a’] 12 >>> a[‘bs’] 23 >>> a[‘k’] KeyError: ‘k’
基本类型 tuple >>> a = (1, 2, 3, 5, ‘a’, ‘b’) >>> a[0] 1 >>> a[-1] b >>> a[2:5] [3, 5, ‘a’] >>>a[-1:0:-1] [‘b’, ‘a’, 5, 3, 2, 1]
控制语句
控制语句 if expression : a = 12 if a < 12: print ‘a less 12’ elif a < 16: print ‘a bigger 16’ else: print ‘ 12 <= a < 16
控制语句 while expression:  a = 12 while a > 0: 		print ‘a is’, a--
控制语句 for target_list in expression_list:  a = [1, 3, 5, 7, 9] foriin a: printi sum = 0 foriinrange(100): 		sum += i print sum
函数定义 def funcname ([parameter_list]): deffoo(a, b): return a * 2 + b printfoo(3, 4)    			# 10 printfoo(b = 3, a = 4) 	# 11
类定义 class classRect: 		def__init__(self, w, h): self.w  = w self.h  = h 		def area(self): 			returnself.w * self.h r1 = Rect(3, 4) r1.area()		#12
高级主题
So Easy?! 迭代器 推导列表&生成列表 装饰器 函数编程 描述符和属性 元编程 ……
函数编程(FP) 函数是第一类 不需要控制、循环语句;用关系表达式、递归代替 通常利用“更高等级”函数
Lambda ,[object Object]
lambda [parameter_list]: expressionp = lambda x: x*x q = lambda x, y: x + y
FP“高等级”函数
示例1:条件判断 if cond1: func1() elif cond2: func2() else: func3() (cond1andfunc1())or (cond2 andfunc2()) or (func3())
示例2:计算阶乘 s = 1 foriinrange(1, 10): 	 s *= i reduce(lambda a, b:a*b, range(1, 10), 1)
示例3:计算100以内质数 foriinrange(2, 100): for j inrange(2, int(pow(i, 0.5)+1): if not i%j: printi printfilter(None,map(lambda y:y*reduce(lambdax,y:x*y!=0, br />map(lambdax,y=y:y%x, range(2,int(pow(y,0.5)+1))),br />                                        1),br />range(2,1000)))
总结 函数编程有时让事情变得简单,有时让事情变得复杂。
推荐书目 Python核心编程:第2版 Python参考手册(第4版) Python高级编程 Foundations of Python Network Programming Python源码剖析

More Related Content

Similar to Python入门

張逸 - 研究所 / 轉學考計算機概論 、公職計算機概要 - 程式語言 - 試閱版
張逸 - 研究所 / 轉學考計算機概論 、公職計算機概要 - 程式語言 - 試閱版張逸 - 研究所 / 轉學考計算機概論 、公職計算機概要 - 程式語言 - 試閱版
張逸 - 研究所 / 轉學考計算機概論 、公職計算機概要 - 程式語言 - 試閱版逸 張
 
数据处理算法设计要点
数据处理算法设计要点数据处理算法设计要点
数据处理算法设计要点thinkinlamp
 
JCConf 2015 TW 高效率資料爬蟲組合包
JCConf 2015 TW 高效率資料爬蟲組合包JCConf 2015 TW 高效率資料爬蟲組合包
JCConf 2015 TW 高效率資料爬蟲組合包書豪 李
 
Grails敏捷项目开发
Grails敏捷项目开发Grails敏捷项目开发
Grails敏捷项目开发Michael Yan
 
第一次程式設計就上手 - 使用Python 與周蟒(zhpy)
第一次程式設計就上手  - 使用Python 與周蟒(zhpy)第一次程式設計就上手  - 使用Python 與周蟒(zhpy)
第一次程式設計就上手 - 使用Python 與周蟒(zhpy)Fred Lin
 
開發學校雲端服務的奇技淫巧(Tips for Building Third-Party School Service)
開發學校雲端服務的奇技淫巧(Tips for Building  Third-Party School Service)開發學校雲端服務的奇技淫巧(Tips for Building  Third-Party School Service)
開發學校雲端服務的奇技淫巧(Tips for Building Third-Party School Service)Sheng-Hao Ma
 
ajax_onlinemad
ajax_onlinemadajax_onlinemad
ajax_onlinemadKitor23
 

Similar to Python入门 (7)

張逸 - 研究所 / 轉學考計算機概論 、公職計算機概要 - 程式語言 - 試閱版
張逸 - 研究所 / 轉學考計算機概論 、公職計算機概要 - 程式語言 - 試閱版張逸 - 研究所 / 轉學考計算機概論 、公職計算機概要 - 程式語言 - 試閱版
張逸 - 研究所 / 轉學考計算機概論 、公職計算機概要 - 程式語言 - 試閱版
 
数据处理算法设计要点
数据处理算法设计要点数据处理算法设计要点
数据处理算法设计要点
 
JCConf 2015 TW 高效率資料爬蟲組合包
JCConf 2015 TW 高效率資料爬蟲組合包JCConf 2015 TW 高效率資料爬蟲組合包
JCConf 2015 TW 高效率資料爬蟲組合包
 
Grails敏捷项目开发
Grails敏捷项目开发Grails敏捷项目开发
Grails敏捷项目开发
 
第一次程式設計就上手 - 使用Python 與周蟒(zhpy)
第一次程式設計就上手  - 使用Python 與周蟒(zhpy)第一次程式設計就上手  - 使用Python 與周蟒(zhpy)
第一次程式設計就上手 - 使用Python 與周蟒(zhpy)
 
開發學校雲端服務的奇技淫巧(Tips for Building Third-Party School Service)
開發學校雲端服務的奇技淫巧(Tips for Building  Third-Party School Service)開發學校雲端服務的奇技淫巧(Tips for Building  Third-Party School Service)
開發學校雲端服務的奇技淫巧(Tips for Building Third-Party School Service)
 
ajax_onlinemad
ajax_onlinemadajax_onlinemad
ajax_onlinemad
 

Python入门