Link Search Menu Expand Document

Python programming refresher

  1. Python programming refresher
    1. Tips on learning how to code
    2. Exercises

Tips on learning how to code

The following are pieces of advice from programmers at the CDH and the library:

  • Google is a programmerā€™s best friend. Any problem you have, thereā€™s a 99.9 percent chance another person has had that exact problem before.
  • There are so many free resources on the internet to help you. Use them!
  • This is just writing instructions. Programming isnā€™t math. Donā€™t count yourself out just because you arenā€™t the best at mathematics.
  • The best way to become a coder is to code. Try things out. Mess up.
  • Weā€™re starting with python, but programming language really doesnā€™t matter. The most important thing is to get started.
  • Donā€™t expect to understand anything the first time around. Be kind to yourself, this is a whole new way of thinking.
  • Be persistent. Coding takes time, and the solution is out there. You will figure it out if you keep working at it!
  • Resist imposter syndrome.

Exercises

If you donā€™t know how to proceed, try googling the solution. Prepare an answer before clicking the ā€œView Solutionā€ button.

Exercise 1: Create a list of all integers between 1 and 10 and assign it to a variable. Print them out.

View Solution

Exercise 2: Write a sentence and store it as a variable. Write a function to return the word count of that sentence.

View Solution

Exercise 3: Make a dictionary that translates each letter of the alphabet into itā€™s corresponding place in the alphabet (e.g. "a" => 1, "b" => 2)

View Solution

Exercise 4: Write a function that turns a word into a list of numbers that corresponds with their placement in the alphabet. Use the dictionary you created in the previous exercise. For example, translate_word('princeton') would return [16, 18, 9, 14, 3, 5, 20, 15, 14]

View Solution

Exercise 5: What are some errors you could encounter when writing the translate_word function?

View Solution
  • We would get an error if we fed 'Princeton' into the function because it contains an uppercase letter. (Try it out to see what kind of error you would get.) To ensure that the string is lowercase, use word = word.lower().
  • This function expects a word. If we fed 'FSI Class of 2025!' into it, it would cause an error because the blank space character and the exclamation point isn't in our dictionary. Python's str.isalpha() function can tell us if any of the characters in a string aren't a letter.
With these considerations in mind, we can rewrite this function.

Code is never finished! We could keep rewriting many of these functions forever. Donā€™t get too wrapped up in making your code ā€œperfectā€. Write code that is clear, well-documented, and serves your ultimate research question.