程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python loop statement

編輯:Python

        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 loop

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

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 A nested loop

for iteration_var in sequence:
for iteration_var in sequence:
Loop statement 

while loop

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

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 A nested loop

while Judge the condition :
while Judge the condition :
Loop statement 

iterator

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.


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved