Python For &While 循环:枚举、中断、继续语句
什么是循环?
循环可以多次执行代码块,直到满足某个条件。它们的使用在编程中相当普遍。不像其他编程语言有for循环、while循环、dowhile等。
什么是 For 循环?
For 循环用于迭代序列的元素。当您有一段代码要重复“n”次时,通常会使用它。
什么是While循环?
While循环用于重复一段代码。它不是运行代码块一次,而是多次执行代码块,直到满足某个条件。
在本教程中,我们将学习
- 如何使用“While Loop”
- 如何使用“For循环”
- 如何使用 For 循环来处理除数字之外的其他事物
- For 循环中的断句
- 在 For 循环中继续语句
- 为 For 循环枚举函数
- 实例
- 如何使用 for 循环一遍又一遍地重复相同的语句
如何使用“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 条件变为 false。
如何使用“For循环”
在 Python 中,“for 循环”被称为 迭代器。
就像while循环一样,“For循环”也用于重复程序。
但与依赖条件真假的while循环不同。 “For 循环”取决于它必须迭代的元素。
示例 :
# #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 循环使用范围内声明的数字进行迭代。
例如,
For循环 对于 x 在 (2,7) 范围内
执行此代码时,它将打印 2 到 7 (2,3,4,5,6) 之间的数字。在这段代码中,数字 7 不被认为在范围内。
For 循环也可以用于一组其他的东西,而不仅仅是数字。我们将在下一节中看到。
如何对字符串使用 For 循环
在这一步中,我们将看到“for 循环”如何用于除数字之外的其他事物。
示例 :
#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:我们将月份(“Jan, Feb, Mar,April,May,June”)存储在变量 Months 中
代码行 4:我们在 Months 中对每个值迭代 for 循环。 Months 的当前值存储在变量 m 中
代码行 5:打印月份
如何在 For 循环中使用 break 语句
Breakpoint 是 For 循环中的一个独特功能,可以让您中断或终止 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 介于 (10, 20) 之间
- 第 11 行代码声明 x==15 处断点的条件,
- 第 12 行代码检查并重复这些步骤,直到达到第 15 行
- 代码行 13 在输出中打印结果
如何在 For 循环中使用“继续语句”
顾名思义,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 整除时不为零。
因此,在我们的范围 (10,11, 12….19,20) 中,只有 3 个数字 (10,15,20) 可以被 5 整除,其余数字则不能。
因此,除了数字 10,15 和 20 之外,“for 循环”将不会继续并打印出这些数字作为输出。
- 第 10 行代码声明变量 x 表示范围 (10, 20)
- 第 12 行代码声明 x 除以 5=0 的条件继续
- 第 13 行代码打印结果
什么是 Python 中的 enumerate()?
在 Python 中枚举() 是一个内置函数,用于为可迭代对象的每个项目分配索引。它在可迭代对象上添加一个循环,同时跟踪当前项目并以可枚举的形式返回对象。该对象可以在for循环中使用list()方法将其转换为列表。
示例 :
枚举函数用于对列表中的成员进行编号或索引。
假设,我们想为我们的月份(Jan、Feb、Marc、….June)做编号,所以我们声明变量 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-Jan)、(1- Feb)、(2- March) 等。
- 代码行 3 声明月份列表 [Jan, Feb,…Jun]
- 代码行 4 为 For 循环声明变量 i 和 m
- 代码行 5 将打印结果并再次进入 For 循环,以便在其余月份进行枚举
实例
让我们看另一个例子,让 For 循环一遍又一遍地重复相同的语句。
Python 循环 | 所有练习的工作代码 |
---|---|
while 循环代码 | x=0 while (x<4): print (x) x= x+1 |
For 循环简单示例 | 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-statement | 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循环”的“枚举函数”代码 | Months = ["Jan","Feb","Mar","April","May","June"] for i, m in enumerate (Months): print (i,m) |
如何使用for循环一遍又一遍地重复相同的语句
您甚至可以使用 for 循环一遍又一遍地重复相同的语句。在这个例子中,我们打印了 3 次“guru99”这个词。
示例:要重复相同语句的次数,我们在变量 i 中声明了数字(i 在 123 中)。因此,当您运行如下所示的代码时,它会打印语句 (guru99),该语句是为我们在 (i in 123) 中的变量声明的数字的多次。
for i in '123': print ("guru99",i,)
输出
guru99 1 guru99 2 guru99 3
与其他编程语言一样,Python 也使用循环,但不是使用一系列不同的循环,而是仅限于两个循环“While 循环”和“for 循环”。
- While 循环根据条件语句的真假执行。
- for循环称为迭代器,它根据条件集迭代元素
- Python For 循环也可以用于一系列其他事情(指定我们要循环的元素集合)
- 在 For 循环中使用断点来在任何特定点中断或终止程序
- Continue 语句会继续打印出语句,并根据条件集打印出结果
- “for循环”中的枚举函数返回我们正在查看的带有索引号的集合成员
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
Python