不可变数据类型:变量所向的内存地址处的值是不可以被改变的。包括:数字类型(整数类型、布尔类型),字符串类型,元组类型
# <1>.数字类型
num0 = 10
print('num0:', id(num0))
num1 = 10
print('num1:', id(num1))
num2 = 10
print('num2:', id(num2))
print(num0 == num1 == num2)
num3 = 20
print('num3:', id(num3))
print(num0 == num1 == num2 == num3)
bool0 = True
print('bool0:', id(bool0))
bool1 = True
print('bool1:', id(bool1))
bool2 = True
print('bool2:', id(bool2))
print(bool0 == bool1 == bool2)
bool3 = False
print('bool3:', id(bool3))
print(bool0 == bool1 == bool2 == bool3)
# <2>.字符串类型
str0 = 'hello, world'
print('str0:', id(str0))
str1 = 'hello, world'
print('str1:', id(str1))
str2 = 'hello, world'
print('str2:', id(str2))
print(str0 == str1 == str2)
str3 = 'hello, world!'
print('str3:', id(str3))
print(str0 == str1 == str2 == str3)
# <3>.元组类型
tuple0 = (1, 2, 3)
print('tuple0:', id(tuple0))
tuple1 = (1, 2, 3)
print('tuple1:', id(tuple1))
tuple2 = (1, 2, 3)
print('tuple2:', id(tuple2))
print(tuple0 == tuple1 == tuple2)
tuple3 = (1, 2, 2)
print('tuple3:', id(tuple3))
print(tuple0 == tuple1 == tuple2 == tuple3)
结论:对于不可变数据类型,不同的变量赋值相同的值,在内存中地址是相同的,该值内存中的地址是不可变的。
可变数据类型:变量所向的内存地址处的值是可以被改变的。包括:列表类型、字典类型
# <1>.列表类型
list0 = [1, 2, 3, 4]
print('list0:', list0, id(list0))
list0.append(5)
print('list0:', list0, id(list0))
list1 = [1, 2, 3, 4]
print('list1:', list1, id(list1))
list1.append(6)
print('list1:', list1, id(list1))
list2 = [1, 2, 3, 4]
print('list2:', list2, id(list2))
list2.append(7)
print('list2:', list2, id(list2))
"""
list0: [1, 2, 3, 4] 2259413721608
list0: [1, 2, 3, 4, 5] 2259413721608
list1: [1, 2, 3, 4] 2259413721672
list1: [1, 2, 3, 4, 6] 2259413721672
list2: [1, 2, 3, 4] 2259414211528
list2: [1, 2, 3, 4, 7] 2259414211528
"""
# <2>.字典类型
dict0 = {'code': '001', 'name': 'tom'}
print('dict0:', dict0, id(dict0))
dict0.update({'sex': 'male'})
print('dict0:', dict0, id(dict0))
dict1 = {'code': '001', 'name': 'tom'}
print('dict1:', dict1, id(dict1))
dict1.update({'sex': 'male'})
print('dict1:', dict1, id(dict1))
dict2 = {'code': '001', 'name': 'tom'}
print('dict2:', dict2, id(dict2))
dict2.update({'sex': 'male'})
print('dict2:', dict2, id(dict2))
"""
dict0: {'code': '001', 'name': 'tom'} 2259414181784
dict0: {'code': '001', 'name': 'tom', 'sex': 'male'} 2259414181784
dict1: {'code': '001', 'name': 'tom'} 2259414181856
dict1: {'code': '001', 'name': 'tom', 'sex': 'male'} 2259414181856
dict2: {'code': '001', 'name': 'tom'} 2259414182216
dict2: {'code': '001', 'name': 'tom', 'sex': 'male'} 2259414182216
"""
结论:对于可变数据类型,每次声明变量都会开辟新的内存地址,但是变量所指向的内存地址处的值是可以被变的,值的变化并不会引起新建对象,即地址是不会变的。
# 内建函数(BIF)
var = 'hello, world'
# 变量类型
var_type = type(var)
print(var_type)
# 类型判断
is_type = isinstance(var, int)
print(is_type)
is_type = isinstance(var, str)
print(is_type)
# 变量属性
var_dir = dir(var)
print(var_dir)
"""
<class 'str'>
False
True
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
"""
非连续式数据
连续式数据:元素是连续存放的,除了第一个前面没有元素,最后一个后面没有元素。支持索引访问和切片操作。包括:字符串(String)、列表(List)、元组(Tuple)
连续式数据
非连续式数据:元素不是连续存放的,任意元素的前后都有可能没有元素,不支持索引和切片操作。包括:字典(Dictionary)、集合(Sets)
# 字符串/元组/列表索引取值
# 字符串索引
var = 'hello, world!'
var_0 = var[0]
print(var_0)
var_3 = var[3]
print(var_3)
# 元组索引
var = (1, 2, 3, 4, 5, 6)
var_0 = var[0]
print(var_0)
var_3 = var[3]
print(var_3)
# 列表索引
var = [1, 2, 3, 4, 5, 6]
var_0 = var[0]
print(var_0)
var_3 = var[3]
print(var_3)
# 字符串/元组/列表类型转换
# str--->tuple/list
str_var = 'hello,world'
tup_var = tuple(str_var)
lis_var = list(str_var)
# tuple--->str/list
tuple_var = (1, 2, 3, 4)
str_var = str(tuple_var)
lis_var = list(tuple_var)
# list--->str/tuple
list_var = [1, 2, 3, 4, 5, 6]
str_var = str(list_var)
tup_var = tuple(list_var)
# 字符串/元组/列表内置方法
str_var = 'hello,world'
# 字符串长度
str_len = len(str_var)
# 字符串反转(reversed + str.join(seq))
str_rev = ''.join(reversed(str_var))
tuple_var = (1, 2, 3, 4)
# 元组长度
tuple_len = len(tuple_var)
# 元组反转
tuple_rev = tuple(reversed(tuple_var))
# 元组求和
tuple_sum = sum(tuple_var)
list_var = [1, 2, 3, 4, 5, 6]
# 列表长度
list_len = len(list_var)
# 列表反转
list_rev = list(reversed(list_var))
# 列表求和
list_sum = sum(list_var)
# 可迭代序列>(索引, 数据)
for index, value in tuple_var:
print(index, value)
for index, value in list_var:
print(index, value)
for tup in zip(tuple_var):
print(tup)
for tup in zip(list_var)
print(tup)




浙公网安备 33010602011771号