Python on Raspberry Pi - Time & Space Complexity
When running Python code on a Raspberry Pi, it is important to understand how the time it takes to run your program grows as your input gets bigger.
We want to know how the program's running time changes when we give it more data to work with.
Analyze the time complexity of the following code snippet.
def sum_numbers(numbers):
total = 0
for num in numbers:
total += num
return total
nums = list(range(1, 101))
print(sum_numbers(nums))
This code adds up all the numbers in a list and prints the total.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each number in the list to add it to the total.
- How many times: Once for every number in the list.
As the list gets longer, the program takes longer because it adds more numbers one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The number of operations grows directly with the size of the input list.
Time Complexity: O(n)
This means the time to run the program grows in a straight line as the input list gets bigger.
[X] Wrong: "The program runs in the same time no matter how many numbers are in the list."
[OK] Correct: Because the program adds each number one by one, more numbers mean more work and more time.
Understanding how loops affect running time helps you explain your code clearly and shows you can think about efficiency, a skill useful in many programming tasks.
"What if we changed the list to a nested list and summed all numbers inside? How would the time complexity change?"
Practice
Solution
Step 1: Understand Raspberry Pi's use with Python
Python on Raspberry Pi is mainly used to control hardware components like LEDs and sensors.Step 2: Compare options with this purpose
The options describing website creation only, running Windows applications, and video editing do not relate to hardware control, so they are incorrect.Final Answer:
To control hardware like LEDs and sensors easily -> Option AQuick Check:
Python on Raspberry Pi = hardware control [OK]
- Confusing Raspberry Pi with a general computer
- Thinking Python on Pi is only for web development
- Assuming it runs Windows apps natively
Solution
Step 1: Recall the standard import syntax for GPIO
The correct import statement isimport RPi.GPIO as GPIOto use GPIO functions easily.Step 2: Check other options for syntax errors
The other options ('import GPIO.RPi', 'from GPIO import RPi', 'import raspberry.GPIO') do not follow Python import syntax or correct module names.Final Answer:
import RPi.GPIO as GPIO -> Option BQuick Check:
Standard GPIO import = import RPi.GPIO as GPIO [OK]
- Swapping module and package names incorrectly
- Using wrong import syntax
- Assuming 'GPIO' alone is a module
import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(18, GPIO.OUT) GPIO.output(18, GPIO.HIGH) print(GPIO.input(18))
Solution
Step 1: Understand GPIO setup and output
Pin 18 is set as output and then set to HIGH, which means voltage is ON (1).Step 2: Check GPIO.input(18) after setting HIGH
Reading the pin returns 1 because it was set HIGH.Final Answer:
1 -> Option CQuick Check:
GPIO.output HIGH means input reads 1 [OK]
- Confusing HIGH with string 'HIGH'
- Expecting 0 instead of 1 after setting HIGH
- Thinking GPIO.input returns string values
import RPi.GPIO as GPIO GPIO.setmode(GPIO.BOARD) GPIO.setup(12, GPIO.OUT) GPIO.output(12, HIGH)
Solution
Step 1: Check usage of HIGH constant
HIGH must be referenced as GPIO.HIGH, not just HIGH alone.Step 2: Verify other code parts
Pin 12 is valid in BOARD mode, setmode can be BOARD, and output pin can be output.Final Answer:
Missing GPIO prefix before HIGH -> Option AQuick Check:
Use GPIO.HIGH, not just HIGH [OK]
- Using HIGH without GPIO prefix
- Confusing BOARD and BCM pin numbering
- Setting output pin as input by mistake
Solution
Step 1: Check pin numbering and setup
BCM mode, import time, GPIO.setup(17, GPIO.OUT) are all correct.Step 2: Verify output and delay
GPIO.output(17, GPIO.HIGH), time.sleep(1), GPIO.output(17, GPIO.LOW) blinks once (on for 1s then off).Step 3: Identify errors in other options
One lacks import time (NameError on sleep); another uses BOARD mode (pin 17 ≠ BCM pin 17 physically); another sets GPIO.IN (should be OUT).Final Answer:
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setup(17, GPIO.OUT) GPIO.output(17, GPIO.HIGH) time.sleep(1) GPIO.output(17, GPIO.LOW) -> Option DQuick Check:
Use BCM mode, output pin, and time.sleep for blinking [OK]
- Mixing BOARD and BCM pin numbers
- Setting pin as input instead of output
- Forgetting to import time module
Ready to prove it?
Pass the test and this pattern moves unknown
