The basic data types of the tuple

Tuple

Uses: storing a plurality of different types of values ​​(can not store a variable-type)

It is defined by: with between parentheses storing data, the data (values ​​can not be changed) separated by commas

= T1 ( ' A ' , ' B ' ) # T1 = tuple (( 'A', 'B')) 

emphasized: If only one value within a tuple, it must be a comma
Common methods: 

# 1, according to an index value (forward + reverse take take): can only take, can not be changed, otherwise an error! 
= tuple1 (. 1, ' hhaha ' , 15000.00,. 11, 22 is, 33 is )   
tuple1 [0]
 >>>. 1 
tuple1 [ -2 ]
 >>> 22 is 
tuple1 [0] = ' Hehe '   # error: TypeError: 

# 2, sections (care regardless of the end, step) 
tuple1 [0:. 6: 2 ] 
 >>> (. 1, 15000.0, 22 is ) 

# . 3, the length 
len (tuple1)  
 >>>. 6 # . 4, the members in operation in and Not 
tuple1 = (. 1, ' hhaha ' , 15000.00,. 11, 22 is,

hhaha' in tuple1)
print('hhaha' not in tuple1)

# 5、count()
tuple1 = (1, 'hhaha', 15000.00, 11, 22, 33)
print(tuple1.count(11))
>>>1
# 6、index()
tuple1 = (1, 'hhaha', 15000.00, 11, 22, 33)
print(tuple1.index('hhaha'))
>>>1

 

Guess you like

Origin www.cnblogs.com/baohanblog/p/11807413.html