This version uses the fact that a non-prime must have at least one prime factor smaller than its square root to maximise search efficiency, by using break statements to fail fast. There is a school of thought which feels that this makes code harder to understand.
Exercise: Find the mean: short version
For the short exercise the following code will work.
Note that the extra float command in the mean function is protection for Python 2, where integer division behaves differently from floating point division by default.
Exercise: Find the mean: version with options parsing
For the version with options parsing, the following code will work.
Exercise: Plots in scripts
This is the "do everything version. We use the axis command to scale everything so the sine and cosine functions can be seen.
Exercise: Complex square root
The following code is acceptable. Note that the primary interest here is the documentation
Exercise: A primes module
Exercise: A primes package
Download and unzip the following file to obtain the example primes Python package. The prime factors function looks something like the following: