上一篇
Python基本数据类型快速指南 - 三分钟掌握核心概念
- Python
- 2025-07-16
- 446
Python基本数据类型快速指南
三分钟掌握Python核心数据类型
Python数据类型简介
Python有7种基本数据类型,理解这些类型是掌握Python编程的基础。每种数据类型都有特定的用途和操作方法。
1 数字类型
-
整数 (int)
表示整数值,如年龄、数量等
age = 25 count = -10
-
浮点数 (float)
表示带有小数点的数字
price = 19.99 temperature = -3.5
2 文本和布尔类型
-
字符串 (str)
表示文本数据,用单引号或双引号包裹
name = "Alice" message = 'Hello, Python!'
-
布尔值 (bool)
表示真(True)或假(False)
is_active = True has_permission = False
3 集合类型
-
列表 (list)
有序可变集合,用方括号表示
fruits = ["apple", "banana", "cherry"] numbers = [1, 2, 3, 4, 5]
-
元组 (tuple)
有序不可变集合,用圆括号表示
coordinates = (10.5, 20.3) colors = ("red", "green", "blue")
4 字典类型
-
字典 (dict)
键值对集合,用花括号表示
person = { "name": "Bob", "age": 30, "city": "New York" }
数据类型检查
使用type()函数可以检查任何变量的数据类型:
print(type(10)) # <class 'int'> print(type(3.14)) # <class 'float'> print(type("Hello")) # <class 'str'> print(type(True)) # <class 'bool'> print(type([1,2,3])) # <class 'list'> print(type((1,2))) # <class 'tuple'> print(type({"a":1})) # <class 'dict'>
Python数据类型要点总结
可变性
- 可变类型:列表、字典、集合
- 不可变类型:整数、浮点数、字符串、元组
何时使用
- 列表:有序项目集合
- 元组:不可变有序集合
- 字典:键值对存储
转换方法
- int() - 转为整数
- float() - 转为浮点数
- str() - 转为字符串
- list() - 转为列表
本文由MingHui于2025-07-16发表在吾爱品聚,如有疑问,请联系我们。
本文链接:https://www.521pj.cn/20255731.html
发表评论