Analysis of the Creation method and particularity of Python tuples

Source: Internet
Author: User

Python tuples can help us easily implement certain functional requirements in practical applications. Here we will describe some basic content to give you a detailed introduction to the correct application method of Python tuples, hoping to help you.

Create and access

 
 
  1. >>> mytuple=(1,2,3,4,'a')  
  2. >>> mytuple  
  3. (1, 2, 3, 4, 'a')  
  4. >>> tuple('abcdefg')  
  5. ('a', 'b', 'c', 'd', 'e', 'f', 'g')  
  6. >>> mytuple[0]  
  7. 1  
  8. >>> mytuple[1:4]  
  9. (2, 3, 4)  
  10. >>> id(mytuple)  
  11. 19758944  
  12. >>> mytuplemytuple=mytuple+('b','c')  
  13. >>> mytuple  
  14. (1, 2, 3, 4, 'a', 'b', 'c')  
  15. >>> id(mytuple)  
  16. 19840112  
  17. >>> 

Operation

 
 
  1. >>> mytuple =(1,2,3)  
  2. >>> mytuple *2  
  3. (1, 2, 3, 1, 2, 3)  
  4. >>> 1 in mytuple  
  5. True  
  6. >>> 4 not in mytuple  
  7. True  
  8. >>> len(mytuple)  
  9. 3  
  10. >>> (1,2)==(2,1)  
  11. False  
  12. >>> 

Special

1) immutable

 
 
  1. >>> mytuple=(1,2,3)  
  2. >>> id(mytuple)  
  3. 19773760  
  4. >>> mytuple+=('a','b')  
  5. >>> id(mytuple)  
  6. 19758944  
  7. >>> 

Default Python tuples

1) all objects separated by commas (,) are not clearly defined by symbols.

 
 
  1. >>> 1,2,3,'a'  
  2. (1, 2, 3, 'a') 

2) Multiple objects returned by all functions

 
 
  1. >>> def f():  
  2. return 1,2,3  
  3. >>> f()  
  4. (1, 2, 3) 

Single Object Python tuples

 
 
  1. >>> a=('a')  
  2. >>> type(a)  
  3. < type 'str'> 
  4. >>> 

To create a single object tuples, follow these steps:

 
 
  1. >>> a=('a',)  
  2. >>> type(a)  
  3. < type 'tuple'> 

List and Python tuples

The tuples are immutable and will not be tampered.

The list and metadata can be converted to each other.

 
 
  1. >>> mytuple=(1,2,3)  
  2. >>> mytuple  
  3. (1, 2, 3)  
  4. >>> mylist=list(mytuple)  
  5. >>> mylist  
  6. [1, 2, 3]  
  7. >>> tuple(mylist)  
  8. (1, 2, 3) 

The above is our introduction to the concepts related to Python 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.