📖 Scenario: You are building a simple real-time dashboard on a Raspberry Pi to monitor temperature and humidity sensors in a greenhouse. The dashboard will collect sensor data, filter readings above a certain threshold, and display the results.
🎯 Goal: Create a Python program that stores sensor readings in a dictionary, sets a threshold for temperature, filters the readings above that threshold, and prints the filtered results.
📋 What You'll Learn
Create a dictionary with sensor names as keys and their temperature readings as values.
Create a variable to hold the temperature threshold.
Use a dictionary comprehension to filter sensors with temperature above the threshold.
Print the filtered dictionary showing sensors exceeding the threshold.
💡 Why This Matters
🌍 Real World
Monitoring environmental conditions like temperature and humidity in places such as greenhouses or server rooms helps maintain optimal conditions and prevent damage.
💼 Career
Skills in handling sensor data and filtering it in real-time are useful for IoT developers, embedded systems engineers, and automation specialists.
Progress0 / 4 steps
1
Create sensor data dictionary
Create a dictionary called sensor_readings with these exact entries: 'sensor1': 22.5, 'sensor2': 27.0, 'sensor3': 19.8, 'sensor4': 30.2
Raspberry Pi
Hint
Use curly braces {} to create a dictionary with keys and values separated by colons.
2
Set temperature threshold
Create a variable called temp_threshold and set it to 25.0
Raspberry Pi
Hint
Assign the value 25.0 to the variable temp_threshold.
3
Filter sensors above threshold
Use a dictionary comprehension to create a new dictionary called high_temps that contains only the sensors from sensor_readings with temperature values greater than temp_threshold
Raspberry Pi
Hint
Use {sensor: temp for sensor, temp in sensor_readings.items() if temp > temp_threshold} to filter the dictionary.
4
Print filtered sensor readings
Write a print statement to display the high_temps dictionary
Raspberry Pi
Hint
Use print(high_temps) to show the filtered dictionary.
Practice
(1/5)
1. What is the main purpose of a real-time sensor dashboard on a Raspberry Pi?
easy
A. To run heavy data processing tasks offline
B. To store sensor data for long-term analysis only
C. To display live sensor data instantly for monitoring
D. To control the Raspberry Pi remotely
Solution
Step 1: Understand the dashboard function
A real-time sensor dashboard shows live data as it changes, helping users monitor conditions instantly.
Step 2: Compare options to purpose
Only To display live sensor data instantly for monitoring describes displaying live sensor data instantly, which matches the dashboard's main goal.
Final Answer:
To display live sensor data instantly for monitoring -> Option C
Quick Check:
Real-time dashboard = live data display [OK]
Hint: Real-time means showing data as it happens [OK]
Common Mistakes:
Confusing storage with live display
Thinking dashboard controls the device
Assuming heavy processing is done on dashboard
2. Which Python code snippet correctly reads a sensor value on Raspberry Pi for real-time updates?
easy
A. sensor_value = sensor.read()
B. sensor_value == sensor.read()
C. sensor.read = sensor_value()
D. sensor_value := sensor.read
Solution
Step 1: Identify correct assignment syntax
In Python, to assign a value from a function call, use single equals: variable = function().
Step 2: Check each option
sensor_value = sensor.read() uses correct assignment and function call syntax. Options B and C misuse operators or assignment. sensor_value := sensor.read uses walrus operator incorrectly without parentheses.
Final Answer:
sensor_value = sensor.read() -> Option A
Quick Check:
Assignment uses =, not == or := alone [OK]
Hint: Use single = to assign function output to variable [OK]
Common Mistakes:
Using == instead of = for assignment
Trying to assign function call incorrectly
Misusing walrus operator without parentheses
3. Given this Python code snippet for updating a dashboard:
import time
sensor_value = 0
for _ in range(3):
sensor_value += 5
print(sensor_value)
time.sleep(1)
What will be the printed output?
medium
A. 5\n10\n15
B. 0\n5\n10
C. 5\n5\n5
D. 0\n0\n0
Solution
Step 1: Analyze the loop increments
Initially, sensor_value = 0. Each loop adds 5, so values become 5, then 10, then 15.
Step 2: Confirm printed values
Each iteration prints the updated sensor_value, so output lines are 5, 10, and 15.
Final Answer:
5\n10\n15 -> Option A
Quick Check:
Increment by 5 each loop = 5,10,15 [OK]
Hint: Add increments stepwise and print each [OK]
Common Mistakes:
Printing before incrementing
Resetting sensor_value inside loop
Ignoring loop iterations count
4. You wrote this code to update a sensor dashboard:
while True:
sensor_value = sensor.read
print(sensor_value)
time.sleep(1)
Why does the dashboard not show updated sensor values?
medium
A. Because time.sleep(1) pauses too long
B. Because sensor.read is missing parentheses to call the function
C. Because print() does not display values
D. Because while True causes an infinite loop
Solution
Step 1: Identify function call syntax error
sensor.read is a function and must be called with parentheses: sensor.read()
Step 2: Understand impact on sensor_value
Without parentheses, sensor_value stores the function object, not the sensor data, so printed values don't update.
Final Answer:
Because sensor.read is missing parentheses to call the function -> Option B
Quick Check:
Function call needs () to get value [OK]
Hint: Always add () to call functions [OK]
Common Mistakes:
Ignoring missing parentheses on function calls
Blaming sleep for no updates
Thinking print() doesn't show output
5. You want to build a real-time dashboard on Raspberry Pi that updates sensor data every second and shows the average of last 5 readings. Which approach is best?
hard
A. Calculate average once at start, then display static value
B. Store all readings forever and calculate average of all data each second
C. Update dashboard only when sensor value changes, ignoring average
D. Store last 5 readings in a list, update every second, calculate average, then update dashboard
To show average of last 5 readings updated every second, you must keep a rolling list of last 5 values.
Step 2: Evaluate options for efficiency and accuracy
Store last 5 readings in a list, update every second, calculate average, then update dashboard correctly stores last 5 readings, updates average each second, and refreshes dashboard. Calculate average once at start, then display static value is static, C ignores averaging, D is inefficient and grows data indefinitely.
Final Answer:
Store last 5 readings in a list, update every second, calculate average, then update dashboard -> Option D
Quick Check:
Rolling average needs recent 5 values updated regularly [OK]
Hint: Keep fixed-size list for rolling average updates [OK]
Common Mistakes:
Calculating average only once
Ignoring rolling window for average
Storing unlimited data causing slowdowns
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.