Tuple type built-in method
tuple (Master)
Tuple is an immutable list, that is, the value of tuple cannot be changed. Therefore, tuple is generally only used for the demand of only storing and not fetching. Therefore, tuples can be replaced by lists, so tuples are rarely used in comparison with lists. The advantage of tuple compared with list is: after the value of list is modified, the structure of list will change, while tuple only needs to be stored, so the list needs to occupy more memory to some extent. But at present, industrial memory is no longer a problem, so industrial metagroups are generally not used.
Purpose: multiple equipment, hobbies, courses and even girlfriends
-
Definition: there can be multiple values of any type in (), comma separated elements
my_play_game = ('lol', 'cf', 'qq') print(f"my_play_game: {my_play_game}") my_play_game: ('lol', 'cf', 'qq')
name_str = ('egon') # () is just the meaning of ordinary inclusion name_tuple = ('egon',) print(f"type(name_str): {type(name_str)}") print(f"type(name_tuple): {type(name_tuple)}") type(name_str): <class 'tuple'> type(name_tuple): <class 'tuple'>
Common operation + built-in method: common operation and built-in method:
Priority
-
Index value
name_tuple = ('lol','dnf','cf','qq') print(f'name_list:{name_list[0]}') name_tuple:lol
-
Slice (head, tail, step)
name_tuple = ('lol','dnf','cf','qq') print(f"name_list[1:3:2]:{name_list[1:3:2]}") name_tuple[1:3]:('dnf',)
-
Length len
name_tuple = ('lol','dnf','cf','qq') print(f'len(name_tuple):{len(name_tuple)}') len(name_tuple):4
-
Member operations in and not in
name_tuple = ('lol','dnf','cf','qq') print(f"dnf in name_tuple:{'dnf' in name_tuple}") print(f"dnf not in name_tuple:{'dnf'not in name_tuple}") dnf in name_tuple:True dnf not in name_tuple:False
-
loop
name_tuple = ('lol','dnf','cf','qq') for i in name_tuple : print(f'name_tple:{i}') name_tple:lol name_tple:dnf name_tple:cf name_tple:qq
-
count gets the value of the specified element
name_tuple = ('lol','dnf','cf','qq') print(f"name_tuple.count('cf'):{name_tuple.count('cf')}") name_tuple.count('cf'):1
-
Index gets the index value of the specified element
name_tuple = ('lol','dnf','cf','qq') print(f"name_tuple.index('qq'):{name_tuple.index('qq')}") name_tuple.index('qq'):3
Save one value or multiple values: one value
-
Order or disorder: order
name_tuple = ('jin',) print(f'id(name_tuple):{id(name_tuple)}') id(name_tuple):30399008
-
Variable or immutable: immutable data type
n = ['a','b','c'] print(f'id(n[0]):{id(n[0])}') n[0] = 'd' print(f'id(n[0]):{id(n[0])}') id(n[0]):31019392 id(n[0]):30591272
The reason why the list can be changed is that the memory address of the value corresponding to the index can be changed
The reason why tuples can't be changed is that the memory address of the value corresponding to the index can't be changed, or conversely, as long as the memory address of the value corresponding to the index hasn't been changed, then tuples will never be changed.
n1 = ('a','b','c',['d','e','f']) print(f'id(n1[0]):{id(n1[0])}') print(f'id(n1[0]):{id(n1[1])}') print(f'id(n1[0]):{id(n1[2])}') print(f'id(n1[0]):{id(n1[3])}') n1[3][0] = 'D' print(f'n1[3][0]:{n1[3][0]}') print(f'id(n1[3]:{id(n1[3])}') print(f'n1:{n1}') id(n1[0]):6574464 id(n1[0]):6556336 id(n1[0]):6144888 id(n1[0]):31868872 n1[3][0]:D id(n1[3]:31868872 n1:('a', 'b', 'c', ['D', 'e', 'f'])