杰瑞科技汇

Python的hello world该怎么写?

Of course! The "Hello, World!" program is the traditional first program you write when learning a new language. Its purpose is to simply display the text "Hello, World!" on the screen.

Python的hello world该怎么写?-图1
(图片来源网络,侵删)

In Python, it's one of the simplest and most elegant examples possible.

The Code

Here is the complete "Hello, World!" program in Python:

print("Hello, World!")

How to Run It

You have two easy ways to run this code.

Method 1: Using the Python Interactive Interpreter (Great for quick tests)

  1. Open your terminal or command prompt.

    Python的hello world该怎么写?-图2
    (图片来源网络,侵删)
    • On Windows: Search for "Command Prompt" or "PowerShell".
    • On macOS or Linux: Open the "Terminal" application.
  2. Type python or python3 and press Enter. You should 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 print statement directly into the interpreter and press Enter.

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

Method 2: Using a Script File (The standard way)

This is the most common method for writing and running programs.

Python的hello world该怎么写?-图3
(图片来源网络,侵删)
  1. Create a file:

    • Open any plain text editor (like Notepad, VS Code, Sublime Text, etc.).
    • Save the file with a .py extension. For example, hello.py.
  2. Write the code:

    • Type the print statement into the file and save it.
      # hello.py
      print("Hello, World!")
  3. Run the script from your terminal:

    • Navigate to the directory where you saved your file using the cd command. For example: cd Desktop.
    • Run the file using the python command:
      $ python3 hello.py
      Hello, World!

Code Breakdown

Let's break down that single line of code:

print("Hello, World!")

  • print() is a 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, strings are enclosed in either double quotes () or single quotes ().
  • The parentheses are used to call the function. Anything you put inside the parentheses is called an argument, which is the input you are giving to the function. Here, you are giving the print() function the string "Hello, World!" as input.

So, in plain English, that line of code reads: "Call the print function with the argument Hello, World!."

Welcome to the world of Python! This is the first step in a very exciting journey.

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