python数据类型-数字

总结:Python3.6版本

注:【进制数也属于整数,我们说的整数为10进制整数】

数字分类:

整数:-1 、1、进制数浮点数:-0.11、0.11进制数:0b1010,0B1010(二进制0b或者0B) 0o127,0O127(八进制0o或者0O) 0x12ef,0X12ef(十六进制0x或者0X)复数:1.23+1.0j,34-4j分数【不常用跳过】

运算操作:

加,减,乘,除,求整,求余,求幂、布尔运算,复数运算等[+ - * \\ // % ** ]

内置函数:

bin:10进制转化为2进制oct:10进制转化为8进制hex:10进制转化为16进制int:字符串或浮点数转化为10进制整数&&其它进制转10进制float:字符串或者整数转化为浮点数complex:complex(a,b)创造复数a为实部b为虚部pow:幂运算abs:绝对值运算round:四舍五入运算ord():求ASCII码数值chr():ASCII码值逆运算

常用模块:

mathround

bool值:

逻辑运算返回布尔值

True:非0数字和非空字符串False:0、None、空字符串

<code># 浮点数
a=12.8



# 二进制数据--int型
b=0b1010

# 复数
c=12.3+5.0j

# 复数
d=-10

# 10进制数据转换为2进制数据
print(bin(10)) # 0b1010

# 10进制数据转换为8进制数据
print(oct(10)) # 0o12

# 10进制数据转换为16进制数据
print(hex(10)) # 0xa

# 2进制数据转化为10进制
print(int(0b1010)) # 10

# 8进制数据转化为10进制
print(int(0o12)) # 10

# 16进制数据转化为10进制
print(int(0xa)) # 10
print(bin(int(0xa))) # 0b1010

# 浮点数转化为10进制
print(int(10.8)) # 10

# base=0把字符串'0o12'当作数字0o12,相当于int(0o12)
print(int('0o12', base=0)) # 10

# 字符串转化为10进制数据 参数进制数必须保持一致
print(int('0o12', base=8)) # 10

# 字符串转浮点数

print(float('123')) # 123.0

# 整型转浮点型
print(float(123)) # 123.0

# 创造复数1为实部2为虚部
print(complex(1, 2)) # (1+2j)

# 幂运算
print(pow(2, 3)) # 8

# 绝对值运算
print(abs(-10.3)) # 10.3

# 四舍五入运算
print(round(10.3)) # 10

# 字符ASCII码运算
print(ord('a')) # 97

# ASCII码逆运算
print(chr(97)) # a

# 算术运算
A = 3
B = 4
print(A+B, A-B, A*B, A/B, A//B, A % B, A**B) # 7 -1 12 0.75 0 3 81

# 复数运算
print(1.2e+2, 1.2e+2j, 1.2e+2+3j) # 120.0 120j (120+3j)

# 逻辑运算返回布尔值、布尔运算
print(A > B and not A >= B, A>B or A
# 0、None、空字符串返回False
print(bool(0), bool(None), bool("")) # False False False

# 非0数值和非空字符串返回True
print(bool(1), bool(-1), bool("bool")) # True True True
/<code>

原型类或者方法:

<code>def bin(*args, **kwargs):
Return the binary representation of an integer.
#返回整数的二进制表示形式
#>>> bin(2796202)0b1010101010101010101010
pass

def oct(*args, **kwargs):
Return the octal representation of an integer.
#返回整数的八进制表示形式
#>>> oct(342391) '0o1234567'
pass

def hex(*args, **kwargs):
Return the hexadecimal representation of an integer.
#返回整数的十六进制表示形式
#>>> hex(12648430)0xc0ffee
pass

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 __init__(self, x, base=10):
int(x=0) -> integer
int(x, base=10) -> integer
pass

#1.转化一个数字或者字符串为整型,如果没有参数则返回0。
#2.如果为数字,则调用__init__函数,如果为浮点数则截断小数位为0取整,没有base参数


#3.如果参数x不是数字或者base参数被传入,那么参数x一定要是一个字符串,字节流,或者字节数组
# 并按照给的base去展示数据并转化为整型,base要和前边的进制一致
#4.base默认为10,值的范围【0、2-36】,0意思为解释字符串为整型数据,按照数字去处理


class float(object):
float(x) -> floating point number
Convert a string or number to a floating point number, if possible.
#转化字符串或者数字为浮点型数字
def __init__(self, x):
pass

class complex(object):
complex(real[, imag]) -> complex number
Create a complex number from a real part and an optional imaginary part.
This is equivalent to (real + imag*1j) where imag defaults to 0.
def __int__(self, *args, **kwargs): # real signature unknown
''' int(self) '''
pass

def pow(*args, **kwargs):
Equivalent to x**y (with two arguments) or x**y % z (with three arguments)
Some types, such as ints, are able to use a more efficient algorithm when
invoked using the three argument form.
pass
#等价于x**y 或者传入三个参数

def abs(*args, **kwargs): # real signature unknown
Return the absolute value of the argument.
#返回参数的绝对值
pass

def round(number, ndigits=None):
round(number[, ndigits]) -> number
Round a number to a given precision in decimal digits (default 0 digits).
This returns an int when called with one argument, otherwise the
same type as the number. ndigits may be negative.

return 0

def ord(*args, **kwargs):
Return the Unicode code point for a one-character string.
pass/<code>