0
0
Raspberry Piprogramming~10 mins

LED class and methods in Raspberry Pi - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - LED class and methods
Create LED object
Call turn_on() method
LED state set to ON
Call turn_off() method
LED state set to OFF
Call toggle() method
LED state switched ON/OFF
End
This flow shows creating an LED object and using its methods to turn it on, off, or toggle its state.
Execution Sample
Raspberry Pi
class LED:
    def __init__(self):
        self.state = False
    def turn_on(self):
        self.state = True
    def turn_off(self):
        self.state = False
    def toggle(self):
        self.state = not self.state
This code defines an LED class with methods to turn it on, off, and toggle its state.
Execution Table
StepActionMethod CalledLED State BeforeLED State After
1Create LED objectN/AN/AFalse (OFF)
2Turn LED onturn_on()False (OFF)True (ON)
3Turn LED offturn_off()True (ON)False (OFF)
4Toggle LEDtoggle()False (OFF)True (ON)
5Toggle LED againtoggle()True (ON)False (OFF)
💡 All method calls completed, LED state updated accordingly.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
led.stateN/AFalseTrueFalseTrueFalse
Key Moments - 3 Insights
Why does the LED state change from False to True after calling turn_on()?
Because the turn_on() method sets the led.state variable to True, as shown in execution_table step 2.
What happens when toggle() is called if the LED is currently ON?
The toggle() method switches the state from True to False by using 'not self.state', as seen in execution_table step 5.
Why is the initial LED state False when the object is created?
The __init__ method sets self.state to False, meaning the LED starts OFF, shown in execution_table step 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is led.state after step 3?
AFalse (OFF)
BTrue (ON)
CNone
DError
💡 Hint
Check the 'LED State After' column for step 3 in the execution_table.
At which step does the LED state first become True (ON)?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'LED State After' column to find when the state changes to True.
If we call toggle() twice starting from OFF, what will be the final state?
ATrue (ON)
BFalse (OFF)
CError
DUndefined
💡 Hint
Refer to steps 4 and 5 in the execution_table where toggle() is called twice.
Concept Snapshot
LED class basics:
- __init__ sets initial state (False = OFF)
- turn_on() sets state to True (ON)
- turn_off() sets state to False (OFF)
- toggle() switches state True<->False
Use methods to control LED state simply.
Full Transcript
We start by creating an LED object, which sets its state to OFF (False). Calling turn_on() changes the state to ON (True). Calling turn_off() sets it back to OFF. The toggle() method flips the state from ON to OFF or OFF to ON. Each method updates the led.state variable accordingly. This simple class helps control an LED's on/off state with clear methods.