Python 2.3 - Executing Python program
Executing Python program
There are three ways to execute Python programs.
·
Using Python’s IDLE window
·
Using Python’s command line window
· Directly from System prompt
The first two methods are called interactive modes where we can type the program one line at a time and the PVM executes it immediately. Remember we can use Python’s IDLE window as script mode too. And the last one is called script mode where we ask the PVM to execute our program after typing the entire program.
Note: In interactive mode, one statement is executed
at a time. And in script mode, complete program is executed.
1. Using Python’s IDLE window
IDLE (integrated development environment or integrated development and learning environment) is a graphical interface i.e. it provides graphical interface to the user. We can also modify the font face, size etc. in IDLE. To open IDLE, click on start button of your System, search for Python 3.8 and click on IDLE as shown in Figure 2.3 above. IDLE is an interactive mode but it also provides the facility to write a complete program (script mode). A window (figure 2.4) will be opened. This window is called Python IDLE or Python Shell. As we know that we can use IDLE for both interactive and script mode. So let’s first understand the interactive mode in IDLE. As soon as you open IDLE (figure 2.4), this window can be used as interactive mode. Now we can type any statement and PVM (interpreter) will execute it instantly. For example, we have written a print statement in IDLE. As soon as we type Enter, the statement will be executed. The next line after ‘print’ statement is the result produced by Python Interpreter. So this is called an interactive mode with Python’s IDLE window. Now we can also use IDLE for script mode i.e. for writing complete program. I have explained this method in topic ‘Recommended method to write programs’ in this chapter. So please refer that topic.
2. Using Python’s command line window
This is also an Interactive mode that will execute Python statement as soon as it is typed. So we should not write a complete program here. Use this mode to practice Python commands only. To open this, click on start button of your System, search for Python 3.8 and click on Python 3.8(64-bit) as shown in Figure 2.3 above. A black window (figure 2.5) will open. This is Python’s command line for interactive mode. Here we have again written a print statement. So, as soon as we press the Enter, the statement will be executed and the result (the line after print statement) will be displayed on screen. So this is also an interactive mode.
The symbol
>>> is called Python prompt where we start writing Python program or
statement.
3. Directly from System prompt
This is the Script mode where we write entire program at once in a Text editor and save the file with “.py” extension. You can open a simple text editor on Windows OS by right clicking on your computer screen, then go to ‘new’ and click on ‘Text Document’. A file named “New Text Document” will be created on the screen. Now double click on the file which was just created. Here you can write your program. Type the code here as shown in Figure 2.6. After writing the program click on “file->Save As” to save your file. Now type your file name with “.py” extension. You can choose any file name. I have chosen “sample.py”. Make sure that you do remember where you are saving your file. I wrote a program and saved my file in ‘E:\programs’ where ‘E’ is the drive and ‘programs’ is the folder as shown in images below –
Do not worry about the program written above. We will discuss everything in upcoming chapters. Here, we are only understanding that how to run and execute Python programs.
In order to execute Python program from “System’s command prompt”, we have to reach the location of our file first. Remember that our file “python.py” is stored in a folder named ‘programs’ and the folder ‘programs’ is stored in drive ‘E’ as you can see in Figure 2.7. In command prompt, type the command “e:” and press Enter to go into drive ‘E’. Then type the command “cd programs” and press Enter to go into folder ‘programs’. Now we are in the folder where we had saved our file.
Now we can execute our program by typing the following command and pressing Enter–
E:\programs> Python sample.py
Right after this command, the result of our program will be displayed.
See image below –
As you can see our program has successfully executed and gave us the output 30. The command “python” is a program that contains a compiler as well as a PVM.
Note: If the program is not executed in System’s
command prompt then make sure that you had selected the check box ‘Add Python
3.8 to path’ when we installed Python software (refer figure 2.2). And if you
forgot to add ‘Python to path’ then you can also add it manually. Just go to
Google and search ‘How to add Python to path’.
Recommended method to write programs
Although we have two methods (interactive and script) to write the programs but interactive method is suitable for practising only. When you want to write a complete program, use script mode that is available through a Text Editor and Python’s IDLE window. We had written the program in Window’s default Text editor, now let’s write the program in Python IDLE. For those who are new to programming, I recommend that you write programs using Python IDLE. IDLE stands for Integrated Development and Learning environment. We can also call it IDE (Integrated Development Environment). IDE is nothing but a software in which programmers write programs. It provides an integrated environment do develop programs. An IDE allows to build applications that combines common developer tools into a single unit.
You can also download other IDE or IDLE such as PyCharm, Microsoft visual studio, Spyder etc. to write Python programs but I recommend the default IDLE which comes with Python software. Default IDLE is best for new learners.
So now open Python IDLE by clinking on ‘IDLE (Python 3.8 64 bit)’ like we did before (see Figure 2.3). But this time click on “File->New File” to open a blank file. Here we should write our Python programs. let’s write back the program in Python IDLE that we wrote in the Text Editor. After writing the program, save the file with the name “sample.py”. You can choose any other file name but choose appropriate name.
To run the program, click on “Run->Run Module” or just
press the F5 key (shortcut key). We will get the output as shown below –
So in this chapter we have discussed all the ways to write and execute Python programs. in next chapter, we will dive into writing actual code.
Comments
Post a Comment