Python 的基本数据类型及其转换 整数的进制转换
1.Python的基本数据类型:
(1)整数型int:
#声明一个整数 <class 'int'>
num = 0
print(num, type(num))
(2)浮点型float:
#声明一个浮点数 <class 'float'>
f1 = -3.1415
f1_type = type(f1)
print(f1, f1_type)
(3)字符串str:
# 字符串
#声明一个字符串 <class 'str'>
s1 = "hello world"
print(s1, type(s1))
s2 = 'hello world'
print(s2, type(s2))
#带有格式的字符串
s3 = """hello
world"""
print(s3, type(s3))
s4 = '''hello
world'''
print(s4, type(s4))
# 单引号 双引号要交替使用
s5 = 'he"llo world+-*/!@#$%^&*()_'
print(s5)
s6 = "he'llo world¥%……&&**("
print(s6)
(4)布尔值boor:
#声明布尔值变量 <class 'bool'>
b1 = True
print(b1, type(b1))
b2 = False
print(b2, type(b2))
(5)空值none:
#空值 <class 'NoneType'>
#声明一个变量 但是又不存储任何数据
n = None
print(n, type(n))
2.Python整数进制转换
(1).四种进制在python中的输出形式:
定义10进制变量
# 数组有0 1 2 3 4 5 6 7 8 9
# 逢十进一
i = 100
print(i, type(i))
# 定义2进制变量 binary
# 以前缀0b开头 数字有0 1
# 逢二进一
b = 0b110011
print(b, type(b))
# 定义16进制变量
# 以0x前缀 数字0-9 字母a-f
# 逢十六进一
h = 0xf11a
print(h, type(h))
# 定义8进制变量
# 以0o前缀 数字0-7
# 逢八进一
o = 0o54
print(o, type(o))
(2).进制之间的转换:
(1).转换成10进制数:从右侧数第n个数字m数值为m*底数的n-1次方
(2).进制转换函数
int 可以转换为10进制 base表示 字符串中内容是几进制
print(int("19", base=10))
print(int("101", base=2))
print(int("17", base=8))
print(int("a1", base=16))
bin函数可以将10进制转2进制
print(bin(10))
oct函数可以将10进制转换为8进制
print(oct(10))
hex函数可以将10进制转换为16进制
print(hex(10))
3.数据类型转换函数
数据类型有以下几种常见形式:int float bool str NoneType
