Key points of this section
1. Let students understand the meaning of list data types
2. Let students master the definition and characteristics of the list
3. The trainees can master the common operation of the list and understand other factory methods
4. Let the students know the range method and be able to convert the data generated by the range method into a list
The full version of python development zero basic graphic tutorial has been uploaded to the official csdn platform. Interested friends can scan the QR code below and have a preview. Go to get it for free!
Introduction
We have mastered the two data types in python before. Now we know that if we want to express how many students there are in our class, we should use int shaping. If we want to express the names of students in our class, we should use str string type. But now I want to express the names of the whole class. What should I use?
Can we solve the problem with the knowledge we learn now?
Maybe some students said that they could use a long string to write in all the students' names, but the problem came.
For example, when a classmate is expelled from school because of his poor study, does it become particularly troublesome for me to find a name in a long string of characters and delete him?
In this case, we think it would be perfect if python provided us with a new data structure that can store many strings and make it easy for us to add, modify and delete.
Definition and creation of lists
Definition: [] is separated by commas, and various data types are stored according to the index. Each position represents an element
List creation
list_test=['Zhang San','Li Si','alex'] #or list_test=list('alex') #or list_test=list(['Zhang San','Li Si','alex'])
List features and common operations
characteristic:
1. Multiple values can be stored
2. Define the list elements from left to right. The subscripts are accessed from 0 in order
3. The value corresponding to the specified index position can be modified and can be changed
Common operations:
#Indexes >>> l = ['egon','alex','seven','yuan'] >>> l[0] 'egon' >>> l[2] 'seven' #section >>> l[0:2] ['egon', 'alex'] >>> l[2:5] ['seven', 'yuan'] >>> l[:2] ['egon', 'alex'] >>> l[2:] ['seven', 'yuan'] >>> l[:] ['egon', 'alex', 'seven', 'yuan'] >>> l[::2] ['egon', 'seven'] >>> l[::-1] ['yuan', 'seven', 'alex', 'egon'] #Add >>> l.append("eva") >>> l ['egon', 'alex', 'seven', 'yuan', 'eva'] #delete >>> l.remove('eva') >>> l ['egon', 'alex', 'seven', 'yuan'] >>> l.pop() 'yuan' >>> l ['egon', 'alex', 'seven'] #length >>> len(l) 3 #contain >>> 'seven' in l True >>> 'yuan' in l False #Cycle: why "i"? >>> for i in l: print(i) egon alex seven
List and string -- split and join
#division >>> s = 'hello world' >>> s.split(' ') ['hello', 'world'] >>> s2= 'hello,world' >>> s2.split(',') #connect >>> l = ['hi','eva'] >>> '!'.join(l) 'hi!eva'
range
Introduction:
Now that you have learned the list, let's create a list from 1 to 100.
Wait a minute. How are you going to create it?
Is it written directly from 1 to 100 or in a loop? It's silly to write from 1 to 100. It seems better to use cycle. However, it is not easy to write in a loop. There is a ready-made method in python that can directly generate a number of 1-100.
>>> range(1,100) range(1, 100) >>> list(range(1,100)) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99] >>> list(range(10)) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> list(range(0,100,2)) [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98]
Just introduce the calling and parameter passing of range. Don't emphasize that range is a generator. Let's use list to convert range into a list