Python入门技术树-操作系统如何运行程序个人总结
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |
操作系统如何运行程序
人和操作系统如何交互最早Ken Thompson设计Unix的时候使用的是命令解释器命令解释器接受用户的命令然后解释他们并执行。
Shell
是人和机器交互的界面分为GUI
和命令行(CLI)
两种方式这里重点介绍命令行的方式。概念Shell
路易斯·普赞Louis Pouzin在1964年至1965年间首次提出随后在MulticsMULTiplexed Information and Computing System项目中首次被实现出来。肯·汤普逊Ken Thompson以Multics上的shell为范本实现的第一个Unix壳层Unix shellThompson shell
。AT&T贝尔实验室的史蒂夫·伯恩在1977年实现了Bourne shell
或sh
它是Version 7 Unix默认的Unix shell替代执行文件同为sh的Thompson shell。微软的Windows操作系统也提供了命令行壳层的功能它们是Windows 95 / 98下的command.com
、Windows NT内核下的cmd.exe
以及PowerShell
。下面的
Python 列表
包含了Unix系统常见的shell
:
shells = [
'Bourne shellsh',
'Almquist shellash',
'Debian Almquist shelldash',
'Bourne-Again shellbash',
'Korn shellksh',
'Z shellzsh',
'C shellcsh',
'TENEX C shelltcsh',
'EMACS shell (eshell)',
'Es shelles',
'esh (Unix) – Easy Shell',
'friendly interactive shellfish',
'rc shellrc– shell for Plan 9 from Bell Labs and Unix',
'scshScheme Shell',
'Stand-alone Shellsash',
'BeanShellbshbsh.Interpreter , bsh.Console',
'Rhino JavaScript壳层org.mozilla.javascript.tools.shell.Main'
]
通过shell我们可以让操作系统执行Python程序。而Python是解释型语言源代码不是直接翻译成机器语言而是先翻译成中间代码再由解释器对中间代码进行解释运行。有两种执行Python程序的方式下面的
Python 字典
包含了这两种方式的信息
# -*- coding: UTF-8 -*-
run = {
"repl": {
"title": "交互式编程( Interactive )",
"desc": [
"打开终端输入 python 回车",
"进入 Python 交互式命令行",
"输入 print('monkey king is coding!')"
]
},
"source": {
"title": "Python 源代源文件( File )",
"desc": [
"使用你喜欢的编辑器拷贝本练习的代码, 保存为run.py",
"打开终端cd 到 run.py 保存的目录",
"输入 python run.py"
]
}
}
交互式命令行通常也称
REPL
表示Read-Eval-Print-Loop
顾名思义是读取
-评估
-打印
-循环
的意思。当你在shell
环境里输入python
并回车就进入了Python的REPL环境
程序总是
- 等待用户输入 Python 代码
- 当收到回车信号后就读取(
Read
)用户输入的 Python 代码- 解释读取到的 Python 代码(
Eval
)- 打印Python 代码的解释结果(
- 回到步骤1循环(
Loop
)该过程实际上我们在之前的习题里已经见过这样的程序经过重新认识后现在可以再次编写一个这样的
REPL
程序功能需求如下
- 提示用户选择想要了解的Python 的两种运行方式
- 用户选择后输出对应Python 运行方式的基本说明如果用户输入错误则提示后让用户重新选择
- 循环该过程直到用户输入
q
退出- 最后输出用户学习了几种运行方式
# -*- coding: UTF-8 -*-
if __name__ == '__main__':
run = {
"i": {
"title": "交互式编程( Interactive )",
"desc": [
"打开终端输入 python 回车",
"进入 Python 交互式命令行",
"输入 print('monkey king is coding!')"
]
},
"f": {
"title": "Python 源代源文件( File )",
"desc": [
"使用你喜欢的编辑器拷贝本练习的代码, 保存为run.py",
"打开终端cd 到 run.py 保存的目录",
"输入 python run.py"
]
}
}
print("有两种基本的方式运行 Python")
for s in run:
item = run.get(s)
print("* {}: {}".format(s, item['title']))
has_learn_i = False
has_learn_f = False
# TODO(You): 请在此实现代码
if has_learn_i and has_learn_f:
print("[2/2]您已完成两种 Python 运行方式的学习")
elif has_learn_f:
print("[1/2]您已完成 Python 源代码方式运行学习")
elif has_learn_i:
print("[1/2]您已完成 Python 交互式命令行运行学习")
else:
print("[0/2]您似乎跳过了运行方式的学习期待下次光临")
程序的示例操作过程如下
有两种基本的方式运行 Python
* repl: 交互式编程( Interactive )
* source: Python 源代源文件( File )
请选择你想了解的 Python 运行方式(输入r/s选择输入 q 退出)r
0. 打开终端输入 python 回车
1. 进入 Python 交互式命令行
2. 输入 print('monkey king is coding!')
请选择你想了解的 Python 运行方式(输入r/s选择输入 q 退出)s
0. 使用你喜欢的编辑器拷贝本练习的代码, 保存为run.py
1. 打开终端cd 到 run.py 保存的目录
2. 输入 python run.py
请选择你想了解的 Python 运行方式(输入r/s选择输入 q 退出)q
[2/2]您已完成两种 Python 运行方式的学习
以下选项是对代码中TODO
部分的多种实现你能找出实现错误的选项吗
while True:
ret = input("请选择你想了解的 Python 运行方式(输入r/s选择输入 q 退出)")
if ret == 'r' or ret == 's':
has_learn_repl = ret=='r'
has_learn_source = ret=='s'
desc = run.get(shoutcut_keys.get(ret))['desc']
for i in range(0, len(desc)):
print("{}. {}".format(i, desc[i]))
elif ret != 'q':
print("[错误] 不支持的运行方式")
break
else:
pass