python3 学习

花了一天,参考W3Cschool学了下python3的基础,做了个笔记给自己看。

探索

python3下字符串、bytes、hex之间的转换

1
2
3
4
5
6
7
8
9
10
11
12
      "".encode()                 b"".hex()
+----------------+ +- ------------------------+
| | | |
| | | |
+-+--+ +-v-----+-+ +-----v-----+
| "" | | bytes | | hex str |
+-^--+ +-+-----^-+ +-----+-----+
| | | |
| | | |
+----------------+ +--------------------------+
b"".decode() bytes.fromhex("")

最左侧是str对象,中间是bytes对象。他俩之间的转换通过encode()与decode()函数,可以指定utf-8,gbk,ascii等编码方式。

最右侧是hex表示的字符串,在python3中可通过bytes对象进行互相转换。

转换示例如下:

  • str - bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
>>> "hello world".encode()
b'hello world'
>>> "hello world".encode('gbk')
b'hello world'
>>> "hello world".encode('ascii') # 英文字符的编码就是它自己
b'hello world'
>>> '我'.encode()
b'\xe6\x88\x91'
>>> '我'.encode('utf-8') # str.encode()的默认编码方式是utf-8
b'\xe6\x88\x91'
>>> '我'.encode('gbk')
b'\xce\xd2'
>>> '我'.encode('ascii')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character '\u6211' in position 0: ordinal not in range(128)
  • bytes - str
1
2
3
4
5
6
7
8
>>> b'\xe6\x88\x91'.decode()		# bytes.decode()的默认解码方式是utf-8
'我'
>>> b'\xce\xd2'.decode('gbk')
'我'
>>> b'\xce\xd2'.decode() # 以gbk编码的字节流,若未指定解码方式,默认以utf-8解码时会出错
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xce in position 0: invalid continuation byte
  • bytes - hex
1
2
3
4
5
6
>>> b'\xe6\x88\x91'.hex()
'e68891'
>>> b'hello world'.hex()
'68656c6c6f20776f726c64'
>>> b'\xce\xd2'.hex()
'ced2'
  • hex - bytes
1
2
3
4
5
6
>>> bytes.fromhex('e68891')
b'\xe6\x88\x91'
>>> bytes.fromhex('68656c6c6f20776f726c64')
b'hello world'
>>> bytes.fromhex('ced2')
b'\xce\xd2'

python3中其他方式:

1
2
3
4
5
6
7
import codecs
>>> codecs.encode(b'aaa','hex')
b'616161'
>>> codecs.decode(b'\x61\x61\x61')
'aaa'
>>> codecs.decode(b'\x61\x61\x61','utf-8')
'aaa'

python2中:

1
2
3
4
5
6
>>> '\x61\x61\x63'.decode()
u'aac'
>>> '616263'.decode('hex')
'abc'
>>> 'abc'.encode('hex')
'616263'

python3模块

  • keyword
1
2
3
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', '__peg_parser__', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
  • sys
1
2
3
4
5
6
7
8
9
10
11
12
13
import sys
for i in sys.argv:
print(i)
for i in sys.path:
print(i)
print(sys.path)

from sys import argv,path
for i in argv:
print(i)
for i in path:
print(i)
print(path)
  • os
1
2
import os
os.system('ls')

数据类型

Python 中有六个标准的数据类型:Number(数字),String(字符串),List(列表),Tuple(元组),Set(集合),Dictionary(字典)

不可变数据:Number(数字),String(字符串),Tuple(元组)

可变数据:List(列表),Set(集合),Dictionary(字典)

1
可通过type(xxx)查看数据类型

Number

python3支持int,float,bool,complex

1
2
3
4
5
6
7
8
9
10
11
12
13
14
>>> 5 + 4  # 加法
9
>>> 4.3 - 2 # 减法
2.3
>>> 3 * 7 # 乘法
21
>>> 2 / 4 # 除法,得到一个浮点数
0.5
>>> 2 // 4 # 除法,得到一个整数
0
>>> 17 % 3 # 取余
2
>>> 2 ** 5 # 乘方
32

String

用双引号或单引号括起来,反斜杠'\'作为转义字符和续行符

1
2
3
4
5
>>> print('Hello\nwrold!')
Hello
world!
>>> print(r'Hello\nwrold!') # 使用r让反斜杠不转义
Hello\nwrold!
  • 字符串拼接:+*
1
2
>>> print('str'+'ing', 'my'*3)
string mymymy
  • 字符串索引:[]
1
2
3
4
5
>>> word = 'Python'
>>> print(word[0], word[5])
P n
>>> print(word[-1], word[-6])
n P
  • 字符串切片:[:]
1
2
3
4
5
6
7
8
9
>>> word = 'ilovepython'
>>> word[1:5]
'love'
>>> word[:]
'ilovepython'
>>> word[5:]
'python'
>>> word[-10:-6]
'love'

List

使用方括号,逗号组成的元素列表,元素类型可不相同

1
2
3
>>> a = ['him', 25, 100, 'her']
>>> print(a)
['him', 25, 100, 'her']
  • 列表串联拼接:+
1
2
3
>>> a = [1, 2, 3, 4, 5]
>>> a + [6, 7, 8]
[1, 2, 3, 4, 5, 6, 7, 8]
  • 列表索引:[]
1
2
3
4
5
6
7
8
>>> a = ['him', 25, 100, 'her']
>>> a[0]
'him'
>>> a[0] = 9 #列表的元素可以更改
>>> print(a)
[9, 25, 100, 'her']
>>> letters[:] = [] #清除列表
>>> letters
  • 列表切片:[:]
1
2
3
4
5
6
7
>>> a = [1, 2, 3, 4, 5,6]
>>> a[2:5] = [13, 14, 15] # 替换一些值
>>> a
[9, 2, 13, 14, 15, 6]
>>> a[2:5] = [] # 删除
>>> a
[9, 2, 6]
  • append() pop()等方法
1
2
3
4
5
6
# 使用append()方法在列表的末尾添加新项:
>>> cubes = [1, 8, 27, 64, 125]
>>> cubes.append(216) # cube列表中添加新值
>>> cubes.append(343) # cube列表中添加第七个值
>>> cubes
[1, 8, 27, 64, 125, 216, 343]

Tuple

使用方括号,逗号组成的元素元组,元素类型可不相同

1
2
3
4
5
>>> a = (1991, 2014, 'physics', 'math')
>>> print(a, type(a), len(a))
(1991, 2014, 'physics', 'math') <class 'tuple'> 4
>>> tup1 = () # 空元组
>>> tup2 = (20,) # 包含一个元素的元组,注意后面有逗号
  • 元组拼接:+
1
2
3
>>> tup1, tup2 = (1, 2, 3), (4, 5, 6)
>>> print(tup1+tup2)
(1, 2, 3, 4, 5, 6)
  • 元组索引:[]
1
2
3
>>> tup1 = (1,2,3)
>>> print(tup1[0])
1
  • 元组切片:[:]
1
2
3
>>> tup1 = (1,2,3)
>>> print(tup1[0:1])
(1,)
  • 删除元组
1
>>> del tup1
  • 元组的内置函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
>>> tuple1 = ('Google', 'W3CSchool', 'Taobao')
>>> len(tuple1) # 计算元组的元素个数
3

>>> tuple2 = ('5', '4', '8')
>>> max(tuple2) # 返回元组中元素的最大值
'8'

>>> tuple2 = ('5', '4', '8')
>>> min(tuple2) # 返回元组中元素的最小值
'4'

>>> list1= ['Google', 'Taobao', 'W3CSchool', 'Baidu']
>>> tuple1=tuple(list1) # 将list列表转换为元组
>>> tuple1
('Google', 'Taobao', 'W3CSchool', 'Baidu')

>>> import operator
>>> dict1 = (1, 2, 3)
>>> dict2 = ('a', 'b', 'c')
>>> operator.eq(dict1, dict2) # 比较两个元组的元素
False

Set

一个无序不重复元素的集合,使用大括号{}或set()函数创建

tips:set()创建空集合,{}创建空字典

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
>>> student = {'Tom', 'Jim', 'Mary', 'Tom', 'Jack', 'Rose'}
>>> print(student) # 重复的元素被自动去掉
{'Jim', 'Jack', 'Mary', 'Tom', 'Rose'}
>>> 'Rose' in student # membership testing(成员测试)
True
>>> # set可以进行集合运算
...
>>> a = set('abracadabra')
>>> b = set('alacazam')
>>> a
{'a', 'b', 'c', 'd', 'r'}
>>> a - b # a和b的差集
{'b', 'd', 'r'}
>>> a | b # a和b的并集
{'l', 'm', 'a', 'b', 'c', 'd', 'z', 'r'}
>>> a & b # a和b的交集
{'a', 'c'}
>>> a ^ b # a和b中不同时存在的元素
{'l', 'm', 'b', 'd', 'z', 'r'}

