Python instruction for beginner

The current version of Python 3.9.x we are using was released in 2018, and the version number of Python is divided into three segments, like ABC. A represents a major version number

Getting to know Python

Introduction to Python

History of Python

  1. Christmas 1989: Guido von Rossum started writing a compiler for the Python language.
  2. February 1991: The first Python compiler (also an interpreter) was born. It was implemented in C language (behind) and can call C language library functions. In the earliest version, Python has provided support for building blocks such as “class”, “function”, and “exception handling”, as well as core data types such as lists and dictionaries. It also supports module-based construction of applications. .
  3. January 1994: Python 1.0 was officially released.
  4. October 16, 2000: Python 2.0 was released, adding a complete garbage collection, provides the right to Unicode support. At the same time, the entire development process of Python is more transparent, the influence of the community on the development progress gradually expands, and the ecosystem begins to form slowly.
  5. December 3, 2008: Python 3.0 was released. It is not fully compatible with the previous Python code. However, because many companies currently use Python 2.x in their projects and operations, Python 3.x is Many new features were later ported to Python 2.6/2.7 versions.

The current version of Python 3.9.x we are using was released in 2018, and the version number of Python is divided into three segments, like ABC. A represents a major version number. Generally, when the overall rewrite or changes that are not backward compatible occur, A is added; B represents a function update, and B is added when a new function appears; C represents a small change (for example, a fixed Bug), add C whenever there is a modification. If you are interested in the history of Python, you can read the online article titled “A Brief History of Python”.

Advantages and disadvantages of Python

Python has many advantages, which can be briefly summarized as follows.

  1. Simple and clear, low learning curve, easier to use than many programming languages.
  2. Open source code, with a strong community and ecosystem, especially in the field of data analysis and machine learning.
  3. Interpreted language is inherently platform portable, and the code can work on different operating systems.
  4. Provides support for two mainstream programming paradigms (object-oriented programming and functional programming).
  5. The code is highly standardized and readable, suitable for people with code cleanliness and obsessive-compulsive disorder.

The disadvantages of Python are mainly concentrated in the following points.

  1. The execution efficiency is slightly lower, and the parts that require high execution efficiency can be written in other languages ​​(such as: C, C++).
  2. The code cannot be encrypted, but now many companies do not sell software but sell services. This problem will be weakened.
  3. There are too many frameworks to choose from during development (for example, there are more than 100 web frameworks), and there are mistakes where there are choices.

Application areas of Python

At present, Python is widely used in the fields of web application back-end development, cloud infrastructure construction, DevOps, network data collection (crawler), automated testing, data analysis, and machine learning.

Install Python interpreter

To start the Python programming journey, you must first install the Python interpreter environment on your computer. The following will take the installation of the official Python interpreter as an example to explain how to install the Python environment on different operating systems. The official Python interpreter is implemented in C language and is also the most widely used Python interpreter, usually called CPython. In addition, the Python interpreter also has Jython implemented in Java language, IronPython implemented in C# language, and PyPy, Brython, Pyston and other versions. Interested readers can learn about it by themselves.

Windows environment

You can download the Windows installer (exe file) of Python from Python official website. It should be noted that if you install Python 3.x in Windows 7 environment, you need to install Service first. Pack 1 patch package (can be installed through the function of some tool software to automatically install system patches), the installation process is recommended to check “Add Python 3.x to PATH” (add Python 3.x to the PATH environment variable) and select custom For installation, it is best to check all items such as “pip”, “tcl/tk”, and “Python test suite” in the setting “Optional Features” interface. It is strongly recommended to choose a custom installation path and ensure that there is no Chinese in the path. After the installation is complete, you will see the prompt “Setup was successful”. If the Python interpreter cannot work due to missing some dynamic link library files when running the Python program later, you can solve it according to the following methods.

If the system shows that the api-ms-win-crt*.dll file is missing, you can refer to Analysis and Solution of the Reasons for Missing api-ms-win-crt*.dll, or directly download Visual C++ Redistributable for Microsoft official website Visual Studio 2015 files are repaired; if some dynamic link library files are missing after updating Windows DirectX, you can download a DirectX repair tool to repair.

Linux environment

The Linux environment comes with Python 2.x version, but if you want to update to the 3.x version, you can download the Python source code from Python official website and pass the source code The specific steps are as follows (take CentOS as an example).

  1. Install dependent libraries (because without these dependent libraries, the source code component installation may fail due to missing underlying dependent libraries).

    yum -y install wget gcc zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel
  2. Download the Python source code and unzip it to the specified directory.

    xz -d Python-3.7.6.tar.xz
    tar -xvf Python-3.7.6.tar
  3. Switch to the Python source code directory and execute the following commands to configure and install.

    cd Python-3.7.6
    ./configure --prefix=/usr/local/python37 --enable-optimizations
    make && make install
  4. Modify the file named .bash_profile in the user’s home directory, configure the PATH environment variable and make it effective.

    cd ~
    vim .bash_profile
    # ... The above code is omitted here...
    export PATH=$PATH:/usr/local/python37/bin
    # ... The following code is omitted here...
  5. Activate the environment variable.

    source .bash_profile

macOS environment

macOS also comes with Python 2.x version, you can install Python 3.x version through the installation file (pkg file) provided by Python official website. After the default installation is complete, you can start the 2.x version of the Python interpreter by executing the python command in the terminal. To start the 3.x version of the Python interpreter, you need to execute the python3 command.

Run Python program

Confirm Python version

You can type the following commands in the Windows command line prompt.

python --version

Type the following command in the terminal of Linux or macOS system.

python3 --version

Of course, you can also enter python or python3 to enter the interactive environment, and then execute the following code to check the Python version.

import sys


Writing Python source code

You can use text editing tools (recommended to use Sublime, Visual Studio Code and other advanced text editing tools ) Write the Python source code and save the file with py as the suffix. The code content is shown below.

print('hello, world!')

Run the program

Switch to the directory where the source code is located and execute the following command to see if “hello, world!” is output on the screen.




Comments in the code

Comments are an important part of a programming language, used to explain the role of the code in the source code to enhance the readability and maintainability of the program. Of course, you can also remove the code segments in the source code that do not need to participate in the operation through comments. , This is often used when debugging a program. The comment will be removed when entering the preprocessor or compiling with the source code, will not remain in the target code and will not affect the execution result of the program.

  1. Single-line comment-the part starting with # and space
  2. Multi-line comments-start with three quotation marks and end with three quotation marks

    The first Python program-hello, world!
    Tribute to the great Mr. Dennis M. Ritchie
    Version: 0.1
    Author: Luo Hao
    print('hello, world!')
    # print("Hello, world!")

Python Development Tools

IDLE-comes with integrated development tools

IDLE is the integrated development tool that comes with the installation of the Python environment, as shown in the figure below. But because IDLE’s user experience is not so good, it is rarely used in actual development.

IPython-A better interactive programming tool

IPython is an interactive interpreter based on Python. Compared with the native Python interactive environment, IPython provides more powerful editing and interactive functions. You can install IPython through the Python package management tool pip. The specific operations are as follows.

pip install ipython


pip3 install ipython

After the installation is successful, you can start IPython through the following ipython command, as shown in the figure below.

Sublime Text-Advanced text editor

-First, you can download and install Sublime Text 3 or Sublime Text 2 through Official Website.

-Installation package management tool.

  1. Open the console through the shortcut key Ctrl+` or select Show Console in the View menu, and enter the following code.

-Sublime 3

import urllib.request,os;pf='Package Control.sublime-package';ipp=sublime.installed_packages_path();urllib.request.install_opener(urllib.request.build_opener(urllib.request.ProxyHandler()));open( os.path.join(ipp,pf),'wb').write(urllib.request.urlopen(''+pf.replace('','%20')) .read())

-Sublime 2

import urllib2,os;pf='Package Control.sublime-package';ipp=sublime.installed_packages_path();os.makedirs(ipp)ifnotos.path.exists(ipp)elseNone;urllib2.install_opener(urllib2.build_opener(urllib2. ProxyHandler()));open(os.path.join(ipp,pf),'wb').write(urllib2.urlopen(''+pf.replace('', '%20')).read());print('Please restart Sublime Text to finish installation')
  1. Enter in the browser to download the installation package of the package management tool, and find the directory named “Installed Packages” under the installation Sublime directory, Add the downloaded file to this file, and then restart Sublime Text to get it done.

-Install the plugin. Open the command panel through Package Control in the Preference menu or the shortcut key Ctrl+Shift+P, and enter Install Package in the panel to find the tool to install the plug-in, and then find the plug-in you need. We recommend that you install the following plugins:

-SublimeCodeIntel-Code auto completion tool plugin. -Emmet-Front-end development code template plugin. -Git-version control tool plugin. -Python PEP8 Autoformat-PEP8 standard automatic formatting plugin. -ConvertToUTF8-Convert local encoding to UTF-8.

Explanation: In fact, Visual Studio Code may be a better choice. It does not cost money and provides more complete and powerful functions. Interested readers can research on their own.

PyCharm-Python development artifact

The installation, configuration and use of PyCharm are introduced in Play with PyCharm, and interested readers can choose to read it.


  1. Enter the following code in the Python interactive environment and view the results. Please try to translate what you see into Chinese.

    import this

Explanation: Enter the above code, and you can see “Python of Zen” written by Tim Peter in the interactive environment of Python. The truth in it is not only Suitable for Python, but also for other programming languages.

  1. Learn to use turtle to draw graphics on the screen.

Explanation: Turtle is a very interesting module built in Python, especially suitable for small partners who have a first experience of computer programming. It was originally part of the Logo language. The Logo language was invented by Wally Feurzig and Seymour Papert in 1966. Programming language.

   import turtle




Reminder: The code provided in this chapter also includes the code for painting the flag and painting the piggy page. Interested readers please study by yourself.

Last modified October 6, 2020