Getting Started with Python

Once Python is installed, it is possible to start writing Python programs. Included as part of the installation is an IDE or Integrated Development Environment called IDLE, that enables you to do this. There are also other IDEs available, such as PyCharm.

Code Commenting

When programming in any language it is useful to add comments to the code to describe what is happening and why. This is useful when the code is revisited at a later date to make enhancements, or for debugging purposes. In order to add a comment in Python a line needs to start with a ‘#’ symbol as shown below.

# This is a comment in Python.

First Python Program

The Python code written below simply displays the words ‘Hello World!’ in a console or terminal window.

print("Hello World!")

Note that the word ‘print’ in blue above is known as a reserved word. Reserved words in a programming language have a specific purpose and therefore cannot be used for anything else. In this case ‘print’ displays a message in the console. If the above code is saved into a file with a ‘.py’ extension, for example, ‘hello_world.py’, it can then be run.

Running a Python Program

Running a Python program will vary slightly depending on what IDE is being used. If IDLE is the IDE of choice, the ‘Run’ menu option can be selected, followed by ‘Run module’. The shortcut key ‘F5’ can also be used. Some alternative IDEs have a ‘Run’ button that has a play symbol on it in green.

Additionally, it is possible to run a Python program from the command line or terminal using the following command.

python hello_world.py

Please note that if during the installation of Python, the option to setup an environment variable wasn’t selected, the full path to the Python application will have to be entered.

C:\path_to_python\python hello_world.py

Similarly, if whilst running the command, the Python file is in a folder other than the current one, the path needs to be provided to it.

python C:\path_to_file\hello_world.py