Chapter One: Start the Python Journey

Learning focus

  1. Introduction to Python.
  2. Ability to independently build a Python development environment.
  3. Understand the writing of Python programs.
  4. Familiar with Python development tools.

1. Know Python

Python, the original meaning means 蟒蛇. In 1989, the Dutchman Guido van Rossum (Guido-Uncle Turtle) invented an object-oriented interpreted high-level programming language, named it Python, the logo is shown in the figure.
Insert picture description here
Python's design philosophy is 优雅、明确、简单, in fact, Python and always carry out this idea, so that now circulating on the Web 人生苦短,我用 Pythonstatement, showing that Python has a simple syntax, development of fast, time-saving and easy to learn and so on.

Python is an extensible programming language. It has a rich and powerful library, able to make use of other languages of the various modules (especially C / C ++ language) easily linked together, so often referred to Python 胶水language.

In 1991, the first public release of Python came out. Since 2004, the usage rate of Python has been increasing linearly, and it has gradually been welcomed and loved by programmers. In 2010, Python won the TIOBE 2010 Language Laureate; from 2017 to 2020 , in the annual programming language rankings released by IEEE Spectrum, Python has won four consecutive hegemonies. The top 10 of the 2020 annual programming language rankings released by IEEE Spectrum are shown in the figure.

1.1 Python version

Since the release of Python, there have been three main versions: Python 1.0 version released in 1994 (obsolete), Python 2.0 version released in 2000 (updated to 2.7.18 in July 2020) and version 3.0 released in 2008 ( It has been updated to version 3.8.5 in July 2020).

1.2 Application areas of Python

As a powerful programming language, Python is favored by many developers because of its simplicity and ease of learning. So what are the application domains of Python? In summary, there are mainly the following aspects:

  1. Application development: have standard capabilities such as script writing and software development.
  2. AI artificial intelligence: machine learning, neural networks, deep learning and other aspects are widely supported and applied. https://www.bilibili.com/video/BV1JE411g7XF?p=39
  3. Data analysis: the cornerstone of the big data industry.
  4. Automated operation and maintenance development: the programming language preferred by operation and maintenance engineers.
  5. Cloud computing: OpenStack (a cloud computing management platform cooperating with Rackspce) has a successful case.
  6. Web crawler: the core tool for data acquisition in the big data industry. Python Daily Practice (20)-Use Python to make a mini translator
  7. Web development: complete framework support, fast development speed. For example, we often visit the innovative community Douban.com,
    Insert picture description here
    the largest online cloud storage website in the United States , which integrates movies, reading, and music . Projects such as OpenStack, a cloud computing management platform cooperating with NASA (National Aeronautics and Space Administration) and Rackspace, etc. It is implemented using Python.
    Insert picture description here
  8. Game development: simple, efficient, and less code. Turtle (turtle) drawing the ultimate actual combat: small turtles challenge the big maze game

2. Set up a Python development environment

So-called 工欲善其事,必先利其器.. Before officially learning Python development, you need to set up a Python development environment. Python is a cross-platform development tool that can be programmed on multiple operating systems, and the written program can also be run on different operating systems. Common operating systems and descriptions are shown in the table.

operating system Description
Windows Windows 7 version or above
is acceptable (Windows 10 is recommended) Note: Python 3.5 and above cannot be used on Windows XP system
Mac OS Starting from Mac OS X 10.3 (Panther), Python 2.x has been included
Linux Recommend the Ubuntu version (the graphical interface is better with Python2.x)
Note: In the latest Ubuntu system, the built-in Python 2.x version is cancelled, and only the latest Python 3 version is retained, so when you enter the Python3 command , You can enter the Python interactive environment.

Note: It is recommended to use the Windows 10 operating system in the personal development and learning stage, and our follow-up learning will be based on the Windows operating system. If you use Mac OS or Linux operating system, you can refer to the online installation steps for the specific development environment construction steps.

2.1 Download and install Python

To start Python development, you need to install a Python interpreter first. Since Python is an interpreted programming language, an interpreter is needed to run the written code. The installation of Python mentioned here is actually the installation of the Python interpreter. The following uses the Windows 10 operating system as an example to introduce the method of installing Python.

2.1.1 How to check the number of bits of the computer operating system

At present, many software, especially programming tools, have optimized 32-bit operating systems and 64-bit operating systems to improve development efficiency and have launched different development kits. Python is no exception, so before installing Python, you need to know the number of bits of your computer's operating system. Check the number of bits of the operating system under Windows 10 operating system. Find the desktop 此电脑icon or use the shortcut keys Windows + Efound on this computer icon in the open interface, right-click the icon in the menu that opens select 属性, as shown.
Insert picture description here
After selecting the properties, the computer system window as shown in the figure will pop up. The 64-bit operating system or 32-bit operating system will be displayed on the right side of the system type label, and this information is the number of bits of the operating system. The current computer operating system has 64 bits.
Insert picture description here
2.1.2 Download Python installation package

On the official Python website, you can easily download the Python development environment. The specific download steps are as follows:

(1) Open the browser (such as Google Chrome browser), enter the official Python website, the address is https://www.python.org/as shown in the figure.
Insert picture description here
Note: If the download button on the right is not displayed when you select the Windows menu item, it should be that the page has not been loaded, and it will be displayed after the loading is complete. Please wait patiently. For stability, it is recommended not to download directly, use Python 3.7.8 version for learning

