OO.o CALC SUM() GIVES WRONG ANSWER

CLIFFORD ILKAY clifford_ilkay at dinamis.com
Thu Nov 20 06:29:40 UTC 2008


Leonard Chatagnier wrote:
> Never used python before but gave it a try. Couldn't figure out the syntax(tried separating list with commas and with + sign) even after reading the manual and after typing sum at the python prompt. Looks like something that you need to know what you are doing and I don't. It was a new experience, however.  Thanks.

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.
-- 
Regards,

Clifford Ilkay
Dinamis
1419-3266 Yonge St.
Toronto, ON
Canada  M4N 3P6

<http://dinamis.com>
+1 416-410-3326
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3273 bytes
Desc: S/MIME Cryptographic Signature
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20081120/53ba1055/attachment.bin>


More information about the ubuntu-users mailing list