VMware ESXi and vSphere Cluster Management
Write Your First Python Program: Hello, World!
Learn to use Python IDLE to enter, save, and run your first program with print(), strings, comments, and a .py source file.
Python is a programming language used to write and run programs. A common first exercise is a Hello, World! program: a tiny program that displays a greeting. This convention gives you a quick way to confirm that your programming environment works and introduces the basic cycle of writing and running code.
In this lesson, you will first display text in the Python shell. Then you will place the same code in an editor window, save it as a Python source file, and run it as a program.
Start the Python IDLE shell
IDLE is Python's beginner-oriented integrated development environment. It includes an interactive Shell window and an editor window for saved programs.
When IDLE opens, it normally displays the Shell window. Look for the Python prompt, which usually appears as three greater-than signs:
>>>The prompt is where you enter Python commands. IDLE runs each command immediately after you press Enter, and the command's output appears below it. This immediate, one-command-at-a-time style is called interactive mode.
Display text with print()
print() is a built-in Python function for displaying output. A function is a named operation that you call with parentheses. The value inside the parentheses is supplied to the function.
Text in Python is called a string. A string must be surrounded by matching quotation marks:
print('Hello, World!')Here, print is the function name, and 'Hello, World!' is a string. Python displays the text inside the quotation marks, but it does not display the quotation marks themselves.
Run Hello, World! interactively
Click after the Python prompt in the Shell window, type the command, and press Enter:
>>> print('Hello, World!')
Hello, World!The line beginning with >>> is the input command. The next line is the output produced by the command. A new prompt then appears so you can enter another command.
For another example, try:
>>> print('I am learning Python.')
I am learning Python.Use single and double quotes
For a simple string, you may use matching single quotes or matching double quotes. These two commands produce the same result:
| Quote style | Example | Result |
|---|---|---|
| Single quotes | print('Hello, World!') | Hello, World! |
| Double quotes | print("Hello, World!") | Hello, World! |
The opening and closing quotation marks must match. Do not open with a single quote and close with a double quote:
print('Hello, World!")The mismatched version causes a SyntaxError, which means Python cannot understand the code's structure. Correct it by using either two single quotes or two double quotes.
Create a Python source file in IDLE
The Shell window is useful for quick experiments, but commands entered there are not automatically preserved as a program. To retain code for later use, write it in an IDLE Editor window.
- In IDLE, choose File > New File.
- A new editor window opens. Enter this program in the editor:
# My first Python program
print('Hello, World!')The editor lets you write and revise several lines of code before running them. Unlike the Shell, typing a line in the editor does not execute it immediately.
Save the program
A file containing Python instructions is a source file. Python source files conventionally use the .py extension.
- In the editor window, choose File > Save.
- Choose a location that is easy to find, such as your beginner practice folder.
- Enter a simple filename such as
hello_world.py. - Confirm that the filename ends in
.py.
Use a descriptive filename made from letters, numbers, and underscores. Avoid names that match standard Python modules, such as string.py or random.py, because those names can interfere with Python's ability to find the standard modules later.
Run the saved module in IDLE
In IDLE, a saved .py source file can be run as a module. A module is a Python source file that can be run or imported.
- Make sure the editor window containing
hello_world.pyis active. - Save any recent changes with File > Save.
- Choose Run > Run Module.
IDLE executes the saved file and returns to the Shell window. You may see a restart indicator before the program's output, such as:
>>> ================================ RESTART ================================
Hello, World!The restart message is normal when IDLE prepares the Shell to run a module. Look at the lines after the indicator for your program's output.
Shell commands versus saved programs
| Feature | Shell window | Editor window |
|---|---|---|
| Primary purpose | Quick experimentation and immediate results | Writing and editing a saved Python program |
| Whether code runs immediately | Yes, after you press Enter | No; save it and choose Run > Run Module |
| Whether code is saved automatically | No | No; use File > Save |
| Typical beginner use | Try a function or small expression | Keep, revise, and rerun a complete exercise |
Interactive mode is convenient when you want to test one idea quickly. A source file is better when you want to retain a program, run it again, or build on it later.
Add comments to your program
A comment is an explanatory note for people reading source code. Python ignores comments when it runs the program, so comments do not produce output.
The hash character, #, starts a single-line Python comment. The comment can appear on its own line above code or after a statement:
# Display a greeting
print('Hello, World!') # The greeting appears in the ShellUse print() when you want visible output. Use comments to explain the purpose of code or leave notes for yourself and other programmers.
Get help in IDLE
IDLE's Help menu provides access to Python documentation and assistance. Use it when you encounter an unfamiliar function, keyword, or piece of syntax. Reading documentation is a normal part of programming: it helps you learn what an operation does, what arguments it accepts, and how to use it correctly.
Troubleshooting
A SyntaxError appears around the text
The string may be missing quotation marks or may use mismatched quote styles. Put the text inside matching single quotes or matching double quotes:
print('Hello, World!')
print("Hello, World!")Nothing happens when code is typed in the editor
Writing code in the editor does not execute it immediately. Save the file with File > Save, then choose Run > Run Module. Check the Shell window for the result.
The file is not clearly recognized as Python code
The filename may not end with .py. Save or rename the source file so it has a name such as hello_world.py.
The output is not visible in the editor
IDLE sends program output to the Shell window, not the editor window. Return to the Shell and look below the restart indicator.
A restart message appears before the output
This is normally part of IDLE's module-running process, not an error. Look at the following lines for the expected output or for an actual error message.
A comment produces no output
That is expected. Python ignores comments. Add a print() statement if you want text to appear in the Shell.
Key points
- IDLE opens with an interactive Shell window and a Python prompt.
- Commands entered at the prompt run immediately.
print()displays a supplied value, such as a string.- Strings need matching single or double quotation marks.
- Use File > New File to open an editor window.
- Save Python source code with the
.pyextension. - Use Run > Run Module to run a saved file and view its output in the Shell.
- Use the Shell for quick experiments and source files for programs you want to retain.
- Comments begin with
#and are ignored by Python.
Next, you can build on this program by storing text in variables, learning more about strings and data types, and accepting user input with input().