The example in this article describes how to use sequences in Python. Share to everyone for your reference. Specific as follows:
Lists, tuples, and strings are sequences, but what are the sequences and why are they so special? The two main features of a sequence are index operators and slice operators. The index operator allows us to fetch a specific item from the sequence. The slice operator allows us to get a slice of the sequence, which is part of the sequence.
#!/usr/bin/python# Filename:seq.pyshoplist = [' Apple ', ' mango ', ' carrot ', ' banana ']# indexing or ' Subscription ' operatio Nprint ' item 0 is ', Shoplist[0]print ' item 1 are ', Shoplist[1]print ' Item 2 is ', Shoplist[2]print ' Item 3 is ', shoplist[3]p Rint ' Item-1 is ', Shoplist[-1]print ' Item-2 are ', shoplist[-2]# slicing on a listprint ' Item 1 to 3 is ', SHOPLIST[1:3]PRI NT ' Item 2 to end ', Shoplist[2:]print ' item 1 to-1 are ', Shoplist[1:-1]print ' item start to end ', shoplist[:]# Slici ng on a stringname = ' swaroop ' print ' characters 1 to 3 are ', Name[1:3]print ' characters 2 to end is ', Name[2:]print ' Charac Ters 1 to-1 are ', name[1:-1]print ' characters start to end are ', name[:]
Output:
Item 0 is Appleitem 1 are mangoitem 2 is Carrotitem 3 are bananaItem-1 is bananaItem-2 are Carrotitem 1 to 3 are [' Mango ', ' Carrot ']item 2 to end are [' carrot ', ' banana ']item 1 to-1 are [' mango ', ' carrot ']item start to end are [' Apple ', ' mango ', ' C Arrot ', ' banana ']characters 1 to 3 are wacharacters 2 to end are Aroopcharacters 1 to-1 is waroocharacters start-to-end is Swaroop
How it works:
First, let's learn how to use an index to get a single item in a sequence. This is also referred to as subscript operation. Whenever you specify a sequence with a number in square brackets, Python grabs the item in the sequence that corresponds to the position. Remember, Python counts from 0 onwards. Therefore, Shoplist[0] crawls the first item, SHOPLIST[3] fetches the fourth element in the shoplist sequence.
The index can also be negative, in which case the position is calculated from the end of the sequence. Therefore, Shoplist[-1] represents the last element of the sequence and Shoplist[-2] The penultimate item of the crawl sequence.
The slice operator is a sequence name followed by a square bracket, with a pair of optional digits in the square brackets, separated by a colon. Note that this is very similar to the index operator you are using. Remember that the number is optional, and the colon is required.
The first number in the slice operator (preceded by a colon) indicates where the slice begins, and the second number (after the colon) indicates where the slice ends. If you do not specify the first number, Python starts from the beginning of the sequence. If you do not specify a second number, Python stops at the end of the sequence. Note that the returned sequence starts at the start position and ends just before the end position. That is, the start position is contained in the sequence slice, and the end position is excluded from the slice.
Thus, Shoplist[1:3] returns a slice containing two items, starting at position 1, including position 2, but stopping a sequence slice at position 3. Similarly, shoplist[:] Returns a copy of the entire sequence.
You can make slices with negative numbers. Negative numbers are used at the beginning of the end of the sequence. For example, shoplist[:-1] Returns a sequence slice that contains all the items except for the last item.
Use the Python interpreter to interactively try out different slices of the specified combination, i.e. you can see the results immediately at the prompt. The magic of sequences is that you can access tuples, lists, and strings in the same way.
Hopefully this article will help you with Python programming.