Assignment 4: Python and Julia
1 Setup
Follow the Install Python and Install Jupyter guides
Follow the Install Julia guide
1.1 Accept Assignment on Github Classroom
Clone this assignment to your computer.
The repository contains two starter scripts with comments marking where you need to write code:
python-task.pyjulia-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
Create a Python virtual environment for this repository.
Install the required packages in the environment:
terminal
python3 -m pip install pandas seaborn2.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
Read
us_gdp.csvinto a pandas DataFrame calledgdp_df.Extract the
yearandgdpcolumns as Python lists calledyearsandus_gdp.Write a function
growth_rate(gdp_new, gdp_old)that returns the percentage growth rate.Using a
forloop and yourgrowth_ratefunction, compute the annual GDP growth rate for each year. Store the results in a list calledgrowth_rates.Using a
forloop and anif/elsestatement, create a list calledlabelsthat contains"Expansion"if the growth rate is positive or"Contraction"if it is zero or negative.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)
- i.e.
Part 2: Dictionaries and Pandas
Create a dictionary with keys
"year","growth_rate", and"label", then create a pandas DataFrame calleddffrom that dictionary.Use boolean indexing to filter
dffor only contraction years. Print the result.Compute and print the mean growth rate.
Part 3: Plotting
- 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 aspython-growth.pdf.
2.3 Save Your Environment
After completing the script, preserve your environment:
terminal
python3 -m pip freeze > requirements.txtThis 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
Activate a Julia environment for this repository.
- Open a Julia REPL, press
]to enter the Pkg REPL, then run:
- Open a Julia REPL, press
pkg>
activate .- Install the required packages:
pkg>
add CSV DataFrames Plots- 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
Read
us_gdp.csvinto a DataFrame calledgdp_dfusingCSV.read().Extract the
yearandgdpcolumns as vectors calledyearsandus_gdp.Write a function
growth_rate(gdp_new, gdp_old)that returns the percentage growth rate.Use broadcasting (the
.operator) to compute all growth rates in a single line. Store ingrowth_rates.Write a function
classify(x)that returns"Expansion"if the input is positive or"Contraction"otherwise.Use broadcasting to apply
classifyto all growth rates. Store inlabels.Use a
forloop with string interpolation ($) to print each year with its growth rate (rounded to 2 decimal places) and label.- Example output:
2015: 3.88% (Expansion)
- Example output:
Part 2: DataFrames
Create a DataFrame called
dfwith columns:year,:growth_rate, and:label.Filter
dffor contraction years and print the result.Compute and print the mean growth rate.
Part 3: Plotting
- Using
Plots.jl, create a bar chart of growth rates by year. Add a title and axis labels. Save the figure asjulia-growth.pdf.
4 Submission
4.1 Push to Github
Make sure to commit and push the following files to Github:
python-task.pyjulia-task.jlrequirements.txtpython-growth.pdfProject.tomlManifest.tomljulia-growth.pdf
You do not need to commit:
.vscode/.venv/(this should have a.gitignoreby default)
If you want to make the .vscode/ folder is ignored, add it to your .gitignore file:
.gitignore
.vscode/