A tuple of Python

Source: Internet
Author: User

In this statement of their own level is very limited, with the blog to write in front of the public is really embarrassing in front of everyone, but out of the mix, the skin must be thick!

Python's tuples and lists are very similar, but the elements in the list are mutable objects, and tuples are immutable objects. The output of the list is surrounded by brackets, and the output of the tuple is surrounded by parentheses.

Each element of a tuple is separated by commas, so you can create a tuple with this feature:

u=1,2,3u# (a)

How to create an empty tuple, or a tuple that contains elements

empty= () one= "Hello",  #注意后面的逗号

Package and sequence unpacking of tuples

For example, u=1,2,3,4 is a packaged example.

Unpack

X,y,z= (a)

X=1,y=2,z=3, it is natural that the number of values on the left side is equal to the number of tuple sequences.

Tuple method of tuples, converting a list to a tuple

>>>t=tuple ([1,2,3,4]) >>>t (1,2,3,4)

What does a tuple ("abcdef") get?

The result is (' A ', ' B ', ' C ', ' d ', ' e ')

There is no limit to the data type of elements in a tuple, but it is a number, a string, a tuple, or a list. Although the elements of a tuple are immutable, the elements in the list in the tuple are mutable.

eg

>>> eg= (1,2,3,4,[5,6]) >>> Eg[4]=[7,8]traceback (most recent call last):  File "<pyshell#15 > ", Line 1, in <module>    eg[4]=[7,8]typeerror: ' Tuple ' object does not support item assignment

will produce an error

>>> eg= (1,2,3,4,[5,6]) >>> eg[4].append (7) >>> eg (1, 2, 3, 4, [5, 6, 7])

Because the list is a mutable type.

Tuples and lists can be converted to each other. List () and tuple ()

A tuple of Python

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.