6. The container data type list_tuple

# ### container type data tuple List 
"" "List available, can be modified, ordered" "" 
# (1) defines an empty list 
listvar = [] 
Print (listvar, type (listvar)) 
 
# forward index 0 2. 4. 3. 1 
listvar = [13,3.14, True, 6-2j, "I'm a guy"] 
# inverted index -5-4-3-2-1 
 
# (2) obtain a list of values among 
res = listvar [4] # forward index 
res listvar [-1] # = reverse index 
Print (RES) 
 
# = General> element value wants to obtain a list of the last len function required by 
the number of elements used to obtain a container of the type of data len # (length) 
#. 5 -. 1 =>. 4 
RES = len (listvar) -1 
 
# Print (listvar [RES])   
Print (listvar [len (listvar) -1]) 
 
# (. 3) modify the list of values among 
listvar [3 ] = "and soothing" 
listvar [-4] = "health than" 
Print (listvar) 
 
 
# tuple ### 
"" "Available, can not be modified, orderly "" "Available, can not be modified, ordered '' ' 
' ''
Proved to be a fundamental characteristic tuple is a comma, a declared empty tuple, can be used directly (), (1) indicates an integer 
'' ' 
# (1) declaring a null group 
tuplevar = () 
Print (tuplevar, type (tuplevar)) 
tuplevar = (1,2,3,4) 
Print (tuplevar, type (tuplevar)) 
tuplevar = (. 1,) 
Print (tuplevar, type (tuplevar)) 
tuplevar = l, 2,3 
Print (tuplevar , type (tuplevar)) 
 
 
# Get the last value 
# 2. 1 forward index 0 
tuplevar = ( "a", "B", False) 
# reverse index -1 -2 -3 
res = tuplevar [-1] 
# or res tuplevar = [len (tuplevar) -1] 
Print (RES) 
 
# (2) can not be modified tuples 
# tuplevar [-1] = True error 
 
 
# ###, and a tuple string STR almost exactly the same, but each element is the character 
'' 'available, can not be modified and orderly' ''
#         0 1  2 3 45 6 7 8 
strvar = "look at you, trembling"
-9-8-7-6-5-4-3-2-1 # 
# (1) the word string acquired 
RES = strvar [2] 
RES = strvar [-7] 
Print (RES) 
 
# (2 ) can not be modified string 
# strvar [-1] = "! " error

  

Guess you like

Origin www.cnblogs.com/eliwen/p/10967648.html