2017-07-15, this is my first day learning python.
First of all, python is a very popular development language at the moment, and its founder is Guido Van Rossum. As far as the current situation is concerned, the popularity of the python language continues to rise, and it has surpassed C# to rank fourth. Python advocates elegance, simplicity, and clarity, and is an excellent and widely used language.
1. Python is an interpreted language that changes interpretation while running.
First, explain the compiler. It compiles every statement of the source program into machine language and saves it into a binary file. In this way, when it is run, the machine directly compiles the Binary file to run this file, the execution speed is very fast. The interpreter is different. When the program is executed, the interpreter will interpret it into machine language one by one for the computer to execute. So obviously, the execution speed is not as fast as the compiled file. This is also because the computer cannot directly recognize and execute the code we write, it can only recognize computer language (ie, binary files).
Compiled VS Interpreted
Compiled type
Advantages: The compiler has a pre-compilation process to optimize the code. Because compilation is only done once and does not need to be compiled again during runtime, the execution efficiency of compiled languages is very high. It can be run directly without the language environment.
Disadvantages: After the compiler compiles the program, if the code is modified, it needs to be recompiled. When compiling, machine code is generated according to the corresponding operating environment. There will be problems when transplanting between different operating systems. It needs to be compiled into different executable files according to the environment of the operating system that needs to be run.
Explanatory type:
Advantages: Good platform compatibility, can run in any environment, The prerequisite is that the interpreter (virtual machine) is installed. Because it is dynamically interpreted, the program itself is very flexible. When modifying the code, there is no need to worry about modifying it and running it directly. It can be deployed quickly, and program updates do not require shutdown maintenance.
Disadvantages: Every time the code is executed, it needs to be accompanied by the process of dynamic interpretation. Performance is not as good as compiled languages.
Why Python and not other languages?
C and Python, Java, C#, etc.
C language: The code is compiled to obtain machine code. The machine code is directly executed on the processor. Each instruction controls the CPU work.
Other languages: The code is compiled to obtain bytecode, the virtual machine executes the bytecode and converts it into machine code and then executes it on the processor
Python and C The language Python is developed from C Developed
For use: Python’s class library is complete and simple to use. If you want to achieve the same function, Python can solve it with 10 lines of code, while C may require 100 lines or more.
For speed : Compared with C, the running speed of Python is definitely slower.
Python and Java, C#, etc.
For use: Linux original Python, other languages do not have it; the above languages have it Very rich class library support
Regarding speed: Python may be slightly inferior in speed
So, there is no essential difference between Python and other languages. The other differences are: good at a certain field, rich talents, and preconceived ideas.
##Types of Python
Cpython The official version of Python, using C Language implementation, the most widely used, CPython implementation will convert the source file (py file) into a bytecode file (pyc file), and then run on the Python virtual machine.
Java implementation of Python, Jython will dynamically compile Python code into Java bytecode and then run on the JVM.
A C# implementation of Python, IronPython compiles Python code into C# bytecode and then runs it on the CLR. (Similar to Jython)
Python implemented by Python recompiles Python's bytecode bytecode into machine code.
1. Low-level language and high-level language
The original computer program was represented by a sequence of 0 and 1. Programmers directly used machine instructions without translation. Paper tape punching input can be executed to get the result. Later, in order to facilitate memory, machine instructions represented by sequences of 0 and 1 were all mnemonic symbols. These mnemonics that corresponded one-to-one to the machine instructions became assembly instructions, and thus the assembly language was born. Both machine instructions and assembly instructions are machine-oriented and are collectively referred to as low-level languages. Because it is a mnemonic for machine instructions for a specific machine, assembly language cannot be independent of the machine (specific CPU architecture). But assembly language also needs to be translated into machine instructions before it can be executed. Therefore, there is also a method of translating assembly language running on one machine into machine instructions running on another machine, and that is cross-assembly technology.
High-level language is a computer language that starts from the perspective of human logical thinking. The level of abstraction is greatly improved. It needs to be compiled into target code on a specific machine before it can be executed. A high-level language statement often requires several machine instructions to complete. The machine-independent nature of high-level languages is achieved by compilers generating different object codes (or machine instructions) for different machines. Specifically speaking, to what extent should a high-level language be compiled? This is related to the compilation technology. It can be compiled into directly executable target code, or it can be compiled into an intermediate representation, and then obtained by different machines and System to execute, this situation usually requires a supporting environment, such as the support of an interpreter or a virtual machine. Java programs compiled into bytecode and then executed by virtual machines on different platforms are good examples. Therefore, saying that a high-level language does not depend on the machine means that the program itself of the high-level language remains unchanged on different machines or platforms, and the target code compiled by the compiler adapts to different machines. In this sense, through cross-assembly, some assemblers can also achieve portability between different machines, but the portability obtained through this approach is far less convenient and practical than high-level languages.
2. Compilation and Interpretation
Compilation is to translate the source program into executable target code. Translation and execution are separate; while interpretation is to The translation and execution of the source program is completed once and no storable object code is generated. This is just an appearance. The biggest difference between the two is: for interpretation and execution, the control power when the program is running lies with the interpreter and not the user program; for compilation and execution, the control power during runtime lies with the user program.
Interpretation has good dynamic characteristics and portability. For example, when interpreting and executing, you can dynamically change the type of variables, modify the program, and insert good debugging diagnostic information into the program. If the interpreter is transplanted to a different system, the program can run on the system to which the interpreter is transplanted without modification. At the same time, the interpreter also has great shortcomings, such as low execution efficiency and large space occupation, because not only space must be allocated to the user program, but the interpreter itself also occupies valuable system resources.
The compiler compiles each statement of the source program into machine language and saves it as a binary file. In this way, the computer can directly run the program in machine language at runtime, which is very fast. Fast; and the interpreter only interprets each line into machine language for the computer to execute when executing the program, so the running speed is not as fast as the compiled program.
Compiled type and interpreted type Let’s look at the compiled type first. In fact, it is the same as assembly language: there is also a translation program to convert our source code and generate the corresponding executable code. . To put it more professionally, this process is called compilation, and the program responsible for compilation is naturally called a compiler. If the program code we write is contained in a source file, then usually an executable file will be generated directly after compilation, and we can run it directly. But for a more complex project, in order to facilitate management, we usually disperse the code in various source files and organize it as different modules. At this time, when compiling each file, an object file (Object file) will be generated instead of the executable file mentioned earlier. Generally, the compilation of a source file will correspond to a target file. The content in these target files is basically executable code, but since it is only part of the entire project, we cannot run it directly yet. After all the source files are compiled, we can finally "package" these semi-finished target files into an executable file. This work is completed by another program, because this process seems to include the executable code. The target files are connected and assembled, so it is also called a link, and the program responsible for linking is called...it is called a linker. In addition to linking target files, the linker may also have various resources, such as icon files, sound files, etc. It is also responsible for removing redundant duplicate codes between target files, etc., so... it is also quite tiring. . After the link is completed, we can generally get the executable file we want.
Above we have briefly introduced the characteristics of compiled languages, now let’s look at interpreted languages. Oh, literally, "compile" and "interpret" both mean "translation", but the difference lies in the timing of translation. For example: If you plan to read a foreign book and you don’t know the foreign language, then you can find a translator and give him enough time to translate the entire book from beginning to end, and then translate the book The native language version is given to you to read; or, you can immediately ask the translator to assist you in reading, and let him translate for you sentence by sentence. If you want to go back to a certain chapter, he will have to translate it for you again.
Two methods, the former is equivalent to the compiled type we just mentioned: convert all the code into machine language at once, and then write it into an executable file; and the latter is equivalent to what we want to say Interpreted type: At the moment before the program is run, there is only the source program and no executable program; and every time the program executes a certain instruction of the source program, there will be a shell program called an interpreter to convert the source code into binary The code is for execution. In short, it is constantly interpreted, executed, interpreted, executed... Therefore, interpreted programs are inseparable from interpreting programs. For example, early BASIC is a classic interpreted language. To execute a BASIC program, you have to enter the BASIC environment, and then you can load the program source file and run it. In an interpreted program, since the program always appears in the form of source code, transplantation is almost no problem as long as there is a corresponding interpreter. Although the source code of compiled programs can also be transplanted, the prerequisite is that they must be compiled separately for different systems. For complex projects, it is indeed a time-consuming task, and it is very likely that some details still need to be modified. code. Moreover, interpreted programs save the compilation step, and are very convenient for modification and debugging. They can be run immediately after editing. Unlike compiled programs, you do not have to wait patiently for the long Compiling...Linking...compilation process every time you make small changes. linking process. However, everything has pros and cons. Since interpreted programs put the compilation process into the execution process, this determines that interpreted programs are destined to be much slower than compiled programs. It is not surprising that the speed difference is hundreds of times. of.
Compiled type and interpreted type, both have advantages and disadvantages. The former has fast program execution speed and lower system requirements under the same conditions. Therefore, it is used when developing operating systems, large-scale applications, database systems, etc., such as C/C++, Pascal/Object Pascal (Delphi), VB and other basic programs. All can be regarded as compiled languages, while some programs such as web scripts, server scripts and auxiliary development interfaces that do not have high speed requirements and have certain requirements for compatibility between different system platforms usually use interpreted languages, such as Java and JavaScript. , VBScript, Perl, Python, etc.
But since compiled and interpreted languages have their own advantages and disadvantages and are in opposition to each other, a number of emerging languages tend to compromise the two. For example, although the Java language is closer to the characteristics of an interpreted language, it does not have the characteristics of an interpreted language before execution. It has been precompiled in advance, and the generated code is an intermediary code between the machine code and the Java source code. When running, it is interpreted and executed by the JVM (Java's virtual machine platform, which can be regarded as an interpreter). It not only retains the high abstraction and portability characteristics of the source code, but also has completed most of the pre-compilation work of the source code, so it executes much faster than "purely interpreted" programs. For languages like VB6 (or previous versions) and C#, although on the surface they generate .exe executable program files, what is actually generated after VB6 is compiled is also an intermediate code, but the compiler inserts a paragraph in front of it. Automatically call the code of an external interpreter (the interpreter is independent of the program written by the user and stored in a DLL file of the system. All executable programs compiled with VB6 must use it) to interpret the actual execution program body. C# (and other .net language compilers) generates .net target code, which is actually executed by the .net interpretation system (just like JVM, it is also a virtual machine platform). Of course, the .net target code is already quite "low-level" and is closer to machine language, so it is still regarded as a compiled language, and its portability is not as powerful as Java claims. Java claims to be "compile once, execute everywhere". And .net is "encode once, compile everywhere". Haha, of course these are off topic. In short, with the continuous development of design technology and hardware, the boundaries between compiled and interpreted methods are becoming increasingly blurred.Dynamic language and static language Usually what we call dynamic language and static language refers to dynamic type language and static type language.
(1) Dynamically typed language: Dynamically typed language refers to a language that only performs data type checking during runtime. In other words, when programming in a dynamically typed language, it is always There is no need to assign a data type to any variable, the language will record the data type internally the first time you assign a value to a variable. Python and Ruby are typical dynamically typed languages, and various other scripting languages such as VBScript are also more or less dynamically typed languages.
(2) Static type language: Static type language is just the opposite of dynamic type language. Its data type is checked during compilation, which means that all data types must be declared when writing a program. Regarding the data type of variables, C/C++ is a typical representative of statically typed languages. Other statically typed languages include C#, JAVA, etc.
Strongly typed definition language and weakly typed definition language
(1) Strong type Definition language: The language that enforces data type definitions. In other words, once a variable is assigned a certain data type, it will always be that data type if it is not cast. For example: if you define an integer variable a, then it is impossible for the program to 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. It is the opposite of a strongly typed definition language, in which a variable can be assigned values of different data types.
Strongly typed definition languages may be slightly slower than weakly typed definition languages, but the rigor brought by strongly typed definition languages can effectively avoid many errors. In addition, there is absolutely no connection between "whether this language is a dynamic language" and "whether this language is type-safe"! For example: Python is a dynamic language, a strongly typed definition language (type-safe language); VBScript is a dynamic language, a weakly typed definition language (type-unsafe language); JAVA is a static language, a strongly typed definition language (type-safe language) language).
Through the above introduction, we can conclude that python is a dynamically interpreted strong type definition language. So what are the advantages and disadvantages of Python made possible by these genes? Let's continue reading.
Advantages and disadvantages of Python
## Let’s look at the advantages first
Python's positioning is "elegant", "clear", and "simple", so Python programs always look simple and easy to understand. Beginners learning Python will not only get started easily, but also can learn more in the future. Write those very, very complex programs.
The development efficiency is very high. Python has a very powerful third-party library. Basically, if you want to realize any function through the computer, the official Python library has the corresponding function. Modules are supported and directly downloaded and called, and then developed on the basis of the basic library, which greatly reduces the development cycle and avoids reinventing the wheel.
High-level language - When you write a program in Python, you don't need to think about low-level things like how to manage the memory used by your program. Details
Portability - Due to its open source nature, Python has been ported to many platforms (with modifications to enable it to work on different platforms superior). If you carefully avoid using system-dependent features, then all of your Python programs will run without modification on almost every system platform on the market
Scalability - If you need a critical piece of your code to run faster or want certain algorithms to be kept private, you can write parts of your program in C or C++ and use them in your Python program.
Embeddability - You can embed Python into your C/C++ program to provide scripting functionality to your program users.
Let’s look at the shortcomings:
is slow, Python runs faster than C The language is indeed much slower, even slower than JAVA. Therefore, this is also the main reason why many so-called experts disdain to use Python. However, in fact, the slow running speed referred to here cannot be directly perceived by users in most cases. Yes, it must be reflected with the help of testing tools. For example, it takes 0.01s to run a program in C, and 0.1s in Python. In this way, C language is directly 10 times faster than Python, which is very exaggerated, but you cannot directly pass it. It can be perceived by the naked eye, because the smallest unit of time that a normal person can perceive is about 0.15-0.4s, haha. In fact, in most cases, Python can fully meet your program speed requirements, unless you want to write a search engine that has extremely high speed requirements. In this case, of course, it is recommended that you use C to implement it.
The code cannot be encrypted because PYTHON is an interpreted language and its source code is stored in the form of text, but I don’t think this is a shortcoming. If your project requires that the source code must be encrypted, then you should not use Python to implement it in the first place.
Threads cannot take advantage of the problem of multiple CPUs. This is the most criticized shortcoming of Python. GIL is the Global Interpreter Lock (Global Interpreter Lock), which is a computer A tool used by programming language interpreters to synchronize threads so that only one thread is executing at any time. Python threads are native threads of the operating system. It is pthread on Linux and Win thread on Windows. The execution of the thread is completely scheduled by the operating system. A python interpreter process has a main thread and multiple user program execution threads. Even on multi-core CPU platforms, parallel execution of multi-threads is prohibited due to the existence of GIL. Regarding the compromise solution to this problem, we will discuss it in detail in the thread and process chapters later.
Of course, Python has some other small shortcomings, which I won’t list here. What I want to say is that no language is perfect, and there are things it is good at and things it is not good at. , it is recommended that you do not compare the disadvantages of one language with the advantages of another language. Language is just a tool, a tool to realize the ideas of programmers. Just like when we learned geometry in middle school, sometimes we needed compasses. Sometimes you need to use a set ruler. It is the right choice to use the corresponding tool to do what it is best at. Many people have asked me before, which one is better, Shell or Python? I replied that Shell is a scripting language, but Python is not just a scripting language, it can do more. Then there are some people who are very clever and say that there is no need to learn Python. Shell can do everything Python can do, as long as you are good enough. B, then mentioned that you can use Shell to write games like Tetris. The only way I can express this is, don’t argue with SB. SB will bring you to the same level as him, and then use sufficient experience to teach you. Knock down.
The above is the detailed content of Introduction to Python and Getting Started Guide. For more information, please follow other related articles on the PHP Chinese website!