"Turn" Python Learning (8) tuples

Source: Internet
Author: User

Transfer from http://www.cnblogs.com/BeginMan/p/3156235.html

One, tuple characteristics

1, similar list, but immutable type, because of this, it can do a dictionary key
2, when processing a group of objects, this group by default is a tuple type (old write wrong "Yuan zu")
3, all the multi-object, comma-delimited, not explicitly defined by the symbol of these are the tuple type by default

1 >>>‘Abc', 1, 2,‘X‘, True2 (‘Abc', 1, 2,‘X‘, True)3 >>> x, y =1,24 >>>X, y 5 (1, 2)  6---------------- ------------- 7 def Foo1 () :  8 return obj1,obj2,obj3< Span style= "COLOR: #008080" > 9 10 11 def Foo2 (): 12  Return [Obj1,obj2,obj3]13 14 def Foo3 (): 15 return (obj1,obj2,obj3)       

4 , comma not less, try to write, even if there is only one element

>>> Type (‘X‘)) <type‘Str' >>>> type (( "x"  ,)) <type  ' tuple "> >>> tup = ( ' x " Span style= "COLOR: #000000" >) >>> type (TUP) <type  "str" >>>> Tup = ( ' x ") >>> type (tup) <type  tuple ";        

Feel good: hi.baidu.com/wuxinzy/item/c2c3cd428c99aa01896d10a7

1. Unable to add elements to the tuple. Tuples do not have a append () or extend () method.
2. You cannot delete an element from a tuple. The tuple does not have a remove () or pop () method.
3. You can find an element in a tuple, because the operation does not change the tuple.
4. You can also use the In operator to check if an element exists in a tuple. So what good is a tuple?

? Tuples are faster than lists. If you define a series of constant values, and all you need to do is traverse it, use a tuple substitution list.
? "Write protection" of data that does not need to be changed will make the code more secure. Using a tuple substitution list is like having an implied assert statement that shows that the data is
? 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.

"Turn" Python Learning (8) tuples

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.