Little White Python Road Day1

Source: Internet
Author: User

Python Road, Day1-python Foundation 1

The content of this section
    1. Python Introduction
    2. History
    3. Python 2 or 3?

First, Python introduction

The founder of Python is Guido van Rossum (Guido van Rossum). During the Christmas of 1989, Guido van Rossum to spend time in Amsterdam, determined to develop a new script interpreter, as an inheritance of the ABC language.

The latest Tiobe rankings (2017, November), Python overtaking C # occupy the four, Python advocating beautiful, clear, simple, is a good and widely used language.

By the visible, python overall upward trend, reflects the Python application is more and more widespread and also gradually obtains the industry recognition!!!

Python can be used in many fields, such as data analysis, component integration, network services, image processing, numerical computing, and scientific computing.

Current Python main application areas:

Cloud Computing, Web Development, Scientific Computing, artificial intelligence, system operation and maintenance, Financial, graphical GUI

Reason: As a dynamic language Python, the language structure is clear and simple, the library is rich, mature and stable, scientific calculation and statistical analysis are very good, production efficiency is much higher than c,c++,java, especially good at strategy backtesting

Programming languages are mainly categorized from the following angles: compiled and interpreted, static and dynamic, strongly typed, and weakly typed.

What is the difference between compilation and interpretation? compileris to compile each statement of the source program into a machine language and save 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.

This is because the computer does not directly recognize and execute the statements we write, it only knows the machine language (in binary form)

Compiled vs explanatory type

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.

first, 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, a high-level language statement often requires a number of 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 type, in fact, it is the same as the assembly language: There is a translation program to convert our source code, 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, although relatively close to the interpretation of the characteristics of the language, but before execution has been pre-compiled, The generated code is the intermediary code between the machine code and the Java source code, which is interpreted and executed 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.

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).

With these introductions, we can conclude thatPython is a strongly typed definition language that is dynamically interpreted . So what are the advantages and disadvantages of python that these genes have achieved? Let's keep looking down.

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. A compromise solution to this problem is discussed in more detail later in the Threads and Processes section.

Of course, Python also has some other small shortcomings, in this does not enumerate, I want to say that any language is not perfect, have good and not good at doing things, it is recommended that you do not take a language disadvantage to the advantage of another language to compare, language is just a tool, is a tool to realize the idea of the program designer, as we learned in high school geometry, sometimes need compasses, sometimes need to use the same setsquare, take the corresponding tools to do what it is best at the right choice. A lot of people asked me, which is the best shell and python? I replied that the shell is a scripting language, but Python is not just a scripting language, it can do more, and then someone who has a dead-point says there is absolutely no need to learn python, Python can do things that the shell can do, as long as you have enough cow B, And then the shell can write Tetris such a game, I can say to express only, not with SB theory, SB will pull you to the same height as he, and then with the full experience to knock you down.

Python Interpreter

When we write the Python code, we get a text file that contains the Python code for the .py extension. To run the code, you need the Python interpreter to execute the .py file.

Since the entire Python language is open source from spec to interpreter, it is theoretically possible for anyone to write a Python interpreter to execute Python code (which is difficult, of course) as long as the level is high enough. In fact, there are a number of Python interpreters.

CPython

When we downloaded and installed Python 2.7 from the official Python website, we immediately got an official version of the interpreter: CPython. This interpreter was developed in C language, so called CPython. Running at the command line python is to start the CPython interpreter.

CPython is the most widely used Python interpreter. All the code for the tutorial is also executed under CPython.

IPython

Ipython is an interactive interpreter based on CPython, meaning that Ipython is only enhanced interactively, but the functionality and CPython of executing Python code are exactly the same. Like many domestic browsers although the appearance of different, but the kernel is actually called ie.

CPython >>> is used as a prompt, while Ipython is used In [ 序号 ]: as a prompt.

PyPy

