Tuesday, February 28, 2012

Executing a Python Program

Well from the last Tutorial, we learnt that Interactive Mode is where Python executes a given function or code on pressing the Enter Key,

But now we will see how to make a small program, and save it, and also how to execute the program.

To write a program, we either use a notepad or the inbuilt Python Text Editor... either way it works.

I hope everyone knows how to open a notepad, so I am skipping on it, The important thing is whenever we write a program in python, it has to be saved with an extension of .py

So if we are using the notepad, we write the program and save it as "name.py"

Now lets see how we write in the Inbuilt Python Text Editor, for this we open up the Python IDLE(the GUI) next we click on File, and then New(Shortcut Ctrl+N). This opens up the Text-Editor of Python, where we can write our program, but even here, after writing the program we need to save the program with a .py extension, or else python would not understand what we are trying to execute while executing.

We can save our program anywhere we want, as long as we have saved with as .py extension, but to make things easy, I create a folder on C Drive, where the Python is stored, and usually name it as "python programs" and all programs are saved inside the folder.

Lets make a small program.

In the Text-Editor type:

print "Program to check if i can execute this"
print "2+2: ", 2+2
print "I think If i have read the post correctly, it should execute without any error"

Now save the code, Click on File on the Text-Editor, Click on Save, and While Saving Dont forget to add the .py extension, lets save this as check.py.

To execute the program, we need to click on Run>>Run Module, For Older Versions, it would be Edit>>Run Script, Or Else You can use the shortcut which is hit the F5 key, this will execute the program.

After executing, we will see the display on the IDLE as

>>>
Program to check if i can execute this
2+2: 4
I think If i have read the post correctly, it should execute without any error
>>>

So I hope now we know how to Create a program and save the program, and also how to execute it.

I hope You Enjoyed this, and Dont forget to check the next post.

Thank You!

1 comments: