目录
整数类型
integer(int)
取值范围:可以表示正数、负数和0
# 定义变量
num1 = 98
num2 = -36
num3 = 0
# 打印输出 type(num1)查看num1的数据类型
print("num1:",num1,type(num1))
print("num2:",num2,type(num2))
print("num3:",num3,type(num3))
运行结果
/Users/xw/PycharmProjects/po_test/venv/bin/python "/Users/xw/Desktop/test/print demo.py"
num1: 98 <class 'int'>
num2: -36 <class 'int'>
num3: 0 <class 'int'>
Process finished with exit code 0
其他进制
# 定义变量
num1 = 0b10101111
num2 = 0o176
num3 = 123
num4 = 0x1EAF
# 整数可以表示为二进制、八进制、十进制、十六进制
print("二进制:",num1) # 175 0b开头,取值范围:1和0
print("八进制:",num2) # 126 0o开头,取值范围:0-7
print("十进制:",num3) # 123 默认进制,取值范围:0-9
print("十六进制:",num4) # 7855 0x开头,取值范围:0-9、a、b、c、d、e、f
# 打印数据类型
print(type(num1))
print(type(num2))
print(type(num3))
print(type(num4))
运行结果
/Users/xw/PycharmProjects/po_test/venv/bin/python "/Users/xw/Desktop/test/print demo.py"
二进制: 175
八进制: 126
十进制: 123
十六进制: 7855
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
Process finished with exit code 0
浮点数类型
float
浮点数包含整数部分和小数部分
a = 3.14159
print(a,type(a))
运行结果
/Users/xw/PycharmProjects/po_test/venv/bin/python "/Users/xw/Desktop/test/print demo.py"
3.14159 <class 'float'>
Process finished with exit code 0
使用浮点数进行存储和计算可能会出现小数位不精确性
num1 = 1.1
num2 = 2.2
# 浮点数存储存在误差和不精确性
print(num1+num2) # 3.3000000000000003
运行结果
/Users/xw/PycharmProjects/po_test/venv/bin/python "/Users/xw/Desktop/test/print demo.py"
3.3000000000000003
Process finished with exit code 0
解决方法
from decimal import Decimal # decimal:十进制的
print(Decimal('1.1')+Decimal('2.2')) # 3.3
运行结果
/Users/xw/PycharmProjects/po_test/venv/bin/python "/Users/xw/Desktop/test/print demo.py"
3.3
Process finished with exit code 0
布尔类型
boolean(bool)
取值范围:True、False
True表示真,False表示假
f1 = True
f2 = False
print(f1,type(f1))
print(f2,type(f2))
运行结果
/Users/xw/PycharmProjects/po_test/venv/bin/python "/Users/xw/Desktop/test/print demo.py"
True <class 'bool'>
False <class 'bool'>
Process finished with exit code 0
布尔值转化为整数计算
True表示1,False表示0
print("True+1:",True+1)
print("False+1:",False+1)
运行结果
/Users/xw/PycharmProjects/po_test/venv/bin/python "/Users/xw/Desktop/test/print demo.py"
True+1: 2
False+1: 1
Process finished with exit code 0
字符串类型
str
又被称为不可变的字符序列
可以使用'单引号'或"双引号"来定义,且定义的字符串必须在一行
可以使用“‘三引号’”来定义,定义的字符串可以在连续的多行
# 单引号
str1 = '人生苦短,我用Python'
# 双引号
str2 = "人生苦短,我用Python"
# 三引号(单引)
str3 = '''人生苦短,
我用Python'''
# 三引号(双引)
str4 = """人生苦短,
我用Python"""
print(str1,type(str1))
print(str2,type(str2))
print("-------------")
print(str3,type(str3))
print("-------------")
print(str4,type(str4))
运行结果
注:三引号输出会根据代码的折行进行折行打印输出
/Users/xw/PycharmProjects/po_test/venv/bin/python "/Users/xw/Desktop/test/print demo.py"
人生苦短,我用Python <class 'str'>
人生苦短,我用Python <class 'str'>
-------------
人生苦短,
我用Python <class 'str'>
-------------
人生苦短,
我用Python <class 'str'>
Process finished with exit code 0
数据类型转换
str()
int、float、bool均可进行str()类型转换
print('将其他的数据类型转换为str,使用str()')
# 定义变量
i = 10
f = 9.8
b = False
# 打印数据类型
print(type(i),type(f),type(b))
# 转换后的数据类型
print(str(i),str(f),str(b),type(str(i)),type(str(f)),type(str(b)))
运行结果
/Users/xw/PycharmProjects/po_test/venv/bin/python "/Users/xw/Desktop/test/print demo.py"
将其他的数据类型转换为str,使用str()
<class 'int'> <class 'float'> <class 'bool'>
10 9.8 False <class 'str'> <class 'str'> <class 'str'>
Process finished with exit code 0
int()
float类型转换int()类型,抹零取整
bool类型转换int()类型,False转换为0,True转换为1
str字符串必须为整数数字串,才可进行int()类型转换,否则报错
print('将其他的数据类型转换为int,使用int()')
# 定义变量
s1 = '32'
s2 = '5.5'
s3 = 'hello'
f = 9.8
b1 = True
b2 = False
# 打印数据类型 <class 'str'> <class 'str'> <class 'str'> <class 'float'> <class 'bool'> <class 'bool'>
print(type(s1),type(s2),type(s3),type(f),type(b1),type(b2))
# 转换后的数据类型
print(int(s1),type(int(s1))) # 将str转换为int类型,且字符串为整数数字
# print(int(s2),type(int(s2))) # 将str转换为int类型,字符串为小数串时报错:ValueError: invalid literal for int() with base 10: '5.5'
# print(int(s3),type(int(s3))) # 将str转换为int类型,字符串为英文字符时报错:ValueError: invalid literal for int() with base 10: 'hello'
print(int(f),type(int(f))) # 将float转换为int类型,截取整数部分,舍掉小数部分
print(int(b1),type(int(b1))) # 将bool类型转换为int类型,False转换为0,True转换为1
print(int(b2),type(int(b2))) # 将bool类型转换为int类型,False转换为0,True转换为1
运行结果
/Users/xw/PycharmProjects/po_test/venv/bin/python "/Users/xw/Desktop/test/print demo.py"
将其他的数据类型转换为int,使用int()
<class 'str'> <class 'str'> <class 'str'> <class 'float'> <class 'bool'> <class 'bool'>
32 <class 'int'>
9 <class 'int'>
1 <class 'int'>
0 <class 'int'>
Process finished with exit code 0
float()
int类型转换float()类型,整数部分末尾添加.0
bool类型转换为float类型,False转为0.0,True转为1.0
str为数字串时可进行float()类型转换,为字符时,不可转换
print('将其他的数据类型转换为float,使用float()')
# 定义变量
s1 = '32.53'
s2 = '98'
s3 = 'hello'
b1 = True
b2 = False
i = 3
# 打印数据类型 <class 'str'> <class 'str'> <class 'str'> <class 'bool'> <class 'bool'> <class 'int'>
print(type(s1),type(s2),type(s3),type(b1),type(b2),type(i))
# 转换后的数据类型
print(float(s1),type(float(s1))) # 将str转换为float类型,str为数字串(小数)
print(float(s2),type(float(s2))) # 将str转换为float类型,str为数字串(整数)
# print(float(s3),type(float(s3))) # 将str转换为float类型,str为英文字符,报错:ValueError: could not convert string to float: 'hello'
print(float(b1),type(float(b1))) # 将bool类型转换为float类型,True转为1.0
print(float(b2),type(float(b2))) # 将bool类型转换为float类型,False转为0.0
print(float(i),type(float(i))) # 将int类型转换为float类型,整数部分后加.0
运行结果
/Users/xw/PycharmProjects/po_test/venv/bin/python "/Users/xw/Desktop/test/print demo.py"
将其他的数据类型转换为float,使用float()
<class 'str'> <class 'str'> <class 'str'> <class 'bool'> <class 'bool'> <class 'int'>
32.53 <class 'float'>
98.0 <class 'float'>
1.0 <class 'float'>
0.0 <class 'float'>
3.0 <class 'float'>
Process finished with exit code 0
str类型和int类型拼接打印输出报错
name = '肖晨'
age = 18
print(type(name),type(age))
print('Hi,我是'+name+',今年'+age+'岁了') # TypeError: can only concatenate str (not "int") to str
运行结果
/Users/xw/PycharmProjects/po_test/venv/bin/python "/Users/xw/Desktop/test/print demo.py"
<class 'str'> <class 'int'>
Traceback (most recent call last):
File "/Users/xw/Desktop/test/print demo.py", line 6, in <module>
print('Hi,我是'+name+',今年'+age+'岁了') # TypeError: can only concatenate str (not "int") to str
TypeError: can only concatenate str (not "int") to str
Process finished with exit code 1
类型转换后,拼接打印输出
name = '肖晨'
age = 18
print(type(name),type(age))
# 打印输出
print('Hi,我是'+name+',今年'+str(age)+'岁了')
# 第二种打印输出方式
print(f'Hi,我是{name},今年{str(age)}岁了')
运行结果
/Users/xw/PycharmProjects/po_test/venv/bin/python "/Users/xw/Desktop/test/print demo.py"
<class 'str'> <class 'int'>
Hi,我是肖晨,今年18岁了
Hi,我是肖晨,今年18岁了
Process finished with exit code 0