收藏 分享(赏)

Python基础教程自学记录.docx

上传人:幼儿教育老师 文档编号:21758970 上传时间:2024-04-22 格式:DOCX 页数:38 大小:397.29KB
下载 相关 举报
Python基础教程自学记录.docx_第1页
第1页 / 共38页
Python基础教程自学记录.docx_第2页
第2页 / 共38页
Python基础教程自学记录.docx_第3页
第3页 / 共38页
Python基础教程自学记录.docx_第4页
第4页 / 共38页
Python基础教程自学记录.docx_第5页
第5页 / 共38页
亲,该文档总共38页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

1、第一章快速改造:基础知识1.2 交互式解释器在 IDLE 编辑器,在提示符后输入help 然后按回车;也可以按下F1 获得有关 IDLE 的帮助信息1.4 数字和表达式1/2 返回 0,整除除法; 1.0/2 返回 0.5,用一个或者多个包含小数点的数字参与计算。另外改变除法的执行方式:from_future_importdivision/可以实现整除, 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 长整数普通

2、整数不能大于2147483647 也不能小于 -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

3、)10 round(1.0/2.0)1.0 round(8.06,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 sqr

4、t sqrt(9)3.01.9.1 cmath 和复数nan-not a number返回的结果Cmath 即 complex math 复数模块 import cmath cmath.sqrt(-1)1j返回的 1j 是个虚数,虚数以 j 结尾;这里没有使用from cmath importsqrt,避免与 math 的 sqrt 冲突。1.10.3 注释符号:#1.11 字符串,使用 ”可以进行转义。1.11.2拼接字符串 Hello, World Hello, World Hello, World Hello,World Hello, +World Hello, World Hello,

5、 +5Traceback (most recent call last):File , line 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,world hello,worl

6、d print repr(hello,world) hello,world print str(hello,world) hello,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 objec

7、ts print The number is:+tmp The number is:42 print The number is:+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:)Fi

