+ 2

Doubts with for loop

If I do this what happens? for _ in range(10): for _ in range(64): … And what if I wanted to use that variable that iterates in the range function?

10th Jul 2024, 7:44 PM
Giovanni Paolo Balestriere
Giovanni Paolo Balestriere - avatar
6 Respuestas
+ 9
Giovanni Paolo Balestriere you can do that in Python, and it behaves nicer than in other languages. The outer loop begins iterating through its range, assigning _ to the first range value. The inner loop reassigns _ to the first range value in its own range, then iterates through its range. At the end of the inner loop, _ will equal 64. It will stay at 64 until the outer loop begins the next iteration. Then _ gets assigned the next value from the outer loop range. The cycle repeats.
10th Jul 2024, 10:38 PM
Brian
Brian - avatar
+ 7
run the code. observe what happens.
10th Jul 2024, 8:06 PM
Lisa
Lisa - avatar
+ 5
If you want to use the loop variable inside the loop, then give it a meaningful name. By convention, the underscore means that the value is irrelevant and you don't care and won't use it.
11th Jul 2024, 3:32 AM
Tibor Santa
Tibor Santa - avatar
+ 4
Giovanni Paolo Balestriere , to get a better understanding of how the loops are working, you can use different value ranges. for outer_ in range(3): # values: 0, 1, 2 for inner_ in range(4, 7): # values: 4, 5, 6 print(f'outer loop: {outer_}, inner loop: {inner_}') result will be: outer loop: 0, inner loop: 4, sum of outer_ + inner_: 4 outer loop: 0, inner loop: 5, sum of outer_ + inner_: 5 outer loop: 0, inner loop: 6, sum of outer_ + inner_: 6 outer loop: 1, inner loop: 4, sum of outer_ + inner_: 5 outer loop: 1, inner loop: 5, sum of outer_ + inner_: 6 outer loop: 1, inner loop: 6, sum of outer_ + inner_: 7 outer loop: 2, inner loop: 4, sum of outer_ + inner_: 6 outer loop: 2, inner loop: 5, sum of outer_ + inner_: 7 outer loop: 2, inner loop: 6, sum of outer_ + inner_: 8
11th Jul 2024, 6:10 AM
Lothar
Lothar - avatar
+ 3
Save yourself (and your code reader) one trouble, please use meaningful variable names. Use of underscore for variable name here can cost you when you want to use the variable. Go ask yourself this question "Which _ variable is it? is it one from the outer loop? or is it one from inner loop?"
10th Jul 2024, 9:01 PM
Ipang
+ 1
[Python] In Python, using the underscore (`_`) as the variable name in a loop is a convention to indicate that the variable is intentionally unused. In the provided nested loops: ```python for _ in range(10): for _ in range(64): # some code ``` the inner loop will run 64 times for each iteration of the outer loop, resulting in a total of \(10 \times 64 = 640\) iterations of the innermost code block. However, if you want to use the variable that iterates in the `range` function, you should give it a meaningful name instead of `_`. Here's how you can do it: ```python for i in range(10): for j in range(64): print(f"Outer loop iteration: {i}, Inner loop iteration: {j}") ``` In this example, `i` is used for the outer loop and `j` for the inner loop. You can then use `i` and `j` within the loop bodies as needed. This way, you can perform operations that depend on the current iteration indices.
12th Jul 2024, 1:22 AM
Harimamy Ravalohery
Harimamy Ravalohery - avatar