‘’'内置数据类型
在编程中,数据类型是一个重要的概念。
变量可以存储不同类型的数据,并且不同类型可以执行不同的操作。
在这些类别中,Python 默认拥有以下内置数据类型:
‘’’
文本类型: str
test_str = ‘我是str’
print(‘test_str的数据类型是:’, type(test_str))
#test_str的数据类型是: <class ‘str’>
数值类型: int, float, complex
test_int = 20201105
print(‘test_int的数据类型是:’, type(test_int))
#test_int的数据类型是: <class ‘int’>
test_float = 666.222
print(‘test_float的数据类型是:’, type(test_float))
#test_float的数据类型是: <class ‘float’>
test_complex = 4 + 5j
print(‘test_complex的数据类型是:’, type(test_complex))
#test_complex的数据类型是: <class ‘complex’>
序列类型: list, tuple, range
test_list = [‘一’, ‘个’, ‘小’, ‘笨’, ‘egg’, ‘0-0’]
print(‘test_list的数据类型是:’, type(test_list))
#test_list的数据类型是: <class ‘list’>
test_tuple = (‘tuple’, ‘is’, ‘unchangeable’, ‘tuple是不可变的’)
print(‘test_tuple的数据类型是:’, type(test_tuple))
#test_tuple的数据类型是: <class ‘tuple’>
test_range = range(1024)
print(‘test_range的数据类型是:’, type(test_range))
#test_range的数据类型是: <class ‘range’>
映射类型: dict
test_dict = {‘key’: ‘book’, ‘value’: ‘Python大蟒蛇’}
print(‘test_dict的数据类型是:’, type(test_dict))
#test_dict的数据类型是: <class ‘dict’>
集合类型: set, frozenset
test_set = {‘我是’, ‘小’, ‘八哥’}
print(‘test_set的数据类型是:’, type(test_set))
#test_set的数据类型是: <class ‘set’>
test_frozenset = frozenset({‘frozenset’, ‘比’, ‘set’, ‘多一组括号!’})
print(‘test_frozenset的数据类型是:’, type(test_frozenset))
#test_frozenset的数据类型是: <class ‘frozenset’>
布尔类型: bool
test_bool = False
print(‘双性人test_bool的数据类型是:’, type(test_bool))
#双性人test_bool的数据类型是: <class ‘bool’>
二进制类型: bytes, bytearray, memoryview
test_bytes = b’I am bytes’
print(‘test_bytes的数据类型是:’, type(test_bytes))
#test_bytes的数据类型是: <class ‘bytes’>
test_bytearray = bytearray(2)
print(test_bytearray)
print(‘test_bytearray的数据类型是:’, type(test_bytearray))
#bytearray(b’\x00\x00’)
#test_bytearray的数据类型是: <class ‘bytearray’>
test_memoryview = memoryview(bytes(2))
print(test_memoryview)
print(‘test_memoryview的数据类型是:’, type(test_memoryview))
#<memory at 0x02D5F028>
#test_memoryview的数据类型是: <class ‘memoryview’>