数字类型
数据类型¶
x = 4
print(type(x))
Note
打印输出结果为: <class 'int'>
int
表示为整数类型
整数运算类比¶
加¶
x = 4
y = 2
z = x + y
print(z)
print(type(z))
6
<class 'int'>
Note
整数相加的结果还是为整数
减¶
x = 4
y = 2
z = x - y
print(z)
print(type(z))
2
<class 'int'>
Note
整数相减的结果还是为整数
乘¶
x = 4
y = 2
z = x * y
print(z)
print(type(z))
8
<class 'int'>
Note
整数相乘的结果还是为整数
除¶
x = 4
y = 2
z = x / y
print(z)
print(type(z))
2.0
<class 'float'>
Note
整数相除的结果为小数
小数运算类比¶
x = 4.0
y = 2.0
z = x + y
print(type(z))
z = x - y
print(type(z))
z = x * y
print(type(z))
z = x / y
print(type(z))
<class 'float'>
<class 'float'>
<class 'float'>
<class 'float'>
Note
小数的加减乘除操作结果都为小数
数据类型转换¶
小数转整数¶
x = int(3.5)
print(x)
print(type(x))
整数转小数¶
x = float(3)
# x = 3 * 1.0
print(x)
print(type(x))