Let's think about it this way for a moment
Wire a button to a GPIO pin and your code can detect its presses and releases — with gpiozero's Button class you can check it two ways: .is_pressed (a property, true/false) or .when_pressed (a callback function, the event-driven pattern). The event-driven pattern (when_pressed) is efficient because it doesn't need to constantly check (poll) inside a loop — the function fires automatically the moment the button is pressed.
Let's connect it to a real scenario
Combine a Button and an LED and you get the classic beginner circuit: 'light the LED while the button is pressed.' Just assign the function references directly — button.when_pressed = led.on, button.when_released = led.off — and you're done (this is one of gpiozero's nice syntax conveniences). Analog sensors like temperature/humidity sensors (DHT11/DHT22), though, need a dedicated library (Adafruit_DHT) and are more involved than plain GPIO digital read/write.
Let's look at it together
from gpiozero import LED, Button
from signal import pause
led = LED(17)
button = Button(2) # BCM pin 2
button.when_pressed = led.on
button.when_released = led.off
pause() # keep script running to listen for button eventsThe LED stays lit while the button is held down, and turns off as soon as you release it.5-minute try-it
Wire up a Button + LED circuit (if you have one) and write the code yourself using when_pressed/when_released events.
A quick word of caution
Watch out for the difference between when_pressed = led.on() (calls the function immediately) and when_pressed = led.on (a function reference) — parentheses or no parentheses is one of the classic beginner mistakes in Python.