杰瑞科技汇

Python的Hello World程序怎么写?

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

Python的Hello World程序怎么写?-图1
(图片来源网络,侵删)

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.

  1. Open your terminal (also called a command prompt, shell, or console).

    Python的Hello World程序怎么写?-图2
    (图片来源网络,侵删)
    • On Windows: Search for "Command Prompt" or "PowerShell".
    • On macOS: Search for "Terminal".
    • On Linux: Open your distribution's terminal application.
  2. Start the Python interpreter by typing python or python3 and 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.
    >>>
  3. Type the code and press Enter. The output will appear immediately.

    >>> print("Hello, World!")
    Hello, World!
    >>>
  4. To exit the interpreter, type exit() or press Ctrl+D.

Option 2: Using a Python File (The Standard Way)

This is the method you'll use for writing real programs.

  1. Open a text editor (like VS Code, Sublime Text, Notepad, or even just Notepad on Windows).

  2. Type the code into the file and save it with a .py extension. Let's name it hello.py.

    # hello.py
    print("Hello, World!")
  3. Run the file from your terminal. Navigate to the directory where you saved the file and use the python command.

    # 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. The print() 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 the print() 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.

分享:
扫描分享到社交APP
上一篇
下一篇