Sequence is the most basic data structure in Python. Each element in the sequence is assigned a number - its position, or index, with the first index being 0, the second index being 1, and so on.
Python has 6 built-in types for sequences, but the most common are lists and tuples.
Operations that can be performed on sequences include indexing, slicing, adding, multiplying, and checking members.
In addition, Python has built-in methods for determining the length of a sequence and determining the largest and smallest elements.
List is the most commonly used Python data type, which can appear as a comma-separated value within square brackets.
The data items in the list do not need to be of the same type
To create a list, just use square brackets to enclose the different data items separated by commas. It looks like this:
list1 = ['Google', 'Runoob', 1997, 2000]; list2 = [1, 2, 3, 4, 5 ]; list3 = ["a", "b", "c", "d"];
Like string indexing, list indexing starts at 0. Lists can be intercepted, combined, etc.
Accessing values in the list
Use subscript index to access the value in the list. You can also use square brackets to intercept characters, as shown below:
list1 = ['Google', 'Runoob', 1997, 2000]; list2 = [1, 2, 3, 4, 5, 6, 7 ]; print ("list1[0]: ", list1[0]) print ("list2[1:5]: ", list2[1:5])
Update list
You can modify or update the data items of the list, or you can use the append() method to add list items, as shown below:
list = ['Google', 'Runoob', 1997, 2000] print ("第三个元素为 : ", list[2]) list[2] = 2001 print ("更新后的第三个元素为 : ", list[2])
Delete list elements
You can use the del statement to delete elements of the list, as shown below:
list = ['Google', 'Runoob', 1997, 2000] print ("原始列表 : ", list) del list[2] print ("删除第三个元素 : ", list)
Python list function& Method
Serial number | Function |
---|---|
1 |
len (list) Number of list elements |
2 | ##max(list)Returns the maximum value of the list element |
min(list)Returns the minimum value of the list element |
|
list(seq)Convert tuple to list |
The above is the detailed content of What are the list methods in python?. For more information, please follow other related articles on the PHP Chinese website!