Booleans, Tuples, 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 [1]:
a = True
b = False

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

In [2]:
True and False
Out[2]:
False
In [3]:
True or False
Out[3]:
True
In [4]:
(False and (True or False)) or (False and True)
Out[4]:
False

Standard comparison operators can also produce booleans:

In [5]:
1 == 3
Out[5]:
False
In [6]:
1 != 3
Out[6]:
True
In [7]:
3 > 2
Out[7]:
True
In [8]:
3 <= 3.4
Out[8]:
True

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 [9]:
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 [10]:
t = (1, 2, 3)

They can contain heterogeneous types like lists:

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

and also support item access and slicing like lists:

In [12]:
t[1]
Out[12]:
2.3
In [13]:
t[:2]
Out[13]:
(1, 2.3)

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

In [14]:
t[1] = 2
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-14-9d97237db197> in <module>()
----> 1 t[1] = 2

TypeError: 'tuple' object does not support item assignment

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.

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 [15]:
d = {'a':1, 'b':2, 'c':3}

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

In [16]:
d['a']
Out[16]:
1
In [17]:
d['c']
Out[17]:
3

Values can also be set this way:

In [18]:
d['r'] = 2.2
In [19]:
print(d)
{'a': 1, 'b': 2, 'c': 3, 'r': 2.2}

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

In [20]:
e = {}
e['a_string'] = 3.3
e[3445] = 2.2
e[complex(2,1)] = 'value'
In [21]:
print(e)
{'a_string': 3.3, 3445: 2.2, (2+1j): 'value'}
In [22]:
e[3445]
Out[22]:
2.2

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

In [23]:
e[4]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-23-a79b29c56d88> in <module>()
----> 1 e[4]

KeyError: 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 [24]:
"a" in d
Out[24]:
True
In [25]:
"t" in d
Out[25]:
False

Note that this also works for lists:

In [26]:
3 in [1,2,3]
Out[26]:
True

Exercise 2

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

In [27]:
# your solution here