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

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

问题描述

如果我写

for i in range(5):打印我

然后它给出 0, 1, 2, 3, 4
这是否意味着 Python 同时为 i 分配了 0、1、2、3、4?
但是,如果我写道:

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

然后我调用a,它只给出5
但是如果我添加 ''print a'' 它会给出 1, 2, 3, 4, 5
所以我的问题是这里有什么区别?
i 是字符串还是列表或其他什么?

或者谁能帮我整理一下:

 for l in range(5):#vs,fs,rs 都是 m*n 矩阵,得到初始值,即 vs[0],fs[0],rs[0] 已知#want 使用这个循环来更新它们vs[l+1]=vs[l]+fs[l]fs[l+1]=((rs[l]-re[l])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,vs,rs数组,对应不同的l值

我怎样才能把它们做成一件?

解决方案

在大多数(如果不是全部)编程语言中,for 循环"是一种多次运行一段代码的机制.

此代码:

for i in range(5):打印我

可以认为是这样工作的:

i = 0打印我我 = 1打印我我 = 2打印我我 = 3打印我我 = 4打印我

所以你看,发生的不是i同时获得0、1、2、3、4,而是顺序.>

我假设当你说调用 a,它只给出 5"时,你的意思是这样的:

for i in range(5):a=i+1打印一个

这将打印给定的 last 值.每次循环迭代时,语句 a=i+1 都会用新值覆盖 a 的最后一个值.

代码基本上是从上到下按顺序运行的,for 循环是一种让代码返回并再次执行某些操作的方法,其中一个变量的值不同.

我希望这能回答您的问题.

If I write

for i in range(5):
      print i

Then it gives 0, 1, 2, 3, 4
Does that mean Python assigned 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 only gives 5
But if I add ''print a'' it gives 1, 2, 3, 4, 5
So my question is what is the difference here?
Is i 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?

解决方案

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 的 range 函数是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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