Python study notes (CSDN) - 1 - Introduction to Python

Follow the courses on CSDN to learn, Introduction to Python-Preliminary knowledge-CSDNPython entry skill tree 

First, here is a mind map of this course:


Table of contents

 1.Python definition

2 History of Python development

2.1 Development of Python

2.2 Python version update

2.3 The difference between Python2.0 and Python3.0

3 Python compilation and running process

3.1 Python interpreter

3.2 Types of Python interpreters

3.3 Python bytecode (.pyc)

3.4 Python virtual machine

4 Python applications

5 Features of Python

6 Advantages of Python


 1.Python definition

Python is an easy-to-learn scripting language that combines interpreted, compiled, interactive, and object-oriented scripting . Python provides high-level data structures, its syntax and dynamic typing, and interpretability make it the programming language of choice for developers.

  • Python is an interpreted language:  there is no compilation part of the development process. Similar to PHP and Perl languages.

  • Python is an interactive language:  code can be executed directly after a Python prompt >>>.

  • Python is an object-oriented language:  Python supports object-oriented style or programming technique where code is encapsulated in objects.

2 History of Python development

2.1 Development of Python

The founder of Python is Guido van Rossum . In 1989, in order to kill the boredom of Christmas, Guido decided to develop a new script interpreter as an inheritance of the ABC language.

2.2 Python version update

 The latest version of python has reached 3.10.6 (2022.8.15)

Download Python | Python.org

2.3 The difference between Python2.0 and Python3.0

  • Output aspect

Python2.0 uses the print keyword for output, such as: print "Hello";

Python3.0 uses the print() function, such as: print("Hello").

  • Input aspect

Python2.0 uses the raw_input() function, for example: name=raw_input("Please enter your name:");

Python3.0 uses the input() function, such as: name=input("Please enter your name:").

  • String encoding format

Python2.0 uses ASCII encoding by default to encode the input string;

Python3.0 uses Unicode encoding to encode strings by default.

  • How to format strings

Python2.0 uses % placeholders to output strings in a standardized format, such as: "Hello,%s" % ("World");

Python3.0 uses the format() function, such as: "Hello,{}".format("World").

  • Source file encoding format

Python2.0 uses ASCII by default, so when using Chinese, add a line of comments at the beginning of the source file: # - - coding: utf-8 - -;

Python3.0 uses utf-8 by default.

  • type of data

python2.0 has int and long integer types

python3.0 only has integer type int

  • Code specifications

Python 2.0 source code is not standardized and there are many duplicate codes

python 3.0 source code is streamlined, beautiful and elegant

3 Python compilation and running process

Although Python source code files (.py) can be run directly using Python commands, Python actually does not directly interpret Python source code. It has a compilation and running process. The specific process is as follows:

First, the Python source code (.py file) is compiled to generate Python Byte Code (Python Byte Code, the extension of the bytecode file is generally .pyc), and then executed by the Python Virtual Machine (PVM for short) Python bytecode, and finally output the running results in the terminal.

Through the above compilation and running process, it can be analyzed: Python is an interpreted language, which refers to interpreting Python bytecode rather than Python source code . The basic idea of ​​this mechanism is consistent with Java and .NET.

3.1 Python interpreter

The Python interpreter ( Interpreter ) translates the Python language into machine instructions that the computer CPU can understand.

That is the python we installed and used

3.2 Types of Python interpreters

  • Cpython : Developed in C language, running Python under the command line starts the Cpython interpreter. The officially downloaded python2.7 is Cpython. Cpython uses >>> as the prompt.
  • Ipython : An interactive interpreter based on CPython. In other words, IPython is only enhanced in interactive mode, but the function of executing Python code is exactly the same as CPython. IPython uses In [serial number]: as the prompt.
  • PyPy : Mainly aimed at execution speed. Use JIT technology to dynamically compile Python code. Most Python code can be run under PyPy.
  • Jpython : A Python interpreter running on the Java platform, which can directly compile Python code into Java bytecode for execution.
  • IronPython : A Python interpreter running on the Microsoft .Net platform that can directly compile Python code into .Net bytecode.
  • Boost.Python : enables Python and C++ class libraries to call each other (.pyc).

3.3 Python bytecode (.pyc)

Bytecode in Python is a data type, and the compiled result of Python code is a bytecode object. The bytecode object can be loaded by the virtual machine and run directly, and the pyc file is the saved form of bytecode on the hard disk.

