Python basic Syntax---a tuple type of data type

Source: Internet
Author: User

Tuple tuples:
One: Basic use
1, the use: The tuple is immutable list, can save multiple values, but multiple values only take the demand, without changing the demand, then use meta-combination most reasonable

2, define the way: in () separated by commas, you can store any type of value

1Names= ('ZJ','WCL','wxx')#names=tuple (' zj ', ' WCL ', ' wxx ')2 Print(type (names))3 emphasis: Always remember to add a comma when there is only one element in the tuple4X= ('ZJ',)5 Print(Type (x))
definition Examples

3, common operation + built-in method

1 names= ('wcl','zj','wxx'  )2 names[0]='wcl_xx'
1, by index value (positive fetch + reverse fetch): can only take
1 names= ('wcl','zj','wxx' ,'lxx','cxxx')2 Print (Names[1:3])
2, slicing (Gu Tou regardless of tail, step)
1 names= ('zj','WCL','wxx' ,'lxx','cxxx')2 Print (Len (names))
3. Length
1 names= ('zj','WCL','wxx' ,'lxx','cxxx')2 Print ('zj' in Names)
4, member operations in and not in
1 names= ('wcl','zj','wxx' ,'lxx','cxxx')2  for inch names: 3 Print (item)    
5. Circulation

II: Summary of the type
1. Save a value or save multiple values
Multiple values can be stored, and the value can be any data type

2. Ordered or unordered
Ordered
3, variable or not variable
Not variable


The underlying principle of variable list:
Refers to the memory address of the value corresponding to the index can be changed

The underlying principle of tuples immutable:
Refers to the memory address of the value corresponding to the index can not be changed
Or conversely, as long as the memory address of the corresponding value of the index does not change, then the tuple is always unchanged

1T1 = (['a','b','C'],'WC','Office')2 3 Print(ID (t1[0]))#16357330502484 Print(ID (t1[1]))#16357330104485 Print(ID (t1[2]))#16357330105046 7T1[0][0] ='AAAA'8 Print(t1)#([' AAAA ', ' B ', ' C '], ' WC ', ' office ')9 Ten Print(ID (t1[0]))#1635733050248
examples Show

Python basic Syntax---a tuple type of data type

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.