equivalent of NULL or OM in Python

Daniel Klein danielk at aracnet.com
Fri Feb 16 09:15:29 EST 2001


In this case, accessing an element outside of the range of values would produce
an IndexError exception. You can 'catch' this exception like this

poly = [(2,3),(0,4)]
try:
    thirdpart = poly[2]
except IndexError:
    print 'There are only 2 parts.'

In Python, when an object has not been initialized, it is set to 'None' (quotes
are for clarity), but I don't think this is useful in this situation.

For question 2:

for element in poly:
    print element[1] # prints the second part of each tuple 

Hope this helps,

Daniel Klein
Portland OR USA



On Fri, 16 Feb 2001 03:37:43 -0500, "cyberian bear" <cyberian_bear at hotmail.com>
wrote:

>1.I'm doing a program in Python which perform +, -, *, / on two polynomials.
>Their coefficient and exponentials are stored in tuples. So for example
>3X^2+4X+4 would be stored like [(2,3),(1,4),(0,4)] but what if the term is
>absent completely from one of the polynomials then i would have to check if
>one of the tuuples is not present at all and not just zero. For  example
>[(2,3),(0,4)]. In Pascal there is NULL in SETL there is OM but is there a
>corresponding statement in Python. I've checked through several sources
>including my textbook but didn't find the satisfactory answer. Maybe I
>should just check if the second term of every tuple(i.e the coefficient) is
>zero which means that the term is not present.
>2. Also can anyone give me a clue how to iterate over all the coefficient
>terms in the tuples in the first polynomials' tuples.
>My guess is should be something like
>for coefficient in polynomial1:
>where polynomial is a list of tuples
>but how do i tell it to iterate specifically over the second term in every
>tuple and not over the whole tuples
>cb
>




More information about the Python-list mailing list