[Tutor] Tutor Digest, Vol 84, Issue 78
Daniel Bankston
dack.bugs at gmail.com
Wed Feb 23 03:59:10 CET 2011
On 2/22/2011 8:03 PM, tutor-request at python.org wrote:
> Send Tutor mailing list submissions to
> tutor at 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-request at python.org
>
> You can reach the person managing the list at
> tutor-owner at 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<tcl76 at hotmail.com>
> To:<tutor at python.org>
> Subject: [Tutor] Concatenating string
> Message-ID:<BAY156-w48E4348EA51168306BB243B5D80 at 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.jtm30 at gmail.com>
> To: tee chwee liong<tcl76 at hotmail.com>
> Cc: tutor at python.org
> Subject: Re: [Tutor] Concatenating string
> Message-ID:<4D63C338.6020101 at 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<bgailer at gmail.com>
> To: tee chwee liong<tcl76 at hotmail.com>
> Cc: tutor at python.org
> Subject: Re: [Tutor] Concatenating string
> Message-ID:<4D63C4FC.4020309 at 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.
>
More information about the Tutor
mailing list