Doing Math with Python

Nov 04, 2015

Breaking long lines in Python

In some of the programs discussed in the book including the sample solutions, you will see statements like:

print('Area: {0}, Estimated ({1}): {2}'.
           format(area_of_circle, points, estimate(radius, points)))

This is really the following single statement:

print('Area: {0}, Estimated ({1}): {2}'.format(area_of_circle, points, estimate(radius, points)))

The first code snippet above is an example of breaking a long line into two (or more) lines so that we don't end up with really long lines in our code. How long should a line be when you should think about breaking it? If your statement's length is more than 80 characters, you should think about breaking it up.

In the book, we often had to do so because of layout reasons even though the statement may not have exceeded 80 characters, and in your projects you will want to do it so that your statements are easier to read and on the average all lines have a similar length. This is formalized (among other things) in PEP 8.

Note that the examples below will for illustrative purposes break lines waaaaay less than 80 characters.

How do you break?

When not calling function

When you are not calling a function, you essentially have two choices:

Use paranthesis

This is exactly how we break the long statement in the example we started this article with. For the moment ignore the call to print() and assume that the statement is:

s = 'Area: {0}, Estimated ({1}): {2}'.format(area_of_circle, points, estimate(radius, points))

This essentially just creates the string s. If we were to split this statement over multiple lines, we would do the following:

s = ('Area: {0}, Estimated ({1}): {2}'
     .format(area_of_circle, points, estimate(radius, points)))

Note the extra beginning and the ending parenthesis.

Here is another example:

s1 = x + x**2/2 + x**3/3 + x**4/4 + x**5/5 + x**6/6 + x**7/7 + x**8/8

Here is how we can use split the above statment into multiple lines using parentheses:

s3 = (x + x**2/2 + x**3/3
     + x**4/4 + x**5/5
     + x**6/6 + x**7/7
     + x**8/8)

Use the line continuation operator

The line continuation operator, \ can be used to split long statements over multiple lines. Here is how we could split the above statement using \ instead:

s3 = x + x**2/2 + x**3/3 \
    + x**4/4 + x**5/5 \
    + x**6/6 + x**7/7 \
    + x**8/8

At the end of every line (except the last), we just add a \ indicating that the next line is also a part of the same statement.

Breaking up those long if statements

Often I have to break long if statements and is in fact one of the most common cases I face at work where I have to break the statement into multiple lines. Here is an example using both the approaches above:

# Using parenthesis
if (cond1 and cond2 and cond3
    and cond4):
    # True block
else:
    # False block

# Using line continuation operator
if cond1 and cond2 and cond3 \
   and cond4:
    # True block
else:
    # False block

When calling functions

By default, when calling functions you can just press enter and without doing anything more keep writing your statement over multiple lines. For example:

x = 1
print(x,
      x)

Hence, we could have broken the first example we saw as:

print('Area: {0}, Estimated ({1}): {2}'.format(area_of_circle,
                                              points,
                                              estimate(radius, points)))

When calling format() we put the arguments over separate lines.

Learning more about Python coding style

If you liked reading this article, you may also find it worth your time going over the Python style guide. You may even find instances where I have not followed a guideline when writing the programs in the book. If you find one, let me know.

Getting in touch

Stay updated or get in touch:

You can contact me directly via:

Find this post useful? Please share.

Share on: Diaspora*TwitterFacebookGoogle+Email

Sep 05, 2015

set compared to SymPy's FiniteSet"">Set operations with Python set compared to SymPy's FiniteSet

Chapter 5 (About) of the book discusses working with mathematical sets in Python. While writing the chapter, I had a choice of whether to use Python 3's built-in set data structure or use SymPy's (0.7.6 +) FiniteSet class. I decided to go ahead with the latter. My choice is briefly explained towards the end of this post, but hopefully it will be clear before that.

Next, I describe how you can use Python 3's built-in set data structure to create sets and perform set operations such as finding the union, intersection or cartesian product of sets. For comparison, I also show how you can do the same using SymPy's FiniteSet class.

Creating a set

We can create a set consisting of the elements {1, 2, 3} in Python 3 as follows:

>>> s1 = {1, 2, 3}
>>> s1
{1, 2, 3}

To create a set when the elements are already in a list (for example), we would use the following syntax:

>>> items = [1, 2, 3]
>>> s2 = set(items)
>>> s2
{1, 2, 3}

The comparative operations using SymPy's FiniteSet class are:

>>> from sympy import FiniteSet
>>> s1 = FiniteSet(1, 2, 3)
>>> s1
{1, 2, 3}

>>> items = [1, 2, 3]
>>> s2 = FiniteSet(*items)
>>> s2
{1, 2, 3}

To create an empty set, in Python 3 you would use create an empty set object:

>>> e = set()
>>> e
set()

In SymPy, an empty set is represented by an EmptySet object. Thus, you can either create an empty set by directly creating an EmptySet object or by creating a FiniteSet object without specifying any set members, like so:

>>> from sympy import EmptySet
>>> e = EmptySet()
>>> e
EmptySet()
>>> e = FiniteSet()
>>> e
EmptySet()

Cardinality and Membership

The len() function returns the number of set members for sets created using either of the above approaches.

