13th chapter for And while Loops
1.for is executed faster than while, and the author of the Python Learning Manual has been re-starting to emphasize this, never too wordy
The use of 2.for is particularly numerous
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 (if you want to modify the original value, you can only use B usage and assign a value to List[idx])
B.for idx in range: Range can generate an ordered integer assignment to the IDX, can have three parameters, range (0,10,2), the first parameter is the starting value, the second argument is the next value of the terminating value (that is, does not contain this value), The third parameter is the number of steps (that is, the number of increments per increment, which 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 that takes the Shard each time, the third parameter is also the number of steps (can also be negative). Shards have the same problem as a
D.for lines in open (' file.txt ', ' R '): This usage will fetch the contents of the file line by row
E.for (x, y) in zip (list, list2):, Zip merges elements of the same index in multiple sequences into tuples, resulting in a tuple list
F.for key in Dict: This is equivalent to the for key in Dict.keys, which makes the sub-simplification in the new version (although the syntax is a bit strange, but it does enter 5 characters less)
G.for (Offset,item) in Enumerate (list):, offset gets an index of the iterator object, item gets its element
H.L = [x+10 for x in list], list parsing [result expression for x in list if x satisfies the condition], incredibly can also [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 folder, root save path root directory, dirs Save folder name under root directory (without full path), files save file name (without full path)
The 3.for and while statements have an else block more than other languages, and the code in the Else block is executed only if there is no break in the loop (there is a need in previous programming 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 vacancy caused by the short sequence will be filled with the first parameter
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) for reading byte blocks
File.readline () #read line by line
File.readlines () #read whole file to list include lines
File.xreadlines () #按需求加载文字列
8. Iterators:
File.readline () is equivalent to performing an iterator file.next (), different when execution File.next () arrives at the end of the file and throws a Stopexception exception (other types of iterators require a ITER (list) conversion)
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 cannot appear where expressions should be present (this is great!). such as The IF (X=next ()) in the C language cannot be used in Python
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Python Learning Note two