Python Programming - First Python Program

Python Programming – First Python Program

First Python Program

We can execute the Python program in two different modes of programming viz. interactive mode programming and script mode programming. Each of them is elaborated as follows;

Interactive Mode Programming

In interactive mode programming, the interpreter is invoked and the programmer can code statements directly to the interpreter without passing a script file as – a parameter. Open up the Python interpreter and it brings up the following information:

$ python
Python2.4.3(#l,Novel 12010,13:34:43)
[GCC 4.1.220080704(RedHat4.1.2-48)] on linux2
Type”help”,”copyright”,”credits”or”license”for more information.
>>>

The simplest program to print Hello Python can be typed and run as follows:

>>>print”Hello, Python!”

If you are running a new version of Python, then you would need to use a print statement with parenthesis as in print (“Hello, Python!”); as displayed in Fig. 1.4. However, in the Python version 2.4.3, this produces the following result:

Hello, Python!

Python Programming - First Python Program chapter 1 img 1

The Script Mode Programming

In script mode programming, the complete script is written in an editor such as Notepad in Windows and then the interpreter is invoked with a script parameter. It begins the execution of the script and continues until the script is finished. Let us write a simple Python program in a script. Python files have extension.py. Type the following source code in the first file as shown in Code 1.1.

Code: 1.1. Illustration of Python first program (Script mode)

print (“Hello, Python!”)

We have saved the script in a <prog> directory under C drive. Then at the command prompt set the path as follows and run the script by calling the python interpreter. Eventually, we will obtain the desired output as Hello, Python! The graphic representation for this method is given in Fig. 1.5.

C:\>prog>path %path%;c:\python
C:\>prog> python first.py
Output:
Hello, Python!

Python Programming - First Python Program chapter 1 img 2

Python Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *