CR的代码文本

all for learning about the world
  订阅 订阅  :: 管理

随笔分类 -  Python

摘要:类的特殊方法 __init__(self,...) 构造__del__(self) 析构__str__(self) print对象或str()时调用__lt__(self,other) 当使用 小于 运算符(<)的时候调用。 类似地,对于所有的运算符(+,>等等)都有特殊的方法。__getitem__(self,key) 让对象可使用索引操作,[key]__len__(self) 对序列对象使用内建的len()函数的时候调用。函数中接收元组和列表 元组和列表是用*前缀词典用**前缀lambda, exec, eval lambda,创建新的函数对象exec,执行字符串中的pytho 阅读全文

posted @ 2011-08-09 16:11 mumuliang 阅读(205) 评论(0) 推荐(0) 编辑

摘要:sys重点关注:sys.stdin、sys.stdout和sys.stderrosos.name 字符串指示你正在使用的平台。比如对于Windows,它是'nt',而对于Linux/Unix用户,它是'posix'。os.getcwd() 函数得到当前工作目录,即当前Python脚本工作的目录路径。os.getenv()和os.putenv() 函数分别用来读取和设置环境变量。os.listdir() 返回指定目录下的所有文件和目录名。os.remove() 函数用来删除一个文件。os.system() 函数用来运行shell命令。os.linesep字符串 给 阅读全文

posted @ 2011-08-09 15:51 mumuliang 阅读(225) 评论(0) 推荐(0) 编辑

摘要:try: if err: raise Exceptionexcept Exception: # do some specialelse: # no exceptionfinally: # always do 阅读全文

posted @ 2011-08-09 15:22 mumuliang 阅读(226) 评论(0) 推荐(0) 编辑

摘要:类1. 使用关键字class2. 类的方法的第一个参数永远是self,像显式的this。 注:第一个参数的名称是随意的,也可以不叫self,但它的意义固定。3. 使用类名后加()来创建一个类对象#!/usr/bin/python#Filename:method.pyclassPerson:defsayHi(self):print'Hello,howareyou?'p=Person()p.sayHi()4. __init__方法,即构造函数classPerson:def__init__(self,name):self.name=namedefsayHi(self):print&# 阅读全文

posted @ 2011-08-09 14:30 mumuliang 阅读(234) 评论(0) 推荐(0) 编辑

摘要:列表[]元组()词典{}1. 都可以多重嵌套2. 切片要注意list[1:3]的意义是,从1开始,3之前的元素,不包含3因此list[1:]的意义,使用同样的逻辑来说是,从1开始,到最后一个元素的后一个位置之前的元素。-_-b3. 默认引用传递mylist = shoplist # mylist仅仅是shoplist的别名newlist = shoplist[:] # newlist是shoplist的拷贝 阅读全文

posted @ 2011-08-09 12:38 mumuliang 阅读(233) 评论(2) 推荐(0) 编辑

摘要:使用模块 1. import module 使用时modulename类似namespace2. from module import 函数和变量列表 类似using namespace的功效,不推荐。3. import module as anothername 很面熟的语法,忘记哪儿见过。给导入模块起别名。被调用模块#!/usr/bin/python#Filename:mymodule.pyimportsysdefsayhi():if__name__=='__main__':print'Hi,thisismymodulespeaking,calledbymyself 阅读全文

posted @ 2011-08-09 10:33 mumuliang 阅读(214) 评论(0) 推荐(0) 编辑

摘要:基本数据类型 整型,长整型,浮点,复数,字符串变量 无需声明或定义类型,在需要时赋值即可使用。函数定义 def 函数名(参数列表)变量使用无需声明或定义类型,函数也因此没有必要定义返回类型。默认情况下返回的是None。 函数的注意点 1. 形参是值传递2. 为外部变量赋值,使用global关键字deffunc(x):print'xis',xx=2print'Changedlocalxto',x x=50func(x)print'Valueofxis',x3. 支持默认参数defsay(message,times=1):printmessage*t 阅读全文

posted @ 2011-08-08 16:08 mumuliang 阅读(267) 评论(0) 推荐(0) 编辑

摘要:注意冒号if else if ... :缩进else :缩进ifguess==number:print'Congratulations,youguessedit.'#Newblockstartshererunning=Falseelifguess<number:print'No,itisalittlehigherthanthat'#Anotherblock#Youcandowhateveryouwantinablock...else:print'No,itisalittlelowerthanthat'#youmusthaveguess> 阅读全文

posted @ 2011-08-08 15:07 mumuliang 阅读(235) 评论(0) 推荐(0) 编辑

摘要:注释前缀 ##!/usr/bin/python#Filename:helloworld.pyprint'HelloPython.'字符串 1. '和"是一样的2. '''用来定义多行字符串,并且其中可自由使用'和">>>print'''Thisisamulti-linestring.thisis1stline.thisissecondline."what'surname?",Iasked."mynameisfourthline!" 阅读全文

posted @ 2011-08-08 14:13 mumuliang 阅读(222) 评论(0) 推荐(0) 编辑

摘要:''':多行字符,内可'和"'*'的时候,在第二个'''前没有空格会EOL。print '''''''',啥都不输出print '''""''' 输出 "">>>print''''a'''''a'>>>print'''' 阅读全文

posted @ 2011-08-08 11:46 mumuliang 阅读(296) 评论(2) 推荐(0) 编辑

点击右上角即可分享
微信分享提示