python The loop statements of are divided into while loop 、for loop 、 Loop nesting and iterators . One of them break, Its function is to jump out of the whole cycle , also continue
The function of is to jump out of the current cycle .
Catalog
for loop
for A nested loop
while loop
while A nested loop
iterator
for x in ... A loop is to substitute each element into a variable x, Then execute the indented block statement .
for each in range(3):
print(each)
0
1
2
Python Provide a range() function , You can generate a sequence of integers , Re pass list() Function can be converted to list
list=['eat','sleep','study']
for act in list:
print(" is ",act)
is eat
is sleep
is study
range(101) You can generate 0-100 Integer sequence of
for iteration_var in sequence:
for iteration_var in sequence:
Loop statement
As long as the conditions are met , It's going to keep cycling , Exit the loop if the condition is not met .
while i<3: # Judge the condition
print(i) # Loop statement
i=i+1
0
1
2
In circulation ,break Statement can exit the loop ahead of time
During the cycle , It can also be done through continue sentence , Skip the current loop , Start the next cycle directly
use Ctrl+C Exit procedure , Or forced end Python process
while Judge the condition :
while Judge the condition :
Loop statement
Iterators are used to iterate through a series of elements , It can not only iterate the sequence , You can also iterate over objects that are not sequences but exhibit sequence behavior . Iterators are very useful for iterating over large collections where the total number of elements cannot be known in advance . Iterators provide a unified interface to access collections , Definition iter()
Method object , You can use iterators to access .
Can be next()
The object that function calls and returns the next value continuously is called an iterator :Iterator
.next()
Function to access each object , Until the object is accessed , Return to one StopIteration
abnormal . Use isinstance()
We can judge whether an object is Iterator
object .
be-all Iterable
Both can pass iter()
Function to Iterator
.