Python for 循环和 while 循环(附示例)
⚡ 智能摘要
Python 循环会重复执行一段代码,直到满足特定条件为止。for 循环和 while 循环,以及 break、continue 和 numerate 语句,共同控制循环执行的次数,如下例所示。

什么是循环?
循环可以多次执行代码块,直到满足某个条件。它们在编程中相当常见。与其他具有 For 循环、while 循环、dowhile 等的编程语言不同。
什么是 For 循环?
For loop is used to iterate over elements of a sequence. It is often used when you have a piece of code which you want to repeat "n" number of time.
什么是 While 循环?
While 循环用于重复执行代码块。它不是运行代码块一次,而是多次执行代码块,直到满足某个条件。
如何使用“While 循环”
While loop does the exactly same thing what "if statement" does, but instead of running the code block once, they jump back to the point where it began the code and repeats the whole process again.
句法
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
- Code 第 4 行:变量 x 被设置为 0
- Code 第 7 行:while 循环检查条件 x<4。x 的当前值为 0,条件为真。控制流进入 while 循环。
- Code 第 8 行:打印 x 的值
- Code 第 9 行:x 的值加 1。控制流返回到第 7 行。此时 x 的值为 1,小于 4。条件为真,while 循环再次执行。如此循环,直到 x 变为 4,while 循环条件变为假。
如何使用“For 循环”
In Python, "for loops" are called 迭代器。
Just like while loop, "For Loop" is also used to repeat the program.
But unlike while loop which depends on condition true or false. "For Loop" depends on the elements it has to iterate.
例子:
# #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 循环使用范围内声明的数字进行迭代。
例如,
对于循环 对于 x 在范围 (2,7) 内
执行此代码时,它将打印 2 到 7 之间的数字 (2,3,4,5,6)。在此代码中,数字 7 不被视为在范围内。
For 循环还可以用于一组其他事物,而不仅仅是数字。我们将在下一节中看到。
如何使用 For 循环处理字符串
In this step, we will see how "for loops" can also be used for other things besides numbers.
例子:
#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
Code Line 3: We store the months ("Jan, Feb , Mar,April,May,June") in variable Months
Code 第 4 行:我们对 Months 中的每个值执行 for 循环。Months 的当前值存储在变量 m 中。
Code 第5行:打印月份
如何在 For 循环中使用 break 语句
断点是 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 时终止并停止执行。为此,我们定义了一个 break 函数,即 `(x==15): break`,这样当代码遇到数字 15 时,程序就会终止。 Code 第 10 行声明变量 x,其范围为 (10, 20)
- Code 第 11 行声明了 x==15 处的断点条件,
- Code 第 12 行检查并重复这些步骤,直到达到第 15 行为止。
- Code 第 13 行 将结果打印到输出中
如何在 For 循环中使用“continue 语句”
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 语句。
In our example, we have declared value 10-20, but between these numbers we only want those number that are NOT divisible by 5 or in other words which don't give zero when divided by 5.
因此,在我们的范围 (10,11, 12....19,20) 中,只有 3 个数字 (10,15,20) 可以被 5 整除,其余的则不能。
So except number 10,15 & 20 the "for loop" will not continue and print out those number as output.
- Code 第 10 行声明变量 x,其范围为 (10, 20)。
- Code 第 12 行声明条件:x 除以 5 等于 0,继续
- Code 第 13 行打印结果
enumerate() 是什么 Python?
Python 中的 enumerate() 是一个内置函数,用于为可迭代对象的每个项分配索引。它会在可迭代对象上添加一个循环,同时保持循环结束。ping trac获取当前项的索引,并以可枚举的形式返回该对象。该对象可以在 for 循环中使用 list() 方法转换为列表。
例子:
枚举函数 用于对列表中的成员进行编号或索引。
假设我们想要对月份进行编号(一月、二月、三月……六月),因此我们声明变量 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
执行代码时,枚举函数的输出返回带有索引号的月份名称,如(0-Jan)、(1-Feb)、(2-March)等。
- Code 第 3 行声明月份列表 [ Jan, Feb,…Jun]
- Code 第 4 行声明了用于 For 循环的变量 i 和 m。
- Code 第 5 行将打印结果,然后再次进入 For 循环,枚举剩余的月份。
实例
让我们看另一个 For 循环重复相同语句的例子。
| Python 循环 | 工进 Code 所有练习 |
|---|---|
| Code for 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 语句 |
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)
|
| Code for "enumerate function" with "for loop" |
Months = ["Jan","Feb","Mar","April","May","June"]
for i, m in enumerate (Months):
print (i,m)
|
如何使用 for 循环重复相同的语句
You can use for loop for even repeating the same statement over and again. Here in the example we have printed out word "guru99" three times.
示例:为了重复相同的语句次数,我们在变量 i 中声明了数字(i in 123)。因此,当您运行如下所示的代码时,它会打印语句(guru99),该语句是我们在变量中声明的数字的多次(i in 123)。
for i in '123':
print ("guru99",i,)
预期产量:
guru99 1 guru99 2 guru99 3
与其他编程语言一样, Python also uses a loop but instead of using a range of different loops it is restricted to only two loops "While loop" and "for loop".
- While 循环是根据条件语句的真假来执行的。
- For 循环称为迭代器,它根据设置的条件迭代元素
- Python For 循环还可以用于其他各种事物的集合(指定我们想要循环的元素集合)
- 断点用于 For 循环中,在特定点中断或终止程序
- Continue语句会继续打印出该语句,并按照设置的条件打印出结果
- Enumerate function in "for loop" returns the member of the collection that we are looking at with the index number
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
