[英]Python - Why do we have to initialise the identifier in while loop but not in for loop?
I learned Visual Basic before and now I'm learning Python. 我以前学习过Visual Basic,现在我正在学习Python。 I realized that in Python, we don't have to initialize a value for the identifier in
for loop
, but it's the other way round in while loop
. 我意识到在Python中,我们不必在
for loop
初始化标识符的值,但是在while loop
却是另一种方式。 It looks unfamiliar to me as in Visual Basic, we have to declare a variable as the identifier in both loops. 对我来说,这看起来很陌生,就像在Visual Basic中一样,我们必须在两个循环中都将变量声明为标识符。
For example, from Python wiki, 例如,在Python Wiki中,
x = 1
while True:
print "To infinity and beyond! We're getting close, on %d now!" % (x)
x += 1
runs perfectly. 运行完美。 However, after I commented
x = 1
, it generates an error. 但是,在我评论
x = 1
,它会产生一个错误。 Why can't it run like for loop
where x
starts from 0? 为什么它不能像
for loop
那样运行for loop
其中x
从0开始?
In addition, even if we assign a value for the identifier in for loop
, for example, 另外,例如,即使我们为
for loop
分配标识符的值,
num = 10
for num in range(5):
print num
print num
the loop will overwrite the value of the assignment. 循环将覆盖分配的值。 Why is it so?
为什么会这样呢?
I know there are plenty of for loop
and while loop
questions here, but mostly they are case studies of OP's code. 我知道这里有很多
for loop
和while loop
问题,但大多数情况下都是OP代码的案例研究。 What I want is a more general explanation about the Python structure itself and some underlying operations of the language when for loop
or while loop
is executed. 我想要的是对Python结构本身以及执行
for loop
或while loop
时语言的一些基本操作的更一般的解释。 Also, in this case, I'm referring to Python 2.7.11. 同样,在这种情况下,我指的是Python 2.7.11。 I will be very grateful if you can point out the difference in terms of the structure for 3.5.1.
如果您能指出3.5.1在结构上的差异,将不胜感激。 Thanks a lot.
非常感谢。
x = 1
while True:
print "To infinity and beyond! We're getting close, on %d now!" % (x)
x += 1
If you comment x = 1
, this line raises an error: 如果注释
x = 1
,则此行将引发错误:
x += 1
because it means, x = x + 1
, as x is not defined, it raises an error(python doesn't know for what value add 1) 因为这意味着
x = x + 1
,因为未定义x,所以会引发错误(python不知道加1的值)
num = 10
for num in range(5):
print num
print num
you're using the same name for both variables, inside loop and outside of this, that's why you have another value of num
inside a loop. 您在循环内和循环外都对两个变量使用了相同的名称,这就是循环内使用另一个
num
值的原因。
Good practice is to have another name for variables inside a loop variable, because you really don't need to have the same for loop variable, as you have outside a loop 优良作法是在循环变量内使用变量的另一个名称,因为您确实不需要在循环外使用相同的for循环变量
so, you can write something like this: 因此,您可以编写如下内容:
num = 10
for i in range(5):
# play with num and i variables
num += i * 2
print num
because the 'range' says you what is the succession of values and define num as a local value. 因为“范围”告诉您什么是值的连续性,并将num定义为局部值。 You see few words but there is a more complex process.
您看不到任何文字,但是过程更加复杂。
An example may be Javascript language. 一个示例可以是Java语言。 In the for loop, you see the initial value of the value iterated through the loop, you see the maximal value and the incrementation.
在for循环中,您可以看到循环迭代的值的初始值,可以看到最大值和增量。 for(var num = 0; var <5; var+=1) in python you see for num in range(5).
for(var num = 0; var <5; var + = 1)在python中,您看到range(5)中的num。 THe while loop is more explicit.
while循环更为明确。
The fact that num is a name used for a global variable and a local variable may cause confusion. num是用于全局变量和局部变量的名称这一事实可能会引起混淆。 In reality the num that you see in the loop is an other variable.
实际上,您在循环中看到的num是另一个变量。
Because Python's for
loop is more like C#'s foreach
or JS's for of
, 由于Python的
for
循环更像是C#的foreach
或JS的for of
,
whereas VB's for
is more like Pascal's for
. 而VB的
for
更像Pascal的for
。
I'll use Pascal instead of VB, because I know it better. 我将使用Pascal而不是VB,因为我更了解它。
Pascal's for
: 帕斯卡
for
:
var i: byte;
begin
for i := 0 to 5 do
begin
writeln(i);
end;
end.
Each Pascal's for
loop can be easily replicated in Python using range
: 每个Pascal的
for
循环都可以使用range
轻松地在Python中复制:
for i in range(5):
print(i)
But, Python's for
is something more: 但是,Python的
for
更多的东西:
for c in "HelloWorld!":
print(c)
You cannot replicate it in Pascal without directly dealing with indexes. 如果不直接处理索引,则无法在Pascal中复制它。
Pascal's and VB's for
works with indexes – they iterate from some index to another, whereas Python's for
loop works with iterables (array, strings, files, generators and much more). Pascal和VB的
for
可以与索引一起使用-它们从某个索引迭代到另一个索引,而Python的for
循环可以与可迭代对象(数组,字符串,文件,生成器等等)一起使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.