# python数据类型——数字

## 整型 Integers

``2 + 2``
``````4

``````
``3 - 4``
``````-1

``````
``4 * 5``
``````20

``````

Python 2.7中，整型的运算结果只能返回整型，除法的结果也不例外。

``12 / 5``
``````2

``````

``2 ** 5``
``````32

``````

``32 % 5``
``````2

``````

``````a = 1
a``````
``````1

``````

``type(a)``
``````int

``````

``````import sys
sys.maxint``````
``````2147483647
``````

## 长整型 Long Integers

``````a = sys.maxint + 1
print type(a)``````
``````<type 'long'>

``````

``a``
``````2147483648L

``````

``````b = 1234L
type(b)``````
``````long

``````

``a - 4``
``````2147483644L

``````

## 浮点数 Floating Point Numbers

``````a = 1.4
type(a)``````
``````float

``````

``12.0 / 5.0``
``````2.4

``````
``12 / 5.0``
``````2.4

``````
``12.0 / 5``
``````2.4

``````

``5 + 2.4``
``````7.4

``````

``3.4 - 3.2``
``````0.19999999999999973

``````
``12.3 + 32.4``
``````44.7

``````
``2.5 ** 2``
``````6.25

``````
``3.4 % 2.1``
``````1.2999999999999998

``````

Python的浮点数标准与CJava一致，都是IEEE 754 floating point standard

``'{:.52}'.format(3.4 - 3.2)``
``````'0.199999999999999733546474089962430298328399658203125'

``````

``print 3.4 - 3.2``
``````0.2

``````

``````import sys
sys.float_info``````
``````sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)

``````

``sys.float_info.max``
``````1.7976931348623157e+308

``````

``sys.float_info.min``
``````2.2250738585072014e-308

``````

``sys.float_info.epsilon``
``````2.220446049250313e-16

``````

## 复数 Complex Numbers

Python 使用 `j` 来表示复数的虚部：

``````a = 1 + 2j
type(a)``````
``````complex

``````

``a.real``
``````1.0

``````
``a.imag``
``````2.0

``````
``a.conjugate()``
``````(1-2j)

``````

## 交互计算

``1 + 2 - (3 * 4 / 6) ** 5 + 7 % 5``
``````-27

``````

Python中运算是有优先级的，优先级即算术的先后顺序，比如“先乘除后加减”和“先算括号里面的”都是两种优先级的规则，优先级从高到低排列如下：

• `( )` 括号
• `**` 幂指数运算
• `* / // %` 乘，除，整数除法，取余运算
• '+ -' 加减

``12.3 // 5.2``
``````2.0

``````
``12.3 // -4``
``````-4.0

``````

## 简单的数学函数

``abs(-12.4)``
``````12.4

``````

``round(21.6)``
``````22.0

``````

``````print min(2, 3, 4, 5)
print max(2, 4, 3)``````
``````2
4

``````

## 变量名覆盖

``type(max)``
``````builtin_function_or_method

``````

``````max = 1
type(max)``````
``````int

``````
``max(4, 5)``
``````---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-41-c60446be959c> in <module>()
----> 1 max(4, 5)

TypeError: 'int' object is not callable

``````

## 类型转换

``````print int(12.324)
print int(-3.32)``````
``````12
-3

``````

``print float(1.2)``
``````1.2

``````

## 其他表示

``1e-6``
``````1e-06

``````

16进制，前面加`0x`修饰，后面使用数字0-9A-F：

``0xFF``
``````255

``````

8进制，前面加`0`或者`0o`修饰，后面使用数字0-7：

``067``
``````55

``````

2进制，前面加`0b`修饰，后面使用数字0或1：

``0b101010``
``````42

``````

## 原地计算 In-place

Python可以使用下面的形式进行原地计算：

``````b = 2.5
b += 2
print b
b *= 2
print b
b -= 3
print b``````
``````4.5
9.0
6.0

``````

## 布尔型 Boolean Data Type

``````q = True
type(q)``````
``````bool

``````

``````q = 1 > 2
print q``````
``````False

``````

``````<, >, <=, >=, ==, !=
``````

Python支持链式比较：

``````x = 2
1 < x <= 3``````
``````True
``````