User Guide

135
In Python, variables can hold any type of information. To extend
the example above, you could assign a string (a sequence of
characters) to the variable result, even when it currently contains
a number.
result = "Hello"
You can use variables wherever you would otherwise type a
value. For example, in math expressions such as x + 5 and x / y.
There are many types of data in Python, but these are some of
the most common:
Python is a “weakly typed” language. This means that you do not
have to declare what type of data a variable holds. For example,
if result contains “Hello” then result+5 is “Hello5”. However,
5+result causes an error when the program is run because Python
tries to use result as a number.
Type Description
Boolean Can either be True or False.
Number A number or fraction of any size. However, you
can specify the type (and size) of number when
you need to be specific.
String A sequence of characters (letters). For example,
“Hello, World!” is a string.
List An ordered sequence of items. Each item can be
any type of data (including other lists). See Using
Lists and Dictionaries.
ByteArray A list of bytes (small, 8-bit numbers). This is mainly
used for writing binary files (see section 6.8) and
when working with the Pi’s GPIO pins.
Tuple Like a list, except that the contents of tuples
cannot be changed while the program is running.
Set An unordered list where each item is unique.
Dictionary A collection of key-value pairs. See Using Lists and
Dictionaries on the following page.