python2的默认数字类型_1.2python基础_数字类型_数字(Number)类型
一、整型(int型、整數(shù))
整型?等價(jià)于C中的有符號(hào)長(zhǎng)整型(long)
與系統(tǒng)的最大整型一致(如32位機(jī)器上的整型是32位,64位機(jī)器上的整型是64位),
可以表示的整數(shù)范圍在[-sys.maxint-1, sys.maxint]之間。整型字面值的表示方法有3種:
十進(jìn)制(常用)、八進(jìn)制(以數(shù)字“0”開頭)和十六進(jìn)制(以“0x”或“0X”開頭)。
整型的標(biāo)準(zhǔn)庫(kù)操作有如下
class int(object):
"""
int(x,底=10)->整數(shù)將一個(gè)數(shù)字或字符串轉(zhuǎn)換成整數(shù),
如果沒有給出參數(shù),則轉(zhuǎn)換為orreturn0if。如果x是一個(gè)數(shù)字,
則返回x. int_()。對(duì)于浮點(diǎn)數(shù),它會(huì)向零截?cái)嗳绻鹸不是一個(gè)數(shù)字,
或者給定了基數(shù),那么x必須是一個(gè)字符串、字節(jié)或hutearray實(shí)例,
表示給定基數(shù)中的整數(shù)字面值。文字的前面可以加“或”。然后被空白包圍。
基數(shù)默認(rèn)為10。有效的基數(shù)是0和2-36。Base 0表示將字符串的Base解釋為整數(shù)文字。
int (“0 b100' = 0)基地
"""
def bit_length(self): # real signature unknown; restored from __doc__
""" 返回表示該數(shù)字的時(shí)占用的最少位數(shù)
>>> bin(37)#bin()是返回二進(jìn)制后面有
'0b100101'
>>> (37).bit_length()
6
"""
pass
def conjugate(self, *args, **kwargs): # real signature unknown
""" 返回該復(fù)數(shù)的共軛復(fù)數(shù)"""
"""
a=123-12j #復(fù)數(shù)沒有實(shí)部時(shí)要補(bǔ)0.0
返回該復(fù)數(shù)的共軛復(fù)數(shù)
b=a.conjugate()
print(b)
#返回復(fù)數(shù)的實(shí)數(shù)部分
a.real
#返回復(fù)數(shù)的虛數(shù)部分
a.imag
"""
pass
@classmethod # known case
def from_bytes(cls, *args, **kwargs): # real signature unknown
"""
功能:res =?int.from_bytes(x)的含義是把bytes類型的變量x,
轉(zhuǎn)化為十進(jìn)制整數(shù),并存入res中。
其中bytes類型是python3特有的類型。
函數(shù)參數(shù):int.from_bytes(bytes, byteorder, *, signed=False)。
在IDLE或者命令行界面中使用help(int.from_bytes)命令可以查看具體介紹。
bytes是輸入的變量;byteorder主要有兩種:
'big'和'little';signed=True表示需要考慮符號(hào)位
舉例說明:int_s? =?int.from_bytes(s, byteorder='little', signed=True),
其中s='\xf1\xff',則輸出int_s=-15。分析一下過程,
'\x'表示十六進(jìn)制數(shù),先把'f1'寫成二進(jìn)制數(shù):1111 0001,
'ff'同上:1111 1111.由于s的高低位標(biāo)志是'little',
即'f1'是低位,'ff'是高位,所以正確的順序應(yīng)該是'fff1',
即11111111 1111 0001.又因?yàn)橐紤]符號(hào)位,第一位是1,所以s是負(fù)數(shù),
要進(jìn)行取反加一才是正確的十進(jìn)制數(shù)(第一位符號(hào)位的1不變),
可以得到10000000 00001111,寫成十進(jìn)制,就是-15,也就是int_s的結(jié)果。
上面的例子中,如果signed=False,則無(wú)符號(hào)位;若byteorder='big',
則輸入s的左邊是高位,右邊是低位。
>>> s1 = b'\xf1\xff'
>>> print(int.from_bytes(s1, byteorder='little', signed=True))
-15
>>> print(int.from_bytes(s1, byteorder='big', signed=False))
61951
>>> s2 = b'\xff\xf1'
>>> print(int.from_bytes(s2, byteorder='little', signed=False))
61951
"""
pass
def to_bytes(self, *args, **kwargs): # real signature unknown
"""
參照上面from_bytes(cls, *args, **kwargs):
這是上面的逆運(yùn)算
"""
pass
def __abs__(self, *args, **kwargs): # real signature unknown
"""返回絕對(duì)值"""
""" x.__abs__() <==> abs(x) """
""" abs(self) """
"""
a=-100
b=abs(a)
c=a.__abs__()
print(b)
print(c)
"""
pass
def __add__(self, *args, **kwargs): # real signature unknown
""" 加法,也可區(qū)分?jǐn)?shù)字和字符串"""
""" x.__add__(y) <==> x+y """
"""
a=10
b=20
c=a.__add__(b)
d=a+b
print(c)
print(d)
"""
pass
def __and__(self, *args, **kwargs): # real signature unknown
""" Return self&value. """
""" x.__and__(y) <==> x&y """
"""
&按位與運(yùn)算符:參與運(yùn)算的兩個(gè)值,
如果兩個(gè)相應(yīng)位都為1,則該位的結(jié)果為1,
否則為0(其他情況都為0)
"""
"""
a=1
b=1
c=a.__and__(b)
d=a&b
print(c)
print(d)
"""
pass
def __bool__(self, *args, **kwargs): # real signature unknown
""" self != 0 """
"""等于0返回False 其他返回True """
"""
False
True
True
"""
pass
def __ceil__(self, *args, **kwargs): # real signature unknown
""" Ceiling of an Integral returns itself. """
pass
def __divmod__(self, *args, **kwargs): # real signature unknown
""" Return divmod(self, value). """
""" 返回一個(gè)元組,第一個(gè)元素為商,第二個(gè)元素為余數(shù)"""
"""
a=14
b=3
c=(a).__divmod__(b)
print(c)
"""
pass
def __eq__(self, *args, **kwargs): # real signature unknown
""" Return self==value. """
""" 判斷兩個(gè)值是否相等"""
"""
a=14
b=3
c=(a).__eq__(b)
d=(a).__eq__(14)
print(c)
print(d)
"""
pass
def __float__(self, *args, **kwargs): # real signature unknown
""" float(self) """
"""轉(zhuǎn)換成floa型,將一個(gè)整數(shù)轉(zhuǎn)換成浮點(diǎn)型"""
""" x.__float__() <==> float(x) """
"""
a=100
b=(a).__float__()
print(b)
"""
pass
def __floordiv__(self, *args, **kwargs): # real signature unknown
""" Return self//value. """
"""整除,保留結(jié)果的整數(shù)部分"""
"""a//b"""
"""
a=100
b=a.__floordiv__(27)
c=a//27
print(b)
print(c)
"""
pass
def __floor__(self, *args, **kwargs): # real signature unknown
""" Flooring an Integral returns itself. """
"""返回本身"""
"""
a=100
b=a.__floor__()
print(b)
"""
pass
def __format__(self, *args, **kwargs): # real signature unknown
"""轉(zhuǎn)換對(duì)象的類型"""
"""
a=100
b=a.__format__('f')
c=a.__format__("0x")
d=a.__format__("b")
print(b)
print(c)
print(d)
"""
pass
def __getattribute__(self, *args, **kwargs): # real signature unknown
""" Return getattr(self, name). """
""""""
pass
def __getnewargs__(self, *args, **kwargs): # real signature unknown
pass
def __ge__(self, *args, **kwargs): # real signature unknown
""" Return self>=value. """
pass
def __gt__(self, *args, **kwargs): # real signature unknown
""" Return self>value. """
pass
def __hash__(self, *args, **kwargs): # real signature unknown
""" Return hash(self). """
pass
def __index__(self, *args, **kwargs): # real signature unknown
""" Return self converted to an integer, if self is suitable for use as an index into a list. """
pass
def __init__(self, x, base=10): # known special case of int.__init__
"""
int([x]) -> integer
int(x, base=10) -> integer
Convert a number or string to an integer, or return 0 if no arguments
are given. If x is a number, return x.__int__(). For floating point
numbers, this truncates towards zero.
If x is not a number or if base is given, then x must be a string,
bytes, or bytearray instance representing an integer literal in the
given base. The literal can be preceded by '+' or '-' and be surrounded
by whitespace. The base defaults to 10. Valid bases are 0 and 2-36.
Base 0 means to interpret the base from the string as an integer literal.
>>> int('0b100', base=0)
4
# (copied from class doc)
"""
pass
def __int__(self, *args, **kwargs): # real signature unknown
""" int(self) """
pass
def __invert__(self, *args, **kwargs): # real signature unknown
""" ~self """
pass
def __le__(self, *args, **kwargs): # real signature unknown
""" Return self<=value. """
pass
def __lshift__(self, *args, **kwargs): # real signature unknown
""" Return self<
pass
def __lt__(self, *args, **kwargs): # real signature unknown
""" Return self
pass
def __mod__(self, *args, **kwargs): # real signature unknown
""" Return self%value. """
pass
def __mul__(self, *args, **kwargs): # real signature unknown
""" Return self*value. """
pass
def __neg__(self, *args, **kwargs): # real signature unknown
""" -self """
pass
@staticmethod # known case of __new__
def __new__(*args, **kwargs): # real signature unknown
""" Create and return a new object. See help(type) for accurate signature. """
pass
def __ne__(self, *args, **kwargs): # real signature unknown
""" Return self!=value. """
pass
def __or__(self, *args, **kwargs): # real signature unknown
""" Return self|value. """
pass
def __pos__(self, *args, **kwargs): # real signature unknown
""" +self """
pass
def __pow__(self, *args, **kwargs): # real signature unknown
""" Return pow(self, value, mod). """
pass
def __radd__(self, *args, **kwargs): # real signature unknown
""" Return value+self. """
pass
def __rand__(self, *args, **kwargs): # real signature unknown
""" Return value&self. """
pass
def __rdivmod__(self, *args, **kwargs): # real signature unknown
""" Return divmod(value, self). """
pass
def __repr__(self, *args, **kwargs): # real signature unknown
""" Return repr(self). """
pass
def __rfloordiv__(self, *args, **kwargs): # real signature unknown
""" Return value//self. """
pass
def __rlshift__(self, *args, **kwargs): # real signature unknown
""" Return value<
pass
def __rmod__(self, *args, **kwargs): # real signature unknown
""" Return value%self. """
pass
def __rmul__(self, *args, **kwargs): # real signature unknown
""" Return value*self. """
pass
def __ror__(self, *args, **kwargs): # real signature unknown
""" Return value|self. """
pass
def __round__(self, *args, **kwargs): # real signature unknown
"""
Rounding an Integral returns itself.
Rounding with an ndigits argument also returns an integer.
"""
pass
def __rpow__(self, *args, **kwargs): # real signature unknown
""" Return pow(value, self, mod). """
pass
def __rrshift__(self, *args, **kwargs): # real signature unknown
""" Return value>>self. """
pass
def __rshift__(self, *args, **kwargs): # real signature unknown
""" Return self>>value. """
pass
def __rsub__(self, *args, **kwargs): # real signature unknown
""" Return value-self. """
pass
def __rtruediv__(self, *args, **kwargs): # real signature unknown
""" Return value/self. """
pass
def __rxor__(self, *args, **kwargs): # real signature unknown
""" Return value^self. """
pass
def __sizeof__(self, *args, **kwargs): # real signature unknown
""" Returns size in memory, in bytes. """
pass
def __str__(self, *args, **kwargs): # real signature unknown
""" Return str(self). """
pass
def __sub__(self, *args, **kwargs): # real signature unknown
""" Return self-value. """
pass
def __truediv__(self, *args, **kwargs): # real signature unknown
""" Return self/value. """
pass
def __trunc__(self, *args, **kwargs): # real signature unknown
""" Truncating an Integral returns itself. """
pass
def __xor__(self, *args, **kwargs): # real signature unknown
""" Return self^value. """
pass
denominator = property(lambda self: object(), lambda self, v: None, lambda self: None) # default
"""the denominator of a rational number in lowest terms"""
imag = property(lambda self: object(), lambda self, v: None, lambda self: None) # default
"""the imaginary part of a complex number"""
numerator = property(lambda self: object(), lambda self, v: None, lambda self: None) # default
"""the numerator of a rational number in lowest terms"""
real = property(lambda self: object(), lambda self, v: None, lambda self: None) # default
"""the real part of a complex number"""
總結(jié)
以上是生活随笔為你收集整理的python2的默认数字类型_1.2python基础_数字类型_数字(Number)类型的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 二值化图像去除小黑点_python与图像
- 下一篇: python线程间通信_python多线