zoukankan      html  css  js  c++  java
  • Python2和Python3的区别【多测师_王sir】

    如下为Python2和Python3的一些区别:
    1.Python2打印可以加括号也可以不加、但是Python打印输出必须要加括号
    Python2打印方式:
    name = 'hello duoceshi'   #把hello duoceshi这个字符串赋值给到name这个变量
    print name
    
    Python3打印方式:
    name = 'hello duoceshi'   #把hello duoceshi这个字符串赋值给到name这个变量
    print (name)
    
    

    2.Python3.X源码文件默认使用utf-8编码,而python2.x的编译最前端需要加上#coding=utf-8
    3.在python2.x里是raw_input和input函数 ,而在python3.x里面2个函数的功能合并为input函数了。
    4.在python2.x当中传统除法只取整数,python3.x传统除法会显示小数点后小数位
    5.python2中sort函数可以将整型和字符串放在一起排序、python3里面只能将整型和整型、字符串和字符串
    分开进行排序
    6.python2列表中有中文直接打印会显示被转义了需要通过str(list1).decode('string_escape')进行转码
    但是在python3里面列表中有中文可以直接打印出来
    7.在python2里面字典的键不可以为整型、python3里面是可以的
    8.在python2里面取字典的键通过print dict1.keys()、python3里面print(dict1.keys())
    运行的结果为:dict_keys(['name', 'age'])、必须要通过for循环来取
    for i in dict1.keys():
    print(i)
    9.python2字典中的has_key函数判断键是否存在字典里面、在python3当中用__contains__函数给替代了
    语法为:print(dict1.__contains__('name'))或者用if 'name' in dict1:
    10.python2中的三目运算是name = input('请输入您的用户名:') print '在上海' if name == 'admin' else '在深圳'
    python3中是:name = input('请输入您的用户名:') print ('在上海') if name == 'admin' else print('在深圳')
    11.python2中执行print range(10)返回的是列表数据如:[0,1,2,3,4,5,6,7,8,9]
    python3中执行print(range(10))返回的数据是:range(0, 10)
    12.python2里面print 100/2打印结果为50数据类型是int整型
    python3里面print(100/2)打印结果为50.0数据类型是float浮点型
    13.python2里面zip函数
    list1 = ['name','class','age','score']
    list2 = ['duoceshi','dcs6',18]
    a = zip(list1,list2)
    print a #a的结果返回的是一个列表
    但是python3里面a返回的是一个对象、需要通过list(a)转换为列表进行输出
  • 相关阅读:
    my first android test
    VVVVVVVVVV
    my first android test
    my first android test
    my first android test
    ini文件
    ZZZZ
    Standard Exception Classes in Python 1.5
    Python Module of the Week Python Module of the Week
    my first android test
  • 原文地址:https://www.cnblogs.com/xiaoshubass/p/13571324.html
Copyright © 2011-2022 走看看