First, Python introduction

Source: Internet
Author: User

one, low-level language and high-level language

The original computer programs are expressed in sequence 0 and 1, the programmer is directly using the machine instructions, without translation, from the tape punch input can be executed to achieve results. Later, in order to facilitate the memory, will be used in 0, 1 sequence of machine instructions are signed to help remember, these and machine instruction one by one corresponding mnemonic is the assembly instructions, resulting in the birth of assembly language. Both machine instructions and assembly instructions are machine-oriented, collectively referred to as low-level languages. Because it is a mnemonic for machine instructions for a particular machine, assembly language cannot be independent of the machine (a particular CPU architecture). But assembly language is also to be translated into machine instructions to execute, so there are also the assembly language running on one machine to run on another machine instructions on the method, that is, cross-assembly technology.

High-level language is a computer language from the perspective of human logical thinking, the degree of abstraction is greatly improved, It needs to be compiled into the target code on a specific machine to execute, and a high-level language statement often requires several machine instructions to complete. High-level languages are independent of machine-specific features that are implemented by the compiler to generate different object code (or machine instructions) for different machines. In particular, how to compile the high-level language to what extent, which is related to the compilation of technology, can be compiled into directly executable object code, can also be compiled into an intermediate representation, and then get to different machines and systems to execute, this situation often need to support the environment, such as the interpreter or virtual machine support, It is a good example that Java programs are compiled into bytecode and then executed by virtual machines on different platforms. Therefore, the high-level language is not dependent on the machine, refers to the different machines or platforms in the high-level language program itself is unchanged, and through the compiler compiled by the target code to adapt to different machines. In this sense, through cross-compilation, some assembler can also obtain the portability between different machines, but this approach is far less portable than the high-level language to facilitate and practical.


Ii. Compilation and interpretation

Compilation is the translation of the source program into an executable target code, translation and execution are separate, and interpretation is the source program translation and execution of a one-time completion, do not generate the target code can be stored. This is only the appearance, the biggest difference between the two is: for the interpretation of execution, the control of the program runtime in the interpreter and not the user program, to compile execution, run-time control in the user program.

Interpretation has good dynamic characteristics and portability, such as the ability to dynamically change the type of variables during interpretation, modify the program, and insert good debugging diagnostic information into the program, and then port the interpreter to a different system, then the program can run on the system porting the interpreter without modification. At the same time, the interpreter also has a lot of shortcomings, such as inefficient execution, occupy large space, because not only to the user program to allocate space, the interpreter itself also occupies a valuable system resources.

The compiler compiles each statement of the source program into a machine language and saves it as a binary file so that the computer can run the program directly in machine language at a very fast speed.
And the interpreter is only in the execution of the program, only one interpretation of the machine language to the computer to execute, so the speed is not as fast as the compiled program to run.

compile-and-interpret
We first look at the compiler, in fact, it is the same as the assembly language: There is a translation program to convert our source code to generate the corresponding executable code. This process is a bit more professional, called compilation (Compile), and the compiler responsible for compiling is naturally called the compiler (Compiler). If we write the program code is contained in a source file, then usually compiled after the direct generation of an executable file, we can run directly. But for a more complex project, for ease of management, we usually distribute the code in various source files as different modules to organize. When you compile individual files, you generate the target files (object file) instead of the executable file that you said earlier. Typically, a source file is compiled with a target file. The contents of these target files are basically executable code, but because they are only part of the entire project, we cannot run them directly. After all the source files have been compiled, we can finally "package" these semi-finished target files into an executable file, which is the responsibility of another program, because this process seems to be to assemble the target file containing executable code, so called link, And the program that is responsible for linking is called ... called the link Program (Linker). Link program in addition to link to the target file, there may be a variety of resources, such as icon files Ah, sound files ah what, but also responsible for removing the target file redundancy between duplicate code, and so on, so ... is also very tired. Once the link is complete, you will generally get the executable file we want.

