Learning: Python learning path (3): Sequence

Source: Internet
Author: User
Document directory
  • Index
  • Parts
  • Arithmetic Operation
  • Common functions
Learning: Python learning path (3): Introduction to sequence

Python has six built-in sequences:

List, tuples, strings, Unicode strings, buffer objects, and xrange objects.

We mainly talk about lists and metadata groups. The biggest difference between the two is that the list can be modified, and the tuples cannot be modified.

The feature of a sequence is to assign a sequence number to each element. It is the first from scratch, and the last one is-1... -N.

General operation indexes of Sequences

Code:

  1. # Coding: UTF-8
  2. Months1 = ["1", "2", "3", "4", "5", "6", "7", "8", "9 ", "10", "11", "12"]
  3. Mouths2 = ("1", "2", "3", "4", "5", "6", "7", "8", "9 ", "10", "11", "12 ")
  4. Print months1
  5. Print mouths2
  6.  
  7. Print type (months1)
  8. Print type (mouths2)
  9.  
  10. Print months1 [1]
  11. Print months1 [-1]

Result:

  1. ['1', '2', '3', '4', '5', '6', '7', '8', '9 ', '10', '11', '12']
  2. ('1', '2', '3', '4', '5', '6', '7', '8', '9 ', '10', '11', '12 ')
  3. <Type 'LIST'>
  4. <Type 'tuple'>
  5. 2
  6. 12

For the so-called index, anyone who has programming experience in other languages knows that there is something called an array subject. However, the index is more powerful and can be reversed, but not in other languages.

Of course, index is also the main feature of sequence.

To distinguish the two in form, brackets enclose the list and parentheses enclose the tuples.

Of course, here we also want to introduce a very powerful command dir:

Source code:

  1. Months1 = ["1", "2", "3", "4", "5", "6", "7", "8", "9 ", "10", "11", "12"]
  2. Mouths2 = ("1", "2", "3", "4", "5", "6", "7", "8", "9 ", "10", "11", "12 ")
  3. Print dir (months1)
  4. Print dir (mouths2)

Result:

  1. ['_ Add _', '_ class _', '_ ins INS _', '_ delattr __', '_ delitem _', '_ delslice _', '_ doc _', '_ eq _', '_ format __', '_ ge _', '_ getattribute _', '_ getitem _', '_ getslice _', '_ gt __', '_ hash _', '_ iadd _', '_ imul _', '_ init _', '_ iter __', '_ le _', '_ len _', '_ lt _', '_ mul _', '_ ne __', '_ new _', '_ reduce _', '_ performance_ex _', '_ repr _', '_ reversed __', '_ rmul _', '_ setattr _', '_ setitem _', '_ setslice _', '_ sizeof __', '_ str _', '_ subclasshook _', 'append', 'Count', 'extend', 'index', 'insert', 'pop ', 'delete', 'reverse', 'sort ']
  2. ['_ Add _', '_ class _', '_ ins INS _', '_ delattr __', '_ doc _', '_ eq _', '_ format _', '_ ge _', '_ getattribute __', '_ getitem _', '_ getnewargs _', '_ getslice _', '_ gt _', '_ hash __', '_ init _', '_ iter _', '_ le _', '_ len _', '_ lt __', '_ mul _', '_ ne _', '_ new _', '_ reduce _', '_ performance_ex __', '_ repr _', '_ rmul _', '_ setattr _', '_ sizeof _', '_ str __', '_ subclasshook _', 'Count', 'index']

All the methods available for this variable are listed here.

Parts

The difference between a shard and an index is that an index is used to access an element, while a shard is used to access a group of elements.

  1. Months = ["1", "2", "3", "4", "5", "6", "7", "8", "9 ", "10", "11", "12"]
  2. # From serial number 3 to serial number 5
  3. Print months [3: 6]
  4. # From serial number 0 to serial number 3
  5. Print months [: 3]
  6. Print months [0: 3]
  7. # Step Size
  8. Print months [: 1]
  9. Print months [: 2]
  10. Print months [: 3]

Result:

  1. ['4', '5', '6']
  2. ['1', '2', '3']
  3. ['1', '2', '3']
  4. ['1', '2', '3', '4', '5', '6', '7', '8', '9 ', '10', '11', '12']
  5. ['1', '3', '5', '7', '9', '11']
  6. ['1', '4', '7', '10']

Fragment sounds amazing. It's easy to start.

Two colons isolate three parts, starting position, ending position + 1, step

Arithmetic Operation

Source code

  1. A = [1, 2, 3]
  2. B = [4, 5, 6]
  3. Print a + B
  4. Print a * 5

Result:

  1. [1, 2, 3, 4, 5, 6]
  2. [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 3]

It is not difficult to understand.

Common functions
  1. A = [1, 2, 3]
  2. Print len ()
  3. Print max ()
  4. Print min ()

Result:

  1. 3
  2. 3
  3. 1
List
  1. A = [1, 2, 3]
  2. A. append (4)
  3. Print
  4. A = [,]
  5. Print a. count (1)
  6. Print a. count (2)
  7. Print a. count (3)
  8. A = [1, 2, 3]
  9. B = [4, 5, 6]
  10. Print a + B
  11. A. extend (B)
  12. Print
  13. Print a. index (4)
  14. A. insert (3, 'aaa ')
  15. Print
  16. Print a. pop ()
  17. Print
  18. Print a. remove ('aaa ')
  19. Print
  20. Print a. reverse ()
  21. Print
  22. A = [3,312,342, 9]
  23. Print
  24. A. sort ()
  25. Print

Result:

  1. [1, 2, 3, 4]
  2. 3
  3. 2
  4. 1
  5. [1, 2, 3, 4, 5, 6]
  6. [1, 2, 3, 4, 5, 6]
  7. 3
  8. [1, 2, 3, 'aaa', 4, 5, 6]
  9. 6
  10. [1, 2, 3, 'aaa', 4, 5]
  11. None
  12. [1, 2, 3, 4, 5]
  13. None
  14. [5, 4, 3, 2, 1]
  15. [3,312,342, 23, 6, 9]
  16. [3, 6, 9, 23,312,342]

For this large pair of methods, in addition to the dir method we mentioned above, there is also a method that can be used to view the usage of the method.

Code

  1. A = [1, 2, 3]
  2. Help (a. insert)

The result is as follows:

  1. Insert (...)
  2. L. insert (index, object) -- insert object before index
Tuples

Tuples are essentially a list that removes the modified content. All functions that can modify the content of a list are unavailable.

 

Related Article

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.