• Skip to primary navigation
  • Skip to main content

data Rebellion

Learning through Adventure

  • Home
  • Blog
  • Beginner Python Course
    • Preface – The Journey Ahead
    • Prologue – The Door
    • Chapter 1 – Arithmetic and Variables
    • Chapter 2 – Strings and Lists
    • Chapter 3 – Conditional Statements
    • Chapter 4 – Functions
    • Chapter 5 – Loops
    • Chapter 6 – Built-in Functions and Methods
    • Chapter 7 – Imports and Nesting
    • Chapter 8 – Opening the Door
    • Epilogue – Only the Beginning
  • About
  • Contact
You are here: Home / IDEs / Coding in Interactive Mode vs Script Mode

Coding in Interactive Mode vs Script Mode

Updated March 25, 2021. Published October 14, 2017. Leave a Comment

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.

Interactive Mode

Interactive mode (a.k.a. shell 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.

Printing a string in the Python IDLE

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

Printing a string in the Spyder IPython console

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.

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.

Script Mode

Now let’s look at script mode (a.k.a. program mode). If 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:

Running a short script in the Python IDLE

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.

Running a short script in the Spyder IDE

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.

Running a short exampe script in the Spyder IDE

Use print() to display values in script mode

Note that some code, when executed, will result in something being displayed 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.

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.

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.

Python Automation Project Ideas Ebook Front Page

Free Ebook: 88 Python Project Ideas for Automating Your Life

The best way to learn programming is by working on real-world projects, so why not work on projects that also save you time and sanity? In this free, curated collection, you'll find project ideas for automating:

  • Common office tasks
  • Birthday gifts and wishes
  • Grocery and meal planning
  • Relationships (just the tedious parts!)
  • And quite a bit more

Subscribe to Data Rebellion and get this Ebook delivered straight to your inbox, as well as other exclusive content from time to time on efficiently learning to code useful things, vanquishing soul-crushing work, and having fun along the way.

Reader Interactions

Leave a comment Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Copyright © 2023

Terms and Conditions - Privacy Policy