We've probably introduced the features of the compiled language, and now we're going to look at the explanatory type. Well, literally, "compilation" and "interpretation" really mean "translation", but the difference is that the timing of the translation is not the same. For example: If you are going to read a foreign language book, and you do not know it, then you can find a translator, give him enough time to translate the whole book from beginning to end, and then give you the native language version of the book to read, or you can immediately let the interpreter assist you to read, let him sentence to you to translate, If you want to look back at a chapter, he will have to re-translate it for you.

Two ways, the former is equivalent to what we just said the compiler: once all the code into the machine language, and then written into the executable, and the latter is equivalent to the interpretation of the type: in the first moment of the program run, there is only the source program and no executable program, and every execution of the program to the source program, a command, There will be a shell called an interpreter that transforms the source code into binary code for execution, and in general, explains, executes, interprets, executes ... Therefore, the interpretation procedure is inseparable from the interpretation procedure. Like the early basic is a classic interpretation of the language, in order to execute the basic program, you have to enter the basic environment before you can load the program source files, run. In an explanatory program, because the program is always in the form of source code, the porting is almost no problem as long as there is a corresponding interpreter. Compiled program Although the source code can also be ported, but the premise is that different systems must be compiled separately, for complex projects, it is really a very small time consumption, and it is likely that some of the details of the place or to modify the source code. Moreover, the interpretation of the program saves the steps of compiling, debugging is also very convenient, editing can be run immediately, do not have to be like a compiled program every small change must be patient waiting for a long compiling ... Linking ... This process of compiling links. However, there are pros and cons, because the interpretation procedure is to put the process of compilation into the implementation process, which determines that the interpretation of the program is destined to be slower than the compilation of a large section, like hundreds of times times the speed gap is not surprising. 

Compiled and interpreted type, both have pros and cons. The former because the program execution speed, the same conditions on the system requirements are low, so like the development of operating systems, large-scale applications, database systems, such as the use of it, such as C + +, Pascal/object Pascal (Delphi), VB and other basic can be regarded as compiled language, and some web script , server scripts, and auxiliary development interfaces for applications where speed requirements are low and the compatibility of different system platforms are required, it is common to use explanatory languages such as Java, JavaScript, VBScript, Perl, Python, and so on.

However, since the compilation and interpretation of the pros and cons of each other, so a number of emerging languages have a tradeoff between the two trends, such as the Java language is relatively close to the interpretation of the characteristics of the language, but before execution has been pre-compiled, generated code is between the machine code and Java source code between the intermediary code , the runtime is interpreted by the JVM (the Java Virtual Machine platform, which can be interpreted as an interpreter). It retains the high abstraction and portability of the source code and has completed much of the precompiled work on the source code, so it is much faster to execute than the "pure interpreter" program. and languages like VB6 (or previous versions), C #, Although the surface is generated by the. exe executable file, but the actual build after VB6 compiled is also a kind of intermediary code, but the compiler in front of a section of the automatic call to an external interpreter code (the interpreter is independent of the user-written program, stored in a DLL file of the system, all the executable program generated by VB6 compilation will be Use it) to explain the actual program body being executed. C # (and other. NET language compilers) generate. NET target code, which is executed by the. NET interpretation system (just like the JVM, which is also a virtual machine platform). Of course, the. NET target code is quite "low-level", closer to the machine language, so it is still considered a compilation language, and its portability is not as strong as Java claims, Java claims to be "compile, execute everywhere", while. NET is "once encoded, compiled everywhere." Oh, of course, these are off-topic. In short, with the development of design technology and hardware, the boundary between the two ways of compiling and interpreting is constantly becoming blurred.

Compiled type
Pros: Compilers typically have pre-compiled procedures to optimize code. Because the compilation is done only once, the runtime does not need to compile, so the program execution of the compiled language is highly efficient. Can run independently from the language environment.
Cons: After compilation, the entire module needs to be recompiled if modifications are required. When compiling the machine code according to the corresponding running environment, porting between different operating systems will be problematic, and you need to compile different executables according to the operating system environment you are running.

Explanatory type
Pros: Good platform compatibility, can be run in any environment, provided the interpreter (virtual machine) is installed. Flexible, modify the code when the direct modification can be quickly deployed, without downtime maintenance.