Dictionary

一种映射类型(mapping type),在大括号{}中包含一些无序的键值对,关键字必须使用不可变类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
>>> tel = {} 		 		# 创建空字典
>>> tel = {'Jack':1557, 'Tom':1320, 'Rose':1886}
>>> tel
{'Tom': 1320, 'Jack': 1557, 'Rose': 1886}
>>> tel['Jack'] # 通过key查询
1557
>>> del tel['Rose'] # 删除一个键值对
>>> tel['Mary'] = 4127 # 添加一个键值对
>>> tel
{'Tom': 1320, 'Jack': 1557, 'Mary': 4127}
>>> list(tel.keys()) # 返回所有key组成的list
['Tom', 'Jack', 'Mary']
>>> sorted(tel.keys()) # 按key排序
['Jack', 'Mary', 'Tom']
>>> 'Tom' in tel # 成员测试
True
>>> 'Mary' not in tel # 成员测试
False

使用dict()构造函数从sequence中构造字典,也可进行推导。(string、list 和 tuple 都属于 sequence(序列))

1
2
3
4
5
6
7
8
>>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
{'jack': 4098, 'sape': 4139, 'guido': 4127}

>>> {x: x**2 for x in (2, 4, 6)}
{2: 4, 4: 16, 6: 36}

>>> dict(sape=4139, guido=4127, jack=4098)
{'jack': 4098, 'sape': 4139, 'guido': 4127}
  • 删除字典元素
1
2
3
4
5
dict = {'Name': 'W3CSchool', 'Age': 7, 'Class': 'First'}

del dict['Name'] # 删除键 'Name'
dict.clear() # 删除字典
del dict # 删除字典

字典类型有一些内置函数,例如 clear()、keys()、values() 等。

语法及API

条件控制

1
2
3
4
5
6
if condition_1:
statement_block_1
elif condition_2:
statement_block_2
else:
statement_block_3

循环

1
2
while 判断条件:
statements
1
2
3
4
for <variable> in <sequence>:
<statements>
else:
<statements>
1
2
3
4
5
6
7
8
9
10
11
12
# 使用break跳出当前循环体
edibles = ["ham", "spam","eggs","nuts"]

for food in edibles:
if food == "spam":
print("No more spam please!")
break
print("Great, delicious " + food)
else:
print("I am so glad: No spam!")

print("Finally, I finished stuffing myself")
  • range()函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
>>> for i in range(5):
... print(i)
...
0
1
2
3
4

>>> for i in range(5,9) :
print(i)
5
6
7
8

>>> for i in range(0, 10, 3) :
print(i)
0
3
6
9

>>> for i in range(-10, -100, -30) :
print(i)
-10
-40
-70

>>> list(range(5))
[0, 1, 2, 3, 4]
  • continue和pass

迭代器与生成器

迭代器的两个基本方法:iter()和next()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 字符串,列表或元组对象都可用于创建迭代器
>>> list=[1,2,3,4]
>>> it = iter(list) # 创建迭代器对象
>>> print(next(it)) # 输出迭代器的下一个元素
1
>>> print(next(it))
2

# 使用for语句遍历迭代器对象
list=[1,2,3,4]
it = iter(list) # 创建迭代器对象
for x in it:
print(x, end=" ")
# 执行结果为:1 2 3 4

# 使用next函数
import sys # 引入 sys 模块
list=[1,2,3,4]
it = iter(list) # 创建迭代器对象
while True:
try:
print(next(it))
except StopIteration:
sys.exit()
#执行完的输出
1
2
3
4

生成器是使用了yield的函数

在调用生成器运行的过程中,每次遇到 yield 时函数会暂停并保存当前所有的运行信息,返回 yield 的值。并在下一次执行 next() 方法时从当前位置继续运行。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import sys

def fibonacci(n): # 生成器函数 - 斐波那契
a, b, counter = 0, 1, 0
while True:
if (counter > n):
return
yield a
a, b = b, a + b
counter += 1
f = fibonacci(10) # f 是一个迭代器,由生成器返回生成

