python数字类型及运算_Python类型和运算--数字
在Python中,數字并不是一個真正的對象類型,而是一組相似類型的分類。不僅包括通常的數字類型(整數和浮點數),黑包括數字表達式,以及高級的數字編程。
基本數字常量
數字 常量
1234, -24 整數(無窮大小)
1.23, 3.14e10 浮點數
0177, 0x9ff, 0b1010 Python2.6中八進制,十六進制,二進制常量
0o177, 0x9ff, 0b1010 Python3.4中八進制,十六進制,二進制常量
1. 整數和浮點數常量、
整數以十進制字符串出現,浮點數帶有一個小數點,可以用科學記數法表達(1.0e10或1.0E10)
2. 十六進制,八進制和二進制常量
十六進制以0x或0X開頭,八進制以0或0o開頭,二進制以0b或0B開頭。
內置函數hex(I),oct(I),bin(I)分別可以表示這三種進制。另外,int(str, base)可以把字符串str轉換為以base為進制的整數。
3. 復數
實部 + 虛部:2+3j,5+2J
內置函數complex(real, imag)來創建復數
4. 編輯其他數字類型
通過調用導入模塊的函數來創建,例如分數。
內置數學工具和擴展
表達式操作符:+ - * ** />> &等
內置數學工具:pow abs round int hex bin
round(number, ndigit)
>>>round(3.1415926,2)
3.14
>>>round(3.1415926)#默認為0
3
>>>round(3.1415926,-2)
0.0
>>>round(3.1415926,-1)
0.0
>>>round(314.15926,-1)
310.0
公用模塊:random match
例如:
printrandom.randint(12,20)#生成的隨機數n: 12 <= n <= 20printrandom.randint(20,20)#結果永遠是20#print random.randint(20, 10) #該語句是錯誤的。下限必須小于上限。
常用方法:
is_integer, is_digit
Operation
Result
Notes
Full documentationx +
y
sum of x and y
x -
y
difference of x and y
x *
y
product of x and y
x /
y
quotient of x and y
x //
y
floored quotient of x andy
(1)
x %
y
remainder of x / y
(2)
-x
x negated
+x
x unchanged
abs(x)
absolute value or magnitude ofx
int(x)
x converted to integer
(3)(6)
float(x)
x converted to floating point
(4)(6)
complex(re, im)
a complex number with real partre, imaginary part im.im defaults to zero.
(6)
c.conjugate()
conjugate of the complex numberc
divmod(x, y)
the pair (x // y, x % y)
(2)
pow(x, y)
x to the power y
(5)
x **
y
x to the power y
(5)
表達式和操作符Operation
Result
Notesx |
y
bitwise or of x andy
x ^
y
bitwise exclusive or ofx and y
x &
y
bitwise and of x andy
x <<
n
x shifted left by n bits
(1)(2)
x >>
n
x shifted right by n bits
(1)(3)
~x
the bits of x inverted
變量和基本表達式
1. 變量在第一次創建時賦值
2. 變量在表達式中使用將被替換為它們的值
3. 變量在表達式中使用以前必須被賦值
4. 變量像對象一樣不需要在一開始進行聲明
也就是說,這些賦值會讓變量a和b自動生成:
%python
>>> a = 3
>>> b = 4
數字顯示的格式與C語言很相似
>>> num = 1/3.0
>>> '%e'%num
'3.333333e-01'
除法:傳統除法,Floor除法和真除法
'Python 2.6:X/Y 表示傳統除法,10/4==2;X//Y Floor除法,10/4 == 2
Python 3.0: X/Y表示真除, 10/4 == 2.5;X//Y Floor除法,10//4 == 2
位操作
與C語言相似,
>>> x = 1
>>> x >> 2
4
>>> x | 2
3
>>> x & 1
1
小數數字
由于浮點型缺乏精確性,因為用來存儲數值的空間有限,例如,
>>> 0.1 + 0.1 + 0.1 - 0.3
5.551115123125783e-17
可以是用小數對象
>>> from decimal import Decimal
>>> Decimal(0.1) + Decimal(0.1) + Decimal(0.1) - Decimal(0.3)
Decimal('0.0')
還可以用來設置全局精度
>>> import decimal
>>> decimal.getcontext().prec = 4
分數類型
>>> from fractions import Fraction
>>> x = Fraction(1,3)
>>> (2.5).as_integer_ratio()
(5, 2)
集合
Python 2.4 引入了一種新的類型--集合(set),這種類型和數學上的集合有著本質的相似
1. Python 2.x的集合
>>> x = set('abcde')
>>> y = set('bdxyz')
>>> x
set(['a', 'c', 'b', 'e', 'd'])
>>> y
set(['y', 'x', 'b', 'd', 'z'])
>>> 'e' in x
True
>>> x - y
set(['a', 'c', 'e'])
>>> x&y
set(['b', 'd'])
>>> x^y
set(['a', 'c', 'e', 'y', 'x', 'z'])
>>> x|y
set(['a', 'c', 'b', 'e', 'd', 'y', 'x', 'z'])
set可以進行添加刪除操作這里不再贅述
2. Python 3.x 的集合
>>> x = set('abcde')
>>> y = set('bdxyz')
>>> x
{'a', 'b', 'c', 'd', 'e'}
這個集合看起來更像是無值的字典
>>> {x**2 for x in [1,2,3,4]}
{16, 1, 9, 4}
總結
以上是生活随笔為你收集整理的python数字类型及运算_Python类型和运算--数字的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 图像内复制粘贴篡改取证matlab_[论
- 下一篇: python安装第三方库win10_音频