Taking a jog down memory lane
Overview
Teaching: 10 min
Exercises: 30 minQuestions
What are we trying to do by the end of the session?
What have we covered in the session yesterday?
Objectives
Recap key concepts covered in Day 1
Key Points
Variables are pieces of data held in memory.
Different data types behave differently; Python does not care about the correctness of your output, only the correctness of your input.
Conditional evaluation provides a tool for us to control program flow.
📢 Welcome Back to Day 2!
I hope everything we talked about yesterday is still relatively fresh in your minds. If not, fret not, we will start off the day with some hands-on refresher exercises. As a reminder, we explored yesterday:
- How data zips across the internet (like tiny digital postcards) and lands in our programs.
- How to fetch an API response and read it without panicking at all those curly braces and square brackets.
- How to work with different data types—text, numbers, and dates—so we can actually use that data.
- How to make decisions in code using conditional logic (
if,elif,else).
The things we covered yesterday will provide us the building blocks to write your very first program to process data from an API. Today, we’ll level up with:
- Writing our own functions so our code can reuse its best tricks.
- Loops to repeat actions without typing the same thing a hundred times.
- Error handling so our programs don’t crash like a toddler on roller skates.
- Reading from and writing to files so our work can be saved and shared.
And at the end of the day, you’ll get to pick an API of your choice and process its data like a pro!
But first, refresher time!
Before we dive in to the contents for today, lets get everyone warmed up as we take a jog down memory lane. We will work in the same conda environment as we did yesterday for the workshop today, so you should not create a new environment. As a reminder, you can activate your environment by using the conda activate <environment name> command in your terminal.
1️⃣ Fetching and Reading API Data
Use this API to suggest a random activity:
https://bored-api.appbrewery.com/random
Print the suggested activity in a easy to read sentence such as the following:
"The suggested activity is Organize your pantry".
Hints
- You’ll need the
requestslibrary.- You will need to retrieve the activity from the JSON
- The key containing the activity is
activity
2️⃣ Working with Data Types
Create variables for:
- Your first name (string)
- Your age (integer)
- Today’s date and the current time in the YYYY-MM-DD, HH:mm format
Hints:
- You can use the
datetimemodule to get the current date and time
3️⃣ Conditional Logic
Write a program that checks if today’s temperature (from exercise 1) is:
- Above 25°C → print “It’s a hot day!”
- Between 15°C and 25°C → print “It’s a pleasant day.”
- Below 15°C → print “Brrr, it’s chilly.”
Hints:
Consider carefully what is the sequence in which we will want to perform this operation. Use the
if,elifkeywords