8、le , line 1, in NameError: name Gumby is not defined name=input(Whats 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:Gumby Gumby raw_input(Whats your name:) What

9、s your name:GumbyGumby raw_input(Enter a number:) Enter a number:331.11.5长字符串、原始字符串和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:nowhere SyntaxError: invalid syntax pri

10、nt c:nowherec:owhere print rc:nowhere c:nowhere print rThis is illegalSyntaxError: EOL while scanning string literal print rThis is illegal This is illegal print rThis is illegal This is illegal(3)Unicode 在字符串前增加前缀U print uhello, world hello, world第二章列表和元组序列中的每个元素被分配一个序号-即元素的位置,也被称为索引。第一个索引为 0,最后一个元

11、素可以使用-1 标记2.1 序列概览Python 包含 6 中内建的序列:列表,元组,字符串, unicode 字符串, buffer 对象和 xrange 对象。列表和元组的主要区别:列表可以修改,元组则不能。内建函数返回元组。几乎所有情况下都可以使用列表代替元组。特殊情况之一:使用元组作为字典的键,因为键不可以更改,所以不能用列表。列表的各个元素通过逗号进行分隔,写在方括号内。 edward=Edward Gumy,42 john=John Smith,50 database=edward,john databaseEdward Gumy, 42, John Smith, 502.2 通用

12、序列操作包括:索引,分片,加,乘以及检查某个元素是否属于序列的成员,除此之外还有计算长度,找出最大元素和最小元素的内建函数。迭代:依次对序列中的每个元素重复执行某些操作。2.2.1 索引从 0 开始,最后一个元素可以使用-1.索引访问的单个元素 greeting=Hello greeting0H greeting-1o four=raw_input(Year:)3 Year:2005 four52.2.2 分片冒号:第一个元素包含在分片内,第二个元素不包含在分片内,是分片之后剩余部分的第一个元素编号。 num=1,2,3,4,5,6,7,8,9,10 num3:64, 5, 6 num0:11

13、 num7:10 # 索引 10 指向第 11 个元素,这个元素不存在。8, 9, 10 num-3:-1 8, 9 num-3:0 num-3:8, 9, 10 num7:8, 9, 10 num:31, 2, 3 num: # 复制整个序列1, 2, 3, 4, 5, 6, 7, 8, 9, 10 num0:10:21, 3, 5, 7, 9 num3:6:34 num:4 1, 5, 9 num8:3:-19, 8, 7, 6, 5 num10:0:-2 10, 8, 6, 4, 2 num0:10:-2 num:-2 10, 8, 6, 4, 2 num5:0:-2 6, 4, 2 n

14、um:5:-2 10, 8 num5:-2 6, 4, 22.2.3 序列相加两种相同类型的序列才能进行链接操作 1,2,3+4,5,6 1, 2, 3, 4, 5, 6 hello, +world hello, world hello, +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*3 42, 42, 42

15、 1,2*31, 2, 1, 2, 1, 2 none*3 # 注意 N 需要大写,不然报错。 None 是一个内建值,它的含义是“什么也没有”Traceback (most recent call last):File , line 1, in none*3NameError: name none is not defined None*3None, None, None2.2.5 成员资格in检查一个值是否在一个序列中。条件为真返回True,条件未假返回False pw=abc a in pw True x in pw False database=John,42,Smith,36 joh

16、n,42 in database # 大小写,要注意 John,42 in database True num=1,2,3,4,5 1,2 in num False 1 in num False 1 in numTrue2.2.6 长度、最小值和最大值内建函数 len、min 和 max num=1,8,3 len(num)3 max(num)8 min(num)1 max(2,3)3max 跟 min 的参数并不一定是序列, 而是以多个数字直接作为参数。 exm=h,12,e,2 max(exm)h exm=12,e,2,h max(exm)h max(A,1,1,a,z)z这个有点意思了,

17、需要以后注意查查,是根据ascii 进行提取的吗?2.3 列表: Python 的“苦力”讨论列表不同于元组跟字符串的地方2.3.1 list 函数 ls=list(Hello) lsH, e, l, l, o .join(ls)Hello2.3.2 基本的列表操作列表可以使用所有适用于序列的操作。而列表是可以修正的。本节介绍可以改变列表的方法:元素赋值、元素删除、分片赋值以及列表方法(请注意,并非所有的列表方法都真正地改变列表)1、改变列表:元素赋值 x=1,1,1 x1=2 x1, 2, 1注意:不能为一个位置不存在的元素进行赋值。2、删除元素del num=1,2,3,4 del num

18、2 num1, 2, 4除了删除列表中的元素, del 还能用于删除其他元素。 可以用于字典元素甚至其他变量的删除操作。3、分片赋值 nm=list(perl) nmp, e, r, l nm2:=list(ar) nmp, e, a, r nm2:=list(ckly) # 可以改变成长度 nmp, e, c, k, l, y nm=1,5 nm1:1=2,3,4 # 插入行的元素 nm1, 2, 3, 4, 5 nm1:4= # 删除一段元素,与 del 的结果一样 nm1, 5 nm=1,2,3,4,5 del nm1:4 nm1, 5可以根据实际步长进行其他操作, 测试的时候貌似要注意

19、对应的位置元素个数。 num=1,2,3,4,5 num1:4:2=8,10 num1, 8, 3, 10, 52.3.3 列表方法方法的调用方式:对象 .方法(参数)1. append 用于在列表末尾追加新的对象,直接修改原来的列表。 lst=1,2,3 lst.append(4) lst1, 2, 3, 4 lst=1,2,3 lst.append(4,5)Traceback (most recent call last):File , line 1, in lst.append(4,5)TypeError: append() takes exactly one argument (2 g

20、iven) lst.append(4,5) lst1, 2, 3, 4, 52. count 方法,统计某个元素在列表中出现的次数: to,be,or,not,to,be.count(to)2 x=1,2,1,1,2,1,1,2 x.count(1,2)13. extendextend方法可以在列表的末尾一次性追加另一个序列中的多个值。换句话说,可以用新列表扩展原有的列表 a=1,2,3 b=4,5,6 a.extend(b) #extend扩展了原来的序列,即 a a1, 2, 3, 4, 5, 6 a+b #链接操作,仅仅返回一个全新的列表1, 2, 3, 4, 5, 6, 4, 5, 6

21、 a1, 2, 3, 4, 5, 6 alen(a):=b # 使用分片来实现相同的结果,但是代码的可读性不如 extend。 a1, 2, 3, 4, 5, 6, 4, 5, 6 a=a+b #此链接方法的效率要比 extend 方法低 a1, 2, 3, 4, 5, 6, 4, 5, 6, 4, 5, 64. indexindex 方法用于从列表中找出某个值第一个匹配项的索引位置。 phase=We,are,hero,! phase.index(hero)2 phase.index(ero)Traceback (most recent call last):File , line 1, i

22、n phase.index(ero)ValueError: ero is not in list5. insert用于将对象插入列表中 num=1,2,3,4,5,6,7 num.insert(3,four) num1, 2, 3, four, 4, 5, 6, 7 num=1,2,3,4,5,6,7 num3:3=four # 意外发现 num1, 2, 3, f, o, u, r, 4, 5, 6, 7 num=1,2,3,4,5,6,7 num3:3=four # 可以分片处理,依然是可读性不如insert num1, 2, 3, four, 4, 5, 6, 76. poppop 方法

23、会移出列表中的一个元素(默认是最后一个) ,并且返回该元素的值。pop 方法是唯一一个既能修改列表又返回元素值(除了None)的列表方法。使用 pop 方法可以实现一种数据结构-栈。对于栈的两个操作(放入和移出),pop 跟 append方法恰好相反。 Python 没有入栈方法,变通使用 append方法。提示:对于先进先出的队列,可以使用insert(0,)来代替 append方法。或者,也可以继续使用append 方法,但必须使用pop(0)来代替 pop().更好的解决方案是使用 collection 模块中的 deque 对象。 x=1,2,3 x.pop()3 x1, 2 x.po

24、p(0)1 x2 x=1,2,3 x.append(x.pop() x1, 2, 37. removeremove 方法用于移出列表中某个值的第一个匹配项: x=to,be,or,not,to,be x.remove(be) xto, or, not, to, be x.remove(bee)Traceback (most recent call last):File , line 1, in x.remove(bee)ValueError: list.remove(x): x not in list注意: remove 是一个没有返回值的原位置改变方法。8. reversereverse方法

25、将列表中的元素反方向存放: x=1,2,3 x.reverse() x3, 2, 19. sortsort 方法用于在原位置对列表进行排序。在“原位置排序”意味着要改变原来的列表, 从而让其中的元素能按一定的顺序排列,而不是返回一个已经排序的列表副本。 x=4,6,2,1,7,9 x.sort() x1, 2, 4, 6, 7, 9* x=4,6,2,1,7,9 y=x.sort() # 因为 sort 方法修改了 x 缺返回了空值 print yNone x1, 2, 4, 6, 7, 9* x=4,6,2,1,7,9 y=x: # 有效的复制整个列表的方法 y.sort() x4, 6,

26、2, 1, 7, 9 y1, 2, 4, 6, 7, 9* x=4,6,2,1,7,9 y=x#简单的赋值是没有用的,仅仅让 x 跟 y 都指向同一个列表。 y.sort() x1, 2, 4, 6, 7, 9 y1, 2, 4, 6, 7, 9另外一种获取已排序的列表副本的方法是,使用sorted函数 x=4,6,2,1,7,9 y=sorted(x) y1, 2, 4, 6, 7, 9 x4, 6, 2, 1, 7, 9 sorted(Python) #sorted可以用于任何序列,却总是返回一个列表。 P, h, n, o, t, y如果要把一些元素按照相反的顺序排列,可以先使用sort

27、 或者sorted, 然 后 再 调 用reserse 方 法 , 或 者 使用reverse 参 数 。Sorted(x).reverse()这样可以。10.高级排序如果希望元素按照特定的方式进行排序,可以通过compare(x,y)的方式自定义比较函数。Compare(x,y),xy 返回正数 ;x cmp(42,32)1 cmp(99,100)-1 cmp(10,10)0 num=5,2,9,7 num.sort(cmp) num2, 5, 7, 9 cmp(42,32)1 num=5,2,9,7 num.sort(cmp) num2, 5, 7, 9Sort 方法还有另外两个参数-ke

28、y 和 reverse。如果要使用它们, 那么就要通过名字来指定。参数key 与 cmp 类似 -必须提供一个在排序过程中使用的函数。 然而该函数并不是直接用来确定对象大小, 而是为每个元素创建一个键, 然后所有元素来排序。 那么如果根据元素的长度进行排序,那么使用 len 作为键函数: x=3aaa,1a,4aaaa,0 x.sort(key=len) x0, 1a, 3aaa, 4aaaa另外一个关键字参数reverse 是简单的布尔值 (True 或者 false),用来知名列表是否进行反向排序。 num=5,2,9,7 num.sort() num2, 5, 7, 9 num.sort(reverse=True) num9, 7, 5, 2cm

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 高中资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:文库网官方知乎号:文库网

经营许可证编号: 粤ICP备2021046453号世界地图

文库网官网©版权所有2025营业执照举报