码迷,mamicode.com
首页 > 编程语言 > 详细

python-基础

时间:2018-10-14 13:54:33      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:ati   python脚本   命令   red hat   usr   程序   文件   info   root   

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

print("Hello World!")

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

[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!

 

python-基础

标签:ati   python脚本   命令   red hat   usr   程序   文件   info   root   

原文地址:https://www.cnblogs.com/scajy/p/9785628.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!