Python Classroom image

Anil  Bist / Professional / Web Technology

 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16|
Lessons:-INTRODUCTION

1.9. The Idle Editor and Execution
1.9.1. Loading a Program in the Idle Editor, and Running It. It is time to put longer collections
of instructions together. That is most easily done by creating a text file and running the Python interpreter
on the file. Idle simplifies that process.
First you can put an existing file into an Idle Edit Window. Click on the Idle File menu and select Open.
(Or as you see, you can use the shortcut Ctrl+O. That means holding down the Ctrl key, and pressing the
letter O for Open.) You should get a file selection dialog. You should have the sample program madlib.py
displayed in the list. Select it and open it. (If you do not see the program, then you either failed to download
the example programs, Section 1.2.1, or you did not start Idle in the proper folder, Section 1.2.4.)
You will see the source code again. Now run this program from inside of Idle: Go to the Run menu of
that Edit window, and select Run Module. Notice the shortcut (F5).
1.9. THE IDLE EDITOR AND EXECUTION 18
If the Shell window does not automatically come to the foreground, select it. You should see a line
saying “RESTART” and then the start of the execution of the Mad Lib program with the cursor waiting for
your entry after the first prompt. Finish executing the program. Be sure to type the final requested Enter,
so you get back to the interpreter prompt: >>>
Look at the editor window again. You should see that different parts of the code have different colors.
String literals are likely green. The reserved words def are likely orange. Look at the last two lines, where
the identifier tellStory is black, and the identifier input is likely purple. Only identifiers that are not
predefined by Python are black. If you create an identifier name, make sure Idle shows it in black.
1.9.2. A Bug Possible When Restarting Program Execution in Idle. When you execute a
program from the Idle Editor, the interpreter gives a banner saying “RESTART”, meaning that all the
things you defined in any shell session so far are wiped clean and the program you are running starts fresh.
There is one egregious exception to that, that was still present at least in the version of Idle for Python 3.1
in Windows. We will try to demonstrate the bug. (A bug is an error in a program.)
Start running the Mad Lib program again by going to the Editor Window containing madlib.py, and
start running the program again, but do not continue....
You should see a prompt for user input generated by the program. Ignore this prompt and go back to
the Edit Window and start the Mad Lib program again.
If this bug is still present, you should see a difference in this restart: This time after the RESTART
banner and the interpreter prompt: >>>, which looks innocent enough, but this program should show the
program’s prompt string for input.
The problem only comes up because you interrupted the last execution when user input was being waited
for. The restart was not complete here: The system is still looking for the pending user input from the last
execution.
The fix is simple: Make sure the Interpreter Window is the currently selected window, and press return
to terminate the lost user input. In some circumstances, you may need to press return a second time.
After that the program should start up normally with its prompt.
Watch out for this behavior, and remember the fix.
1.9.3. The Classic First Program . Make sure you have Idle started in your Python directory (in
Windows with the provided Idle shortcut link), where you will store program files. (Do not start Idle from
the Windows Start Menu!) If you just started Idle now, you may already have a blank Edit Window in
front of you. If not, open a new window by going to the File menu and selecting New Window. This gives
you a rather conventional text editing window with the mouse available, ability to cut and paste, plus a few
special options for Python.
Type (or paste) the following into the editor window:
print(’Hello world!’)
Save the file with the File menu -> Save, and then enter the file name hello.py. Python program files
should always be given a name ending in ".py", and you must enter the .py extension explicitly .
If you look in the editor, you should see that your text is color coded. The editor will color different
parts of Python syntax in special colors. (In version 2.4 of Python, the coloring only happens after you save
your file with the ’.py’ ending.)
Now that you have a complete, saved program, choose Run menu -> Run Module. You should see the
program run in the Python Shell window.
You just wrote and executed a program. Unlike when you use the shell, this code is saved to a file in
your Python folder. You can open and execute the file any time you want. (In Idle, use File->Open.)
To the interpreter, a program source file corresponds to a Python module. We will tend to use the more
general term: a program file is a module. Note the term from the menu when running the program.
Distinguish program code from Shell text: It is easy to confuse the Shell and the Edit windows. Make
sure you keep them straight. The hello.py program is just the line
print(’Hello world!’)
that you typed into the edit window and saved. When you ran the program in Idle, you saw results in the
Shell. First came the Restart notice, the one-line output from the program saying hello, and a further Shell
prompt:

 
 
 
image
Anil  Bist

Skills    Python

Qualifications :- High School - SLV, College/University - Graphic Era Deemed Univ University,
Location :-Dehradun,Dehradun,Uttarakhand,India
Description:-

I started my Professional Journey in 2006 with one of the Web Development Company in Bangalore and my 1st framework was "Ruby on Rail" as Web development and delivered around 5+ Projects using this platform. Then came another dimension as JEE/Sturst framework, Gradually I realized that I want to build something on my own and give my passion and energy on creating something different a
Explore

 

  Students (0)