You are on page 1of 25

Introduction to the Python

Interpreter and its use.

Mac Littlefield
COMSC-122
Using Python
• Python must be installed and configured prior to
use
• One of the items installed is the Python interpreter
• Python interpreter can be used in two modes:
• Interactive mode: enter statements on keyboard
• Script mode: save statements in Python script
The Script mode can be run in four ways:
1. Directly from the Command Line
2. From within the Python Console
3. Through use of IDLE (Integrated DeveLopment
Environment)
4. Directly from the Windows Explorer GUI
• You must have set the pathway so that the operating
system knows where the Python interpreter is located.
for this method to work.
First Method: The Interactive Mode
• When you start Python in interactive mode, you
will see a prompt
• Indicates the interpreter is waiting for a Python
statement to be typed
• Prompt reappears after previous statement is executed
• Error message displayed If you incorrectly type a
statement
• Good way to learn new parts of Python
Running Python in Interactive Mode
• The simplest way to enter the Command Line
Interactive Mode is:
• Hold down the Windows Key and then strike R
• In Windows 10, there is another simple way to do
this, you only need right click on the icon in the
lower left hand corner of the screen
• Then select Run on the menu that appears:
Running Python in Interactive Mode
• What comes up next is the Run Window:

• Type in “cmd”, click on OK, and you will see the


Console window:
Running Python in Interactive Mode
• If we have not already set the Pathway for Python,
we need to get into the folder in which Python exists.
• On my computer it exists in Python34
• This is done with the cd\python34 command:
Running Python in Interactive Mode
• Now that we’re in the folder where Python34
exists, we’re ready to execute Python

• Now we’re in the Python Shell, ready to start


executing Python statements.
Running Python in Interactive Mode
• Here are a few print commands executed at the
console command line.
Writing Python Programs and
Running Them in Script Mode
• Statements entered in interactive mode are not
saved as a program
• For this reason, I do not find the Interactive mode
very useful, excepting for testing out how certain
commands work.
• If you really want to run a program, you need to
employ the Script Mode.
Second Method: Running Script Mode
• To have a program use script mode
• Save a set of Python statements in a file
• The filename should have the .py extension
• To run the file, or script, type
python filename
at the operating system command line
Writing your first Python Program
• Use an editor like Notepad to write a short Program.
• Save the program as Test.py
Running your first Python Program
• Now go to the Command Line and run the program.
3rd Method: Execute Program in Python Console

• You can also execute the program from within the


Python Console by typing: import test
4th Method: The IDLE
Programming Environment
• IDLE (Integrated Development Program): single
program that provides tools to write, execute and
test a program
• Automatically installed when Python language is
installed
• Runs in interactive mode
• Has built-in text editor with features designed to help
write Python programs
• You can enter the IDLE programming Environment by
running the Batch File called: Idle.bat
• If you installed your Python interpreter in a folder called
Python34, then the pathway to Idle.bat should be:
• C:\Python34\lib\idlelib\idle.bat
• Using Windows Explorer, go to the idlelib folder and create a
shortcut for the idle.bat file.
• Save that shortcut to your desktop.
The Integrated Development
Environment (IDLE) for Python
• Here’s what we see when we go into IDLE
Running a Program in the IDLE
Environment
• We’ll open up a program called Chaos.py
Running Chaos in IDLE
Running a Program in the IDLE
Environment
• The program emerges ready to be Run.
Running a Program in the IDLE
Environment
• Click on Run/Run Module or strike F5 key.
Running a Program in the IDLE
Environment
• Now the program executes and asks you to enter
a number between 0 and 1.
Running a Program in the IDLE
Environment
• We will type in .25 and hit Enter.
• 10 lines of output emerge from the program.
Running a Program in the IDLE
Environment
• The other way that we could have run this program is
by typing: import chaos at the command line.
5th Method: Run Directly from
Windows Explorer
• Double click on chaos.py within Windows Explorer
• In order to do this the Python Interpreter must be in the
Pathway or this won’t work.
The Console will Emerge and run
the Program
• The key to holding this console on the screen is
contained in the last line of the program:
exit = input(‘Hit any key to Exit: ‘)

You might also like