Language: EN

python-como-empezar

Our first Python program

Let’s start creating our first application in Python. A very simple application that will only display “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 its charm😉).

Although it is logically very basic, the goal is to familiarize ourselves with the basic structure, run our program, and ultimately… make sure that “everything works.”

Creating the program

I assume you already have Python installed on your system, and have installed a code editor like VSCode. Although for this first example, any text editor will work.

To create our “Hello World” program in Python, we simply need to create a text file, and save it with a .py extension.

It’s that easy. We don’t need to start a project or launch some process. One of the advantages of Python is its simplicity. The Python interpreter will take care of everything when we run it.

For example, we create a folder called MyProject. You can do this with your operating system’s File Explorer, or if you do it via 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 whatever name you want.

Writing the code

In the file hello_world.py, we will write the following code:

# Our first application in Python - "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 saved our hello_world.py file.

Now, to run our program, we simply write the following command:

python hello_world.py

With this, we are telling the Python interpreter to execute the script hello_world.py. For this, we use the command python.

For the Python interpreter to work, it must have been installed as PATH of the operating system.

If everything is configured correctly, we will see the message Hello World! printed on the screen.

Hello World!

It’s that simple! Nothing more is needed. Just run the command python passing the name of our script.