DS105 2025-2026 Autumn Term Icon

💻 Week 03 Lab

Control Flow Practice with Real Data

Author

Dr Jon Cardoso-Silva

Published

15 October 2025

🥅 Learning Goals

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 for loops and if/else statements
    • Your notes on DataQuest lessons will be essential today
  • 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 I 🗣️ TEACHING MOMENT Catch Up & Revision 20 min Connect learning + revise loops & conditionals
Part II 🗣️ TEACHING MOMENT Code Demonstration 30 min Understand API → JSON → Read workflow
Part III 🎯 ACTION POINTS Control Flow Practice 35 min Create is_hot_day list independently
Wrap-Up 🗣️ TEACHING MOMENT Solution & Connections 5 min Link to W04 Practice & W01 algorithm

👉 NOTE: Whenever you see a 🗣️ TEACHING MOMENT, this means your class teacher deserves your full attention!

💡 What about Git? We’ll set up your W04 Practice repository after this lab. Today’s focus is pure data analysis practice and connecting everything you’ve learned.

Part I: Catch Up & Revision (20 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.

Discussion Topics (Teacher-Led):

Your teacher will ask you to recall and connect:

What We’ve Learned So Far:

  • 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

How It All Connects:

Your teacher will help you see how these pieces fit together into a complete data workflow.

Revision: Loops and Conditionals

Your teacher will revise with examples:

  • For loops: How do we process each item in a list?
  • If-else statements: How do we make decisions about data?
  • Combining them: How do we use loops + conditionals to categorize data?

Setup for Today:

  1. Download the notebook

    Download W03-NB03-Control-Flow-Practice.ipynb and save it to your week03/ folder in my-ds105a-notes.

  2. Create the data folder

    Open Terminal and run:

    cd /files/my-ds105a-notes/week03
    mkdir data
  3. Open the notebook

    Your teacher will now show you the notebook structure and explain what’s coming in Part II.

🔗 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 II: Code Demonstration - API to Analysis Workflow (30 min)

Note to class teachers: This is a teacher-led code demonstration. Walk through the notebook Part II section step by step, running the code and explaining what each part does. Students follow along in their own notebooks but you’re showing complete working code, not asking them to fill in gaps. Explain why we use archive API, the structure of API requests, JSON saving/reading patterns, and nested data extraction. Make connections to yesterday’s lecture. Students should run the code along with you and see it work.

Your class teacher will demonstrate the complete workflow from API request to data ready for analysis. Follow along in your notebook and run the code as it’s explained.

Your teacher will walk through:

Step 1: Collect Data from Open-Meteo API

  • Why we use the archive endpoint (historical data, not forecasts!)
  • Building the API request with proper parameters:
    • Latitude/longitude for London
    • Date range: July 2024
    • Daily maximum temperature
  • Making the request and checking the response

Step 2: Save Data to JSON File

  • Why saving is essential (speed + API respect)
  • Using json.dump() with with open()
  • The indent=2 parameter for readable JSON
  • Where the file gets saved (data/july_2024.json)

Step 3: Read Data from JSON File

  • Reading saved data with json.load()
  • Understanding nested dictionary structure
  • Extracting dates and temperatures into separate lists
  • Verifying the data loaded correctly

📊 This is the professional workflow: Collect once → Save immediately → Work from file. You’ll use this exact pattern for W04 Practice with 20 years of data instead of one month.

As your teacher demonstrates:

  • Run each code cell in your own notebook
  • Watch the output and make sure it matches
  • Ask questions if something doesn’t make sense
  • Note how the code connects to yesterday’s lecture patterns

💡 Key Insight: The code is complete and working. You’re seeing the full workflow in action, not filling in gaps. This gives you the reference you need for W04 Practice.

Part III: Control Flow Practice - Create the is_hot_day List (35 min)

Note to class teachers: This is the main challenge. Students work independently or in pairs on Part III of the notebook to create the is_hot_day list. That’s the only task. Circulate to help with debugging. At the end (last 5 minutes), move to the Wrap-Up section where you’ll demonstrate the solution and make the W04/W01 connections.

Now it’s your turn to apply what you learned in DataQuest. You have one clear task.

🎯 YOUR TASK

In Part III of your notebook, create a list called is_hot_day that categorizes each day in July 2024:

  • True if temperature ≥ 28°C
  • False if temperature < 28°C

The strategy is outlined in your notebook:

  1. Create an empty list: is_hot_day = []
  2. Loop through temperatures: for i in range(len(temps)):
  3. Use if/else to check each temperature
  4. Append True or False to your list

Work on this now. Try it yourself first. Your teacher will demonstrate the solution at the end.

Debugging Tips:

  • Print temps[i] inside your loop to see what you’re comparing
  • Check your list length: len(is_hot_day) should equal len(temps)
  • Use sum(is_hot_day) to count True values
  • Talk through your logic with a neighbour

🤔 Think about it: This is the same pattern you’ll use for W04 Practice, just scaled up. Same loop structure, same conditional logic, same append pattern.


Wrap-Up: Solution & Connections (5 min)

Note to class teachers: Bring everyone together. Demonstrate the is_hot_day solution on the projector, explaining the logic step by step. Then make the crucial connections: 1) This is exactly the pattern for W04 Practice (same logic, scaled to 20 years), and 2) This brings to life the algorithm they created in W01 Lab Section 3. Show them how far they’ve come - from designing an algorithm on paper to implementing it in Python!

Your class teacher will demonstrate the solution and explain how this connects to everything you’ve learned.

Teacher Demonstration:

Your teacher will show you one way to create the is_hot_day list and explain the logic.

The Big Connections:

Your teacher will explain:

  1. Connection to W04 Practice

    The W04 Practice task asks you to do almost exactly what you did today, but:

    • With 20 years of data instead of one month
    • Counting hot days per year
    • Saving results to a CSV file

    The loop + conditional pattern stays the same. You just learned the core logic!

  2. Connection to W01 Lab Section 3

    Remember in Week 01 Lab when you designed an algorithm for detecting heatwaves? You described the steps in plain English:

    • Look at each day
    • Check if it’s hot
    • Keep track of results

    That was the algorithm. Today you brought it to life in Python! You now have the skills to implement what you designed back in Week 01.

🎓 You’ve Come a Long Way: From describing algorithms in words (W01) to implementing them with loops and conditionals (today). This is real programming!


🎯 Next Steps: W04 Practice Setup

The work you did today gives you the complete workflow for W04 Practice. Your class teacher will now guide you through setting up the W04 repository.

What You Have Now:

✅ Complete working code for API → JSON → Read workflow
✅ Understanding of loops and conditionals for data analysis
✅ A reference notebook with the exact patterns you’ll need
✅ Confidence in the fundamental data science workflow

What’s Next for W04 Practice:

The 📝 W04 Practice task uses the same workflow:

  • Collect 20 years of data (same API request pattern, different dates)
  • Count “hot days” for each year (same loop + conditional logic)
  • Output results to a CSV file (you saw this in yesterday’s lecture)
  • Write a clear README explaining your project
  • Use Git to track your work

The core workflow is exactly the same: Collect → Store → Read → Analyze → Output

📋 Post-Lab Setup: Your class teacher will now guide you through accepting the W04 Practice GitHub Classroom assignment and cloning your repository. This takes approximately 15 minutes. Then you’ll have everything ready to start!

Deadline: Wednesday 22 October, 21:00

Your lab notebook is now your W04 reference! The code works, the patterns are there, you just need to adapt them to 20 years of data instead of one month.