关于for loops in python的问题Confusion in nested loops

问题描述 投票:0回答:0

好吧,所以我是 python 的新手,对一个数字有点困惑,我无法理解它来自哪里,数字是 2,我理解其他数字为什么以及如何循环和增加,但我只是不明白在哪里2个来自。

这是代码:

for a in range(5):
    print(i)
    for i in range(3):
        print(a)

这是输出。

2
0
0
0
2
1
1
1
2
2
2
2
2
3
3
3
2
4
4
4

我也尝试以 (i,end=" ") (a,end=" ") 方式打印 2 0 0 0 2 1 1 1 2 2 2 2 2 3 3 3 2 4 4 4 为了更多地了解它,我尝试了这个 --> print(i,end=" | ") 2 | 0 0 0 2 | 1 1 1 2 | 2 2 2 2 | 3 3 3 2 | 4 4 4

这让我更加困惑所以如果有人能告诉我这个起始 2 来自哪里或者为什么 2 为什么不是 0 1 2 000 0 1 2 111 .... 0 1 2 444 像这样以及为什么 2

for-loop nested-loops python-3.11
© www.soinside.com 2019 - 2024. All rights reserved.