[Tutor] Concatenating string
hi, >>> bin(0xff0) '' >>> bin(0xff1) '0001' >>> a=bin(0xff0)+bin(0xff1) >>> a '0001' >>> b=0xff0 >>> c=0xff1 >>> d=b+c >>> d 8161 >>> bin(d) '1' 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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Concatenating string
On 22/02/11 13:46, tee chwee liong wrote: hi, >>> bin(0xff0) '' >>> bin(0xff1) '0001' >>> a=bin(0xff0)+bin(0xff1) >>> a '0001' >>> b=0xff0 >>> c=0xff1 >>> d=b+c >>> d 8161 >>> bin(d) '1' 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. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Concatenating string
On 2/22/2011 8:46 AM, tee chwee liong wrote: hi, >>> bin(0xff0) '' >>> bin(0xff1) '0001' >>> a=bin(0xff0)+bin(0xff1) >>> a '0001' >>> b=0xff0 >>> c=0xff1 >>> d=b+c >>> d 8161 >>> bin(d) '1' 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. -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Concatenating string
On 22/02/2011 14.46, tee chwee liong wrote: hi, >>> bin(0xff0) '' >>> bin(0xff1) '0001' >>> a=bin(0xff0)+bin(0xff1) >>> a '0001' >>> b=0xff0 >>> c=0xff1 >>> d=b+c >>> d 8161 >>> bin(d) '1' 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? As Adam and Bob already pointed out, you are using two different conversion tools and obtain two different types. If you need a binary representation of a number AS A STRING, you can use bin(n) as you did. Or you can use hex(n) to see an hexadecimal view of the same number, again AS A STRING. If you want a binary or hex NUMBER, you already have it: EVERY number is stored internally as a binary value, even if you enter or see it in decimal or in a different base. Look at the following transcript from the Python interpreter, and please pay attention to the presence or absence of the quotes: . . >>> hex(0b1001001001001001001) . '0x49249.' . >>> 0b1001001001001001001 . 299593 . >>> 0x49249 . 299593 . >>> bin(0x49249) . '0b1001001001001001001' . >>> Hope this will help you. Francesco Loffredo - Nessun virus nel messaggio. Controllato da AVG - www.avg.com Versione: 10.0.1204 / Database dei virus: 1435/3457 - Data di rilascio: 21/02/2011 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Concatenating string
hi, i dont know why when i re-run bin(0xff0) today at IDLE Python 2.5, it gives me traceback error. >>> bin(0xff0) Traceback (most recent call last): File "", line 1, in bin(0xff0) NameError: name 'bin' is not defined i guess Python 2.5 doesn't support binary conversion of hex. confuse why it worked yesterday when i tried out. thanks tcl ## 2) You've already done that several times, just use bin() HTH, Adam. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Concatenating string
hi Francesco, couldnt get hex of bin working on IDLE Python 2.5 when i type: >>> hex(0b1001001001001001001) SyntaxError: invalid syntax >>> bin(0x49249) Traceback (most recent call last): File "", line 1, in bin(0x49249) NameError: name 'bin' is not defined pls advise. thanks tcl ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Concatenating string
On 01/-10/-28163 02:59 PM, tee chwee liong wrote: hi Francesco, couldnt get hex of bin working on IDLE Python 2.5 when i type: hex(0b1001001001001001001) SyntaxError: invalid syntax bin(0x49249) Traceback (most recent call last): File "", line 1, in bin(0x49249) NameError: name 'bin' is not defined pls advise. thanks tcl Just read the docs: http://docs.python.org/library/functions.html#bin According to that page, this function was new in version 2.6 DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 84, Issue 78
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 + From: tee chwee liong To: Subject: [Tutor] Concatenating string Message-ID: Content-Type: text/plain; charset="iso-8859-1" hi, bin(0xff0) '' bin(0xff1) '0001' a=bin(0xff0)+bin(0xff1) a '0001' b=0xff0 c=0xff1 d=b+c d 8161 bin(d) '1' 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 + From: Adam Bark To: tee chwee liong 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) '' bin(0xff1) '0001' a=bin(0xff0)+bin(0xff1) a '0001' b=0xff0 c=0xff1 d=b+c d 8161 bin(d) '1' 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 To: tee chwee liong 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) '' bin(0xff1) '0001' a=bin(0xff0)+bin(0xff1) a '0001' b=0xff0 c=0xff1 d=b+c d 8161 bin(d) '1' 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
[Tutor] reading large text file as numpy array
Dear All I have a large text file with more than 50k lines and about 784 floats in each line. How can I read the whole file as a single numpy array? Also, is it possible to save a session (like in MATLAB) and reload it later? thanks ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor