We will demonstrate to write a simple Python program, which prints “Hello World”. Type the following lines in IDLE text editor and save it as “HelloWorld.py”.
#! /usr/bin/env python print('Hello world')
The first line is called “shebang line” or “hashbang line”. The second line gives the output: “Hello World”. There are numerous ways to run a Python program. The simplest approach is to press F5 functional key after saving the program in IDLE text editor. The output is shown below:
>>> Hello world
Executing Python script
Python script can be executed using F5 functional key, from Python’s IDE. It can also be executed using command prompt by typing the following command:
$ python <filename>
On different platforms, the execution of Python scripts (apart from running from inside Python’s IDE) can be carried out as follows:
Linux
On Unix/Linux system, Python script can be made directly executable, like shell scripts, by including the following expression as first line of the script (assuming that the interpreter is on the user’s PATH) and giving the file an executable mode.
#! /usr/bin/env python
The ‘#!’ must be the first two characters of the file. Note that the hash or pound character ‘#’ is used to start a comment in Python. The script can be given an executable mode/permission, using the chmod command:
$ chmod +x HelloWorld.py
Windows
On Windows system, the Python installer automatically associates .py files with python .exe, so that double-dick on a Python file will run it as a script. The extension can also be .pyw, in that case, the console window that normally appears is suppressed. At MS DOS prompt, the Python script can be executed by going to the directory containing the script and just entering the script name (with extension).