Traditional Culture Encyclopedia - Traditional festivals - Holidays can make you a Ph master in seconds

Holidays can make you a Ph master in seconds

Understand these questions and you will become a Python master in seconds. Come and learn.

Understand these questions and you will become a Python master in seconds

1. Why use Indentation to group statements?

Guido van Rossum believes that using indentation to group statements is very elegant and greatly

improves the clarity of ordinary Python programs. Most people learn and like this feature after a while.

Since there are no opening/closing brackets, there will be no divergence between the parser-perceived grouping and the human reader.

Occasionally C programmers will encounter code snippets like this:

Only the x++ statement is executed if the condition is true, but the indentation would make you think that is not the case.

Even experienced C programmers sometimes stare at it for a long time and wonder

why y is decreasing even though x > y.

Because there are no opening/closing brackets, Python is less prone to coding conflicts

In C, parentheses can be placed in many different places. If you're used to reading and writing code in one style, you'll feel at least a little uneasy when reading (or being asked to write) code in another style. .

Many coding styles place opening/closing brackets on a line by themselves. This makes the program quite long, wastes valuable screen space, and makes it more difficult to fully understand the program

Understanding these issues will make you a Python master in seconds

Understood. Ideally, the function should fit on one screen (for example, 20--30 lines

). 20 lines of Python can accomplish more than 20 lines of C. It's not just

due to missing opening/closing brackets - missing declarations and advanced data types are also

cause - but indentation based on syntax definitely helps.

2. Why do simple arithmetic operations produce strange results?

Please see the next question.

3. Why are floating point calculations inaccurate?

Users are often surprised by results like this:

And think this is a bug in Python. This is not the case. This has less to do with

Python and more to do with how the underlying platform handles floating point numbers.

The float type in CPython is stored using the double type of the C language.

The value of a float object is a binary floating point number stored with a fixed precision (usually 53 bits).

Since Python uses C operations, the latter relies on < /p>

Hardware implementation to perform floating point operations. This means that as far as floating point operations are concerned, Python behaves like many popular languages, including C and Java.

Many numbers that can be easily represented in decimal cannot be represented in binary floating point.

For example, after entering the following statement:

Understand these questions and you will become a Python master in seconds

>>>x=1.2

The value stored for x is an approximation (very close) to the decimal value of 1.2, but not exactly

it. On a typical machine, the actual stored value is:

1.8811601198116911901160119911891190118011001180110011

(binary)

This corresponds to the decimal value:

1.199999999999999555910790149937383830547332763671875

(decimal)

The typical 53-bit precision provides 15-16 decimal digits of precision for Python floating point numbers

-4. Why Python strings Is it immutable?

Has several advantages.

One is performance: knowing that strings are immutable means that we can allocate space for it when

is created, and the storage requirements are fixed. This is one of the reasons for the difference between tuples and

lists.

Another advantage is that strings in Python are considered as "basic" as numbers

. No action will change the value 8 to another value. In Python, no action will change the string "8" to any other value.

5. Why must "self" be used explicitly in method definitions and calls?

This idea is borrowed from the Modula-3 language. It has proven to be very

useful for many reasons.

Understand these issues and you will become a Python master in seconds

First of all, it is more obvious that methods or instance properties are used instead of local

variable. Reading self.x or self.meth() makes it clear that instance variables or methods are used even if you don't know the class definition. In C++, this can be determined by

the lack of local variable declarations (assuming that global variables are rare or easily identifiable)

--but in Python there are no local variable declarations, so it must Find the class definition

to determine. Some C++ and Java coding standards require instance attributes to have the m_ prefix, so this explicitness is still useful in these languages.

Second, this means that if you want to explicitly reference or call the method from a specific class, no special syntax is required

. In C++, if you want to override a method in a base class in a derived class, you have to use the :: operator -- in Python you can write

baseclass .methodname(self, ) . This is useful for in it() methods,

especially when a derived class method wants to extend a base class method of the same name, but must call the base class method in some

way .

Finally, it solves the syntactic problem of variable assignment: for local variables in Python

those variables (by definition!) are assigned in the function body (and are not explicitly < /p>

Declaring an assignment as global) must somehow tell the interpreter that an assignment is

In order to assign an instance variable rather than a local variable, it is best to pass the statement

p>

method (for efficiency reasons). C++ does this through declarations, but Python does not have declarations, and it would be a shame to introduce them just for this purpose.

Using explicit self.var solves this problem well. Similarly, for using

Understand these problems, you will become a Python master in seconds

instance variables, you must write self var which means the unqualified name inside the method

References do not have to search the instance's directory. In other words, local variables and instance variables exist in two different namespaces, and you need to tell Python which namespace to use.

6. Why cannot assignment be made in expressions?

The reason why assignment is not allowed in Python expressions is common in other languages.

The hard-to-find error is caused by this structure:

The error is a simple typo: x=0, assigning 0 to variable x, and comparing x==

0 It's certainly to be expected.

There have been many proposals for alternatives. Most are hacks to save typing, but use arbitrary or implicit syntax or keywords and do not meet the simple criteria for language change proposals: It should intuitively provide the correct meaning to human

readers who have not yet been introduced to the concept.

The best way is to use an iterator, so that you can loop through the object through a for statement

. For example, file objects support the iterator protocol, so it can be simply written as:

Understand these issues and you will become a Python master in seconds

7 Why does Python have problems with certain functions (such as list. index() ) is implemented using the

method, while other functions (such as len(List) ) are implemented using the function

?

(a) For some Operations, prefix notation is easier to read than suffix - prefix (and infix!) operations have a long tradition in mathematics, serving as visual aids to mathematicians

Notation for thinking questions. Compare how easily we can rewrite a formula like x*(a+b) as x_a+x_b with how clumsily we can do the same thing using the original 00 notation. ”

(b) When you read code that says len(X), you know that it is asking for the length of something. This tells us two things: The result is an integer, and the argument is some kind of container

In contrast, when reading x.len(), you must already know that x is some kind of container that implements the interface, or it is. Containers that inherit from a class with standard len(). When a class that does not implement mapping has a get() or key() method, or a class that is not a file has a write() method

, we occasionally get confused.

8. Why is join) a string method instead of a list or tuple method?

Join() is a string method, because in When using this method, you tell the delimiter character

string to iterate over a sequence of strings and insert itself between adjacent elements. The parameters of this method can be any object that follows the sequence rules, including any new class you define. There are similar methods for bytes and byte array objects.

Understand these questions and you will become a Pytho master in seconds

9. Why do lambda expressions not contain statements?

Python’s lambda expressions cannot contain statements. , because Python's syntax

framework cannot handle statements nested inside expressions. However, in Python,

this is not a serious problem. Unlike lambda forms in other languages ??that add functionality, Python's lambdas are just shorthand if you're too lazy to define the function.

Functions are already first-class objects in Python and can be declared in the local scope.

So the only advantage of using a lambda instead of a locally defined function is that you don't

need to create a name for the function - it's just an allocated function object (as with

The lambda expression generates local variables with the same object type!

10. Isn’t it possible to simulate threads in the interpreter instead of relying on the operating system?

Is the thread implementation of ?

Answer 1: Unfortunately, the interpreter pushes at least one

C stack frame for every Python stack frame. Additionally, extensions can call back into Python at any time. Therefore, a complete thread implementation requires C thread support.

Answer 2: Fortunately, Stackless Python has a completely redesigned

interpreter loop that avoids the C stack.