python basis - lists, tuples

= List1 [1,2,3,4,5,6] 
List2 = [ 'Wang', 'Cong']

#. 1 values of elements in the list (by index)
Print (List1 [. 3]). 4 #
Print ( List2 [. 1]) # Cong
# Print (List1 [. 6]) is given an index value exceeds #
# lists can be nested lists, tuples, strings, dictionaries, digital,
# index values can then
print (list2 [1 ] [. 3]) # G

# len: calculate the length of the list
Print (len (List2))

# max returns the maximum value of list elements
# min returns a minimum value list elements
# Note: the principle here is to be noted compared

# index a value index to find the location of the first match from the list
Print (list1.index (5))
# Print (list1.index (10)) if there is an error

#count: statistics an element in the list the number of occurrences of
# if there is an error

# 2 listing can be modified, in situ modification
List1 [2] = 10
Print (List1) # [. 1, 2, 10,. 4,. 5,. 6]
# listing variable type, different from the string \
# List2 [. 1] [2] = 'O' being given, because strings immutable
# append: Add a new object at the end of the list
# Extend: a plurality of values in another sequence of adding the end-time list
list3 = []
list3.append ( 'wangcomg')
Print (list3)
list3 = []
list3.extend ( 'wangcong')
Print (list3)
# Note that two different operations of the
# insert: inserting an object into the specified position
list4 = [1,2,3,4]
list4.insert (1,100)
Print (list4) # [. 1, 100, 2,. 3,. 4]

# remove the list element
list5 = [1,2,3,4,5]
# POP: delete the last element in the list
list5.pop ()
Print (list5) # [. 1, 2,. 3,. 4]
# remove: the first occurrence of the value list to remove one element
list5.remove (. 4)
Print (list5) # [. 1, 2,. 3]
# Clear: Please empty list: a list but there,
# del : delete the list,
list6 = [, 2, 3]
list6.clear ()
Print (list6)
list7 = [, 2, 3]
del list7
Print (list7) # save that list does not exist
# Sort (Key = None, Reverse = False)
# sort the list, the default is ascending
# Copy: Copy List: shallow copy
# tuples and lists are similar, when tuples are immutable and can not be additions and deletions,
# only You can query, and the query is basically the same list

Guess you like

Origin www.cnblogs.com/cong12586/p/11349940.html