python3.x与python2.x的不同(一)

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类-元类
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值