Python For 和 While 循环:Enumerate、Break、Continue 语句
什么是循环?
循环可以在某个条件满足之前执行一段代码多次。它们在编程中非常常见。不像其他编程语言有 For Loop、while loop、dowhile 等。
什么是 For Loop?
For loop 用于迭代序列中的元素。当你有一段代码想要重复“n”次时,通常会使用它。
什么是 While Loop?
While Loop 用于重复执行一段代码。它不像只运行一次代码块,而是执行代码块多次,直到满足某个特定条件。
如何使用“While Loop”
While 循环与“if 语句”所做的事情完全相同,但它不是只运行一次代码块,而是跳转回代码开始执行的点并再次重复整个过程。
语法
while expression Statement
示例:
# #Example file for working with loops # x=0 #define a while loop while(x <4): print(x) x = x+1
预期输出
0 1 2 3
- 代码行 4:变量 x 设置为 0
- 代码行 7:While 循环检查条件 x<4。x 的当前值为 0。条件为真。控制流进入 while 循环。
- 代码行 8:打印 x 的值
- 代码行 9:x 增加 1。控制流返回到第 7 行。现在 x 的值为 1,小于 4。条件为真,while 循环再次执行。一直持续到 x 变为 4,while 条件变为假。
如何使用“For Loop”
在 Python 中,“for loops”被称为迭代器。
与 while 循环一样,“For Loop”也用于重复程序。
但与依赖条件真假的 while 循环不同,“For Loop”取决于它需要迭代的元素。
示例:
# #Example file for working with loops # x=0 #define a while loop # while(x <4): # print x # x = x+1 #Define a for loop for x in range(2,7): print(x)
预期输出
2 3 4 5 6
For Loop 按照 range 中声明的数字进行迭代。
例如,
For Loop for x in range (2,7)
当执行此代码时,它将打印 2 到 7 之间的数字(2、3、4、5、6)。在此代码中,数字 7 不被包含在 range 内。
For Loops 还可以用于其他用途,而不仅仅是数字。我们将在下一节中看到这一点。
如何对字符串使用 For Loop
在此步骤中,我们将了解“for loops”如何用于数字以外的其他用途。
示例:
#use a for loop over a collection Months = ["Jan","Feb","Mar","April","May","June"] for m in Months: print(m)
预期输出
Jan Feb Mar April May June
代码行 3:我们将月份(“一月、二月、三月、四月、五月、六月”)存储在变量 Months 中。
代码行 4:我们遍历 Months 中的每个值。Months 的当前值存储在变量 m 中。
代码行 5:打印月份。
如何在 For Loop 中使用 break 语句
断点是 For Loop 中一个独特的功能,它允许您中断或终止 for 循环的执行。
示例:
#use a for loop over a collection #Months = ["Jan","Feb","Mar","April","May","June"] #for m in Months: #print m # use the break and continue statements for x in range (10,20): if (x == 15): break #if (x % 2 == 0) : continue print(x)
预期输出
10 11 12 13 14
在此示例中,我们声明了 10-20 的数字,但我们希望我们的 for 循环在数字 15 处终止并停止进一步执行。为此,我们通过定义 (x==15): break 来声明 break 函数,因此一旦代码调用数字 15,它就会终止程序。代码行 10 声明变量 x 在 range (10, 20) 之间。
- 代码行 11 声明断点条件为 x==15。
- 代码行 12 检查并重复步骤,直到达到数字 15。
- 代码行 13 在输出中打印结果。
如何使用“continue 语句” in For Loop
Continue 函数顾名思义,它将终止 for 循环的当前迭代,但会继续执行剩余的迭代。
示例
#use a for loop over a collection #Months = ["Jan","Feb","Mar","April","May","June"] #for m in Months: #print m # use the break and continue statements for x in range (10,20): #if (x == 15): break if (x % 5 == 0) : continue print(x)
预期输出
11 12 13 14 16 17 18 19
当您想从列表中获取特定值时,可以在 for 循环中使用 continue 语句。
在我们的示例中,我们声明了 10-20 的值,但我们只想要那些不能被 5 整除的数字,或者换句话说,那些除以 5 不等于零的数字。
所以,在我们的 range (10,11, 12….19,20) 中,只有 3 个数字(10、15、20)可以被 5 整除,其余的则不能。
因此,除了数字 10、15 和 20 之外,“for 循环”将不会继续,并将那些数字作为输出打印出来。
- 代码行 10 声明变量 x 用于 range (10, 20)。
- 代码行 12 声明 x 除以 5 等于 0 的条件,继续。
- 代码行 13 打印结果。
Python 中的 enumerate() 是什么?
Python 中的 enumerate() 是一个内置函数,用于为可迭代对象的每个项分配索引。它在可迭代对象上添加一个循环,同时跟踪当前项,并以可枚举的形式返回对象。此对象可以在 for 循环中使用,通过使用 list() 方法将其转换为列表。
示例:
Enumerate function 用于对列表中的成员进行编号或索引。
假设我们想为月份(一月、二月、三月、…六月)进行编号,所以我们声明变量 i 来枚举数字,而 m 将打印列表中的月份数量。
#use a for loop over a collection Months = ["Jan","Feb","Mar","April","May","June"] for i, m in enumerate (Months): print(i,m) # use the break and continue statements #for x in range (10,20): #if (x == 15): break #if (x % 5 == 0) : continue #print x
预期输出
0 Jan 1 Feb 2 Mar 3 April 4 May 5 June
代码执行后,enumerate 函数的输出将返回月份名称及其索引号,例如(0-一月)、(1-二月)、(2-三月)等。
- 代码行 3 声明月份列表 [一月、二月、…六月]
- 代码行 4 为 For Loop 声明变量 i 和 m。
- 代码行 5 将打印结果,然后再次进入 For Loop 以枚举其余月份。
实践示例
让我们看另一个使用 For Loop 重复执行相同语句的示例。
Python 循环 | 所有练习的工作代码 |
---|---|
while 循环的代码 |
x=0 while (x<4): print (x) x= x+1 |
For Loop 简单示例 |
x=0 for x in range (2,7): print (x) |
在字符串中使用 for 循环 |
Months = ["Jan","Feb","Mar","April","May","June"] for m in (Months): print (m) |
在 for 循环中使用 break 语句 |
for x in range (10,20): if (x == 15): break print (x) |
在 for 循环中使用 Continue 语句 |
for x in range (10,20): if (x % 5 == 0): continue print (x) |
带“for 循环”的“enumerate function”代码 |
Months = ["Jan","Feb","Mar","April","May","June"] for i, m in enumerate (Months): print (i,m) |
如何使用 for 循环重复执行相同的语句
您甚至可以使用 for 循环重复执行相同的语句。在下面的示例中,我们将单词“guru99”打印了三次。
示例:要重复执行相同的语句多次,我们在变量 i (i in 123) 中声明了数字。因此,当您运行下面的代码时,它将打印语句(guru99)的次数等于我们变量 i (i in 123) 中声明的数字。
for i in '123': print ("guru99",i,)
预期输出
guru99 1 guru99 2 guru99 3
与其他编程语言一样,Python 也使用循环,但它仅限于两种循环:“While loop”和“for loop”,而不是使用各种范围的循环。
- While 循环根据条件语句的真假执行。
- For 循环称为迭代器,它根据设定的条件迭代元素。
- Python For 循环还可用于各种其他用途(指定我们要循环遍历的元素集合)。
- 断点用于 For Loop,以便在任何特定点中断或终止程序。
- Continue 语句将继续打印语句,并根据设定的条件打印结果。
- “for loop”中的 Enumerate function 返回我们正在查看的集合的成员及其索引号。
Python 2 示例
以上代码是 Python 3 示例,如果您想在 Python 2 中运行,请参考以下代码。
# How to use "While Loop" #Example file for working with loops # x=0 #define a while loop while(x <4): print x x = x+1 #How to use "For Loop" #Example file for working with loops # x=0 #define a while loop # while(x <4): # print x # x = x+1 #Define a for loop for x in range(2,7): print x #How to use For Loop for String #use a for loop over a collection Months = ["Jan","Feb","Mar","April","May","June"] for m in Months: print m #How to use break statements in For Loop #use a for loop over a collection #Months = ["Jan","Feb","Mar","April","May","June"] #for m in Months: #print m # use the break and continue statements for x in range (10,20): if (x == 15): break #if (x % 2 == 0) : continue print x #How to use "continue statement" in For Loop #use a for loop over a collection #Months = ["Jan","Feb","Mar","April","May","June"] #for m in Months: #print m # use the break and continue statements for x in range (10,20): #if (x == 15): break if (x % 5 == 0) : continue print x #How to use "enumerate" function for "For Loop" #use a for loop over a collection Months = ["Jan","Feb","Mar","April","May","June"] for i, m in enumerate (Months): print i,m # use the break and continue statements #for x in range (10,20): #if (x == 15): break #if (x % 5 == 0) : continue #print x
输出
0 1 2 3 2 3 4 5 6 Jan Feb Mar April May June 10 11 12 13 14 11 12 13 14 16 17 18 19 0 Jan 1 Feb 2 Mar 3 April 4 May 5 June