Python 简明教程
Python - Hello World Program
本教程将教您如何使用 Python 编程语言编写一个简单的 Hello World 程序。此程序将利用 Python 内置的 print() 函数来打印字符串。
This tutorial will teach you how to write a simple Hello World program using Python Programming language. This program will make use of Python built-in print() function to print the string.
Hello World Program in Python
打印 "Hello World" 是 Python 中的第一个程序。此程序不会接受任何用户输入,它只会输出到屏幕上。用于测试已正确安装编译和运行程序所需的软件。
Printing "Hello World" is the first program in Python. This program will not take any user input, it will just print text on the output screen. It is used to test if the software needed to compile and run the program has been installed correctly.
Steps
以下是如何编写一个 Python 程序来打印 Hello World –
The following are the steps to write a Python program to print Hello World –
-
Step 1: Install Python. Make sure that Python is installed on your system or not. If Python is not installed, then install it from here: https://www.python.org/downloads/
-
Step 2: Choose Text Editor or IDE to write the code.
-
Step 3: Open Text Editor or IDE, create a new file, and write the code to print Hello World.
-
Step 4: Save the file with a file name and extension ".py".
-
Step 5: Compile/Run the program.
Python Program to Print Hello World
# Python code to print "Hello World"
print ("Hello World")
在以上代码中,我们写了两行。第一行是注释,将被解释器忽略,第二行是 print() 语句,将输出屏幕上给定的消息(“Hello World”)。
In the above code, we wrote two lines. The first line is the Python comment that will be ignored by the Python interpreter, and the second line is the print() statement that will print the given message ("Hello World") on the output screen.
Output
Hello World
Different Ways to Write and Execute Hello World Program
Using Python Interpreter Command Prompt Mode
使用 Python 解释器非常容易地显示 Hello World 消息。从 Windows 操作系统的命令终端启动 Python 解释器,然后从 Python 提示符发出 print statement ,如下所示 −
It is very easy to display the Hello World message using the Python interpreter. Launch the Python interpreter from a command terminal of your Windows Operating System and issue the print statement from the Python prompt as follows −
PS C:\> python
Python 3.11.2 (tags/v3.11.2:878ead1, Feb 7 2023, 16:38:35) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print ("Hello World")
Hello World
类似地,Hello World 消息通过 Linux 系统打印。
Similarly, Hello World message is printed on Linux System.
$ python3
Python 3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print ("Hello World")
Hello World
Using Python Interpreter Script Mode
Python 解释器还在脚本模式下工作。打开任何文本编辑器,输入以下文本并保存为 Hello.py
Python interpreter also works in scripted mode. Open any text editor, enter the following text and save as Hello.py
print ("Hello World")
对于 Windows 操作系统,打开命令提示符终端 (CMD),然后按如下所示运行程序 −
For Windows OS, open the command prompt terminal (CMD) and run the program as shown below −
C:\>python hello.py
这将显示以下输出
This will display the following output
Hello World
从 Linux 终端运行程序
To run the program from Linux terminal
$ python3 hello.py
这将显示以下输出
This will display the following output
Hello World
Using Shebang
在 Linux 中,您可以将 Python 程序转换为一个可执行脚本。代码中的第一条语句应为井号 * #!*。它必须包含 Python 可执行文件的路径。在 Linux 中,Python 安装在 /usr/bin 目录中,可执行文件名为 python3。因此,我们往 hello.py 文件中添加该语句
In Linux, you can convert a Python program into a self executable script. The first statement in the code should be a shebang * #!*. It must contain the path to Python executable. In Linux, Python is installed in /usr/bin directory, and the name of the executable is python3. Hence, we add this statement to hello.py file
#!/usr/bin/python3
print ("Hello World")
您还需要使用 chmod +x 命令为文件授予可执行权限
You also need to give the file executable permission by using the chmod +x command
$ chmod +x hello.py
接着,您可以运行带有以下命令行的程序:
Then, you can run the program with following command line −
$ ./hello.py
这将显示以下输出
This will display the following output
Hello World
FAQs
1. Why the first program is called Hello World?
只是一个简单的程序,用于测试 basic syntax 和 Python programming language 的编译器/解析器配置。
It is just a simple program to test the basic syntax and compiler/interpreter configuration of Python programming language.
2. Installation of Python is required to run Hello World program?
是的。需要安装 Python 来运行 Hello World 程序。
Yes. Python installation is required to run Hello World program.
3. How do I run a Python program without installing it?
TutorialsPoint 开发了一个在线环境,您可以在其中运行您的代码。您可以使用 Python online compiler 来运行您的 Python 程序。
TutorialsPoint developed an online environment where you can run your codes. You can use the Python online compiler to run your Python programs.