Baptism of the soul, practice python (6)-live code + list, python live
Active usage:
Use input built-in functions
Note: In python2 and python3, the input function is not the same. In python2, the input type is the same as the input type, and in python3, the input type is the string, this is the same as the raw_input function in python2, and there is no raw_input in python3.
With input, you can interact with users and make the code live.
List, what is a list, the feature of the List is brackets, just like the feature of a string is a quotation mark
You can create a list using either of the following methods:
List Method:
The underlined method has special significance.
Therefore, common list methods include append, count, extend, index, insert, pop, remove, reverse, and sort.
Append: adds a list element, but only one object can be added at a time.
Note that [only one object can be added at a time]
Count: used for counting
Extend: add another list and merge it with the added list.
Index: returns the index (subscript) location of the element in the list.
Note that the index starts from 0 and counts from left to right. Therefore, element 2 is at Index 1, which is not commonly recognized as element 2.
Insert: insert element
The first parameter in insert is the index, and the second parameter is the element to be inserted.
Pop: If no index parameter is added, the elements at the end are deleted and printed. If an index parameter is added, the corresponding index parameters are deleted and printed:
Remove: specify an element and delete it.
Reverse: sorts the elements in the list in reverse order.
Sort: by default, the list is sorted by ASCII encoding from small to large.
Therefore, if you want to sort data in a descending order, you can use sort and reverse to flip it over.
However, it is not so troublesome because the sort method also has a parameter, reverse. This reverse is not the one above. This reverse is a parameter in sort because its parameter is equal to False, that is, the default value is false, so from small to large, if the reverse is equal to True, it can be directly from large to small. The usage is almost the same as that of the sorted function. Just look at the supplementary content below.
The list is a mutable object that supports the modification of the original location. What is the original modification? After the above method is used, the corresponding id of the memory space remains unchanged:
Additional content:
1. sorted Function
Sorted and sort functions are similar, but sorted is a built-in function, and sort is only a method in the list.
Example:
In the reverse parameter, the default value is false, indicating that the default value is sorted from small to large. If it is set to True, the effect is as follows:
Note that using the sorted function only prints the result without changing its object:
If you want to change the value, you need to assign the result obtained by sorted to the variable:
Sorted will play a major role in the future.
2. List slice (list slice): obtains multiple list elements at a time.
[] Indicates that index position 3 ends from index position 1, but does not include
The parameter in the slice is the index, and the object will not be changed after the slice. If you want to change it, assign a new value.
What if I want to cut out the entire list?
In this way, index 3 represents the last element, but it cannot be split. Because it is not included, it can be like this:
If the element after the colon is omitted, the last element can be taken out. Of course, you will think, you can omit the latter, can you omit the former? Yes:
Can I omit them all? Yes:
If they are omitted, the whole slice can be taken out or called out. Does it seem a bit like copying the whole image? Yes, it is a copy, and we will talk about deep copy later, we will continue to explain this when copying a copy.
Another problem is that I don't want to take it in this way. I just want to take some of them. For example, what should I do if I want to take an index of 1, 3, 5, or, 4?
Based on the index 0, 2, 4:
What does this [: 2] mean by writing ?, The first colon knows that directly copying the whole, and the second colon is followed by the step length. In general, every step of the average person is a step distance, some people have to go two steps long, and take two steps to achieve the effects of 0, 2, and 4.
Of course, it is also possible to take 1, 3, and 5:
This list has fewer elements. For more elements, see:
Of course, the step size can also be changed. You can take one step in three steps and one step in four.