企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# Python – 数据类型 > 原文: [https://howtodoinjava.com/python/python-data-types/](https://howtodoinjava.com/python/python-data-types/) 数据类型定义变量的类型。 由于所有内容都是 [Python](https://howtodoinjava.com/python-tutorial/) 中的对象,因此**数据类型**实际上是类; 变量是类的实例。 在任何编程语言中,都可以对不同类型的数据类型执行不同的操作,其中某些数据类型与其他数据类型相同,而某些数据类型可能非常特定于该特定数据类型。 ## 1\. Python 中的内置数据类型 Python 默认具有以下内置数据类型。 | 类别 | 数据类型/类名 | | --- | --- | | **文本/字符串** | [`str`](https://howtodoinjava.com/python/python-strings/) | | **数值** | [`int`](https://howtodoinjava.com/python/python-integer-ints/),`float`,`complex` | | **列表** | [`list`](https://howtodoinjava.com/python/python-lists/),[`tuple`](https://howtodoinjava.com/python/python-tuples/),`range` | | **映射** | `dict` | | **集合** | `set`,`frozenset` | | **布尔值** | `bool` | | **二进制** | `bytes`,`bytearray`,`memoryview` | ## 2.详细的数据类型 #### 2.1 字符串 字符串可以定义为用单引号,双引号或三引号引起来的字符序列。 三引号(`"""`)可用于编写多行字符串。 ```py x = 'A' y = "B" z = """ C """ print(x) # prints A print(y) # prints B print(z) # prints C print(x + y) # prints AB - concatenation print(x*2) # prints AA - repeatition operator name = str('john') # Constructor sumOfItems = str(100) # type conversion from int to string ``` #### 2.2 整数,浮点数,复数 这些是数字类型。 它们是在将数字分配给变量时创建的。 * `int`保留不限长度的有符号整数。 * `float`保留浮点精度数字,它们的精度最高为 15 个小数位。 * `complex` – 复数包含实部和虚部。 ```py x = 2 # int x = int(2) # int x = 2.5 # float x = float(2.5) # float x = 100+3j # complex x = complex(100+3j) # complex ``` #### 2.3 列表,元组,范围 在 Python 中,**列表**是某些数据的**有序序列**,使用方括号(`[ ]`)和逗号(`,`)编写。 列表可以**包含不同类型**的数据。 **切片运算符**`[:]`可用于访问列表中的数据。 连接运算符(`+`)和重复运算符(`*`)的工作方式类似于`str`数据类型。 使用切片运算符可以将**范围**视为`sublist`,从`list`中取出。 **元组**与`list`类似,但`tuple`是只读数据结构,我们无法修改元组项的大小和值。 另外,项目用括号`(, )`括起来。 ```py randomList = [1, "one", 2, "two"] print (randomList); # prints [1, 'one', 2, 'two'] print (randomList + randomList); # prints [1, 'one', 2, 'two', 1, 'one', 2, 'two'] print (randomList * 2); # prints [1, 'one', 2, 'two', 1, 'one', 2, 'two'] alphabets = ["a", "b", "c", "d", "e", "f", "g", "h"] print (alphabets[3:]); # range - prints ['d', 'e', 'f', 'g', 'h'] print (alphabets[0:2]); # range - prints ['a', 'b'] randomTuple = (1, "one", 2, "two") print (randomTuple[0:2]); # range - prints (1, 'one') randomTuple[0] = 0 # TypeError: 'tuple' object does not support item assignment ``` #### 2.4 字典 **字典**或字典是项的键值对的**有序集合**。 键可以保存任何原始数据类型,而值是任意的 Python 对象。 字典中的项目用逗号分隔并括在花括号`{, }`中。 ```py charsMap = {1:'a', 2:'b', 3:'c', 4:'d'}; print (charsMap); # prints {1: 'a', 2: 'b', 3: 'c', 4: 'd'} print("1st entry is " + charsMap[1]); # prints 1st entry is a print (charsMap.keys()); # prints dict_keys([1, 2, 3, 4]) print (charsMap.values()); # prints dict_values(['a', 'b', 'c', 'd']) ``` #### 2.5 集,`frozenset` 可以将 python 中的**集**定义为大括号`{, }`内各种项目的**无序集**。 集**的元素不能重复**。 python 集**的元素必须是不可变的**。 与`list`不同,集合元素没有`index`。 这意味着我们只能遍历`set`的元素。 **冻结集**是正常集的不变形式。 这意味着我们无法删除任何项目或将其添加到冻结集中。 ```py digits = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} print(digits) # prints {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} print(type(digits)) # prints <class 'set'> print("looping through the set elements ... ") for i in digits: print(i) # prints 0 1 2 3 4 5 6 7 8 9 in new lines digits.remove(0) # allowed in normal set print(digits) # {1, 2, 3, 4, 5, 6, 7, 8, 9} frozenSetOfDigits = frozenset({0, 1, 2, 3, 4, 5, 6, 7, 8, 9}) frozenSetOfDigits.remove(0) # AttributeError: 'frozenset' object has no attribute 'remove' ``` #### 2.6 布尔值 布尔值是两个常量对象`False`和`True`。 它们用于表示真值。 在数字上下文中,它们的行为分别类似于整数 0 和 1。 ```py x = True y = False print(x) #True print(y) #False print(bool(1)) #True print(bool(0)) #False ``` #### 2.7 字节,字节数组,内存视图 **字节**和**字节数组**用于处理二进制数据。 **内存视图**使用缓冲区协议访问其他二进制对象的内存,而无需进行复制。 字节对象是单个字节的**不变**序列。 仅在处理与 ASCII 兼容的数据时,才应使用它们。 `bytes`字面值的语法与`string`字面值相同,只是添加了`'b'`前缀。 始终通过调用构造器`bytearray()`来创建`bytearray`对象。 这些是**可变的**对象。 ```py x = b'char_data' x = b"char_data" y = bytearray(5) z = memoryview(bytes(5)) print(x) # b'char_data' print(y) # bytearray(b'\x00\x00\x00\x00\x00') print(z) # <memory at 0x014CE328> ``` ## 3\. `type()`函数 `type()`函数可用于获取任何对象的数据类型。 ```py x = 5 print(type(x)) # <class 'int'> y = 'howtodoinjava.com' print(type(y)) # <class 'str'> ``` 将您的问题留在我的评论中。 学习愉快! 参考: [Python 文档](https://docs.python.org/3/library/stdtypes.html)