exercise 22&23 总结+读代码

来源:互联网 发布:淘宝photoshop教程 编辑:程序博客网 时间:2024/05/11 23:24

Here's what you do:

  1. Go to bitbucket.org, github.com, or gitorious.org with your favorite web browser and search for "python."
  2. Pick a random project and click on it.
  3. Click on the Source tab and browse through the list of files and directories until you find a .py file.
  4. Start at the top and read through the code, taking notes on what you think it does.
  5. If any symbols or strange words seem to interest you, write them down to research later.


Now try some of these other sites:

  • github.com
  • launchpad.net
  • gitorious.org
  • sourceforge.net



总结
1.print 用法:
    1)一般情况最后不需加逗号;
    2)加逗号表示显示在同一行,不加会自动在新的一行显示
2.%? 格式符用法总结在exercise5
    常用的:
   %d 格式化整数
   %f 格式化浮点数,可以指定小数点后的精度,默认显示6位小数,例如%.2f显示2位小数。
    %s 格式化字符串#显示字符串内容
    %r  大字符串     #显示字符串 包括字符串前面的""
3.转义字符
    \n 换行
    \'
    \b 退格,表示删除前面一个字符
    \t 水平制表符  #作用同tab 4个空格键
    \v 垂直制表符,表示换行,然后从\v的地方开始输出。
    \n 换行
    \f 换页
    \r 回车 回到行首

4. 键入 raw_input    int(raw_input())用于计算
5. 提示 age = raw_input("How old are you? ")
6. pydoc????????

7.解包变量
from sys import argv      #从python功能库中导入功能   “features"真正的名称是:modules   / libraries
script, first, second, third = argv # argv是“参数变量”,解变量

8.读取文件
    open(路径+文件名,读写模式) w  以写方式打开
    f.read()
    f.close()
    f.seek(0/1/2)     #0代表从头开始,1代表当前位置,2代表文件最末尾位置。

9.exercise17有点模糊  怎么到to_file的???
10. echo 将内容写入某文件
      cat 显示某文件(txt?)
11. from os.path import exists (专门有os.path 模块介绍)   ##从os.path模块中导入exists命令

12.定义函数
def print_two(*args):  #给函数定义了一个变量组???*args
    arg1, arg2 = args    

def function(valuables):    ###def 最后是引号!!!!
     ------  
     return -----    ##函数能返回信息

13.raw_input &input 
1)这两个函数均能接收字符串,但 raw_input() 直接读取控制台的输入(任何类型的输入它都可以接收)。
而对于 input() ,它希望能够读取一个合法的 python 表达式,即你输入字符串的时候必须使用引号将它括起来,否则它会引发一个 SyntaxError 。
2)raw_input() 将所有输入作为字符串看待,返回字符串类型。而 input() 在对待纯数字输入时具有自己的特性,它返回所输入的数字的类型 int, float。

除非对 input() 有特别需要,否则一般情况下我们都是推荐使用 raw_input() 来与用户交互。

14. with  as
普通代码如下:
file= open("/tmp/foo.txt")
data=file.read()
file.close()
但可能产生问题:一是可能忘记关闭文件句柄;二是文件读取数据发生异常,没有进行任何处理。
下面是用try——finally来处理异常的加强版本:
file= open("/tmp/foo.txt")
try:
    data=file.read()
finally:
    file.close()

简短版本:
withopen("/tmp/foo.txt")
as
file:
    data=file.read()






基本思想是with所求值的对象必须有一个__enter__()方法,一个__exit__()方法。
紧跟with后面的语句被求值后,返回对象的__enter__()方法被调用,这个方法的返回值将被赋值给as后面的变量。当with后面的代码块全部被执行完之后,将调用前面返回对象的__exit__()方法。
????????












0 0