Python’s Interactive Mode - Python Bootcamp: The Crash Course for Understanding the Basics of Python Computer Language (2016)

Python Bootcamp: The Crash Course for Understanding the Basics of Python Computer Language (2016)

Chapter 2. Python's Interactive Mode

The Python programming language has 2 different modes:

1. Normal– In this mode, you’ll run the scripted and completed Python files using the built-in interpreter.

2. Interactive– This is a command line program that can give instant feedback for each of your statements. This mode gives feedback while performing previous statements stored in the machine’s memory. Technically, the interactive mode evaluates statements individually and holistically while new ones are being entered into the Python interpreter.

This chapter will focus on the interactive mode. To activate it, just enter“python” without adding any argument. This is an excellent way of learning the programming language: you’ll play around statements and syntax variations. After typing“python,” the screen will show you a message similar to the one below:

Important Note: If Python doesn’t work, make sure that you have set your path properly.

Notice that the message has“>>>” at the end. These symbols indicate that you are using Python’s interactive mode. Here, the system will immediately run whatever you type. Actually, if you’ll type 1 + 1, Python will give you 2. You can use this mode to become familiar with Python and test its capabilities. If you have learned new statements, activate the interactive mode and check them one by one.

The image below shows an interactive session:

As you can see, Python’s interactive environment is an excellent learning and programming tool. However, you have to be extremely careful when using it since it can be confusing sometimes. For instance, the image below shows a Python script that is considered valid in the interactive mode:

If you’ll use this script as shown in the interactive mode, you’ll get a surprising result:

The Python interpreter says that the second print’s indentation is unexpected. Before writing the next statement, you need to end the first one (i.e. the“if” statement) using a blank line. For instance, you must enter the statements using this format:

This will give the following result:

The Interactive Mode

You may use“-i” to activate the interactive mode. This flag will stop Python from closing when the program is done. Computer programmers use this flag a lot, especially during the prototyping and debugging stages. Here’s an example: