Python Hello World Example

In this section we will learn to write and test our first Python Hello World Example program.

Python Hello World Example

Python Hello World Example - Writing and testing First Python Hello World example program

In the last section we have installed Anaconda Python 3 and tested installation by running the python terminal. We also execute few Python code in the terminal. In this section we will learn further and develop our first Python program.

This is a simple tutorial, which is easy to learn "Hello World" programming after installing python programming language on your computer. This program will familiarize you with the python programming language. You will learn to test your installation with simple python program by learning the steps to write and execute python code. This tutorial will show you the basic syntax of Python programming language.

The Hello World is very first program in any programming language that developers learn to understand the programming language. With this example program developer also learns to write and run the program using the programming language. We will also start our Endeavour to learn Python 3 with "Hello World" program which prints "Hello World" message on console when executed by Python interpreter.

We will run this program in Python console and also through .py file. The Python console is good if you are doing simple testing of your code. But for running the program in production python file (.py) file is used and configured to run by python interpreter.

So, let's get started python programming with writing and running "Hello World" example program. With this program you will learn about the coding and testing process in Python.

Step 1: Writing "Hello World" program in Python shell

First of we will show you to run the Hello World program in python shell. Open terminal in your Ubuntu Operating system and then type "python" and this will open the Python shell as shown below:

Python 3 shell

In the Python terminal type following code:

print("Hello World")

Above code will print the "Hello World" message on the python terminal as shown below:

Python Hello World Example

Let's breakdown this programming code, this example code prints "Hello World" message on the python console. We have used the pint() function of Python to actually print message on console. The print() is a function of Python which instructs the Python interpreter to print the String value passed to this function as parameter. In Python function called are made with the use of parenthesis.

The print() is one of the many built-in function that comes with the Python programming language and this becomes available to the program in execution time.

After testing the code you can type exit() in the console to exit from the Python console.

Step 2: Running "Hello World" program in Python from a .py file

You can also write your code in a text file and save it with .py extension. This file is called python code file and can be executed from command line with the following command:

python file.py

The python command will read the code written in file.py and execute it in sequence. Writing code into .py file is the final goal of developing program in python. You can test your code in python shell but finally you have save your code into a .py file so that it can be deployed on the server.

Create a new file say test.py and add following code:

print("Hello World")

You can use any of favorite editor in Ubuntu and create above file. The easiest way to create this file from terminal is to use the vi editor. Type following command in terminal window:

vi test.py

This will open vi editor, once vi editor opens you press the 'i' key and then it will enter in insert mode. In this insert mode you will be able to type code as shown below:

editing python code in vi editor

After typing code press "Esc" button and then on the vi prompt type "wq" and press enter key. This will code the vi editor.

Now in terminal you can type:

python test.py

This will execute the python program as shown below:

Running python code in file from command line

The Python interpreter run code in test.py and prints "Hello World" message on the console. Note that it is not printing the double code in the console. The double code is used to passing string "Hello World" to the print() function. You can also pass variable in the print function. For example you can define a string variable with "Hello World" text and then call print function in following way:

msg = "Hello World"
print(msg)

It will print output:

Type "help", "copyright", "credits" or "license" for more information.
>>> msg = "Hello World"
>>> print(msg)
Hello World
>>> 

In this tutorial we have learned to write Hello World program in python and run it. You also learned to make python file and run it from console.

Check more tutorials at Python Tutorials