In-depth exploration of the magic of the Python Sequence

Source: Internet
Author: User

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.

 
 
  1. shoplist = ['apple', 'mango', 'carrot', 'banana']  
  2. # Indexing or 'Subscription' operation  
  3. print 'Item 0 is', shoplist[0]  
  4. print 'Item 1 is', shoplist[1]  
  5. print 'Item 2 is', shoplist[2]  
  6. print 'Item 3 is', shoplist[3]  
  7. print 'Item -1 is', shoplist[-1]  
  8. print 'Item -2 is', shoplist[-2]  
  9. # Slicing on a list  
  10. print 'Item 1 to 3 is', shoplist[1:3]  
  11. print 'Item 2 to end is', shoplist[2:]  
  12. print 'Item 1 to -1 is', shoplist[1:-1]  
  13. print 'Item start to end is', shoplist[:]  
  14. # Slicing on a string  
  15. name = 'swaroop' 
  16. print 'characters 1 to 3 is', name[1:3]  
  17. print 'characters 2 to end is', name[2:]  
  18. print 'characters 1 to -1 is', name[1:-1]  
  19. print 'characters start to end is', name[:]   
  20. $ python seq.py  
  21. Item 0 is apple  
  22. Item 1 is mango  
  23. Item 2 is carrot  
  24. Item 3 is banana  
  25. Item -1 is banana  
  26. Item -2 is carrot  
  27. Item 1 to 3 is ['mango', 'carrot']  
  28. Item 2 to end is ['carrot', 'banana']  
  29. Item 1 to -1 is ['mango', 'carrot']  
  30. Item start to end is ['apple', 'mango', 'carrot', 'banana']  
  31. characters 1 to 3 is wa  
  32. characters 2 to end is aroop  
  33. characters 1 to -1 is waroo  
  34. characters start to end is swaroop 

The above is all the content of the Python sequence we introduced.

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.