[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Dear programmers. I am trying to have a complete understanding

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 9
Thread images: 2

File: question.png (3KB, 227x44px) Image search: [Google]
question.png
3KB, 227x44px
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.
>>
>>299491
>>299492
>>299496

+
>>
>>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
>>
File: fib.png (6KB, 322x187px) Image search: [Google]
fib.png
6KB, 322x187px
>>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:
Thread posts: 9
Thread images: 2


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.