1. 程式人生 > >Python全棧__叠代器、生成器、知識點補充、列表推導式,生成器表達式、如何系統科學的學習Python

Python全棧__叠代器、生成器、知識點補充、列表推導式,生成器表達式、如何系統科學的學習Python

ide swa XP iter count CA slow iterable 就是

1、叠代器

  (1)可叠代對象

技術分享圖片
1 s1 = 123
2 for i in s1:
3     print(i)
可叠代對象

  示例結果:

技術分享圖片
D:\Python36\python.exe "E:/Python/課堂視頻/day13視頻與課堂筆記/day13課堂筆記/day13/02 叠代器.py"
1
2
3
True
False

Process finished with exit code 0
示例結果

  int object is not iterable

技術分享圖片
1 for i in 123:
2     print(i)
Int object is not iterable

示例結果:

技術分享圖片
D:\Python36\python.exe "E:/Python/課堂視頻/day13視頻與課堂筆記/day13課堂筆記/day13/02 叠代器.py"
Traceback (most recent call last):
  File "E:/Python/課堂視頻/day13視頻與課堂筆記/day13課堂筆記/day13/02 叠代器.py", line 8, in <module>
    for i in 123:
TypeError: int object is not iterable

Process finished with exit code 
1
示例結果

  內部含有__iter__方法的就是可叠代對象,遵循可叠代協議。

技術分享圖片
1 dir
2 print(dir(123))  # ‘__iter__‘
可叠代對象的判斷

  結果:

技術分享圖片
1 D:\Python36\python.exe "E:/Python/課堂視頻/day13視頻與課堂筆記/day13課堂筆記/day13/02 叠代器.py"
2 [__add__, __class__, __contains__, __delattr__, __dir__, __doc__, __eq__, __format__
, __ge__, __getattribute__, __getitem__, __getnewargs__, __gt__, __hash__, __init__, __init_subclass__, __iter__, __le__, __len__, __lt__, __mod__, __mul__, __ne__, __new__, __reduce__, __reduce_ex__, __repr__, __rmod__, __rmul__, __setattr__, __sizeof__, __str__, __subclasshook__, capitalize, casefold, center, count, encode, endswith, expandtabs, find, format, format_map, index, isalnum, isalpha, isdecimal, isdigit, isidentifier, islower, isnumeric, isprintable, isspace, istitle, isupper, join, ljust, lower, lstrip, maketrans, partition, replace, rfind, rindex, rjust, rpartition, rsplit, rstrip, split, splitlines, startswith, strip, swapcase, title, translate, upper, zfill] 3 4 Process finished with exit code 0
示例結果

技術分享圖片
D:\Python36\python.exe "E:/Python/課堂視頻/day13視頻與課堂筆記/day13課堂筆記/day13/02 叠代器.py"
[__add__, __class__, __contains__, __delattr__, __dir__, __doc__, __eq__, __format__, __ge__, __getattribute__, __getitem__, __getnewargs__, __gt__, __hash__, __init__, __init_subclass__, __iter__, __le__, __len__, __lt__, __mod__, __mul__, __ne__, __new__, __reduce__, __reduce_ex__, __repr__, __rmod__, __rmul__, __setattr__, __sizeof__, __str__, __subclasshook__, capitalize, casefold, center, count, encode, endswith, expandtabs, find, format, format_map, index, isalnum, isalpha, isdecimal, isdigit, isidentifier, islower, isnumeric, isprintable, isspace, istitle, isupper, join, ljust, lower, lstrip, maketrans, partition, replace, rfind, rindex, rjust, rpartition, rsplit, rstrip, split, splitlines, startswith, strip, swapcase, title, translate, upper, zfill]

Process finished with exit code 0
View Code

2、生成器

3、知識點補充

4、列表推導式,生成器表達式

5、如何系統科學的學習Python

Python全棧__叠代器、生成器、知識點補充、列表推導式,生成器表達式、如何系統科學的學習Python