In a powerful object-oriented general-purpose computer language such as the Python programming language, there are many usage methods that differ greatly from those common programming languages, for example, the Python sequence introduced today is one of them. First, we will learn how to use indexes to obtain a single item in the sequence. This is also called a subscript operation. Whenever you use a number in square brackets to specify a Python sequence, Python captures the items at the corresponding position in the sequence. Remember, Python starts counting from 0. Therefore, shoplist [0] crawls the first item and shoplist [3] captures the fourth element in the shoplist sequence.
- Sample Code for calling Python functions in C
- Analysis of Python programming specifications
- Analysis of basic Python naming conventions
- Python Incremental Backup implementation tips
- Python data conversion code in-depth analysis
The index can also be a negative number. In that case, the position is calculated from the end of the sequence. Therefore, shoplist [-1] indicates the last element of the sequence, while shoplist [-2] captures the second to last item of the sequence.
The Slice operator is followed by a sequence name in Python. It contains an optional number and is separated by a colon. Note that this is very similar to the index operator you are using. Remember that numbers are optional, while colons are required.
Before the first number of colons in the slice operator) indicates the start position of the slice, and after the second number of colons) indicates the end of the slice. If the first number is not specified, Python starts from the beginning of the sequence. If the second number is not specified, Python stops at the end of the sequence. Note that the returned sequence starts from the starting position and ends just before the ending position. That is, the start position is included in the sequence slice, and the end position is excluded from the slice.
In this way, shoplist [] returns a sequence slice starting from position 1, including position 2, but stops at position 3. Therefore, it returns a slice containing two items. Similarly, shoplist [:] returns a copy of the entire sequence.
You can use a negative number for slicing. A negative number is used to start from the end of the sequence. For example, shoplist [:-1] returns a sequence slice that contains all projects except the last project.
Use the Python interpreter to interactively specify a combination of different slices, that is, you can see the result immediately at the prompt. The magic of Python sequences is that you can access tuples, lists, and strings in the same way.
- shoplist = ['apple', 'mango', 'carrot', 'banana']
- # Indexing or 'Subscription' operation
- print 'Item 0 is', shoplist[0]
- print 'Item 1 is', shoplist[1]
- print 'Item 2 is', shoplist[2]
- print 'Item 3 is', shoplist[3]
- print 'Item -1 is', shoplist[-1]
- print 'Item -2 is', shoplist[-2]
- # Slicing on a list
- print 'Item 1 to 3 is', shoplist[1:3]
- print 'Item 2 to end is', shoplist[2:]
- print 'Item 1 to -1 is', shoplist[1:-1]
- print 'Item start to end is', shoplist[:]
- # Slicing on a string
- name = 'swaroop'
- print 'characters 1 to 3 is', name[1:3]
- print 'characters 2 to end is', name[2:]
- print 'characters 1 to -1 is', name[1:-1]
- print 'characters start to end is', name[:]
- $ python seq.py
- Item 0 is apple
- Item 1 is mango
- Item 2 is carrot
- Item 3 is banana
- Item -1 is banana
- Item -2 is carrot
- Item 1 to 3 is ['mango', 'carrot']
- Item 2 to end is ['carrot', 'banana']
- Item 1 to -1 is ['mango', 'carrot']
- Item start to end is ['apple', 'mango', 'carrot', 'banana']
- characters 1 to 3 is wa
- characters 2 to end is aroop
- characters 1 to -1 is waroo
- characters start to end is swaroop
The above is all the content of the Python sequence we introduced.