• 第一个Python程序


    Hello World 程序

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

           print(”Hello World!“)

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

           localhost:~tang$ vim hello.py

           localhost:~tang$python hello.py

           Hello World!

    指定解释器

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

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

            #!/usr/bin/env python

             print ("Hello Word!")

    #!/usr/bin/env python 与#!/usr/bin/python区别

    前一个使用env,是查找系统中Python的环境变量,建议使用这种,而后一种是写死了执行路径,只找/usr/bin下的Python环境,会导致找不到环境而执行不成功,所以不建议使用!

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

    在交互其中执行

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

    H:>python
    Python 3.6.3 (v3.6.3:2c5fed8, Oct  3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)]
     on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> print("Hello World!")
    Hello World!
    >>>
    

     如在Linux下

     

    [tang@vm10-140-33-128 ~]$ python
    Python 2.6.6 (r266:84292, Jan 22 2014, 09:42:36) 
    [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> print ("Hello world!") 
    Hello world!
    >>> 
    

     对比其他语言的hello world

    C++

    #include <iostream>
    int main (void)
    {
    std::cout<<"Hello world";
    }
    

    C

    #include <stdio.h>
    int main (void)
    {
    printf("
    hello world!");
    return 0;
    }

    JAVA

    public class HelloWorld{
         //程序入口
         public static void main(String arge[]){
         // 向控制台输出信息
         System.out。println(”Hello World!“);
    }
    }
    

    PHP

    <?php
                       eho"hello world!";
    ?>
    

    RUBY

    puts "Hello World."
    

    GO

    package main
    
    import "fmt"
    
    func main(){
    
            fmt.printf("Hello World!
     God Bless You!");
    }
    

      

     

     

  • 相关阅读:
    Cocos2dx-demo演示项目:Part2
    利用Python抓取亚马逊评论列表数据
    Cocos2dx-demo演示项目:Part1
    正则表达式匹配原理
    js正则函数中test和match的区别
    【别人家的孩子系列之】浅析正则表达式—(原理篇)
    JS 获取浏览器窗口大小
    javascript的insertBefore、insertAfter和appendChild简单介绍
    javascript 限制字符串字数换行 带BUG
    一行一行分析JQ源码学习笔记-06
  • 原文地址:https://www.cnblogs.com/lzhn/p/7792727.html
Copyright © 2020-2023  润新知