完整版Python基础教程自学记录.docx
《完整版Python基础教程自学记录.docx》由会员分享,可在线阅读,更多相关《完整版Python基础教程自学记录.docx(28页珍藏版)》请在冰豆网上搜索。
完整版Python基础教程自学记录
第一章快速改造:
基础知识
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/2
0
>>>1.0/2
0.5
>>>1.0//2.0
0.0
>>>10%3
1
>>>9**(1/2)
1
>>>9**(1.0/2)
3.0
>>>2.75%0.5
0.25
>>>-9%4
3
>>>-3%2
1
>>>-3/2
-2
1.4.1长整数
普通整数不能大于2147483647也不能小于-2147483648,若更大的数,可以使用长整数。
长整数结尾有个L,理论上小写也可以,不过为了便于识别,尽可能用大写。
1.4.2十六进制和八进制
0XAF返回175,十六进制;
010返回8,八进制
>>>0xAF
175
>>>010
8
1.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**3
8
>>>abs(-10)
10
>>>round(1.0/2.0)
1.0
>>>round(8.06,2)
8.06
>>>round(8.06,1)
8.1
1.9模块import
>>>importmath
>>>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命令之后,就可以直接使用函数,而不需要使用模块名最为前缀了。
但是要注意在不同模块引用,可能导致函数冲突。
>>>frommathimportsqrt
>>>sqrt(9)
3.0
>>>
1.9.1cmath和复数nan-notanumber返回的结果
Cmath即complexmath复数模块
>>>importcmath
>>>cmath.sqrt(-1)
1j
返回的1j是个虚数,虚数以j结尾;这里没有使用fromcmathimportsqrt,避免与math的sqrt冲突。
1.10.3注释符号:
#
1.11字符串,使用”\”可以进行转义。
1.11.2拼接字符串
>>>'Hello,''World'
'Hello,World'
>>>'Hello,''World'
'Hello,World'
>>>'Hello,'+'World'
'Hello,World'
>>>'Hello,'+5
Traceback(mostrecentcalllast):
File"",line1,in
'Hello,'+5
TypeError:
cannotconcatenate'str'and'int'objects
>>>
需要保证两边是一样的字符串,而有其他格式要报错的
1.11.3字符串表示str和repr-两个均为函数,事实上str是一种类型
Str会将值转换为合理形式的字符串。
另外一种是通过repr函数,创建一个字符串。
Repr(x)也可以写作`x`实现(注意:
`是反引号),python3.0中已经不适用反引号了
>>>print'hello,world'
hello,world
>>>printrepr('hello,world')
'hello,world'
>>>printstr('hello,world')
hello,world
>>>print1000L
1000
>>>1000L
1000L
>>>printrepr(1000L)
1000L
>>>printstr(1000L)
1000
>>>tmp=42
>>>print'Thenumberis:
'+tmp
Traceback(mostrecentcalllast):
File"",line1,in
print'Thenumberis:
'+tmp
TypeError:
cannotconcatenate'str'and'int'objects
>>>print'Thenumberis:
'+`tmp`
Thenumberis:
42
>>>print'Thenumberis:
'+str(tmp)
Thenumberis:
42
>>>print'Thenumberis:
'+repr(tmp)
Thenumberis:
42
1.11.4input和raw_input的比较
>>>name=input("What'syourname:
")
What'syourname:
Gumby
Traceback(mostrecentcalllast):
File"",line1,in
name=input("What'syourname:
")
File"",line1,in
NameError:
name'Gumby'isnotdefined
>>>name=input("What'syourname:
")
What'syourname:
'Gumby'
后面输入的字符串增加了引号不报错。
>>>input('Enteranumber:
')
Enteranumber:
3
3
>>>name=input("What'syourname:
")
What'syourname:
'Gumby'
>>>raw_input("What'syourname:
")
What'syourname:
Gumby
'Gumby'
>>>raw_input("What'syourname:
")
What'syourname:
Gumby
'Gumby'
>>>raw_input('Enteranumber:
')
Enteranumber:
3
'3'
>>>
1.11.5长字符串、原始字符串和unicode
(1)长字符串使用三引号;转义的反斜杠用于行尾
>>>print'hello,\
world!
'
hello,world!
>>>print'''hello,
world!
'''
hello,
world!
>>>1+2+3\
+4
10
(2)原始字符串,对于反斜线并不会特殊对待,以r开头,注意字符串尾部
>>>print'c:
\nowhere'
c:
owhere
>>>printr'c:
\nowhere'
SyntaxError:
invalidsyntax
>>>print'c:
\nowhere'
c:
owhere
>>>printr'c:
\nowhere'
c:
\nowhere
>>>printr"Thisisillegal\"
SyntaxError:
EOLwhilescanningstringliteral
>>>printr"Thisisillegal\\"
Thisisillegal\\
>>>printr"Thisisillegal""\\"
Thisisillegal\
(3)Unicode在字符串前增加前缀U
>>>printu'hello,world'
hello,world
第二章列表和元组
序列中的每个元素被分配一个序号--即元素的位置,也被称为索引。
第一个索引为‘0’,最后一个元素可以使用-1标记
2.1序列概览
Python包含6中内建的序列:
列表,元组,字符串,unicode字符串,buffer对象和xrange对象。
列表和元组的主要区别:
列表可以修改,元组则不能。
内建函数返回元组。
几乎所有情况下都可以使用列表代替元组。
特殊情况之一:
使用元组作为字典的键,因为键不可以更改,所以不能用列表。
列表的各个元素通过逗号进行分隔,写在方括号内。
>>>edward=['EdwardGumy',42]
>>>john=['JohnSmith',50]
>>>database=[edward,john]
>>>database
[['EdwardGumy',42],['JohnSmith',50]]
>>>
2.2通用序列操作
包括:
索引,分片,加,乘以及检查某个元素是否属于序列的成员,除此之外还有计算长度,找出最大元素和最小元素的内建函数。
迭代:
依次对序列中的每个元素重复执行某些操作。
2.2.1索引
从0开始,最后一个元素可以使用-1.索引访问的单个元素
>>>greeting="Hello"
>>>greeting[0]
'H'
>>>greeting[-1]
'o'
>>>four=raw_input('Year:
')[3]
Year:
2005
>>>four
'5'
2.2.2分片
冒号:
第一个元素包含在分片内,第二个元素不包含在分片内,是分片之后剩余部分的第一个元素编号。
>>>num=[1,2,3,4,5,6,7,8,9,10]
>>>num[3:
6]
[4,5,6]
>>>num[0:
1]
[1]
>>>num[7:
10]#索引10指向第11个元素,这个元素不存在。
[8,9,10]
>>>num[-3:
-1]
[8,9]
>>>num[-3:
0]
[]
>>>num[-3:
]
[8,9,10]
>>>num[7:
]
[8,9,10]
>>>num[:
3]
[1,2,3]
>>>num[:
]#复制整个序列
[1,2,3,4,5,6,7,8,9,10]
>>>num[0:
10:
2]
[1,3,5,7,9]
>>>num[3:
6:
3]
[4]
>>>num[:
:
4]
[1,5,9]
>>>num[8:
3:
-1]
[9,8,7,6,5]
>>>num[10:
0:
-2]
[10,8,6,4,2]
>>>num[0:
10:
-2]
[]
>>>num[:
:
-2]
[10,8,6,4,2]
>>>num[5:
0:
-2]
[6,4,2]
>>>num[:
5:
-2]
[10,8]
>>>num[5:
:
-2]
[6,4,2]
>>>
2.2.3序列相加
两