list 、 Tuples 、 Strings are collectively referred to as sequences : Common ground :1. Each element can be retrieved by index 2. The index value of the first element is 0 3. You can get a range by slicing 4. There are many operators in common .
Depending on whether this feature can be modified , Sequences are divided into variable sequences and immutable sequences , A list is a variable sequence , Tuples and strings are immutable sequences .
stay Python Each object in has three basic properties : The only sign 、 type 、 value .
id(): The built-in functions are : Returns an integer value representing the unique identification of the specified object , For variable sequences ,id() The values of are all the same , Immutable sequence ,id() The return value is different
>>> s = [1, 2, 3]
>>> id(s)
1976242284864
>>> s *= 2
>>> s
[1, 2, 3, 1, 2, 3]
>>> id(s)
1976242284864
is & is not : Used to detect objects id Whether the values are equal , So as to determine whether it is the same object , Also known as the identity operator .
in & not in Determine whether the problem is included in Operator is to determine whether an element is contained in a sequence .
del Used to delete one or more specified objects , It can also be used to delete specified elements in a variable sequence . for example :
>>> x = [1, 2, 3, 4]
>>> del x[1:3]
>>> x
[1, 4]
The step value can also be set , Delete from beginning to end , The step value is 2
>>> x = [1, 2, 3, 4, 5]
>>> del x[: : 2]
>>> x
[2, 4]
list 、 Tuples 、 Functions for converting strings to and from each other :list() Convert to list ;tuple() Convert to tuple ;str() Convert to string .
min() & max() Compare incoming parameters , And return the minimum and maximum values ,
>>> t = "FishC"
>>> max(t)
's'
notes :26 In alphabetical order , The bigger the back , The encoding value of upper case letters precedes that of lower case letters , It is smaller than small letters .
len() & sum() Calculate the length , Sum up .
sorted() & reversed() Sort from small to large ,sorted() A new list is returned , The original list will not be affected , If you use s Of sort() Method , The list will be changed , And can only process lists .reversed() The function returns the inverse iterator of a parameter , Use list() Convert a reverse iterator to a list .
>>> s = [1, 2, 3, 0, 6]
>>> sorted(s)
[0, 1, 2, 3, 6]
>>> s.sort()
>>> s
[0, 1, 2, 3, 6]
notes :
>>> s = [1, 2, 5, 8, 0]
>>> reversed(s)
<list_reverseiterator object at 0x000001CC231531C0>
>>> list(reversed(s))
[0, 8, 5, 2, 1]