Python3 解释器
阅读 (134) |
发布于 2020-05-11 18:42:13
Linux/Unix的系统上,一般默认的 python 版本为 2.x,我们可以将 python3.x 安装在 /usr/local/python3 目录中。
然后通过命令来启动python3
$ PATH=$PATH:/usr/local/python3/bin/python3 # 设置环境变量
$ python3 --version
Python 3.4.0
交互式编程
在命令行输入 python 来启动python解释器, 就可以进入到python的交互式编程环境
# python
Python 3.6.5 (default, Apr 9 2019, 17:44:18)
[GCC 4.8.2 20131212 (Red Hat 4.8.2-8)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello world")
hello world
>>> for i in range(3):
... print(i)
...
0
1
2
>>> istrue = True
>>> if istrue:
... print('yes')
...
yes
脚本式编程
vim test.py
#!/usr/bin/env python3
print("hello world!")
for i in range(3):
print(i)
istrue = True
if istrue:
print('yes')
然后通过解释器来执行
#python test.py
hello world!
0
1
2
yes
### 给脚本可执行权限
#chmod +x test.py
#./test.py
hello world!
0
1
2
yes