Python: The misleading of tuples and parentheses

Source: Internet
Author: User

The book says that tuples are contained within parentheses and cannot be modified. The list is enclosed in brackets and can be modified.

Lists can be nested lists, tuples can be nested tuples, generally no one mixed, this test is correct:

>>> Aa[0] (aa[0]=) >>> Traceback (most recent):  File ' <stdin> ', line 1, in <module>typeerror: ' Tuple ' object does not support item assignment>>> aa[0][0]12>>> aa[0][0]=  34Traceback (most recent):  File "<stdin>", line 1, in <module>typeerror: ' Tuple ' object does not Support Item Assignment

However, the dictionary was used during the drawing process, and the value of the tuple was inadvertently modified:

>>> pos = {0: (1), 2: (+, +), 3: (+), 4: (+, +)}>>> pos{0: (20, 20), 1: (20, 2: (+), 3: (+), 4: ()}>>> pos[0]= >>> pos{0: (1, 2), 1: (20, 40), 2: (40, 40), 3: (+), 4: (+)}>>>
This is then found in the dict search of the reference manual:


>>> a = Dict (one=1, two=2, three=3) >>> B = {' One ': 1, ' both ': 2, ' three ': 3}>>> c = dict (Zip ([' O Ne ', ' both ', ' three '], [1, 2, 3]) >>> d = dict ([' One ', 1), (' Three ', 3)]) >>> e = dict ({' thre E ': 3, ' one ': 1, ' both ': 2} ' >>> a = = b = = C = = d = = Etrue

In other words, the value of the value in the dictionary can be modified whether it is enclosed in parentheses, brackets, curly braces, or parentheses.

Another misleading reference is the relationship between tuples and parentheses in Python, where tuples are determined by commas, not parentheses. As you can see, even if you don't have parentheses, you're a tuple.

>>>a= ("One", "one", "one") >>>a[0] ' one ' >>>b= ("one") >>>b[0] ' O ' >>>c= ("one") >>>c[0] ' one ' >>>d= "one", >>>d[0]one

Copyright NOTICE: Welcome reprint, Reprint please indicate the source http://blog.csdn.net/ztf312/

Python: The misleading of tuples and parentheses

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.