Learn Python for loop statements and pythonfor
Let's talk about it.
Basic for operations
For is used to read elements from an object sequentially. Let's take a look at the following example and use the for loop to find out which data objects can be used and which cannot be used. It also provides a review of past content.
Copy codeThe Code is as follows:
>>> Name_str = "qiwsir"
>>> For I in name_str: # You can use the for loop for str.
... Print I,
...
Q I w s I r
>>> Name_list = list (name_str)
>>> Name_list
['Q', 'I', 'w', 's', 'I', 'R']
>>> For I in name_list: # list can also be used
... Print I,
...
Q I w s I r
>>> Name_set = set (name_str) # set can also be used
>>> Name_set
Set (['Q', 'I', 's', 'R', 'w'])
>>> For I in name_set:
... Print I,
...
Q I s r w
>>> Name_tuple = tuple (name_str)
>>> Name_tuple
('Q', 'I', 'w', 's', 'I', 'R ')
>>> For I in name_tuple: # tuple is acceptable.
... Print I,
...
Q I w s I r
>>> Name_dict = {"name": "qiwsir", "lang": "python", "website": "qiwsir. github. io "}
>>> For I in name_dict: # dict is no exception
... Print I, "-->", name_dict [I]
...
Lang --> python
Website --> qiwsir. github. io
Name --> qiwsir
In addition to the above data types, the file can also be used for, this in the previous special "Do Not Red header file" two articles to explain how to use for to read the file object content. If the viewer forgets it, You can browse it.
In list parsing, the purpose of for should not be underestimated. This is explained during list parsing. However, it is better to review it again, no, haha.
Copy codeThe Code is as follows:
>>> One = range (1, 9)
>>> One
[1, 2, 3, 4, 5, 6, 7, 8]
>>> [X for x in one if x % 2 = 0]
[2, 4, 6, 8]
I will not talk about anything. The powerful list parsing will become more and more familiar in the future. I admire it.
If you use python3, you will find that dictionary parsing and tuples parsing are also amazing.
To upgrade to a higher level, you must make a summary. The for loop mentioned above is summarized as follows:
Enter the image description
Express in one text:
Copy codeThe Code is as follows:
For iterating_var in sequence:
Statements
Iterating_var is the iteration variable of the Object sequence, that is, the sequence must be an object that can have a certain sequence. Note that no sequence exists, that is, the element can be obtained according to a certain script. Of course, the file object is a sequence. We do not use the script to get each row. If we read it, because it is also a str, we can still use the script to read its content.
Zip
What is zip? In interactive mode, use help (zip). The official documents are as follows:
Zip (...)
Zip (seq1 [, seq2 [...])-> [(seq1 [0], seq2 [0]...), (...)]
Return a list of tuples, where each tuple contains the I-th element from each of the argument sequences. The returned list is truncated in length to the length of the shortest argument sequence.
Experiment to understand the above documents:
Copy codeThe Code is as follows:
>>> A = "qiwsir"
>>> B = "github"
>>> Zip (a, B)
[('Q', 'G'), ('I', 'I'), ('w', 'T'), ('s ', 'H'), ('I', 'U'), ('R', 'B')]
>>> C = [1, 2, 3]
>>> D = [9, 8, 7, 6]
>>> Zip (c, d)
[(1, 9), (2, 8), (3, 7)]
>>> E = (1, 2, 3)
>>> F = (9, 8)
>>> Zip (e, f)
[(1, 9), (2, 8)]
>>> M = {"name", "lang "}
>>> N = {"qiwsir", "python "}
>>> Zip (m, n)
[('Lang ', 'python'), ('name', 'qiwsir')]
>>> S = {"name": "qiwsir "}
>>> T = {"lang": "python "}
>>> Zip (s, t)
[('Name', 'lang ')]
Zip is a built-in function. Its parameters must be of a sequence data type. If it is a dictionary, keys are considered as sequences. Then, the elements corresponding to the sequence are made up of tuples as a list element.
The following is a special case where a parameter is generated as a sequence of data:
Copy codeThe Code is as follows:
>>>
'Qiwsir'
>>> C
[1, 2, 3]
>>> Zip (c)
[(1,), (2,), (3,)]
>>> Zip ()
[('Q',), ('I',), ('w',), ('s',), ('I ',), ('R',)]
This function is used with for to implement:
Copy codeThe Code is as follows:
>>> C
[1, 2, 3]
>>> D
[9, 8, 7, 6]
>>> For x, y in zip (c, d): # implement one-to-one printing
... Print x, y
...
1 9
2 8
3 7
>>> For x, y in zip (c, d): # sums up and down the corresponding quantities in the two lists.
... Print x + y
...
10
10
10
The above addition function can be written as follows if zip is not needed:
Copy codeThe Code is as follows:
>>> Length = len (c) if len (c) <len (d) else len (d) # judge the length of c and d and take the short length out.
>>> For I in range (length ):
... Print c [I] + d [I]
...
10
10
10
Which of the above two statements is better? The former? The latter? Haha. I see it almost. You can also do this:
Copy codeThe Code is as follows:
>>> [X + y for x, y in zip (c, d)]
[10, 10, 10]
As mentioned previously, list Parsing is tough. Of course, you can also:
Copy codeThe Code is as follows:
>>> [C [I] + d [I] for I in range (length)]
[10, 10, 10]
For Loop statements are often used later. In fact, we have used a lot of them before. Therefore, it should be no stranger to the official.
Python loop statements
For t in range (0, 37 ):
G = phrase (t)
Y = GB + 4.667345
Print t
Print g
Print y
Change
Result = [(I, phrase (I), phrase (I) + 4.667345) for I in range ()]
Print (result), you can see the result.
Python loop statements
Of course. All programming languages support nested loops ......
For I in range (1, 5 ):
J = 1
While j <3:
Print I * j
J + = 1