98 lines
5.3 KiB
Markdown
98 lines
5.3 KiB
Markdown
|
|
# Python 数据类型
|
|||
|
|
|
|||
|
|
> 原文:[https://python.land/python-data-types](https://python.land/python-data-types)
|
|||
|
|
|
|||
|
|
在这一节中,我们仔细看看最重要的 Python 数据类型。Python 为我们提供了几种原生数据类型来存储和处理数据。这些都是你需要很好了解的基本构件。当考虑一个问题时,解决方案的一部分通常是选择正确的数据类型。了解每种数据类型的能力将使选择正确的数据类型变得容易得多。
|
|||
|
|
|
|||
|
|
目录
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
* [基本和高级 Python 数据类型](#Basic_and_advanced_Python_data_types "Basic and advanced Python data types")
|
|||
|
|
* [Python 中的可变性](#Mutability_in_Python "Mutability in Python")
|
|||
|
|
* [如何检查 Python 数据类型?](#How_to_check_the_Python_data_type "How to check the Python data type?")
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
## 基本和高级 Python 数据类型
|
|||
|
|
|
|||
|
|
我们区分基本类型和更高级的数据结构。Python 中的基本数据类型存储单个值,比如一个数字或一段文本。Python 中的基本数据类型有:
|
|||
|
|
|
|||
|
|
* [整数](https://python.land/python-datatypes/python-integer)
|
|||
|
|
* 浮点数
|
|||
|
|
* 复数
|
|||
|
|
* [布尔型](https://python.land/introduction-to-python/booleans-and-conditionals)
|
|||
|
|
* [琴弦](https://python.land/introduction-to-python/strings)
|
|||
|
|
|
|||
|
|
接下来,我们有更高级的 Python 数据类型。它们可以存储许多项目,如项目列表或键值对:
|
|||
|
|
|
|||
|
|
* 元组
|
|||
|
|
* [列表](https://python.land/python-data-types/python-list)
|
|||
|
|
* [范围](https://python.land/deep-dives/python-range)
|
|||
|
|
* [字典](https://python.land/python-datatypes/dictionaries)
|
|||
|
|
* [设置](https://python.land/python-data-types/python-set)
|
|||
|
|
|
|||
|
|
这些类型都有与众不同的特征。例如,范围可以快速有效地计算,元组不能修改(而列表可以),集合允许您进行数学集合计算。
|
|||
|
|
|
|||
|
|
## Python 中的可变性
|
|||
|
|
|
|||
|
|
Python 数据类型可以分为两类:**可变的**和**不可变的**。或者更准确地说:可洗的和不可洗的。如果我们可以改变一个对象持有的数据,那么它就是可变的,如果我们不能改变它,那么它就是不可变的。Python 中不可变数据类型的示例有:
|
|||
|
|
|
|||
|
|
* 所有数字(整数、浮点数、复数)
|
|||
|
|
* [布尔型](https://python.land/introduction-to-python/booleans-and-conditionals)
|
|||
|
|
* [琴弦](https://python.land/introduction-to-python/strings)
|
|||
|
|
* 元组
|
|||
|
|
|
|||
|
|
可变的 Python 数据类型有:
|
|||
|
|
|
|||
|
|
* [列表](https://python.land/python-data-types/python-list)
|
|||
|
|
* [字典](https://python.land/python-data-types/dictionaries)
|
|||
|
|
* [集](https://python.land/python-data-types/python-set)
|
|||
|
|
|
|||
|
|
### 为什么以及什么时候数据类型是可变的?
|
|||
|
|
|
|||
|
|
我们还没有深入研究所有这些类型,但是让我们以一个列表为例。在不知道具体细节的情况下,我可以告诉你,你可以在列表中添加更多的项目,删除项目,并替换它们。对于一个列表来说,这些并不奇怪,对吗?所以一个列表是可以改变的;因此它是可变的。
|
|||
|
|
|
|||
|
|
Thank you for reading my tutorials. I write these in my free time, and it requires a lot of time and effort. I use ads to keep writing these *free* articles, I hope you understand! **Support me by disabling your adblocker on my website** or, alternatively, **[buy me some coffee](https://www.buymeacoffee.com/pythonland)**. It's much appreciated and allows me to keep working on this site!
|
|||
|
|
|
|||
|
|
然而,整数只是一个数字,就像 2、3 和 4 一样。你不能改变一个数字;事情就是这样。我几乎能听到你现在在想什么。"但是我可以改变一个 [Python 变量](https://python.land/introduction-to-python/variable),即使它是一个整数!"你是对的,但是那是不同的东西。
|
|||
|
|
|
|||
|
|
让我们看一个例子,我们将字符串“hello”赋给一个名为`mystring`的变量,然后更改它:
|
|||
|
|
|
|||
|
|
```py
|
|||
|
|
>>> mystring = 'hello'
|
|||
|
|
>>> mystring = 'world'
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
我们现在做的是给一个变量重新赋值。我们没有改变字符串“hello”本身。
|
|||
|
|
|
|||
|
|
还有另外一种解释。一个变量指向计算机内存中的一个点。这就是我们所说的指针。在第一个实例中,`mystring` 指向内存中存储字符串“hello”的位置将`mystring`改为“world”后,它指向内存中存储单词“world”的另一个位置。我们没有改变字符串“hello”。'
|
|||
|
|
|
|||
|
|
事实上,我们可以通过执行以下操作来证明这一点:
|
|||
|
|
|
|||
|
|
```py
|
|||
|
|
>>> mystring = 'hello'
|
|||
|
|
>>> mycopy = mystring
|
|||
|
|
>>> mystring = 'world'
|
|||
|
|
>>> print(mycopy)
|
|||
|
|
'hello'
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
我们创建了指向字符串“hello”的第二个变量。当我们将`mystring`改为指向一个不同的字符串时,我们仍然可以引用之前的字符串,因为`mycopy`也指向‘hello’字符串在内存中的位置。
|
|||
|
|
|
|||
|
|
例如,这不同于列表。如果变量`mylist`指向内存中的一个列表结构,而我们改变了那个列表,它仍然指向同一个列表结构。我们所做的只是改变列表结构本身(它的内容)。Python 不替换列表,而是修改它。
|
|||
|
|
|
|||
|
|
## 如何检查 Python 数据类型?
|
|||
|
|
|
|||
|
|
Python 中有一个名为`type`的内置函数,可以用来检查数据类型。让我们来看看`type`在工作中的一些例子:
|
|||
|
|
|
|||
|
|
```py
|
|||
|
|
>>> type(3)
|
|||
|
|
<class 'int'>
|
|||
|
|
>>> type('hello')
|
|||
|
|
<class 'str'>
|
|||
|
|
>>> type([1,2,3])
|
|||
|
|
<class 'list'>
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
如果你正在 [REPL](https://python.land/introduction-to-python/the-repl) 中做实验,`type`是一个有价值的函数,它可以让你更深入地了解引擎下发生了什么!
|