在计算机编程中,数据类型指定了可以存储在变量内部的数据种类。例如,
num = 24
在这里,24(一个整数)被赋给了 num 变量。所以 num 的数据类型是 int
类。
Python 数据类型
数据类型 | 类 | 描述 |
---|---|---|
数值型 | int, float, complex | 持有数值 |
字符串 | str | 持有一系列字符 |
序列 | list, tuple, range | 持有项目的集合 |
映射 | dict | 以键值对形式持有数据 |
布尔型 | bool | 持有 True 或 False |
集合 | set, frozenset | 持有唯一项目的集合 |
由于在 Python 编程中一切皆对象,数据类型实际上是类,而变量是这些类的实例(对象)。
Python 数值数据类型
在 Python 中,数值数据类型用于持有数值。
整数、浮点数和复数都属于Python 数字类别。它们在 Python 中被定义为 int
、float
和 complex
类。
int
- 持有无长度限制的有符号整数。float
- 持有浮点小数,精确到15位小数。complex
- 持有复数。
我们可以使用 type() 函数来了解一个变量或值属于哪个类。
让我们看一个例子,
num1 = 5
print(num1, 'is of type', type(num1))
num2 = 2.0
print(num2, 'is of type', type(num2))
num3 = 1+2j
print(num3, 'is of type', type(num3))
输出
5 is of type <class 'int'> 2.0 is of type <class 'float'> (1+2j) is of type <class 'complex'>
在上面的例子中,我们创建了三个名为 num1、num2 和 num3 的变量,其值分别为 5、5.0 和 1+2j
。
我们还使用了 type()
函数来了解某个变量属于哪个类。
由于,
- 5 是一个整数值,
type()
返回int
作为 num1 的类,即<class 'int'>
- 2.0 是一个浮点数值,
type()
返回float
作为 num2 的类,即<class 'float'>
1 + 2j
是一个复数,type()
返回complex
作为 num3 的类,即<class 'complex'>
Python 列表数据类型
列表(List)是一个有序的、由相似或不同类型的项目组成的集合,项目之间用逗号分隔,并用方括号 [ ]
括起来。例如,
languages = ["Swift", "Java", "Python"]
在这里,我们创建了一个名为 languages 的列表,其中包含 3 个字符串值。
访问列表项
要访问列表中的项目,我们使用索引号 (0, 1, 2 ...)。例如,
languages = ["Swift", "Java", "Python"]
# access element at index 0
print(languages[0]) # Swift
# access element at index 2
print(languages[2]) # Python
在上面的例子中,我们使用索引值来访问 languages 列表中的项目。
languages[0]
- 访问 languages 中的第一个项目,即Swift
languages[2]
- 访问 languages 中的第三个项目,即Python
要了解更多关于列表的信息,请访问 Python 列表。
Python 元组数据类型
元组(Tuple)是一个有序的项目序列,与列表相同。唯一的区别是元组是不可变的。元组一旦创建就不能被修改。
在 Python 中,我们使用圆括号 ()
来存储元组的项目。例如,
product = ('Xbox', 499.99)
在这里,product 是一个元组,包含一个字符串值 Xbox
和一个浮点数值 499.99。
访问元组项
与列表类似,我们使用索引号来访问 Python 中的元组项目。例如,
# create a tuple
product = ('Microsoft', 'Xbox', 499.99)
# access element at index 0
print(product[0]) # Microsoft
# access element at index 1
print(product[1]) # Xbox
要了解更多关于元组的信息,请访问 Python 元组。
Python 字符串数据类型
字符串(String)是由单引号或双引号表示的字符序列。例如,
name = 'Python'
print(name)
message = 'Python for beginners'
print(message)
输出
Python Python for beginners
在上面的例子中,我们创建了字符串类型的变量:name 和 message,其值分别为 'Python'
和 'Python for beginners'
。
要了解更多关于字符串的信息,请访问 Python 字符串。
Python 集合数据类型
集合(Set)是一个无序的、由唯一项目组成的集合。集合由花括号 { }
内用逗号分隔的值定义。例如,
# create a set named student_id
student_id = {112, 114, 116, 118, 115}
# display student_id elements
print(student_id)
# display type of student_id
print(type(student_id))
输出
{112, 114, 115, 116, 118} <class 'set'>
在这里,我们创建了一个名为 student_info 的集合,其中包含 5 个整数值。
由于集合是无序集合,索引没有意义。因此,切片操作符 []
不起作用。
要了解更多关于集合的信息,请访问 Python 集合。
Python 字典数据类型
Python 字典(Dictionary)是一个有序的项目集合。它以键/值对的形式存储元素。
在这里,键是与每个值相关联的唯一标识符。
让我们看一个例子,
# create a dictionary named capital_city
capital_city = {'Nepal': 'Kathmandu', 'Italy': 'Rome', 'England': 'London'}
print(capital_city)
输出
{'Nepal': 'Kathmandu', 'Italy': 'Rome', 'England': 'London'}
在上面的例子中,我们创建了一个名为 capital_city 的字典。这里,
- 键是
'Nepal'
,'Italy'
,'England'
- 值是
'Kathmandu'
,'Rome'
,'London'
使用键访问字典值
我们使用键
来检索相应的值
。但反之则不行。例如,
# create a dictionary named capital_city
capital_city = {'Nepal': 'Kathmandu', 'Italy': 'Rome', 'England': 'London'}
print(capital_city['Nepal']) # prints Kathmandu
print(capital_city['Kathmandu']) # throws error message
在这里,我们使用键从 capital_city 字典中访问了值。
由于 'Nepal'
是键,capital_city['Nepal']
访问其相应的值,即 Kathmandu
然而,'Kathmandu'
是 'Nepal'
键的值,所以 capital_city['Kathmandu']
会抛出错误信息。
要了解更多关于字典的信息,请访问 Python 字典。