zoukankan      html  css  js  c++  java
  • 第一个Python程序

    一、准备工作

    Python安装方法参考:http://www.cnblogs.com/oyoui/p/8316949.html

    视频详解:http://list.youku.com/albumlist/show/id_28961509.html?from=singlemessage

    Linux、Mac无需安装,使用自带Python环境。

    二、Hello World程序

    Hello World程序基本上是绝大多数语言学习时的第一个程序,Python也不例外。

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

    1 print("Hello World!")

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

    1 localhost:~ ruan$ vim hello.py
    2 localhost:~ ruan$ python hello.py
    3 Hello World!

    指定解释器

    上一步中执行 python hello.py 时,明确的指出 hello.py 脚本由 python 解释器来执行。

    如果想要类似于执行shell脚本一样执行python脚本,例:hello.py,那么就需要在 hello.py 文件的头部指定解释器,如下:   

    1 #!/usr/bin/env python
    2   
    3 print "hello,world"

    如此一来,执行: ./hello.py 即可。

    PS:执行前需给予 hello.py 执行权限,chmod 755 hello.py

    在交互器中执行 

    除了把程序写在文件里,还可以直接调用python自带的交互器运行代码 

    Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:25:58) [MSC v.1500 64 bit (AMD64)] on win32
    Type "copyright", "credits" or "license()" for more information.
    >>> print("Hello World!")
    Hello World!
    >>> 

    PS:此为Windows下python自带的交互器。

      

          

  • 相关阅读:
    133
    132
    131
    130
    129
    128
    2019.10.16考试解题报告
    2019.10.15考试解题报告
    洛谷 P1352 没有上司的舞会
    2019.10.13考试解题报告
  • 原文地址:https://www.cnblogs.com/gyf9527/p/8570514.html
Copyright © 2011-2022 走看看