Python 语法细节(Python 2.x 与 Python 3.x 语法差异)

Language differences and workarounds

  • 查询 Python 语言版本:

    >> import sys
    >> sys.version
    '3.5.2 |Anaconda 4.2.0 (64-bit)| (default, Jul  5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)]'
    
    >> sys.version_info
    sys.version_info(major=3, minor=5, micro=2, releaselevel='final', serial=0)
    
    
    # sys.version_info 可直接与 (3. )(tuple)做比较运算;
    
    >> sys.version_info > (3, )

0. Python 3.x 不支持 long()

>> long(1)
name 'long' is not defined

>> int(1)
>> float(1)

1. int vs float

没想到二者还各有这么多丰富的接口。Python 世界里,万物皆对象。

  • int 类型的基本成员函数

    这里写图片描述

    • bit_length():返回 int 类的对象的二进制形式所代表的位数;
  • float 类型的基本成员函数


    这里写图片描述

    >> a = 4.5
    >> (a/1.5).is_integer()
    True
原文地址:https://www.cnblogs.com/mtcnn/p/9423771.html