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.
we are assuming you already have Python installed in your machine, 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\Ahmed-PC> python --version
In this article, I will show you how to write the Hello World program in python 3 in different ways. We will consider the following two ways :
Run Hello World program in Python interactive shell
An interactive 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.
However, this should work on Mac os and Linux if it doesn’t let me know in the comments below. To run Hello world in Python, you have to type the following command in the Python interactive shell.
-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.
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 –
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 the Python interactive shell.123print("Hello world")
- 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.
Next Recommended Article: How To Write Comments In Python