zoukankan      html  css  js  c++  java
  • python学习

    关于help:

    python中 help(raw_input)

    结果如下:

    Help on built-in function raw_input in module __builtin__:

    raw_input(...)
    raw_input([prompt]) -> string

    Read a string from standard input. The trailing newline is stripped.
    If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.
    On Unix, GNU readline is used if enabled. The prompt string, if given,
    is printed without a trailing newline before reading.
    lines 1-9/9 (END)

    关于输出:

    如何输出多个格式化值

    print "%s is number %d" % ("hello",1) 必须加上括号

    关于输入:

    raw_input()和input()的区别

    raw_input()既可接受数字类型的输入也可接受字符型输入,如下:

    name = raw_input("your name: ")  

    ///you can input Alex,then name is Alex

    but name = input("your name: ")

    ///you can not input Alex, you can only input digital, 输入11,then name is 11

    关于注释:

    一种是对于某一个代码块的功能的描述,如#

    还有一种是对某个模块,类或者函数的起始处添加一个字符串,起在线文档功能

    如下:

         def foo():
                "this is a doc string."
                 return True
    那么如何输出foo()的注释呢?
    print f.__doc__ 或者 help(f) 也可以
    ///this is a doc string.

    关于and 和 or :
    等价于 && 和 ||

    关于字符串中取出子串:
    string = "hello world"
    ///string[2] = l
    ///string[:] = "hello world"
    ///string[1:] = "ello world"

    还有一个非常特别的东西,那就是
    ///string[-1] = d 从后面到前面是从-1开始

    字符串连接操作:+ 字符串重复操作:*
    'hello'+'world'
    ///helloworld
    'hello'*2
    ///hellohello

    关于for循环:
    for循环中任何可迭代的对象都可以被for循环迭代,不管是字符串,列表,元组,字典,还是文件

    关于列表解析:

    [表达式 for x in 可迭代对象 if 表达式]

    squared = [x**2 for x in range(4)]   #求x的平方
    sqdEvens = [x**2 for x in range(8) if not x % 2]     #  求x平方中能够被2整除的

    文件操作:

    filename = raw_input("输入文件名:")
          fs = open(filename,'r')
          for eachLine in fs:
                print eachLine,
          fs.close()

    好有意思,竟然文件也可以被迭代
    取出文件中每一个单词,并保存在一个列表中,如何做?

     代码如下:

    #!/usr/bin/env python
    import re
    
    fs = open('hello','r')
    S = [];
    for line in fs:  //迭代操作文件的每一行
        print line
        line = line.split(' ') //以空格作为划分生成一个列表
        for s in line:
            s = re.match(r'[a-zA-Z0-9]+',s).group(0)  //group(0)才是匹配的字符串
            S.append(s)   //列表尾添加元素,可以使用append,但是append智能一个个的添加
                 //如何将一个列表插入另一个列表的尾呢?可以使用+,a+b,就是将列表b添加到列表b的后面。
    print S

    文件如下:

    hello world, Alex is king.
    your name is alex

    运行结果如下:

    当然要修改文件,或者读取文件中某些字符,也可以使用read(),readline(),readlines(),write(),..

    下面当然要介绍python操作文件的所有模式,

    打开一个文件有集中模式,首先是open(file,'r')表示只读,open(file,'w')表示只写,不能读,open(file,'+')表示可写可读,open(file,'a')表示只写,不可读。

    open(file,'w')表示只写

    open(file,'a')表示只写

    两者同时表示只写,但是是有区别的,第一种会将文件的内存冲洗掉,重新从文件开头写入新的值,而第二种从文件尾写入,不会冲洗掉文件原来的东西。

    而且什么时候才真的写入是有规定的,因为写是有缓冲区的,为了减少I/O的操作,不会每一次的写入马上就进入文件,而是先写到缓冲区中,然等缓冲区慢,或者文件close掉,才写入文件,所以为了防止异常出现而导致文件没有close掉,导致内容没有写入缓冲区,所以一定要保证每一次写入都是成功的,而不是只停留在缓冲区中。当然缓冲区的大小也可以指定,默认一般4KB,有一个函数flush(),调用后可不用close(),就可以将缓冲区的内容写入文件中。

    read(size),当size设置了值,则读size大小的字符,当size没设置,则读整个文件。当然读的过程会改变当前位置指针的值

    readline(size),当size设置了,如果不超过一行的大小,则读取size大小的字符数,如果超过了一行的大小,将当前文件指针指向的这一行,从文件指针开始读,直到遇到换行停止。

    readlines(size),当size设置了,如果不超过一行的大小,则读取size大小的字符数,如果超过了一行的大小,则不像readline(),它可以跨行读取,一次读取多行,读大小size停止而已。

    seek(size),将文件当前指针指向从文件头开始到size大小的时候停止。

    fs.seek(0)指针停在了文件头

     tell(...)
     |      tell() -> current file position, an integer (may be a long integer).

     write(...)
     |      write(str) -> None.  Write string str to file.
     |      
     |      Note that due to buffering, flush() or close() may be needed before
     |      the file on disk reflects the data written.
     |  
     |  writelines(...)
     |      writelines(sequence_of_strings) -> None.  Write the strings to the file.
     |      
     |      Note that newlines are not added.  The sequence can be any iterable object
     |      producing strings. This is equivalent to calling write() for each string.

    实用的函数

    dir([obj])     #显示对象的属性,如果没有提供参数,则显示全局变量的名字

    help([obj])   #显示对象的文档字符串帮助,没提供参数则会进入交互式帮助

    int(obj)       #将一个对象转换为整型

    len(obj)       #返回对象的长度

    open(fn,mode)      #以mode方式打开一个文件名为fn的文件

    range([start,]stop[,step])     #返回一个整数列表,起始值为start,结束值为stop-1,star默认值为0,step默认值为1

    raw_input(str)       #等待用户输入一个字符串,可以提供一个可选的参数用作提示信息。

    str(obj)                #将一个对象转换为字符串

    type(obj)              #返回对象的类型(返回值本省是一type对象!)

  • 相关阅读:
    分页查询
    web 开发中 405报错
    html 中input标签的name属性
    怎么样利用debug
    bzoj 1314: River过河 优先队列
    bzoj 4004: [JLOI2015]装备购买 拟阵 && 高消
    bzoj 1133: [POI2009]Kon dp
    bzoj 4127: Abs 树链剖分
    bzoj 2406: 矩阵 上下界网络流判定
    再写FFT模板
  • 原文地址:https://www.cnblogs.com/GODYCA/p/2866835.html
Copyright © 2011-2022 走看看