The sequence is the most basic data structure in Python. Each element in the sequence is assigned a number-its position, or index, the first index is 0, the second index is 1, and so on.
Python has 6 built-in types of sequences, but the most common are lists and tuples.
Sequences can be performed by operations including indexing, slicing, adding, multiplying, and checking members.
In addition, Python has built-in methods for determining the length of a sequence and determining the maximum and minimum elements.
A list is the most commonly used Python data type and can appear as a comma-separated value within a square bracket.
Data items for a list do not need to have the same type
Create a list by enclosing separate data items separated by commas in square brackets.
CRUD for---List
Python's total delete refrence are all using Del
---list script operators
The operands of the list to + and * are similar to strings. The + sign is used for the combined list, and the * number is used for repeating lists.
As shown below:
Python Expressions |
Results |
Description |
Len ([1, 2, 3]) |
3 |
Length |
[1, 2, 3] + [4, 5, 6] |
[1, 2, 3, 4, 5, 6] |
Combination |
[' hi! '] * 4 |
[' hi! ', ' hi! ', ' hi! ', ' hi! '] |
Repeat |
3 in [1, 2, 3] |
True |
Whether the element exists in the list |
For x in [1, 2, 3]: print x, |
1 2 3 |
Iteration |
---python list interception
The list of Python intercepts the type of string manipulation as follows:
=[' spam ',' spam ',' spam! ' ]
Operation:
Python Expressions |
Results |
Description |
L[2] |
' spam! ' |
Read the third element in the list |
L[-2] |
' Spam ' |
Reads the second-to-last element in a list |
L[1:] |
[' Spam ', ' spam! '] |
To intercept a list starting with the second element |
Python list Functions & methods
Python contains the following functions:
Serial Number |
function |
1 |
CMP (List1, List2) Compare two elements of a list |
2 |
Len (list) Number of list elements |
3 |
Max (list) Returns the maximum value of a list element |
4 |
MIN (list) Returns the minimum value of a list element |
5 |
List (seq) Convert a tuple to a list |
Python contains the following methods:
Serial Number |
Method |
1 |
List.append (obj) Add a new object at the end of the list |
2 |
List.count (obj) Count the number of occurrences of an element in a list |
3 |
List.extend (seq) Append multiple values from another sequence at the end of the list (extend the original list with a new list) |
4 |
List.index (obj) Find the index position of the first occurrence of a value from the list |
5 |
List.insert (index, obj) Inserting an object into a list |
6 |
List.pop (Obj=list[-1]) Removes an element from the list (the last element by default), and returns the value of the element |
7 |
List.remove (obj) To remove the first occurrence of a value in a list |
8 |
List.reverse () Reverse List of elements |
9 |
List.sort ([func]) Sort the original list |
List of lists in Python