Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a for-loop more understandable in python? [closed]

Tags:

python

loops

I am currently teaching some python programming to some fairly young students. One thing I want them to learn is how to write a for-loop.

So far, the way I have shown it to the students is like this:

for i in range(1,11):
    print(i)

which gives a loop where i goes from 1 to 10.

My problem is that it seems strange to students that they need to write 11 as the second argument to range(1,11) when they want the loop to go up to 10. The students find this confusing.

In C/C++ and related languages, such a loop can be written like this:

for(int i = 1; i <= 10; i++)
    { /* do something */ }

It seems to me like the C++ way of expressing the loop is more intuitive since in that case I can explicitly write 1 and 10 which are the first and last values that I want the loop variable to take.

When working with for-loops in python, I end up telling the students something like "we just have to accept that we need to write 11 when we want the loop to go to 10, it is a bit annoying but you just have to learn that the range function works in that way". I am not happy about that; I want them to learn that programming is fun and I am afraid this kind of thing makes it less fun.

Since python is often described as a language emphasizing readability, I suspect there is a nicer way to express a for-loop, a way that would cause less confusion for my students.

Is there a better and/or less confusing way to express this kind of for-loop in the python language?

like image 658
Elias Avatar asked May 08 '19 12:05

Elias


3 Answers

You could show them this code for a better understanding:

start = 1
length = 10
for i in range(start,start+length):
    print(i)

There is also another feature that works like this, it's called slice.

like image 145
Gábor Fekete Avatar answered Nov 15 '22 04:11

Gábor Fekete


Remind them that there is a reason the range function works this way. One helpful property of it is that the number of times the loop will run is equal to the second argument of range minus the first argument.

I think people get really hung up on this, but the fact is for loops in Python are very different than from C. In C, for loops are basically a wrapper around a while loop.

These two examples should help show the difference between how loops work in C versus python.

# for(int x=1; x <= 10; x++)
x = 1
while x <= 10:
    print(x)
    x += 1


i = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]  # range(1, 11)
for x in i:
    print(i)

But honestly, the real problem here is that all loops and arrays are easier to understand and work with if they start at zero, not one. Please consider adjusting your examples to start at zero.

This way, if you want to loop 10 times, you use the number 10.

   # for(int x=0; x < 10; x++)
x = 0
while x < 10:
    print(x)
    x += 1


i = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]  # range(10)
for x in i:
    print(i)
like image 17
soundstripe Avatar answered Nov 15 '22 03:11

soundstripe


I believe there are two simple ways to answer the question. 1) One way to explain this answer is by using mathematical notation half closed interval [a,b). In this interval, one endpoint is included (In this example it is 'a' ) but not the other endpoint ('b'). So for your example,

for i in range(1,11):
     print(i)

(1,11) is a half closed interval where a and b are 1 and 11 respectively.

2) You can also explain using the following examples

    for i in range(1,11)  //in python 
        {do something}

    for(int i=1;i<11;i++)  //in C++
        {do something}

In both of these case, i iterates from 1 to 10. This seems more intuitive to me.

like image 8
Saurav Rai Avatar answered Nov 15 '22 03:11

Saurav Rai