Python basic data structure - tuple

Sometimes how we do not want a list of data to be modified people? You can use a tuple store, tuples also known as read-only list, can not be modified.

Definition: list-like, but [] into ()

characteristic:

  1 can store a plurality of values

  2. immutable

  3.
defines tuple element order from left to right, starting from index 0 accessed sequentially and orderly

create

  1 ages = (11, 22, 33, 44, 55) 2 3 # 4 5 ages = tuple((11, 22, 33, 44, 55)) 

Common Operations

  

1  # index 
2 >>> AGEs = (. 11, 22 is, 33 is, 44 is, 55 )
 . 3 >>> AGEs [0]
 . 4 . 11
 . 5 >>> AGEs [. 3 ]
 . 6 44 is
 . 7 >>> AGEs [-1 ]
 . 8 55
 . 9  # slice: List with  
10  # cycles 
. 11 >>> for Age in AGEs:
 12 is      Print (Age)
 13 is . 11
 14 22 is
 15 33 is
 16 44 is
 . 17 55
 18 is  # length 
19 >>> len(ages)
20 5
21 #包含
22 >>> 11 in ages
23 True
24 >>> 66 in ages
25 False
26 >>> 11 not in ages
27 False

Note: tuples immutable itself, if the tuple further includes other variable elements, these variable elements may vary

why? Since tuples just the address of each memory storage element, [ 'Wangyou Hu', 'Jack'] of this list itself presence tuple memory address does not change, but the list contains the memory address of an element is the presence of additional space It is variable.

Guess you like

Origin www.cnblogs.com/wyh-study/p/11322958.html