SunFounder ESP32 Starter Kit
Setting the Specific Data Type
If you want to specify the data type, you can use the following constructor functions:
Example Date Type
x = int(20) int
x = float(20.5) float
x = complex(1j) complex
x = str(“Hello World”) str
x = list((“apple”, “banana”, “cherry”)) list
x = tuple((“apple”, “banana”, “cherry”)) tuple
x = range(6) range
x = dict(name=”John”, age=36) dict
x = set((“apple”, “banana”, “cherry”)) set
x = frozenset((“apple”, “banana”, “cherry”)) frozenset
x = bool(5) bool
x = bytes(5) bytes
x = bytearray(5) bytearray
x = memoryview(bytes(5)) memoryview
You can print some of them to see the result.
a = float(20.5)
b = list(("apple", "banana", "cherry"))
c = bool(5)
print(a)
print(b)
print(c)
>>> %Run -c $EDITOR_CONTENT
20.5
['apple', 'banana', 'cherry']
True
>>>
Type Conversion
You can convert from one type to another with the int(), float(), and complex() methods: Casting in python is therefore
done using constructor functions:
• int() - constructs an integer number from an integer literal, a float literal (by removing all decimals), or a string
literal (providing the string represents a whole number)
• float() - constructs a float number from an integer literal, a float literal or a string literal (providing the string
represents a float or an integer)
• str() - constructs a string from a wide variety of data types, including strings, integer literals and float literals
a = float("5")
b = int(3.7)
c = str(6.0)
(continues on next page)
3.6. 1.6 (Optional) MicroPython Basic Syntax 299