Data structures and algorithms under Python---1: let everything begin with nothing to do

Source: Internet
Author: User

I also forgot about how long it was, like a little over three weeks, and finally finished reading data Structure and algorithms with Python as well as problem solving with algorithms and datastructures (figure That part did not look carefully, too difficult too time, graduation design began, a little busy). "GitHub address, which contains the two books with the note-book and the following 0 code"

So, this is the first summary note from the point of the beginning (which is referred to in the two books Python-related but the data structure and algorithm-independent stuff)

Directory:

0: There are some things

A: A few sentences for Python summary of the Idle language

Two: What happens to the Python assignment statement

Three: Two Methods of Class: Access method and Mutation method

Four: Python's variable lookup mechanism

Five: Python program running memory mechanism

* Five: operator overloading

* Six: Python class built-in methods

0: Some things:

A: A few sentences for Python's Idle language summary:

Python is an explanatory language, with an interpreter (interpreter) running

Python is a dynamic language, and no fixed variable type is required in the program, and different types can be called before and after the same variable.

Python is an object-facing language, and all of its data structures are objects. (Note: If you see a document that encounters type, it is actually a class)

Two: What happens to the Python assignment statement

Assignment statement: x = 6

In fact, the process is to establish a point (reference), the left is pointing, the right side is an object or a pointer to the object, you can have more than one point to the same object (object), [note the meaning of the object here, can be values, instances, functions, etc.],

In this case, a variable called x (variable) is created to have the reference of Object 6.

Attention:

>>> a=[1,2,3,4]

>>> B=a

>>> A[0]=none

>>> b

[None, 2, 3, 4]

As can be seen here, the point is actually pointing to the physical address of a pile of things, a change has changed.

Three: two Ways of Class

There are two approaches in all object-facing languages:

Access methods (accessor method) and mutation methods (Mutator method)

The access method obtains the object to use but does not alter the object, and the mutation method is to change the object:

>>> A[::-1]

[5, 4, 3, 2, 1]

>>> A

[1, 2, 3, 4, 5]

>>> A.reverse ()

>>> A

[5, 4, 3, 2, 1]

Actually wrote the class can find the difference between the two, the approximate access method is used to return some of the desired value, the mutation method is used to change the object content, generally can not return.

If there is no access method for a class, it means that data cannot be taken, which is meaningless. And if there is no mutation method for a class that is immutable data, str,int, and so on data types.

Four: Python's variable lookup mechanism

Python variable lookup is first divided into two steps: First in the local scope lookup, such as the Green Val, first in the green area to find, no longer in the encloding scope lookup, that is, the light red area

In front of the local scope and enclosing scope, in fact we are most familiar with the global scope, that is, globally variable

Usage:

X=0

def AHA ():

Global X

...

Finally there is the Python build-in scope:

For example, write X=int (' 6 ') in the function,

Will look in turn to see if the object int function is in local scope,enclosing scope,

Global scope, last look build-in scope

Summing up is the LEGB of the search order.

V: Python's memory mechanism

It can also be seen in fact that each function or class has its own memory space, in fact, Python at run time, will use two parts to run, it is in RAM (random memory), two parts is a heap (heap) and a run-time stack (time run heap), When the Python interpreter (interpreter) executes to a function, it presses its corresponding activation record into the Run-time stack and opens a new scope, noting that all local Scope-owned objects are stored in the heap, with their references in the Activation record, and once the function is executed, the Activation record pops up and the next entry points to another scope. See

* When it comes to ram, if you don't have a concept, it's important to know that he can access an arbitrary address at an O (1) time, which directly explains why some data structures access elements with a time complexity of O (1)

E.g: look at the following recursive memory processing process

def recsumfirstn (n):

if n = = 0:

return 0

return recsumfirstn (n-1) + N

Five and six are *, I think the content is more, and so I have two days to get them out of a separate record to write down, here first mention, put two pictures show very important, especially sixth

Five: operator overloading

VI: Python class built-in methods

Suddenly did not find the summary of the figure, two days found the upload

Data structures and algorithms under Python---1: let everything begin with nothing to do

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.