Python - Hello World Program


This tutorial will teach you how to write a simple Hello World program using Python Programming language. This program will make use of Python built-in print() function to print the string Hello, world! on our screen.

The Hello World program is a basic computer code written in a general purpose programming language, used as a test program. It doesn't ask for any input and displays a Hello World message on the output console. It is used to test if the software needed to compile and run the program has been installed correctly.

Hello World Program at Command Prompt

It is very easy to display the Hello World message using the Python interpreter. Launch the Python interpreter from a command terminal of your Windows Operating System and issue the print statement from the Python prompt as follows −

PS C:\> python
Python 3.11.2 (tags/v3.11.2:878ead1, Feb 7 2023, 16:38:35) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

>>> print ("Hello World")
Hello World

Similarly, Hello World message is printed on Linux System.

$ python3
Python 3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> print ("Hello World")
Hello World

Hello World Program using Script

Python interpreter also works in scripted mode. Open any text editor, enter the following text and save as Hello.py

print ("Hello World")

For Windows OS, open the command prompt terminal (CMD) and run the program as shown below −

C:\>python hello.py

This will display the following output

Hello World

To run the program from Linux terminal

$ python3 hello.py

This will display the following output

Hello World

Using Shebang #! in Linux Scripts

In Linux, you can convert a Python program into a self executable script. The first statement in the code should be a shebang #!. It must contain the path to Python executable. In Linux, Python is installed in /usr/bin directory, and the name of the executable is python3. Hence, we add this statement to hello.py file

#!/usr/bin/python3

print ("Hello World")

You also need to give the file executable permission by using the chmod +x command

$ chmod +x hello.py

Then, you can run the program with following command line −

$ ./hello.py

This will display the following output

Hello World

Thus, we can write and run Hello World program in Python using the interpreter mode and script mode.

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements