python入门培训.pptx_第1页
python入门培训.pptx_第2页
python入门培训.pptx_第3页
python入门培训.pptx_第4页
python入门培训.pptx_第5页
已阅读5页,还剩27页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

United Information Technology Co., Ltd.Page 1 Python入门培训演示 文件级别:公开 2011-10-25 系统测试部 叶华 United Information Technology Co., Ltd.Page 2 目录 Python的介绍 Python基本使用 Python实例 参考资料 United Information Technology Co., Ltd. Python的介绍-python与其它语言的区别 Python PK C: python是动态编译语言,c 是静态编辑语言 。 C中内容管理是由开发者管理,python中内存问题由python解释器负责。 python有很多库文件。C语言中对于混杂数组(python中得列表)和哈希表( python中得字典)还没有想要的标准库。 Python 不能用来写内核。 借助Python语言提供的API,使用C或者C+来对Python进行功能性扩展 Python PK Java : python是动态编译语言,Java是静态编辑语言。 Python支持面向对象”分隔 注释符是#,注释多行使用doc string( ) 变量无需类型定义 可进行函数式编程(FP) Python3.x的变迁 United Information Technology Co., Ltd.Page 9 Python的介绍-版本 本讲义约定使用Python 2.x版本 3.x版本由于库没有跟上改进,暂时不推荐使用 United Information Technology Co., Ltd.Page 10 Python的介绍-安装python Linux用户 #下载python包,并安装 $tar zxf python2.7.tar.gz $cd Python2.7 $./configure $make 将两个语句连接在一行中。 5.: 将代码块的头和体分开。 6.语句用缩进的方式体现。 7. 不同缩进度分割不同的代码块。 8.Python 文件以模块的形式组织。 United Information Technology Co., Ltd.Page 16 Python的基本使用-字符串 赋值 str1=“abcdefg” str2 = 123 456 7 操作 print str10 #输出第一个元素 print str11:5 #输出1-4索引的元素 len(str1)#输入字符串长度 for char in str1: #for遍历字符串 print char while index ab in( not in ) “abcd ” #成员操作符 判断 print str1-1 提示 : 字符串类型是不可以改变的,如果你想要改 变一个字符窜就必须通过闯进一个新串的方法。即你 不能只改变一个字符串的一个字符或一个字串。 序列类型操作符 对象 not in 序列 -成员关系操 作符 Seq1 + seq2 -连接操作符(+) Seq1 * seq2 - 重复操作符(*) Seq1n:m -切片操作 United Information Technology Co., Ltd.Page 17 Python的基本使用-列表(list) 赋值 A=10,11,12,13 #元素为整数 B=“red”,”blue”,”green” #元素为字符串 C= #定义空列表 E=A+B #两个列表相加 操作 number = 0,1,2,3 ;number2=“a”,”b” #定义列表 number1:3 #某一部分元素 len(number) #统计列表长度 x = number0 #赋值第一个元素 number0 = 68 #修改元素值 number.append(4) #追加元素 number.insert(3,5) #插入元素 insert(索引位置,插入元素值) number.extend(number2) #合并number & number 列 number.pop(0) #删除第一个值 del number1 #删除元素 列表:一组任意类型的值,按照一定顺序组合而 成的。组成列表的值叫做组员。每个元素被标识 一个索引,索引从0开始。 United Information Technology Co., Ltd.Page 18 Python的基本使用-列表(list) range函数 range(1,5) #返回一个整数列表 操作 for i in range(1,5): print i United Information Technology Co., Ltd.Page 19 Python的基本使用-序列 赋值 f = (2,3,4,5) #整数序列 g = (,) # 空序列 h = (2, 3,4, (10,11,12) # 多维列表 操作 x = f1 # 将f1的元素值赋值x = 3 y = f1:3 # 获得索引为1,2的元素 z = h11 # 二维数组看待 z = 4 特色 l与list类似,最大的不同序列是一种只读且不可变更的数据结构 l不可取代序列中的任意一个元素,因为它是只读不可变更 United Information Technology Co., Ltd.Page 20 Python的基本使用-字典(dict) 赋值 a = # 定义空字典 b = x: 3, y: 4 c = uid: 105, login: beazley, name : David Beazley 操作 u = cuid # 根据索引读取元素值 cshell = “/bin/sh” # 重定义 Copy = c.copy() #拷贝 len(c) #字典元素量 c.clean()#清空字典 United Information Technology Co., Ltd.Page 21 Python的基本使用-条件语句 ifelse ifelif 语句 if y 0 : print “ y 0 ” elif y = 0 : print “ y = 0 ” else : print “ y = a and b c ): print “b is still between a and c ” United Information Technology Co., Ltd.Page 22 Python的基本使用-循环语句 While 语句 import time i = 5 While(i) : print “hello world ! %s”%i time.sleep(1) i = i-1 For 语句(遍历序列的元素) for i in 3, 4, 10, 25: print i # 字符串输出 for c in “Hello World“: print c United Information Technology Co., Ltd.Page 23 Python的基本使用-控制流终止 continue continue语句被用来告诉Python跳过当前循环块中的剩余语句,然后 继续 进行下一轮循 环 break break语句是用来 终止 循环语句的 for i in range(1,5): print i if i = 4 : print “ over the test !” break else : print “ test for continue command 1 !” Continue print “test for continue command 2 !” United Information Technology Co., Ltd.Page 24 Python的基本使用-函数 def语句 def (arg1,arg2) def add(p1 ,p2): print p1,”+”, p2 “=”, p1+p2 Add(1,2) 1 + 2 = 3 返回多个值 return 返回函数值,如果没有return语句,返回返回值为None。 def divide(a,b): q = a/b r = a - q*b return q,r x,y = divide(42,5) # x = 8, y = 2 United Information Technology Co., Ltd.Page 25 Python的基本使用-模块 程序可分成好几个模块: 一个py文件就是一个模块;目录下面增加_init_.py也是 # numbers.py def divide(a, b): q = a/b r = a - q*b return q, r def gcd(x, y): g = y while x 0: g = x x = y % x y = g return g Import语句 import numbers x, y = numbers.divide(42,5) n = numbers.gcd(7291823, 5683) United Information Technology Co., Ltd.Page 26 Python的基本使用-异常处理 try语句 try: f = open(“foo“) except IOError : print “Couldnt open foo . Sorry.“ raise语句 def factorial(n): if n factorial(-1) Traceback (innermost last): File “, line 1, in ? File “, line 3, in factorial ValueError: Expected non-negative number United Information Technology Co., Ltd. 文件读写 open()函数 f = open(“foo”,“w”) # 写方式打开文件 g = open(“bar”,“r”) # 只读方式打开文件 f.close()#关闭文件 文件的读取/写入 f.write(“Hello World“) buff = g.read() # 读取文件所有数据 line = g.readline() # 读一行数据 lines = g.readlines() # 以列表的方法返回文件所有数据 格式化的输入 使用%来格式化字符串 for i in range(0,10): f.write(“2 times %d = %dn“ % (i, 2*i) Page 27 United Information Technology Co., Ltd.Page 28 Python实例 例子1-python语言开发脚本程序 例子2-python支持多平台 例子3-多模块python语言开发脚本程序 United Information Technology Co., Ltd.Page 29 其他 类和对象,面向对象编程 Python的标准库与第三方库 United Information Technology Co., Ltd.Page 30 Python参考资料 Python中文社区 http:/pyth

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论