80-629 -- Python exercises

These are meant to complement the python tutorial. In brackets next to each exercise I've indicated which Sections are required to complete the exercise.

I suggest using a separate cell for each exercise.

  1. [Section 3] Lists. Create a list of containing the 10 smallest prime numbers (encode each element as an number). Ensure that your list has exactly 10 elements. Then print the last five elements of the list.

    *Bonus*: list every second element in the list.
  2. [Section 4] Control statements (loops). Print the sum of the elements in the list you created in the previous exercise.
  3. [Section 4] Control statements (loops). Write a function that checks whether a particular input is prime or not. The function should either print "PRIME" or "NOT PRIME".

    *Bonus*: When your function finds a non-prime member, have it explain to the user why the number is not prime by listing all of its (positive) divisors.
  4. [Section 5] Lists. Suggest two different ways for reversing the order of the elements in a list (you can re-use your list of prime numbers from above).
  5. [Section 5] Functional programming tools. Using the filter function, automatically create a list of all prime numbers less than 100.
  6. [Section 5] Dictionaries. Organize a list of all natural number less than 100 into a dictionary according to whether or not they are prime (i.e., a dictionary with two keys: 'prime' and 'not prime').
  7. [Section 7] Input & Output. Write the list of all prime numbers less than 100 to a file. Then read the file back into a list.

    *Bonus* When printing right-align all numbers.
  8. [Section 9] Classes and objects. Create a class that will be used to manipulate prime numbers. Your class should have three functionalities: 1) generate all prime number in a particular range, 2) test the membership of a new number, and 3) print all prime number from a given list. Note you are free to re-use some of the functions that you've created before.

Solutions