Home  >  Article  >  Backend Development  >  Detailed explanation and examples of tuple sequence structure in Python language

Detailed explanation and examples of tuple sequence structure in Python language

高洛峰
高洛峰Original
2017-03-28 16:43:151876browse

Sequence is a data storage method often used in programming. Almost every programming language provides tabular data structures, such as one-dimensional and multi-dimensional arrays in C and Basic, etc. The sequence types provided by the Python language are the richest, most flexible, and most powerful among all programming languages.

A sequence is a series of consecutive values ​​that are usually related and arranged in a certain order. Commonly used sequence structures in Python include lists, tuples, etc.

Tuples are similar to lists, but are immutable sequences. Once a tuple is created, its elements cannot be modified by any method.

Tuples are defined in the same way as lists, but when defined, all elements are placed in a pair of parentheses "(" and ")" instead of square brackets.

Tuple creation and deletion

Use "=" to assign a tuple to a variable

>>>a_tuple= (' a', )

>>> a_tuple

('a',)

>>>a_tuple= ('a', 'b ', 'mpilgrim', 'z', 'example')

>>> a_tuple

('a', 'b', 'mpilgrim', 'z', 'example')

>>> a=3

>>> a

3

>>> a=3,

>>> a

(3,)

Use tuplefunctionConvert other sequences to tuples

>>> print tuple('abcdefg')

('a', 'b', 'c', 'd', 'e', 'f', 'g')

>>> aList

[-1, -4, 6, 7.5, -2.3, 9, -11]

>>> tuple(aList)

(-1, -4, 6, 7.5, -2.3, 9, -11)

Use del to delete tuplesObject, tuple elements cannot be deleted

The difference between tuples and lists

The data in the tuple is not allowed to be changed once it is defined.

Tuples do not have methods such as append(), extend() and insert(), and elements cannot be added to tuples;

Tuples do not have remove() or pop() methods, nor The del operation cannot be performed on the tuple elements, and the elements cannot be deleted from the tuple.

The built-in tuple() function accepts a list parameter and returns a tuple containing the same elements, while the list() function accepts a tuple parameter and returns a list. In effect, tuple() freezes the list, while list() melts the tuple.

Advantages of tuples

Tuples are faster than lists. If a series of constant values ​​are defined and all that needs to be done is to iterate over it, then a tuple is generally used instead of a list.

Tuples that "write-protect" data that does not need to be changed will make the code safer.

Some tuples can be used as dictionary keys (especially tuples containing immutable data like strings, numeric values, and other tuples). Lists can never be used as dictionary keys because lists are not immutable.

The above is the detailed content of Detailed explanation and examples of tuple sequence structure in Python language. 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