数据类型
- 整数(int)
- 浮点数(float)
- 字符串(str)
- 布尔值(bool)
区分数字类型
我们可以使用type()
方法来区分数字类型。
# 字符串
print(type("111"))
print(type("中国"))
# 数字
print(type(1))
# 浮点数
print(type(2.2))
# bool类型
print(type(True))
运行后输出结果如下:
<class 'str'>
<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>
类型转换
如果想将数据进行类型转换,可以使用下面几个操作:
- int()
- str()
- float()
- bool()
# 字符串转成数字
print(int("1"))
# 数字转成字符串
print(str(123))
# 数字转成float
print(float(123))
# 数字转bool
print(bool(999))
输入结果如下:
1
123
123.0
True
变量定义
productPrice = 100
buyCount = 10
buyPrice = productPrice * buyCount
print(buyPrice)
输出结果:
1000