时间:2025-03-22 14:12
人气:
作者:admin
Python 的安装过程非常简单,以下是针对不同操作系统的详细步骤:
使用 Homebrew 安装
brew install python3
系统预装了 Python 2,建议通过 python3 命令使用新版本
大多数发行版已预装 Python 3,可通过以下命令验证:
python3 --version
若需更新版本(以 Debian/Ubuntu 为例):
sudo apt-get update && sudo apt-get install python3
打开文本编辑器,输入以下代码并保存为 hello.py:
import datetime # 导入日期时间模块
print("Hello World!") # 输出经典问候语
print("now time:", datetime.datetime.now().strftime("%Y年%m月%d日 %H:%M:%S"))
# 获取当前时间并格式化为中文格式
import datetime 语句用于导入 Python 的内置模块 datetime,它提供了处理日期和时间的功能。print("Hello World!"):Python 中最经典的输出语句,用于向控制台输出文本。datetime.datetime.now():调用模块中的方法获取当前时间。.strftime("%Y年%m月%d日 %H:%M:%S"):
%Y 表示四位数年份,%m 表示两位数月份,%d 表示日期;%H 表示 24 小时制小时,%M 表示分钟,%S 表示秒。在终端中切换到代码保存的目录,执行以下命令:
python3 hello.py # Windows 系统可能需要使用 python hello.py
输出结果:
Hello World!
now time: 2025年03月22日 14:30:45 # 实际时间以运行时为准
细节决定成败!
个人愚见,如有不对,恳请斧正!
上一篇:力扣第268题-丢失的数字
下一篇:力扣第283题-移动0