Free Courses
certificate icon
Certificate

Learn on Scaler Topics and get certified.

static-certificate

Hello World Program in Python

Learn about Hello World Program in Python.

Updated - 11 Feb 20226 mins readPublished : 6 Jan 2022
Published : 6 Jan 2022
quiz
Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Overview

Python is a very powerful and widely-used programming language developed in the late 1980s by Guido van Rossum. Hello world is the first program written by most programmers. This article uses the in-built print function to write a hello world program in Python.

Introduction

If you ask programmers, what was their first program? Most of them would say the "Hello, World" program.

It is a simple program that prints a Hello, World! message on the screen.

In this article, you will begin your journey into programming by writing the same hello world Program in Python.

So, Let's begin.

Introduction to Python

Python is a very powerful, simple to learn, and widely-used programming language developed in the late 1980s by Guido van Rossum.

If you search on Wikipedia, you will find the following definition:

Python is an interpreted high-level, general-purpose programming language.

What does this mean?

Let's understand each of the three keywords:

  • High Level

    It is easier to understand and use when compared to a low-level language like C or Assembly.

  • Interpreted

    A Python program is executed line by line. It means that the first line is:

    • Read
    • Converted into machine code (binary or 0's and 1's).
    • Executed. And so on for all the lines in the code...

    It is different than languages like C or Java, which need to be compiled (fully translated) into machine code (0's and 1's) before they can be run.

  • General Purpose

    We can use Python in a wide variety of applications, including:

    • Data Science.
    • Web Development.
    • Machine Learning.
    • Bioinformatics.
    • Robotics and many more

Python is one of the best choices to get started with programming and software development.

It is easy to learn yet powerful enough to create complex software applications.

python used in a wide variety of applications

You can download and install Python for your preferred operating system using the official website.

Takeaway:

  • Python is an interpreted high-level, general-purpose programming language.

Creating a New Python Project

Now, you may be wondering where do I write the Python code?

You can write the code in any text editor (like notepad in windows)! There is no restriction on that, although it is highly recommended that you use a text editor for writing code or an IDE.

An IDE (Integrated Development Environment) is an application that combines all the tools needed for development in a single platform.

A code editor and IDE will provide you with features like:

  • Syntax Highlighting: highlighting different code elements with different colors.
  • Debugging: finding and resolving bugs in the code.
  • Autocompletion and many more.

I recommend using the free VS Code Editor as it has brilliant extensions to help you in development and is preferred by many developers.

If you want to use an IDE, you may go with PyCharm.

You may also use an online code editor like ide.new or vscode.dev.

Creating project directory

Now, open your editor and create a new folder named hello.

Inside that folder, create a new file named hello.py.

Every Python file ends with the .py extension.

A Python file is also called a Python script.

Takeaway:

  • We write code in a code editor or an IDE.
  • Every Python file ends in a .py extension.

How to Write Python Hello World Program

A hello world program is just a simple program that prints a Hello, World! message on the screen.

Now, we will write the hello world code inside the hello.py file:

print("Hello, World!")

Yes, That's it! Just a single line.

Python's beauty is that the code is primarily short, readable, and easy to understand (no jargon!). In comparison, a hello world program in C would have taken around five lines.

Explanation of the code

how to write python hello world program

The print is a built-in function in Python. It is used to display any message on the screen.

But wait, what is a function?

A function is like a black box that takes some input and returns an output by doing some processing.

python function input outout

A function is just a set of related statements that performs a task.

We can also write our function, but in this case, we are using one that Python provides by default.

A function receives input in parenthesis (), here we are sending "Hello, World". You can replace this with any text (or string as we call it in the programming world).

The text is written in either single quotes '' or double quotes "" to distinguish it from function names and other reserved keywords in the language.

Thus, when the program is run, the print function is "called" and outputs "Hello, World" on the screen.

Takeaway:

  • Print is a built-in function in Python used to display a message on the screen.

How to Run Python Hello World Program

Now, we will run the program using the Python interpreter.

The Python interpreter is a program that comes together with the Python language.

It reads our Python file line by line and executes each line of the code.

We can invoke the interpreter using the python command in the terminal (or PowerShell in Windows):

python hello.py

Note 📝:

  • Depending on your Python version and how your computer is set up, you may have to type python3 instead of python before the file name.
  • Make sure you navigate the right path inside the terminal using the cd (change directory) command.
    • If you are using VS Code, you can easily access the terminal from the menu.

Output:

Hello, World!

Yay! you just ran your first Python program.

Try changing "Hello, World!" to something else and rerun the program.

Takeaway:

  • A Python program is executed by the Python interpreter using the python command in the terminal.

Running Python in the Interpreter

As Python is an interpreted language, we can run it without creating a file using the Python interpreter.

Type python (or python3) in the terminal and press enter.

We will present you with an interpreter like this:

• yuvraajsj18@Inspiron-5580 /mnt/c/Users/yuvra python3
Python 3.8.10 (default, Sep 28 2021, 16:10:42) 
[GCC 9.3.0] on linux 
Type "help", "copyright", "credits" or "license" for more information. 
>>>

Now, you can run any python command by writing it after the >>> symbol.

Let's quickly write the hello, world line inside the interpreter:

>>> print("Hello, World!")
Hello, World!
>>>

Voila! Same code and output as before.

It can be especially helpful for experimenting with some code or function. But we must use a Python file when we write large, complex programs.

What next?

Now, you have a basic understanding of Python. I encourage you to move forward in your journey by diving deep into more topics like:

  • Conditionals (Using if/else).
  • Functions.
  • Loops.

It will get more interesting as you learn more and more!

Happy Coding 👩‍💻!

Takeaway:

  • We can also run Python code in the interpreter directly without creating a file.

Conclusion

  • Python is an interpreted high-level, general-purpose programming language.
  • It was created in the late 1980s by Guido van Rossum.
  • Python is used in many domains like data science and web development.
  • We use a code editor, IDE, or online code editor to write Python programs.
  • A python file has a .py extension known as a Python script.
  • We use the print function to display text on the screen.
  • A Python program is executed by the Python interpreter using the python command in the terminal.
  • We can also run Python code in the interpreter directly without creating a file.
Challenge Time!
quiz
quiz
Time to test your skills and win rewards! Note: Rewards will be credited after the next product update.
Free Courses by top Scaler instructors