For the flip coin demo, use range instead: heads = (sc.parallelize(range(n)) .map(lambda _: random.random()) .filter(lambda r: r <= 0.5) .count()) Python 3 was released in 2008! Why is the code with this course in 2.x?!