交互模式和非交互模式】_交互模式下与脚本模式下的编码

news/2024/7/19 15:59:29 标签: python, java, linux, 编程语言, js

交互模式和非交互模式】

When programming in Python, you have two basic options for running code: interactive mode and script mode. Distinguishing between these modes can be slightly confusing for beginners, especially when you’re trying to follow along with others’ tutorials, so here’s a brief rundown.

使用Python进行编程时,有两个用于运行代码的基本选项:交互模式和脚本模式。 对于初学者来说,区分这些模式可能会有些混乱,尤其是当您尝试与他人的教程一起学习时,因此这里简要介绍一下。



互动模式 (Interactive Mode)

Interactive mode is great for quickly and conveniently running single lines or blocks of code. Here’s an example using the python shell that comes with a basic python installation. The “>>>” indicates that the shell is ready to accept interactive commands. So for example if you want to print the statement “this is interactive mode”, simply type the appropriate code and hit enter.

交互模式非常适合快速方便地运行单行或代码块。 这是使用基本python安装随附的python shell的示例。 “ >>>”表示外壳已准备好接受交互式命令。 因此,例如,如果要打印“这是交互模式”语句,只需键入适当的代码并按Enter。

interactive mode example

If by chance you instead have the Spyder code editor installed, printing the same statement in interactive mode will look something like this:

如果偶然您安装了Spyder代码编辑器,则在交互模式下打印相同的语句将如下所示:

interactive mode spyder example

The IPython console used above in Spyder is a little different from a normal Python shell, but for our purposes we can treat them as more or less identical.

上面在Spyder中使用的IPython控制台与普通的Python shell有所不同,但是出于我们的目的,我们可以将它们视为或多或少相同。

If you play around with both tools, you may notice that if you input a line of code that’s the first line of some larger block of code, like a for loop or conditional statement, you will be allowed to enter additional lines of code after the first. Press enter twice after your last line of code, and the whole block will run.

如果您同时使用这两种工具,则可能会注意到,如果您输入的代码行是某个较大代码块的第一行,例如for循环或条件语句,则将允许在代码行之后输入其他代码行。第一。 在最后一行代码之后按Enter键两次,整个块将运行。



脚本模式 (Script Mode)

If instead you are working with more than a few lines of code, or you’re ready to write an actual program, script mode is what you need. Instead of having to run one line or block of code at a time, you can type up all your code in one text file, or script, and run all the code at once. In the standard Python shell you can go to “File” -> “New File” (or just hit Ctrl + N) to pull up a blank script in which to put your code. Then save the script with a “.py” extension. You can save it anywhere you want for now, though you may want to make a folder somewhere to store your code as you test Python out. To run the script, either select “Run” -> “Run Module” or press F5. You should see something like the following:

相反,如果您正在使用多行代码,或者准备编写实际的程序,则需要脚本模式。 您不必一次运行一行或一段代码,而可以在一个文本文件或脚本中键入所有代码,然后一次运行所有代码。 在标准的Python Shell中,您可以转到“文件”->“新文件”(或直接按Ctrl + N)以拉出一个空白脚本,将代码放入其中。 然后以“ .py”扩展名保存脚本。 您现在可以将其保存在任何位置,尽管您可能需要在测试Python时在某个位置创建一个文件夹来存储代码。 要运行脚本,请选择“运行”->“运行模块”或按F5。 您应该看到类似以下的内容:

script mode example

If instead you are using Spyder, here’s what running the same code in script mode will look like. The process is similar, except you’ll click the green “Run file” button in the tool bar (or press F5 again) to run the script.

相反,如果您使用的是Spyder,则这是在脚本模式下运行相同代码的样子。 该过程与之类似,除了您将单击工具栏中的绿色“运行文件”按钮(或再次按F5)以运行脚本。

script mode example spyder

As you’ll see, if you’re working with more than a handful of lines of code or you’re building an actual program, script mode is often the way to go. Here’s a simple example with more than a single line or block of code that you’d have to enter in separate commands in interactive mode.

正如您将看到的,如果您要处理的代码行数不胜数,或者正在构建实际的程序,则通常采用脚本模式。 这是一个简单的示例,其中包含多行或多行代码,您必须以交互方式在单独的命令中输入代码。

script mode example spyder 2



使用print()以脚本模式显示值 (Use print() to display values in script mode)

Something to note is that some code will result in something being displayed when executed in interactive mode, but that same code will be ignored in script mode. For example, try running 1 + 1 in both modes. Interactive mode will display the result of the calculation, while script mode won’t result in anything being printed to the console. This is where the print() function comes into play, as seen earlier in this post.

