The list of self-study python notes with the little turtle is updating...

After reading these notes, you can get started with Python.
Watch the video of Little Turtle on Station B, and organize your notes by the way.

Lesson 11 Tuples
Lesson 12 strings
Lesson 13 format format
Lesson 15 lambda expressions

Lesson 10 list

1. What can be stored in the list?

      In the list, you can store integers, floating-point numbers, strings, objects... Turtle meal said that the Python list is an array with hormones. If you compare the array to a container, then the Python list is a big warehouse, and Ta can store what we have learned. Any data type
      such as creating an array of mixed types: mix = [1,'turtle powder',3.14,[1,2,3]]

2. What are the ways to add elements to the list?

      1️⃣append()
      2️⃣extend()
      3️⃣insert()

number = [1,2,3,4,6] #创建一个列表
number.append(7) # 向列表中添加一个元素 5 
number.extend([89]) # 向列表中添加两个元素
number.insert(45) # 在索引为 4 处添加 5 元素

  Four lines of code output results:
      Out[1]:[1, 2, 3, 4, 6]
      Out[2]:[1, 2, 3, 4, 6, 7]
      Out[3]:[1, 2, 3, 4, 6, 7, 8, 9]
      Out[4]:[1, 2, 3, 4, 5, 6, 7, 8, 9]

  The usage and differences of these three methods:
      append() method can only add one element at the end of the list ;
      extend() method can add multiple elements at the end of the list , but the parameter list must be added as a list ;

      insert( The) method can specify the element to be inserted into the specified position. There are two parameters in the parameter list. The first parameter is the index value (the list index value starts from 0), and the second parameter is the value of the inserted element.

Example 1: Suppose the following list is given: member = ['Little Turtle','Dark Night','Lost','Yijing','Autumn Dance Setting Sun'], the list is required to be modified to: member = ['Little Turtle' , 88,'Dark night', 90,'Lost', 85,'Yijing', 90,'Autumn dancing in the setting sun', 88]

Method 1: Use insert() and append() methods to modify the list

1. member.insert(1, 88) 
2. member.insert(3, 90) 
3. member.insert(5, 85) 
4. member.insert(7, 90) 
5. member.append(88)

Method 2: Re-create a list coverage with the same name.

member = [' 小甲鱼 ', 88, ' 黑夜 ', 90, ' 迷途 ', 85, ' 怡静 ', 90, ' 秋舞斜阳 ', 88]

Example 2: Use a for loop to print each content in the member list above

member = [' 小甲鱼 ', 88, ' 黑夜 ', 90, ' 迷途 ', 85, ' 怡静 ', 90, ' 秋舞斜阳 ', 88]
for each in member:
	print(each) 
# 自己可以试一试,打印出来的格式是每一个元素占一行 

Example 2 Improvement: Make the printed style look better. The
Insert picture description here code must be typed by yourself, so I will put a screenshot, otherwise you don't know what mistakes you will make! ! !

3. What are the ways to delete elements from the list?

      1️⃣ remove(element value)
      2️⃣ del member[index value]
      3️⃣ pop()

number = [1,1,2,3,9,4,5,66,6,7,20]
number.remove(9) # [1, 1, 2, 3, 4, 5, 66, 6, 7, 20]
del number[0] # [1, 2, 3, 4, 5, 66, 6, 7, 20]
number.pop() # 20
number.pop(5) # 66

  The usage and differences of these three methods: The
      parameter in the remove (parameter) method is the element in the list you want to delete; the

      del list [index value] statement is not a built-in method, so the format is different, del deletes the index in the list Corresponding value; The

      pop() method has a return value. If there is no parameter in the parameter list, the last value of the list will be deleted. If the index value is added, the value of the index will be deleted.

4. List fragmentation

       List fragmentation can help get multiple elements in the list

number = [1,2,3,4,5]
print(number[1:3]) # 输出结果为[2,3] 即打印索引1-3之间的元素 包括索引 1,不包括索引 3,即相当于一个左闭右开区间
print(number[:3]) # [1,2,3],输出从列表开始到索引3之间的元素,同样不包括索引3的元素,即索引0,1,2
print(number[2:]) # [3,4,5] 输出索引2到列表结束
print(number[:]) # 输出整个列表
5. Other methods

sort() method : The code for sorting the list is
as follows:

list = [4,2,5,9,1,0,8]
list.sort()
print(list) # 输出的结果为[0,1,2,4,5,8,9]

sort(reverse=False) , when the parameter is False, it is output in normal order, when the parameter is True, it is output in reverse order after sorting

list = [4,2,5,9,1,0,8]
list.sort(reverse=True)
print(list) # 输出的结果为[9,8,5,4,2,1,0]

Count (data value) method : return the number of times the data value appears in the list
index (data value) method : return the index value of the first appearance of the data value in the list
reverse() method : flip the list
copy() method : change List copy
clear() method : clear the list to an empty list, but the list still exists

6, the logical operation of the list

I use a few examples to show:
1.
Insert picture description here
      Note: Lists can also be directly judged. When judging the size, as long as one of the numbers in a list is larger, the list will be larger.
2.
Insert picture description here
      Note: Not the same type. Use + operation, it is best to use append, insert, extend method
3.
Insert picture description here
      Extension: How to obtain a list of mixed types:Insert picture description here

7. List comprehension/list comprehension

Guess you like

Origin blog.csdn.net/A_Tu_daddy/article/details/105043726