OO.o CALC SUM() GIVES WRONG ANSWER

Leonard Chatagnier lenc5570 at sbcglobal.net
Thu Nov 20 20:05:05 UTC 2008


--- On Thu, 11/20/08, CLIFFORD ILKAY <clifford_ilkay at dinamis.com> wrote:

> From: CLIFFORD ILKAY <clifford_ilkay at dinamis.com>
> Subject: Re: OO.o CALC SUM() GIVES WRONG ANSWER
> To: "Ubuntu user technical support, not for general discussions" <ubuntu-users at lists.ubuntu.com>
> Date: Thursday, November 20, 2008, 12:29 AM
> Leonard Chatagnier wrote:
<snip>  Thanks.

Thanks for the info buy be forewarned that I'm no programmer but less experienced. I'll give it a shot anyway. Note, I never installed ipython; just used python.  Did try entering the sequence of numbers separated by commas which gave me a syntax error with no explanation. However, I left in the sum function the parts following the sequence of numbers, ie, ", start=0) -> value". (perhaps that was the syntax error.) I'll try it again.  From what I'm reading below it looks like the cursor is different in ipython(In [8]) compared to python(>>>) which makes it a little confusing. however I got the same sum output as you show below.
I'll try with ipython as you have an example below.
Thanks for the feedback,

> 
> Install ipython and do:
> 
> import math
> 
> Then type "math." (note the dot after
> "math") and hit the tab key.
> You'll see a bunch of functions, most of which will be
> obvious. The ones
> that aren't obvious, you can get help very easily by
> typing a question
> mark immediately after the function's name. E.g.
> 
> In [5]: math.frexp?
> Type:           builtin_function_or_method
> Base Class:     <type
> 'builtin_function_or_method'>
> String Form:    <built-in function frexp>
> Namespace:      Interactive
> Docstring:
>     frexp(x)
> 
>     Return the mantissa and exponent of x, as pair (m, e).
>     m is a float and e is an int, such that x = m * 2.**e.
>     If x is 0, m and e are both 0.  Else 0.5 <= abs(m)
> < 1.0.
> 
> Quite often, the obvious way is the right way in Python.
> There is quite
> an elegance to it.
> 
> There is a built-in sum function and you can get its syntax
> by typing
> "sum?". Here is example usage.
> 
> In [8]: sum?
> Type:           builtin_function_or_method
> Base Class:     <type
> 'builtin_function_or_method'>
> String Form:    <built-in function sum>
> Namespace:      Python builtin
> Docstring:
>     sum(sequence, start=0) -> value
> 
>     Returns the sum of a sequence of numbers (NOT strings)
> plus the value
>     of parameter 'start'.  When the sequence is
> empty, returns start.
> 
> 
> In [9]: sum ([1,2,3,4])
> Out[9]: 10
> 
> You just have to learn a few of the basics of Python's
> datatypes. The []
> signifies a list. List elements must be separated by
> commas. It's very
> easy to iterate over elements of a list. E.g. the following
> code does
> the same as above but additionally prints each element in
> the list.
> 
> In [10]: mylist = [1,2,3,4]
> 
> In [11]: sum (mylist)
> Out[11]: 10
> 
> In [12]: for each_element in mylist:
>    ....:     print each_element
>    ....:
>    ....:
> 1
> 2
> 3
> 4
> 
> It's quite worthwhile learning Python because it's
> such a versatile and
> approachable language. I would highly recommend "Learn
> to Program Using
> Python" by Alan Gauld and the "Think Python: An
> Introduction to Software
> Design"
> <http://www.greenteapress.com/thinkpython/thinkpython.html>
> if
> you're new to Python. If you're an experienced
> programmer who has solid
> theoretical and practical background in programming,
> you'll breeze
> through both quickly but if you have gaps in your
> knowledge, neither
> assumes prior knowledge so you will get a good grounding in
> the basics
> of programming and of Python. I've used both as
> teaching resources with
> high school students and it worked out quite well.
> 

Leonard Chatagnier
lenc5570 at sbcglobal.net





More information about the ubuntu-users mailing list