Booleans, Tuples, Sets, and Dictionaries

Booleans

A boolean is one of the simplest Python types, and it can have two values: True and False (with uppercase T and F):

In [ ]:
a = True
b = False

Booleans can be combined with logical operators to give other booleans:

In [ ]:
True and False
In [ ]:
True or False
In [ ]:
(False and (True or False)) or (False and True)

Standard comparison operators can also produce booleans:

In [ ]:
1 == 3
In [ ]:
1 != 3
In [ ]:
3 > 2
In [ ]:
3 <= 3.4

Exercise 1

Write an expression that returns True if x is strictly greater than 3.4 and smaller or equal to 6.6, or if it is 2, and try changing x to see if it works:

In [ ]:
x = 3.7

# your solution here

Tuples

Tuples are, like lists, a type of sequence, but they use round parentheses rather than square brackets:

In [ ]:
t = (1, 2, 3)

They can contain heterogeneous types like lists:

In [ ]:
t = (1, 2.3, 'spam')

and also support item access and slicing like lists:

In [ ]:
t[1]
In [ ]:
t[:2]

The main difference is that they are immutable, like strings:

In [ ]:
t[1] = 2

We will not go into the details right now of why this is useful, but you should know that these exist as you may encounter them in examples.

Sets

The Python data type of a set is an unordered collection of unique objects that supports the operations on sets known from mathematics. Emphasizing again, each object -- element -- in a set appears only once.

Sets are defined via the set() function, or with curly braces {}:

In [ ]:
x = set('abcde')      # old syntax
y = {1,2,3,'a','b'}
print(x)
print(y)              # there is no order of the objects in a set

Difference

In [ ]:
x - y

Union

In [ ]:
x | y

Intersection

In [ ]:
x & y

Superset, subset

In [ ]:
x > y, x < y

Symmetric difference (XOR), all elements not common to both sets

In [ ]:
print(x ^ y)
print((x | y) - (x & y))

Testing for membership of an element in a set

In [ ]:
'c' in x

Associated with the set object class are a number of methods to .add, or .remove elements (and more). Python sets have the limitation that the objects in a set need to be of immutable type. For instance, lists cannot be elements of a set for this reason.

What are sets good for? They can, e.g., be used to filter for duplicates (though elements may be reordered in the process):

In [ ]:
L = [1,2,3,2,4,5]
print('A list                          ', L)
print('Set of the elements of the list ', set(L))
L = list(set(L))
print('Set converted back to list      ', L)

Dictionaries

One of the data types that we have not talked about yet is called dictionaries (dict). If you think about what a 'real' dictionary is, it is a list of words, and for each word is a definition. Similarly, in Python, we can assign definitions (or 'values'), to words (or 'keywords').

Dictionaries are defined using curly brackets {}:

In [ ]:
d = {'a':1, 'b':2, 'c':3}

Items are accessed using square brackets and the 'key':

In [ ]:
d['a']
In [ ]:
d['c']

Values can also be set this way:

In [ ]:
d['r'] = 2.2
In [ ]:
print(d)

The keywords don't have to be strings, they can be many (but not all) Python objects:

In [ ]:
e = {}
e['a_string'] = 3.3
e[3445] = 2.2
e[complex(2,1)] = 'value'
In [ ]:
print(e)
In [ ]:
e[3445]

If you try and access an element that does not exist, you will get a KeyError:

In [ ]:
e[4]

Also, note that dictionaries do not know about order, so there is no 'first' or 'last' element.

It is easy to check if a specific key is in a dictionary, using the in operator:

In [ ]:
"a" in d
In [ ]:
"t" in d

Note that this also works for lists:

In [ ]:
3 in [1,2,3]

Exercise 2

Try making a dictionary to translate a few English words into German and try using it!

In [ ]:
# your solution here