当前位置: 移动技术网 > IT编程>脚本编程>Python > python-基础

python-基础

2018年10月14日  | 移动技术网IT编程  | 我要评论

邓小喵,黄游戏网站,无锡市委副书记跳塔

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!

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网