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

命令行模式

在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代码的环境。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值