Python基础学习知识教学教育资料(自学资料文本记录方案计划).doc





《Python基础学习知识教学教育资料(自学资料文本记录方案计划).doc》由会员分享,可在线阅读,更多相关《Python基础学习知识教学教育资料(自学资料文本记录方案计划).doc(38页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、第一章 快速改造:基础知识1.2交互式解释器在IDLE编辑器,在提示符后输入help然后按回车;也可以按下F1获得有关IDLE的帮助信息1.4数字和表达式1/2返回0,整除除法;1.0/2返回0.5,用一个或者多个包含小数点的数字参与计算。另外改变除法的执行方式:from_future_import division/可以实现整除,1.0/2.0返回0.0%取余数; *幂运算; 1/20 1.0/20.5 1.0/2.00.0 10%31 9*(1/2)1 9*(1.0/2)3.0 2.75%0.50.25 -9%43 -3%21 -3/2-21.4.1长整数普通整数不能大于214748364
2、7也不能小于-2147483648,若更大的数,可以使用长整数。长整数结尾有个L,理论上小写也可以,不过为了便于识别,尽可能用大写。1.4.2十六进制和八进制0XAF返回175 ,十六进制;010返回8,八进制 0xAF175 01081.5变量包含字母、数字和下划线。首字母不能是数字开头。1.8函数Pow计算乘方:pow(2,3),2*3均返回8;pow等标准函数称为内建函数。Abs(-10)求绝对值,返回10;round(1.0/2.0)返回1.0,把浮点数四舍五入为最接近的整数值。 pow(2,3)8 2*38 abs(-10)10 round(1.0/2.0)1.0 round(8.0
3、6,2)8.06 round(8.06,1)8.11.9模块 import import math math.floor(8.8) 向下取整8.0 math.ceil(8.8)向上取整9.0 int(math.ceil(32.1)33 int(32.9)32 flo=math.floor flo(33.9)33.0使用了from 模块import 函数 ,这种方式的import命令之后,就可以直接使用函数,而不需要使用模块名最为前缀了。但是要注意在不同模块引用,可能导致函数冲突。 from math import sqrt sqrt(9)3.01.9.1 cmath和复数 nan-not a
4、number返回的结果Cmath即complex math复数模块 import cmath cmath.sqrt(-1)1j 返回的1j是个虚数,虚数以j结尾;这里没有使用from cmath import sqrt,避免与math 的sqrt冲突。1.10.3注释符号: #1.11字符串,使用”可以进行转义。1.11.2拼接字符串 Hello, WorldHello, World Hello, WorldHello,World Hello, +WorldHello, World Hello, +5Traceback (most recent call last): File , line
5、1, in Hello, +5TypeError: cannot concatenate str and int objects需要保证两边是一样的字符串,而有其他格式要报错的1.11.3字符串表示str和repr-两个均为函数,事实上str是一种类型Str会将值转换为合理形式的字符串。另外一种是通过repr函数,创建一个字符串。Repr(x)也可以写作x实现(注意:是反引号),python3.0中已经不适用反引号了 print hello,worldhello,world print repr(hello,world)hello,world print str(hello,world)hel
6、lo,world print 1000L1000 1000L1000L print repr(1000L)1000L print str(1000L)1000 tmp=42 print The number is:+tmpTraceback (most recent call last): File , line 1, in print The number is:+tmpTypeError: cannot concatenate str and int objects print The number is:+tmpThe number is:42 print The number is:+
7、str(tmp)The number is:42 print The number is:+repr(tmp)The number is:421.11.4 input和raw_input的比较 name=input(Whats your name:)Whats your name:GumbyTraceback (most recent call last): File , line 1, in name=input(Whats your name:) File , line 1, in NameError: name Gumby is not defined name=input(Whats
8、your name:)Whats your name:Gumby后面输入的字符串增加了引号不报错。 input(Enter a number:)Enter a number:33 name=input(Whats your name:)Whats your name:Gumby raw_input(Whats your name:)Whats your name:GumbyGumby raw_input(Whats your name:)Whats your name:GumbyGumby raw_input(Enter a number:)Enter a number:331.11.5长字符
9、串、原始字符串和unicode(1)长字符串 使用三引号;转义的反斜杠用于行尾 print hello, world!hello, world! print hello,world!hello,world! 1+2+3 +410(2)原始字符串,对于反斜线并不会特殊对待,以r开头,注意字符串尾部 print c:nowherec:owhere print r c:nowhereSyntaxError: invalid syntax print c:nowherec:owhere print rc:nowherec:nowhere print rThis is illegalSyntaxErro
10、r: EOL while scanning string literal print rThis is illegalThis is illegal print rThis is illegal This is illegal(3)Unicode在字符串前增加前缀U print uhello, worldhello, world第二章 列表和元组序列中的每个元素被分配一个序号-即元素的位置,也被称为索引。第一个索引为0,最后一个元素可以使用-1标记2.1序列概览Python包含6中内建的序列:列表,元组,字符串,unicode字符串,buffer对象和xrange对象。列表和元组的主要区别:列
11、表可以修改,元组则不能。内建函数返回元组。几乎所有情况下都可以使用列表代替元组。特殊情况之一:使用元组作为字典的键,因为键不可以更改,所以不能用列表。列表的各个元素通过逗号进行分隔,写在方括号内。 edward=Edward Gumy,42 john=John Smith,50 database=edward,john databaseEdward Gumy, 42, John Smith, 502.2通用序列操作包括:索引,分片,加,乘以及检查某个元素是否属于序列的成员,除此之外还有计算长度,找出最大元素和最小元素的内建函数。迭代:依次对序列中的每个元素重复执行某些操作。2.2.1索引从0开
12、始,最后一个元素可以使用-1.索引访问的单个元素 greeting=Hello greeting0H greeting-1o four=raw_input(Year:)3Year:2005 four52.2.2分片冒号:第一个元素包含在分片内,第二个元素不包含在分片内,是分片之后剩余部分的第一个元素编号。 num=1,2,3,4,5,6,7,8,9,10 num3:64, 5, 6 num0:11 num7:10 #索引10指向第11个元素,这个元素不存在。8, 9, 10 num-3:-18, 9 num-3:0 num-3:8, 9, 10 num7:8, 9, 10 num:31, 2,
13、 3 num: #复制整个序列1, 2, 3, 4, 5, 6, 7, 8, 9, 10 num0:10:21, 3, 5, 7, 9 num3:6:34 num:41, 5, 9 num8:3:-19, 8, 7, 6, 5 num10:0:-210, 8, 6, 4, 2 num0:10:-2 num:-210, 8, 6, 4, 2 num5:0:-26, 4, 2 num:5:-210, 8 num5:-26, 4, 22.2.3序列相加两种相同类型的序列才能进行链接操作 1,2,3+4,5,61, 2, 3, 4, 5, 6 hello, +worldhello, world hel
14、lo, +1,2Traceback (most recent call last): File , line 1, in hello, +1,2TypeError: cannot concatenate str and list objects2.2.4乘法数字X乘以一个序列会生成新的序列,原序列被重复X次 PH*3PHPHPH 42*342, 42, 42 1,2*31, 2, 1, 2, 1, 2 none*3 #注意N需要大写,不然报错。None是一个内建值,它的含义是“什么也没有” Traceback (most recent call last): File , line 1, in
15、 none*3NameError: name none is not defined None*3None, None, None2.2.5成员资格 in检查一个值是否在一个序列中。条件为真返回True,条件未假返回False pw=abc a in pwTrue x in pwFalse database=John,42,Smith,36 john,42 in database # 大小写,要注意False John,42 in databaseTrue num=1,2,3,4,5 1,2 in numFalse 1 in numFalse 1 in numTrue2.2.6 长度、最小值和
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- python 基础 学习 知识 教学 教育 资料 自学 文本 记录 方案 计划 规划

限制150内