Python的范围函数是如何工作的? [英] How does the Python's range function work?

查看:109
本文介绍了Python的范围函数是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有我在以下的循环中都不是很了解,谁能提供一些提示?提前致谢

  for我在范围(5):
打印我

然后它给0,1,2 3,4是python把0,1,2,3,4同时分配给我的?但是如果我写道:

 我在范围(5)中:
a = i + 1

然后我调用一个,它只给出5,但是如果我添加''打印一个''它给1,2, 3,4,5。所以我的问题是这里有什么区别?我是一个字符串或列表或别的东西?或者任何人都可以帮我理清:

pre $ 在范围内(5):
#vs,fs ,rs都是m * n个矩阵,得到了初始值,即vs [0],fs [0],rs [0]是已知的
#wnt使用这个foor循环来更新它们
vs [ 1 + 1] = vs [1] + fs [1]
fs [l + 1] =(rs [1] -re [1])$ ​​b $ b rs [l + 1] = rs [ l] + vs [l]
#然后这段代码给vs,fs,rs

如果我运行这种代码,那么只有当l = 5时,我才能得到答案...我怎么能让他们开始循环?即l = 0获得vs [1],fs [1],rs [1 ],那么l = 1就得到了vs [2],rs [2],fs [2] ......等等的值,但python给出了不同的fs数组,对应于不同的值我怎样才能使他们成为一块?只是更新行,谢谢

解决方案

所有的编程语言都是一种运行一段代码的机制。



这段代码:

 对于范围(5)中的我:
print i

可以这样认为:
$ b $ pre $ i = 0
print i
i = 1
print i
i = 2
print i
i = 3
print i
i = 4
打印i

所以你看,发生了什么不是 i 同时获得值0,1,2,3,4 ,而是按顺序。



I假设当你说call a,它只给5时,你的意思是这样的:

  ):
a = i + 1
打印一个

em> last 值。每次循环迭代时,语句 a = i + 1 都会覆盖最后一个值 a
$ b

代码基本上按顺序运行,从上到下,for循环是使代码返回并重新运行的一种方式,对于其中一个变量。

我希望这能回答你的问题。


all,I am not really understand for loop in the following,can anyone give some tips?thanks in advance

for i in range(5):
      print i

then it gives 0,1,2,3,4 is that python assign 0,1,2,3,4 to i at the same time? however if i wrote:

for i in range(5):
      a=i+1

then I call a, it gives 5 only, but if i add ''print a'' it gives 1,2,3,4,5 .So my question is what is the difference here? i is a string or a list or something else? or maybe can anyone help me to sort out:

for l in range(5):
    #vs,fs,rs are all m*n matrixs,got initial values in,i.e vs[0],fs[0],rs[0] are known
    #want use this foor loop to update them
    vs[l+1]=vs[l]+fs[l]
    fs[l+1]=((rs[l]-re[l])
    rs[l+1]=rs[l]+vs[l]
#then this code gives vs,fs,rs

if i run this kind of code, then I will get the answer only when l=5...how can I make them start looping?i.e l=0 got values for vs[1],fs[1],rs[1], then l=1 got values for vs[2],rs[2],fs[2]......and so on.but python gives different arrays of fs,vs,rs, correspond to different value of l, how can I make them one piece? just update rows,thanks

解决方案

A "for loop" in most, if not all, programming languages is a mechanism to run a piece of code more than once.

This code:

for i in range(5):
    print i

can be thought of working like this:

i = 0
print i
i = 1
print i
i = 2
print i
i = 3
print i
i = 4
print i

So you see, what happens is not that i gets the value 0, 1, 2, 3, 4 at the same time, but rather sequentially.

I assume that when you say "call a, it gives only 5", you mean like this:

for i in range(5):
    a=i+1
print a

this will print the last value that a was given. Every time the loop iterates, the statement a=i+1 will overwrite the last value a had with the new value.

Code basically runs sequentially, from top to bottom, and a for loop is a way to make the code go back and something again, with a different value for one of the variables.

I hope this answered your question.

这篇关于Python的范围函数是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