Programming Style

prime_numbers = [x for x in range(2, 101) if all(x % y != 0 for y in range(2, int(x**0.5) + 1))

This is very concise, however, it is not readable. It is a monster of an expression!

What would happen if we just rewrite it like this?

def is_prime(n):
    if n <= 1:
        return False
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0:
            return False
    return True

prime_numbers = [x for x in range(2, 101) if is_prime(x)]

It is more code, however, it is much more readable… (Don’t care about not understanding all the syntax now, we’ll see that later).

Python has a nice style guide available. It is good to consult it once in a while.