1

Expressing index (0-9) with two digits like- 01,02,03... rather 1,2,3... to work with Series(Pandas),
I worte this code in jupyter notebook:

students = {01 : 'Ajoy',
           03 : 'Tanzin',
           04 : 'Sakib',
           07 : 'Rahsed',
           10 : 'Shawon'}
s = pd.Series(students)
s

I got back:

File "<ipython-input-50-ae82df390e04>", line 1
    students = {01 : 'Ajoy',
                 ^
SyntaxError: invalid token

But, when I made a little change, starting index (0-9) with only one digit,the program executed successfully!

students = {1 : 'Ajoy',
           3 : 'Tanzin',
           4 : 'Sakib',
           7 : 'Rahsed',
           10 : 'Shawon'}
s = pd.Series(students)
s

Output :

1       Ajoy
3     Tanzin
4      Sakib
7     Rahsed
10    Shawon
dtype: object

Why I can't start the index using the first method?
Is there any bound to do it?
Or, how can I start expressing the index starts with 2 digits?

Adam.Er8
  • 12,675
  • 3
  • 26
  • 38

2 Answers2

2

How to express the index with 2 digits:

Will it be OK for you if the index is a string? strings can of course be whatever you like, and pandas allows it:

students = {'01' : 'Ajoy',
           '03' : 'Tanzin',
           '04' : 'Sakib',
           '07' : 'Rahsed',
           '10' : 'Shawon'}
s = pd.Series(students)
print(s)

Output:

01      Ajoy
03    Tanzin
04     Sakib
07    Rahsed
10    Shawon
dtype: object

Why you can't use int literals with leading zeroes:

As you can read in the docs for lexical analysis of integer-literals:

Note that leading zeros in a non-zero decimal number are not allowed. This is for disambiguation with C-style octal literals, which Python used before version 3.0.

In short, in C (and in Python before version 3.0), a leading zero before a numeric literal identified an octal literal.
Since Python 3.0, octal literals are identified by 0o, but to avoid the ambiguity, they decided to not allow leading zeroes at all.

example for how confusing it may be:

Python 2.7.5 (default, Apr  2 2020, 13:16:51) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 0777 == 511
True
>>> 
Adam.Er8
  • 12,675
  • 3
  • 26
  • 38
0

leading zeros are not allowed in python 3 you can refer this question SyntaxError invalid token