Build Your Own AI Assistant Part 1 - Creating the Assistant
116820 Views
Is the new Raspberry Pi AI Kit better than Google Coral?
114678 Views
Control Arduino with Python using Firmata / PyFirmata
87081 Views
How to Map with LiDAR - using a Raspberry Pi Zero 2W, RPLidar and Rviz
57314 Views
Creating a Supercomputer with a Raspberry Pi 5 Cluster and Docker Swarm!
53588 Views
Node-Red Automation, MQTT, NodeMCU & MicroPython
52067 Views
SMARS Lab upgrade with PyCharm
Chicken Nugget Piano
Pi Tray - Mini-rack - Part II
Weather Station Display
Pi 10 Inch Mini-rack
Installing and Using DeepSeek-R1:1.5 on a Raspberry Pi with Docker
Mini-Rack 3D Design Tutorial
0h 20m
Using the Raspberry Pi Pico's Built-in Temperature Sensor
0h 24m
Getting Started with SQL
0h 32m
Introduction to the Linux Command Line on Raspberry Pi OS
0h 42m
How to install MicroPython
0h 8m
Wall Drawing Robot Tutorial
0h 22m
Learn Linux from the basics to advanced topics.
Learn how to use a Raspberry Pi Pico
Learn MicroPython the best language for MicroControllers
Learn Docker, the leading containerization platform. Docker is used to build, ship, and run applications in a consistent and reliable manner, making it a popular choice for DevOps and cloud-native development.
Learn how to build SMARS robots, starting with the 3D Printing the model, Designing SMARS and Programming SMARS
Learn how to build robots, starting with the basics, then move on to learning Python and MicroPython for microcontrollers, finally learn how to make things with Fusion 360.
Learn Python, the most popular programming language in the world. Python is used in many different areas, including Web Development, Data Science, Machine Learning, Robotics and more.
Learn how to create robots in 3D, using Fusion 360 and FreeCAD. The models can be printed out using a 3d printer and then assembled into a physical robot.
Learn how to create Databases in Python, with SQLite3 and Redis.
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 >