Cons: Every time you run, you have to explain it again, performance is not as good as the compiled language.

Dynamic language and static language
Usually we call dynamic language, static language refers to dynamic type language and static type language.

(1) Dynamic type language: Dynamic type language refers to the language of data type checking during run time, that is, when programming in a dynamic type language, you never have to specify a data type for any variable, and the language will record the data type internally when you assign it to a variable for the first time. Python and Ruby are a typical dynamic type language, and many other scripting languages, such as VBScript, are also dynamic type languages.

(2) Static type language: Static type language is just the opposite of dynamic type language, its data type is checked during compilation, that is, when writing a program to declare all variables of the data type, C + + is a static type language typical representative, other static type language also has C #, Java and so on.

Strongly typed definition language and weak type definition language

(1) Strongly typed definition language: A language that enforces the definition of a data type. In other words, once a variable is assigned a data type, it is always the data type if it is not cast. For example: If you define an integer variable A, the program simply cannot treat a as a string type. A strongly typed definition language is a type-safe language.

(2) Weakly typed definition language: a language in which data types can be ignored. In contrast to strongly typed definition languages, a variable can assign values of different data types.

Strongly typed definition language may be slightly slower than weak type definition language, but the rigor of strong type definition language can effectively avoid many errors. In addition, "The language is not a dynamic language" and "the language is the type of security" is completely no connection between!
For example: Python is a dynamic language and is a strongly typed definition language (type-safe language); VBScript is a dynamic language and is a weak type definition language (type unsafe language); Java is a static language and is a strongly typed definition language (type-safe language).

Python the pros and cons

See the pros first

    1. Python's positioning is "elegant", "clear", "simple", so the Python program looks always easy to understand, beginners learn python, not only easy to get started, but also in the future, you can write those very very complex programs.
    2. Development efficiency is very high, Python has a very powerful third-party library, basically you want to achieve any function through the computer, the Python official library has the corresponding modules to support, directly download the call, on the basis of the base library to develop, greatly reduce the development cycle, to avoid repeating the wheel.
    3. High-level language ———— when you write programs in the Python language, you don't have to consider the underlying details such as how to manage the memory used by your program
    4. Portability ———— because of its open source nature, Python has been ported on many platforms (modified to make it work on different platforms). If you are careful to avoid using system-dependent features, all your Python programs can run on almost any system platform on the market without modification
    5. Scalability ———— If you need a piece of your critical code to run faster or you want some algorithms to be private, you can write some of your programs in C or C + + and then use them in your Python program.
    6. Embeddable ———— You can embed python into your C + + program to provide scripting functionality to your program users.

Look at the disadvantages again:

    1. Slow, Python runs faster than the C language, and slower than Java, so this is the main reason why many so-called Daniel disdain to use Python, but in fact, this refers to the speed of slow in most cases the user is not directly aware of, Must rely on the use of testing tools to reflect, such as you use C a program to spend 0.01s, Python is 0.1s, so c directly than Python 10 times times faster, is very exaggerated, but you can not directly perceive through the naked eye, Because a normal person can perceive the smallest unit of time is 0.15-0.4s around, haha. In fact, in most cases python has been fully able to meet your requirements for the speed of the program, unless you want to write to the speed of the most demanding search engine, in this case, of course, it is recommended that you use C to achieve.
    2. Code can not be encrypted, because Python is an explanatory language, its source code is stored in the form of a name, but I do not think this is a disadvantage, if your project requires that the source codes must be encrypted, then you should not use Python in the beginning to implement.
    3. Threads do not take advantage of multi-CPU problems, which is one of the most common drawbacks of Python, the Gil, the Global Interpreter lock (interpreter lock), is a tool that the computer programming language interpreter uses to synchronize threads so that only one thread executes at any moment, The python thread is the native thread of the operating system. On Linux for Pthread, on Windows for win thread, the execution of threads is fully dispatched by the operating system. A Python interpreter process has a main thread and the execution thread for multiple user programs. Multi-threaded parallel execution is prohibited even on multicore CPU platforms due to the existence of the Gil.
There are many advantages and disadvantages, and so on ...

Rules for variable definitions:

      • Variable names can only be any combination of letters, numbers, or underscores
      • The first character of a variable name cannot be a number
      • The following keywords cannot be declared as variable names
        [' and ', ' as ', ' assert ', ' Break ', ' class ', ' Continue ', ' Def ', ' del ', ' elif ', ' Else ', ' except ', ' exec ', ' finally ', ' for ', ' F ' Rom ', ' Global ', ' if ', ' import ', ' in ', ' was ', ' lambda ', ' not ', ' or ', ' pass ', ' print ', ' raise ', ' return ', ' try ', ' while ', ' WI Th ', ' yield ']
character encoding

The Python interpreter encodes the content when it loads the code in the. py file (default Ascill)

ASCII (American Standard Code for Information interchange, United States Standards Information Interchange Code) is a set of computer coding systems based on the Latin alphabet, mainly used to display modern English and other Western European languages, which can be used up to 8 Bit to represent (one byte), that is: 2**8 = 256-1, so the ASCII code can only represent a maximum of 255 symbols.

About Chinese

To deal with Chinese characters, programmers designed GB2312 for Simplified Chinese and big5 for traditional Chinese.

GB2312 (1980) contains a total of 7,445 characters, including 6,763 Kanji and 682 other symbols. The inner code range of the Chinese character area is high byte from B0-f7, low byte from A1-fe, occupy code bit is 72*94=6768. 5 of these seats are d7fa-d7fe.

GB2312 supports too few Chinese characters. The 1995 Chinese character extension specification GBK1.0 contains 21,886 symbols, which are divided into Chinese characters and graphic symbol areas. The Chinese character area consists of 21,003 characters. The 2000 GB18030 is the official national standard for replacing GBK1.0. The standard contains 27,484 Chinese characters, as well as Tibetan, Mongolian, Uyghur and other major minority characters. Now the PC platform must support GB18030, the embedded products are not required. So mobile phones, MP3 generally only support GB2312.

From ASCII, GB2312, GBK to GB18030, these coding methods are backwards compatible, meaning that the same character always has the same encoding in these scenarios, and the latter standard supports more characters. In these codes, English and Chinese can be handled in a unified manner. The method of distinguishing Chinese encoding is that the highest bit of high byte is not 0. According to the programmer, GB2312, GBK, and GB18030 belong to the double-byte character set (DBCS).

Some Chinese Windows default internal code or GBK, you can upgrade to GB18030 through the GB18030 upgrade package. But GB18030 relative GBK increases the character, the ordinary person is difficult to use, usually we still use the GBK to refer to the Chinese Windows inside code.

It is clear that the ASCII code cannot represent all the words and symbols in the world, so it is necessary to create a new encoding that can represent all the characters and symbols, namely: Unicode

Unicode (Uniform Code, universal Code, single code) is a character encoding used on a computer. Unicode is created to address the limitations of the traditional character encoding scheme, which sets a uniform and unique binary encoding for each character in each language, which specifies that characters and symbols are represented by at least 16 bits (2 bytes), that is: 2 **16 = 65536,
Note: Here is a minimum of 2 bytes, possibly more

UTF-8, which is compression and optimization of Unicode encoding, does not use a minimum of 2 bytes, but instead classifies all characters and symbols: the contents of the ASCII code are saved with 1 bytes, the characters in Europe are saved in 2 bytes, and the characters in East Asia are saved in 3 bytes ...

Therefore, when the Python interpreter loads the code in the. py file, the content is encoded (default Ascill).

Notes

When the line stares: # is annotated content

Multiline Comment: "" "Annotated Content" ""

Details: http://www.runoob.com/python3/python3-tutorial.html data type 1, digital

2 is an example of an integer.
Long integers are just larger integers.
3.23 and 52.3E-4 are examples of floating-point numbers. The e tag represents a power of 10. Here, 52.3E-4 means 52.3 * 10-4.
( -5+4j) and (2.3-4.6j) are examples of complex numbers, where -5,4 is a real number, j is an imaginary number, and what is the plural in mathematics?

int (integral type)

On a 32-bit machine, the number of integers is 32 bits and the value range is -2**31~2**31-1, which is -2147483648~2147483647
On a 64-bit system, the number of integers is 64 bits and the value range is -2**63~2**63-1, which is -9223372036854775808~9223372036854775807long (Long integer)
Unlike the C language, Python's long integers do not refer to the positioning width, that is, Python does not limit the size of long integer values, but in fact, because of limited machine memory, we use a long integer value can not be infinite.
Note that, since Python2.2, Python automatically converts integer data to long integers if an integer overflows, so it does not cause any serious consequences if you do not add the letter L after long integer data.
Float (float type)        First Literacy http://www.cnblogs.com/alex3714/articles/5895848.html
A floating-point number is used to process real numbers, which are numbers with decimals. Similar to the double type in C, accounting for 8 bytes (64 bits), where 52 bits represent the bottom, 11 bits represent the exponent, and the remaining one represents the symbol.
Complex (plural)
The complex number consists of real and imaginary parts, the general form is X+yj, where x is the real part of the complex, and Y is the imaginary part of the complex, where x and y are real numbers. Note: Small number pools exist in Python:-5 ~ 257 2, Boolean true or False 1 or 03, string string commonly used functions:
    • Remove whitespace
    • Segmentation
    • Length
    • Index
    • Slice
4. List

Basic operation:

    • Index
    • Slice
    • Additional
    • Delete
    • Length
    • Slice
    • Cycle
    • Contains
5, tuples (immutable list) 6, dictionary (unordered)

Common operations:

    • Index
    • New
    • Delete
    • Key, value, key-value pairs
    • Cycle
    • Length

7. Collection

A collection is an unordered, non-repeating combination of data, and its main functions are as follows:

    • Go to the weight, turn a list into a set, and then automatically go heavy.
    • Relationship test, test the intersection of two sets of data, difference set, and the relationship between the set
Data OperationsArithmetic operations:

Comparison operation:

Assignment operation:

Logical operation:

Member Operations:

Identity operation:

Bit operations:

* Bitwise negation Operation rule (bitwise negation plus 1) detailed http://blog.csdn.net/wenxinwukui234/article/details/42119265

Operator Precedence:

If ... elif...elsefor loopwhile loop

Ternary operations

if Else value 2

If the condition is true: result = value 1
If the condition is false: result = value 2

In-process

    • Binary, 01
    • Octal, 01234567
    • Decimal, 0123456789
    • Hex, 0123456789ABCDEF binary to 16 binary conversion http://jingyan.baidu.com/album/47a29f24292608c0142399cb.html?picindex=1
Computer memory address and why use 16 binary? Why use 16 binary 1, computer hardware is 1012 binary, 16 binary is just a multiple of 2, it is easier to express a command or data. Hex is shorter, because when the conversion of a 16 binary number can be the top 4 bits 2 binary number, that is, a byte (8 bits can be used two 16 in the notation) 2, the earliest provisions of the ASCII character set used is 8bit (late expansion, but the base unit is 8bit), 8bit with 2 16 binary directly can express, regardless of reading or storage is more convenient than other systems
3, the computer CPU operation is also in accordance with the ASCII character set, 16, 32, 64 in such a way in the development, so the data exchange when the 16 system also appears better
4, in order to unify the specification, CPU, memory, hard disk we see are used in the 16 binary calculation


16 Where to use the binary
1, network programming, data exchange needs to parse bytes is a byte of a byte processing, 1 byte can be used 0xFF two 16 binary to express. With the network capture, you can see that the data is transmitted via 16-binary.
2, data storage, storage in the hardware is 0101 of the way, stored in the system is expressed in the form of a byte

3, some common values of the definition, such as: We often use the HTML in the color expression, is the use of the 16-way, 4 16-bit can express a good millions of color information.

For Python, everything is an object, objects are created based on the class

First, Python introduction

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.