Last class also introduced that Yuanzu is an immutable data type, so we have no way to modify, delete and add its internal elements, but the language is so magical that we can't operate on Yuanzu itself. We can also re store the results of Yuanzu's operation into a new Yuanzu, so we can enrich Yuanzu's operation. The method is completely feasible, but it has some limitations. Let's try it.
1, Try to modify Yuanzu
tup1 = ('p', 'y', 't', 'h', 'o', 'n') print(tup1) print(id(tup1)) tup1 = (1, 2, 3, 4, 5, 6, 7, 8, 9) print(tup1) print(id(tup1)) # Not the same as the Yuanzu above tup1[1] = 100 print(tup1)
Return result:
('p', 'y', 't', 'h', 'o', 'n') 31565184 (1, 2, 3, 4, 5, 6, 7, 8, 9) 31465360 TypeError: 'tuple' object does not support item assignment
From the above results, we can see that the memory addresses after the first two assignments are different, so although they are called tup1, they are actually two different Yuanzu. The third one has a type error after we directly modify the element with subscript 1, indicating that the internal elements of Yuanzu cannot be modified.
2, Try to delete Yuanzu and Yuanzu elements
First, let's delete the whole Yuanzu
tup = ('python Video tutorial', "http://www.wakey.com.cn/") print(tup) del tup print(tup)
Return result:
NameError: name 'tup' is not defined
It shows that Yuanzu can be deleted.
Try deleting the elements in Yuanzu again.
tup = ('python Video tutorial', "http://www.wakey.com.cn/") print(tup) del tup print(tup) del tup[1] print(tup)
Return result:
TypeError: 'tuple' object doesn't support item deletion
It can be seen that the elements in Yuanzu cannot be deleted, which proves that Yuanzu is an immutable data type.
3, Yuanzu splicing
Since the elements in the Yuanzu are immutable, let's splice the two Yuanzu and try to return a new Yuanzu.
tup1 = ('p', 'y', 't', 'h', 'o', 'n') tup2 = (1, 2, 3, 4, 5, 6, 7, 8, 9) print(tup1 + tup2) print(tup1) # The original Yuanzu will not change, indicating that Yuanzu cannot change print(tup2) print(tup2 * 3) print(tup2)
Return result:
('p', 'y', 't', 'h', 'o', 'n', 1, 2, 3, 4, 5, 6, 7, 8, 9) ('p', 'y', 't', 'h', 'o', 'n') (1, 2, 3, 4, 5, 6, 7, 8, 9) (1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9) (1, 2, 3, 4, 5, 6, 7, 8, 9)
It can be seen from the return results that a Yuanzu is indeed returned after splicing, but the original Yuanzu printed remains unchanged, indicating that the returned Yuanzu is a new Yuanzu.
4, Find Yuanzu element
Similar to a list, we can find elements by subscript
tup1 = ('python', 'java', 'php', 'MySql', 'C++', 'C', 'php', 'C#') print(tup1[5]) # Find the sixth element print(tup1[-2]) # Find the penultimate element print(tup1[1:4]) # Find the second to fifth elements print(tup1[4:]) # Find from fifth to last print(tup1[:4]) # Find from 0 to 5 print(tup1[1:6:2]) # From the second to the seventh, query every two print(tup1[::-1]) # Flip Yuanzu print(tup1[5:2:-1]) # Turn Yuanzu from 5 to 2, and you can't get the value of 2
Return result:
C php ('java', 'php', 'MySql') ('C++', 'C', 'php', 'C#') ('python', 'java', 'php', 'MySql') ('java', 'MySql', 'C') ('C#', 'php', 'C', 'C++', 'MySql', 'php', 'java', 'python') ('C', 'C++', 'MySql')
The above is a summary of the operation methods of adding, deleting, changing and checking the Yuanzu elements in python. After reading this article, you should try to practice more to increase your impression, otherwise you will forget after reading the next second. Learning python or any programming language is not a big fat man. It is a slow accumulation and summary of experience. Knock the code!
Source: www.wakey.com.cn/document-tuple-operate.html