Hello World Program in Python Language

Introduction to Hello World Program in Python Programming Language

In this tutorial, you will learn how to create a straightforward ‘Hello World’ program using the Python programming

language. This program will utilize Python’s built-in print() function to display the message ‘Hello, world!’ on your screen.

The ‘Hello World’ program is a fundamental piece of computer code written in a versatile programming language. It serves as a test program, designed to perform a simple task without requiring any user input. Its sole purpose is to output a ‘Hello World’ message to the console. This program is commonly used to verify whether the necessary software for compiling and executing code has been correctly installed.

Running the Hello World Program in Command Prompt

Displaying the “Hello World” message using the Python interpreter is a straightforward process. You can execute the Python interpreter from the command terminal of your Windows operating system or a Linux system and issue the print statement to achieve this. Here’s how to do it:

On Windows:

  1. Open a command prompt terminal.
  2. Type python and press Enter. This will launch the Python interpreter.
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.
  1. At the Python prompt (>>>), enter the following command and press Enter:
>>> print("Hello World")
Hello World

On Linux:

  1. Open a terminal.
  2. Type python3 and press Enter. This will launch the Python 3 interpreter.
$ 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.
  1. At the Python prompt (>>>), enter the following command and press Enter:
>>> print("Hello World")
Hello World

Running the Hello World Program as a Script

The Python interpreter also works in script mode. You can create a Python script by opening a text editor, entering the following code, and saving it as “Hello.py”:

print("Hello World")

On Windows:

  1. Open the command prompt terminal (CMD).
  2. Navigate to the directory where you saved “Hello.py.”
  3. Run the program using the following command:
C:\>python hello.py

This will display the following output:

Hello World

On Linux:

  1. Open a terminal.
  2. Navigate to the directory where you saved “Hello.py.”
  3. Run the program using the following command:
$ 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 #!, which must contain the path to the Python executable. Python is typically installed in the /usr/bin directory in Linux, and the executable is named python3. Here’s how to add this statement to the “hello.py” file:

#!/usr/bin/python3

print("Hello World")

Additionally, you need to give the file executable permission using the chmod +x command:

$ chmod +x hello.py

Then, you can run the program with the following command:

$ ./hello.py

This will display the following output:

Hello World

Discover more from PiEmbSysTech

Subscribe to get the latest posts sent to your email.

Leave a Reply

Scroll to Top

Discover more from PiEmbSysTech

Subscribe now to keep reading and get access to the full archive.

Continue reading