for x in range( y ):

for loop

Use: for loopvar in range( num ) :
.    statements 
Description: Runs the following block of statements for each value of seq
Parameters: loopvar Loop variable, can be any variable name. With each loop the loop variable take on the from the range() command.
range(num) Produces a sequence of numbers from 0 to num-1, and assigns it to loop variable loopvar each time round the loop. The range command can have more parameters to allow for different start and stop values. See documentation on the range( ) command
Returns: None
Related: rangeifif elsebreak, continue

The for statement is a looping command. It runs a block of code multiple multiple times.

Each time it runs through the loop, the loop variable loopvar is assigned a new value. The range command determines which value is assigned to the loop variable. If the range command has a single parameter num, then the loop variable will start at 0 and increment by 1 each time round the loop, until the loop variable is equal to the parameter num.  The range command accepts multiple parameters to control the start and stop, and step values for the loop variable. See the range( ) command page for more information.

The break statement can be used inside the block to break out of the loop, and the continue statement can be used to skip the rest of the current loop.


pindir(P1_0, OUT)              # Set port P1_0 as an output (this is the Launchpad red led)
for i in range(20):            # do 20 times
   out(P1_0, 0)                # set the output pin P1_0 (red led) to a 0 (off)
   wait(500)                   # wait for 500ms  (half a second)
   out(P1_0, 1)                # set the output pin P1_0 (red led) to a 1 (on)