The sequence method is used in python, and the sequence method is used in python.

Source: Internet
Author: User

The sequence method is used in python, and the sequence method is used in python.

This article describes how to use sequence in python. Share it with you for your reference. The details are as follows:

Lists, tuples, and strings are all sequences, but what are sequences? Why are they so special? The two main features of a sequence are index operators and slice operators. The index operator allows us to capture a specific item from the sequence. The Slice operator allows us to obtain a slice of a sequence, that is, a part of the sequence.

#!/usr/bin/python# Filename: seq.pyshoplist = ['apple', 'mango', 'carrot', 'banana']# Indexing or 'Subscription' operationprint '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 listprint '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 stringname = '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[:]

Output:

Item 0 is appleItem 1 is mangoItem 2 is carrotItem 3 is bananaItem -1 is bananaItem -2 is carrotItem 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 wacharacters 2 to end is aroopcharacters 1 to -1 is waroocharacters start to end is swaroop

How it works:

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 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.

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 a sequence name followed by a square bracket, which 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.

The first number (before the colon) in the slice operator indicates the start position of the slice, and the second number (after the colon) 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 sequences is that you can access tuples, lists, and strings in the same way.

I hope this article will help you with Python programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.