Bell number \$B(n)\$ is defined as the number of ways of splitting \$n\$ into any number of parts, also defined as the sum of previous \$n\$ Stirling numbers of second kind.Here is a snippet of Python code that Wikipedia provides (slightly modified) to print bell numbers:def bell_numbers(start, stop):   t = [[1]]                        ## Initialize the triangle as a two-dimensional array   c = 1                            ## Bell numbers count   while c <= stop:       if c >= start:           yield t[-1][0]           ## Yield the Bell number of the previous row       row = [t[-1][-1]]            ## Initialize a new row       for b in t[-1]:        row.append(row[-1] + b)  ## Populate the new row       c += 1                       ## We have found another Bell number       t.append(row)                ## Append the row to the triangle   for b in bell_numbers(1, 9):       print bBut I have to print the \$n\$th bell number, so what I did was I changed the second last line of code as follows:for b in bell_numbers(n,n)This does the job, but I was wondering of an even better way to print the \$n\$th bell number.
Printing nth bell number
python;combinatorics
You can use the Dobinski formula for calculating Bell numbers more efficiently. Dobinski's formula is basically something like this line, although the actual implementation is a bit more complicated (this function neither returns precise value nor is efficient, see this blogpost for more on this):import mathITERATIONS = 1000def bell_number(N):    return (1/math.e) * sum([(k**N)/(math.factorial(k)) for k in range(ITERATIONS)])E.g. you can use the mpmath package to calculate the nth Bell number using this formula:from mpmath import *mp.dps = 30print bell(100)You can check the implementation here