13th chapter for And while Loops
1.for is running faster than while, the author of the Python Learning Manual has been re-launched to emphasize this, never too wordy
2.for is used in many ways
A.for x in list: Similar to foreach in C #, but changes to x cannot change the original value unless x is a reference to the object (assuming that you want to change the original value, you can only use the B method and assign a value to List[idx])
B.for idx in range (10):. Range can generate an ordered integer assignment to the IDX. Can have three of the parameters. Range (0,10,2). The first number of parameters is the starting value. The second parameter is the next value of the terminating value (that is, this value is not included). The third parameter is the number of steps (that is, each increment). This can also be negative for backward stepping), and range can be used with Len: Range (len list) to get the index of the list element
C.FOR item in List[::2]:, here List[::2] is a shard. Item is the element in the Shard each time it is taken, and the third parameter is the same as the number of steps (which can also be negative).
Shards have the same problem as a
D.for Line in open (' file.txt ', ' R '): This method will fetch the contents of the file
E.for (x, y) in zip (list, list2):, Zip merges elements of the same index in multiple series into tuples. Thus generating a list of tuples
F.for key in Dict: This is equivalent to the for key in Dict.keys, which makes a sub-simplification in the new version number (though it is a bit of a syntactic quirk). But I did enter a few 5 characters)
G.for (Offset,item) in Enumerate (list):. Offset gets the index of the object that can be iterated. Item gets its elements
H.L = [x+10 for x in list], list parsing [result expression for x in list if x satisfies the condition], even can [x+y for x in ' abc ' for Y in ' LMN '], the result is [' Al ', ' am ', ' an ', ' bl ', ' bm ', ' bn ', ' cl ', ' cm ', ' cn '
G.for root,dirs,files in Os.walk (path):. Traverse the folder, Root saves the path root folder. Dirs Save the folder name under the root folder (without the full path), files save the file name (without the full path)
The 3.for and while statements have an else block more than other languages, and only the code in the Else block will run if the break is not run in the loop (there is a real need in previous programming, and it is visible that python meets the needs of experience)
4.Dict initialization can use zip to simplify dict (keys, values)
5.zip will truncate the result with the shortest length sequence
6.map (None, List, list2), map and zip type, the difference is that the short sequence will be filled with the first number of vacancies
7.file = open (' file.txt ', ' r '), file has three ways to read
File.read () #读取整个文件 (fee memory)
File.read (1) #read by Charactor. File=open (' file.txt ', ' RB ') followed by file.read (1) means reading a block of bytes
File.readline () #read line by line
File.readlines () #read whole file to list include lines
File.xreadlines () #按需求载入文字列
8. Iterators:
File.readline () is equivalent to running the iterator File.next (). A Stopexception exception is thrown when running File.next () arrives at the end of the file (other types of iterators require the ITER (list) to be converted)
Sorted (list) #使用迭代将list又一次排序
SUM (list) #使用迭代将list元素内容相加
Any (list) #使用迭代将全部元素or求结果
All (list) #使用迭代将全部元素and求结果
' && '. Join (strlist) #使用迭代在全部字符串元素中间插入 && get a string
List (listable) #使用迭代创建可迭代对象的list对象
Tuple (listable) #使用迭代创建可迭代对象的元组新对象
9.pass is an empty statement
10. Assignment statements should not appear in the present situation (this is great!). such as The IF (X=next ()) in the C language cannot be used in Python
Python Learning Note two