Home > Article > Backend Development > Python introductory learning data structure
一 Variables
Variables in python are different from variables in c/c++. In c/c++, the essence of a variable is the address of memory, but in python, when we define a variable and assign a value, it is as follows:
a='ABC'
The python interpreter does two things: (1) Create a string of 'ABC' in the memory; (2) Create a variable named a in the memory and point it to 'ABC'. In other words, python takes up more memory than c/c++. Here you must correctly handle the definition of variables in Python.
II Encoding
Regarding the issue of encoding, there are historical factors involved. Since computers were first manufactured in the United States, only 127 commonly used characters were encoded into the computer at first, which was ASCII code. But in later development, characters like Chinese need to be processed. At this time, a single byte is not enough, and at least 2 bytes need to be used. In order to handle all the languages in the world, Unicode was invented to solve this problem. Unicode usually uses 2 bytes to represent a character, and some remote characters may need to use 4 bytes.
Although the introduction of Unicode solves the encoding problem of multiple languages, there is still a waste of resources in use. If Unicode encoding is used uniformly, but all texts are in English, wouldn't it waste a lot of memory? To solve this problem, UTF-8 encoding was later introduced. In UTF-8 encoding, Unicode characters are allocated according to the number of bytes the characters need to occupy. For example, English letters are encoded into 1 byte, and Chinese characters are usually 3 bytes. This can save a lot of memory.
Three list and tuple
List and tuple are both built-in data types in python. The difference is that once the tuple is initialized, it cannot be changed. Define a list as follows:
#define a list
l=['Michael','Luffy','Nancy']
List is an ordered collection, just like an array in c/c++, the subscript Starting from 0, elements can be added and removed at any time. The operations that can be performed on the list are: append(), insert(), pop() and [].
Of course the list can be nested, as follows:
#define a nest list
l=['Michael','Luffy','Nancy', ['Corey','Jason']]
tuple Values must be assigned during initialization, as shown below:
#define a tuple
t=('Michael','Luffy','Nancy')
Here we need to explain the immutability of tuple. The immutability of the tuple is only for the "pointing" to its elements. For example, in the above example, the tuple points to 'Michael', 'Luffy' and 'Nancy' respectively. What cannot be changed is that it can only always point to these strings. It cannot point to other strings after definition.
Understanding the true meaning of "immutable", we can define a tuple whose content can be changed. As follows:
#define a alterable tuple
t=('Michael','Luffy','Nancy',['Corey','Jason'])
t[3][0]='Jefrey'
t[ 3][1]='Avery'
As you can see from the above code, first define a tuple named t. After modifying the data, the list pointed to during initialization is not modified, but the content of the list is changed. This is allowed for python.
IV dict
The dict in python is the map in c/c++, which is a structure composed of key/value pairs. What we need to pay attention to about dict is its key requirements. dict requires that the key must be an immutable object. This is because dict calculates the storage location of Value based on the key. If the same key is calculated each time and the result is different, then the inside of the dict will be confused. The algorithm for calculating the location through this key is called a hash algorithm.
In python, strings and integers are immutable objects, while lists are mutable and cannot be used as keys.
Five Postscript
The above is a personal summary of learning python. If there are any errors, please leave a message! ! !
The above is the content of data structure for introductory learning of Python. For more related articles, please pay attention to the PHP Chinese website (www.php.cn)!