Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the knowledge of Python data structure?

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article is to share with you about the relevant knowledge of Python data structure, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article. Let's take a look at it.

Today we will learn about the data structure of python.

1. Module first acquaintance

The power of Python is that it has very rich and powerful standard libraries and third-party libraries.

The general standard library is in the Lib directory, and the third-party library is in site-packages.

1.1 sys module (sys module of the python interpreter, written in c voice, no sys.py in the Lib directory)

The sys module is mainly used to provide operations related to the python interpreter, such as:

Examples of usage:

1.2 os Modul

The os module provides functional interface functions of most operating systems. When the os module is imported, it will adapt to different operating system platforms, according to different platforms for corresponding operations, in python programming, often deal with files, directories, so can not be separated from the os module. Python programming, often deal with files, directories, this is inseparable from the os module.

Examples of usage:

Can you understand what the following code means? Leave a message at the end of the article to communicate together.

2. Python operation mechanism

We know that python is an interpreted language, so what is the generated * .pyc file? C should be the abbreviation of compiled.

Some interpretive languages can also optimize the whole program when translating the program through the optimization of the interpreter, which is close to the compiled language in efficiency.

In addition, with the rise of virtual machine-based languages such as Java, we cannot simply divide languages into interpretive and compiled languages.

Using Java as an example, Java is first compiled into a bytecode file by a compiler and then interpreted into a machine file by an interpreter at run time. So we say that Java is a language that compiles and then interprets.

In fact, Python, like Java/C#, is a virtual machine-based language.

A brief introduction to the running process of Python

When the python program is running, the result of the compilation is saved in the PyCodeObject in memory, and when the Python program is finished, the Python interpreter writes the PyCodeObject back to the pyc file.

When the python program runs for the second time, the program will first look for the pyc file on the hard disk, and if it finds it, it will load it directly, otherwise it will repeat the above process.

Determine the update time and execute py or pyc.

It is said that pyc file is actually a persistent storage method of PyCodeObject.

3. Data type

Int (Integer)

On a 32-bit machine, the number of bits of an integer is 32 bits, and the range of values is

-2 ^ 31 ~ 2 ^ 31-1

On a 64-bit system, the number of bits of an integer is 64 bits, and the range of values is

-2 ^ 63 ~ 2 ^ 63-1

This is why 32-bit systems have a maximum memory of 4 gigabytes.

4G=4*1024M*1024k*1024-1 = 2 ^ 31-1

Long (long Integer)

Starting from Python2.2, if an integer overflows, Python automatically converts the integer data to a long integer

Python3 no longer distinguishes int from long.

Float (floating point)

Floating-point numbers are used to deal with real numbers, that is, numbers with decimals.

A floating point number is a numerical representation of a number that belongs to a specific subset of rational numbers. Decimals include rational and irrational numbers.

So floating-point numbers must be decimals, and decimals are not necessarily floating-point numbers.

Floating-point computation refers to the operation in which floating-point numbers are involved, which is usually accompanied by approximation or rounding because it cannot be accurately represented.

Complex number

It consists of the real part and the imaginary part, the general form is x+yj, where x is the real part of the complex number, y is the imaginary part of the complex number, where x and y are real numbers.

Boolean value

String

Perhaps the most important new feature of Python 3 is a clearer distinction between text and binary data. The text is always Unicode, represented by the str type, and the binary data represented by the bytes type. Python 3 does not mix str and butes in any implicit way, and because of this, python 3 cannot concatenate strings and character packages, search for strings in byte packets, or pass functions with arguments to byte packages.

Str is converted to bytes-encode

Bytes is converted to str-decode

Python data transmission, are all binary data transmission.

Determine the type of a variable

4. String operation

Case processing

String format judgment

Formatted output

Enter processing of character spaces

string manipulation

Other

5. List, tuple operation

List

Tuple

Tuple (): also known as a read-only list, there are only two count index methods.

6. Dictionary operation

Dictionary A key-value data type

Syntax:

Characteristics of dictionaries:

a)。 Dict is unordered.

b)。 Key must be unique. So is born to be heavy.

Commonly used:

Ergodic dictionary

Fromkeys usage

7. Deep copy and shallow copy

7.1 numbers and strings

As mentioned in the previous article: numbers and strings cannot be modified once they are created. if you replace a string, it will only recreate a string in memory, but there will be no change for the original string.

7.2 data structures such as dictionaries, lists, etc.

Let's look at an example:

Think about this: if you make the following changes to the value of alphabet, will the values of the other four variables change?

Let's take a look at the values of the other four variables:

7.3 to sum up

Direct "=" assignment, all point to the same memory address, alphabet change, alphabet3 also change. Different from simple numbers and strings

List.copy (), like copy.copy () of the copy module, is shallow copy, copy only the first layer, the lower layer is just the copy memory address, and the source variable is changed if it is changed deeply.

Full clone, not only copy the memory address, then deep copy, use the copy.deepcopy () of the copy module

The above is the relevant knowledge of Python data structure, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report