Home > Article > Backend Development > Everything you need to know about Python lists
In Python program development, lists are often used. Suppose there are 50 students in a class and you need to count the total score of each student. If you do not use a list, you need to define 50 variables to store the total score of each student. This is quite troublesome. The best way is Use lists. Next, the editor will take you to learn the knowledge of lists!
1. For readers who have studied C language or Java language, they all know that these two languages support arrays, and Python does There is no concept of arrays, but there is the concept of lists. The list will store all elements in a pair of square brackets ([]), and adjacent elements are separated by commas, as shown below:
listName=[元素1,元素2,元素3,...元素n]
in the above The variable of the list is listName, and element 1 ~ element n represent the elements in the list.
In C language, arrays store the same type of data. Compared with lists in Python and C language arrays, the more powerful thing is that lists can store the same type of data, and they can also store different types. The data. As shown below:
listName=[1,'a']
2. There are two ways to create a list, as shown below:
1) Use square brackets ([]) to create a list, the syntax is as follows Shown:
listName=[元素1,元素2,元素3,...元素n]
Use square brackets ([]) to create a Python list. "=" means assigning a value to a variable name. Among them, listName is the variable name, and the elements 1 to n in the square brackets represent the elements in the list.
Next, let’s learn how to use square brackets ([]) to create a list through an example. The code is as follows:
a=[1,2,3,4,5] b=["Python","Java","C语言"]
In the above code, the variable name is a The list of , stores values;
The list of variable name b, stores strings.
2) Use the list() method to create a list. The list() method converts tuples or strings into lists. The syntax is as follows:
listName=list(a)
listName is Variable name, list(a) where the parameter of a represents the string or tuple to be converted into a list.
Next, let’s use an example to understand the use of the list() method. The specific code is as follows:
a = ('Java', 10, 'Python', 'PHP',20) list1 = list(a) print("list1列表中元素有: ", list1) b = "This is Python" list2 = list(b) print("list2列表中元素有: ", list2)
The rendering of the program running is as follows Display:
1. There are two ways to access elements in the list, As shown below:
1) Use the subscript value (index value) to access an element in the list. The syntax is as follows:
listName=['A','B','C','D']#定义一个列表 listName[i]#语法
Declare a listName variable A list of names, accessing an element in the list is based on the "variable name" and "subscript value". For example, when accessing the C element in the list, the subscript value starts from 0, so the subscript value of the element C is 2. To access the C elements in the list, use listName[2]
2) Use slicing to access the elements of the list. The syntax is as follows:
listName=['A','B','C','D']#定义一个列表 listName[start,end,step]#切片的语法
In the syntax of slicing, start Indicates the starting position, end indicates the ending position, and step indicates the step size.
Next, let’s learn about using slices to access elements of a list through an example. The specific code is as follows:
listName=['A','B','C','D','E','F','G'] print(listName[1:3]) print(listName[3:]) print(listName[1:6:2]) print(listName[-5:-2])
In the above code, listName[1:3] It means starting from the subscript value 1 to 3, because the left is closed and the right is open, the subscript value 3 cannot be obtained;
listName[3:] means starting from the subscript value 3 to the end;
listName[1:6:2] means starting from the subscript value 1 to 6, with a step size of 2;
listName[-5:-2] indicates that the subscript value starts from -5 to -2. Negative subscript values need to be taken in reverse. -5 is for the C element.
The rendering of program operation is as follows:
The above is the detailed content of Everything you need to know about Python lists. For more information, please follow other related articles on the PHP Chinese website!