Of course! Here is the classic "Hello, World!" program in Python, along with a detailed explanation.

The Short Answer (The Code)
This is the simplest way to print "Hello, World!" to your screen.
print("Hello, World!")
How to Run It
You have a couple of easy options to run this code.
Option 1: Using the Python Interactive Interpreter (REPL)
This is great for quick tests.
-
Open your terminal (also called a command prompt, shell, or console).
(图片来源网络,侵删)- On Windows: Search for "Command Prompt" or "PowerShell".
- On macOS: Search for "Terminal".
- On Linux: Open your distribution's terminal application.
-
Start the Python interpreter by typing
pythonorpython3and pressing Enter. You'll see a>>>prompt.$ python3 Python 3.10.4 (main, Jun 29 2025, 12:14:53) [GCC 11.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>>
-
Type the code and press Enter. The output will appear immediately.
>>> print("Hello, World!") Hello, World! >>> -
To exit the interpreter, type
exit()or pressCtrl+D.
Option 2: Using a Python File (The Standard Way)
This is the method you'll use for writing real programs.
-
Open a text editor (like VS Code, Sublime Text, Notepad, or even just Notepad on Windows).
-
Type the code into the file and save it with a
.pyextension. Let's name ithello.py.# hello.py print("Hello, World!") -
Run the file from your terminal. Navigate to the directory where you saved the file and use the
pythoncommand.# First, navigate to the folder where you saved hello.py # (This command might be different on your system) cd /path/to/your/folder # Now, run the file $ python3 hello.py Hello, World!
Code Breakdown
Let's break down the single line of code: print("Hello, World!")
print()is a built-in function in Python. A function is a reusable block of code that performs a specific action. Theprint()function's job is to display output to the console."Hello, World!"is a string. A string is simply a sequence of text. In Python, you define a string by enclosing it in either double quotes () or single quotes ().- The parentheses are used to call a function and pass arguments to it. Here, we are passing the string
"Hello, World!"as the argument to theprint()function.
So, in plain English, the line print("Hello, World!") tells the Python interpreter: "Execute the print function, and give it the text Hello, World! to display."
Variations (for learning)
Here are a few other ways to write "Hello, World!" that can teach you more about Python.
Using Single Quotes
Strings can also be defined with single quotes. This is functionally identical.
print('Hello, World!')
Printing Multiple Things
You can print multiple items by separating them with a comma. Python will automatically add a space between them.
print("Hello,", "World!")
# Output: Hello, World!
Using a Variable
This shows you how to store data in a variable and then use it.
# Store the string in a variable called 'message' message = "Hello, World!" # Print the value stored in the variable print(message)
Welcome to the world of Python! This simple program is the traditional first step for learning any new programming language.
