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
LidarBot
Snaszy NAS a 3D printed NAS for Raspberry Pi
Waveshare CM5 boards
The Best Arduino Robot for Beginners
SMARS Lab upgrade with PyCharm
Chicken Nugget Piano
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
85% Percent Complete
By Kevin McAleer, 3 Minutes
At some point you will want to make a block of code that you will use many times, perhaps with different values. This is not only essential to save repeating the same block of code over and over again, its a better technique to improve reliability and readability.
In MicroPython we use the def keyword to define the code that follows as a function.
def
Functions can also have parameters of there own.
Lets create a simple function that will print a message when its called:
def say_hello(): # This function prints the message hello print('hello') say_hello()
The example above defines (thats why its the def keyword) the function called say_hello, notice that we also need to provide the () brackets as functions need this, we also need to use the : colon symbol to tell MicroPython that the following block of code relates to this function.
say_hello
()
:
The function code is also indented.
We often want our function to take in an input variable so that we can use it in the function code. Lets extend our say_hello example so that it can include a name and age.
def say_hello(name, age): # This function prints the message hello print('hello', name, 'how does',age,'feel?') say_hello('Kev', 47)
In the example above we’ve added two parameters name and age. Our functions can use these parameters within its block of code, in this case we use it to print out the message hello Kev how does 47 feel?. Notice how Python accepts the two different types of variables - the name is a string (Str) of text, and age is an integer (a whole number with no decimal points).
name
age
hello Kev how does 47 feel?
Str
We can take this a step further and pass variables to the function and it will work just the same:
def say_hello(name, age): # This function prints the message hello print('hello', name, 'how does',age,'feel?') my_name = 'Kev' my_age = 47 say_hello(my_name, my_age)
Functions can also call other functions, though on a MicroController you may find that the device crashes if it runs out of memory, if you nest too many functions, but that will take quite a few levels.
Functions can do more than just run blocks of code, they can return values too. Lets make a new function that takes a value and adds 1 to it.
1
def add_one(number): number += 1 return number a = add_one(1) print(a)
In the example above we define the function add_one which has one parameter number. It then adds 1 to the number and returns the number to the caller.
add_one
number
return
We assign a to the result of running add_one(1), which should return the value 2. Therefore the variable a should contain the value 2. Run the code and try passing different values and changing the number += 1 to different values.
a
add_one(1)
2
number += 1
Challenge Write your own function that takes a value, multiplies it by 10, and then returns that value.
Write your own function that takes a value, multiplies it by 10, and then returns that value.
10
< Previous Next >