Home > Net >  Why is declaring a variable in For loop necessary?
Why is declaring a variable in For loop necessary?

Time:10-16

Im Python beginner and i im really confused with for loop in Python and have trouble understanding it properly. Before transitioning to Python i learned basics of programming using C.

And the way i understand for loop in python is that it iterates over a certain given sequence. And for each element in sequence it executes following block of code.

Therefore for example for this for loop in Python:

for i in range (0,10):
    if i==5:
        i =3
    print i

Equivalent for loop in C would look like this if my understanding is correct:

int r[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
for (int j = 0; j < sizeof(r)/sizeof(r[0]); j  ) {
    int i = r[j];
    if (i == 5)
        i  = 3;
}

The way i understand this is that variable used in c (in this example variable j) for executing block of code certain number of times is not needed.Since now number of elements in sequence determines number of times loop gets executed. Or in other words number of elements represents a value that is used to determine how many times to execute loop.

But then why do we need a variable declared in for loop if we already have value which instructs how many times to execute block of code which is why loops are used in first place ? Why is it not possible to write in Python for example:

a = [1,2,3]
for a:
    print("Hello") 

Shouldn't this loop simply use number of elements as parameter/value that instructs it how many times to execute following code?

Why is additional variable needed in case like this when it doesn't serve any purpose in terms of what we try to accomplish with loop.Which is to execute certain instructions certain number of times?

Im sure im missing something and someone can hopefully help clear up any confusions.

CodePudding user response:

Solution

Python actually has a built-in way of dealing with this, and `for a does not work as it looks like you missed a variable, which the interpreter does not like

Instead, try using for _ in a:, which is the modern convention.

Code

a = [1,2,3]
for _ in a:
    print("Hello") ```

CodePudding user response:

I guess a short answer is: "because Python assumes you may need a reference to the current item while iterating over an iterable"

In the C example you are giving, j is an index so it serves to retrieve the item with r[j], but Python lets you reference the item r[j] directly - and you access it via the variable that you declare.

Python's doc says:

The for statement in Python differs a bit from what you may be used to in C or Pascal. Rather than always iterating over an arithmetic progression of numbers (like in Pascal), or giving the user the ability to define both the iteration step and halting condition (as C), Python’s for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence.

That in itself may explain your confusion when relating to C. Perhaps you are thinking of things like forEach ?

In Python, you have to declare a variable that will reference the current item in the iterable you are iterating over, but that doesn't mean you have to use that reference (hence the examples with _ which is just a convention for a reference that you have no intention to use)

Python lets you iterate over just about anything iterable, the following will work just fine:

a = ["no compelling", "reason to have", "an arithmetic progression of numbers"]
for _ in a:
    print("Hello")
  • Related