Python online course

Write Your First Python Program: Hello, World!

Learn to run Hello, World! interactively, create a Python script, save it as hello.py, and run it with Python IDLE.

Python is a programming language used to write and run programs. A program is a set of instructions that Python executes. In this lesson, you will create a traditional first program: Hello, World! Its goal is simple—display text on the screen.

You will use IDLE, Python's basic integrated development environment. IDLE includes an interactive Shell for trying commands and an editor for writing saved programs.

Start Python IDLE

Open IDLE from your operating system's applications menu. IDLE normally opens with a Shell window. The Shell is an interactive window where you enter one Python command at a time and see the result immediately.

The Shell displays startup information, such as the Python version and platform. This information is produced when Python starts; it is not code that you need to type. Look for the prompt, usually three greater-than signs:

>>>

The prompt is a marker showing that Python is ready to accept a command. This way of working is called interactive mode: you enter a command, press Enter, and receive a result immediately.

Use the print() function

A function is a named operation that performs a task. print() is a built-in Python function for displaying values or text as output.

Place the text you want to display inside the function's parentheses. Text is represented by a string, which is text enclosed in quotation marks. Ordinary strings can use either single quotes or double quotes:

print('Hello, world!')

At the Shell prompt, type the command and press Enter:

>>> print('Hello, world!')
Hello, world!
>>>

The line beginning with >>> is the command you entered. The next line is the output produced by Python. Afterward, the prompt appears again, so the Shell remains available for another command.

Single and double quotes

Single quotes and double quotes both delimit a basic string. The opening and closing quotes must match.

Code form | Valid? | Why

print('Hello, world!') | Yes | Uses matching single quotes

print("Hello, world!") | Yes | Uses matching double quotes

print('Hello, world!") | No | Uses mismatched string delimiters

These two valid commands display the same output:

print('Hello, world!')
print("Hello, world!")

For more about text values, read about Python strings.

Shell window versus editor window

Interactive Shell commands are useful for quick experiments. However, a command typed into the Shell is not the same as maintaining a reusable program file. If you want to reopen, edit, and run your code later, save it in a Python source file.

Source code is the human-readable Python instructions you write. A saved source file containing Python instructions is called a script. IDLE's editor window is where you write and modify that file.

IDLE component | Primary use | Where results appear

Shell window | Test individual commands interactively | Immediately below the entered command

Editor window | Write, save, and revise a program file | Program results are sent to the Shell

Create a Python file in IDLE

  1. In the IDLE Shell, select File > New File.
  2. IDLE opens a separate editor window. This is where you write a multi-line program.
  3. Enter the following code in the editor:
# Display a greeting
print('Hello, world!')

The two lines have different roles. The first is a comment. The second calls print() to display the greeting.

Save the program

  1. In the editor window, select File > Save.
  2. Choose a location where you can easily find the file.
  3. Enter a descriptive filename such as hello.py.
  4. Confirm the save operation.

The .py filename extension identifies a Python source file. IDLE requires the editor contents to be saved before you can run the file as a module.

A module is a Python file that can be run or imported. When IDLE runs a saved file, its Run menu uses the term “module.”

Run the saved program

  1. Make sure the hello.py editor window is active.
  2. Select Run > Run Module.
  3. If IDLE asks you to save the file, save it before continuing.
  4. Switch to the Shell window to view the result.

The output appears in the Shell, not inside the editor:

===== RESTART =====
Hello, world!

IDLE may show a restart indicator before the program output. This means IDLE restarted the Shell's execution environment before running the script. The indicator is normal; it is not part of your program's output.

The editor keeps the source code, while the Shell displays what the running program produces. You can return to the editor, revise the script, save it, and run the module again.

Add comments

A comment is a note for people reading source code. Python does not execute a comment. A single-line comment begins with #:

# Display a greeting
print('Hello, world!')

The comment explains the purpose of the print() line and does not appear in the output. You can also place a comment after executable code, but a # placed before code comments out the rest of that line.

# Python ignores this entire line
print('This line runs')

Learn more in Python comments.

Get help in IDLE

IDLE includes help and documentation features for looking up Python functions and language features. When you need more detail about a name such as print, use IDLE's Help capability to search the documentation or open the relevant reference information.

Use help as a reference while experimenting: look up what a function does, check its arguments, and then try a small example in the Shell. For a related introduction, see Python help mode.

Troubleshooting

The command does not execute

Check that the cursor is at the Shell prompt, enter the print() command, and press Enter. Typing a command without pressing Enter does not execute it.

Python reports a syntax error

Usually, a quote is missing or mismatched, or a parenthesis is incomplete. Use matching quotes and both opening and closing parentheses:

print('Hello, world!')

Text is treated as an undefined name

If you enter text without quotation marks, Python interprets the words as names rather than as text. Put the text inside single or double quotes:

print('Hello, world!')

The file cannot be run as a module

Save the editor file first. Use File > Save and ensure the filename ends in .py, then choose Run > Run Module.

Output is missing from the editor

IDLE sends program output to the Shell window. After selecting Run > Run Module, switch to the Shell and look below the restart indicator.

A comment seems to stop the code

A # comments out the rest of its line. Remove the # from code that should run, or put the explanatory comment on its own line.

Your complete first program

# Display a greeting
print('Hello, world!')

Save this code as hello.py, then run it with Run > Run Module. The Shell should display:

Hello, world!

Key points to remember

  • A program is a set of instructions that Python executes.
  • print() displays output.
  • Text strings go inside matching single or double quotation marks.
  • Press Enter to execute a command entered at the Shell prompt.
  • Use the Shell for quick experiments and an editor file for reusable code.
  • Save Python source files with the .py extension before running them as IDLE modules.
  • Run a saved file with Run > Run Module; its output appears in the Shell.
  • Comments begin with # and are ignored by Python.

After this exercise, you can continue with running Python code or explore the Python overview.