Similarly, to check if an item x is present in a set, s created using any of the above approaches, we can use the statement, x in s.

Union and intersection

The union() method can be used in both cases to find the union of two or more sets:

>>> s1 = set([1, 2, 3])
>>> s2 = set([2, 3, 4])
>>> s3 = set([2, 3, 4, 5])
>>> s1.union(s2).union(s3)
{1, 2, 3, 4, 5}

Similary in the case of SymPy:

>>> from sympy import FiniteSet
>>> s1 = FiniteSet(1, 2, 3)
>>> s2 = FiniteSet(2, 3, 4)
>>> s3 = FiniteSet(2, 3, 4, 5)
>>> s1.union(s2).union(s3)
{1, 2, 3, 4, 5}

The intersection() method can be used to find the intersection of two or more sets created using either of the above approaches. Continuing with the above three sets:

>>> s1 = set([1, 2, 3])
>>> s2 = set([2, 3, 4])
>>> s3 = set([2, 3, 4, 5])
>>> s1.intersection(s2).intersection(s3)
{2, 3}

Similary, in SymPy:

>>> s1.intersection(s2).intersection(s3)
{2, 3}

Cartesian product

To find the cartesian product of sets created via the built-in set data structure, we have to use the product() function in the itertools module:

>>> s1 = {1, 2, 3}
>>> s2 = {4, 5, 6}
>>> import itertools
>>> itertools.product(s1, s2)
<itertools.product object at 0x10418c990>

However considering that the cartesian product of two sets should be another set, the product() function doesn't really then return the cartesian product itself, but (an iterator to) the elements in it. Hence, if we try to apply the result returned by the function directly to a method or function which is expected to be applicable to a set, it will fail. For example, itertools.product(s1, s2).union(s3) will result in an error, but set(itertools.product(s1, s2)).union(s3) will work.

Using SymPy's FiniteSet, you can use the * (multiplication or product) operator to find the cartesian product and the result is a set itself. Thus, it is closer to what a cartesian product is mathematically. An example follows:

>>> s1 = FiniteSet(1, 2, 3)
>>> s2 = FiniteSet(4, 5, 6)
>>> >>> s3 = FiniteSet(7, 8, 9)
>>> (s1*s2).union(s3)
{7, 8, 9} U {1, 2, 3} x {4, 5, 6}

Cartesian product of a set with itself

To find the cartesian product of a set with itself, i.e. s1*s1 for example, we pass in a keyword argument, repeat while calling the itertools.product() function. The value of repeat is the power we want to raise the set to. Thus, itertools.product(s1, repeat=2) will calculate the cartesian product, s1*s1:

>>> s1 = {1, 2, 3}
>>> set(itertools.product(s1, repeat=2))
{(1, 2), (3, 2), (1, 3), (3, 3), (3, 1), (2, 1), (2, 3), (2, 2), (1, 1)}

In SymPy, the ** operator can be used for finding the cartesian product of a set with itself:

>>> s1 = FiniteSet(1, 2, 3)
>>> s1**2
{1, 2, 3} x {1, 2, 3}

Subset/super set/proper subset checking

The issubset() and issuperset() methods are available for sets created via either approaches to check if a set is a subset and super set of another, respectively. Thus, s1.issubset(s2) will check if s1 is a subset of s2.

Checking for proper subset and superset

To check if a set, s1 is a proper subset of another set, s2 when using built-in set, we can do the following:

>>> s1 = {1, 2, 3}
>>> s2 = {1, 2, 3, 4}
>>> s1.issubset(s2) and s1 != s2
True

We can do something similar for proper superset.

In SymPy, we have is_proper_subset() and is_proper_superset() methods which can be used to check if a set is a proper subset or superset of another, respectively. Thus, the above would be written as s1.is_proper_subset(s2).

Calculating the powerset

For sets created via built-in set data structure, there is no direct method available to create the power set. However, you can use the powerset recipe described in the itertools documentation.

On the other hand, in SymPy, there is a powerset() method available which returns the power set:

>>> s1 = FiniteSet(1, 2, 3)
>>> s1.powerset()
{EmptySet(), {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}}

You can see that the powerset() method returns the power set and not the elements in it.

Choice of SymPy's FiniteSet over set

From the above comparison, we can see that SymPy's FiniteSet provides us with nice features such as being able to use the * operator to find the cartesian product, ** operator to calculate the cartesian product with itself and powerset() method for calculating the power set. These are not present when using the built-in set data structure. This was certainly a big driving factor in my choice, since SymPy was also being used in other chapters of the book.

However, a key reason for my choice was that I wanted to show how we can create sets which did not allow addition or removal once created - like mathematical sets. This need was fulfilled by SymPy's FiniteSet since it used Python's frozenset data structure and not the set data sturcture.

The alternative to that would have been to use frozenset directly, but I just did not like the idea of it and I would have also missed out on the nice features FiniteSet would provide (eventually). I should note here that once I had made the decision to go with FiniteSet, I contributed patches to SymPy to make the methods of FiniteSet more compatible with Python's built in set and also implement minor features I discussed above.

Find this post useful? Please share.

Share on: Diaspora*TwitterFacebookGoogle+Email