Values & Variables Types

NUMBERS, TEXT, LISTS AND DICTIONARIES

By Kevin McAleer,    5 Minutes


Cover photo of a yellow typewritter

Data Types

Programs often need to work with numbers and text, adding and subtracting, asking questions and processing answers.

MicroPython is able to label different types of values so that it can make different commands available to us.

The different types of values are listed below:

List of MicroPython data types

Text type: str
Numeric types: int, float, complex
Lists and sequences: list, tuple
Dictionaries (mapping of key-value pairs) dict
Set types: set, frozenset
True/False (Boolean) type: bool
Binary types: bytes, bytearray, memoryview
None type: NoneType

List of common MicroPython data types with examples

Value Types Description Example
int A whole number, which can be positive or negative 1, 2, 3, 4, -500, 1000
float A decimal number, which can be positive or negative 3.14, -2.5, 1.66666
str A line of text 'hello world', 'my name is Kevin'
list A collection of words or numbers, or objects, (with no duplicates) [‘cat’,’dog’,’fish’,’gecko’], or [1,2,3,5,8,13,21]
dict A dictionary - a list, with pairs of values {‘name’:’Kevin’, ‘address’:’UK’, ‘age’:45}
tuple A collection of ordered items, can have duplicates ("fish",1,2,3,)
object An instance of a class (we’ll cover this in a later session) Object of 'Cat()' Class
byte A single 8 bit byte, which is a value between 0 and 255 255, '0xFF', '0b11111111'

Note The more complex data types have been excluded from this list. We’ll look at them in more detail in later modules.


When we assign a value to a variable, it is said to have the a type. e.g.

a = 1
b = 2.1
c = "hello"

In this example a contains the value 1 and is of type integer, b contains the value 2.1 and is of type float (because it has a decimal point), and c contains hello and is of type string because it contains a text wrapped inside some speech marks.


Implied Types

In other languages, such as C/C++ you will need to tell the compiler what kind of datatype a variable is before it is used. This is known as a strongly typed language; it helps the compiler allocate the correct amount of memory for the variable before it is used.

C++ Example
int a;   // tells the compiler that 'a' is an integer
float b; // tells the compiler that 'b' is a floating point number 
a = 1;
b = 2.3;

In Python and MicroPython you do not need to assign a type to a variable, Python is smart enough to figure this out by the value being assigned to it. This is known as weakly typed language. The trade off is that Python looks cleaner to read and doesn’t need data types to be assigned before they’re used, however this can lead to confusion if another type of value is expected or assigned to a variable of a differnet type.

MicroPython Example
a = 1   # python knows that 1 is an integer value so 'a' becomes an 'int' type
b = 2.3 # python knows that 2.3 is a floating point value so 'b' becomes a 'float' type

George Boole the Father of Boolean Logic

English mathematician and logician George Boole was the author of The Laws of Thought (1854) which documents the algebra of logic. He founded the idea that logic can be expressed in a series of symbols that show an output of True or False given a set of inputs. These can be expressed as Truth Tables, and is now commonly called Boolean Logic, which is what all modern computing is based upon.


Example Truth Table

‘AND’ Logic

input a input b output x
False False False
True False False
True True True

An AND logic gate has two inputs; a and b and one output x. The table shows each of the possible state the inputs and the outputs can be in. With AND gates both inputs need to be True for the output to be True. This is used a lot in graphics processing too.

We will look at logic operators in MicroPython in the Operators lesson.


< Previous Next >