www.sqnote.cn

Python 第一天

1:python 导入(import)模块的时候从哪里去寻找?

import sys

print sys.path

说明:运行上面这段语句就可以看到从那里搜索导入模块的。

2:python怎么写象Java中main一样的测试代码?(当然java里不建议你那么写...)

if __name__=="__main__":

说明:当在Pydev中F9直接运行该py的时候,__name__就是"__main__",当import的时候,__name__通常为模块的文件名。

3:dictionary (字典)list (列表)tuple (元组),有什么不同?

定义:

map={"one":"SQ","two":"SQnote"}

list=["SQ","SQnote","SQnote.cn","www.sqnote.cn"]

t=("SQ","SQnote","SQnote.cn")

取值:

map["one"]

list[0]

list[0:2] #slice , this is a new list

删除 and 添加:

del map["one"] #delete only key is one

map.clear() #delete all map data

map["three"]=3 #it can add "three":3

list.remove("SQ")

list.insert(2,"www.SQnote.cn")

list += ["new SQ"] # this method will return new list

list.append("append sq")  # "append sq" is a object, so if list.append(["1","2"]), add this object :  ["1","2"]
list.extend(["extend sq"]) #join two list , this is diff append.  this is also before list object

搜索

list.index("SQ")

"SQ" in list

"SQ" in t

 

总结:明天再继续了...

Relate Posts:

Tags: python

By SQ post on 2009-1-19 00:17 AM Python |

添加评论

9+5