Results 1 to 6 of 6

Thread: Working my way through Beginning Python

  1. #1
    Join Date
    Jun 2005
    Location
    Malta
    Beans
    4,187
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Working my way through Beginning Python

    Hi guys, I am working my way through Beginning Python (2005), and I found this exercise :

    Create a string with the format specifier %u and pass a negative number to it. When Python evaluates it, consider the answer it returns, which you may find surprising.

    Code:
    print '%%u and %u' % (-10)
    %u and -10
    It's just like I used %d. Anyone help me out? I am guessing that %u means unsigned int, so it should have given me an error message or something, but it didn't in this case...why?
    Last edited by Lord Illidan; June 5th, 2007 at 04:18 PM. Reason: Edit : found a mistake in the code section..replaced the output

  2. #2
    Join Date
    Jun 2005
    Beans
    2,314

    Re: Working my way through Beginning Python

    Yes, %u means unsigned. So I would expect negative numbers to lose their sign. As for why no error - I guess there's not enough checking. Python tends to be quite lax about type conversions and so on.

    Here's the page:
    http://docs.python.org/lib/typesseq-strings.html

  3. #3
    Join Date
    Jun 2005
    Location
    Malta
    Beans
    4,187
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: Working my way through Beginning Python

    So, why did it display -10 as -10? At the very least, I thought it would convert -10 to 10.
    Last edited by Lord Illidan; June 5th, 2007 at 04:32 PM.

  4. #4
    WW is offline Iced Blended Vanilla Crème Ubuntu
    Join Date
    Oct 2004
    Beans
    1,532

    Re: Working my way through Beginning Python

    It appears that the convention was changed in 2.4.

    Python 2.3
    Code:
    $ python
    Python 2.3.5 (#2, Oct 16 2006, 19:19:48)
    [GCC 3.3.5 (Debian 1:3.3.5-13)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> print '%d, %u' % (-10,-10)
    __main__:1: FutureWarning: %u/%o/%x/%X of negative int will return a signed string in Python 2.4 and up
    -10, 4294967286
    >>>
    Python 2.4
    Code:
    ~$ python
    Python 2.4.3 (#2, Oct  6 2006, 07:52:30)
    [GCC 4.0.3 (Ubuntu 4.0.3-1ubuntu5)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> print '%d, %u' % (-10,-10)
    -10, -10
    >>>

  5. #5
    Join Date
    Jun 2005
    Location
    Malta
    Beans
    4,187
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: Working my way through Beginning Python

    Ah, that makes sense. Anyone knows why?

  6. #6
    Join Date
    Jun 2005
    Location
    Malta
    Beans
    4,187
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: Working my way through Beginning Python

    Ah, that makes sense. Anyone knows why?

    EDIT : sry about the double post - had probs with the internet.
    Last edited by Lord Illidan; June 5th, 2007 at 04:57 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •