Dictionary
- . Dictionary is one of the built-in data types in Python, and he defines the relationship between the key and the value.
- Each element has a key-value pair, and the entire element set is enclosed in curly braces {}.
- You can get value from key, but you cannot get the key through value. The reason is: Dictionary inside key is unique, but value is not unique.
- In a dictionary, you cannot have two identical keys at the same time, assigning a value to an existing key overrides the original value, and you can add key-value pairs at any time.
- When used in dictionary, key is sensitive to case. Key and key are considered to be different keys.
- Dictionary is not just for storing strings. The value of dictionary can be any data type, including strings, integers, objects, and even other dictionary. In a dictionary, the data type of value can be different and can be mixed and matched as needed. Dictionary's key requires much more rigor, but he can also be a string, integer, and other type. You can also mix and match key data types in a dictionary.
- Del allows you to remove individual elements from a dictionary using key.
- Clear COM you and a dictionary inside remove all the elements. The empty curly brace collection represents the dictionary of an element.
- List
- A list is a collection of ordered elements enclosed in square brackets [].
- The list can be used as an array starting with the 0 subscript. The first element of any non-empty list is always list[0]
- The index of a negative number starts counting forward from the end of the list to access the element. Any of the last elements of a non-empty list is list[-1]
- The list can be split using List.slice () and List[1:3], and a list can be copied directly using the list ":"
- Append can add elements to the end of the list
- Insert to move a single element to the fixed position of the list
- extend is used to connect to list. Note that you do not use multiple parameters to invoke extend, which is called using a list parameter.
- Lists's two methods extend and append look similar, but in fact they are completely different. extend accepts a parameter, which is always a list, and adds each element in the list to the original list
- On the other hand, append accepts a parameter, which can be any data type, and is simply appended to the tail of the list. Call the append method here using a list parameter containing 3 elements.
- Index finds the first occurrence of a value in the list and returns the index value.
- To test whether a value is inside the list, use in, and return TrueIf the value exists, otherwise return to False .
- Remove removes the first occurrence of a value from the list.
- Pop is an interesting thing. It does two things: delete the last element of the list, and then return the value of the deleted element. Note that this differs from Li[-1] , which returns a value but does not change the list itself. Also differs from li.remove (value), which changes the list but does not return a value.
- Lists can also be connected with the + operator. List = list + otherlist equivalent to list. Extend (otherlist) . But the + operator returns a new (connected) list as a value, and extend only modifies the existing list. In other words, for large lists, the extend executes faster.
- Python supports the + = operator. Li + = [' both '] is equivalent to li.extend ([' both ']). The + = operator can be used for lists, strings, and integers, and it can also be overloaded for user-defined classes.
- The * operator can act as a repeating device on the list. Li = [1, 2] * 3 equals to li = [1, 2] + [1, 2] + [1, 2], and three lists are connected as one.
Touple
- A tuple is an immutable list. One is that creating a tuple cannot change it in any way.
- A tuple is defined in the same way as a list, except that the entire set of elements is surrounded by parentheses rather than square brackets.
- The elements of a tuple are sorted in the same order as the list. The tuples index is the same as list starting from 0, so the first element of a non-empty tuple is always t[0].
- A negative index is counted as a list from the tail of a tuple.
- As with list shards (slice) can also be used. Note that when a list is split, a new list is obtained, and when a tuple is split, a new tuple is obtained.
- Tuple has no method: No append or extend method, no remove or pop method, no index method, can use in to see if an element exists in a tuple.
The difference between list, tuple, and dict in Python