In Python, sequence is a general term for an ordered set. There are seven types of sequences in Python.
These are:
1.Unicode String
2.String
3.Lists
4.Tuple
5.Byte array
6.Buffer
7.Xrange object
Among these seven , three are the most popular. These three are:
1.Lists
2.Tuple
3.String
Main concepts of sequences in Python
Of all sequence types, lists are the most versatile. List elements can be any objects. Lists are mutable, which means they can be changed. Its elements can be updated, deleted, and elements can be inserted.
Tuples are also like lists, but with one difference is that they are immutable, which means they cannot be changed after they are defined.
Strings are slightly different from lists and tuples. Strings can only store characters. Strings have a special symbol.
The following are the operations that can be performed on sequences:
Operators combine two sequences in one process. It is also called connection.
For example, [1,2,3,4,5][6,7] will evaluate to [1,2,3,4,5,6,7].
*The operator repeats the sequence a defined number of times.
For example, [1,22] * 3 will evaluate to [1,22,1,22,1,22].
If x is an element of NewSeq, x in NewSeq returns True, otherwise it returns False. This statement can be canceled using not(x in NewSeq) or x not in NewSeq.
NewSeq [i] Returns the i-th character of NewSeq. Sequences in Python are indexed from zero, so the index of the first element is 0, the second is 1, and so on.
NewSeq[-i] returns the i-th element from the end of NewSeq, so NewSeq[-1] will be the last element of NewSeq and NewSeq[-2] will be the second -last element.
All sequences in python can be sliced.
The above is the detailed content of What is a sequence in Python?. For more information, please follow other related articles on the PHP Chinese website!