🎏 This is the 12th day of my participation in the Gwen Challenge. Check out the details: Gwen Challenge
0 x00 📢 preface
Python is a programming language that knows how not to interfere with your programming. It’s easy to learn and powerful enough to build Web applications and automate boring stuff.
This article is an introduction to sequence types.
0 x01 sequences (sequence)
The most basic data structure in Python is a sequence. A sequence is a contiguous memory space that can hold multiple values in a certain order. Each element in the sequence has a number, its position or index. Each value can be accessed by the number of its location, called an index.
Sequence types include strings, lists, tuples, collections, and dictionaries, which support the following common operations. Collections and dictionaries do not support indexing, slicing, adding, and multiplying operations. Strings are also a common sequence that can also access characters within a string directly through an index.
Lists vs tuples
The main difference between lists and tuples is that lists can be modified, whereas tuples cannot. This means that lists are good for situations where you need to add elements halfway through, and tuples are good for situations where changing the sequence is prohibited for some reason.
Lists can be used instead of tuples in almost all cases. You cannot use lists instead of tuples when they are used as dictionary keys because dictionary keys are not allowed to be modified.
0x02 General operation
The index
A string literal can be indexed directly without first assigning it to a variable.
>>> 'Hello'[1]
'e'
Copy the code
If a function call returns a sequence, it can be indexed directly.
>>> yearnum = input('please input year: ') [3]
please input year: 2021
>>> yearnum
'1'
Copy the code
slice
Slice assignment
>>> name = list('Perl')
>>> name
['P'.'e'.'r'.'l']
>>> name[2:] = list('ar')
>>> name
['P'.'e'.'a'.'r']
>>> name = list('Perl')
>>> name[1:] = list('ython')
>>> name
['P'.'y'.'t'.'h'.'o'.'n']
Copy the code
Slice inserts/removes elements
>>> numbers = [1.5]
>>> numbers[1:1] = [2.3.4]
>>> numbers
[1.2.3.4.5]
Copy the code
Delete slices
>>> numbers[1:4] = [] # del numbers[1:4] equivalent
>>> numbers
[1.5]
Copy the code
add
The addition operator concatenates sequences
> > > [1.2.3] + [4.5.6]
[1.2.3.4.5.6] > > >'Hello,' + 'world! '
'Hello, world! '
Copy the code
multiply
When multiplying the sequence by a number x, the sequence is repeated x times to create a new sequence:
>>> 'python' * 5
'pythonpythonpythonpythonpython'
>>> [42] * 10
[42.42.42.42.42.42.42.42.42.42]
Copy the code
Checks whether a particular value is contained in the sequence
Use the in keyword to check whether a particular value is a member of a sequence.
>>> permissions = 'rw'
>>> 'w' in permissions
True
>>> 'x' in permissions
False
>>> users = ['mlh'.'foo'.'bar']
>>> input('Enter your user name: ') in users
Enter your user name: mlh
True
>>> subject = '$$$ Get rich now!!! $$$'
>>> '$$$' in subject
True
Copy the code
0x03 Common Methods
clear
The clear method clears the list in place.
>>> lst = [1.2.3]
>>> lst.clear() LST [:] = []
>>> lst []
Copy the code
copy
Method copy copies a list
>>> a = [1.2.3]
>>> b = a.copy() # equivalent to a[:] or l ist(a)
>>> b[1] = 4
>>> a
[1.2.3]
Copy the code
extend
The extend method lets you append multiple values to the end of the list at once. You can use one list to extend another list
>>> a = [1.2.3]
>>> b = [4.5.6]
>>> a.extend(b) [len(a):] = b a=a+b
>>> a
[1.2.3.4.5.6]
>>> a + b
[1.2.3.4.5.6]
>>> a
[1.2.3]
Copy the code
insert
The insert method is used to insert an object into the list.
>>> numbers = [1.2.3.5.6.7]
>>> numbers.insert(3.'four') Numbers [3:3] = ['four']
>>> numbers
[1.2.3.'four'.5.6.7]
Copy the code
pop
The pop method removes an element from the list (the last element by default, and an index can be specified) and returns it. Pop is the only list method that both modifies the list and returns a value other than None.
>>> x = [1.2.3]
>>> x.pop()
3
>>> x [1.2]
>>> x.pop(0)
1
>>> x
[2]
Copy the code
reverse
The reverse method sorts the elements in the list in reverse order
>>> x = [1.2.3]
>>> x.reverse()
>>> x
[3.2.1]
Copy the code
sorted
The sorted method is used to sort a list in-place. Sorting in place means modifying the original list so that its elements are sorted, rather than returning a copy of the sorted list. The sorted function can be used with any iterable object
>>> x = [4.6.2.1.7.9]
>>> y = sorted(x) Y = x.opy () => y.sort()
>>> x
[4.6.2.1.7.9]
>>> y
[1.2.4.6.7.9]
>>> sorted('Python')
['P'.'h'.'n'.'o'.'t'.'y']
Copy the code