108640 Views
83860 Views
59555 Views
48723 Views
48311 Views
47806 Views
KevsArcade
C2Pi-O Laser cut Camera holder
Build a laser-cut robot
Robots and Lasers
Arduino Plug and Make Kit Review
Pi to Pico W Bluetooth Communication
Getting Started with SQL
Introduction to the Linux Command Line on Raspberry Pi OS
How to install MicroPython
Wall Drawing Robot Tutorial
BrachioGraph Tutorial
Intermediate level MicroPython
KevsRobots Learning Platform
70% Percent Complete
By Kevin McAleer, 5 Minutes
So far, our programs have been simple lists of tasks that MicroPython carries out for us. But what if we want the program to do different things depending on the value of a variable?
We can test or check the value of our variables using the if statement.
if
Using the if statement is simple, we need a condition to check, and then an action to follow if that condition is True.
True
We also need to do something different in our code, we need to indent the code under the if statement to tell MicroPython that this block of code is only to be run if the condition is True.
if name == 'Kevin': print('Hi Boss')
Notice that there are two equals signs ==. This is because we use one equal sign = to assign values, but two equal signs == for comparing two value.
==
=
We an also compare values to check if they are the same:
a = 1 b = 1 if a == b: print('They are the same')
In the example above we assign the value 1 to the variable named a, and then we assign the value 1 to the variable b. We then compare the two using the if a == b condition.
1
a
b
if a == b
Colons Notice the : colon after the condtional statement, this tells MicroPython that what follows is a block of code. We can write the code on the same line if its just one line, otherwise we need to indent the block of code like this:
Notice the : colon after the condtional statement, this tells MicroPython that what follows is a block of code. We can write the code on the same line if its just one line, otherwise we need to indent the block of code like this:
:
if a == b: print('They are the same') print('a has the value', a, 'and b has the value',b)
This will output:
>>> They are the same a has the value 1 and b has the value 1 >>>
So far we’ve checked for a condition and if that condition is True then we’ve run a block of code, but what if we want to run another block of code if the condition is False. We could do this:
False
if a == 1: print('a is 1') if a != 1: print('a is not 1')
In the code above we check if a is equal to 1 (if a == 1) and if it is we print the message a is q, we then do another check to see if a is not equal to 1 (if a != 1), and then print the message a is not 1.
if a == 1
a is q
if a != 1
a is not 1
However, there is a quicker way to do the second test without another if statement. We can use the else keyword. else means run the block below if the first condition was False.
else
if a == 1: print('a is 1') else: print('a is not 1')
Code Blocks & Indentation Note that Python and MicroPython recognise blocks of code by their indentation, which is defined by white space (typically 4 spaces per indented block). Other languages like JavaScript and C use curly braces { } to define blocks of code. Python Example if a == 1: # this is a code block, notice its indented by 4 spaces print('a = ', a) C++ Example #include <iostream> void main(){ int a; if (a ==1) { std::cout << "a = " << a; } } You can see in the C++ example blocks of code are defined by the curly braces {}, and these are nested. Curly braces make the code more cluttered and also mean to have to remember to close the curly braces otherwise you’ll have errors.
Note that Python and MicroPython recognise blocks of code by their indentation, which is defined by white space (typically 4 spaces per indented block). Other languages like JavaScript and C use curly braces { } to define blocks of code.
4
JavaScript
C
{ }
if a == 1: # this is a code block, notice its indented by 4 spaces print('a = ', a)
#include <iostream> void main(){ int a; if (a ==1) { std::cout << "a = " << a; } }
You can see in the C++ example blocks of code are defined by the curly braces {}, and these are nested. Curly braces make the code more cluttered and also mean to have to remember to close the curly braces otherwise you’ll have errors.
{}
We’re not done yet; if statements have one more trick, it can run another alternative block of code after checking and running the first block of code, or before running the else statement.
Elif, a shortening of “else if”, sits between the first if statement test, and the final else statement. It can provide for additional checks each with their own blocks of code.
Only one of the blocks of code is run using the if, elif and else statement.
elif
if a == 1: print('a is 1') elif a <= 1: print('a is less than 1') else: print('a is not 1')
Assumed True When checking to see if a value is True or False we can could write this: a = True if a == True: print('a is True') However we can shorten this: a = True if a: print('a is True') Note 1 is also True and 0 is also False
When checking to see if a value is True or False we can could write this:
a = True if a == True: print('a is True')
However we can shorten this:
a = True if a: print('a is True')
Note 1 is also True and 0 is also False
0
< Previous Next >