zoukankan      html  css  js  c++  java
  • 2-python学习——hello world

    "hello world"是编程界一个经久不衰的例子,几乎所有语言的学习教程都把它当做第一个程序的范例。学习的过程就是再造轮子的过程,千万不要以为有人做过的,就不去学习了。

    hello world

    我们先打开CPython解释器。

    o@o-pc:~$ python2.7
    Python 2.7.10 (default, Jun 17 2015, 14:15:05) 
    [GCC 4.9.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 
    

    打开之后就可以在>>>的后面输入python语句了。
    我们先试一下print这个命令,看是否成功输出"hello world"

    >>> print "hello world"
    hello world
    

    因为这是在python2.7环境下,如果切换到python3.x这就行不通了。不信请看

    o@o-pc:~$ python3.4
    Python 3.4.3 (default, Mar 26 2015, 22:03:40) 
    [GCC 4.9.2] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> print "hello world"
      File "<stdin>", line 1
        print "hello world"
                          ^
    SyntaxError: Missing parentheses in call to 'print'
    

    python3.4下报了一个错误,意思是语法错误,调用print的时候缺少括号
    我们使用有括号的版本就好了。

    >>> print ("hello world")
    hello world
    

    python脚本文件

    上面是在python的交互界面下输出的"hello world",那么有没有办法不使用这种交互界面,而是写一个python脚本文件呢?当然是可以了啦。
    我们可以新建一个文件2.py,然后写入

    print("hello world")

    然后我们使用python 2.py来执行它。

    o@o-pc:~$ python 2.py 
    hello world
    

    关于python脚本的文件名,并不一定要以.py做后缀,这只是比较通用的做法。
    如果你想指定这个脚本的解释器,那么可以在脚本的最前面加上一行来指明你所选择的解释器,例如#! /bin/python3.4

    #! /bin/python3.4
    print("hello world")

    help函数

    学习过linux/unix系统编程的人,应该对manpage这个东西是比较熟悉的。而python中提供了一个类似的东西,那就是help函数了。如果不清楚某个函数怎么用,就使用help(函数名)来获取相关的文档信息。
    python 官方文档中文站

    先来获取一下print函数的用法

    >>> help(print)
    
    
    print(...)
        print(value, ..., sep=' ', end='
    ', file=sys.stdout, flush=False)
        
        Prints the values to a stream, or to sys.stdout by default.
        Optional keyword arguments:
        file:  a file-like object (stream); defaults to the current sys.stdout.
        sep:   string inserted between values, default a space.
        end:   string appended after the last value, default a newline.
        flush: whether to forcibly flush the stream.
    
    

    print函数

    print概述

    全是英文的,看起来有困难吧。那就别管它了,我来解释一下。


    print函数可以用来打印一些值,默认是输出到标准输出
    file参数可以控制输出流到别的位置。
    end参数控制字符串输出后是否自动加' ',你可以改为别的。
    sep参数用于控制分割符,默认是空格。
    flush参数控制是否强制刷新流。
    这样说是不是还不清除,没关系,举个栗子嘛。

    >>> print(1,"+",2,"=",1+2,sep='	',end=' ')
    1	+	2	=	3 >>> print(1,"+",2,"=",1+2)
    1 + 2 = 3
    >>> 
    

    看上面的输出,分割符换成了' ',结尾没有加' '的。对比一下就很清楚了吧。
    这是在python3.4下做的,如果换做python2.7,那么输出会变成

    >>> print(1,"+",2,"=",1+2)
    (1, '+', 2, '=', 3)
    

    这是两者的区别。

    print格式化输出

    print的格式化输出,可以参考C语言的printf函数的格式化选项,这是类似的。

    看下面的代码

    >>> a=1000
    >>> print("%d"%a)
    1000
    >>> b=12.345
    >>> print("%f"%b)
    12.345000
    >>> print("%f"%(a+b))
    1012.345000
    >>> print("%d"%(a+b))
    1012
    
    

    分析一下。
    这里先是定义了一个变量a,并赋值为1000,然后使用print来格式化输出。%d表示以整数的方式来输出,后面紧跟的%a是取变量a的值的意思,和shell脚本中的$有点类似。
    后门又定义了一个变量b,赋值为123.45,然后以浮点数的形式输出。
    最后两个是输出链路这两者的和。注意,python中的数据是向下取整的。
    python中的变量不像C/C++这类强类型的语言,它的变量类型只与其最后一次被赋值有关。看下面的语言,重新给变量b赋值了一个字符串"hello",然后输出它。

    >>> b="hello"
    >>> print("%s"%b)
    hello
    

    再看这个

    >>> type(a)
    <class 'int'>
    >>> type(b)
    <class 'str'>
    >>> b=123
    >>> type(b)
    <class 'int'>
    

    要注意,格式化字符串一定要用""包含起来,并且后面紧跟要输出的变量。有两个格式化选项%s%r比较特殊,无论变量保存的数据类型是什么,都能正常输出。
    原因是%s调用的是str()函数把对象转化为str类型,而%r是调用了repr()将对象转化为字符串。
    下面是在python2.7下进行的,python3.x已经不支持这种默认转换了。

    >>> import time
    >>> d=time.localtime()
    >>> print d
    time.struct_time(tm_year=2015, tm_mon=6, tm_mday=17, tm_hour=15, tm_min=47, tm_sec=56, tm_wday=2, tm_yday=168, tm_isdst=0)
    >>> print "%s"%d
    time.struct_time(tm_year=2015, tm_mon=6, tm_mday=17, tm_hour=15, tm_min=47, tm_sec=56, tm_wday=2, tm_yday=168, tm_isdst=0)
    >>> print "%r"%d
    time.struct_time(tm_year=2015, tm_mon=6, tm_mday=17, tm_hour=15, tm_min=47, tm_sec=56, tm_wday=2, tm_yday=168, tm_isdst=0)
    

    python3.x可以这样做,而且这样做是值得提倡的做法。

    >>> print("%s"%(str(d)))
    time.struct_time(tm_year=2015, tm_mon=6, tm_mday=17, tm_hour=15, tm_min=44, tm_sec=49, tm_wday=2, tm_yday=168, tm_isdst=0)
    
    >>> print("%s"%repr(d))
    time.struct_time(tm_year=2015, tm_mon=6, tm_mday=17, tm_hour=15, tm_min=44, tm_sec=49, tm_wday=2, tm_yday=168, tm_isdst=0)
    

    print直接输出变量

    print可以直接输出变量的,是按照变量的类型来输出的。关于python变量,将在下一篇文档中详述。

    >>> a=10.10
    >>> b=123
    >>> c="nihao"
    >>> d='c'
    >>> print(a,b,c,d)
    10.1 123 nihao c
    
  • 相关阅读:
    git 命令手册
    leetcode #7 revert integer 问题
    leetcode #1 twoSum问题:简单实用哈希表
    c++模板函数分离编译的问题
    matlab 与c/c++ 混合MEX的编程
    springboot项目打war包
    springboot-jpa多数据源
    springboot使用RestTemplate+httpclient连接池发送http消息
    IDEA如何安装lombok
    Springboot如何启用文件上传功能
  • 原文地址:https://www.cnblogs.com/oloroso/p/4594840.html
Copyright © 2011-2022 走看看