Python基础教程自学记录.docx
《Python基础教程自学记录.docx》由会员分享,可在线阅读,更多相关《Python基础教程自学记录.docx(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
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Python 基础教程 自学 记录