python learning nine (list type)


List (List)
- is a list of objects in python
- objects (object) is dedicated to the memory used to store data in an area
- like values, he can only save a single data
- the list can save multiple ordered value
list is created by []
to create a list
my_list = [] # which is an empty list
print (type (my_list))

when the stored data list, called elements, a plurality of elements can be stored in the list, the list can be created element specifies the list
my_list = [10] # this is a list of elements
my_list = [1, 2, 3 , 4] # this is a list of multi-element, to each element, division number

list can be stored in any for
my_list = [10, "hello" , None, False]

list of objects is in the order of insertion to hold the list
can be obtained list of elements by an index (index), the index is an element position in the list, each element has an index, the index is an integer from 0 starting
my_list = [10, 20, 30 , 40]
to get the list of elements indexed by
the syntax:
my_list [index]
Print (my_list [0])
If the index uses more than the maximum range, error:
Traceback (mostrecentcalllast): File "<the INPUT>", line1, in <Module> IndexError: listindexoutofrange
get the length of the list, use len ()
function to get the maximum index - 1
is an index range
Print (len (my_list))
## lists for the slice
- the slice means from the existing list, get a sub-list
stus = [ "a", " b", "c", "d", "e "," F "]
- negative values in the index list, if it is negative, taken from the rear forward
print (stus [-1]) # F obtained
- obtained by slicing a specified element, the element acquired by slicing the , the starting position the element will include, but not including the end position of the element
syntax:
list [starting position: end position]
Print (stus [0: 2]) # ab is obtained
if the end position is omitted, will be taken up to Finally,
print (stus [1:]) # Get all
if the start position is omitted, it starts from the first element taken
print (stus [: 3]) # ABC obtained

- a step value
Syntax:
List [Starting position: End position: step]
# step, he said interval is obtained every acquisition element, the default value is. 1
Print (stus [0:. 5: 2]) is obtained ACE #
# step can be negative, but it can not be 0
Print (stus [:: -. 1]) # fedcba obtained
if it is negative, it will want to take from the list of the top element behind
the operation list ##
stus = [ "a", " b", "c "," d "," e "," f "]

plus:
my_list = [. 1, 2,. 3] + [" A "," B "," C "] # can be spliced list
print (my_list)
multiplication :
my_list = [. 1, 2,. 3] * #. 3 may be repeated a specified number of the list

in Not in and
in the specified element is used to check whether there is a list of
print ( "a" in stus) # returns True that there is, return False None

not in used to check whether the specified element in the list
print ( "z"in stus) # None returns True, and False that there

max () # Get the list of maximum
min () # Get the minimum list
= ARR [. 1, 2,. 3,. 4,. 5, 666, 77777]
Print (max (ARR))
Print (min (ARR))

x.inxde () # obtains the location specified element in the list, if there is no value error
print (arr.index (5))

Guess you like

Origin www.cnblogs.com/wangwen022/p/11299751.html