[Tutor] why no chaining?
I thought I'd get [2,3,4,5] from this but instead I get nothing. Why isn't it chaining? >>> q = list(set([1,2,3,1,4,1,5,1,5])).remove(1) >>> q >>> -- Jim Today is the day that would have been yesterday if tomorrow was today ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Tutor Digest, Vol 134, Issue 32 - amazing range
> Range objects are special: not only do they produce values lazily as > needed, but they also support len(), indexing, slicing, and membership > testing, none of which generators are capable of doing: > > Steve - That's really cool. Worth making a dumb mistake to find it out ;') Jim Today is the day that would have been yesterday if today was tomorrow ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] why no chaining?
On 12/04/15 07:06, Jim Mooney wrote: I thought I'd get [2,3,4,5] from this but instead I get nothing. Why isn't it chaining? q = list(set([1,2,3,1,4,1,5,1,5])).remove(1) q Because that's just the way it was designed. It's common in Python for methods that modify an object to return None. I don't like it either, but that's the way it is. You need to create a reference to the object then call the method then use the modified object. >>> q = list(set([1,2,3,1,4,1,5,1,5])) >>> q.remove(1) >>> print q Aside: Normally you would use the help() function to find out how methods work and what they return but sadly the documentation for the in-place methods doesn't indicate the return is None. Some of them have a warning that its "IN PLACE" but remove does not. Which is a pity. >>> help([].sort) Help on built-in function sort: sort(...) L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*; cmp(x, y) -> -1, 0, 1 (END) >>> help([].remove) Help on built-in function remove: remove(...) L.remove(value) -- remove first occurrence of value. Raises ValueError if the value is not present. (END) Personally I think it would help if the first lines for these methods read like: remove(...) -> None L.remove...etc As is the case with functions that return a value: Help on built-in function pow in module __builtin__: help(pow) pow(...) pow(x, y[, z]) -> number With two arguments, equivalent to x**y. With three arguments, equivalent to (x**y) % z, but may be more efficient (e.g. for longs). (END) HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Please disable “digest” mode in order to participate (was: Tutor Digest, Vol 134, Issue 32 - amazing range)
Jim Mooney writes: > That's really cool. Worth making a dumb mistake to find it out ;') Jim, you are evidently receiving “digest” messages from this forum. Please disable that in order to receive individual messages, so you can participate properly: responding to individual messages that the rest of us see. Enabling “digest” mode is only for when you are *certain* that you will never at any point need to respond to any message from the forum. That's not true, so should be turned off. -- \ “I find the whole business of religion profoundly interesting. | `\ But it does mystify me that otherwise intelligent people take | _o__)it seriously.” —Douglas Adams | Ben Finney ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] On learning Fortran and C++ for scientific computing
Vick wrote: > So can Fortran crunch 250 digits numbers in an integration formula under 3 > seconds with the same computing parameters as above? Or is Python better > at it? So by better you mean faster. Pure CPython is usually much slower than Fortran, but as there are many optimised libraries written in C or sometimes even Fortran available for use with CPython, that often doesn't matter. The mpmath documentation states that the library's performance can be improved by using gmp, so in this case the library you should rely on is gmpy or gmpy2. Do you have one of these installed? What does $ python3 -c 'import mpmath.libmp; print(mpmath.libmp.BACKEND)' gmpy print? Once you have your script working with gmp you could simplify it to just the integration, and when you are down to 10 or 20 lines you could challenge your friend to write the Fortran equivalent. If he answers he's not willing to spend a week on the superior Fortran solution, or if he comes up with something that takes 2.7 seconds you see that he was bullshitting you. If his code finishes in 0.1 second and with the correct result, you might consider learning the language -- but keep in mind that to save one hour of execution time you have to run the script more than 1200 times, and when you need 10 minutes longer to write the Fortran than the Python code you may still get a bad deal as your time usually costs more than that of a machine. That said, even though I rarely write any C code knowing C does help me understand what happens in C programs (for example the Python interpreter) and generally broadens the horizon. Therefore I'd recommend learning C (or Fortran) anyway if you are interested in the computing part of your research. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] why no chaining?
Op 12-04-15 om 09:58 schreef Alan Gauld: Aside: Normally you would use the help() function to find out how methods work and what they return but sadly the documentation for the in-place methods doesn't indicate the return is None. Some of them have a warning that its "IN PLACE" but remove does not. Which is a pity. >>> help([].sort) Help on built-in function sort: sort(...) L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*; cmp(x, y) -> -1, 0, 1 (END) >>> help([].remove) Help on built-in function remove: remove(...) L.remove(value) -- remove first occurrence of value. Raises ValueError if the value is not present. (END) Personally I think it would help if the first lines for these methods read like: remove(...) -> None L.remove...etc Are you using Python 2? Because this is what I get in Python 3: remove(...) L.remove(value) -> None -- remove first occurrence of value. Raises ValueError if the value is not present. The same for help(list.sort) BTW. Timo ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] On learning Fortran and C++ for scientific computing
In a message of Sun, 12 Apr 2015 10:25:54 +0400, "Vick" writes: >S 0 to 1e+31> 1/sqrt(.86 *(1+z)^4 + .282 * (1+z)^3 - .86 >*(1+z)^2 + .718) if you try this integration you will get completely >wrong numbers on computing devices that do not possess ultra-high precision >and accuracy. Python is as accurate as you can get, every bit as accurate as C++ or Fortran. >I had to use mpmath with precision of 250 and integration method called Tanh >Sinh Quadrature with accuracy 120 to get the desired results. I checked the >answer with Mathematica and they concur. But the calculation is done in >about 3 seconds on my laptop with intel core i5 at 2.5 GHz with 4 GB RAM. >So can Fortran crunch 250 digits numbers in an integration formula under 3 >seconds with the same computing parameters as above? Or is Python better at >it? Some of us who were unhappy with the performance of python wrote pypy. (I am one of the people, so very much biased here.) PyPy uses a just-in-time compiler to get its speed. If you want to look at the speed of pypy vs the speed of CPython (which is what we call the particular implementation(s) of python you are using, because it is written in C) you can take a look at these pages. http://speed.pypy.org/ Click on a benchmark, and see the code, and see how much faster we are than CPython. Mathematical formulae are what we are very, very good at. Indeed we are often better than some old Fortran library -- so we have users who are busy rewriting their code in Python in order to use pypy for the increased speed of mathematical computation. If all you are doing is pure math -- and not going out, for instance and trying to graph this thing at the same time -- then we can probably give you speed which is equivalent to C. But this assumes that you can write this code where the heavy calculating is done inside a loop, which is executed many times, because that is what PyPy optimises. If all you have is straight in-line executed-one-time-only code, there will be no speed win for you. Also, the jit takes time to warm up. It has to set up its own internal bookkeeping before it can get to work on your code. So if your code doesn't take very long to run, it may run slower in pypy -- because the savings your get are competely swallowed up in the setup costs. However, it is easy to find out. You can get pypy here: http://pypy.org/download.html It's a perfectly compliant, working python. The version compatible with 3.2.5 is not as speedy as the one compatible with 2.7. So just run your python code with this -- it should run completely unchanged -- and see how fast it is. If you are still unhappy with the performance, post your code to pypy-...@python.org and we can give you suggestions on how to tune your python code for better performance under pypy. If you expose a place where pypy ought to be fast, but isn't, we will fix pypy and get you a working faster binary. But I think any more talk about pypy belongs on the pypy-dev mailing list. Laura Creighton ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Please disable “digest” mode in order to participate (was: Tutor Digest, Vol 134, Issue 32 - amazing range)
In a message of Sun, 12 Apr 2015 18:05:16 +1000, Ben Finney writes: >Jim, you are evidently receiving “digest” messages from this forum. > >Please disable that in order to receive individual messages, so you can >participate properly: responding to individual messages that the rest of >us see. > >Enabling “digest” mode is only for when you are *certain* that you will >never at any point need to respond to any message from the forum. That's >not true, so should be turned off. Or see if your mailer, like mine, has an option for bursting digests in place. Bursting is splitting a digest into individual messages so that you can reply to them individually. (Very few mailers do this, which is a real shame. And every one I know of calls this 'bursting'.) Then please burst your digests before you reply to them. Laura Creighton ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] why no chaining?
On 12/04/15 11:16, Timo wrote: Personally I think it would help if the first lines for these methods read like: remove(...) -> None L.remove...etc Are you using Python 2? Because this is what I get in Python 3: Yes, I just fired up the default python which is 2.7. I never thought to check what Python 3 help said. Good to see its been addressed., -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] still breaking chains
If join returns a string, why am I getting a syntax error when I try to slice it? >>> 'alfabeta'[2:5] 'fab' >>> ''.join(['a', 'l', 'f', 'a', 'b', 'e', 't', 'a')[2:5] SyntaxError: invalid syntax -- Jim ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] still breaking chains
On Apr 12, 2015 4:00 PM, "Jim Mooney" wrote: > > If join returns a string, why am I getting a syntax error when I try to > slice it? > > >>> 'alfabeta'[2:5] > 'fab' > >>> ''.join(['a', 'l', 'f', 'a', 'b', 'e', 't', 'a')[2:5] > SyntaxError: invalid syntax If you're seeing a SyntaxError, don't look for explanations that are semantic. Look for syntax. In this case, note that the list of characters had not been closed with a right bracket yet. Best of wishes! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Please disable “digest” mode in order to participate
Laura Creighton writes: > In a message of Sun, 12 Apr 2015 18:05:16 +1000, Ben Finney writes: > >Please disable that in order to receive individual messages, so you > >can participate properly: responding to individual messages that the > >rest of us see. > > Or see if your mailer, like mine, has an option for bursting digests > in place. Bursting is splitting a digest into individual messages so > that you can reply to them individually. That's an interesting feature. It's not sufficient though: the resulting messages lack the full header of each message, so any reply will not be properly marked as a response to the original. The only way to participate properly in a discussion on a mailing list is to respond to the actual messages that were sent. And that's only possible if you disable “digest” mode beforehand. -- \ “Those who write software only for pay should go hurt some | `\ other field.” —Erik Naggum, in _gnu.misc.discuss_ | _o__) | Ben Finney ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] still breaking chains
Jim Mooney writes: > If join returns a string, why am I getting a syntax error when I try to > slice it? > > >>> 'alfabeta'[2:5] > 'fab' > >>> ''.join(['a', 'l', 'f', 'a', 'b', 'e', 't', 'a')[2:5] > SyntaxError: invalid syntax This demonstrates the primary problem with so-called “chaining”. Your statement is too complex, and you have misunderstood where in that statement the error is. Break the statement into at least two: get the result of the ‘join’, and then in the next statement slice that value. You'll see where the error was. One important principle to follow is to write your statements to be very simple, so that when something goes wrong it is as easy as possible to read the statement and understand what it *actually* says. Chaining long strings of operations together in a single statement goes directly against that principle, and hence is to be avoided. -- \ “[T]he speed of response of the internet will re-introduce us | `\to that from which our political systems have separated us for | _o__)so long, the consequences of our own actions.” —Douglas Adams | Ben Finney ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] still breaking chains
On Sun, Apr 12, 2015 at 11:03:07AM -0700, Jim Mooney wrote: > If join returns a string, why am I getting a syntax error when I try to > slice it? Because you have a syntax error. Syntax error means you have written something which the Python compiler cannot understand, because the syntax is wrong, mangled or confused, or something is missing from the line of code. (If the missing element is a closing bracket, brace or parethesis, the compiler may not report the error until the line *after* the offending line.) An English example: "I would like a cup of coffee" -- correct syntax "would coffee I cup like" -- syntax error Human beings are more flexible with syntax and can sometimes decipher rather mangled invalid sentences (e.g. most of us can understand Yoda) but programming language compilers are less flexible and require you to be pedantically correct: + 1 2 # syntax error function(x y) # syntax error '-'.join['a', 'b') # syntax error This does not mean that Python cannot add numbers, call functions, or join substrings. It means the compiler cannot understand my mangled code. Fix the syntax: 1 + 2 function(x, y) '-'.join(['a', 'b']) In your example: > >>> 'alfabeta'[2:5] > 'fab' > >>> ''.join(['a', 'l', 'f', 'a', 'b', 'e', 't', 'a')[2:5] > SyntaxError: invalid syntax I believe you have deleted what you might have thought was a blank line, but actually gives you a hint as to what the error is. When I try to run that, I get this: py> ''.join(['a', 'l', 'f', 'a', 'b', 'e', 't', 'a')[2:5] File "", line 1 ''.join(['a', 'l', 'f', 'a', 'b', 'e', 't', 'a')[2:5] ^ SyntaxError: invalid syntax Look carefully at the "blank" line: it isn't actually blank. Way over on the right hand side you will see a caret ^ pointing to the spot where the compiler first noticed something it could not understand. In your email, it may not line up, but in the interactive interpreter the caret will line up with ) the closing round bracket. Why? Because before you close the method call ''.join( ) you have to close the list [ ]: ''.join([...]) # okay ''.join([...) # syntax error It is sad that the Python compiler does not report a more useful error message here. It would be awesome if it would report: SyntaxError: missing closing bracket ] but we should count our blessing that we get anything at all. -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] still breaking chains
On Sun, Apr 12, 2015 at 6:47 PM, Ben Finney wrote: [...] > One important principle to follow is to write your statements to be very > simple, so that when something goes wrong it is as easy as possible to > read the statement and understand what it *actually* says. +1! This principle I have found to be incredibly helpful as I continue to learn Python and better programming in general. Where it especially pays dividends for me is when I must go back to where I was coding after an extended interruption. I find that when I have followed this principle I have a much better chance of *immediately* understanding my intent, then when I have to decipher my thoughts from a "one-liner" that might occupy less space, but where its complexity has me pausing in thought to be sure I understand what I actually was trying to do. -- boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor