I am following the Freenove code (ref:- FNK0020 page 202 https://www.freenove.com/tutorial.html) to program a stepper motor. I have simplified the code a little, in any case as far as I can tell the final GPIO.LOW seems completely redundant, am I missing something?

motor_pins = (1, 2, 3, 4)
step = (0x01, 0x02, 0x04, 0x08)

for j in range(0, 4):
    for i in range(0, 4):
        GPIO.output(motor_pins[i], ((step[j] == 1<<i) and GPIO.HIGH or GPIO.LOW))

so the code is cycling through each pin, for each step cycle, and the output is condition and HIGH or LOW. For each cycle through the step, one of the pins returns HIGH and the rest LOW but I can't understand why the or LOW is needed at the end because the AND function will only return a True condition for one of the pins while the rest will be False therefore I think the or LOW is redundant, can someone clarify?

  • it does not take much to run a test to determine if GPIO.LOW is redundant .... don't fall into a trap of thinking that if you do not set an output to HIGH, then it is magically set to LOW
    – jsotola
    Commented Jan 27, 2021 at 23:40

1 Answer 1


This is a simple programming Question, not specific to the Pi.

It appears to be the python equivalent of the ?= conditional expression

There appears to be nothing unusual, although clever programming tricks like this just make code like this difficult to maintain.
It would be clearer if the author used parentheses.

NOTE In this particular case it is not strictly necessary (as GPIO.LOW == 0 == False and would be converted by the interpreter) but in general the extra term is needed.

Incidentally, if you replaced the GPIO.output with print the operation would be obvious. Understanding why takes a more in depth understanding of python expression evaluation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.