This study includes the following contents:
- list
- Definition of list
- List creation
- Add an element to the list
- Delete elements from the list
- Gets the element in the list
- Common operators for lists
- Other methods of listing
- tuple
- Create and access a tuple
- Update and delete a tuple
- Tuple related operators
- Built in method
- Decompress tuple
- character string
- Definition of string
- Slicing and splicing of strings
- Common built-in methods for string
- String formatting
- Dictionaries
- Variable type and immutable type
- Definition of dictionary
- Create and access Dictionaries
- Built in method of dictionary
- aggregate
- Creation of collections
- Accessing values in a collection
- Built in methods for collections
- Conversion of sets
- Immutable set
- sequence
- Built in functions for sequences
Difficulties:
1,m for i in range(start,stop,step)
Take start as the starting point, stop as the ending point, excluding stop, and step as the difference between the previous and subsequent terms, and then bring the results of i in range() into the m expression in turn.
Note: since the element of the list can be any object, the pointer of the object is saved in the list. Even if you save a simple [1,2,3], there are 3 pointers and 3 integer objects.
x = [0 for i in range(5)] print(x, type(x)) # [0, 0, 0, 0, 0] <class 'list'> x = [i for i in range(10)] print(x, type(x)) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] <class 'list'> x = [i for i in range(1, 10, 2)] print(x, type(x)) # [1, 3, 5, 7, 9] <class 'list'>
2, list.append(obj) adds a new object at the end of the list and accepts only one parameter. The parameter can be any data type. The appended element maintains the original structure type in the list.
list.extend(seq) appends multiple values in another sequence at the end of the list at one time (expands the original list with the new list)
x = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'] x.append('Thursday') print(x) # ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Thursday'] x = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'] x.extend(['Thursday', 'Sunday']) print(x) # ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Thursday', 'Sunday']
3,list.reverse() reverse the elements in the list, reverse -- collation, reverse = True , descending, reverse = False , ascending
list.sort(key=None, reverse=False) sorts the original list.
- key -- it is mainly used to compare elements. There is only one parameter. The parameters of the specific function are taken from the iteratable object. Specify an element in the iteratable object to sort.
- Reverse -- collation, reverse = True , descending, reverse = False , ascending (default).
- This method does not return a value, but sorts the objects in the list.
# Gets the second element of the list def takeSecond(elem): return elem[1] x = [(2, 2), (3, 4), (4, 1), (1, 3)] x.sort(key=takeSecond) print(x) # [(4, 1), (2, 2), (1, 3), (3, 4)]
4. Python tuples are similar to lists, except that tuples cannot be modified after they are created, which is similar to strings.
- When a tuple contains only one element, you need to add a comma after the element, otherwise the parentheses will be used as operators
x = (1) print(type(x)) # <class 'int'> x = () print(type(x)) # <class 'tuple'> x = (1,) print(type(x)) # <class 'tuple'>
5. The tuple size and content cannot be changed, so there are only two methods: count and index.
- count('python ') is recorded in tuple't'. This element appears several times, obviously once
- index(10.31) is the index to find the element in tuple , t , which is obviously 1
t = (1, 10.31, 'python') print(t.count('python')) # 1 print(t.index(10.31)) # 1
6. Common escape characters in Python
7. Python string formatting symbol
8. Formatting operator helper