Python Basic Grammar Lesson 3 (List)

[] List

1. What can be stored in the list

1. What types can be stored in the list?

 The list is a large warehouse, treasure chest, all types learned , can be put in the list.

my_list = [1,2,3,(2,'polo'),6,'hello',['lemon','python']]
print(my_list)

Results: [1, 2, 3, (2, 'polo'), 6, 'hello', ['lemon', 'python']]

2. The length of the list len ​​()

my_list = [1,2,3,(2,'polo'),6,'hello',['lemon','python']]
print(len(my_list)) #7

3. Get the elements in the list --- index (similar to a string)

The result of the list index is still a list (what data type is the element, and what type is the result of the index, will not change)

my_list = [1,2,3,(2,'polo'),6,'hello',['lemon','python']]
print(my_list[-1][1]) # python

4. The slice of the list is still a list

 

Second, create a list

Empty list :

number = []
print(number) #[]

Common list:

number = [1,2,3]
print(number) 

Mixed list:

list_1 = [1,2,3,(2,'polo'),6,'hello',['lemon','python']]
print(list_1)  # [1, 2, 3, (2, 'polo'), 6, 'hello', ['lemon', 'python']]

 

Common operations of the list: add, delete, modify

Third, add elements to the list

1. append (): append new objects at the end of the list by default. Add the parameter as an element to the end of the list. (There is only one append parameter, and append can only add one element at a time)

list_1 = [1,2,3,(2,'polo'),6,'hello',['lemon','python']]
list_1.append(9)
print(list_1)  # [1, 2, 3, (2, 'polo'), 6, 'hello', ['lemon', 'python'], 9]

 ❤ append () is useful for lists, but the returned result is None, which is a Python rule. (Remove () is the same.)

my_list = [1,2,3,(2,'polo'),6,'hello',['lemon','python']]
new_elem = my_list.append(888)
print(my_list) 
print(new_elem)

结果:
[1, 2, 3, (2, 'polo'), 6, 'hello', ['lemon', 'python'], 888]
None

 

2. extend (): append multiple values ​​in another sequence at the end of the list at once, that is, use the parameter as a list to extend the original list and merge the list.

list_1 = [1,2,3,(2,'polo'),6,'hello',['lemon','python']]
list_1.extend([2,4,5,6])
print(list_1)  #[1, 2, 3, (2, 'polo'), 6, 'hello', ['lemon', 'python'], 2, 4, 5, 6]

 

3. insert (): There are 2 parameters, the first parameter is the position to be inserted, and the second parameter is the value to be inserted at the position to be inserted.

list_1 = [1,2,3,(2,'polo'),6,'hello',['lemon','python']]
list_1.insert(3,'hello world')
print(list_1)  #[1, 2, 3, 'hello world', (2, 'polo'), 6, 'hello', ['lemon', 'python']]

 

Fourth, delete the elements in the list

1. Delete the specified content (I do n’t know the position of the index, I only know that there is this specific content in the list, it must be deleted before it is available, and it does not exist in the list, it is impossible to operate)

list_1 = [1,2,3,(2,'polo'),6,'hello',['lemon','python']]
list_1.remove(['lemon','python'])
print(list_1)  # [1, 2, 3, (2, 'polo'), 6, 'hello']

remove () can only remove one element, there are multiple same elements in the list, the default is to remove the first

list_1 = [1,'hello',2,'hello',3,(2,'polo'),6,'hello',['lemon','python']]
list_1.remove('hello')
print(list_1)  #[1, 2, 'hello', 3, (2, 'polo'), 6, 'hello', ['lemon', 'python']]

 

2. Delete the specified index position pop (), the returned result is the deleted element value

list_1 = [1,2,3, (2, ' polo ' ), 6, ' hello ' , [ ' lemon ' , ' python ' ]]
 # pop () without index value, the last element in the list is removed by default 
list_1.pop ()
 print (list_1) # [1, 2, 3, (2, 'polo'), 6, 'hello']
list_1 = [1,2,3, (2, ' polo ' ), 6, ' hello ' , [ ' lemon ' , ' python ' ]]
 # pop () with index value, delete the content of the corresponding index value 
list_1 .pop (3 )
 print (list_1) # [1, 2, 3, 6, 'hello', ['lemon', 'python']]

❤View the result of pop () (return value)

list_1 = [1,2,3,(2,'polo'),6,'hello',['lemon','python']]
elem = list_1.pop()
print(elem)
print(list_1)

result:

['lemon', 'python']

[1, 2, 3, (2, 'polo'), 6, 'hello']

 

Five, modify the elements in the list

list_1 = [1,2,3, (2, ' polo ' ), 6, ' hello ' , [ ' lemon ' , ' python ' ]] 
list_1 [ 2] = ' hahhahahha ' 
print (list_1) # [1, 2. , 'hahhahahha', (2, 'polo'), 6, 'hello', ['lemon', 'python']];

 

Six, the list of function methods

1.clear () clear list elements

❤a = The return value of my_list.clear () is None

my_list = [2,1,3]
my_list.clear()
print(my_list)      # []
print(my_list.clear())   #None

  

2.sort () sorting digital sorting, text does not need to be sorted

my_list = [2,1,3,4,6,5]
my_list.sort()
print(my_list)      # [1,2,3,4,5,6]
print(my_list.sort())   #None
= my_list [l, 2,3, [ ' . 3 ' ,. 6 ]] 
my_list.sort ()     # error, different types of comparison can not

 

3.reverse () descending order

my_list = [2,1,3,4,6,5]
my_list.sort(reverse=True)
print(my_list)      # [6,5,4,3,2,1]
print(my_list.sort(reverse = True))  #None

 

Guess you like

Origin www.cnblogs.com/ananmy/p/12732567.html