Python Basics (i) first knowledge of Python

Source: Internet
Author: User
Tags garbage collection

About Python

First, Python introduction

Python (United Kingdom pronunciation:/?pa?θ?n/American pronunciation:/?pa?θɑ?n/), is a widely used high-level programming language, belongs to the general-purpose programming language, created by Guido van Rossum, the first edition was released in 1991. It can be viewed as an improvement (adding the advantages of some other programming language, such as object-oriented) to LISP. As an interpreted language, Python's design philosophy emphasizes the readability of the code and the simplicity of the syntax (especially the use of spaces to indent blocks of code rather than braces or keywords). Allows developers to express ideas in less code than C + + or Java,python. Whether it's a small or large program, the language tries to make the structure of the program clear.

Like scheme, Ruby, Perl, TCL, and other dynamic-type programming languages, Python has dynamic type systems and garbage collection capabilities that automatically manage memory usage and support a variety of programming paradigms, including object-oriented, imperative, functional, and procedural programming. It itself has a large and extensive standard library.
The Python virtual machine itself can run on almost all operating systems. Python's formal interpreter, CPython, is a community-driven, free software written in C, and is currently managed by the Python Software Foundation.

Ii. The origins of Python

The founder of Python is 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. Python was chosen as the name of the program because he was a fan of the BBC drama-Monty Python's Flying Circus. ABC is a language of instruction designed by Guido. As far as Guido himself is concerned, the language of ABC is very graceful and powerful and is designed specifically for non-professional programmers. However, the ABC language did not succeed, the reason, Guido think is caused by non-open.
Guido is determined to avoid this error in Python, and to get a very good result, a perfect combination of C and some other languages.
In this way, Python was born in the hands of Guido. In fact, the first implementation is on a Mac machine. It can be said that python evolved from ABC and was largely influenced by the Modula-3 (another rather beautiful and powerful language designed for small groups). and a combination of Unix shell and C habits.
Currently Guido is still the main developer of Python, determining the direction of the entire Python language. The Python community often calls him a benevolent dictator.

Three, Python design philosophy and positioning

Python's design philosophy is "elegant", "clear", "simple". The Python developer's philosophy is "in one way, preferably one way to do something," and therefore it is very different from other languages that have a distinct personal style. When designing the Python language, Python developers tend to reject the fancy syntax when faced with a variety of choices, and choose a syntax that is unambiguous or rarely ambiguous. These guidelines are called "Python maxims." Run import this in the Python interpreter to get a complete list:

Python 3.6.0 (V3.6.0:41df79263a11, Dec, 08:06:12) [MSC v.1900 64bit (AMD64)] on Win32 Type"Copyright","credits" or "license ()"  forMore information. >>>ImportThis is the Zen of Python, by Tim Peters Beautiful isbetter than ugly. Explicit isbetter than implicit. Simple isbetter than complex. Complex isbetter than complicated. Flat isbetter than nested. Sparse isbetter than dense.    Readability counts. Special cases aren'T special enough to break the rules.Although practicality beats purity. Errors should neverPasssilently.    Unless explicitly silenced.    In the face of ambiguity, refuse the temptation to guess. There should be one-- andPreferably only one--obvious-do it. Although that, may notBe obvious at first unless're Dutch.Now isbetter than never. Although never isOften better than *right*Now . If the implementation isHard to explain, it's a bad idea.If the implementation isEasy to explain, it is a good idea. Namespaces is one honking great idea-Let's do more than those!

Note : The above is excerpted from Wikipedia, Address: Https://zh.wikipedia.org/wiki/Python.

Iv. What is the language of Python? 1. Compiling and Interpreting1) compilers and interpreters

* compiler is to translate each statement of the source program into machine language, and save it as a binary file (that is, the executable code), translation and execution is separate, so that the runtime computer can be directly in the machine language to run the program, fast;
* While the interpreter is only in the execution of the program, only one interpretation of the machine language to the computer to perform (that is, the translation and execution of the source program is done one time, do not generate the target code can be stored), 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).

* The biggest difference between the two is: for the implementation of the compiler, the control of the runtime in the user program, for the interpretation of execution, the program run time control in the interpreter and not the user program;

2) 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 and portability, which 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.
Disadvantage: Every time you run the time to explain, the implementation of low efficiency, occupy a large space, not only to the user program to allocate space, the interpreter itself also occupies a valuable system resources, so performance is not as good as the compiled language.

3) Working mode of the compiled language

1. Source file (contains all program code)----> compiler (COMPILE)----> executable (run directly)

2. A project (containing individual source files)----> compiler (COMPILE)----> generate the corresponding target file----> Linker (Linker)----> "package" the target file into an executable (link) Link program, in addition to linking the destination file, There may be a variety of resources, such as icon files, sound files, but also responsible for removing redundant duplication of code between the target files, and so on;

4) Working mode of explanatory language

before the program runs, there is only the source program and no executable program, and every execution of the program to the source program, there will be a shell called an interpreter to convert the source code into binary code for execution, in general, is to constantly explain, execute, interpret, execute ...

2. Dynamic language and static 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 the 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 of language typical representative, other static type language also has C #, Java and so on.

3. Strong type definition language and weak type definition language

1) Strongly typed definition language: Enforces the language of the data type definition. 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) Weak type definition language: The data type can be ignored by the language. 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).

v. Advantages and disadvantages of Python

According to the latest Tiobe leaderboard, Python ranks fourth in the popularity of all programming languages and is an excellent and widely used language.

( note : The Tiobe leaderboard is based on the number of experienced programmers, courses, and third-party vendors on the Internet, and uses search engines such as Google, Bing, Yahoo!) and Wikipedia, Amazon, YouTube to figure out rankings, Just reflecting the popularity of a programming language does not explain how well a programming language is, or how much code is written in a language. )

https://www.tiobe.com/tiobe-index/

Now that Python is so hot, what are its advantages?

1. Advantages of Python
    • Simple: Python is a language that represents the idea of simplicity. Reading a good Python program feels like reading English. It allows you to focus on solving problems rather than figuring out the language itself;
    • Easy to learn: Python is extremely easy to get started because Python has extremely simple documentation ;
    • Fast: The bottom of Python is written in C, many standard libraries and third-party libraries are written in C, running very fast;
    • Free, open Source: Python is one of FLOSS(Free/ open source software). The user is free to publish copies of the software, read its source code , make changes to it, and use it as part of the new free software. Floss is the concept of sharing knowledge based on a group;
    • High-level languages: When writing 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;
    • portability: Because of its open source nature, Python has been ported on many platforms (modified to make it work on different platforms). These platforms include Linux, Windows, FreeBSD, Macintosh, Solaris, OS/2, Amiga, AROS, as/400, BeOS, os/390, z/OS, Palm OS, QNX, VMS, Psion, Acom RISC OS, VxWorks, PlayStation, Sharp Zaurus, Windows CE, PocketPC, Symbian and Google Linux-based Android platforms;
    • explanatory: A program written in a compiled language such as C or C + + can be from source file (that is, C or C + +) translates to a language used by your computer ( binary code , which is 0 and 1). This process is done by compiler and different tags, options. When you run the program, the connection/reprint software copies your program from the hard disk into memory and runs. Programs written in the Python language do not need to be compiled into binary code. You can run the program directly from source code inside the computer, Python interpreter The converts the source code into an intermediate form called bytecode , and then translates it into a computer-used machine language and run. This makes it easier to use Python. Also makes Python programs easier to migrate;
    • Object-oriented : Python supports both process -oriented programming and object-oriented programming. In a "process-oriented" language, a program is built from a procedure or simply a function of reusable code. In the " object-oriented " language, the program is constructed of objects composed of data and functions;
    • Extensibility: If you need a piece of critical code to run faster or you want some algorithms to be private, you can write some programs in C or C + + and then use them in a Python program;
    • Embeddable: Python can be embedded in a C + + program to provide scripting capabilities to program users;
    • Rich Library: The Python standard library is really huge. It can help with all kinds of work, including regular expressions , document generation, unit tests , threads , databases, Web browsers, CGI, FTP, e-mail, XML, XML-RPC, HTML, WAV files, cryptography Systems , GUI ( graphical user interface ), TK, and other system-related operations. This is called Python's "fully functional" concept. In addition to the standard library, there are many other high-quality libraries, such as Wxpython, twisted, and Python image libraries, and so on;
    • Canonical code: Python uses forced indentation to make the code more readable. Programs written in the Python language do not need to be compiled into binary code.
2, the shortcomings of Python
    • Single-line statements and command-line output problems: Many times you cannot write a program to a line, such as import sys;for I in Sys.path:print i. Perl and awk do not have this limitation, it is easier to complete a simple program under the shell, do not need to like Python, the program must be written to a. py file.
    • Unique Syntax: This may not be referred to as limitation, but it is also confusing for many beginners by indenting the way in which the statements are differentiated. Even the most experienced Python programmers can get caught up in a trap.
    • Slow running: This is compared to C and C + +.

Of course, any language is not perfect, have good and not good at, language is just a tool, is to realize the idea of the program designer tools, as long as we use them, can create value!

vi. Description of Python's interpreter and its development environment1. Python interpreterPython is a cross-platform scripting language, Python rules a Python syntax, and implementing the Python syntax interpreter becomes the Python interpreter.
    • CPython (Classicpython, which is the original Python implementation, needs to be distinguished from other implementations when CPython is called, or a python that is implemented as a C language). This is the most commonly used version of Python, and the CPython implementation converts the source file (py file) into a bytecode file (PYc file) and then runs on the Python virtual machine;
    • Jyphon (Python, formerly known as Jpython;java language implementation, is now officially released). Jython can directly invoke Java's various function libraries, which will dynamically compile Python code into Java bytecode, and then run on the JVM;
    • PyPy (python written in Python language)
    • IronPython (for. NET and the ECMA CLI Python implementation). IronPython can directly invoke the various library functions of the. NET Platform. You can compile a Python program into a. NET program.
    • Zhpy (Python language supporting the use of complex/Hymnal statement writing program)

2, the development environment of Python

    • Idle:python built-in IDE (supplied with Python installation package);

    • Pycharm : Developed by a well-known JetBrains company with a set of tools to help users improve their efficiency when developing with the Python language, such as debugging, syntax highlighting, project management, code jumps, smart tips, AutoComplete, unit tests, version control. In addition, the IDE provides advanced features to support professional web development under the Django framework;

    • Komodo and Komodo Edit: the latter is a free lite version of the former;

    • Spyder: Install the Advanced IDE that comes with Anaconda;

    • The IDE is available in Pythonwin:activepython or PYWIN32, only for Windows;

    • SPE (Stani's Python Editor): Free software With more features, based on Wxpython;

    • Ulipad: Free software with more functions, based on Wxpython; the author is a Chinese python master Limodou;

    • Wingide: May be the most functional IDE, but not free software (educational users and open source users can apply for free key);

    • Eric: PYQT-based free software, powerful. The full name is: the Eric Python IDE;

    • Drpython;

    • Pyscripter: The lightweight open source Python IDE developed with Delphi, supports Python2.6 and 3.0;

    • Pype: An open-source cross-platform pythonide;

    • Bpython: A lightweight Python interpreter developed under the Unix-like operating system using the curses library. Grammar hints function;

    • Eclipse + Pydev Plugin: easy to debug program;

    • Emacs: With Python support, auto-complete, refactor and other functions require plug-in support;

    • Vim: The latest version of the 7.3 compilation can be added to Python support, the Python code to provide automatic prompt support;

    • Visual Studio 2003 + Visualpython: Windows only, discontinued maintenance, poor functionality

    • SlickEdit;

    • Visual Studio + Python Tools for Visual Studio

    • TextMate

    • Netbeans IDE

    • Sublime

    • Ipython

In addition, such as notepad++, EditPlus, UltraEdit and other general Programmer's text editor software can also provide some support for Python code editing, such as code auto-coloring, comment shortcut keys, etc., personal comparison recommended Pycharm and Sublime, of course, the python comes with the idle is also very good.

Python Basics (i) first knowledge of Python

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.