博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
交互模式和非交互模式】_交互模式下与脚本模式下的编码
阅读量:2518 次
发布时间:2019-05-11

本文共 5262 字,大约阅读时间需要 17 分钟。

交互模式和非交互模式】

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 。 这将以交互方式运行所有突出显示的代码,而无需保存脚本。 这使得快速测试脚本的不同部分非常容易,并且是交互模式和脚本模式的完美结合。



翻译自:

交互模式和非交互模式】

转载地址:http://jnqwd.baihongyu.com/

你可能感兴趣的文章
Linux 如何写makefile文件
查看>>
PHPExcel 使用心得
查看>>
洛谷 P3374 【模板】树状数组 1(单点加,区间和)
查看>>
verilog 代码编写小记
查看>>
PyQT的安装和配置
查看>>
从 docker 到 runC
查看>>
守护进程
查看>>
php数组
查看>>
PHP curl登录 跳过验证码
查看>>
.NET json格式 使用Newtonsoft.Json.JsonConvert类 附读取文件方法
查看>>
Deep RL Bootcamp Lecture 7: SVG, DDPG, and Stochastic Computation Graphs
查看>>
Deep RL Bootcamp Lecture 6: Nuts and Bolts of Deep RL Experimentation
查看>>
从Android设备中提取内核和逆向分析
查看>>
青暮三行诗
查看>>
thrift的lua实现
查看>>
VMware 安装完centos7 IP 问题
查看>>
python小白——进阶之路——day3天-———运算符
查看>>
C#身份证号码校验
查看>>
JS函数调用方式
查看>>
HDU2519:新生晚会
查看>>