1. hello world程序
  在linux下创建一个文件叫hello.py,并输入
  

print("Hello World!")

  然后执行命令:python hello.py,输出

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
[root@test-1 python]# vim hello.py
[root@test-1 python]# python hello.py 
Hello World!

  指定解释器

  上一步中执行python hello.py时,明确的指出hello.py脚本由python解释器来执行。
  如果想要类似于执行shell脚本一样执行python脚本,例如:./hello.py,那么就需要在hello.py文件的头部指定解释器,如下:

[root@test-1 python]# cat hello.py 
#!/usr/bin/env python #指定解释器
print("Hello World!")
[root@test-1 python]# chmod +x hello.py 
[root@test-1 python]# /python/hello.py 
Hello World!

  如此一来,执行:./hello.py 或/python/hello.py

  ps:执行前需给予hello.py执行权限,chmod 755 hello.py
在交互器中执行
  除了把程序写在文件里,还可以直接调用python自带的交互器运行代码,
  

[root@test-1 python]# python
Python 2.7.5 (default, Apr 11 2018, 07:36:10) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello world!")
hello world!

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