(2) Click the Windows menu item to enter the detailed download list. Since the latest version of the Python interpreter is not stable, in the opened page, scroll down and select the latest version of Python 3.7 to learn and use. Because the author's computer is a 64-bit Windows operating system, so directly click the Windows menu item to enter the download list as shown in the figure.
Insert picture description here
In the list shown in the figure, with x86the installation package words, it indicates that the tools can be used on Windows 32 bit operating system; with the x86-64installation package words, it indicates that the tools may be Windows 64 bit operating system Used on. In addition, the marked web-based installerword installation package, expressed the need to complete the installation by networking; marked executable installerwords installation package, represented by the executable file *.exefor offline installation; marked embeddable zip filewords installation package, represents an embedded version can be integrated into other applications in.

(3) On the Python download list page, the download links of each version provided by Python are listed, and readers can download them as needed. For example, now you want to download Python 3.7.8 version, can be found in the position shown, click the Windows x86-64 execxutable installerhyperlink, download the Windows 64-bit operating systems offline installation package.
Insert picture description here
(4) After the download is complete, the browser will automatically pop up a prompt 此类型的文件可能会损害您的计算机。您仍然要保留 python-3.7.8-amd64.exe吗?. At this time, click the retain button to retain the file.

(5) After the reservation file, a name for the resulting python-3.7.8-amd64.exeinstallation file.

2.1.3 Install Python in Windows 64-bit operating system

The steps to install Python 3.7.8 on a Windows 64-bit operating system are as follows:

(1) Double-click on the downloaded installation file python-3.7.8-amd64.exe, displays the Setup Wizard dialog box, select Add Python3.7 to PATHthe check box indicates that auto-configure the environment variables, as shown in FIG.
Insert picture description here
Note: Be sure to check the Add Python 3.7 to PATH check box, otherwise the error that XXX is not an internal or external command will appear in the later learning, which will affect the learning efficiency.

(2) Click Customize installationbutton, a custom installation (custom installation can modify the installation path), the pop-up dialog box installation options default settings, as shown in FIG.
Insert picture description here
(3) Click the Next button to open the advanced options dialog box. In this dialog box, the installation path is automatically set to C:\Program Files\Python37, and the other default settings can be used, as shown in the figure.
Insert picture description here
(4) Click the Install button to start installing Python, as shown in the figure.
Insert picture description here
(5) After the installation is complete, a dialog box as shown in the figure will be displayed.
Insert picture description here

2.2 Test whether the installation is successful

After the Python installation is complete, you need to check whether Python is successfully installed. For example, the Windows 10 operating system detects Python is successfully installed, can be on the right side of the Start menu, 在这里输入你要搜索的内容start a command line window, type cmd in the text box or direct Windows + Rshortcut to bring up a small window, enter cmd in the window.
Insert picture description here
After the input current command prompt python, press the <Enter>key, if the message shown appears, the Python installation is successful, and the system will enter the interactive Python interpreter.
Insert picture description here

3. The first Python program

After installing Python, an IDLE will be installed automatically. It is a Python Shell (you can see it on the title bar of the opened IDLE window), and program developers can use the Python Shell to interact with Python. The following will introduce how to use IDLE to develop the first Python program.

3.1 Writing Hello World in IDLE

Click the start menu of the Windows 10 system, and then select all programs in turn ==> Python 3.7 ⇒ IDLE (Python 3.7 64-bit) menu item.

You can open the IDLE window, as shown in the figure.
Insert picture description here
In IDLE, you can output simple statements, but in actual development, you can usually not only include one line of code. When you need to write multiple lines of code, you can create a separate file to save these codes and execute them together after all of them are written. The specific method is as follows:

(1) On the menu bar of the IDLE main window, select the File ⇒ New File menu item, a new window will open, in which you can directly write Python code. In the line of code then press the Enter <Enter>button, automatically wraps to the next line, continue to wait for input, as shown in FIG.
Insert picture description here
(2) In the code editing area, write multiple lines of code. For example, to output the Chinese and English version 人生苦短,我用 Python, the code is as follows:

print("人生苦短,我用Python。")
print("Life is short,you need Python。")

Editing the code shown in the document window in FIG Python:
Insert picture description here
(3) press the shortcut key to <Ctrl+S>save the file, where the file name to demo.py. Among them, .pyis the extension of the Python file.

3.2 Run Python program

In IDLE, the Python already written to run the program, you can select Run / Run Module menu item in the menu bar (or press the <F5>shortcut key) to achieve. For example, Section 3.1 prepared to run Python, you can select Run → Run Module menu item in the menu bar (may be directly press <F5>the shortcut key), result of the program shown in FIG.
Insert picture description here
After running the program, the Python Shell window will open to display the running results, as shown in the figure:
Insert picture description here
In the Python Shell window of IDLE, enter a Python statement and press the Enter key, and the syntax error shown in the figure is displayed:
Insert picture description herethe problem is Because an invalid character was entered. There is no error in the Python statement here, only a full-width space is accidentally output at the end of the statement, that is, the character marked in red as shown in the figure above. Delete it to solve the problem. Note: When entering code in Python, unless there are full-width spaces in the string, you must not use full-width spaces. Since this error is relatively hidden and not easy to find, everyone needs to develop good coding habits.

4. Use a third-party development tool Pycharm

PyCharm is a Python integrated development environment developed by Jetbrains. Thanks to its smart code editor, it can realize functions such as automatic code formatting, code completion, smart prompts, refactoring, unit testing, automatic import, and one-click code navigation. It has become a powerful tool for Python professional developers and beginners. This chapter will explain in detail how to use the PyCharm tool. Pycharm download, installation, and usage details

5. After class exercises

  1. Install Python 3.7.8 version on your computer
  2. Write identity information for yourself in IDLE Python, including name, age, ID number, contact information, etc., and then display them on the screen.

Guess you like

Origin blog.csdn.net/xw1680/article/details/114261028