On 2/22/2011 8:03 PM, tutor-requ...@python.org wrote:
Send Tutor mailing list submissions to
        tutor@python.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.python.org/mailman/listinfo/tutor
or, via email, send a message with subject or body 'help' to
        tutor-requ...@python.org

You can reach the person managing the list at
        tutor-ow...@python.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Tutor digest..."


Today's Topics:

    1. Concatenating string (tee chwee liong)
    2. Re: Concatenating string (Adam Bark)
    3. Re: Concatenating string (bob gailer)
    4. Re: Concatenating string (Francesco Loffredo)
    5. Re: Concatenating string (tee chwee liong)
    6. Re: Concatenating string (tee chwee liong)


----------------------------------------------------------------------

Message: 1
Date: Tue, 22 Feb 2011 13:46:22 +0000
From: tee chwee liong<tc...@hotmail.com>
To:<tutor@python.org>
Subject: [Tutor] Concatenating string
Message-ID:<bay156-w48e4348ea51168306bb243b5...@phx.gbl>
Content-Type: text/plain; charset="iso-8859-1"


hi,

bin(0xff0)
'111111110000'
bin(0xff1)
'111111110001'
a=bin(0xff0)+bin(0xff1)
a
'111111110000111111110001'
b=0xff0
c=0xff1
d=b+c
d
8161
bin(d)
'1111111100001'

question:
1) why is it that a and d values are different? i'm using Python 2.5.
2) how to convert hex to bin in Python 2.5?


thanks
tcl                                     
-------------- next part --------------
An HTML attachment was scrubbed...
URL:<http://mail.python.org/pipermail/tutor/attachments/20110222/334e5091/attachment-0001.html>

------------------------------

Message: 2
Date: Tue, 22 Feb 2011 14:07:52 +0000
From: Adam Bark<adam.jt...@gmail.com>
To: tee chwee liong<tc...@hotmail.com>
Cc: tutor@python.org
Subject: Re: [Tutor] Concatenating string
Message-ID:<4d63c338.6020...@gmail.com>
Content-Type: text/plain; charset="iso-8859-1"; Format="flowed"

On 22/02/11 13:46, tee chwee liong wrote:
hi,

bin(0xff0)
'111111110000'
bin(0xff1)
'111111110001'
a=bin(0xff0)+bin(0xff1)
a
'111111110000111111110001'
b=0xff0
c=0xff1
d=b+c
d
8161
bin(d)
'1111111100001'

question:
1) why is it that a and d values are different? i'm using Python 2.5.
2) how to convert hex to bin in Python 2.5?


thanks
tcl
Hi,

1) As you can see bin() returns a binary representation of the number
you pass to it as a string. Thus when you do a=bin(0xff0)+bin(0xff1) you
are concatenating the two strings returned by bin. For d you are
carrying out the mathematical addition operation on the two numbers
before converting it to binary.
2) You've already done that several times, just use bin()

HTH,
Adam.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:<http://mail.python.org/pipermail/tutor/attachments/20110222/557ae98c/attachment-0001.html>

------------------------------

Message: 3
Date: Tue, 22 Feb 2011 09:15:24 -0500
From: bob gailer<bgai...@gmail.com>
To: tee chwee liong<tc...@hotmail.com>
Cc: tutor@python.org
Subject: Re: [Tutor] Concatenating string
Message-ID:<4d63c4fc.4020...@gmail.com>
Content-Type: text/plain; charset="iso-8859-1"; Format="flowed"

On 2/22/2011 8:46 AM, tee chwee liong wrote:
hi,

bin(0xff0)
'111111110000'
bin(0xff1)
'111111110001'
a=bin(0xff0)+bin(0xff1)
a
'111111110000111111110001'
b=0xff0
c=0xff1
d=b+c
d
8161
bin(d)
'1111111100001'

question:
1) why is it that a and d values are different? i'm using Python 2.5.
Why would you expect otherwise?

What are the types of a and d? They are not the same type; therefore you
get different results.

How do you determine the type of an object?
1 - Use the type() function.
2 - notice '' around a and not around d
3 - Read the documentation:
bin(/x/)  Convert an integer number to a binary string. The
documentation is weak here, but at least it tells you that the result is
a string.
0x is covered under (at least for Python 2.6) in 2.4.4. Integer and long
integer literals.

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to