SlideShare a Scribd company logo
Python入门fengxing <annidy@gmail.com>
目录Python简介Python环境搭建Python基本语法高级主题Q&A
Python简介
Python起源创始人Guido van Rossum1989年12月,Guido为了打发圣诞节假期,决定开发一种新的脚本语言,作为ABC语言后后代。蒙提·派森的飞行马戏团的狂热爱好者,选择了 Python 作为项目的标题
Python与脚本Python通常被认为是“脚本语言”,但它完全有能力开发大型项目又称之为“胶水语言”,因为通常在性能要求极高的部份会用其它语言(比如C/C++)实现
Python特点简洁高级语言代码可读性高可移植性好高效
简洁The Zen of PythonBeautiful 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.orgActivePythonCygwin PythonPython(x,y)……
Python的IDEIDLEPythonWinulipadKomodoWing IDEEclipse+Pydev……
我的PythonActive Python 2.6ipythongVim
Hello Worldprint “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 aThis is apple>>> b = “Ha” * 3>>> print bHaHaHa
基本类型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 = 12if a < 12:print ‘a less 12’elif a < 16:print ‘a bigger 16’else:print ‘ 12 <= a < 16
控制语句while expression: a = 12while a > 0:		print ‘a is’, a--
控制语句for target_list in expression_list: a = [1, 3, 5, 7, 9]foriin a:printisum = 0foriinrange(100):		sum += iprint sum
函数定义def funcname ([parameter_list]):deffoo(a, b):return a * 2 + bprintfoo(3, 4)    			# 10printfoo(b = 3, a = 4) 	# 11
类定义classclassRect:		def__init__(self, w, h):self.w  = wself.h  = h		def area(self):			returnself.w * self.hr1 = Rect(3, 4)r1.area()		#12
Python入门
高级主题
So Easy?!迭代器推导列表&生成列表装饰器函数编程描述符和属性元编程……
函数编程(FP)函数是第一类不需要控制、循环语句;用关系表达式、递归代替通常利用“更高等级”函数
Lambda作用:创建匿名函数
lambda [parameter_list]: expressionp = lambda x: x*xq = lambda x, y: x + y
FP“高等级”函数
示例1:条件判断if cond1:func1()elif cond2:func2()else:func3()(cond1andfunc1())or (cond2 andfunc2()) or (func3())
示例2:计算阶乘s = 1foriinrange(1, 10):	 s *= ireduce(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:printiprintfilter(None,map(lambda y:y*reduce(lambdax,y:x*y!=0, \map(lambdax,y=y:y%x, range(2,int(pow(y,0.5)+1))),\                                        1),\range(2,1000)))
总结函数编程有时让事情变得简单,有时让事情变得复杂。
推荐书目Python核心编程:第2版Python参考手册(第4版)Python高级编程Foundations of Python Network ProgrammingPython源码剖析

More Related Content

PPT
開放原始碼的回收與再利用
PDF
Python速成指南
PDF
Python in Computer Vision
PPT
Introduction to Python
PPT
Python story
PPT
Python Basic
ODP
Op 20090411
PPT
Go语言: 互联网时代的C
開放原始碼的回收與再利用
Python速成指南
Python in Computer Vision
Introduction to Python
Python story
Python Basic
Op 20090411
Go语言: 互联网时代的C

Similar to Python入门 (7)

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

Python入门