Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
π Ready to explore and create with us? Click [here] and join today!
Variablesο
Variables are containers used to store data values.
Creating a variable is very simple. You only need to name it and assign it a value. You donβt need to specify the data type of the variable when assigning it, because the variable is a reference, and it accesses objects of different data types through assignment.
Naming variables must follow the following rules:
Variable names can only contain numbers, letters, and underscores
The first character of the variable name must be a letter or underscore
Variable names are case sensitive
Create Variableο
There is no command for declaring variables in MicroPython. Variables are created when you assign a value to it for the first time. It does not need to use any specific type declaration, and you can even change the type after setting the variable.
x = 8 # x is of type int
x = "lily" # x is now of type str
print(x)
>>> %Run -c $EDITOR_CONTENT
lily
Castingο
If you want to specify the data type for the variable, you can do it by casting.
x = int(5) # y will be 5
y = str(5) # x will be '5'
z = float(5) # z will be 5.0
print(x,y,z)
>>> %Run -c $EDITOR_CONTENT
5 5 5.0
Get the Typeο
You can get the data type of a variable with the type() function.
x = 5
y = "hello"
z = 5.0
print(type(x),type(y),type(z))
>>> %Run -c $EDITOR_CONTENT
<class 'int'> <class 'str'> <class 'float'>
Single or Double Quotes?ο
In MicroPython, single quotes or double quotes can be used to define string variables.
x = "hello"
# is the same as
x = 'hello'
Case-Sensitiveο
Variable names are case-sensitive.
a = 5
A = "lily"
#A will not overwrite a
print(a, A)
>>> %Run -c $EDITOR_CONTENT
5 lily