【python】Ch5_数字与动态类型

继上一环节 Ch4:https://blog.csdn.net/qq_37165755/article/details/79843412 所提到的八点总览,现在要开始一层一层拨开里面的内容了,以下有请“数字”登场。

数字并不是一个真正的对象类型,而是一组类似类型的分类,其中有很多中类型被包含在这个大类里面,包含了如下:
    1. 整数 integer:我们最一般看到的样子,123这种纯粹数字
    2. 浮点数 float:有小数点是最直观可辨认的,还有加了科学记号 E 或 e 的那种数
    3. 复数 complex:实部 + 虚部,其中虚部以 j 或是 J 表示,内部看来就是以浮点数表示,但实际算法不同
    4. 无穷精度整数
    5. 固定精度十进制数
    6. 集合
    7. bool 值 ... etc


长整型数:整数部分用长整型,浮点数用双精度浮点型来记录,至少有32位,跟 C 语言的编译精度一样。其电脑內存空間有多大,就會有多少位數字。一般在數字後面加上 L 可以變成長整型,but if the number is too big for calculating, python would automaticallychanges the type of the number into a proper corresponding one. This is reallyconvenient to calculate huge amount of data if the computer is supportive.
长整型数常量:1 或 L 结尾,就成了 python 的长整型数
八进制:开头多为 0 开始,数字从 0~7
十六进制:开头多为 0x 或是 0X,后面接 0~9 和 A~F,编写数字的时候大小写都可以

表达式的 operators: + - * /  >>  ** 等
内置数学函数:pow, abs 等
内置数学模块:random, math 等
更多的 operators 要查找的话参照如下网址:http://www.runoob.com/python/python-operators.html
The operational sequence of the operators are all intuitivelypreset in Python already. No extra worries about this. For example, * is prior than +.

如果需要一些严谨的数字运算,Numpy提供了进阶的计算拓展包,里面包含了矩阵,向量等多样的数学运算工具。

这些数字在运算中的复杂程度依序为,整数 --> 长整数 --> 浮点数 --> 复数。

而如果两个不同类型的数做了运算,那 python 会自动把结果升级成为最复杂的那个类型,例如 1+3.14 ,结果就会是个浮点数。


数字显示格式
有些時候計算機的好壞(就是電腦的好壞)會影響浮點數顯示出來的位數結果,他可能算不到那麼後面因此顯示一個四捨五入過後的值。下面两种方式可以 transform number into a string: 
    1. repr(num)  >>>  '0.333331' 如同上面长整数常量类型的结尾方式

    2. str(num)  >>>  'o.333333' 编程了字符串而不再是数字

要特別注意的操作符作用:

>>> x = 1
>>> X << 2
4   # 因為從二進制的角度看 1 為 0001,左邊整串數字向左邊平移兩格,移出去的忽略,空出來的補 0 ,結果就是0100,答案為 4 。
>>> X | 2
3   #|為 or 的意思,2 表示 0010,與 1 的 0001 疊在一起則為 0011,答案為 3 。
>>> X & 1
1   # & 為 and 的意思,答案為 1。

Octal and hex literals (八進制與十六進制):

# 我們也可以直接函數定義進制的種類
>>> oct(64), hex(64), hex(255)
('0100', '0x40' ,'0xff')
# 又或者可以直接用 int 函數的第二個參數定義種類
>>> int('0100'), int('0100', 8), int('0x40', 16)
(100, 64, 64)

內置運算符:

# Except for the math tool named NumPy, there are also some useful intrinsic tools already existing in python.So we better be familiar with these operators.
>>> import math

>>> math.pi, math.e, math.sin(2*math.pi/180),math.sqrt(144), abs(-42)
(3.1425926…, 2.71828182…,0.0348996…, 12.0, 42)
>>> 2**4, pow(2,4), int(2.567), round(2.567),round(2.567, 2)
(2, 3.0, 2.57)
>>> import ramdom

