1.7 Exercises

Carrying out the following exercises in both R and Python. There is no need to do the exercises in any particular order. Take the time to design pseudo-code and think about what the code does before jumping directly into the programming.

You may choose to carry out each of the exercises separately, or to write a single program that carries out all of the individual exercises.

You will find much of the base code you need in this module’s examples, but you will need to tweak and add to this code to carry out the exercises. Do not hesitate to look for information and inspiration on the Internet and in the documentation.

  1. Create three variables and assign numerical values to each of these variables. Then write one or more statements that carry out the following types of operations using these variables: addition, subtraction, multiplication, division, raising to a power.

  2. Create three variables and assign string values to each of these variables. Write a statement that joins the three strings into a single string. Write some code that prints the string. Write some code that tests to see if a substring of your choice is contained within the larger string.

  3. Create three variables and assign lists to each of these variables. Join the three lists into a new list containing three distinct sub-lists (a list of three lists). Create a list from this list without sub-lists (all original list elements are part of a single larger list). Create a fourth list by splitting this resulting list in half and assigning the second half of the list to a new variable. Extract the last item of this list (it can either stay in the original list or be removed from it) and assign this element to a variable.

  4. Write a statement that contains at least three nested blocks. Use at least three of the following control flow options: if, if else, while, for, break, continue (Python only), next, switch.

  5. Write a function that takes three arguments as input and returns one value. Call the function with arguments of your choosing.

  6. Execute the relevant command that shows a list of the packages (for R) or modules (for Python) that are currently installed in your environment. Use the available documentation to determine what some of these do. Write some code that uses functions and objects supplied by these packages.

  7. Print to the standard output three sentences of your choosing, on three separate lines, using a single statement of code.

  8. Locate a comma separated values (.csv) file stored on your computer or online. Read this file into the notebook and store the results in one or more variables.

  9. Create a new file and write four lines in .csv format to this file. In a separate statement, write four more lines to this existing file, without overwriting the original file.

  10. Write enough code to generate at least five different error messages. Copy these error messages into a text document, and write a short note under each explaining the meaning of the error message, and how the code was fixed.

  11. Using a language of your choice, write a function that, when passed a dataset, reports 5 interesting pieces of information about the dataset. Load a dataset and run the function on this dataset.

  12. Using a language of your choice, write two functions. The output of the first function should work as the input to the second function. The first function should read in a dataset and generate a subset of the dataset based on some chosen criteria. The second function should read in a dataset and provide summary data of some type for each column in the dataset. Load a dataset and run both functions on the dataset.

  13. Write a program that sorts a list of numbers.

  14. Write a program that sorts a list of character strings.