Python Learning 1.2 - Python development environment configuration

Python language Windows system development environment settings

Enter the official website and find the corresponding download page

We need to use a Python interpreter to run Python programs, so our first step is to download the Python interpreter.

The link below is the official download address of the Python interpreter adapted to the Windows version
https://www.python.org/downloads/windows/
The link below is the official download address of the Python interpreter adapted to the Mac OS version
https://www .python.org/downloads/macos/


Choose the appropriate Python interpreter version and download it

The following are all preceded byWindowsTake the version as an example (the same applies to Mac OS):
Users can click on the two links enclosed by the orange boxes in the picture below according to their own circumstances, and select the Python 3 or Python 2 interpreter to download.

Python interpreter download icon

If you have any incompatibility issues, you can continue to scroll down to find and choose the version that suits you.

Installation of Python interpreter

After downloading, you can open the installation program. Because I have already installed it, there may be some differences in the specific process. The basic one is generally the default.

Insert image description here
Remember that when you first clicked on it, there was an Add Python to PATH (added to the environment variable) below. If you forget to click on it, you need to add it manually.

Insert image description here
The installation settings are almost the same as the two pictures above; I am just used to putting things into the D drive. In fact, you can also put things into the C drive, but you may encounter some permission problems later, such as when downloading the library package. No permission to read and write to C drive...etc.

Manually add environment variables

Right click on this PC --> Properties (R)
Insert image description here

Scroll down to findAdvanced system settings, click to open
Insert image description here

turn upenvironment variables, click to open
Insert image description here

Click firstParh variable in system variables, then clickEdit (I)
Insert image description here

After opening clickNew
Insert image description here

Then copy and paste the directory address where Python is installed, as shown in the figure below: I am not particularly sure about three of them, because I have not been able to use them before.
Insert image description here

Finally, there is a judgment method:win+r, then entercmd, and then click OK.
Insert image description here

enterpython, and press Enter. If something like the picture below appears with a version number, it means that the Python interpreter has been successfully installed and can be used; if not, first check whether the environment variables are configured.
Insert image description here

Writing and running Python programs

The interactive programming method used below uses the IDLE editor in Python,
while the file-based programming method uses the interactive module of vscode to run.
Note:I only interacted after writing all the code, but I was too lazy to save it as a file, which would not affect the demonstration results.

Two programming methods in Python

Interactive and document-based

  • Interactive
    runs the results in real time for each input statement, suitable for grammar exercises
  • File-
    based execution of a set of statements in batches and the results, the main way of programming

Among them, the file format is programmed in PythonMain way

Example 1: Calculation of the area of ​​a circle

To calculate the area of ​​a circle based on the radius r, the required code is as follows

r = 25
area = 3.1415926 * r * r
print(area) # 输出结果为 1963.4937
print("{:.2f}".format(area)) # 输出结果为 1963.49
  • Run the example interactively:

    Interactive running results

  • File running example:

    File execution results

Some careful people here may find that the running results are different. This is due to accuracy issues, but generally the running results are the same, and this subtle difference can be ignored.

Example 2: Drawing of contangent circles

To draw multiple contangent circles, the required code is as follows

import turtle
turtle.pensize(2)
turtle.circle(10)
turtle.circle(40)
turtle.circle(80)
turtle.circle(160)
  • Interactive:
    Each time a statement is entered in interactive mode, it will run once and you can draw contangent circles step by step.

    Interactive running results

  • File format:
    In the file format, you need to write statements once, and then run the program for drawing contangent circles at once.

    File execution results

Example 3: Drawing a five-pointed star

To draw a five-pointed star, the code required is as follows

import turtle
turtle.pensize(2)
turtle.circle(10)
turtle.circle(40)
turtle.circle(80)
turtle.circle(160)
  • Interactive:
    In interactive mode, it will be run every time a statement is entered, and the five-pointed star can be drawn step by step.

    Interactive running results

  • File format:
    In the file format, you need to write the statement once, and then run the program for drawing the five-pointed star at once.

    File execution results
    At the end of the article, I still thank you all for your patience in reading this blog. The writing may not be that good, but I am very happy to be able to share some of the things I have learned. Thank you again for your patience in reading this blog.

Guess you like

Origin blog.csdn.net/m0_68192925/article/details/125733714