The data structure of the Python introductory learning

Source: Internet
Author: User
A variable

The variables in Python are different from the variables in C + +. In C + +, the nature of a variable is the address of the memory, but in Python, when we define a variable and assign a value, the following:

A= ' ABC '

The Python interpreter did two things: (1) Create a string of ' abc ' in Memory, (2) Create a variable named a in memory and point it to ' ABC '. In other words, Python consumes more memory than C + +. It is important to handle the defined variables in Python correctly.

Two-coded

For the coding problem, there are historical factors inside. Since the computer was originally made in the United States, it was the first to encode the usual 127 characters into the computer, the ASCII code. However, in later development, you also need to deal with characters such as Chinese, when a single byte is not enough, at least 2 bytes are required. In order to deal with all the languages in the world, Unicode was invented to solve this problem. Unicode typically uses 2 bytes to represent one character, and an individual remote may need to use 4 bytes.

The introduction of Unicode solves the problem of coding in many languages, but there is still a waste of resources in the use. If Unicode encoding is used uniformly, but all the text is in English, wouldn't it be a waste of memory? In order to solve this problem, UTF-8 encoding was introduced later. In UTF-8 encoding, Unicode characters are allocated according to the number of bytes that a character needs to occupy, such as an English letter encoded in 1 bytes, and a Chinese character is usually 3 bytes. This will save a lot of memory.

Three list and tuple

Both list and tuple are a data type built into Python. The difference is that a tuple cannot be changed once it is initialized. Define a list as follows:

#define a list
l=[' Michael ', ' Luffy ', ' Nancy '

A list is an ordered collection that, like an array in C + +, starts at 0 and can be added and removed at any time. The actions that can be performed on the list are: Append (), insert (), Pop (), and [].

Of course, the list can be nested defined, as follows:

#define A nest List
l=[' Michael ', ' Luffy ', ' Nancy ', [' Corey ', ' Jason ']

A tuple must be assigned at initialization time, as follows:

#define a tuple
t= (' Michael ', ' Luffy ', ' Nancy ')

Here we need to explain the non-change of the tuple. The immutable nature of a tuple is only directed toward its elements, such as the example above, where the tuple points to ' Michael ', ' Luffy ', and ' Nancy ', and cannot be changed until the string is pointed to, but cannot be pointed to other strings after the definition.

Understanding the true meaning of "immutable", we can define a tuple that can transform the content. As follows:

#define a alterable tuple
t= (' Michael ', ' Luffy ', ' Nancy ', [' Corey ', ' Jason '])
t[3][0]= ' Jefrey '
t[3][1]= ' Avery '

As can be seen from the above code, a tuple named T is defined first. The list that was pointed to when the initialization was not modified after the data has been modified, only changes the contents of the list, which is allowed for Python.

Four Dict

The dict in Python is the map in C/s + +, which is a structure of key/value pairs. For dict we need to be aware of its requirements for key, DICT requires that key must be an immutable object. This is because Dict calculates the storage location of value based on key, and if each calculation of the same key results in a different result, the interior of the dict is chaotic, and the algorithm that computes the position by this key is called the hashing algorithm.

In Python, strings and integers are immutable objects, and list is mutable and cannot be used as a key.

Five PostScript

These are personal learning python summary, if there are errors, please leave a message!!!

The above is the introduction of Python learning data structure content, more related articles please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.