第二章代码 PRint_list(the_list,indent=False,level=0,out_file=sys.stdout) 这是一个递归方法,用为输出the_list 中的数据
the_list 输入要打印的数据类型,可以为任意indent=False 是否打印缩进level 打印缩进的级别,如the_list元素的缩进为0,the_list的自己元素中的list的元素缩进就为+1 就为1;如此递加out_file=sys.stdout默认值为stdout,即输出在console窗口中。 流程图如下:range 是一个类。 range(10)返回的是0..<10的一个range实例,可以用for in遍历。 range(0,10,2)返回0..10并且以2为增量的一个range实例。 其中有两个函数
| count(...) | rangeobject.count(value) -> integer -- return number of occurrences of value | | index(...) | rangeobject.index(value, [start, [stop]]) -> integer -- return index of value. | Raise ValueError if the value is not present.第一个函数统计某一个值在range中出现的次数,不在时返回0,有是返回数字。既然是range为什么会出现两次…有什么意义? 第二个函数返回value对应的索引位值,如果value不在range会抛出 ValueError异常,x is not in range
>>> item = range(2,10,2)>>> index=item.index(9)Traceback (most recent call last): File "<pyshell#25>", line 1, in <module> index=item.index(9)ValueError: 9 is not in range>>> index=item.index(8)>>> >>> print(index)3end=”可以输出将多个值输出在一行 sys.stdout默认输出在console中
新闻热点
疑难解答