国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

head first python 第二章代碼

2019-11-08 03:04:28
字體:
供稿:網(wǎng)友

第二章代碼 PRint_list(the_list,indent=False,level=0,out_file=sys.stdout) 這是一個遞歸方法,用為輸出the_list 中的數(shù)據(jù)

the_list 輸入要打印的數(shù)據(jù)類型,可以為任意indent=False 是否打印縮進level 打印縮進的級別,如the_list元素的縮進為0,the_list的自己元素中的list的元素縮進就為+1 就為1;如此遞加out_file=sys.stdout默認值為stdout,即輸出在console窗口中。 流程圖如下: 這里寫圖片描述import sysdef print_list(the_list,indent=False,level=0,out_file=sys.stdout): if(isinstance(the_list,list)):#isinstance 運行時識別,判斷變量“the_list”是否為一個list類型。 for each_item in the_list:#this funciton takes one positional argument called "the list",which is any Python list() if(isinstance(each_item,list)): print_list(each_item,indent,level+1,out_file)#判斷each_item是否為list,如果是進行遞歸調(diào)用,并在‘t’后加+1 else: if(indent): for tab_stop in range(level): print("/t",end='',file=out_file) print(each_item,file=out_file) else: if(indent): for tab_stop in range(level): print("/t",end='',file=out_file) print(the_list,file=out_file) -

一 isinstance()

>>> help(isinstance)Help on built-in function isinstance in module builtins:isinstance(obj, class_or_tuple, /) Return whether an object is an instance of a class or of a subclass thereof. A tuple, as in ``isinstance(x, (A, B, ...))``, may be given as the target to check against. This is equivalent to ``isinstance(x, A) or isinstance(x, B) or ...`` etc.

二 range()

class range(object) | range(stop) -> range object | range(start, stop[, step]) -> range object | | Return an object that produces a sequence of integers from start (inclusive) | to stop (exclusive) by step. range(i, j) produces i, i+1, i+2, ..., j-1. | start defaults to 0, and stop is omitted! range(4) produces 0, 1, 2, 3. | These are exactly the valid indices for a list of 4 elements. | When step is given, it specifies the increment (or decrement). | start | | step | | stop

range 是一個類。 range(10)返回的是0..<10的一個range實例,可以用for in遍歷。 range(0,10,2)返回0..10并且以2為增量的一個range實例。 其中有兩個函數(shù)

| 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.

第一個函數(shù)統(tǒng)計某一個值在range中出現(xiàn)的次數(shù),不在時返回0,有是返回數(shù)字。既然是range為什么會出現(xiàn)兩次…有什么意義? 第二個函數(shù)返回value對應(yīng)的索引位值,如果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)3

三 print

print(...) print(value, ..., sep=' ', end='/n', file=sys.stdout, flush=False) Prints the values to a stream, or to sys.stdout by default. Optional keyWord arguments: file: a file-like object (stream); defaults to the current sys.stdout. sep: string inserted between values, default a space. end: string appended after the last value, default a newline. flush: whether to forcibly flush the stream.

end=”可以輸出將多個值輸出在一行 sys.stdout默認輸出在console中


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 嘉祥县| 澄城县| 钦州市| 广德县| 昭觉县| 北京市| 都江堰市| 盐边县| 鹰潭市| 元氏县| 克东县| 长治市| 读书| 子洲县| 夹江县| 沙洋县| 通榆县| 郧西县| 定边县| 轮台县| 英超| 柯坪县| 红桥区| 社会| 盐池县| 林芝县| 秭归县| 伊金霍洛旗| 成都市| 南康市| 报价| 婺源县| 循化| 小金县| 天祝| 育儿| 九龙坡区| 蓬溪县| 洪雅县| 巴青县| 修武县|