Python数据类型与进制及类型转换

1.Python的基本数据类型

基本数据类型有int float bool str 以及NoneType

其中int表示整数 float表示浮点数 bool包括True和False str表示字符串 NoneType表示空值

2.Python整数进制转换

(1)# int 可以转换为十进制 base表示字符串中的内容是几进制

print(int("19",base=10))

(2)# bin函数可以将十进制转换为二进制

print(bin(10))

(3)# oct函数可以将十进制转换为八进制

print(oct(88))

(4)# hex函数可以将十进制转换为十六进制

print(hex(100))

3.Python数据类型转换函数

(1)Int

# int可以将数字类型的字符串转整数

v = int("10abcdef",base=16)

print(v,type(v))

# int可以将小数转换为整数

v2 = int(3.114)

print(v2,type(v2))

# int可以将bool值转为整数

v3 = int(True)

v4 = int(False)

print(v3,v4)

(2)Float

# float可以将数字类型(最多有一个小数点)的字符串转换为数字

f1 = float("10")

print(f1, type(f1))

f2 = float("10.5")

print(f2, type(f2))

f3 = float(True)

f4 = float(False)

print(f3, f4, type(f3), type(f4))

(3)bool

# 任意类型都可以转换为布尔值

b1 = bool(0)

print(b1, type(b1))

b2 = bool(0.00000001)

print(b2, type(b2))

b3 = bool("False")

print(b3, type(b3))

b4 = bool("")

print(b4, type(b4))

b5 = bool(None)

print(b5, type(b5))

(4)Str

# str 可以将任意类型转换为字符串

s1 = str(1)

print(s1, type(s1))

s2 = str(1.0001)

print(s2, type(s2))

s3 = str(False)

print(s3, type(s3)) # "True"

s4 = str(None)

print(s4, type(s4))