A First Step in Python Programming
A computer program is a list of instructions that tell the computer what to do.
A program, like any system, can be seen as sequence of
Input → Process → Output
The Hello World program is the common first program used to introduce
a programming language, it has the objectives of
- Verifying that someone is able to properly run a program
- Verifying that a program is able to send output to the user
- Verifying that someone is able to enter the source code (program statements)
of a program.
Python is an interpreted language, which means that
python is a program itself that reads statements
that are written in the python language, and executes the statements.
Running Python Interactively
If you have python installed, you can run python interactively, which
means you can enter commands and have python read and interpret statements as you
type them in. Text entered by the user/programmer is shown below in blue.
Example: running python from a Linux command line
python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
print "Hello World"
Hello World!
>>>
quit()
Explanation:
The python command starts the python interpreter.
After sending the user some information about python, python
sends the user a prompt >>>
The first command entered by the user is the python statement:
print "Hello world"
which executes the python print statement, sending the
output "Hello World" to the screen.
Python again prompts the user for more input using the >>> prompt,
and the user enters
>>>quit()
which terminates python (by calling the python quit function.
Running Python with a stored program
Most progams will be entered using a text file, where the programs
can be stored, edited as needed, and reused. Create a text file named helloWorld.py
and enter the following one line program
With the program saved, python can be run from the command line with the name of the stored program.
Example: running python with a program name
python helloWorld.py
Hello World!
Explanation:
The python helloWorld.py command starts the python interpreter
and python will immediately begin to process the program
in file helloWorld.py.
Python will not display any program information about
itself, nor will it provide a prompt. Python executes
the program helloWorld.py and then terminates.
Making a python program executable from the command line
When you run python from the command line, the operating system
will search your path for the python interpreter. In unix/Linux you can
find out where a program is located using the which command.
To find out where python has been installed, issue the following
Linux command:
In my system, the which command replies with:
By adding the following line as the first line of helloWorld.py,
and making the file executable, the operating system will always
know to use /usr/bin/python to execute the program:
#!/usr/bin/python
print "Hello World"
Make the program helloWorld.py executable using the following unix/Linux command:
The program helloWorld.py can be run from the command line using only the name of the stored program.
Example: running an executable python program
./helloWorld.py
Hello World!
Explanation:
When the operating system is told to execute the helloWorld.py program, it will see by
the first line in the file that it should use the python interpreter,
and python will immediately begin to process the program.
Python will not display any program information about
itself, nor will it provide a prompt. Python executes
the program helloWorld.py and then terminates.
The ./ before helloWorld.py
in the command tells the operating system to look in 'the current directory' for the program
helloWorld.py