CS 111: Introduction to Computer Science

Practice Exercises

This is a list of programming exercises you can use to improve your command of Python. I will keep adding to it when I think of new ideas. I might conceivably use some of these exercises on exams, in future assignments, or as examples in class, but mostly, I just have them here in case you would like to practice.

Note that some of these exercises require concepts we won't discuss in class until later in the term: command-line arguments, recursion, etc. I have made no real effort to put elementary exercises earlier in the list than advanced exercises. If you don't think you know enough to do a particular exercise, then try a different one. (Or use your textbook to learn what you need to know.)

Feel free to ask questions about these exercises any time.


Write a program that prints out a message of your choosing. "Hello, world." is the traditional message.

Write a program that asks the user for his/her name and age, and prints out a customized greeting involving both the name and age.

Write a program that prints out all the odd numbers from 1 to 99.

Write a program that prints out all the even numbers from 100 to 0, starting at 100 and going down.

Write a program that asks the user for a positive integer n and then prints out all the odd numbers less than or equal to n.

Write a function that prints the lyrics of "n Bottles of Beer on the Wall", where n is a parameter passed to the function.

Write a function that returns the nth Fibonacci number.

Write a recursive function that returns the nth Fibonacci number.

Write a program that:

  1. prints a menu of numbered options
  2. asks the user to type a number
  3. performs the action specified by the user's number

Write a program that:

  1. prints a menu of numbered options, including one for the option "Quit"
  2. asks the user to type a number
  3. performs the action specified by the user's number
  4. if the user did not choose the "Quit" number, go back to step #1.

Modify the menu programs listed above to detect and politely complain about invalid user input.

Write a function that returns True if its parameter n is a prime number, and False otherwise.

Write a function that returns True if its parameter s is a palindome, and False otherwise.

Write a function that takes one string parameter and returns the reverse of the string. For example, if the parameter is 'moose', the function would return 'esoom'.

Write a function that returns the number of vowels in its string parameter s.

Write a function that returns the number of consonants in its string parameter (perhaps use the vowel-counting function to simplify this function?).

Write a function that solves quadratic equations. (What parameters would such a function require? What would the return value be?)

Write a function that takes two integers as parameters and returns their greates common divisor.

Write a function that returns True if its list parameter contains duplicates (i.e. at least two identical items), and False otherwise.

Write a function that returns True if all the items in the list are numbers (integers or real numbers), and False otherwise.

Write a function that takes a list of numbers as a parameter, and returns the mean of the numbers in the list.

Write a function that takes a list of numbers as a parameter, and returns the median of the numbers in the list.

Write a function that takes a list of numbers as a parameter, and returns the mode of the numbers in the list. (What if there are multiple modes?)

Write a function that takes a list of numbers as a parameter, and returns the maximum of the list (i.e. the largest number in the list).

Write a function that takes a list of objects as a parameter, and returns True if all the objects have the same type, and False otherwise.

Write a function that removes the duplicate items from a list passed to the function as a parameter.

Write a function that returns the number of distinct items in a list.

Write a function that takes two lists as parameters, and returns a list containing the union of the two lists, with all duplicates removed.

Write a function that takes two lists as parameters, and returns a list containing the intersection of the two lists, with all duplicates removed.

Write a function that takes a file name as its parameter, and returns the number of characters in the named file.

Write a function that takes a file name as its parameter, and returns the number of lines in the named file.

Write a function that takes two strings as parameters, and returns True if the two strings are anagrams of one another.

Write a function that takes a day number and returns the month in which the day occurs. For example, monthOfDay(35) would return 2 (for February), and monthOfDay(360) would return 12. A first version of this program could ignore leap years, but a better version would somehow take leap year into account.

Write a function that takes a month (1-12) and a day (1-31), and returns the day number within the year. For example dayNumber(2, 5) would return 36 (because Feb 5 is the 36th day of the year).

Write a program that accepts the user's name and age as command-line arguments, and prints a customized greeting involving both.

Write a program that lists all the jpg files in the current directory and its subdirectories.

Write a program that lists all the ".xxx" files in a directory, where "xxx" and the directory are specified on the command line.

Some harder ones

Write a program that will play Bagels with you. Writing it so that you are the guesser is reasonably straight-forward. Writing it so that the computer can be the guesser is harder, but cool.

Same thing, with Tic-tac-toe.

This one's harder, but cool. Write a program to let the user enter a string, and then print out a list of anagrams of the string. For example, if I typed "Jeffrey Robert Ondich", I'd get a list of anagrams, possibly including "fjord conifer thereby". For this one, you'll need a word list. A good one is SOWPODS, a copy of which can be found at http://cs.carleton.edu/faculty/jondich/documents/sowpods.txt.