Basic knowledge of computer--language introduction

Source: Internet
Author: User
Tags shebang

interpreted and compiled languages

The computer itself does not recognize high-level languages, and when we run a program, we need a "translator" to translate the high-level language into a language that the computer can read. The "translation" process is divided into two types:

    1. Compiling a compiled language before executing the program, the compiler will first execute a compilation process to compile the program into machine language. Then, when the program runs again, do not "translate", but can be directly executed. Like the C language. The advantage of a compiled language is that it does not have to be explained when running the program, and can be used directly with the translated files.
    2. Interpreting the interpreted language is not a compile process, but rather the code is interpreted and then run by the interpreter as the program runs. Like Python.

After the emergence of virtual machine-based languages such as Java, programming languages are not simply divided into compiled or interpreted languages. Java is the first time a compiler compiles code into a bytecode file, and then executes Java bytecode in the JVM and interprets it as a machine language. So we say Java is a semi-compiled, semi-explanatory language.

C #, at the first execution, compiles the code into an IL intermediate code file, which is then executed by the JIT compiler to compile the cost of the machine code. Equivalent to compiling two times.

For Python, see below.

The first Python program

First we open the Python interactive interpreter and execute the following command:

1 # python3 2 Python 3.5.1+ (default, Mar, 22:46:26) 3 [GCC 5.3.1 20160330] on linux4 Type ' help ', ' copyright ', ' cr Edits "or" license "for more information.5 >>> print (' Hello World ') 6 Hello World

Then we write one of the simplest Python programs. Create a new first.py file with the following file contents:

1 print ("First Hello world!")
 

New second.py

Import Firstprint ("Second Hello world!")

Then we like to execute a second file:

Python second.py

Output:

First Hello world! Second Hello world!
Procedures for the execution of Python programs

This time we will find that a pycache directory has been generated; The directory has file FIRST.CPYTHON-35.PYC. If the Python version is 2.x, FIRST.PYC is generated directly in the current directory. We say that Python is an interpreted language, so what is this PYc file? According to the general understanding, C in PYC should be the meaning of compile, since that is how the Python code is converted into a series of machine instructions? Now let's take a good look at the causal relationship here. In fact, Python and Java, C # is the same, the three principles of program execution can be summed up in two words-virtual machine, byte code. There's a very central thing in Python,--interpreter, when we typed Python in the command line, when we typed the command in the shell python , it was to activate the interpreter; python second.py The Python interpreter is immediately activated and then executes the Python program. The interpreter in Pyhton also has a very important job to do-compiling the. py file.

When executing a program, the Python interpreter starts by compiling the Python source code in the file, generating a python bytecode (byte code), and handing the bytecode to the Python virtual machine, which executes the bytecode sequentially, one after the other, until the program executes. As we python second.py did, we found that only the second.py file generated the PYc file for the reason we don't know, but we can guess that Python will compile only the files that need to be compiled when it executes. What is the file that needs to be compiled? For example, the first.py file is not only called by second.py, but also by other PY file calls, this time, if the first.py to compile, the next time directly call the compiled PYC file, so that the speed of execution.

Then someone will ask, if I modified the first.py file, the next time I call First.pyc is not the latest code, for this we do not need to worry. The PYc file contains the time it was created, and when the Python program executes, the first attempt is to load the PYc file, which, during the loading process, will be more than the time of the py file and the PYc file, and if the PYc file time is earlier than the Py file time, the py file will be recompiled. Generate a new PYc file, otherwise it will call the PY directly from the file.

Python Overall architecture

Knowing the execution of a Python program, you should also understand what the overall architecture of Python is. The overall architecture of Python can be divided into three main parts.

    • The left part of Python provides a large number of modules, libraries, and user-defined modules.
    • The middle part of the Python core-interpreter, also can be called virtual machine, where the direction of the arrow is Python running the direction of data flow, scanner corresponding lexical analysis, used to file or command input each line of code to cut into tokens; Parser the corresponding grammatical analysis, the parsing results of scanner are analyzed and the abstract Syntax tree (AST) is established. Compiler generates a set of instructions based on the established AST-byte code; Code Evaluator executes a bytecode file, so it is also called a virtual machine.
    • The arrows between the right part of the object/type, memory allocation, and interpreter indicate the "use" relationship between the two; The arrows between the run-time state and the interpreter indicate a "modify" relationship. Python will constantly modify the state of the current interpreter and switch between different states during execution.
Python program file Header #!
#!/usr/bin/env python print ("Hello world!")

Shebang is a string line (#!) consisting of a pound sign and an exclamation mark, which appears in the first two characters of a text file. In the case of shebang in a file, the program loader of the Unix-like operating system parses the contents of the Shebang, interprets the contents as an interpreter directive, invokes the instruction, and takes the file path containing the shebang as the parameter of the interpreter. #! is first used to help the kernel find the Python interpreter, but it will be ignored when importing the module. It is therefore necessary to join #! only in documents that are directly executed.

File encoding What is the file encoding (learn to ignore)

The text images we see on our monitors are not what we see when they are stored on the computer. If you open the hard drive and take the platters out of the microscope, you'll see a lot of bumps in it. Similar to the CDs we normally see. The location of the bump represents 0 and 1, respectively. This is because there are only two states of electrical signals in the computer, with electricity and no electricity. When storing data, if we want to save a letter "a", then we can use a certain length of 0 and one to represent. For example, use "01000001" to denote a. With this correspondence, we can save some of the characters we see in the usual way.
So there is the ASCII (United States (national) Information Interchange Standard (generation) code), using 7 or 8 BITS encoding scheme, can give up to 256 characters. With ASCII code, data normalization can be achieved between different computers.
But there are some limitations when using ASCII. He can represent a maximum of 256 characters. If there are other characters, there is nothing to do. ASCII can only represent 26 basic Latin alphabet, Arabic numerals, and English punctuation. So it can only be used to display modern American English.
Later the computer world began to have other languages, ASCII code has not been able to meet the demand. Later people in different languages individually customized a set of their own code, while maintaining compatibility with ASCII. These encodings are collectively known as MBCS, where everyone begins to use double-byte. (China's called gb*, such as GBK).
Later people began to think that so many coding, some coding is not compatible, too big head, so a group of people sitting together came up with a way: All languages use the same encoding, this encoding is Unicode. Unicode uses a minimum of 2 bytes (1 bytes =1byte=8bit= A binary number with a length of 8) to represent letters and symbols, and sometimes 4 bytes. This solves the problems encountered above.
Unicode, also known as the Universal Code, is a standard in the industry. But some people think that if I want to represent an ASCII character, using Unicode to indicate that it is not too wasteful, then someone came up with another solution--utf-8.
UTF-8 is the compression and optimization of Unicode encoding, the most important feature is that it uses a variable length encoding, he no longer uses a minimum of 2 bytes, but all the characters are categorized. The contents of the ASCII code are saved with 1 bytes, the characters in Europe are saved in 2 bytes, and the characters in East Asia are saved with 3 bytes ...

    • The relationship between different encodings

Python2 the default encoding of the file is ASCII, when the file contains Chinese language will be error, at this time, we need to set the default encoding of the file, as follows:

#!/usr/bin/env python #-*-Coding:utf-8-*-  

In Python3, the default encoding for a file is UTF-8, which does not already exist.

Basic knowledge of computer--language introduction

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.