标签:
基本数据类型
在Python中,一切事物都是对象,对象基于类创建
注:查看对象相关成员的函数有:var,type,dir
基本数据类型主要有:
接下来将对以上数据类型进行剖析:
1、整数 int
如:18、73、-100
每一个整数都具备如下功能:
class int(object):
"""
int(x=0) -> 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
"""
def bit_length(self): # real signature unknown; restored from __doc__
"""
int.bit_length() -> int
Number of bits necessary to represent self in binary.
"""
"""
表示该数字返回时占用的最少位数
例如:整数37用二进制方法表示为:0b100101,调用.bit_length方法时他只占了6位
>>> bin(37)
‘0b100101‘
>>> (37).bit_length()
6
整数10用二进制方法表示为:0b1010,调用.bit_length方法时他只占了4位
>>> bin(10)
‘0b1010‘
>>> (10).bit_length()
4
"""
return 0
def conjugate(self, *args, **kwargs): # real signature unknown
""" Returns self, the complex conjugate of any int."""
"""返回该复数的共轭复数""" # <--- 银角大王说没啥卵用
pass
@classmethod # known case
def from_bytes(cls, bytes, byteorder, *args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__
"""
int.from_bytes(bytes, byteorder, *, signed=False) -> int
Return the integer represented by the given array of bytes.
The bytes argument must be a bytes-like object (e.g. bytes or bytearray).
The byteorder argument determines the byte order used to represent the
integer. If byteorder is ‘big‘, the most significant byte is at the
beginning of the byte array. If byteorder is ‘little‘, the most
significant byte is at the end of the byte array. To request the native
byte order of the host system, use `sys.byteorder‘ as the byte order value.
The signed keyword-only argument indicates whether two‘s complement is
used to represent the integer.
"""
"""
python官方给出了下面几个例子,我是没看懂,谁看懂了告诉我下:
这个方法是在Python3.2的时候加入的
>>> int.from_bytes(b‘\x00\x10‘, byteorder=‘big‘)
16
>>> int.from_bytes(b‘\x00\x10‘, byteorder=‘little‘)
4096
>>> int.from_bytes(b‘\xfc\x00‘, byteorder=‘big‘, signed=True)
-1024
>>> int.from_bytes(b‘\xfc\x00‘, byteorder=‘big‘, signed=False)
64512
>>> int.from_bytes([255, 0, 0], byteorder=‘big‘)
16711680
"""
pass
def to_bytes(self, length, byteorder, *args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__
"""
int.to_bytes(length, byteorder, *, signed=False) -> bytes
Return an array of bytes representing an integer.
The integer is represented using length bytes. An OverflowError is
raised if the integer is not representable with the given number of
bytes.
The byteorder argument determines the byte order used to represent the
integer. If byteorder is ‘big‘, the most significant byte is at the
beginning of the byte array. If byteorder is ‘little‘, the most
significant byte is at the end of the byte array. To request the native
byte order of the host system, use `sys.byteorder‘ as the byte order value.
The signed keyword-only argument determines whether two‘s complement is
used to represent the integer. If signed is False and a negative integer
is given, an OverflowError is raised.
"""
"""
# 应该是跟上面的from_bytes是相反的,感觉没啥卵用
官方例子:
>>> (1024).to_bytes(2, byteorder=‘big‘)
b‘\x04\x00‘
>>> (1024).to_bytes(10, byteorder=‘big‘)
b‘\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00‘
>>> (-1024).to_bytes(10, byteorder=‘big‘, signed=True)
b‘\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00‘
>>> x = 1000
>>> x.to_bytes((x.bit_length() // 8) + 1, byteorder=‘little‘)
b‘\xe8\x03‘
"""
pass
def __abs__(self, *args, **kwargs): # real signature unknown
""" abs(self)"""
"""
返回一个绝对值
例子:
>>> (10).__abs__()
10
>>> (-10).__abs__()
10
"""
pass
# 这里有两个__add__() 应该是用来区分数字和字符串的
def __add__(self, *args, **kwargs): # real signature unknown
""" Return self+value."""
"""
例子:
>>> (3).__add__(4)
7
# 在python3.x中,数字不能和字符串相加
>>> (3).__add__(‘a‘)
NotImplemented
"""
pass
def __and__(self, *args, **kwargs): # real signature unknown
""" Return self&value."""
"""
例子:
>>> a = 3
>>> a.__add__(4)
7
"""
pass
def __bool__(self, *args, **kwargs): # real signature unknown
""" self != 0 """
"""
判断一个整数对象是否为0,如果为0,则返回False,如果不为0,则返回True
例子:
>>> (-1).__bool__()
True
>>> (0).__bool__()
False
"""
pass
def __ceil__(self, *args, **kwargs): # real signature unknown
""" Ceiling of an Integral returns itself. """
"""
好像是返回自己本身,没啥卵用
例子:
>>> (35).__ceil__()
35
>>> (35343).__ceil__()
35343
>>> (-35343).__ceil__()
-35343
"""
pass
def __divmod__(self, *args, **kwargs): # real signature unknown
""" Return divmod(self, value). """
"""
返回一个元组,第一个元素为商,第二个元素为余数
例子:
>>> (3).__divmod__(2)
(1, 1)
>>> (3).__divmod__(3)
(1, 0)
>>> (3).__divmod__(4)
(0, 3)
"""
pass
def __eq__(self, *args, **kwargs): # real signature unknown
""" Return self==value. """
"""
判断两个值是否相等
例子:
>>> (3).__eq__(4)
False
>>> (3).__eq__(3)
True
"""
pass
def __float__(self, *args, **kwargs): # real signature unknown
""" float(self) """
"""
将一个整数转换成浮点型
例子:
>>> a = 1
>>> a.__float__()
1.0
"""
pass
def __floordiv__(self, *args, **kwargs): # real signature unknown
""" Return self//value. """
"""
整除,保留结果的整数部分
例子:
>>> a = 10
>>> b = 3
>>> a.__floordiv__(b)
3
"""
pass
def __floor__(self, *args, **kwargs): # real signature unknown
""" Flooring an Integral returns itself. """
"""
返回本身,没啥卵用
例子:
a = 10
>>> a.__floor__()
10
"""
pass
def __format__(self, *args, **kwargs): # real signature unknown
""" """
"""
转换对象的类型
例子:
>>> a = 10
>>> a.__format__(‘f‘)
‘10.000000‘
>>> a.__format__(‘b‘)
‘1010‘
"""
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=0) -> 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<<value. """
pass
def __lt__(self, *args, **kwargs): # real signature unknown
""" Return self<value. """
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<<self. """
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"""
标签:
原文地址:http://www.cnblogs.com/CongZhang/p/5128864.html