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:
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 --version Python 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
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.
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.
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
Now let's look at another more preferred way of running Python programs in your machine.
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 Python interactive shell
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.
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 -
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.
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 from djangocentral