Bird
Raised Fist0
Raspberry Piprogramming~20 mins

Python on Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Raspberry Pi Python Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
GPIO Pin Setup and Output
What is the output of this code when run on a Raspberry Pi with an LED connected to GPIO pin 18?
Raspberry Pi
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)

GPIO.output(18, GPIO.HIGH)
time.sleep(1)
GPIO.output(18, GPIO.LOW)
print("LED toggled")
GPIO.cleanup()
ALED toggled
BRuntimeError: No access to GPIO
CSyntaxError: invalid syntax
DLED toggled twice
Attempts:
2 left
💡 Hint
Focus on what the print statement outputs after toggling the LED.
❓ Predict Output
intermediate
2:00remaining
Reading a Button Press
What will be printed if the button connected to GPIO pin 23 is pressed when this code runs?
Raspberry Pi
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)

if GPIO.input(23) == GPIO.LOW:
    print("Button Pressed")
else:
    print("Button Not Pressed")

GPIO.cleanup()
ASyntaxError: invalid indentation
BButton Not Pressed
CRuntimeError: GPIO not set
DButton Pressed
Attempts:
2 left
💡 Hint
Remember that the button pulls the pin LOW when pressed due to pull-up resistor.
🔧 Debug
advanced
2:30remaining
Fix the PWM Frequency Error
This code is intended to make an LED connected to GPIO 12 fade in and out using PWM. What error will it raise?
Raspberry Pi
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(12, GPIO.OUT)

pwm = GPIO.PWM(12, 1000)
pwm.start(0)

try:
    for dc in range(0, 101, 5):
        pwm.ChangeDutyCycle(dc)
        time.sleep(0.05)
    for dc in range(100, -1, -5):
        pwm.ChangeDutyCycle(dc)
        time.sleep(0.05)
finally:
    pwm.stop()
    GPIO.cleanup()
ANo error, LED fades in and out
BRuntimeError: PWM not supported on this pin
CTypeError: PWM frequency must be a number, not str
DSyntaxError: invalid syntax in for loop
Attempts:
2 left
💡 Hint
Check the data type of the frequency argument in GPIO.PWM.
❓ Predict Output
advanced
2:00remaining
I2C Sensor Data Reading
What will this code print if the I2C device at address 0x48 returns the byte 0x1A when reading register 0x00?
Raspberry Pi
import smbus

bus = smbus.SMBus(1)
address = 0x48
register = 0x00

value = bus.read_byte_data(address, register)
print(f"Sensor value: {value}")
AIOError: No such device
BSensor value: 26
CSensor value: 0x1A
DTypeError: unsupported operand
Attempts:
2 left
💡 Hint
Remember that 0x1A in hexadecimal equals 26 in decimal.
🧠 Conceptual
expert
3:00remaining
Understanding Raspberry Pi GPIO Pull-up and Pull-down
Which statement correctly explains the difference between pull-up and pull-down resistors in Raspberry Pi GPIO input pins?
APull-up resistors connect the pin to 3.3V to ensure a default HIGH state; pull-down resistors connect the pin to ground to ensure a default LOW state.
BPull-up resistors connect the pin to ground to ensure a default LOW state; pull-down resistors connect the pin to 3.3V to ensure a default HIGH state.
CPull-up resistors increase the voltage beyond 3.3V; pull-down resistors decrease voltage below 0V.
DPull-up and pull-down resistors are used to amplify the input signal on GPIO pins.
Attempts:
2 left
💡 Hint
Think about how to keep a GPIO pin at a known voltage level when no button is pressed.

Practice

(1/5)
1. What is the main purpose of using Python on a Raspberry Pi?
easy
A. To control hardware like LEDs and sensors easily
B. To create websites only
C. To run Windows applications
D. To edit videos

Solution

  1. Step 1: Understand Raspberry Pi's use with Python

    Python on Raspberry Pi is mainly used to control hardware components like LEDs and sensors.
  2. 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.
  3. Final Answer:

    To control hardware like LEDs and sensors easily -> Option A
  4. Quick Check:

    Python on Raspberry Pi = hardware control [OK]
