Support Our Site

To ensure we can continue delivering content and maintaining a free platform for all users, we kindly request that you disable your adblocker. Your contribution greatly supports our site's growth and development.

How To Run "Hello, World!" Program In Python

4 min read

First released in 1991, Python is a popular high-level programming language used for general purpose programming.

The reason for the instant popularity of python is in contrast to other popular languages such as C, C++, Java, and C#, Python strives to provide a simple but powerful syntax.

In this article, we will see "How to run a Hello World! program in Python". This is typically the first program you will run in any programming language.

Python programs must be written with a particular structure. The syntax must be correct to get the desired output, or the interpreter will generate error messages and will not execute the program.

Note - This article is srticlty on Python 3  and won't give the desired outpout for python 2.x users

If you are not already aware of this, there was a significant update of Python which rolled out in 2008 addressed as Python 3; this update resulted in a big split between Python 2 and Python 3.

Basically, there are two major Python versions which are currently in active use:

  • Python 3.x is the currently active version and is under development.
  • Python 2.x the legacy version and will only receive security update till 2020, yet many projects are still on Python 2

They both contrast from each other in many ways, you can read about them in detail here.

The most significant change is that, in Python 2 Print statement is not a function, and therefore it is invoked without parentheses. However, in Python 3, it is a function and must be invoked with parentheses.

So we are assuming you already have Python installed in your machine, if not go through our installation guides -

In case you are confused about which version of Python you have, you can simply check that with running,  python -- version in your terminal.

I am on a windows machine, so I see this-

C:\Users\USER> python --versionPython 3.6.5

Your version might be slightly different than mine, but as long it is Python 3.x we are good to go.

Okay so now we can move on, and start coding.

In this article, I will show you to how to write "Hello, World!" program in different ways in python 3.

We will consider the following two ways :

1. Write the program directly into the Interactive Python shell
2. Write the program into an editor, save it, and then run it from the terminal

How to run a Hello World program from Python interactive shell

Interactive Python shell is a pure Python integrated development environment available for Windows, Linux, and Mac OS X. This is also known as the Python interpreter.

You can launch the interaction shell by just running python in your terminal and run exit() to quit the interpreter, as shown below.

running python Interpreter in windows

I am on windows and have tweaked my command prompt a little you can do this too, by following this article.

However, this should work on Mac os and Linux if it doesn't let me know in comments below.

Now let's run the program.

To run Hello world in Python, you have to type the following command in Python interactive shell.

print("Hello, world!")

This is a Python statement. A statement is a command that the interpreter executes. The built-in function "print"  simply prints the message.

The thing to remember here is that the message should always be in a single('message') or double quotes("message") or else the compiler will throw errors.

Well done! You have successfully run your python program from the interactive shell.

Now exit the shell with exit()

Now let's look at another more preferred way of running Python programs in your machine.

How to run Hello world program from a file in Python.

One thing you might have noticed that the Python shell doesn't let you save your program and you can't write a large multiple line program in the Python interactive shell.

Therefore a more productive way of running Python programs is that first save your program in a file then run it from your terminal.

The most common text editors for python are -

  1. Sublime Text
  2. VS code
  3. Atom

You can use any one of them, for the sake of this tutorial I am using the most common and my favorite one VS code.

Now follow the below steps :

First, make a file named hello_world.py and save it on your desktop.

Keep in mind that .py is the extension for Python files and you must always include this.

Then write the same statement we wrote in Python interactive shell

print("Hello, World!")

This is how your file should look, as I already, mentioned I am on VS code your editor might look different you can see my VScode configuration here.

Hello world program in python 3

Now save the file again, and close the editor we are done with the file.

Then open your terminal and navigate to Desktop, then execute this - python hello_world.py

If you did it right, then you should see the same output I have below. If not, you have done something wrong go through the steps again.

how to run hello world in python

That's it Now you know how to run Python programs from Python interactive shell and from a python file.

Hope this article helped you. If you have any question regarding this article or python feel free to drop a comment below.


PYTHON

Latest Articles

Latest from djangocentral

How to Use Subquery() in Django With Practical Examples

In the realm of web development, Django stands as a powerful and versatile framework for building robust applications. One of the key aspects of developing efficient and optimized web applications is handling database queries effectively. In this article…
Read more →

4 min read

DRF Serializer: Handling OrderedDict and Converting It to a Dictionary or JSON

In Django Rest Framework (DRF) tests, when you access serializer.data, you might encounter an OrderedDict instead of a regular dictionary. This behavior is intentional and reflects the design of DRF's serialization process.Understanding the Problem The u…
Read more →

3 min read

Django Rest Framework CheetSheet: Mastering API Development

Django Rest Framework (DRF) is a powerful toolkit that makes building robust and scalable web APIs with Django a breeze. Whether you're a seasoned Django developer or a newcomer, having a comprehensive cheat sheet at your disposal can be a game-changer. …
Read more →

5 min read

How to Perform NOT Queries in Django ORM

In Django, performing NOT queries allows you to exclude certain records from the query results based on specific conditions. The NOT operator, represented by the tilde (~) when used in conjunction with the Django ORM's Q object, helps you construct compl…
Read more →

3 min read