Python Tutorial

Hello World Program in Python

Write and run your first Python Hello World program, understand print(), comments, strings, indentation, functions, and common beginner mistakes.

What is the Hello World program?

Hello World is a small first program that displays a message. It is useful because beginners can write, run, and understand it without learning variables, classes, or external packages first.

Write your first Python program

Create a file named hello.py and add the following code:

print("Hello, World!")

Run the file from a terminal with python hello.py. On some systems, the command is python3 hello.py. The program prints:

Output
Hello, World!

How does the program work?

  • print(): A built-in Python function that writes values to the standard output.
  • "Hello, World!": A string literal containing the text passed to the function.
  • Parentheses: They contain the value supplied as the function argument.
  • New line:print() normally moves the cursor to the next line after displaying the value.

Add a comment

A comment begins with the # character. Python ignores the comment while running the program, so comments are useful for explaining intent:

# Display a welcome message
print("Hello, World!")

Print a message stored in a variable

Instead of passing the string directly, store it in a variable and print the variable:

message = "Hello, World!"
print(message)

Python determines the value’s type at runtime. Here, message refers to a string.

Use a function

A function gives a piece of behavior a reusable name. The indented line belongs to the function body:

def show_message():
    print("Hello, World!")

show_message()

The function is defined first and then called with show_message().

Print several values

print() accepts multiple values and separates them with spaces by default:

name = "Asha"
language = "Python"
print("Hello", name, "Welcome to", language)

Important beginner concepts

  • Indentation: Use consistent spaces to define blocks after statements such as def, if, and loops. Four spaces are the common convention.
  • Case sensitivity:print and Print are different names; only the lowercase built-in is correct.
  • File extension: Save Python source files with the .py extension.
  • Virtual environments: Use python -m venv .venv to isolate project packages when a project grows beyond the standard library.
  • Style: Use meaningful names, short functions, and consistent formatting. PEP 8 is the commonly followed Python style guide.

Common mistakes

  • Writing Print() instead of the case-sensitive print().
  • Leaving a quotation mark open in a string.
  • Running the command from a directory that does not contain hello.py.
  • Using inconsistent indentation when the code contains a function or another block.

Next steps

After Hello World, practice variables, data types, user input, operators, conditions, loops, and functions. These concepts form the foundation for useful Python scripts and applications.

Further Reading

Continue learning with these related Python tutorials:

Continue learning