Python Hello World

Table of Contents

Introduction

This post will show you how to run the storical “Hello World” program in Python.

For the uninitiated, the Hello World program has the purpose of printing the “Hello World” string on the screen.
Usually this program is the first step in learning a new programming language.

I leave you this interesting article for those wishing to deepen the history of the hello world program.

Write the Hello World program in Python

The only prerequisite is to have a Python version installed on your computer.
If you have read this post on how to create a virtualenv fine, but for this program we won’t need it.

Let’s start creating a new file called, for example, hello_world.py.
If you change the name, just be sure to use the correct one for the next tutorial steps.

Once the file has been created, the next step is to write the program!

print("Hello World!")

The final step is to copy and paste the code above in your file and save it.

As you can see, the program it is very easy indeed there is just one line of code.
Before seeing the program output, let’s focus on the python function called print that is present in the code.

print(*objectssep=’ ‘end=’\n’file=sys.stdoutflush=False)
Print objects to the text stream file, separated by sep and followed by endsependfile, and flush, if present, must be given as keyword arguments.

The one above is the definition present in the Python documentation but, in simple words, we can say that the print function allows us to print something on the screen.

Output

If you got here the only thing you are missing is running the program.

Open a terminal and type the following command:

python hello_world.py

If everything worked correctly the output should be this:

Python Hello World output
Python Hello World output

A common error is

can't open file 'hello_world.py': [Errno 2] No such file or directory

This means that you are in the wrong directory, or more simply that the hello_world.py file is not in the directory where the terminal is located. To fix it, just move in the correct directory and lunch again the command.

Conclusion

Following this simple tutorial you have learned how to create and run the Hello World program in Python.
Now you are ready to delve into the other basics of the Python programming language.
Let me know in the comments if this post was helpful to you and if you ran into any issues!

Leave a Comment

Your email address will not be published. Required fields are marked *