π» Week 03 Lab
Control Flow Practice with Real Data
By the end of this lab, you should be able to: i) Collect data from an API and save to JSON files, ii) Read JSON data back into Python for analysis, iii) Use loops to process data systematically, iv) Use conditionals to categorize and filter data.
This lab builds directly on yesterdayβs π₯οΈ W03 Lecture where you learned about file I/O and saw code patterns for working with JSON files. Today youβll apply those patterns alongside your DataQuest knowledge of loops and conditionals to analyze real temperature data.
π Preparation
- Complete the π W03 Practice (Terminal commands and DataQuest loops/conditionals)
- You should be comfortable with
forloops andif/elsestatements - Your notes on DataQuest lessons will be essential today
- You should be comfortable with
- Review the π₯οΈ W03 Lecture slides on JSON file I/O
- Youβll adapt those code patterns today
- Ensure you can access Nuvolos and launch VS Code:
Using Nuvolos
π£οΈ Lab Roadmap
| Part | Activity Type | Focus | Time | Outcome |
|---|---|---|---|---|
| Part 0 | π― ACTION POINTS | Setup & Preparation | 5 min | Download notebook + create data folder |
| Part I | π£οΈ TEACHING MOMENT | Catch Up & Revision | 10 min | Connect learning + revise loops & conditionals |
| Part II | π£οΈ TEACHING MOMENT | Code Demonstration | 20 min | Understand API β JSON β Read workflow |
| Part III | π― ACTION POINTS | Control Flow Practice | 40 min | Create is_hot_day list independently |
| Wrap-Up | π£οΈ TEACHING MOMENT | Solution & W04 Setup | 10-15 min | Demonstrate solution + W04 Practice setup |
π‘ What about Git? You will need to use your Git skills for your π W04 Practice repository right after this lab. The focus of the lab itself, though, is to help you write code with loops and conditionals.
Part 0: Setup & Preparation (5 min)
Note to class teachers: Students complete this setup independently while you prepare for Part I. This gives them time to get organized and ensures everyone is ready for the teaching moments. Circulate to help with any technical issues.
Setup for Today:
Download the notebook
Use the button below to download the notebook we will use in this lab and save it to your
week03/folder inmy-ds105a-notes.Create the data folder
Open Terminal and run:
cd /files/my-ds105a-notes/week03 mkdir dataOpen the notebook
Open the notebook in VS Code and get ready for Part I.
π The Big Picture: Today youβll bring together loops, conditionals, API calls, and file operations to analyze real temperature data. This is exactly the workflow youβll use for W04 Practice!
Part I: Catch Up & Revision (10 min)
Note to class teachers: This is an interactive discussion to connect what students have learned across W01-W03. Gather from students what theyβve learned, help them see connections between Terminal, Python fundamentals, APIs, and file I/O. Then revise if-else and for loops with concrete examples before showing the notebook. This sets context for todayβs work. Make it conversational and build on their responses.
Your class teacher will lead a discussion to connect everything youβve learned so far and revise key concepts before todayβs practical work.
Suggested Discussion Topics:
- W01: What is data? Working with pandas to read CSV files and filter data
- W02: APIs and how to collect data from the internet using
requests.get() - W03 Practice: Terminal commands (
cd,ls,pwd,mkdir) and DataQuest loops/conditionals - W03 Lecture: File systems, JSON file operations, and why we save data locally
Your teacher will now show you the notebook structure, help you catch up with the relevant programming concepts and explain whatβs coming in Part II.
Part II: Code Demonstration (20 min)
Note to class teachers: Demonstrate Section 2 of the notebook line by line. Explain API requests, JSON file I/O, and nested data extraction. Students run code along with you. Focus on the complete workflow: API β JSON save β JSON load β data extraction. Connect to W03 Lecture patterns.
Your class teacher will demonstrate Sections 1 and 2 of the notebook, explaining each line of code as you follow along and run it yourself.
What youβll see:
- API request to collect July 2024 temperature data
- Saving data to JSON file (
data/july_2024.json) - Reading the JSON file back into Python
- Extracting dates and temperatures into separate lists
π‘ Why you should save data locally: Every time you send a request to an API, you are using up a limited resource. It is common for API servers to get overwhelmed and start rejecting requests. By saving the data locally, you collect it once and then work with it without relying on the availability of the API server.
Part III: Control Flow Practice (40 min)
Note to class teachers: Students work independently or in pairs on Part III of the notebook to create the is_hot_day list. Circulate to help with debugging. At the end, demonstrate the solution and connect to W04 Practice and W01 Lab.
π― ACTION POINTS
Write the relevant code to create a list called is_hot_day that categorises each day in July 2024 as True if temperature β₯ 28Β°C and False if temperature < 28Β°C.
π‘ Consult your π W03 Practice notes to remind yourself of how to write code with loops and conditionals.
Wrap-Up & Next Steps (10-15 min)
Note to class teachers: Demonstrate your own is_hot_day solution, then point students to W04 Practice. If time allows, guide students through setting up W04 Practice repository; otherwise, remind them of the support sessions and point them to
#helpon Slack.
Your class teacher will demonstrate the solution and guide you through setting up π W04 Practice due on Thursday 23 October, 12:00 GMT.
π Useful Resources
π Essential Guides
- 3οΈβ£ Data Science Workflow: Complete workflow stages and best practices
- 4οΈβ£ Git & GitHub Guide: Version control commands and workflows
π» Course Materials
- π£οΈ W03 Lecture: File I/O demonstration and API patterns
- π W03 Practice: DataQuest loops and conditionals practice
- π W04 Practice: Your next task (due Thursday 23 October, 12:00 GMT)
π Getting Help
- Slack: Post questions to
#helpchannel - Office Hours: Book via StudentHub
- Self-Guided Practice: Fire up a new chat with our Custom AI assistant and type βTime for a challenge!β
Check staff availability on the β Contact Hours page.
π External Resources
Open-Meteo API Documentation: Weather data API reference
π’ Remember: Your lab notebook has complete working code - use it as reference for your W04 Practice! The patterns you learned today will be essential for your next assignment.
