Assignment 4: Python and Julia

Author

Matthew DeHaven

Published

February 23, 2026

1 Setup

1.1 Accept Assignment on Github Classroom

  1. Accept Assignment 4 on Github Classroom.

  2. Clone this assignment to your computer.

The repository contains two starter scripts with comments marking where you need to write code:

  • python-task.py
  • julia-task.jl

and a shared data file:

  • us_gdp.csv — US nominal GDP (trillions of USD) from 2014–2023

You will complete both scripts to read in the same CSV, compute growth rates, classify expansions and contractions, and produce a chart in each language.

2 Python Task

2.1 Create Environment

  1. Create a Python virtual environment for this repository.

  2. Install the required packages in the environment:

terminal
python3 -m pip install pandas seaborn

2.2 Complete python-task.py

Open python-task.py and complete each section. The script has three parts:

Part 1: For Loops, If/Else, and f-strings

  1. Read us_gdp.csv into a pandas DataFrame called gdp_df.

  2. Extract the year and gdp columns as Python lists called years and us_gdp.

  3. Write a function growth_rate(gdp_new, gdp_old) that returns the percentage growth rate.

  4. Using a for loop and your growth_rate function, compute the annual GDP growth rate for each year. Store the results in a list called growth_rates.

  5. Using a for loop and an if/else statement, create a list called labels that contains "Expansion" if the growth rate is positive or "Contraction" if it is zero or negative.

  6. Print each year with its growth rate and label using f-strings. Format the growth rate to 2 decimal places.

    • i.e. 2015: 3.88% (Expansion)

Part 2: Dictionaries and Pandas

  1. Create a dictionary with keys "year", "growth_rate", and "label", then create a pandas DataFrame called df from that dictionary.

  2. Use boolean indexing to filter df for only contraction years. Print the result.

  3. Compute and print the mean growth rate.

Part 3: Plotting

  1. Using seaborn, create a bar chart of growth rates by year. Color bars green for positive growth and red for negative. Add a title and axis labels. Save the figure as python-growth.pdf.

2.3 Save Your Environment

After completing the script, preserve your environment:

terminal
python3 -m pip freeze > requirements.txt

This saves all the current package versions so others can recreate your environment with python3 -m pip install -r requirements.txt.

3 Julia Task

3.1 Create Environment

  1. Activate a Julia environment for this repository.

    • Open a Julia REPL, press ] to enter the Pkg REPL, then run:
pkg>
activate .
  1. Install the required packages:
pkg>
add CSV DataFrames Plots
  1. Link the Julia environment to your VS Code workspace.1

1 Open the Command Palette (Shift+Cmd+P) > “Julia: Change Current Environment”, or click “Julia env:” in the bottom-left of VS Code.

3.2 Complete julia-task.jl

Open julia-task.jl and complete each section. The script has three parts:

Part 1: Broadcasting, Functions, and String Interpolation

  1. Read us_gdp.csv into a DataFrame called gdp_df using CSV.read().

  2. Extract the year and gdp columns as vectors called years and us_gdp.

  3. Write a function growth_rate(gdp_new, gdp_old) that returns the percentage growth rate.

  4. Use broadcasting (the . operator) to compute all growth rates in a single line. Store in growth_rates.

  5. Write a function classify(x) that returns "Expansion" if the input is positive or "Contraction" otherwise.

  6. Use broadcasting to apply classify to all growth rates. Store in labels.

  7. Use a for loop with string interpolation ($) to print each year with its growth rate (rounded to 2 decimal places) and label.

    • Example output: 2015: 3.88% (Expansion)

Part 2: DataFrames

  1. Create a DataFrame called df with columns :year, :growth_rate, and :label.

  2. Filter df for contraction years and print the result.

  3. Compute and print the mean growth rate.

Part 3: Plotting

  1. Using Plots.jl, create a bar chart of growth rates by year. Add a title and axis labels. Save the figure as julia-growth.pdf.

4 Submission

4.1 Push to Github

Make sure to commit and push the following files to Github:

  • python-task.py
  • julia-task.jl
  • requirements.txt
  • python-growth.pdf
  • Project.toml
  • Manifest.toml
  • julia-growth.pdf

You do not need to commit:

  • .vscode/
  • .venv/ (this should have a .gitignore by default)
Note

If you want to make the .vscode/ folder is ignored, add it to your .gitignore file:

.gitignore
.vscode/