Topics: python

Protected: Prevent concurrent execution using file locking

There is no excerpt because this is a protected post.

Tuple + Tuple?

An interesting trivial task is to concatenate two tuples in python. Let’s say we try this out: a = (‘dd’) b = (‘aa’,’bb’) print a+b Unfortunately likely we will get the following error: Traceback (most recent call last): File "test.py", line 20, in <module> print a+b TypeError: cannot concatenate ‘str’ and ‘tuple’ objects

Interesting ways to display a Python List in String

For objects with string value >>> mylist = [‘spam’, ‘ham’, ‘eggs’] >>> print ‘, ‘.join(mylist) spam, ham, eggs >>> print ‘\n’.join(mylist) spam ham eggs For objects with non-string value >>> print str(mylist).strip(‘[]’) spam, ham, eggs >>> print str(mylist)[1:-1] spam, ham, eggs >>> print ‘, ‘.join(map(str, mylist)) spam, ham, eggs Taken from Decalage

Sets and Mathematical Operations in Python

A description of matched sets mathematical operations in Python, taken from the Python Tutorial.

else clause on loops

An interesting control flow for Python, for n in range(2, 10): for x in range(2, n): if n % x == 0: print n, ‘equals’, x, ‘*’, n/x break else: # loop fell through without finding a factor print n, ‘is a prime number’