
上QQ阅读APP看本书,新人免费读10天
设备和账号都新为新人
2.2.2 基本程序编写
1.Python的环境变量
在Linux/Unix操作系统下,一般默认Python版本为2.x,可以将Python 3.x安装在/usr/local/python3目录中。安装完成后,可以将路径/usr/local/python3/bin添加到Linux/Unix操作系统的环境变量中,这样就可以通过shell终端输入“python3”命令来启动Python 3。
在Windows操作系统下,可以通过以下命令来设置Python的环境变量,假设Python安装在 C:\python3目录下,如图2-4设置环境变量。

图2-4 Windows操作系统的环境变量设置
2.交互式编程
可以在命令提示符中输入“python”命令来启动Python解释器,进入交互式编程。
在Python提示符中,输入以下语句,然后按回车键查看运行效果:

当键入一个多行结构时,续行是必需的。如图2-5所示,if 语句是多行代码。

图2-5 多行代码图
如果需要退出交互式编程,输入exit()函数或quit()函数即可。
3.脚本式编程
将如下代码复制至hello.py文件:

通过python hello.py命令执行该脚本,输出结果为“Hello,Python!”。