Exercise 9

Exercise 9#

There are many modules that offer file I/O solutions that are more convenient than that offered by the Python Standard library. In practice, you should use the best tool for the job, but the challenge of this exercise is to only make use of the functions offered in the Python Standard Library.

Question 1#

Write a script that reads the data from data1.csv and plots the data on a single set of axis. Create a legend using appropriate labels. You may assume (or rather infer from the heading) that the first column is the independent variable.

Question 2#

In this question you will write data to a file and test that file be reading it and plotting the data from that.

  1. Generate data for the functions:

    • \(f(x) = x^3 + 2\)

    • \(g(x) = -x^2 + 2x + 4\)

    • \(h(x) = |4x - 1|\)

    for \(-1 < x < 1\), and save the data to a file. Don’t forget to include a header.

  2. Read in your data and plot it. You can double check that everything is working by comparing this plot to using your data directly.

Question 3#

Write a script that reads the data from data2.txt and outputs it in a more readable format:

  • Round each figure to 2 decimal places

  • Make sure each column is aligned with at least a 4 space gap between them

  • Make sure the decimal points of the numbers are also aligned

Hint: Reading on 4.8 String Formatting will help.