The first lesson. Python entry and environment introduction

Introduction to Python

Python is an interpreted and interactive language with a concise syntax. The interpreted language refers to execution through an interpreter, so the efficiency will be relatively low; the interactive type means that it can be executed interactively with commands. It can be seen that python has two execution methods. Python is also dynamic and does not need to declare types.
The Python source code directory is similar to the following structure:
1.include: contains various header files
2.Lib: contains various standard libraries, implemented by python
3.Modules: modules written in C language
4.Objects: internal objects, int, list, etc. Implementation of
5.Python: interpreter core, encapsulate most of the functions into python.dll, the
execution process is: .py -> compile to bytecode. pyc -> pass to the interpreter (stacked virtual machine) -> get Results
The stack virtual machine above refers to bytecode. Pyc has nothing to do with the machine, only the virtual machine can interpret it. .pyc is also an object object. In the file, you will see python.exe, whose function is to use a simple and compact exe to call each dll library under "Python" for explanation.
Here is a small example:

#解释型方式接收参数
import sys
print('the file name',sys.argv[0],'The time run',sys.argv[1])
for i in range(int(sys.argv[1])):
    print(i,'time run')

"""
示例:
terminal>python ./STACK/ground.py 2
the file name ./STACK/ground.py The time run 2
0 time run
1 time run
"""

Interpreter classification

The official python is implemented by the C language, so the interpreter is also called Cpython.
The python implemented by python is called PyPy. The python implemented
by .Net is Ironpython;
Cpython has two important stacks, the first is the execution stack, which is used to store instructions and The operand, the other is the block stack, which is used to store loop and exception information.
The source file .py–>.pyc can also be completed with the complie() function. .py is essentially a character text, and complie() can generate Object: .pyc

#将.py编译到.pyc
src="""
print('hello')
"""
pyobj=compile(src,'file.py','exec')
exec(pyobj)#类似import file.py,将这个src的pyc加载到当前会话

Packages and modules

Module and package are two different things, a module is a file, suppose there is a module modules.py

import modules

It means that if the various objects (variables, functions, classes) in the subsequent callable module are
changed to

from modules import *

The next time you call the object, you will not need to add the prefix "modules."
You will see it in some scripts

if __name__=="__main__":

The function of if name == " main ": when a module needs to be used as a program entry, it can be adaptive; a
package is a combination of various modules, commonly used built-in packages are: sys, re, os
built-in packages are also a complex system, often To view the documentation, we can view the documentation through the command line:

>python -m pydoc -p 0

Third-party packages are managed by pip. Common third-party packages include numpy, pandas, etc.

Environment introduction

There are many environments. There are jupyter notebook and jupyter lab
in jupyter notebook. The shortcut keys are explained in detail: in the esc+h
notebook, there are two types of cell: code and markdown, code can run, markdown supports latex, latex is written in html format, and The automatic vector rendering
notebook drawing display requires a magic switch:

%matplotlib inline

Briefly review the various magic switches:

%quickref

Online google driver (requires VPN), codespace, but in some areas, the speed is relatively slow, for the convenience of elementary learning, you can use the online python3 interpreter
or go directly to the official website Shell

Guess you like

Origin blog.csdn.net/qq_40943760/article/details/109091142