Python Learning [Part 1] Introduction to Python

高洛峰
Release: 2017-02-13 17:18:30
Original
1345 people have browsed it
  1. Introduction to Python

  2. Development History

  3. Python 2 or 3?

1. Introduction to Python

The founder of python is Guido van Rossum. During the Christmas period of 1989, in order to kill time in Amsterdam, Guido van Rossum decided to develop a new script interpreter as a successor to the ABC language.


#In the latest TIOBE rankings, Python has overtaken PHP to occupy the fifth place. Python advocates elegance, clarity, and simplicity, and is an excellent and widely used language.

Python can be used in many fields, such as: data analysis, component integration, network services, image processing, numerical computing and scientific computing and many other fields. At present, almost all large and medium-sized Internet companies in the industry are using Python, such as: Youtube, Dropbox, BT, Quora (China Zhihu), Douban, Zhihu, Google, Yahoo!, Facebook, NASA, Baidu, Tencent, Autohome, Meituan et al. Things that Internet companies widely use Python to do generally include: automated operation and maintenance, automated testing, big data analysis, crawlers, Web, etc.

Currently Python’s main application areas:

  • Cloud computing: the most popular language for cloud computing, typical application OpenStack

  • ##WEB Development: Many excellent WEB frameworks, many large websites are developed in Python, including Youtube, Dropbox, and Douban. . . , Typical WEB frameworks include Django

  • Scientific operations, artificial intelligence: Typical libraries NumPy, SciPy, Matplotlib, Enthought libraries, pandas

  • System operation Wei: One of the essential languages ​​for operation and maintenance personnel

  • Finance: quantitative trading, financial analysis, in the field of financial engineering, Python is not only used, but also used the most, and its importance is increasing year by year. . Reason: As a dynamic language, Python has a clear and simple language structure, rich libraries, mature and stable, efficient scientific calculation and statistical analysis, and its production efficiency is much higher than that of c, c++, and java. It is especially good at strategy backtesting

  • Graphic GUI: PyQT, WxPython,TkInter

Comparison of Python and other common languages

C and Python, Java , C#, etc.

C language: The code is compiled to obtain machine code, which is directly executed on the processor. Each instruction controls the CPU work

Other languages: The code is compiled to obtain bytecode, virtual The machine executes the bytecode and converts it into machine code and then executes it on the processor

Python and C Python is a language developed from C

For use: Python has a complete class library And it is 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.

Regarding speed: high-level languages ​​cannot compare with C in terms of running speed

Python and Java, C#, etc.

For use: Linux original Python, other languages ​​do not have it; the above languages ​​have very rich class library support

For speed: Python may be slightly inferior in speed

So, there is no essential difference between Python and other languages. The other differences are: being good at a certain field, having rich talents, and being preconceived.

Python is a dynamically interpreted, strongly typed definition language

Advantages:

  1. Python's positioning is "elegant", "clear", and "simple", so Python programs always look simple and easy to understand. Beginners learning Python are not only easy to get started, but also can write very, very complex programs if they go deeper in the future.

  2. 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 corresponding modules to support it. Download it directly. After the call, development is carried out on the basis of the basic library, which greatly reduces the development cycle and avoids reinventing the wheel.

  3. High-level language————When you write a program in Python, you don’t need to think about low-level details such as how to manage the memory used by your program

  4. Portability - Due to its open source nature, Python has been ported to many platforms (with modifications to enable it to work on different platforms). If you carefully avoid using system-dependent features, then all of your Python programs can run without modification on almost any system platform on the market

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

  6. Embeddability - You can embed Python into your C/C++ program to provide scripting functionality to your program users.

shortcoming:

  1. Slow speed. Python runs much slower than C language and slower than JAVA. Because Python is an interpreted language, your code will be translated line by line during execution. into machine code that the CPU can understand. This translation process is very time-consuming, so it is very slow. The C program is directly compiled into machine code that the CPU can execute before running, so it is very fast. But in fact, the slow running speed referred to here cannot be directly perceived by users in most cases, and must be reflected with the help of testing tools.

  2. The code cannot be encrypted because PYTHON is an interpreted language and its source code is stored in text form. However, I don’t think this is a disadvantage. If your project requires source code The code must be encrypted, then you shouldn't use Python to implement it in the first place.

  3. 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 used by computer programming language interpreters. A tool for synchronizing 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.

Types of Python

When we write Python code, what we get is a Python code containing A text file with a .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 specification to interpreter, in theory, as long as the level is high enough, anyone can write a Python interpreter to execute Python code (of course it is very difficult). In fact, multiple Python interpreters do exist.

CPython

