1. Python Basics1. Python Basics\1.2 How Programs Are Written\1.2.2 Interactive Mode

1.2.2 Interactive Mode

 

One way of running a Python program is to type at a terminal (or, command prompt):

 

python my_program_file.py

 

In addition to running your programs, the Python interpreter has an interactive mode where users can run Python code, one line at a time. The user enters a line of code, and the interpreter executes it and prints the result.

 

Start a new terminal session, and then type the following at the command prompt:

 

python

 

This will launch the interactive mode.

 

Here is a sample interactive session. User input is highlighted in green.  Try it!  Type in each statement, pressing the Enter key after each one:

 

>>> x = 5

>>> x * 25

125

>>> x ** 3

125

>>> print "I have two bicycles."

I have two bicycles.

>>> (x < 7)

True

>>> (x < 4)

False

 

This mode is useful for experimentation. If you have an idea for some code, and you want to quickly see whether it works or not, run the interactive mode and type it in directly. If the code fails, interactive mode prints the error, but allows you to keep trying things.

 

 


 

 

 

Top of Page