亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> Python

Python For &While 循环:枚举、中断、继续语句

什么是循环?

循环可以多次执行代码块,直到满足某个条件。它们的使用在编程中相当普遍。不像其他编程语言有for循环、while循环、dowhile等。

什么是 For 循环?

For 循环用于迭代序列的元素。当您有一段代码要重复“n”次时,通常会使用它。

什么是While循环?

While循环用于重复一段代码。它不是运行代码块一次,而是多次执行代码块,直到满足某个条件。

在本教程中,我们将学习

如何使用“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

如何使用“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) 之间

如何在 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 循环”将不会继续并打印出这些数字作为输出。

什么是 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) 等。

实例

让我们看另一个例子,让 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 循环”。

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

  1. C# while 和 do...while 循环
  2. C# for 循环
  3. C# 中断语句
  4. C# continue 语句
  5. C++ 中断语句
  6. C for 循环
  7. Python 语句、缩进和注释
  8. Python pass 语句
  9. Java中使用while和for循环的回文数程序
  10. Python For &While 循环:枚举、中断、继续语句
  11. Python range() 函数:Float、List、For 循环示例
  12. Python 中的 Enumerate() 函数:循环、元组、字符串(示例)