Python学习笔记(2)——第一个Python程序

328 阅读2分钟

命令行模式

在Windows开始菜单选择“命令提示符”,就进入到命令行模式(或者按下windows+R键,输入“cmd”),它的提示符为C:\>

┌────────────────────────────────────────────────────────┐
│Command Prompt                                    - □ x │
├────────────────────────────────────────────────────────┤
│Microsoft Windows [Version 10.0.0]                      │
│(c) 2015 Microsoft Corporation. All rights reserved.    │
│                                                        │
│C:\> _                                                  │
│                                                        │
│                                                        │
│                                                        │
│                                                        │
│                                                        │
│                                                        │
│                                                        │
└────────────────────────────────────────────────────────┘

Python交互模式

在命令行模式输入python,就看到类似如下的一堆文本输出,然后就进入到Python交互模式,它的提示符是>>>

┌────────────────────────────────────────────────────────┐
│Command Prompt - python                           - □ x │
├────────────────────────────────────────────────────────┤
│Microsoft Windows [Version 10.0.0]                      │
│(c) 2015 Microsoft Corporation. All rights reserved.    │
│                                                        │
│C:\> python                                             │
│Python 3.7 ... on win32                                 │
│  ... for more information.                  │
│>>> _                                                   │
│                                                        │
│                                                        │
│                                                        │
│                                                        │
└────────────────────────────────────────────────────────┘

在Python交互模式下输入exit()并回车,就退出了Python交互模式,并回到命令行模式:

┌────────────────────────────────────────────────────────┐
│Command Prompt                                    - □ x │
├────────────────────────────────────────────────────────┤
│Microsoft Windows [Version 10.0.0]                      │
│(c) 2015 Microsoft Corporation. All rights reserved.    │
│                                                        │
│C:\> python                                             │
│Python 3.7 ... on win32                                 │
│Type "help", ... for more information.                  │
│>>> exit()                                              │
│                                                        │
│C:\> _                                                  │
│                                                        │
│                                                        │
└────────────────────────────────────────────────────────┘

命令行模式和Python交互模式的区别

注意区分命令行模式和Python交互模式。

在命令行模式下,可以执行python进入Python交互式环境,也可以执行python hello.py运行一个.py文件。 如果敲一个命令python hello.py,看到如下错误:

┌────────────────────────────────────────────────────────┐
│Command Prompt                                    _ □ x │
├────────────────────────────────────────────────────────┤
│Microsoft Windows [Version 10.0.0]                      │
│(c) 2015 Microsoft Corporation. All rights reserved.    │
│                                                        │
│C:\> python hello.py                                    │
│python: can t open file hello.py: [Errno 2] No such     │
│file or directory                                       │
│                                                        │
│                                                        │
│                                                        │
│                                                        │
│                                                        │
└────────────────────────────────────────────────────────┘

错误提示No such file or directory说明这个hello.py在当前目录找不到,必须先把当前目录切换到hello.py所在的目录下,才能正常执行:

┌────────────────────────────────────────────────────────┐
│Command Prompt                                    _ □ x │
├────────────────────────────────────────────────────────┤
│Microsoft Windows [Version 10.0.0]                      │
│(c) 2015 Microsoft Corporation. All rights reserved.    │
│                                                        │
│C:\> cd work                                            │
│                                                        │
│C:\work> python hello.py                                │
│Hello, world!                                           │
│                                                        │
│                                                        │
│                                                        │
│                                                        │
└────────────────────────────────────────────────────────┘

此外,在命令行模式运行.py文件和在Python交互模式直接运行Python代码有所不同。Python交互模式会把每一行Python代码的结果自动打印出来,但是,直接运行Python代码却不会。

比如,在Python交互模式下,输入:

>>> 100 + 200 + 300
600

然后在命令行模式下执行:

C:\work>python calc.py

则不会得到输出

必须使用print()打印出来。改写cal.py

print(100 + 200 + 300)

再执行python calc.py,就可以看到结果:

C:\work>python calc.py
600

Python交互模式的代码是输入一行,执行一行,而命令行模式下直接运行.py文件是一次性执行该文件内的所有代码。可见,Python交互模式主要是为了调试Python代码用的,也便于初学者学习,并不是正式运行Python代码的环境。