需要注意的是,某些代码在交互模式下执行时将导致显示某些内容,但是在脚本模式下将忽略相同的代码。 例如,尝试在两种模式下运行1 +1。 交互模式将显示计算结果,而脚本模式不会导致任何内容打印到控制台。 如本文前面所述,这就是print()函数起作用的地方。



在交互模式下执行大量代码 (Executing Lots of Code in Interactive Mode)

You might find, however, that there are times when you want to run lots of code but don’t feel like saving a script. In this case, there are a couple things you can do to make interactive mode run more code.

但是,您可能会发现有时候您想运行很多代码,但又不想保存脚本。 在这种情况下,您可以做一些事情来使交互模式运行更多代码。

The first way is to simply copy and paste all the code from wherever into the shell or IPython prompt. Often this will run all the code just fine.

第一种方法是简单地将所有代码从任何位置复制并粘贴到Shell或IPython提示符中。 通常,这会运行所有代码。

The second way, and what I often find most convenient, is to copy and paste the code in question to a blank script in Spyder. Then you highlight however much of the code you want to run and hit Ctrl + Enter. This will run all the highlighted code interactively without you ever having to save a script. This makes it really easy to quickly test out different parts of a script and is a nice mixture of interactive and script mode.

第二种方法(也是我通常最方便的方法)是将有问题的代码复制并粘贴到Spyder中的空白脚本中。 然后, 突出显示要运行的许多代码,然后按Ctrl + Enter 。 这将以交互方式运行所有突出显示的代码,而无需保存脚本。 这使得快速测试脚本的不同部分非常容易,并且是交互模式和脚本模式的完美结合。



翻译自: https://www.pybloggers.com/2017/10/coding-in-interactive-mode-vs-script-mode/

交互模式和非交互模式】


http://www.niftyadmin.cn/n/984925.html

相关文章

Android系统架构及内核简介

(来源于ThinkPHP) Android是Google公司开发的基于Linux平台的开源手机操作系统,它包括操作系统、中间件、用户界面和应用程序,而且不存在任何以往阻碍移 动产业创新的专利权障碍,并由Google公司于2007年11月5日正式发布。同时,Google公司组建了一个开放手…

JDK源代码学习系列07----Stack

JDK源代码学习系列07----Stack1.Stack源代码很easypackage java.util;public class Stack<E> extends Vector<E> {// 版本号ID。这个用于版本号升级控制&#xff0c;这里不须理会&#xff01;private static final long serialVersionUID 1224463164541339165L;//…

整理总结 --- HDFS HDFS一些疑问

目前Hadoop只支持单用户写&#xff0c;不支持并发多用户写。 可以使用Append操作在文件的末尾添加数据&#xff0c;但不支持在文件的任意位置进行修改。 HDFS设计之处并不支持给文件追加内容&#xff0c;这样的设计是有其背景的 但从HDFS2.x开始支持给文件追加内容&#xff0c…

第1章1节《MonkeyRunner源码剖析》概述:前言(原创)

天地会珠海分舵注&#xff1a;本来这一系列是准备出一本书的&#xff0c;详情请见早前博文“寻求合作伙伴编写《深入理解 MonkeyRunner》书籍“。但因为诸多原因&#xff0c;没有如愿。所以这里把草稿分享出来&#xff0c;所以错误在所难免。有需要的就参考下吧&#xff0c;转发…

瑞芯微开源_微包和开源信任扩展

瑞芯微开源Like everybody else this week we had fun with the pad-left disaster. We’re from the Python community and our exposure to the node ecosystem is primarily for the client side. We’re big fans of the ecosystem that develops around react and as such…

波罗蜜多 python之旅_Python简介,序言–未来之旅

波罗蜜多 python之旅Data Rebellion presents 数据叛乱呈现 恶魔与矮人&#xff0c;Python冒险简介 (Demons and Dwarves, An Intro to Python Adventure)呼吁冒险 (Call to Adventure) A shadow falls over the land. Hero after hero arises to confront the encroaching ev…

Hadoop中java环境变量的设置问题,设置好了,却依然报错

今天重新开一个账户配置hadoop&#xff0c;可是都完成了&#xff0c;运行时&#xff0c;出现找不到java的情况&#xff0c;我的java是在.zshrc中设置的&#xff0c;export JAVA_HOME/usr/lib/jvm/java&#xff0c;在原来都是可以得&#xff0c;但是这次怎么都不行&#xff0c;后…

如何对所有用户进行预测?相关数据集推荐一下

预测所有用户的方法可能会因预测的内容而异。在进行预测之前&#xff0c;首先要了解预测的内容。 假设你想预测用户的购买行为&#xff0c;那么你可以使用监督学习的方法。这种方法需要收集具有标签的数据&#xff0c;比如用户的购买历史记录&#xff0c;然后使用这些数据训练一…