pojna
pojna

Reputation: 41

Python, range(), double loops,

Codes and result are shown below.

I'm curious about the prints beginning wiht 1 instead of 0 as start. Where does the program get 1 from?

Can someone please help me here? Thanks!

for i in range(5) :
    for j in range(i) :
        print(i, end=" ")
    print()

1
2 2
3 3 3
4 4 4 4

The same result with codes below:

for i in range(5) :
    for j in range(0, i) :
        print(i, end=" ")
    print()

By altering (0, i) to (1, i), it's also logically omitted 1, but how does it come to a single 2 as the result shown below?

for i in range(5) :
    for j in range(1, i) :
        print(i, end=" ")
    print()    

2
3 3
4 4 4

Upvotes: 0

Views: 190

Answers (1)

ScienceSnake
ScienceSnake

Reputation: 617

Because for j in range(0): loops 0 times, so it never prints i when its 0. If you look closely at your output, you'll see that the first line is actually blank.

Upvotes: 2

Related Questions