常见数据类型

Python for Developers 入门

Jasmin Ludolf

Senior Data Science Content Developer

小数类型

  • 整数(Integer):不含小数

$$

  • 浮点数(Float):含小数,如 1.5
  • 不需要引号

$$

$$

new_ingredient_quantity = 1.5
Python for Developers 入门

布尔类型

$$

  • 布尔值(Boolean):True 或 False
  • 适合存储是/否信息
  • 首字母大写
  • 不加引号

$$

is_in_stock = True
is_in_stock = False
Python for Developers 入门

type 函数

  • 数据类型:
    • 字符串
    • 整数
    • 浮点数
    • 布尔值
type()
  • 返回数据类型
Python for Developers 入门

type 函数

字符串

ingredient_name = "tomatoes"

print(type(ingredient_name))
<class 'str'>

整数

ingredient_quantity = 2

print(type(ingredient_quantity))
<class 'int'>

浮点数

new_ingredient_quantity = 1.5

print(type(new_ingredient_quantity))
<class 'float'>

布尔值

is_in_stock = True

print(type(is_in_stock))
<class 'bool'>
Python for Developers 入门

理解数据类型

  • 理解代码做什么以及原因
  • type() 判断类型
  • 确定可执行的运算

多人屏幕上编写代码的人

1 图片来源:wutzkoh
Python for Developers 入门

运算符

  • 使用称为运算符的符号进行计算

算术运算符:

  • + 加法
  • - 减法
  • * 乘法
  • / 除法

数字:

  • 与常规数学一致
    print(2 + 1.5)
    
3.5

字符串:

  • 仅支持 +*

"Hi" + "There" = "HiThere"

"Hi" * 3 = "HiHiHi"

Python for Developers 入门

让我们练习吧!

Python for Developers 入门

Preparing Video For Download...