Python3为Python2的下一版本,但是它打破了与2.X的向后兼容性。
python3.X对语法的修改有下面几个方面。
1)/成为真正除法,int/int=float;
2)long与int统一,而且删除了后缀L;
3)True、False、None现在都是关键字;
4)print()括号不能掉
先简单介绍一下open()
fid=open("log.txt","a")#打开文件log.txt,model为"a";
'r' open for reading (default)
'w' open for writing, truncating the file first
'x' create a new file and open it for writing
'a' open for writing, appending to the end of the file if it exists
'b' binary mode
't' text mode (default)
'+' open a disk file for updating (reading and writing)
'U' universal newline mode (deprecated)
现在开始进入正题
print([object, ...][, sep=' '][, end='endline_character_here'][, file=redirect_to_here])
print>>fid,"log.txt"变成print("log.txt",file=fid)
print>>sys.stderr,"an error occurred"变成
5)raw_input()与input()变成python3的input();
6)用数据类型bytes literal 及 bytes对象存储二进制
bytes([initializer[, encoding]])
b = (b'\xc3\x9f\x65\x74\x61')
b = bytes('\xc3\x9f\x65\x74\x61', 'iso-8859-1')
7)具有单一的字符串类型str,其功能类似于版本2.x的unic类型
repr('é')
8)format
(1)>>>"I love {0}, {1}, and {2}".format("eggs", "bacon", "sausage")
'I love eggs, bacon, and sausage'
(2)>>>"I love {a}, {b}, and {c}".format(a="eggs", b="bacon", c="sausage")
'I love eggs, bacon, and sausage'
(3)>>>"I love {0}, {1}, and {param}".format("eggs", "bacon", param="sausage")
'I love eggs, bacon, and sausage'
(4)>>>print(format(10.0, "7.3g"))
10
9)3.0 内的另一个重大改变是字典内 dict.iterkeys()、 dict.itervalues() 和
dict.iteritems() 方法的删除。取而代之的是 .keys()、 .values() 和 .items(),
它们被进行了修补,可以返回轻量的、类似于集的容器对象,而不是键和值的列表。
d={1:"dead",2:"parrot"}
print(d.items())
#dict_items([(1, 'dead'), (2, 'parrot')])
print(1 in d,"dead" in d,2 in d,"parrot" in d)
#True False True False
print(list(d))
#[1, 2]
for key,values in d.items():
print(key)
print(values)
#1
#dead
#2
#parrot
10)ABC类-元类