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:
- # Coding: UTF-8
- Months1 = ["1", "2", "3", "4", "5", "6", "7", "8", "9 ", "10", "11", "12"]
- Mouths2 = ("1", "2", "3", "4", "5", "6", "7", "8", "9 ", "10", "11", "12 ")
- Print months1
- Print mouths2
-
- Print type (months1)
- Print type (mouths2)
-
- Print months1 [1]
- Print months1 [-1]
Result:
- ['1', '2', '3', '4', '5', '6', '7', '8', '9 ', '10', '11', '12']
- ('1', '2', '3', '4', '5', '6', '7', '8', '9 ', '10', '11', '12 ')
- <Type 'LIST'>
- <Type 'tuple'>
- 2
- 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:
- Months1 = ["1", "2", "3", "4", "5", "6", "7", "8", "9 ", "10", "11", "12"]
- Mouths2 = ("1", "2", "3", "4", "5", "6", "7", "8", "9 ", "10", "11", "12 ")
- Print dir (months1)
- Print dir (mouths2)
Result:
- ['_ 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 ']
- ['_ 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.
- Months = ["1", "2", "3", "4", "5", "6", "7", "8", "9 ", "10", "11", "12"]
- # From serial number 3 to serial number 5
- Print months [3: 6]
- # From serial number 0 to serial number 3
- Print months [: 3]
- Print months [0: 3]
- # Step Size
- Print months [: 1]
- Print months [: 2]
- Print months [: 3]
Result:
- ['4', '5', '6']
- ['1', '2', '3']
- ['1', '2', '3']
- ['1', '2', '3', '4', '5', '6', '7', '8', '9 ', '10', '11', '12']
- ['1', '3', '5', '7', '9', '11']
- ['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
- A = [1, 2, 3]
- B = [4, 5, 6]
- Print a + B
- Print a * 5
Result:
- [1, 2, 3, 4, 5, 6]
- [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 3]
It is not difficult to understand.
Common functions
- A = [1, 2, 3]
- Print len ()
- Print max ()
- Print min ()
Result:
- 3
- 3
- 1
List
- A = [1, 2, 3]
- A. append (4)
- Print
- A = [,]
- Print a. count (1)
- Print a. count (2)
- Print a. count (3)
- A = [1, 2, 3]
- B = [4, 5, 6]
- Print a + B
- A. extend (B)
- Print
- Print a. index (4)
- A. insert (3, 'aaa ')
- Print
- Print a. pop ()
- Print
- Print a. remove ('aaa ')
- Print
- Print a. reverse ()
- Print
- A = [3,312,342, 9]
- Print
- A. sort ()
- Print
Result:
- [1, 2, 3, 4]
- 3
- 2
- 1
- [1, 2, 3, 4, 5, 6]
- [1, 2, 3, 4, 5, 6]
- 3
- [1, 2, 3, 'aaa', 4, 5, 6]
- 6
- [1, 2, 3, 'aaa', 4, 5]
- None
- [1, 2, 3, 4, 5]
- None
- [5, 4, 3, 2, 1]
- [3,312,342, 23, 6, 9]
- [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
- A = [1, 2, 3]
- Help (a. insert)
The result is as follows:
- Insert (...)
- 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.