python There are two modes of operation : Interactive and scripted . Interactive can be through cmd Command line window or IDEL Realization , And scripted by writing a script (.py Closing document ) Realization . among Interactive is mainly used for simple python Run or test debugging python When used , Scripting is running python The main method of program .
1 Execute... In interactive mode Python
In this mode, there is no need to create script files , Directly in Python Write the corresponding... In the interpreter's interaction mode Python Sentence can be used .
1.1 Windows Command line tools
adopt Windows Command line tools for interactive running python. Find... On the start menu “ Command prompt ”, open , Go to command line mode , Or press “win +R” key , start-up “ function ”, stay “ function ” Input in cmd And then go back , The command line tool pops up , Then input python Enter to start Python Interpreter , The following interface appears .
Then type... At the command prompt print('Hello world!'), Then press enter to output “Hello world!”.
Interactive mode : Write a line of code , And python Interact once ,python Do it once .1.2 IDEL Interactive operation python
adopt IDEL Interactive operation python. from “ Start ” Find Python->IDEL, Here's the picture .
start-up IDEL after , The same input print('Hello world!'), Then press enter to output “Hello world!”.
Also type a line of code , And python Interact once ,python Do it once . It's just IEDL in python The code can highlight .2 Scripted execution Python
2.1 Write a script file with a text editor
Through text editor , Write a script file , Name it test.py, as follows :
Then in command line mode cd To test.py File path , Then input python test.py You can enter. , as follows :
Note the path of the script file , If the current working path and script file are not in the same path , To enter The path of the script file , Or give the full path of the script file .2.2 IDEL Write script files in
stay IDEL Click on the file->new file, An unnamed... Will pop up (Untitled) Script window , Then enter the following code , And press Ctrl+S Save as test.py .
num = int(input(' Please enter an integer : '))
# Output 1 - num( contain ) All primes in
for i in range(2,num+1):
fg=0
for j in range(2,i-1):
if i%j ==0:
fg=1
break
if fg==0:
print(i)
Press F5 Run saved test.py Script , The following interface will appear
3 Interactive and scripted comparison
- Run in interactive mode python sentence , It will automatically print out the calculation results ; The way of script file will not .
- Run in interactive mode python sentence , Every input statement will not be saved , After exiting the interactive environment, it will disappear , But you can save all the statements through the script file .