A detailed and practical example of the tuple sequence structure in Python language

Source: Internet
Author: User
Sequences are often used in the design of data storage, almost every programming language provides tabular data structures, such as C and basic in a one-dimensional, multidimensional arrays and so on. The sequence types provided by the Python language are the richest, most flexible, and most powerful in all programming languages.

A sequence is a series of contiguous values, which are usually related and arranged in a certain order. The sequence structures commonly used in Python are lists, tuples, and so on.

Tuples and lists are similar, but they are immutable, and once created, they cannot be modified by any means.

Tuples are defined in the same way as lists, but when defined, all elements are placed in a pair of parentheses "(" and ")" instead of in 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,)

Converting other sequences to tuples using the tuple function

>>> 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)

You cannot delete a tuple element by using Del to delete a tuple object

The difference between a tuple and a list

Once the data in the tuple is defined, it is not allowed to change.

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

The tuple does not have a remove () or pop () method, and it cannot perform a del operation on a tuple element, and cannot delete an element from a tuple.

The built-in tuple () function takes a list parameter and returns a tuple containing the same element, while the list () function takes a tuple parameter and returns a list. From the effect, the tuple () freezes the list, and the list () melts the tuple.

The advantages of tuples

Tuples are faster than lists. If you define a series of constant values, and all you need to do is traverse it, you generally use tuples instead of lists.

Tuples "write-protected" data that does not need to be changed will make the code more secure.

Some tuples can be used as dictionary keys (especially tuples that contain immutable data such as strings, numeric values, and other tuples). The list can never be used as a dictionary key because the list is not immutable.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.