花了一天,参考W3Cschool学了下python3的基础,做了个笔记给自己看。
探索
python3下字符串、bytes、hex之间的转换
1 2 3 4 5 6 7 8 9 10 11 12 "".encode() bytes.hex(b"") +----------------+ +- ------------------------+ | | | | | | | | +-+--+ +-v-----+-+ +-----v-----+ | "" | | bytes | | hex str | +-^--+ +-+-----^-+ +-----+-----+ | | | | | | | | +----------------+ +--------------------------+ bytes.decode(b"") bytes.fromhex("")
最左侧是str对象,中间是bytes对象。他俩之间的转换通过encode()与decode()函数,可以指定utf-8,gbk,ascii等编码方式。
最右侧是hex表示的字符串,在python3中可通过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' ) 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)
1 2 3 4 5 6 7 8 9 10 > >> bytes.decode(b"\xce\xd2" , "gbk" ) '我' > >> b'\xe6\x88\x91' .decode() '我' > >> b'\xce\xd2' .decode('gbk' ) '我' > >> b'\xce\xd2' .decode() 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
1 2 3 4 5 6 7 8 > >> bytes.hex(b'hello world' ) '68656c6c6f20776f726c64' > >> b'\xe6\x88\x91' .hex() 'e68891' > >> b'hello world' .hex() '68656c6c6f20776f726c64' > >> b'\xce\xd2' .hex() 'ced2'
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模块
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']
1 2 3 4 5 6 7 8 9 10 11 12 13 import sysfor i in sys.argv: print(i) for i in sys.path: print(i) print(sys.path) from sys import argv,pathfor i in argv: print(i) for i in path: print(i) print(path)
1 2 import os os.system('ls')
数据类型
Python 中有六个标准的数据类型:Number(数字),String(字符串),List(列表),Tuple(元组),Set(集合),Dictionary(字典)
不可变数据:Number(数字),String(字符串),Tuple(元组)
可变数据:List(列表),Set(集合),Dictionary(字典)
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!' ) 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]
1 2 3 4 5 6 # 使用append()方法在列表的末尾添加新项: > >> cubes = [1, 8, 27, 64, 125] > >> cubes.append(216) > >> cubes.append(343) > >> 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 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) > >> 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 True > >> ... > >> a = set ('abracadabra' ) > >> b = set ('alacazam' ) > >> a {'a', 'b', 'c', 'd', 'r'} > >> a - b {'b', 'd', 'r'} > >> a | b {'l', 'm', 'a', 'b', 'c', 'd', 'z', 'r'} > >> a & b {'a', 'c'} > >> 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' ] 1557 > >> del tel['Rose' ] > >> tel['Mary' ] = 4127 > >> tel {'Tom': 1320, 'Jack': 1557, 'Mary': 4127} > >> list(tel.keys()) ['Tom', 'Jack', 'Mary'] > >> sorted(tel.keys()) ['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' ] 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 3 4 for <variable> in <sequence>: <statements> else : <statements>
1 2 3 4 5 6 7 8 9 10 11 12 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" )
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]
迭代器与生成器
迭代器的两个基本方法: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 sysdef 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 ) 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 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( 10 ); printinfo( 70 , 60 , 50 );
lambda [arg1 [,arg2,…..argn]]:expression
1 2 3 4 5 sum = lambda arg1, arg2: arg1 + arg2; 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)) bbb ==> ccc > >> print ('aaa' .center(10),'bbb' .center(10)) aaa bbb > >> print ('aaa' .zfill(10)) 0000000aaa
将值转换为字符串:repr(),str()
1 2 3 4 >>> repr('\x64') "'d'" >>> str('\x64') 'd'
读写文件
open() 将会返回一个 file 对象
1 2 3 4 5 open (filename, 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 a, b = 0 , 1 while b < 10 : print(b) a, b = b, a + b;
用于将结果输出到同一行,或者在输出的末尾添加不同的字符
1 2 3 4 a, b = 0 , 1 while b < 1000 : print(b,end=',' ) a, b = b, a + b;
1 2 3 hex(十进制数):将10进制数转换成16进制,并以字符串形式表示。 int(str/十进制数,base):将字符串或数字转换成整形。base为进制数,默认为十进制,指定base时第一个参数只能是字符串。