tuple unpacking operation

"" " 

Tuple is immutable 

" "" 

user_tuple = ( ' ADMIN ' , 18 is, " CD " , " MALE " )
 Print (user_tuple) 

# tuple unpacking 

name, Age, address, Gender = user_tuple
 Print (name, Age , address, Gender)   # ADMIN 18 is MALE CD 

#   may be so unpacking 
name, * = OTHER user_tuple
 Print (name, OTHER)   # ADMIN [18 is, 'CD', 'MALE'] 

'' ' 
  immutable because tuple tuple which also has elements of immutability, 
  If the tuple of elements put a list, then you can also change,
  however,Not recommended placed in a tuple list 
  Consider the following example
 '' ' 

T = ( ' Person ' , [ ' ADMIN ' , 18 is ])
 Print (T)   # (' Person ', [' ADMIN ', 18 is]) 

T [ . 1]. the append ( ' CD ' )
 Print (T)   # ( 'Person', [ 'ADMIN', 18 is, 'CD']) 

# tuple in the list element has changed, but the list is the id of the same value of 

"" " 

tuple compared with the list, advantages: 
1. performance optimization 
2. thread safety 
3. dict as a key, only immutable objects can become a dict of Key 
4. unpacking and easy to use 
." ""

Guess you like

Origin www.cnblogs.com/z-qinfeng/p/12037625.html