How to run the program after python is written, how to run the python program after writing

This article will tell you how to run the program written in python, and how to run it after writing the program in python. I hope it will be helpful to you. Don’t forget to bookmark this site.

Python is an interpreted scripting programming language. Such a programming language generally supports two code running modes:

1) Interactive programming

Enter the code directly in the command line window, press the Enter key to run the code, and immediately see the output result; after executing a line of code, you can continue to enter the next line of code, press Enter again and view the result... the whole process It is as if we are talking to the computer, so it is called interactive programming.

2) Write source files

Create a source file, put all the code in the source file, and let the interpreter read and execute the code in the source file line by line until the end of the file, which is to execute the code in batches . This is the most common way of programming, and it is what we should focus on learning.

In this section, we will introduce the above two programming methods in detail.

Python interactive programming

Generally, there are two ways to enter the Python interactive programming environment. The first method is to enter pythonthe command in the command line tool or the terminal (Terminal) window, and >>>then you can start to enter the code when you see the prompt, as shown below:
 


Figure 1 Use pythoncommands to enter the interactive programming environment


The second way to enter the Python interactive programming environment is to open the IDLE tool that comes with Python, and it will enter the interactive programming environment by default, as shown below:
 


Figure 2 Open the IDLE tool and enter the interactive programming environment


IDLE supports code highlighting, which looks more refreshing, so it is recommended to use IDLE programming.

In fact, you can enter any complex expression (including mathematical calculations, logical operations, loop statements, function calls, etc.) in the interactive programming environment, and Python can always help you get the correct result. This is one of the reasons why many non-professional programmers like Python: even if you are not a programmer, as long as you enter the operation you want to perform, Python can tell you the correct answer.

From this point of view, Python's interactive programming environment is equivalent to an extremely powerful "calculator", which is much more powerful than the calculators that come with Windows and Mac OS X systems.

Write Python source files

Interactive programming is just for fun, real project development still needs to write source files.

A Python source file is a plain text file without any special formatting inside, you can open it with any text editor, for example:

  • Notepad program under Windows;
  • Vim, gedit, etc. under Linux;
  • TextEdit tool under Mac OS;
  • Cross-platform Notepad++, EditPlus, UltraEdit, etc.;
  • More professional and modern VS Code and Sublime Text (also supports multiple platforms).


Note that typesetting tools such as WordPad, Word, and WPS cannot be used to write Python source files, because typesetting tools generally have built-in special formats or special characters, which will make the code "messy" and cannot be recognized by the Python interpreter.

source file suffix

Python source files have a suffix of .py. Source files in any programming language have a specific suffix, for example:

  • The suffix of the C language source file is .c;
  • The suffix of the C++ source file is .cpp;
  • The suffix of the JavaScript source file is .js;

  • The suffix of the C# source file is .cs;

  • Java source files have the suffix .java.

The suffix is ​​only used to distinguish different programming languages, and will not cause the internal format of the source file to change, and the source file is still plain text. Compilers (interpreters), editors, and users (programmers) all rely on suffixes to distinguish which programming language the current source file belongs to.
The encoding format of the source file
Python source file is a plain text file, which will involve the issue of encoding format, that is, which encoding is used to store the source code.

Python 3.x already uses UTF-8 as the default source file encoding format, so it is recommended that you use professional text editors, such as Sublime Text, VS Code, Vim, Notepad++, etc., which support UTF-8 encoding by default.

UTF-8 is cross-platform and internationalized, and it is the general trend to use UTF-8 in programming languages.

If you don't know the encoding format, please click the link below to learn:
ASCII encoding, store English to the computer
GB2312 encoding and GBK encoding, store Chinese to the computer
Unicode character set, store the world's text to the computer
Run the source file
Use an editor (I'm used to using Sublime Text) to create a source file, name it demo.py, and enter the following code:

print("Python教程")
a = 100
b = 4
print(a*b)

After the input is complete, pay attention to save.

There are two ways to run Python source files:

1) Use the IDLE tool that comes with Python to run source files.

Open the demo.py source file through file -> openthe menu, and then select it in the menu bar in the source file Run->Run Module, or press the F5 shortcut key to execute the code in the source file.

2) Run the source file in the command line tool or terminal (Terminal).

Enter the command line tool or terminal (Terminal), switch to the directory where demo.txt is located, and then enter the following command to run the source file:

python demo.py

After running the command, you can see the output immediately.

Here is a brief introduction to the python command. Its syntax is very simple, and its basic format is as follows:

python <源文件路径>

The source file path here can be an absolute path starting from the drive letter (C drive, D drive), for example; you can D:\PythonDemo\demo.pyalso enter the directory where the source file is located before executing the python command, and then only write the file name, that is, use relative path.

It should be noted that the Windows system is not case-sensitive, so you don’t need to pay attention to case when entering the source file path on the Windows platform. However, Unix-like systems (Mac OS X, Linux, etc.) are case-sensitive, and you must pay attention to case-sensitive issues when entering the Python source file path on these platforms.

Guess you like

Origin blog.csdn.net/chatgpt001/article/details/132712081