while True:
try:
print (next(f), end=" ")
except StopIteration:
sys.exit()

输出:

1
0 1 1 2 3 5 8 13 21 34 55

函数

1
2
def  函数名(参数列表):
函数体
  • 不定长参数:*
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
def printinfo( arg1, *vartuple ):
"打印任何传入的参数"
print "输出: "
print arg1
for var in vartuple:
print var
return;

# 调用printinfo 函数
printinfo( 10 );
printinfo( 70, 60, 50 );

# 输出:
# 10
# 输出:
# 70
# 60
# 50
  • 匿名函数:lambda

lambda [arg1 [,arg2,…..argn]]:expression

1
2
3
4
5
sum = lambda arg1, arg2: arg1 + arg2;

#调用sum函数
print "Value of total : ", sum( 10, 20 )
print "Value of total : ", sum( 20, 20 )

输入和输出

输入:input(),read()

输出:print(),write(),str.format(),sys.stdout()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
>>> for x in range(1, 11):
... print('{0:2d} {1:3d} {2:4d}'.format(x, x*x, x*x*x))
...
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000

>>> print('bbb'.ljust(10),'==>','ccc'.rjust(10)) # str.rjust()与str.ljust()
bbb ==> ccc

>>> print('aaa'.center(10),'bbb'.center(10)) # str.center()
aaa bbb

>>> print('aaa'.zfill(10)) # st=r.zfill()
0000000aaa

将值转换为字符串:repr(),str()

1
2
3
4
>>> repr('\x64')
"'d'"
>>> str('\x64')
'd'

读写文件

open() 将会返回一个 file 对象

1
2
3
4
5
open(filename, mode)					# 若不提供mode的话,默认以只读方式打开
>>> f = open('/tmp/workfile', 'w') # 以只写的方式打开文件
>>> f = open('/tmp/workfile','r') # 以只读的方式打开文件
>>> f = open('/tmp/workfile','a') # 以追加的方式打开文件
>>> f = open('/tmp/workfile','r+') # 以读写的方式打开文件

f.read(n):读取文件内容,以字符串或字节对象返回。n指定读取大小,若n为0或负数则返回文件所有内容

f.readline():读取文件中的一行’\n’,如果返回空字符串则说明已经读取到最后一行

1
2
3
4
5
6
>>> f.readline()	
'This is the first line of the file.\n'
>>> f.readline()
'Second line of the file\n'
>>> f.readline()
''

f.readlines() :返回文件包含的所有行,返回的是一个list。

1
2
3
4
>>> f.readlines()
['line1 line11\n', 'line2 line22\n', 'line3 line33\n']
>>> f.readlines(20)
['line1 line11\n', 'line2 line22\n']

f.write():f.write(string) 将 string 写入到文件中, 然后返回写入的字符数

1
2
3
4
5
6
7
>>> f.write('This is a test\n')
15

>>> value = ('the answer', 42) # 若写入的不是字符串,则需要先进行转换
>>> s = str(value)
>>> f.write(s)
18

f.tell():返回文件对象当前所处的位置,是从文件开头算起的字节数。

f.seek():改变文件当前位置。 f.seek(offset, from_what)

1
2
3
4
5
from_what 的值, 如果是 0 表示开头, 如果是 1 表示当前位置, 2 表示文件的结尾,例如:

seek(x, 0) : 从起始位置即文件首行首字符开始移动 x 个字符
seek(x, 1) : 表示从当前位置往后移动x个字符
seek(-x, 2):表示从文件的结尾往前移动x个字符

f.close():关闭文件。

标准库概览

W3Cschool-标准库概览

tips

  • 复合赋值
1
2
3
4
5
6
# Fibonacci series: 斐波纳契数列
# 两个元素的总和确定了下一个数
a, b = 0, 1
while b < 10:
print(b)
a, b = b, a + b;
  • end关键字

用于将结果输出到同一行,或者在输出的末尾添加不同的字符

1
2
3
4
a, b = 0, 1
while b < 1000:
print(b,end=',')
a, b = b, a + b;
  • int与hex
1
2
3
hex(十进制数):将10进制数转换成16进制,并以字符串形式表示。

int(str/十进制数,base):将字符串或数字转换成整形。base为进制数,默认为十进制,指定base时第一个参数只能是字符串。