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?
When I'm teaching someone programming (just about any language) I introduce
for
loops with terminology similar to this code example:... which, conveniently enough, is syntactically valid Python code.
The Python
range()
function simple returns or generates a list of integers from some lower bound (zero, by default) up to (but not including) some upper bound, possibly in increments (steps) of some other number (one, by default).So
range(5)
returns (or possibly generates) a sequence: 0, 1, 2, 3, 4 (up to but not including the upper bound).A call to
range(2,10)
would return: 2, 3, 4, 5, 6, 7, 8, 9A call to
range(2,12,3)
would return: 2, 5, 8, 11Notice that I said, a couple times, that Python's
range()
function returns or generates a sequence. This is a relatively advanced distinction which usually won't be an issue for a novice. In older versions of Pythonrange()
built a list (allocated memory for it and populated with with values) and returned a reference to that list. This could be inefficient for large ranges which might consume quite a bit of memory and for some situations where you might want to iterate over some potentially large range of numbers but were likely to "break
" out of the loop early (after finding some particular item in which you were interested, for example).Python supports more efficient ways of implementing the same semantics (of doing the same thing) through a programming construct called a generator. Instead of allocating and populating the entire list and return it as a static data structure, Python can instantiate an object with the requisite information (upper and lower bounds and step/increment value) ... and return a reference to that. The object then keeps track of which number it returned most recently and computes the new values until it hits the upper bound (and which point it signals the end of the sequence to the caller using an exception called "StopIteration"). This technique (computing values dynamically rather than all at once, up-front) is referred to as "lazy evaluation."
Other constructs in the language (such as those underlying the
for
loop) can then work with that object as though it were a list.For most cases you don't have to know whether your version of Python is using the old implementation of
range()
or the newer one based on generators. You can just use it and be happy. If you're working with ranges of millions of items, or creating thousands of different ranges of thousands each, then you might notice a performance penalty for usingrange()
on an old version of Python. In such cases you could re-think your design and usewhile
loops, or create objects which implement the "lazy evaluation" semantics of a generator, or use thexrange()
version ofrange()
if your version of Python includes it, or therange()
function from a version of Python that uses the generators implicitly.Concepts such as generators, and more general forms of lazy evaluation, permeate Python programming as you go beyond the basics. They are usually things you don't have to know for simple programming tasks but which become significant as you try to work with larger data sets or within tighter constraints (time/performance or memory bounds, for example).
The
range
function wil give you a list of numbers, while thefor
loop will iterate through the list and execute the given code for each of its items.This simply executes
print i
five times, for i ranging from 0 to 4.This will execute
a=i+1
five times. Since you are overwriting the value ofa
on each iteration, at the end you will only get the value for the last iteration, that is 4+1.Useful links:
http://www.network-theory.co.uk/docs/pytut/rangeFunction.html
http://www.ibiblio.org/swaroopch/byteofpython/read/for-loop.html
range(x)
returns a list of numbers from 0 to x - 1.for i in range(x):
executes the body (which isprint i
in your first example) once for each element in the list returned byrange()
.i
is used inside the body to refer to the “current” item of the list. In that case,i
refers to an integer, but it could be of any type, depending on the objet on which you loop.is the same as
It is looping, probably the problem is in the part of the print...
If you can't find the logic where the system prints, just add the folling where you want the content out:
A "for loop" in most, if not all, programming languages is a mechanism to run a piece of code more than once.
This code:
can be thought of working like this:
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:
this will print the last value that a was given. Every time the loop iterates, the statement
a=i+1
will overwrite the last valuea
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.