KevsRobots Learning Platform
75% Percent Complete
By Kevin McAleer, 4 Minutes

Often we want to run a piece of code a specific number of times.
We can use the for statement to run a block of code a number of times:
for i in range(100):
print('i is:',i)
In this example, the loop will start by assign the variable i the value 0, and then run the block of code print('i is:',i) which will print out:
i is 0
Parameters / Arguments
Parameters, also known asargumentsare variables that act as inputs into a function. Weโll look at functions shortly. An example of an argument is the value100that we just used in in therangefunction. In some programming languages there is a difference between an argument (a literal value like100), and a parameter that is a variable (such asathat refers to another value). MicroPython doesnโt make such a distinction.
- Arguments can be values or variables
- Arguments are separated by commas
- Arguments are positional - the order the arguments are provided should match the order the function expects
- Arguments can be named, in which case the order doesnโt matter
- Arguments can be optional
The range function can take one, two or three parameters:
range(start ยซoptionalยป, end, steps ยซoptionalยป)
The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 until the end number is reached.
for i in range(100):
print(i)
If two parameters are provided, the range() function starts from start value and increments in steps of 1 until the end value is reached (its actually the end value -1).
for i in range(50,200):
print(i)
If three parameters are provided, the range() function starts from start value and increments in steps of the steps value until the end value is reached (again, the end value -1).
for i in range(50,200,10):
print(i)
if we want to run a block of code many times, based on whether or not a condition is True or False, we can use the while loop.
Youโll often see this to run blocks of code indefinitely on MicroControllers as we may want to run the code forever or until the device is reset.
while True:
print('hello')
The while loop is particularly useful in MicroPython as we may want to check the status of a sensor or externally connected device.
import machine
button = machine.Pin(0, machine.Pin.IN)
while button.value() == 0:
print('button is not pressed')
In this example we use the machine library to access the general purpose input/output pins (also known as GPIO pins). We assign pin 0 to the button variable and also define the pin as an input put using the machine.Pin.IN parameter.
The progam will keep printing the button is not pressed until button is pressed and returns a 1 value.
| Statement | Control Statement & Description |
|---|---|
Break |
Terminates the loop statement and transfers execution to the statement immediately following the loop. |
Continue |
Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating. |
Pass |
The pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute. |
You can use the arrows โ โ on your keyboard to navigate between lessons.
Comments