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. 将一个数字或者字符串(str.isNum==True),如果没有给参数,默认为0(如print(int())结果为0) 如果参数x是一个数字,则返回x.__int__()的结果,如果是一个浮点数,则会只取整数, (如print(int(2.5))的结果为2) ---------------------------------------------------------------------------------- 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 base参数为进制,默认为10进制 """ def bit_length(self): # real signature unknown; restored from __doc__ """ int.bit_length() -> int ---------------------------------------------------------------------------------- 返回一个int数转换为二进制后至少要用多少位来表示,返回值为int类型 Number of bits necessary to represent self in binary. 数字必须转换为二进制后再计算 ---------------------------------------------------------------------------------- >>> bin(37) '0b100101' >>> (37).bit_length() 6 如:37的二进制为'0b100101'(6位),那么37的bit_length()结果为6 ---------------------------------------------------------------------------------- """ return 0
示例
print(int()) # 结果为0 print(int(2.5)) # 结果为 n = 250 n_bytes = bin(n) print(n_bytes) # 结果为0b11111010 print(n.bit_length()) # 结果为8 print(int('0b1010',base=0)) # 结果为10