Hint: Think about what Raspberry Pi projects usually do [OK]
Common Mistakes:
  • Confusing Raspberry Pi with a general computer
  • Thinking Python on Pi is only for web development
  • Assuming it runs Windows apps natively
2. Which of the following is the correct way to import the GPIO library in Python on Raspberry Pi?
easy
A. import GPIO.RPi
B. import RPi.GPIO as GPIO
C. from GPIO import RPi
D. import raspberry.GPIO

Solution

  1. Step 1: Recall the standard import syntax for GPIO

    The correct import statement is import RPi.GPIO as GPIO to use GPIO functions easily.
  2. 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.
  3. Final Answer:

    import RPi.GPIO as GPIO -> Option B
  4. Quick Check:

    Standard GPIO import = import RPi.GPIO as GPIO [OK]
Hint: Remember the exact module name: RPi.GPIO [OK]
Common Mistakes:
  • Swapping module and package names incorrectly
  • Using wrong import syntax
  • Assuming 'GPIO' alone is a module
3. What will be the output of this Python code on Raspberry Pi?
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
GPIO.output(18, GPIO.HIGH)
print(GPIO.input(18))
medium
A. 0
B. HIGH
C. 1
D. Error

Solution

  1. Step 1: Understand GPIO setup and output

    Pin 18 is set as output and then set to HIGH, which means voltage is ON (1).
  2. Step 2: Check GPIO.input(18) after setting HIGH

    Reading the pin returns 1 because it was set HIGH.
  3. Final Answer:

    1 -> Option C
  4. Quick Check:

    GPIO.output HIGH means input reads 1 [OK]
Hint: HIGH means 1, LOW means 0 in GPIO [OK]
Common Mistakes:
  • Confusing HIGH with string 'HIGH'
  • Expecting 0 instead of 1 after setting HIGH
  • Thinking GPIO.input returns string values
4. Identify the error in this Raspberry Pi Python code snippet:
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT)
GPIO.output(12, HIGH)
medium
A. Missing GPIO prefix before HIGH
B. Wrong pin number for BOARD mode
C. GPIO.setmode should be BCM
D. GPIO.setup should be input, not output

Solution

  1. Step 1: Check usage of HIGH constant

    HIGH must be referenced as GPIO.HIGH, not just HIGH alone.
  2. Step 2: Verify other code parts

    Pin 12 is valid in BOARD mode, setmode can be BOARD, and output pin can be output.
  3. Final Answer:

    Missing GPIO prefix before HIGH -> Option A
  4. Quick Check:

    Use GPIO.HIGH, not just HIGH [OK]
Hint: Always prefix constants with GPIO. [OK]
Common Mistakes:
  • Using HIGH without GPIO prefix
  • Confusing BOARD and BCM pin numbering
  • Setting output pin as input by mistake
5. You want to blink an LED connected to pin 17 on Raspberry Pi using Python. Which code snippet correctly blinks the LED once with a 1-second delay?
hard
A. import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setup(17, GPIO.IN) GPIO.output(17, GPIO.HIGH) time.sleep(1) GPIO.output(17, GPIO.LOW)
B. import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(17, GPIO.OUT) GPIO.output(17, 1) time.sleep(1) GPIO.output(17, 0)
C. import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BOARD) GPIO.setup(17, GPIO.OUT) GPIO.output(17, GPIO.HIGH) time.sleep(1) GPIO.output(17, GPIO.LOW)
D. 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)

Solution

  1. Step 1: Check pin numbering and setup

    BCM mode, import time, GPIO.setup(17, GPIO.OUT) are all correct.
  2. 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).
  3. 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).
  4. 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 D
  5. Quick Check:

    Use BCM mode, output pin, and time.sleep for blinking [OK]
Hint: Use GPIO.HIGH/LOW with time.sleep for blinking [OK]
Common Mistakes:
  • Mixing BOARD and BCM pin numbers
  • Setting pin as input instead of output
  • Forgetting to import time module
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard