What is the range defined by range in Python?

Asked

Viewed 197 times

-4

When I define

for i in range (5):

My range will run through 0,1,2,3,4,5 or 1,2,3,4,5

  • 5

    Out of curiosity, why didn’t you test it before asking?

  • 1

    It depends on the rest of the code and the environment. You can even play a song, depending on the modifications and version of the interpreter. You can give more details of what went wrong when you tried?

2 answers

1

Will generate 0, 1, 2, 3 and 4 Normally, the first range parameter is the initial number, the second is the final but excluding one. In its example, range(5), python assumes that the initial parameter is 0. It will generate the numbers based on this condition: greater/equal to zero and less than 5.

-1

In Python the count starts from scratch, then:

list(range(5))
[0,1,2,3,4]
  • list(range(5)) returns [0, 1, 2, 3, 4]

  • opa, it was my mistake here in the rush to type. I will edit.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.