Home > Article > Backend Development > What are the similarities and differences between Python lists and tuples
Same points: It belongs to an ordered sequence, can be forced to convert, use len() to count the number of elements, and use count() to count the number of occurrences of a specified element. The differences: 1. The tuple type is tuple, and the list type is list; 2. Tuples cannot be modified, while lists can modify element values based on indexes; 3. Lists can delete elements, but tuples cannot.
The operating environment of this tutorial: Windows 10 system, python3.9 version, Dell G3 computer.
Recommended learning: Python video tutorial
1. Same Point
(1) has the same index, which is 0~~n-1 from left to right.
(2) The splicing is the same and can be spliced with " ".
(3) The counts are the same. They use the len() function to count the number of elements and the count() function to count the number of occurrences of the specified element.
(4) are all ordered sequences.
(5) can be deleted using del.
(6) You can use "*" to repeat itself.
(7) can be forced to convert.
(8)The slicing methods are the same.
(9) You can use for loop to perform element traversal, index traversal and enumeration traversal.
(10) Use the index() method to get the index of the specified element.
(11) Use operator in to test whether an element is contained
2. Differences
## 2.1 types are different.
2.2 Modification methods are different
Description | |
---|---|
Delete the element corresponding to the specified index and return the deleted element. If no index is specified, the last element will be deleted by default | |
Remove the first encountered specified element | |
Remove elements or lists, clear the address | |
Remove the list, keep the address, only clear the content | |
Clear the list |
Introduction to Programming! !
The above is the detailed content of What are the similarities and differences between Python lists and tuples. For more information, please follow other related articles on the PHP Chinese website!