经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Python » 查看文章
python-基础
来源:cnblogs  作者:七月流星雨  时间:2018/10/15 9:26:13  对本文有异议

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

  1. print("Hello World!")

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

  1. [root@test-1 python]# vim hello.py
  2. [root@test-1 python]# python hello.py
  3. Hello World!

  指定解释器

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

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

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

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

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

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号