After we downloaded and installed Python 2.7 from the official Python website, we directly got an official version of the interpreter: CPython. This interpreter is developed in C language, so it is called CPython. Running python from the command line starts the CPython interpreter.

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

IPython

IPython is an interactive interpreter based on CPython. That is to say, IPython is only enhanced in the interactive mode, but the function of executing Python code is completely the same as CPython. the same. For example, although many domestic browsers have different appearances, their cores actually call IE.

CPython uses >>> as the prompt, while IPython uses In [serial number]: as the prompt.

PyPy

PyPy is another Python interpreter that targets execution speed. PyPy uses JIT technology to dynamically compile Python code, so it can significantly improve the execution speed of Python code.

Most Python codes can be run under PyPy, but there are some differences between PyPy and CPython, which results in the same Python code executing under the two interpreters may have different results. 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. It can directly compile Python code into Java bytecode for execution.

IronPython

IronPython is similar to Jython, except that IronPython is a Python interpreter running on the Microsoft .Net platform and can directly compile Python code into .Net bytecode.

Summary

There are many Python interpreters, but the most widely used one is CPython. If you want to interact with Java or .Net platform, the best way is not to use Jython or IronPython, but to interact through network calls to ensure the independence between programs.

2. History of Python development

  • ## In 1989, in order to pass the Christmas holiday, Guido began to write Python language translater. The name Python comes from Guido’s beloved TV series Monty Python’s Flying Circus. He hopes that this new language called Python can meet his ideal: to create a language between C and shell that is comprehensive, easy to learn, easy to use, and scalable.

  • In 1991, the first Python compiler was born. It is implemented in C language and can call C language library files. From its birth, Python has had: classes, functions, exception handling, core data types including tables and dictionaries, and a module-based expansion system.

  • Granddaddy of Python web frameworks, Zope 1 was released in 1999

  • ##Python 1.0 - January 1994 added lambda, map, filter and reduce .
  • ##Python 2.0 - October 16, 2000, added a memory recycling mechanism, forming the basis of the current Python language framework
  • Python 2.4 - November 30, 2004, the same year that 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 2014, it was announced that Python 2.7 would be supported until 2020, and reaffirmed that there would be 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

 

三、Python 2 or 3?

In summary : Python 2.x is legacy, Python 3.x is the present and future of the language

Python 3.0 was released in 2008. The final 2.x version 2.7 release came out in mid-2010, with a statement of

extended support for this end-of-life release. The 2.x branch will see no new major releases after that. 3.x is

under active development and has already seen over five years of stable releases, including version 3.3 in 2012,

3.4 in 2014, and 3.5 in 2015. This means that all recent standard library improvements, for example, are only

available by default in Python 3.x.


Guido van Rossum (the original creator of the Python language) decided to clean up Python 2.x properly, with less regard for backwards compatibility than is the case for new releases in the 2.x range. The most drastic improvement is the better Unicode support (with all text strings being Unicode by default) as well as saner bytes/Unicode separation.

Besides, several aspects of the core language (such as print and exec being statements, integers using floor pision) have been adjusted to be easier for newcomers to learn and to be more consistent with the rest of the language, and old cruft has been removed (for example, all classes are now new-style, "range()" returns a memory efficient iterable, not a list as in 2.x). 

 

py2与3的详细区别

PRINT IS A FUNCTION

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

Old: print "The answer is", 2*2 New: print("The answer is", 2*2)
Old: print x, # Trailing comma suppresses newline New: print(x, end=" ") # Appends a space instead of a newline
Old: print # Prints a newline
New: 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)!
Copy after login


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

print("There are <", 2**32, "> possibilities!", sep=""
Copy after login


ALL IS UNICODE NOW

从此不再为讨厌的字符编码而烦恼

还可以这样: (A,*REST,B)=RANGE(5)

<strong>>>> a,*rest,b = range(5)
>>> a,rest,b
(0, [1, 2, 3], 4)
</strong>
Copy after login


某些库改名了



Old Name



New Name



_winreg



winreg



ConfigParser



configparser



copy_reg



copyreg



Queue



queue



SocketServer



socketserver



markupbase



_markupbase



repr



reprlib



test.test_support



test.support


 

  

 

 

 

 

 

 

 

 

 

 

 




 

 

还有谁不支持PYTHON3?


One popular module that don't yet support Python 3 is Twisted (for networking and other applications). Most

actively maintained libraries have people working on 3.x support. For some libraries, it's more of a priority than

others: Twisted, for example, is mostly focused on production servers, where supporting older versions of

Python is important, let alone supporting a new version that includes major changes to the language. (Twisted is

a prime example of a major package where porting to 3.x is far from trivial 

更多Python学习【第一篇】Python简介 相关文章请关注PHP中文网!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!