美文网首页大数据 爬虫Python AI SqlPython小哥哥
Python基础教程:教你如何在交互式环境中执行Python程序

Python基础教程:教你如何在交互式环境中执行Python程序

作者: 14e61d025165 | 来源:发表于2019-06-06 15:55 被阅读0次

    相信接触过Python的伙伴们都知道运行Python脚本程序的方式有多种,目前主要的方式有:交互式环境运行、命令行窗口运行、开发工具上运行等,其中在不同的操作平台上还互不相同。今天,小编讲些Python基础的内容,以Windows下交互式环境为依托,演示Python程序的运行。

    一般来说,顺利安装Python之后,有两种方式可以进入Python交互性环境。一种是在Python自带的IDLE中直接打开交互式窗口,如下图所示:

    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1559807688038" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

    <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

    ** Python学习交流群:1004391443,这里有资源共享,技术解答,还有小编从最基础的Python资料到项目实战的学习资料都有整理,希望能帮助你更了解python,学习python**

    另一种是打开开始菜单,输入cmd之后,进入命令行窗口,之后输入Python,回车之后便可以进入到交互式环境中去,如下图所示:

    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1559807688043" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

    <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

    在>>>提示符后边输入编程语句,然后回车进行执行,就会得到运行的结果。简单来看,这个交互式环境有点类似计算器,一次执行一条语句,而且还可以保存结果。下图是部分操作实例:

    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1559807688046" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

    <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

    需要注意的是特殊变量“_”,它在Python交互式环境中的意思是前面运算的结果,一般在程序中使用其构造成新的表达式,在此基础上做进一步计算。在交互式环境中,我们可以直接使用常量,也可以定义变量,但是必须要事先先定义好变量,不然的就会出现命名错误,如例子中的最后一行代码中的a没有定义,所以报错了,但是length和width都没有报错,因为他有定义。

    在Python交互式环境中,如果存在语法错误,程序在运行之后会报错,而且会得到错误信息,可以帮助我们很轻松的找到报错的行数和报错内容,只要我们对症下药就可以药到病除啦,bug分分钟解决掉~~

    在交互式环境中,输入的代码不会被保存下来,当关闭Python得到运行窗口之后,之前输入的代码将不会被保存。但是在交互式环境中按下键盘中的上下键,可以寻找历史命令,但是它是短暂性的记忆,当退出程序之后,这些命令将不复存在。

    如果你需要编写较长程序的时候,使用命令行就不太方便了。此时你可以利用文本编辑器进行编辑脚本。在IDLE中点击左上方的file,然后选择New window菜单,就可以新建一个窗口了,如下图所示:

    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1559807688049" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

    <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

    在这里,你可以编写你的代码,但请一定要注意缩进,否则会经常出现indentation(缩进)错误,语句的缩进在Python中相当重要,因为不同的缩进反映了语句块的不同层次之间的关系。编写好的文件需要进行保存(Ctrl+s),之后以.py的拓展名进行保存。保存好之后,按下快捷键F5就可以运行程序了,之后在IDLE的窗口中便可以看到运行的结果。当然你也可以右键,然后点击run,就可以运行程序了。

    后期也会接着跟大家分享 Python学习教程 更多的文章和总结,大家一定要坚持学习,坚持做习题!

    相关文章

      网友评论

        本文标题:Python基础教程:教你如何在交互式环境中执行Python程序

        本文链接:https://www.haomeiwen.com/subject/hhduxctx.html