7 Replies - 526 Views - Last Post: 08 July 2013 - 11:17 AM Rate Topic: -----

#1 datus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 08-July 13

Python syntax error...not sure why?

Posted 08 July 2013 - 10:28 AM

For input 1,000,000 in python shell 3.3.2 i got (1, 0, 0) as an output and i got the point that python interprets 1,000,000 as comma-separated sequence of integers.

But for 6,09 i got syntax error. Why it is so?

Is This A Good Question/Topic? 0
  • +

Replies To: Python syntax error...not sure why?

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7965
  • View blog
  • Posts: 13,589
  • Joined: 19-March 11

Re: Python syntax error...not sure why?

Posted 08 July 2013 - 10:33 AM

An int with a leading 0 is read as octal, so 9 is not a legitimate token.
Try 6.017 and see what you get. (hint: python displays in decimal, regardless of the input base)
Was This Post Helpful? 2
  • +
  • -

#3 datus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 08-July 13

Re: Python syntax error...not sure why?

Posted 08 July 2013 - 10:50 AM

View Postjon.kiparsky, on 08 July 2013 - 10:33 AM, said:

An int with a leading 0 is read as octal, so 9 is not a legitimate token.
Try 6.017 and see what you get. (hint: python displays in decimal, regardless of the input base)



python is displaying list even with mixed type input
>>> 3,'hello',4
(3, 'hello', 4)
>>> 3,4.5,9
(3, 4.5, 9)
>>> 3,09,9
SyntaxError: invalid token


so you mean an octal integer is an invalid token in python?...
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7965
  • View blog
  • Posts: 13,589
  • Joined: 19-March 11

Re: Python syntax error...not sure why?

Posted 08 July 2013 - 11:00 AM

No, I mean that 9 is not an octal digit. Just like 'a' is not a valid decimal digit, but you can use it if you're inputting hex:

>>> 0xaa
170

>>> 0aa
ERROR!
Was This Post Helpful? 0
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2151
  • View blog
  • Posts: 3,308
  • Joined: 21-June 11

Re: Python syntax error...not sure why?

Posted 08 July 2013 - 11:04 AM

No, he means an octal integer literal containing the digit 9 (or, for that matter, 8) is an invalid token as octal numbers can't contain those digits. Writing 017 would be fine (and behave the same way as writing 15), but 09 is illegal because that's not a valid number in base 8.

PS: Using the comma like that creates a tuple, not a list (a list would have square brackets around it - not round ones). And yes, tuples (as well as lists) can contain values of different types.
Was This Post Helpful? 0
  • +
  • -

#6 datus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 08-July 13

Re: Python syntax error...not sure why?

Posted 08 July 2013 - 11:08 AM

View Postjon.kiparsky, on 08 July 2013 - 11:00 AM, said:

No, I mean that 9 is not an octal digit. Just like 'a' is not a valid decimal digit, but you can use it if you're inputting hex:

>>> 0xaa
170

>>> 0aa
ERROR!


ok... I got your point...
then in that case as it is converting hex to decimal, it should also convert octal to decimal, but that is not the case....
>>> 0xaa
170
>>> 02
SyntaxError: invalid token
>>> 

Was This Post Helpful? 0
  • +
  • -

#7 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2151
  • View blog
  • Posts: 3,308
  • Joined: 21-June 11

Re: Python syntax error...not sure why?

Posted 08 July 2013 - 11:12 AM

You must be using Python 3 then. In Python 3 octal literals are written using the prefix 0o and writing numbers with a leading zero without the o is now illegal.
Was This Post Helpful? 1
  • +
  • -

#8 datus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 08-July 13

Re: Python syntax error...not sure why?

Posted 08 July 2013 - 11:17 AM

View Postsepp2k, on 08 July 2013 - 11:12 AM, said:

You must be using Python 3 then. In Python 3 octal literals are written using the prefix 0o and writing numbers with a leading zero without the o is now illegal.

thanks for help!
you made my day :bigsmile:/>
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1