If there is a test.py file that needs to be executed, it will first generate a .pyc file. Generally, the possible situations are as follows:

  1. Executing python test.py will compile test.py into bytecode and interpret it for execution, but test.pyc will not be generated.
  2. If other modules are loaded in test.py, such as import urllib2, then Python will compile urllib2.py into bytecode, generate urllib2.pyc, and then interpret and execute the bytecode.
  3. If you want to generate test.pyc, you can use python's built-in module py_compile to compile it, or you can execute the command to generate test.pyc. python -m py_compile test.py 
  4. When loading a module, if both .py and .pyc exist, python will run using .pyc. If the compilation time of .pyc is earlier than the time of .py, the .py file will be recompiled and the .pyc file will be updated.

3.4 Python virtual machine

Python does not compile the py file into machine code for running. Instead, the python virtual machine interprets and runs the py statements one by one. The execution method of the python virtual machine is to imitate the running method of ordinary x86 executable files.

The virtual machine input is a bytecode .pyc file. The bytecode file loader loads the binary .pyc file into the memory. It is interpreted and executed by the execution engine, and the output is the execution result of the bytecode file.

4 Python applications

Python applications mainly include the following areas:

  • Linux/UNIX operation and maintenance: Provides API (Application Programming Interface) to facilitate system maintenance and management.
  • GUI program development (PyQt, Kivy, etc.)
  • Web program development (Django, Flask and other frameworks): supports the latest XML technology.
  • Mobile App development (PyQt, Kivy, etc.): Python's PyOpenGL module encapsulates the "OpenGL application programming interface" and can perform two-dimensional and three-dimensional image processing. The PyGame module can be used to write game software.
  • Web crawler (providing data sources for search engines, deep learning and other fields)
  • Network programming (based on protocols such as Socket): Provides a wealth of modules to support socket programming, which can easily and quickly develop distributed applications. Many large-scale software development projects such as Zope, Mnet and BitTorrent. Google are using it extensively.
  • Graphics processing: It is supported by graphics libraries such as PIL and Tkinter, which can facilitate graphics processing.
  • Text processing: The re module provided by python can support regular expressions, and also provides SGML and XML analysis modules. Many programmers use python to develop XML programs.
  • Database programming: Can communicate with Microsoft SQL Server, Oracle, Sybase, DB2, Mysql, SQLite and other databases through modules that follow the Python DB-API (Database Application Programming Interface) specification. Python comes with a Gadfly module, which provides a complete SQL environment.
  • Data Science: NumPy extensions provide extensive interfaces to many standard mathematical libraries. Machine learning (scikit-learn, TensorFlow framework), data statistical analysis and visualization (Matplotlib, seaborn framework).

5 Features of Python

  • Easy to read and learn : Python is a very simple language with a clear style and forced indentation. Python has a simple syntax and is extremely easy to get started.
  • Free and open source : Python is free/open source software. You can modify and use its source code yourself.
  • Portability : Python can be ported on many platforms. Commonly used platforms include Linux, Windows, VxWorks, PlayStation, Windows CE, PocketPC, etc.
  • Interpretability : The Python interpreter converts the source code into bytecode, and then translates it into the machine language used by the computer and runs it, that is, the Python code does not need to be compiled before running.
  • Object-oriented : Python supports object-oriented programming. Programs define classes through composition and inheritance.
  • Scalability : If you need a critical piece of code to run faster or you want some algorithms not to be made public, you can write part of the program in C or C++ and then call it in the Python program.
  • Embeddability : Python can be embedded into C/C++ programs to provide scripting functionality to program users.
  • Interactive command line : python can be run in single-step literal translation, and the code can be executed directly after a Python prompt >>>.
  • Rich standard library : Python standard library includes string processing (dictionary, array slicing, regular expression re), document generation, multi-threading, serialization, database, HTML/XML parsing (BeautifulSoup, Expat), unit testing (PyUnit ), code version control (PySVN), WAV files, network control (urllib2), cryptosystem, GUI (graphical user interface, PyQt), graphics modules (Tkinter, PyTCL, WxPython), etc. In addition to the standard library, there are many other high-quality libraries, such as wxPython, Twisted, and the Python imaging library, among others.
  • Standardization : Using forced indentation makes the code more readable and reduces visual confusion.
  • Glue language : Python is often used as a glue language to "glue" programs written in different languages ​​together. Boost.Python enables Python and C++ class libraries to call each other (.pyc); Jpython is Python implemented in Java, and both class libraries can be used at the same time; Ironpython is the version of Python on the .NET platform.

6 Advantages of Python

  • Easy to learn, great for beginners but also great for experts
  • Highly scalable, suitable for large projects or small one-off programs (called scripts)
  • Portable, cross-platform
  • Embeddable (making ArcGIS scriptable)
  • stable and mature
  • Large user community

Guess you like

Origin blog.csdn.net/weixin_44362628/article/details/126352061