Let’s start to create our first application in Python. Our first Python application that will print “Hello World” on the screen.
In the world of programming, “Hello World” is usually the first program written when learning a new language. It’s almost a tradition, which also has a certain charm😉.
Although it’s logically very basic, the goal is to familiarize ourselves with the basic structure, execute our program, and well… make sure “everything works.”
Creating the program
I assume you already have Python installed on your system, and installed a code editor like VSCode. Although for this first example any text editor will serve us.
To create our “Hello World” program in Python, we simply have to create a text file, and save it with the .py
extension.
That’s easy. We don’t need to start the project, or launch some process. One of the advantages of Python is its simplicity. The Python interpreter will take care of everything when executing it.
For example, we create a folder called MyProject
. You can do it with the File Explorer of your operating system, or if you do it by terminal like this
mkdir MyProject
cd MyProject
Now, inside this folder, we create a text file. For example hello_world.py
, although you can use the name you want.
Writing the code
In the hello_world.py
file, we will write the following code:
# Our first Python application - "Hello World"
print("Hello World!")
This code is very simple. We simply use the print()
function, which is used to display messages on the screen. In this case, we tell Python to print “Hello World!“.
Running the program
Once we have written our code, we save the file and open a terminal or command line, in the location where we have saved our hello_world.py
file.
Now, to run our program, simply type the following command:
python hello_world.py
With this, we are telling the Python interpreter to execute the hello_world.py
script. For this we use the “python” command.
For the Python interpreter to work, it must have been installed as a system PATH.
If everything is set up correctly, we will see the message “Hello World!” printed on the screen.
Hello World!
That simple! Nothing else is needed. Just run the python
command passing the name of our script.
However, in general we will use an IDE or a code editor like Visual Studio Code, as we saw in this article How to use Python with VS Code