1. Execute Python in interactive mode. In this mode, you don't need to create a script file, you can just write the corresponding Python statements in the interactive mode of the Python interpreter.
1) The way to turn on the interactive mode:
Windows:
Find "Command Prompt" in the start menu and open it to enter the command line mode:
You can enter Python's interactive mode by typing: python in command-line mode.
Linux:
Type python directly into the terminal, or if you have python3 installed, enter the corresponding version of the Python interactive environment according to the name of the softwire you created, for example, if I created a softwire using python3, type python3.
2) To exit the interactive mode, just type exit().
Windows:
Linux:
3) Output in interactive mode: Hello World!
Windows:
Linux:
2. Output via script
Using a text editor, write a script file named , and type python in command line mode.
Windows:
Linux:
[Vicky@localhost code]$ touch [Vicky@localhost code]$ vi [Vicky@localhost code]$ python3 Hello World!
In this way, pay attention to the path of the script file. If the current working path and the script file are not in the same path, you have to enter the path of the script file, or give the full path of the script file.
1) Go to the path where the script file is located and execute it
C:\Windows\System32>G: G:\test>python Hello World!
2) Give the full path to the script file
C:\Windows\System32>python G:\test\ Hello World!
3. Specify the path to the python program in the script file, change the file to an executable, and then run the file directly.
Linux:
1) Modify the file by adding #! /usr/bin/python3
[Vicky@localhost code]$ vi [Vicky@localhost code]$ cat #!/usr/bin/python3 print("Hello World!")
2) Modify file permissions and add executable permissions
[Vicky@localhost code]$ chmod u+x [Vicky@localhost code]$ ls -la -rwxrw-r--. 1 Vicky Vicky 41 10moon 19 15:40
3) Running
[Vicky@localhost code]$ ./ Hello World!
When executing in this way, be sure to specify the interpreter in the script file, otherwise the script file cannot be run directly
[Vicky@localhost code]$ cat print("Hello World!") [Vicky@localhost code]$ ls -la -rwxrw-r--. 1 Vicky Vicky 22 10moon 19 15:40 [Vicky@localhost code]$ ./ ./:classifier for objects in rows such as words1: Unexpected symbols `"Hello World!"' There are grammatical errors in the neighborhood ./:classifier for objects in rows such as words1: `print("Hello World!")'
4. Comparison of the interactive mode and the script file approach
1) In interactive mode, the result of the operation will be printed out automatically, whereas by means of a script file it won't be
Interaction Patterns:
[fanya@localhost code]$ python3 Python 3.6.5 (default, Oct 19 2018, 10:46:59) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> 100+200 300 >>> exit()
Script file:
[fanya@localhost code]$ vi [fanya@localhost code]$ cat 100+200 [fanya@localhost code]$ python3 [fanya@localhost code]$
There is no output, so to output, you must use the print function to print.
[fanya@localhost code]$ vi [fanya@localhost code]$ cat print(100+200) [fanya@localhost code]$ python3 300 [fanya@localhost code]$
(2) In interactive mode, every statement you type is not saved and disappears when you exit the interactive environment, but you can save all the statements you've written in a script file. So it's common to write Python code by writing script files.
Note: Don't use word and windows notebooks when writing script files as they save in utf-8 BOM format which can cause script execution errors. You can use sublime, editplus, notepad++, etc.
The above summary of the three ways of executing Python scripts is all that I have shared with you, and I hope it will give you a reference, and I hope you will support me more.