Because list does not accept [01,02,03,04]

Asked

Viewed 112 times

-4

I’m starting in Python, I want to create a list of numbers, I wanted to use

dezenas = [[01,02,03,04],[05,06,07,08],[09,10,11,12],[13,14,15,16],[17,18,19,20],
           [21,22,23,24],[25,26,27,28],[29,30,31,32],[33,34,35,36],[37,38,39,40],
           [41,42,43,44],[45,46,47,48],[49,50,51,52],[53,54,55,56],[57,58,59,60],
           [61,62,63,64],[65,66,67,68],[69,70,71,72],[73,74,75,76],[77,78,79,80],
           [81,82,83,84],[85,86,87,88],[89,90,91,92],[93,94,95,96],[97,98,99,00]]

You’re making a mistake when you start with exemplo[01],someone can help me!

  • 5

    Why 01 ? The number is 1

1 answer

14

"It is wrong because it is wrong" :D

Python 2

In Python 2, your syntax would be wrong, as numbers prefixed by 0 would be octal, and there would be no octal 08 (valid octal digits are 0 to 7).

Also, the results probably wouldn’t be what you expect if you used in situations like 027, for example. See this Python 2 print:

>>> print 027 + 3
26

This is because 027 in octal is 23 in decimal.


Python 3

In Python 3, to avoid this confusion, it was decided not to accept the prefix zero, forcing the programmer to explain an octal in this way: 0o27, thus eliminating ambiguities.

In this way, a sequence of numbers started with zero is an invalid token. Except for decimals like 0.82 - who curiously accept being written as 0000.82.

I think by now you’ve noticed that the solution is not to invent fashion, and represent numbers as they are:

dezenas = [[ 1, 2, 3, 4],[ 5, 6, 7, 8],[ 9,10,11,12] ...

You can use a little bit of space to align, if that’s the problem.

  • 1

    "It’s wrong because it’s wrong" :D - I couldn’t avoid a little haha laugh :D

  • 1

    @Isac I could not get better words to say, was in the sense of "it’s not about anything obscure, it’s elementary error of syntax even, as indicated in the output" :D

Browser other questions tagged

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