Thread: Syntax help

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0

    Syntax help


    Okay I'm learning to program in Python. I don't have previous programming experience. I'm not a math guy, so could you please recommend me some good books to learn math.

    Why is this invalid:

    >>> zip_code = 02492
    SyntaxError: invalid token

    Why is this valid:

    >>> zip_code = 02132
    >>> zip_code
    1114

    I'm getting a semantic error :/

    It has something to do with the way numbers are multiplied/divided I think.

    >>> 01
    1
    >>>010
    8
    >>> 0100
    64
    >>> 01000
    512
    >>> 010000
    4096
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    Looks like a leading 0 digit indicates octal notation.

    Base 8.

    Use strings for zip codes

    PennPlaza = '10001'

    ooops, bad example.

    zip = '01234'

    # better

    Comments on this post

    • pr0x1mity agrees : Very useful and really helpful
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0
    Thanks a lot! What's a good base 8 book that I could learn from?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    4
    As a general rule, if you aren't going to do math on something, don't treat it as a number, even if it is composed to numerals.

    (Incidentally, treating numbers with a leading 0 as octal was eliminated in Python 3.)

    Comments on this post

    • pr0x1mity agrees : Gave general helpful guidelines to beginning programmers
    • Dietrich agrees : a good reminder
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481

    I like base 0.


    Code:
       0b1 0b5 0ba 0bz 0b85manyzeros93472  NB. base 0
    1 5 10 35 2
    
       0.1b345   NB. base 0.1
    5.43
    
       _3b123    NB. base negative 3
    6
    
       (3*(-3)^0) + (2*(-3)^1) + (1*(-3)^2)
    6
    
       4r3p1  NB. 4 pi over 3
    4.18879
    www.jsoftware.com
    Last edited by b49P23TIvg; January 19th, 2013 at 09:43 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo