zoukankan      html  css  js  c++  java
  • 【320】Python 2.x 与 3.x 的区别

    通过代码移植的报错进行梳理!

    1. print 函数的区别

      Python 2.x 中可以加空格或者括号,但是 Python 3.x 只能是括号的

    # Python 2.x
    >>> print "processing..."
    processing...
    >>> print("processing...")
    processing...
    
    # Python 3.x
    >>> print("processing...")
    processing...
    

    2. raw_input 与 input 函数

      Python 2.x 中 raw_input 与 Python 3.x 中 input 函数类似,而 Python 2.x 中的 input 函数接收数字或者带引号字符串

    # Python 2.x
    >>> a = raw_input("Value: ")
    Value: Alex
    >>> a
    'Alex'
    >>> b = input("Value: ")
    Value: "Alex"
    >>> b
    'Alex'
    
    # Python 3.x
    >>> a = input("Value: ")
    Value: Alex
    >>> a
    'Alex'
    

    参考:python2.x和python3.x中raw_input( )和input( )区别

    3. Tkinter 模块

      Python 2.x 为 Tkinter,但是 Python 3.x 为 tkinter(小写)

    # Python 2.x
    >>> import Tkinter
    
    # Python 3.x
    >>> import tkinter
    

    参考:Python GUI编程(Tkinter)

    4. 除法运算符

      Python 2.x 中 /、// 均为整除,但是 Python 3.x 中 / 表示除以,// 表示整除

    # Python 2.x
    >>> 77/60
    1
    >>> 77//60
    1
    
    # Python 3.x
    >>> 77/60
    1.2833333333333334
    >>> int(77/60)
    1
    >>> 77//60
    1
    

    5. cmd 默认 Python 版本

      需要通过修改环境变量来切换 Python 2.x 和 Python 3.x

    参考:如何快速切换Python运行版本,如何选择Python版本

  • 相关阅读:
    LibreOJ2302
    POJ3693
    BZOJ3697
    BZOJ2599
    BZOJ2152
    BZOJ1468
    BZOJ4653
    BZOJ4553
    BZOJ4552
    BZOJ4551
  • 原文地址:https://www.cnblogs.com/alex-bn-lee/p/9206979.html
Copyright © 2011-2022 走看看