python forloop accessing GPIO pins
Question: I'm new to python raspberry pi. Trying to shorten my code and use for loops
for repetitive parts in my code like changing all pins to high and low.

Trying to use a for loop to access pins. Is this even possible?

    
    
    import RPi.GPIO as GPIO
    from time import sleep
    
    
    R1=22
    R2=10
    R3=9
    R4=11
    
    
    GPIO.setup(R1, GPIO.OUT)
    GPIO.setup(R2, GPIO.OUT)
    GPIO.setup(R3, GPIO.OUT)
    GPIO.setup(R4, GPIO.OUT)
    
    for x in range(1, 5):
        print "We're on time %d" % (x)
        GPIO.output(R + %d % (x), GPIO.HIGH)
        sleep(1)
        GPIO.output(R + %d % (x), GPIO.LOW)
        sleep(1)
    
    GPIO.cleanup()
    


Answer: That sort of thing is possible, but often nicer to use a `list` or `dict`:

    
    
    pins = [22, 10, 9, 11]
    
    for pin in pins:
        GPIO.setup(pin, GPIO.OUT)
    
    for x in range(1, 5):
        GPIO.output(pins[x], GPIO.HIGH)
        sleep(1)
        GPIO.output(pins[x], GPIO.LOW)
        sleep(1)
    

