Dear programmers.
I am trying to have a complete understanding of the first code line.
>What does for n in range(3): do?
I understand that letter (n) can be replaced with a variable.
Since I do not have, the (for n in range(3):) function/command repeats the line below it 3 times.
I understand the second line completely, but how to better understand the first line?
>>299484
http://pythoncentral.io/pythons-range-function-explained/
I don't know the exact terminology, but I will try to explain.
A for loop in other languages is sometimes defined as follows:
for(int i = 0; i < 3; i++)
this will loop while i is less than 3 incrementing by 1 each time.
in python, the range function can except multiple arguments: start, stop, step.
Just putting in 3 means: start at 0 go three times by 1.
The range function in this case produces an array/list of the values for i. Each time the code inside the loop is executed, i takes on the next value [0,1,2]
I don't know that language...
But In think it's the same as C's:
for (i=1; i<=3; i++){
It runs the loop 3 times, changing the value of i each time, though in this case, i isn't used for anything but counting the loops.
So, first time it gets to the print statement,
>i=1
the second iteration
>i=2
the third,
>i=3
then the loop ends
Observe it by printing i from inside the loop.
Something like:
>print ( i, ' Hooray', end=' ')
Above syntax may not be right.
>>299496
>>299492
Python for loops are what other languages call a foreach loop.
In Python, when you go
for x in y
It means "run this code block for every value contained in y, and each time make x that value".
So if y were an array, the code in the loop would run once for each value in the array, and each time x would be the value from the array.
In most modern languages (Java, C# etc.) you can do the same thing with something like
foreach object x in y {/*do something */}
If you use a for loop with range, then it's the same as using it with an array that just contains 1, 2, 3, 4.... ; in the code in >>299484 i isn't even used anyway, so it just runs the for block three times, ignoring i being 0, 1, and 2.
>>299484
range([initial value], [final value], [step]) is an iterator. It basically returns a value each time it is called. Imagine if you had a function like this:
// Some global variable
int global_var;
int range(n){
if(global_var > n){
return false;
}
return ++global_var;
}
Something like that. It's basically a "function" that returns a new value each time it is called. That's an iterator in Python. So in Python when you do
for i in range(3):
You're basically saying "assign i the value returned by the 'function' range until it is no longer a true value " and the function range returns a new integer up to 3, at which point it returns false and the for loop breaks.
>>299484
range(n) gives a list of positive integers up to n including zero. range(3) = (0,1,2). In the program 'i' is arbitrary, could be anything (a dummy variable) but the program is saying for i taking values in the set (0,1,2), do the next line of the program. in this particular program, you're just saying how many times to print hooray. if you were to have print('Hooray',i) you'de get three lines:
Hooray 0
Hooray 1
Hooray 2
>>299510
>It means "run this code block for every value contained in y, and each time make x that value".
Strictly speaking, Python's for-loops are about iterators, not collections. A list just happens to be of the simpler iterable objects. A non-list example would be running a for-loop over a generator: