Home  >  Article  >  Backend Development  >  What are the similarities and differences between Python lists and tuples

What are the similarities and differences between Python lists and tuples

青灯夜游
青灯夜游Original
2021-01-14 11:19:4816327browse

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.

What are the similarities and differences between Python lists and tuples

The operating environment of this tutorial: Windows 10 system, python3.9 version, Dell G3 computer.

Recommended learning: Python video tutorial

The similarities and differences between Python lists and tuples

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.

    The tuple type is: tuple
  • The list type is: list

2.2 Modification methods are different

    Tuples cannot be modified
  • Lists can modify element values ​​based on index

2.3 Different deletion methods

  • The list uses functions such as pop() to delete elements

Function nameDescriptionpop()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 defaultremove()Remove the first encountered specified elementdelRemove elements or lists, clear the addressclear()Remove the list, keep the address, only clear the contentlist=[]Clear the list
    Tuples cannot delete elements, only use del deletes the entire tuple.
2.4 Different search methods

    Tuples can only be viewed using the Index() function.
  • The list can only be viewed using the Index() function.
For more programming-related knowledge, please visit:

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn