Basic knowledge of Python (1) First knowledge of Python

Introduction to Python

1. Introduction to Python

    Python (UK pronunciation: /ˈpaɪθən/ US pronunciation: /ˈpaɪθɑːn/), is a widely used high-level programming language, belonging to the general-purpose programming language, created by Guido Van Rossum, the first version was released in 1991. Think of it as an improved (adding some of the advantages of other programming languages, such as object orientation) LISP. As an interpreted language, Python's design philosophy emphasizes code readability and concise syntax (especially the use of whitespace indentation to delimit code blocks, rather than curly braces or keywords). Python allows developers to express ideas with less code than C++ or Java. Whether it's a small or large program, the language tries to make the structure of the program unambiguous.

    Like dynamically typed programming languages ​​like Scheme, Ruby, Perl, Tcl, etc., Python has a dynamic type system and garbage collection, automatically manages memory usage, and supports a variety of programming paradigms, including object-oriented, imperative, functional, and procedural programming. It itself has a huge and extensive standard library.
    The Python virtual machine itself runs on almost all operating systems. Python's official interpreter, CPython, is written in C and is a community-driven free software currently managed by the Python Software Foundation.

 

2. The origin of Python

    The founder of Python is Guido van Rossum. During Christmas 1989, Guido van Rossum decided to develop a new script interpreter as a successor to the ABC language in order to pass the time in Amsterdam. Python was chosen as the name of the program because he is a fan of the BBC TV series Monty Python's Flying Circus. ABC is a language of instruction designed by Guido. In Guido's opinion, ABC is a very beautiful and powerful language designed for non-professional programmers. However, the ABC language did not succeed, and Guido believes that the reason is due to non-openness.
    Guido was determined to avoid this error in Python, and achieved very good results, a perfect combination of C and some other languages.
    And just like that, Python was born in Guido's hands. In fact, the first implementation was on a Mac. It can be said that Python grew out of ABC, mainly influenced by Modula-3 (another rather beautiful and powerful language, designed for small groups). And combined the Unix shell and C habits.
    At present, Guido is still the main developer of Python, deciding the development direction of the entire Python language. The Python community often calls him a benevolent dictator.

 

3. Python Design Philosophy and Positioning

    Python's design philosophy is "elegance", "clear", "simple". The philosophy of Python developers is "one way, preferably only one way to do one thing", and as such it is very different from other languages ​​that have a distinctly personal style. When faced with multiple choices when designing the Python language, Python developers generally reject fancy syntax in favor of explicit syntax with no or little ambiguity. These guidelines are called "Python maxims". Run import this inside the Python interpreter to get the full list:

Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
    Type "copyright", "credits" or "license()" for more information.
    >>> import this
    The Zen of Python, by Tim Peters

    Beautiful is better than ugly.
    Explicit is better than implicit.
    Simple is better than complex.
    Complex is better than complicated.
    Flat is better than nested.
    Sparse is better than dense.
    Readability counts.
    Special cases aren't special enough to break the rules.
    Although practicality beats purity.
    Errors should never pass silently.
    Unless explicitly silenced.
    In the face of ambiguity, refuse the temptation to guess.
    There should be one-- and preferably only one --obvious way to do it.
    Although that way may not be obvious at first unless you're Dutch.
    Now is better than never.
    Although never is often better than *right* now.
    If the implementation is hard to explain, it's a bad idea.
    If the implementation is easy to explain, it may be a good idea.
    Namespaces are one honking great idea -- let's do more of those!

Note : The above content is taken from Wikipedia, address: https://zh.wikipedia.org/wiki/Python .

 

4. What kind of language is Python?

  1. Compile and interpret

1) Compiler and Interpreter

    *The compiler translates each statement of the source program into machine language and saves it as a binary file (the executable object code). The translation and execution are separated, so that the runtime computer can directly run the program in machine language The program is very fast;
    *The interpreter only interprets the program one by one into machine language for the computer to execute (that is, the translation and execution of the source program is completed at one time, and no storable memory is generated. Object code), so the running speed is not as fast as the compiled program. This is because the computer cannot directly recognize and execute the statements we write, it can only recognize machine language (in binary form).

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

2) Compiled VS Interpreted   

    Compiled
    advantages: The compiler generally has a precompiled process to optimize the code. Because the compilation is done only once and does not need to be compiled at runtime, the program execution efficiency of the compiled language is high. Can be run independently of the locale.
    Disadvantage: After compiling, if you need to modify it, you need to recompile the entire module. When compiling, the machine code is generated according to the corresponding operating environment. There will be problems in porting between different operating systems. Different executable files need to be compiled according to the operating operating system environment.

    Interpreted
    advantages: good platform compatibility and portability, can run in any environment, provided that an interpreter (virtual machine) is installed. Flexible, you can directly modify the code when modifying the code, and can be deployed quickly without downtime for maintenance.
    Disadvantages: It has to be interpreted every time it runs, which has low execution efficiency and takes up a lot of space. Not only does it have to allocate space for user programs, but the interpreter itself also occupies valuable system resources, so its performance is not as good as compiled languages.

 

3) The working mode of compiled language

    1. Source file (including all program code) ----> compiler (compile) ----> executable file (directly run)

    2. A project (including each source file) ----> compiler (compile) ----> generate the corresponding object file ----> linker (Linker) ----> "package the object file" In addition to linking object files, the linker may also have various resources, such as icon files, sound files, and is also responsible for removing redundant and repetitive code between object files, etc.;

 

4) The working mode of interpreted language

   At the moment before the program runs, there is only the source program but no executable program; and every time the program executes an instruction of the source program, a shell called the interpreter will convert the source code into binary code for use. To execute, in a word, is to explain, execute, explain, execute…

 

2. Dynamic and static languages

    1) Dynamically typed language: A dynamically typed language refers to a language that does data type checking during runtime, that is to say, when programming in a dynamically typed language, you never have to specify a data type for any variable, and the language will The first time you assign to a variable, the data type is recorded internally. Python and Ruby are typical dynamically typed languages, and other scripting languages ​​such as VBScript are also somewhat dynamically typed languages.
    2) Statically typed language: A statically typed language is just the opposite of a dynamically typed language. Its data type is checked during compilation, that is to say, the data type of all variables must be declared when writing a program. C/C++ is a statically typed language. Typical representatives, other statically typed languages ​​include C#, JAVA, etc.

 

3. Strongly Typed Definition Language and Weakly Typed Definition Language

   1) Strongly Typed Definition Language: A language that enforces the definition of data types. That is to say, once a variable is assigned a data type, if it is not cast, it will always be this data type. For example: if you define an integer variable a, it is impossible for the program to treat a as a string type at all. A strongly typed language is a type-safe language.
    2) Weakly typed language: a language in which data types can be ignored. It is the opposite of strongly typed language, a variable can be assigned values ​​of different data types.

    Strongly typed language may be slightly inferior to weakly typed language in speed, but the rigor brought by strongly typed language can effectively avoid many errors. Also, there is absolutely no connection between "is this language dynamic" and "is this language type safe"! For example: Python is a dynamic language and is a strongly typed language (type safe language); VBScript is a dynamic language and is a weakly typed language (type unsafe language); JAVA is a static language and is a strongly typed language (type safe language) language).

 

Five, the advantages and disadvantages of Python

    According to the latest TIOBE rankings, Python ranks fourth in popularity among all programming languages ​​and is an excellent and widely used language.

( Note : The TIOBE rankings are based on the number of experienced programmers, courses and third-party vendors on the Internet, and use search engines (such as Google, Bing, Yahoo!) and Wikipedia, Amazon, YouTube to count ranking data, just reflect The popularity of a programming language doesn't tell you whether a programming language is good or not, 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. The advantages of Python

  • Simplicity: Python is a language that represents the idea of ​​simplicity. Reading a good Python program feels like reading English. It enables you to focus on solving problems rather than figuring out the language itself;
  • Easy to learn: Python is extremely easy to use because Python has extremely simple documentation  ;
  • Fast speed: The bottom layer of Python is written in C language, and many standard libraries and third-party libraries are also written in C, which runs very fast;
  • Free, Open Source: Python is one of FLOSS (Free/ Open Source Software). Users are free to distribute copies of the software, read its source code , make changes to it, and use parts of it in new free software. FLOSS is based on the concept of a group sharing knowledge;
  • High-level language: Write programs in Python without having to worry about low-level details such as how to manage the memory your program uses;
  • Portability: Due to its open source nature, Python has been ported to many platforms (modified to 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's linux-based android platform;
  • Interpreted: A program written in a compiled language such as C or C++ can be converted from a source file (ie C or C++) to a language your computer uses ( binary code , ie 0s and 1s). This process is done through the compiler and different flags, options. When running a program, the linker/loader software copies your program from the hard disk to memory and runs it. Programs written in the Python language do not need to be compiled into binary code. You can run the program directly from the source code . Inside the computer, the Python interpreter converts the source code into an intermediate form called bytecode , which is then translated into the machine language used by the computer and run. This makes working with Python much simpler. It also makes Python programs more portable;
  • Object-Oriented : Python supports both procedural and object-oriented programming. In "procedurally oriented" languages, programs are built from procedures or functions that are just reusable code. In " object-oriented " languages, programs are built from objects that combine data and functionality;
  • Scalability: If you need a critical piece of code to run faster or you want some algorithms to be kept private, you can write part of the program in C or C++ and then use them in your Python program;
  • Embeddability: Python can be embedded into C/C++ programs to provide scripting functions to program users;
  • Rich library: The Python standard library is really huge. It can help with a variety of jobs, including regular expressions , documentation generation, unit testing , threading , databases, web browsers, CGI, FTP, email, XML, XML-RPC, HTML, WAV files, cryptography, GUI ( GUI ), Tk, and other system-related operations. This is called Python's "full-featured" philosophy. In addition to the standard library, there are many other high-quality libraries such as wxPython, Twisted, and the Python image library, among others;
  • Canonical code: Python uses mandatory indentation to make the code more readable. Programs written in the Python language do not need to be compiled into binary code.

 

2. Disadvantages of Python

  • Problems with single-line statements and command-line output: In many cases, the program cannot be written in one line, such as import sys;for i in sys.path:print i. However, perl and awk do not have this limitation, and it is more convenient to complete simple programs under the shell. It is not necessary to write the program into a .py file like Python.
  • Unique syntax: This probably shouldn't be called a limitation, but the way it uses indentation to distinguish statement relationships still confuses many beginners. Even experienced Python programmers can fall into the trap.
  • Running slow: here I mean compared to C and C++.

   

    Of course, no language is perfect, and there are both good and bad ones. Language is just a tool, a tool to realize the ideas of programmers. As long as we make good use of them, we can create value!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325076084&siteId=291194637