2、Python解释器

文章目录

  • Python解释器
    • 交互式编程
    • 脚本式编程

Python解释器

  • Linux/Unix的系统上,Python解释器通常被安装在/usr/local/bin/python3.x这样的有效路径(目录)里。
  • 我们可以将路径/usr/local/bin添加到您的Linux/Unix操作系统的环境变量中,这样您就可通过shell 终端输入下面的命令来启动 Python 。
python3.x
  • 在Windows系统下你可以通过以下命令来设置Python的环境变量,假设你的Python安装在C:\Python36下:
set path=%path%;C:\python36

交互式编程

  • 我们可以在命令提示符中输入“Python”命令来启动Python解释器:
python
  • 执行以上命令后,出现如下窗口信息:
C:\Users\Administrator>python
Python 3.6.3 |Anaconda custom (64-bit)| (default, Oct 15 2017, 03:27:45)
1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
  • 在python提示符中输入以下语句,然后按回车键查看运行效果:
print('hello Python!')
  • 以上命令执行结果为:
hello Python!
  • 当键入一个多行结构时,续行是必须的,我们可以看如下if语句:
>>> the_world_is_flat = True
>>> if the_world_is_flat:
...    print('Be careful not to fall off!')
...
Be careful not to fall off!
>>>

脚本式编程

  • 将如下代码拷贝至hello.py文件中:
print('hello Python!')
  • 通过以下命令执行该脚本:
python hello.py
  • 输出结果为:
hello Python!
  • 在Linux/Unix系统中,你可以在脚本顶部添加以下命令让Python脚本可以向SHELL脚本一样可直接执行:
#! /usr/bin/env python3.6
  • 然后修改脚本权限,使其有执行权限,命令如下:
chmod 755 hello.py
  • 执行以下命令:
./hello.py
  • 输出结果为:
hello Python!

你可能感兴趣的:(Python)