Python programming refresher
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.
Exercise 2: Write a sentence and store it as a variable. Write a function to return the word count of that sentence.
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
)
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]
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, useword = 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'sstr.isalpha()
function can tell us if any of the characters in a string aren't a letter.
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.