一、命令行模式
Windows 的Power Shell (Dos) 命令提示符与 Linux/Mac 的终端是用户与系统交互的命令行模式。Windows命令提示符类似 "C:\Users\Kevin>"
Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。
PS C:\Users\Kevin>
打开Linux/Mac终端,提示符类似 “KevindeMacBook-Pro:~ python$”
Last login: Mon Dec 3 11:40:15 on ttys000
KevindeMacBook-Pro:~ python$
注意:在执行.py 文件时必须在命令行模式下运行。
KevindeMacBook-Pro:~ python$ python hello.py
Hello world!
KevindeMacBook-Pro:~ python$
当前目录下要有hello.py文件,内容只有一行:print("Hello world!"),否则会显示:
KevindeMacBook-Pro:~ python$ python hello.py
(null): can't open file 'hello.py': [Errno 2] No such file or directory
KevindeMacBook-Pro:~ python$
二、Python交互模式
在命令行模式输入:python 命令
KevindeMacBook-Pro:~ python$ python
Python 3.7.0 (default, Jun 28 2018, 07:39:16)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
这时进入到Python交互模式,它的提示符是 >>> ,输入exit()将退出Python交互模式:
KevindeMacBook-Pro:~ python$ python
Python 3.7.0 (default, Jun 28 2018, 07:39:16)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
KevindeMacBook-Pro:~ python$
输入python回车前按“Tab”键,可以获取系统可能存在的python版本(python2.7, python3.6, python3.7):
KevindeMacBook-Pro:~ python$ python
python python3 python3.6m-config pythonw
python-config python3-config python3.7 pythonw2.7
python.app python3.6 python3.7-config
python2.7 python3.6-config python3.7m
python2.7-config python3.6m python3.7m-config
在交互模式提示符>>> 下,输入代码,直接回车,将立刻得到运行结果。输入:666 + 888 回车
>>> 666 + 888
1554
>>>
输入:print("Hello world!") 回车
>>> print("Hello world!")
Hello world!
>>>
输入:"Hello world!" 回车
>>> "Hello world!"
'Hello world!'
>>>
用双引号 "" 或 单引号 '' 括起来的,是字符串,Python 交互模式会直接显示字符串或数字,输入 666 回车
>>> 666
666
>>>
试试输入 hello 回车
>>> hello
Traceback (most recent call last):
File "", line 1, in
NameError: name 'hello' is not defined
>>>
因为hello不是正确的Python命令,之前也没有定义过 hello 变量,因此系统返回一个错误提示。
0 讨论
没有讨论.
联系人
邮箱
站点
讨论