Files
ailearning/docs/da/009.md
2020-10-19 21:08:55 +08:00

810 lines
7.0 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 数字
## 整型 Integers
整型运算,加减乘:
In [1]:
```py
2 + 2
```
Out[1]:
```py
4
```
In [2]:
```py
3 - 4
```
Out[2]:
```py
-1
```
In [3]:
```py
4 * 5
```
Out[3]:
```py
20
```
在**Python 2.7**中,整型的运算结果只能返回整型,**除法**的结果也不例外。
例如`12 / 5`返回的结果并不是2.4而是2
In [4]:
```py
12 / 5
```
Out[4]:
```py
2
```
幂指数:
In [5]:
```py
2 ** 5
```
Out[5]:
```py
32
```
取余:
In [6]:
```py
32 % 5
```
Out[6]:
```py
2
```
赋值给变量:
In [7]:
```py
a = 1
a
```
Out[7]:
```py
1
```
使用`type()`函数来查看变量类型:
In [8]:
```py
type(a)
```
Out[8]:
```py
int
```
整型数字的最大最小值:
在 32 位系统中,一个整型 4 个字节,最小值 `-2,147,483,648`,最大值 `2,147,483,647`
在 64 位系统中,一个整型 8 个字节,最小值 `-9,223,372,036,854,775,808`,最大值 `9,223,372,036,854,775,807`
In [9]:
```py
import sys
sys.maxint
```
Out[9]:
```py
2147483647
```
## 长整型 Long Integers
当整型超出范围时,**Python**会自动将整型转化为长整型,不过长整型计算速度会比整型慢。
In [10]:
```py
a = sys.maxint + 1
print type(a)
```
```py
<type 'long'>
```
长整型的一个标志是后面以字母L结尾
In [11]:
```py
a
```
Out[11]:
```py
2147483648L
```
可以在赋值时强制让类型为长整型:
In [12]:
```py
b = 1234L
type(b)
```
Out[12]:
```py
long
```
长整型可以与整型在一起进行计算,返回的类型还是长整型:
In [13]:
```py
a - 4
```
Out[13]:
```py
2147483644L
```
## 浮点数 Floating Point Numbers
In [14]:
```py
a = 1.4
type(a)
```
Out[14]:
```py
float
```
在之前的除法例子`12 / 5`假如想要使返回的结果为2.4,可以将它们写成浮点数的形式:
In [15]:
```py
12.0 / 5.0
```
Out[15]:
```py
2.4
```
In [16]:
```py
12 / 5.0
```
Out[16]:
```py
2.4
```
In [17]:
```py
12.0 / 5
```
Out[17]:
```py
2.4
```
上面的例子说明,浮点数与整数进行运算时,返回的仍然是浮点数:
In [18]:
```py
5 + 2.4
```
Out[18]:
```py
7.4
```
浮点数也可以进行与整数相似的运算,甚至可以取余:
In [19]:
```py
3.4 - 3.2
```
Out[19]:
```py
0.19999999999999973
```
In [20]:
```py
12.3 + 32.4
```
Out[20]:
```py
44.7
```
In [21]:
```py
2.5 ** 2
```
Out[21]:
```py
6.25
```
In [22]:
```py
3.4 % 2.1
```
Out[22]:
```py
1.2999999999999998
```
**Python**的浮点数标准与**C****Java**一致,都是[IEEE 754 floating point standard](http://en.wikipedia.org/wiki/IEEE_floating_point)。
注意看 `3.4 - 3.2` 的结果并不是我们预期的`0.2`,这是因为浮点数本身储存方式引起的,浮点数本身会存在一点误差。
事实上,**Python** 中储存的值为'0.199999999999999733546474089962430298328399658203125'因为这是最接近0.2的浮点数。|
In [23]:
```py
'{:.52}'.format(3.4 - 3.2)
```
Out[23]:
```py
'0.199999999999999733546474089962430298328399658203125'
```
当我们使用`print`显示时,**Python**会自动校正这个结果
In [24]:
```py
print 3.4 - 3.2
```
```py
0.2
```
可以用`sys.float_info`来查看浮点数的信息:
In [25]:
```py
import sys
sys.float_info
```
Out[25]:
```py
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)
```
例如浮点数能表示的最大值:
In [26]:
```py
sys.float_info.max
```
Out[26]:
```py
1.7976931348623157e+308
```
浮点数能表示的最接近0的值
In [27]:
```py
sys.float_info.min
```
Out[27]:
```py
2.2250738585072014e-308
```
浮点数的精度:
In [28]:
```py
sys.float_info.epsilon
```
Out[28]:
```py
2.220446049250313e-16
```
## 复数 Complex Numbers
**Python** 使用 `j` 来表示复数的虚部:
In [29]:
```py
a = 1 + 2j
type(a)
```
Out[29]:
```py
complex
```
可以查看它的实部,虚部以及共轭:
In [30]:
```py
a.real
```
Out[30]:
```py
1.0
```
In [31]:
```py
a.imag
```
Out[31]:
```py
2.0
```
In [32]:
```py
a.conjugate()
```
Out[32]:
```py
(1-2j)
```
## 交互计算
可以将复杂的表达式放在一起计算:
In [33]:
```py
1 + 2 - (3 * 4 / 6) ** 5 + 7 % 5
```
Out[33]:
```py
-27
```
在**Python**中运算是有优先级的,优先级即算术的先后顺序,比如“先乘除后加减”和“先算括号里面的”都是两种优先级的规则,优先级从高到低排列如下:
* `( )` 括号
* `**` 幂指数运算
* `* / // %` 乘,除,整数除法,取余运算
* '+ -' 加减
整数除法,返回的是比结果小的最大整数值:
In [34]:
```py
12.3 // 5.2
```
Out[34]:
```py
2.0
```
In [35]:
```py
12.3 // -4
```
Out[35]:
```py
-4.0
```
## 简单的数学函数
绝对值:
In [36]:
```py
abs(-12.4)
```
Out[36]:
```py
12.4
```
取整:
In [37]:
```py
round(21.6)
```
Out[37]:
```py
22.0
```
最大最小值:
In [38]:
```py
print min(2, 3, 4, 5)
print max(2, 4, 3)
```
```py
2
4
```
## 变量名覆盖
不要用内置的函数来命名变量,否则会出现意想不到的结果:
In [39]:
```py
type(max)
```
Out[39]:
```py
builtin_function_or_method
```
不要这样做!!!
In [40]:
```py
max = 1
type(max)
```
Out[40]:
```py
int
```
In [41]:
```py
max(4, 5)
```
```py
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-41-c60446be959c> in <module>()
----> 1 max(4, 5)
TypeError: 'int' object is not callable
```
## 类型转换
浮点数转整型,只保留整数部分:
In [42]:
```py
print int(12.324)
print int(-3.32)
```
```py
12
-3
```
整型转浮点型:
In [43]:
```py
print float(1.2)
```
```py
1.2
```
## 其他表示
除了10进制外整数还有其他类型的表示方法。
科学计数法:
In [44]:
```py
1e-6
```
Out[44]:
```py
1e-06
```
16进制前面加`0x`修饰后面使用数字0-9A-F
In [45]:
```py
0xFF
```
Out[45]:
```py
255
```
8进制前面加`0`或者`0o`修饰后面使用数字0-7
In [46]:
```py
067
```
Out[46]:
```py
55
```
2进制前面加`0b`修饰后面使用数字0或1
In [47]:
```py
0b101010
```
Out[47]:
```py
42
```
## 原地计算 In-place
**Python**可以使用下面的形式进行原地计算:
In [48]:
```py
b = 2.5
b += 2
print b
b *= 2
print b
b -= 3
print b
```
```py
4.5
9.0
6.0
```
## 布尔型 Boolean Data Type
布尔型可以看成特殊的二值变量,其取值为`True``False`
In [49]:
```py
q = True
type(q)
```
Out[49]:
```py
bool
```
可以用表达式构建布尔型变量:
In [50]:
```py
q = 1 > 2
print q
```
```py
False
```
常用的比较符号包括:
```py
<, >, <=, >=, ==, !=
```
**Python**支持链式比较:
In [51]:
```py
x = 2
1 < x <= 3
```
Out[51]:
```py
True
```
In [ ]: