3 Reading
- Sheppard Chapter 1: Set up Anaconda (Python 3.6). Don’t worry about Jupyter notebooks or Spyder. Our workflow will be to put everything in a script to be run from the command line or in a IPython terminal.
- Learn Python3 the Hard Way through Exercise 8.
- Learn Python the Hard Way video for Exercise 21 on
Functions. Note: This was made for Python 2.7, which didn’t require
printto be used as a function. - Sheppard Chapters 3–4, 18, 5, 7, 9 (except 9.2 & 9.4), 10–13, 16. Chapters 6, 8, 14, 15, and 17 can be skimmed quickly or skipped altogether. These chapters just list various useful functions.
4 Exercises
The exercises in Sheppard are okay, but they’re set up more like trivia than actual programming problems. Instead, I would recommend practicing on the following problems found on Project Euler. I’ve added notes for several of the problems. Problem 42 is especially relevant because it requires reading data from a file and processing that data.
- 1 & 2.
- 3: The solution should have three functions:
is_primethat takes an integernand returnsTrueifnis prime andFalseotherwise;find_factorthat takes an integernand finds some factor of that integerfactorizethat takes an integernand, using the functionsis_primeandfind_factor, returns a list of the prime factors ofn(that is, a list of prime numbers that, when multiplied together, equaln).
- 4: The solution should use a function
is_palindromethat takes a numbernand returnsTrueif the number is a palindrome andFalseotherwise. - 6: Use a Numpy array (e.g.,
np.arange) and vector dot product. - 7: Use your
is_primefunction from Problem 3. - 8: Copy the 1000-digit number directly into your Python script.
- 9 & 10.
- 18 & 67*: Use backward induction. 67 might be harder for a beginner because you have to read a file from disk. If you find a general solution to 18 that’s not brute force, you can skip 67 for now.
- 42:
- Create a dictionary
letter_scorewhose keys are all the letters of the alphabet and whose values are the letters' letter score, e.g.{'A': 1, 'B': 2, ... }. Hint: you can (and should) do this programmatically usingfrom string import ascii_uppercaseand Python'srangebuiltin function. - Create a function
word_scorethat takes a word string as an argument and returns the word score. It should use theletter_score:dictionary. - Come up with a way to check if a word score is a triangle number.
- Use the pandas library (
import pandas as pd) to read in the file and create a DataFrame with three columns: “word”, “word_score”, “is_triangle”.
- Create a dictionary