基本数据类型一:数字类型
Python中最基本的数据类型之一就是int数据类型。
- 数字类型中有int、long 、float、complex。
- python3.x之后已经不区分int和long,统一用int;python2.x还是区分的。
- float类型和其它语言的float基本一致,浮点数,说白了,就是带小数点的数,精度与机器相关。
- complex:实数+虚数 就是复数,虚数部分必须有 j。
在Pycharm(Python版本3.6.0)中输入int,然后Ctrl+鼠标左键点击进入builtins.py文件。找到int的定义。截取关于int的描述和最基本的函数:
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.
>>> bin(37)
'0b100101'
>>> (37).bit_length()
6
"""
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.
"""
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.
"""
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"""
bit_length()
-
函数把当前数字的二进制,至少用n位表示出来。例如:
a = 37 b = a.bit_length() print(b)
得到结果是6。正确。
但在实验中,下面代码得到
a = bin(37) b = a.bit_length() print(b)
得到 AttributeError: ‘str’ object has no attribute ‘bit_length’
查看了一下上面a的type是str,也就是说bin(37)返回的是一个字符串为’0b100101’,不是int类型。
conjugate()
-
复数.conjugate(), 返回复数的共轭。
复数型:实数(real)+虚数(imag) 就是复数,虚数部分必须有j。from sympy import conjugate a = 1 + 4j t = conjugate(a) print(t)
得到 (1-4j)
查看类型为complex型。
from_bytes()
-
int.from_bytes(bytes, byteorder, *, signed=False) -> int
返回由给定字节数组表示的整数。
字节参数必须是一个类似于字节的对象(例如字节或字节数组)。
字节顺序参数确定用于表示
如果字节顺序是’big’,那么最重要的字节在字节数组的开头。
如果字节顺序是’little’,则有效字节位于字节数组的末尾。
要请求本机主机系统的字节顺序,使用“sys.Byte order”作为字节顺序值。带符号关键字的参数指示两个的补码是否为用于表示整数。
to_bytes()
-
int.to_bytes(length, byteorder, *, signed=False) -> bytes
返回一个表示整数的字节数组。
整数是用长度字节表示的。OverflowError是如果整数不能用给定的字节。
字节顺序参数确定用于表示
如果字节顺序是’big’,那么最重要的字节在字节数组的开头。
如果字节顺序是’little’,则有效字节位于字节数组的末尾。
要请求本机主机系统的字节顺序,使用“sys.Byte order”作为字节顺序值。带符号关键字的参数确定两个的补码是否为用于表示整数。
如果有符号为False,并且为负整数
将引发OverflowError。