In our last tutorial, we have discussed steps to install the latest release of Python.
Installing Python(as explained in our last tutorial) also installs its IDLE IDE i.e.
an interactive integrated development environment used to code in Python, which highlights all the keywords
and has a Python Shell, also known as interpreter, responsible for the execution of the Python programs, display their output and
to notify us about the errors in a program.
In order to execute your first python program using IDLE IDE, we need to go to the folder where the IDLE IDE has been installed and execute it.
Executing the IDLE Integrated Development Environment
In our last tutorial of discussing steps to install Python, we did not play with the default installation settings, hence, the Python on our system has been installed in its default location i.e.
Double-clicking the file idle.bat in this folder,
executes the IDLE IDE and opens the window of Python Shell, with a prompt >>>(at which you could type in a code to execute).
Figure 1
Two ways to code a Python program using IDLE
There are two ways to code a Python program using IDLE -
By coding directly into the Python Shell of IDLE.
By coding in a separate file.
In both cases, the Python Shell(interpreter) will display the output or the errors associated with the program. Let us see both the situations with an example by printing a
the customary message Hello World!.
Advertisement
Coding directly into the Python Shell of IDLE
In Python Shell, we could type in a Python code, which will be executed in the sequence we type it and its output is displayed.
Let us type in a small program at the prompt of this Python Shell i.e. >>>, which prints customary string message Hello World, as shown in the picture below.
Figure 2
Coding in a separate file.
Using the Python Shell window, we could create a new separate file by clicking on the File-> New File menu options on the top menu and code in it.
Let us name this file first.py.
Note: All console programs in Python are stored with a .py extension, whereas all GUI Python programs are stored with .pyw extension.
By default, the programs coded in a separate file using IDLE will be stored in the same directory where the IDLE is stored, i.e.
but you could store your Program in a directory of your choice and that's what we are going to do now. We will save the file of our first Python program in a location D:/Python( as shown in the picture below), and we are going to name it
first.py.
first.py
Figure 3
How to execute the code in a separate file
To execute this code in a separate file, we have three options -
We could click on the menu options on top of this window i.e. Run -> Run Module, or
We could just press the ALT + F5 in the window of this program which is coded in IDLE.
Doing so will open the Python Shell window, displaying the output of the code which was stored in this separate file or the errors associated with it, as shown in the picture below.
Figure 4
Apparently, we could also execute a Python program(which is coded in a separate file) using the CMD i.e. command prompt
of Windows Operating System, by just changing the current path to the directory in which we have stored our Python program
and by typing in the name of the program with its .py extension. This launches the interpreter and executes the program in the console window,
as shown in the picture below.
Figure 5
And this concludes our tutorial on how to execute your first program in Python using IDLE integrated development environment.
Hope it will be a useful guide to some.