<

Saving programs in Python

Published on
9,545 Points
3,545 Views
Last Modified:
Approved
Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we save it.

So when we use Python IDLE(GUI) we can navigate to "File" and then click on "New Window" or we can use shortcut i.e."Ctrl+N" which will open a new text editor where we can write our program and save it into any location on the operating system. From the new window we can execute our program too.

Below are the screen shots taken after opening IDEL and then opening a New window.
Opening a new window
New window

Now let us write a simple program and save it on any location, but when we save the file the extension of the file name will always be ".py" :

x = raw_input("Enter your name : ")
print "Hello  " + x

Open in new window


Once it is saved on your system then navigate to "Run" and click on "Run Module" or "F5" which will run on the Python Shell as like below:

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
Enter your name : Sloba
Hello  Sloba
>>> 

Open in new window


I have named this program as "test.py". Below is the image of the file that I have saved it on my desktop.

Now we can also run this program by simply double click on it. But we will be unable to see the result after we enter the name as I have shown earlier, because it is processed so fast that we cannot see if the printed the input successfully or not.
So for avoiding this let us modify the program a bit and execute it from my desktop.

Modified code:
x = raw_input("Enter your name : ")
print "Hello  " + x
raw_input(" Hit <enter> to Exit ")

Open in new window


Now when we run the script from desktop then it will show the result after we pass the our name as input:

Enter your name : Sloba
Hello  Sloba
 Hit <enter> to Exit

Open in new window


So once we hit enter the command prompt will be closed. Now we come to know like how to save our Python program and if we want to hold before exit then we can make use of a function called "RAW_INPUT".
0
Comment
Author:Swadhin Ray
3 Comments
LVL 3

Expert Comment

by:P0j0
Nice article. One thing that jumped out at me was

 "Here I am using Python IDEL(GUI) to write a simple program and save it, so that we can just execute it in future."

Isn't it called Python IDLE?

-P0j0
0
LVL 17

Author Comment

by:Swadhin Ray
Thanks a lot  to catching this out :-)...  will change it right now ..

Regards,
Sloba
0
LVL 75

Expert Comment

by:käµfm³d 👽
Since there is more than one IDE option for python, should the title of the article not be:  "Saving Python Programs in IDLE", or similar?
0

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Join & Write a Comment

Suggested Articles

Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month