Python from entry to proficiency Chapter 1 (Understanding Python)

Reference tutorial: Dark Horse Programmer Python Tutorial_600 episodes of Python tutorials from entry to proficiency (you can learn it if you understand Chinese)_bilibili_bilibili

1. Get to know Python

1. The origin of Python

(1) During the Christmas period of 1989, in order to kill time in Amsterdam, Guido van Rossum decided to develop a new interpreter as a successor to the ABC language.

(2) ABC is a teaching language designed by Guido. In Guido's opinion, ABC is a very beautiful and powerful language and is specially designed for non-professional programmers. However, the ABC language did not succeed. Guido believed that the reason was due to non-openness. Guido was determined to avoid this mistake in Python and achieved very good results.

(3) Python (Python) was chosen as the name of the program because it is a fan of the BBC TV series Monty Python's Flying Circus.

(4) In 1991, the first Python interpreter was born. It was implemented in C language and could call C language library files.

2. Interpreter

(1) Computers cannot directly understand any language other than machine language, so the program language written by the programmer must be translated into machine language before the computer can execute the program.

(2) Tools that translate other languages ​​into machine language are called compilers.

(3) There are two ways of compiler translation, one is compilation and the other is interpretation. The difference between the two methods lies in the translation time point. When a compiler runs in interpreted mode, it is also called an interpreter.

(4) Compiled languages ​​and interpreted languages:

①Compiled language:

[1] The program requires a special compilation process before execution. The program is compiled into a machine language file. There is no need to re-translate it when running. Just use the compiled result directly.

[2] The program execution efficiency is high, it depends on the compiler, and its cross-platform performance is poor.

[3] For example, C and C++ are compiled languages.

②Interpreted language:

[1] Programs written in interpreted languages ​​are not pre-compiled, the program code is stored in text form, and the code is run directly sentence by sentence.

[2] When publishing a program, it seems that the compilation process is saved, but when running the program, it must be interpreted before running.

[3] For example, Python is an interpreted language.

③ Comparison between compiled languages ​​and interpreted languages:

[1] Speed: Compiled languages ​​execute faster than interpreted languages.

[2] Cross-platform: Interpreted languages ​​are better cross-platform than compiled languages.

3. Features of Python

(1) Python is a completely object-oriented language. Functions, modules, numbers, and strings are all objects. Everything in Python is an object.

(2) Python fully supports inheritance, overloading, multiple inheritance, overloaded operators, and generic design.

(3) Python has a powerful standard library. The core of the Python language only contains common types and functions such as numbers, strings, lists, dictionaries, and files, while the Python standard library provides additional functions such as system management, network communication, text processing, database interfaces, graphics systems, and XML processing.

(4) The Python community provides a large number of third-party modules, which are used in a manner similar to the standard library. Their functions cover many fields such as scientific computing, artificial intelligence, machine learning, Web development, database interfaces, and graphics systems.

(5) If you need a key piece of code to run faster or you want certain algorithms not to be made public, you can write this part of the program in C or C++ and then use them in the Python program.

4. Object-oriented thinking

(1) Object-oriented is a way of thinking and a programming technology.

(2) Before solving a problem, first consider who will do it, how to do it, and whose responsibility it is. Finally, just get the thing done, and the target is "who".

(3) To solve complex problems, you can find multiple different objects, each performing their own duties, working together to achieve the goal, and ultimately completing the requirements.

5. Advantages and Disadvantages of Python

(1) Advantages: simple and easy to learn; free and open source; object-oriented; rich libraries; scalability.

(2) Disadvantages: Slow running speed.

2. Understand Python programs

1. Basic concepts of Python source programs

(1) The Python source program is a text file in a special format, which means that any text editing software can be used for Python development.

(2) The file extension of Python programs is usually .py .

2、HelloPython

Enter the following code in the .py file and run it. The running results are displayed in the window below PyCharm. In this case, it is "HelloPython".

# print函数可在窗口打印引号中的内容
print("HelloPython")

3. Understanding errors (BUG)

(1) Definition of error: The written program cannot be executed normally, or the execution result is not what we expect.

(2) Errors are commonly known as BUGs. BUGs are very common among programmers during development.

(3) Several common errors:

Name error : For example, when using print("Hello world"), "print" is not spelled correctly.

Syntax error : For example, statements that should be separated into separate lines are all written on one line. As shown in the figure below, multiple prints are written on one line.

③Indentation error : Python is a programming language with a very strict format. For now , just remember not to add spaces in front of each line of code .

4. Comments

(1) The role of comments: Use languages ​​you are familiar with (such as Chinese and English) to annotate certain codes in the program to enhance the readability of the program.

(2) Single line comment:

A single-line comment starts with "#" , and everything to the right of "#" is regarded as explanatory text, not the actual program to be executed, and only serves as an auxiliary explanation, as shown in the figure below.

②In order to ensure the readability of the code, it is recommended to add a space after "#" and then write the corresponding explanatory text . (It doesn’t matter if you don’t add spaces. Some compilers will remind you, but it will not affect the correct operation of the program)

③In order to ensure the readability of the code, there must be at least two spaces between comments and code . (It doesn’t matter if you don’t add spaces. Some compilers will remind you, but it doesn’t affect the correct operation of the program. It’s just not beautiful enough)

(3) Multi-line comments (block comments):

① If you want to write a lot of comment information that cannot be displayed completely in one line, you can use multi-line comments.

② To use multi-line comments in a Python program, you can use a pair of three consecutive quotation marks (both single quotation marks and double quotation marks are acceptable), and add explanatory text inside the quotation marks, as shown in the figure below.

5. Line continuation character

(1) Python programs are written line by line, and there is no limit to the length of each line of code. If a single line of code is too long, you can use the line continuation character "\" to split the single line of code into multiple lines of expression.

(2) Things to note when using line continuation characters:

① There cannot be a space after the line continuation character, and a line break must be made directly.

②The line continuation character cannot be inserted anywhere. For example, it cannot be inserted in keywords and variable names.

(3) Example:

a = 2005
b = 803

print("数字{}\
和数字{}的乘积是{}".format\
          (a,b,a*b),end="。")
# 等价于print("数字{}和数字{}的乘积是{}".format(a,b,a*b))

Guess you like

Origin blog.csdn.net/Zevalin/article/details/135109321