>>> random.random()
0.4974…..
>>> random.randint(1, 10)
5  # pick up a random integer from 1 to 10 andprint it out
>>> random.choice(['life of brian', holy grail','meaning of life'])
'life of brian'

小數字數 decimal number:

# 小數就如同浮點數,有固定的精度,但是在運算方面有個缺陷, eg:
>>> 0.1 + 0.1 + 0.1 - 0.3
5.551115123125…-017

# The result is approaching zero but actually it is not zero. This flaw is caused by the fundamental limit of thehardware. The way to fix this problem is to import function of decimal.
>>> from decimal import Decimal

>>> Decimal('0.1') + Decimal('0.1') +Decimal('0.1') - Decimal('0.3')
Decimal('0')

集合set:

# Create a set object, 將一個序列或其他的迭代對象傳遞給內置 set 函數
>>> x = set('abcde')
>>> y = set('bdxyz')
>>> x
Set(['a', 'c', 'b', 'd','e'])     # mind that the sequence is notguarantee

When using this function 'set' to create a list, the mathematical set calculation would be executable. If there is only a list created by simple statement, the list won't support this operator such as | and &. 這個 set 功能很方便大數據的分選與精煉。

布爾型 bool: True and False
是一個預先定義的內置變量名稱,實際上僅僅是整數類型 int 的一個子類,(True, False) = (1, 0),行為和 0 與 1 是一樣的,但他有特殊的邏輯打印形式,以 Ture 或 False 顯示。

可將此 Ture 或 False 但作為整數 1 和 0 的變量,所以 True + 3 = 4。


而 python 里面的变量类型也是动态的,不用通过宣告来定义。例如键入 a = 3,那么 a 这个变量就被创建了。

  • Variable is an element within the system list, possessing the space for object oriental appointment.
  • Object is a piece of distributed memory, having enough space to represent the declared value
  • 引用是自动形成的,从变量到对象的指针

变量类型
类型的概念存在于对象中,而非变量名本身,因此变量不会被任何与之关联的类型约束,他只是在一个特定的时间点简单引用了特定对象而已。例如:

>>>  a = 3          #it's an integer
>>>  a = 'spam'         #now it's a string
>>>  a = 1.23        #not it's a floating point

当从新给 a 赋值的时候,之前占用的对象空间就被自动回收了,也称作垃圾搜集。

变量使用
逻辑上来说,如果键入了 a = 3,则过程如下:
    1. 创建对象 object 来代表 3
    2. 创建一个变量 variable 为 a
    3. 把创建的 object and variable 做一个关联

Mind that the variable is always linked to the object. Technically, object is more complex with its structure, 而不是有足夠空間表達它的值那麼簡單,每個對象都有它的標準頭部訊息。一個是「類型標志符」標註對象類型,另一個是引用的「計數器」用來決定是否能夠回收這個對象。


引用共享

繼續剛才 a 的例子,再添加一個變量 b ,如下指令:

>>>  b = a
# 雖然 b 沒有被賦值,但變量 a 因為這個指令成了對象 3 的一個引用,在內部實際上指向了對象的內存空間,通過運行常量表達式創建。

# If we set a more complex situation below:
>>>  a = 3
>>>  b = a
>>>  a ='spam'
# Then, b will still be 3 but not affected by the newobject orientation that happened to a.
不过... 例外无所不在,有些对象 object 像是 list 这类的可以被原地修改的数据形态,如果经过共享之后,还修改了其中一个 variable 的内容,则被共享的另一个 variable 也会因此受到牵连,如下示例:
>>>  x = [1, 2, 3, 4]
>>>  y = x
>>>  y
[1, 2, 3, 4]        # now the y variable is the same as x variable
>>>  x[0] = 99      # change the first parameter of the list into 99
>>>  x
[99, 2, 3, 4]
>>>  y
[99, 2, 3, 4]        # y is affected by the change of x list
# This is the part we have to bare in mind for the developing process. devil is always hidden in the details.
 
# What if we don't want this situation happens to us? The only little difference is to link y as the copy one of x. Eg:
>>>  y = x[:]        # make a copy of x
# Then, whatever the x is changed next, the change won't affect y anymore.
# But this approach has it own limit too. This can only be used to the object with sequence. Dictionary would not be probable for this way. We should therefore use function 「copy」
 
>>>  import copy
>>>  y = copy.copy(x)          # make a top-level 'shallow' copy of any object Y
>>>  y = copy.deepcopy(x)         # make a deep copy of any object Y:copy all nested parts

共享引用与相等的差别
如果說 y 共享了 x 的對象,並不代表 x 就是 y,只能說他們兩個值一樣。以下例證:

>>>  x = 42
>>>  y = x
>>>  x == y
True
>>>  y is x
False        #they are just different object but linked together
 
# But if both x and y are pointed to the same object individually, x would be y.
>>>  y = 42
>>>  x == y
True
>>>  y is x
True

When writing code by using python, it's not necessary to trace the type of the object constantly. The type of the object will constantly be changed by our latest update.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值