This is the 21st day of my participation in Gwen Challenge
You should always be grateful to those who give you adversity. “– Children of The Weather
Directory:
- Sequence type definition
- Sequence type handling functions and methods
- Tuple types and their operations
- List types and their operations
- Typical application scenarios of sequence types
Sequence type definition
A sequence is a sequence of elements, so the elements can be the same, the element type can be different, remember that set types can not have the same elements.
When you look at multidimensional data later in Python you’ll see that sequences are one-dimensional data vectors, arranged in a linear fashion. Similar to sequences in mathematics, such as Sn, SN-1… N and n-1 are subscripts, so elements of a Python sequence type are also guided between ordinals, accessing specific elements of the sequence through subscripts.
A sequence is a base class type. To put it simply, we do not use sequence types directly when writing code. Instead, we use several data types derived from sequence types, such as string types, tuples, and lists. Therefore, all operations of sequence types are equally applicable to the above three data types, which have their own unique operation capabilities. Therefore, sequence is a basic data type structure.
Expression of ordinal number of sequence type: in sequence type, elements also have index relations of increasing ordinal number forward and decreasing ordinal number backward.
Serial numbers if you remember from the string, there are two numbering systems for serial numbers: forward increasing and backward decreasing.
So a string type is an extension of a sequence type, except that each element is just a character in a string type, whereas in a sequence type, the element type can be any data type.
Sequence type processing functions and methods
Sequence types provide some operators and some handlers:
Six operators:
X in S: Returns True if x is an element of sequence S, False otherwise. X not in S: Returns False if x is an element of sequence S, True otherwise. S +t: Joins two sequences. S * N or N * S: Copies the sequence S n times. S [I]: index, which returns the ith element in S. I is the serial number of the sequence, and its serial number has two systems: forward increasing and reverse decreasing. S [I :j] or [I :j:k] : slice, return the element subsequence from I to j in sequence S with k as the step size.Copy the code
A typical derivative of a sequence type is a list type, so we use list types as examples:
Ls =["python",123,".py"] ls[::-1]# is a slicing operation that returns all elements in the list, but returns the element subsequence extracted from the end of the listCopy the code
Common types and methods for sequence types:
Len (s) : Returns the length of the sequence S. Min (s) : Returns the smallest element in sequence S, which is required to be comparable, otherwise an error is reported. Max (s) : Returns the largest element in the sequence S. S.index (x) or s.index(x, I,j) : Returns the position where the sequence S starts from I to the first occurrence of the element X in the position. S.count (x) : Returns the total number of occurrences of x in sequence S.Copy the code
Practical examples:
Ls =["python",123,".py"] len(ls) s="python.py"Copy the code
Tuple types and their operations
A tuple is an extension of a sequence type. It is essentially a sequence type. A tuple is unique in that, once created, it cannot be modified.
Tuples are created using parentheses () or tuple(), separated by commas. When using tuples, you may or may not use parentheses. Practical examples:
Def fun () : return 1, 2Copy the code
In Python, the 1 and 2 returned by the above function are treated as tuples, and the number of elements returned is up to the user.
Tuples can also be nested, and once a tuple is defined, its value cannot be changed. Tuples inherit all the common operations of sequence types, that is, the functions of related sequences, and are handled in a way that is common to tuples. Because tuples cannot be modified after creation, there are no other special operations. Practical examples:
Item ="dog","cat","tiger" print(item [::-1]) creature="dog","cat","tiger" print(creature[::-1]) c=(123,"python",creature) print(c[-1][0])Copy the code
List types and their operations
Lists are also sequence types that can be modified at will after they are created. Use method: create by square brackets [] or list(). Use commas to separate elements in the list. Each element type can be different. If we use [] or list, then we are actually creating a list, whereas if we only use assignment, then it is just a list.
Operating functions and their methods:
Ls [I]=x: replace the ith element of the list ls with x. Ls [I :j:k]=lt: Replace the sublist of the corresponding element after LS slicing with list lt. Del ls[I] : Deletes the ith element in the list ls. Del ls[I :j:k] : delete the element with k step from I to j in the list. Ls + =lt: Updates list ls, adding list lt elements to list LS. Ls *=n: Updates list ls with elements repeated n times.Copy the code
Practical examples:
Ls = [" dog ", "cat", "tiger"] ls [1] = [1, 2, 3, 4] print (ls) ls = [" dog ", "cat", "tiger"] ls [1] = [1, 2, 3, 4] print (ls) del Print (ls) print(ls*2) print(ls*2) print(ls*2)Copy the code
The operation of the list can also be simply understood as the operation of the data, that is, add, delete, change and search.
Operating functions and their methods:
Ls.append (x) : Adds an element x to the end of list ls. Ls.clear () : Deletes all elements in list ls. Ls.copy () : Generates a new list and assigns all elements in ls. Ls.insert (I,x) : Adds element x at position I of list ls. Ls.pop (I) : Fetches the i-th element in the list ls and deletes it. Ls.remove (x) : Removes the first element x that appears in list ls. Ls.reverse () : Reverses the elements in the list.Copy the code
Practical examples:
Ls =["dog","cat","tiger"] ls.append(123) print(ls)# print ls.insert(3,"human") print(ls) ls.reverse() print(ls)Copy the code
Typical application scenarios of sequence types
The most common scenario for sequence types is to do data presentations, such as a representation of a set of data, and then operate on it. The tuple type derived from a sequence can represent scenarios in which the element does not change, preferring to be used in fixed collocation scenarios, such as return. Lists are more flexible and are the most commonly used sequence type.
To: Because of the immutable nature of its tuple type elements, it can protect data when operating on certain data.
(Python series) To continue…