数据类型定义变量的类型。 由于所有内容都是 Python 中的对象,因此数据类型实际上是类; 变量是类的实例。
在任何编程语言中,都可以对不同类型的数据类型执行不同的操作,其中某些数据类型与其他数据类型相同,而某些数据类型可能非常特定于该特定数据类型。
1. Python 中的内置数据类型
Python 默认具有以下内置数据类型。
| 类别 | 数据类型/类名 |
|---|---|
| 文本/字符串 | str |
| 数值 | int,float,complex |
| 列表 | list,tuple,range |
| 映射 | dict |
| 集合 | set,frozenset |
| 布尔值 | bool |
| 二进制 | bytes,bytearray,memoryview |
2.详细的数据类型
2.1 字符串
字符串可以定义为用单引号,双引号或三引号引起来的字符序列。 三引号(""")可用于编写多行字符串。
x = 'A'y = "B"z = """C"""print(x) # prints Aprint(y) # prints Bprint(z) # prints Cprint(x + y) # prints AB - concatenationprint(x*2) # prints AA - repeatition operatorname = str('john') # ConstructorsumOfItems = str(100) # type conversion from int to string
2.2 整数,浮点数,复数
这些是数字类型。 它们是在将数字分配给变量时创建的。
int保留不限长度的有符号整数。float保留浮点精度数字,它们的精度最高为 15 个小数位。complex– 复数包含实部和虚部。
x = 2 # intx = int(2) # intx = 2.5 # floatx = float(2.5) # floatx = 100+3j # complexx = complex(100+3j) # complex
2.3 列表,元组,范围
在 Python 中,列表是某些数据的有序序列,使用方括号([ ])和逗号(,)编写。 列表可以包含不同类型的数据。
切片运算符[:]可用于访问列表中的数据。
连接运算符(+)和重复运算符(*)的工作方式类似于str数据类型。
使用切片运算符可以将范围视为sublist,从list中取出。
元组与list类似,但tuple是只读数据结构,我们无法修改元组项的大小和值。 另外,项目用括号(, )括起来。
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 对象。
字典中的项目用逗号分隔并括在花括号{, }中。
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 aprint (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的元素。
冻结集是正常集的不变形式。 这意味着我们无法删除任何项目或将其添加到冻结集中。
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 linesdigits.remove(0) # allowed in normal setprint(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。
x = Truey = Falseprint(x) #Trueprint(y) #Falseprint(bool(1)) #Trueprint(bool(0)) #False
2.7 字节,字节数组,内存视图
字节和字节数组用于处理二进制数据。 内存视图使用缓冲区协议访问其他二进制对象的内存,而无需进行复制。
字节对象是单个字节的不变序列。 仅在处理与 ASCII 兼容的数据时,才应使用它们。
bytes字面值的语法与string字面值相同,只是添加了'b'前缀。
始终通过调用构造器bytearray()来创建bytearray对象。 这些是可变的对象。
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()函数可用于获取任何对象的数据类型。
x = 5print(type(x)) # <class 'int'>y = 'howtodoinjava.com'print(type(y)) # <class 'str'>
将您的问题留在我的评论中。
学习愉快!
参考: Python 文档