PyPy is another Python interpreter whose goal is to perform speed. PyPy uses JIT technology to dynamically compile Python code (note that it is not interpreted), so it can significantly improve the execution speed of Python code.

Most python code can run under PyPy, but PyPy and CPython are somewhat different, which results in the same Python code being executed under both interpreters. If your code is to be executed under PyPy, you need to understand the differences between PyPy and CPython.

Jython

Jython is a Python interpreter running on the Java platform that compiles python code directly to Java bytecode execution.

IronPython

IronPython is similar to Jython, except that IronPython is a Python interpreter running on the Microsoft. NET platform that compiles python code directly into. NET bytecode.

Summary

Python has a lot of interpreters, but the most widely used is cpython. If you want to interact with the Java or. NET platform, the best approach is not to use Jython or IronPython, but to interact via network tuning to ensure the independence of each program.

Ii. History of Python
  • In 1989, in order to pass the Christmas holiday, Guido began to write the Python language compiler. The name Python, from Guido's beloved TV show Monty Python's Flying Circus. He hoped that the new language, called Python, would fit his ideals: create a language that is all-powerful, easy to learn, easy to use, and extensible, between C and Shell.
  • 1991, the first Python compiler was born. It is implemented in C language and can call the C language library file. From birth, Python already has: classes, functions, exception handling, core data types including tables and dictionaries, and module-based expansion systems.
  • Granddaddy of Python Web frameworks, Zope 1 is released in 1999
  • Python 1.0-january 1994 adds lambda, map, filter and reduce.
  • Python 2.0-october 16, 2000, added a memory recovery mechanism that forms the basis of the Python language framework Now
  • Python 2.4-november 30, 2004, the same year now the most popular web framework Django was born
  • Python 2.5-september 19, 2006
  • Python 2.6-october 1, 2008
  • Python 2.7-july 3, 2010
  • In November, it is announced that Python 2.7 would is supported until 2020, and reaffirmed that there would is no 2. 8 release as users were expected to move to Python 3.4+ as soon as possible
  • Python 3.0-december 3, 2008
  • Python 3.1-june 27, 2009
  • Python 3.2-february 20, 2011
  • Python 3.3-september 29, 2012
  • Python 3.4-march 16, 2014
  • Python 3.5-september 13, 2015
Third, Python 2 or 3?Python 2.x is a legacy, python3.x is the language of the present and future, and most importantly python2.x support up to 2020! The detailed difference between PY2 and 3

PRINT is A FUNCTION

The statement have been replaced with a print () function, with keyword arguments to replace most of the special syntax of T He old statement (PEP 3105). Examples:

1234567 Old: print"The answer is"2*2New: print("The answer is"2*2)Old: printx, # Trailing comma suppresses newline New: print(x, end=" ") # Appends a space instead of a newlineOld: print# Prints a newlineNew: print() # You must call the function!Old: print>>sys.stderr, "fatal error" New: print("fatal error"file=sys.stderr)Old: print(x, y) # prints repr((x, y))New: print((x, y)) # Not the same as print(x, y)!

You can also customize the separator between items, e.g.:

1 print("There are <"2**32"> possibilities!", sep="")

All are UNICODE now

No longer bothered by annoying character encodings

Can also play this way: (A,*REST,B) =range (5)

1234 <strong>>>> A, * rest,b  =  range ( 5 >>> a,rest,b ( 0 1 2 3 ],  4 ) < / Strong>

  

Some libraries have been renamed

Old Name

New Name

_winreg

WinReg

Configparser

Configparser

Copy_reg

Copyreg

Queue

Queue

Socketserver

Socketserver

Markupbase

_markupbase

Repr

Reprlib

Test.test_support

Test.support

  

Who else doesn't support PYTHON3?

A popular module that does not support Python 3 is twisted (for networking and other applications).

Reference Reference Address: http://www.cnblogs.com/alex3714/articles/5465198.html

Little White Python Road Day1

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.