I understand the concept of FOR LOOP but having trouble understand the code sequence.

Any advice on how to understand the logic and structure of code better? How to apply the for loop syntax in different situations? Thanks!

Hey @zain.ul.zafar,

It’d be helpful if you could share some examples that are giving you trouble. Maybe a question from one of the lessons?

Anyhow, check out this article from w3 Schools on for loops, they’re a good resource for any and all coding questions.

Hope it helps!

values = [16, 1, 7, 2, 19, 12, 5, 20, 2, 10, 10, 14, 17, 14, 1, 16, 19, 7, 9, 19]

Increase by one each value contained in the `values`

list.

solution:

for i in range(len(values)):

values[i] += 1

Can someone explain what’s happening here?

Why woudnt this run:

for x in values:

values += 1

Hey @zain.ul.zafar,

Here’s what’s going on:

for i in range(len(values)):

Here, we’re iterating over the range of numbers that is the length of the list. So, if the list is 10 members long, i will equal 1, then 2, then 3 etc. during each iteration of the loop until it reaches 10 when the loop will end.

values[i] += 1

Here we add one to the member of the list at index i and store that back in the list at the same location.

So that’s how the solution works, now let’s look at your alternative:

for x in values:

values += 1

Here’s the trouble: you’re iterating over the list called “values”, not over a series of numbers. So, for example, the first time the loop runs it will do so with x = 16 (since this is the first number in the list called “values”). Then, the second time the loop runs, it will do so with x = 1, the third time it runs x = 7 and so on.

Hopefully you can see the problem now, the second part of your code reads:

values += 1

So, in the first iteration, x = 16 and so we add 1 to the item in the list at values[16], and you can do this, it won’t give you an error (unless one of the numbers in the list is greater than the length of the list. But it won’t do what you want the code to do, which is to add one to each member of the list.

Hope this helps!

Thanks @Johnsonk51502

The only different between:

for i in range(len(values)):

values[i] += 1

vs

for x in values:

values += 1

is that in the 2nd option we are defining a range for the loop to end. I get that. But that only means that

(for x in values:

values += 1)

the loop should run forever no?

Also wouudnt the range() function end before the last value in the list?

For e.g:

for x in range(6):

print(x)

0

1

2

3

4

5

Another question on the same topic:

What is the difference between:

values = [5, 4, 7, 8, 9, 3]

values_copy =

for v in range(len(values)):

values_copy.append(values[v])

print(values_copy)

vs

values = [5, 4, 7, 8, 9, 3]

values_copy =

for v in values:

values_copy.append(v)

Please update & format your code lines. I see checkboxes instead of

`values <something should be here>`

.

It becomes a bit difficult for us to understand your queries and answer them correctly.

From what I can make out:

- range function has three parameters: start (included), stop (excluded) & step (the incremental value). So the stop value will always be excluded from a range function.

```
So range(1, 6, 1) will give the output
>>> [1, 2, 3, 4, 5]
And range(7, -2, -2) will give the output
>>> [7, 5, 3, 1, -1]
```

Have you tried the codes provided here on your own using `print()`

? What was the actual output? What was the expected output? How were these two different. Please share with us this info.

values = [5, 4, 7, 8, 9, 3]

values_copy =

for v in range(len(values)):

values_copy.append(values[v])

print(values_copy)

vs

values = [5, 4, 7, 8, 9, 3]

values_copy =

for v in values:

values_copy.append(v)

They give the same and correct answer. wanted to understand the difference in coding logic.

and understand the RANGE logic.

My question is that since the end is exclude when use it in the following scenario:

range(len(values) - woud’nt the last element in the list values excluded and give us an inaccurate result? Please seen Johnson’s reply above for reference.

Thanks for the help

for the range and exclusion of last value refer to this post

In the first case `len()`

method first calculates the length/ size/ count of elements in values list. Then this length becomes the `stop`

(upper limit) for the `range()`

method. The above post will answer your question about why the actual last value is always `stop - step`

**(in this case step = 1).**

The for loop variable `v`

then takes the values as given by the range method, which is then used as index values in code `values[v]`

. So in every iteration, the index value is incremented by 1 and the next item in the `values`

list is appended to the `values_copy`

list.

The variable v will have values from 0, 1, … and so on.

The second `for loop`

directly iterates over the `values`

list. Here variable `v`

takes direct value from the list instead of index position.

Here the variable v will take values as 5, 4, …, 3, i.e. direct elements from the `values`

list.

By the way, the formatted code would look like this:

```
values = [5, 4, 7, 8, 9, 3]
values_copy = []
# additional code
values_copy2 = []
for v in range(len(values)):
values_copy.append(values[v])
# additional code
values_copy2.append(v)
# additional code
print(values_copy, values_copy2)
```

vs.

```
values = [5, 4, 7, 8, 9, 3]
values_copy = []
for v in values:
values_copy.append(v)
```

Try the additional codes and let us know if you noticed a difference.

My Code:

```
for x in range(len(values1)):
```

Hello

I am confused about when in for loop we have to define the end of the loop using range and len functions (as above) and when not to do that. The syntax above would have worked without defining the loop with Range Length function as well. I am not able to see a pattern when we have to define with Range/Len and when can proceed without it. Can someone please educate me on how to distinguish between situations where and how how to recognize when we have to use a Range(len function to define a for loop and when not?

Thank You!

Screen Link:

My Code:

Find the value with the highest frequency.

values = [72, 50, 48, 50, 7, 66, 62, 32, 33, 75, 30, 85, 6, 85, 82, 88, 30, 32, 78, 39, 57, 96, 45, 57, 61, 32, 10, 62, 48, 32, 96, 75, 15]

```
most_frequent = values[0]
for value in values:
if values.count(value) > values.count(most_frequent):
most_frequent = value
print(most_frequent)
I do not understand the code.
What does most_frequent = values[0] mean?
what does values.count(value) > **values.count(most_frequent)** mean?
why is most_frequent = values[0] and not most_frequent = values[]?
Thank you!!!
```

The best way to learn how to code is to break it down and run one piece at a time! Then try to see how the combined code works (which also should be done in an incremental approach!).

**For Range and Len**

- If a list (keeping list for simplicity) has a fixed no. of elements then you can iterate over it directly. For example, grades - A to F. There will always be 6 elements in this list. So a simple:

```
for grade in grades:
# do something
or
for grade in ["A", "B", "C", "D", "E", "F"]:
# do something
```

will work.

When the no. of elements are not fixed in a list and its length keeps fluctuating we use the range and len function. For example, students logged in at 10 am, 1 pm and 5 pm at Dataquest.

It could be that most students prefer or get time to learn after 5 pm. So more no. of students at that time. Less no. of students at 10 pm. And at 1 pm (hypothetically regardless of our different time zones) most of us are having lunch so 0 or very few students.

Now is there a point in writing 24 `for loops`

for each hour in a day assuming the same of no. of students log in? No right!

Instead, we can use the length as the variable according to which the no. iterations for the `for loop`

is decided and executed. Hence, the below code will work, irrespective of how many students are logged in, in that hour:

```
for num_students in range(len(students_loggedin)):
# do something
```

You haven’t responded to my previous post yet. So for your other query, please write the code in a Jupyter Notebook and share with us how you broke it down and experimented with it. For instance, show us what was the result you got for these updated code lines:

```
most_frequent = values[0]
print(most_frequent )
for value in values:
# if values.count(value) > values.count(most_frequent):
# most_frequent = value
print(value, values.count(value), values.count(most_frequent))
```