2016年2月16日

python类与对象

摘要: 1.改变对象的字符串显示只需要重新定义它的__str__()和__repr__()方法 >>> class Pair: ... def __init__(self,x ,y): ... self.x = x ... self.y = y ... def __repr__(self): ... ret 阅读全文

posted @ 2016-02-16 12:31 叶摇 阅读(129) 评论(0) 推荐(0)

2016年2月15日

python函数

摘要: 1.构造一个可接受任意数量参数的函数 >>> def avg(first, *rest): ... return(first + sum(rest)) / (1 + len(rest)) ... >>> avg(1,2) 1.5 >>> avg(1,2,3,4,5) 3.0 其中*rest代表除第一 阅读全文

posted @ 2016-02-15 09:20 叶摇 阅读(155) 评论(0) 推荐(0)

2016年1月29日

python迭代器和生成器

摘要: 1.手动遍历迭代器 当遍历可迭代对象的所有元素,但不想用for()循环时,可以使用next()函数并在代码中捕获StopIteration异常,如下手动读取文件所有行: def manual_iter(): with open('/etc/passwd') as f: try: while True 阅读全文

posted @ 2016-01-29 18:03 叶摇 阅读(151) 评论(0) 推荐(0)

2016年1月28日

python字符串和文本操作

摘要: 1.需要将一个字符串切割为多个字段, 分隔符并不是固定不的(比如空格个数不确定) 这时就不能简单的使用string对象的split()方法,需要使用更加灵活的re.split()方法 >>> line = 'adaead jilil; sese, lsls,aea, foo' >>> import 阅读全文

posted @ 2016-01-28 15:20 叶摇 阅读(4674) 评论(0) 推荐(0)

2016年1月26日

python基本数据类型

摘要: 一、 整数 即没有小数位的阿拉伯数字:20,100,2000 1 class int(object): 2 """ 3 int(x=0) -> int or long 4 int(x, base=10) -> int or long 5 6 Convert a number or string to 阅读全文

posted @ 2016-01-26 14:20 叶摇 阅读(110) 评论(0) 推荐(0)

导航