Note
Download here
Data structures¶
Any code you write in the workbook should be re-usable where possible, i.e. it should work if the values assigned to any variables are modified.
List indexing¶
Remember that we can have: - any number of items in a list, separated by commas - different data types and structures within our list
… and that the syntax for accessing elements is:
[position]
[start:stop]
[start:stop:step]
presidents = ['Ronald Reagan', 'George H. W. Bush', 'William J. Clinton', \
'George W. Bush', 'Barack Obama', 'Donald J. Trump'] #U.S. Presidents 1981-2020
Assign to a variable called ``number_presidents`` the number of entries in (i.e. ``len``gth of) ``presidents``:
number_presidents = len(presidents)
number_presidents
6
Assign to a variable called ``last_president`` the final president in ``presidents``:
last_president = presidents[-1]
last_president
'Donald J. Trump'
n = 4
Assign to a variable called ``nth_president``, the name of that president in the list:
the first entry in
presidents
would be returned whenn = 1
(remember that Python is zero-indexed)you can assume that
n
is always<= len(presidents)
nth_president = presidents[n-1]
nth_president
'George W. Bush'
String indexing¶
Remember that we can use indexing syntax in the same way on strings.
Assign to a variable called ``space_index``, the (zero-indexed)
position of the first space character (``’ ‘``) in ``nth_president``:
- use the .index()
method - you can assume all entries contain a
space
space_index = nth_president.index(' ')
space_index
6
Using ``nth_president`` and ``space_index``, assign to a variable called ``name_nth_president`` a string of the characters in ``nth_president`` up to (but not including) the first space:
name_nth_president = nth_president[:space_index]
name_nth_president
'George'
List methods¶
ratings = [7, 4, 8, 2, 9, 4, 3, 6, 8, 6, 7, 9, 2, 0, 3, 4, 1, 0, 2]
today = [5, 8, 2]
Use ``.extend()`` to add the ``today`` list to ``ratings``:
ratings.extend(today)
ratings
[7, 4, 8, 2, 9, 4, 3, 6, 8, 6, 7, 9, 2, 0, 3, 4, 1, 0, 2, 5, 8, 2]
Use ``.sort()`` and then ``.reverse()`` to order the elements in ``ratings`` with the highest numbers coming first:
ratings.sort()
ratings.reverse()
ratings
[9, 9, 8, 8, 8, 7, 7, 6, 6, 5, 4, 4, 4, 3, 3, 2, 2, 2, 2, 1, 0, 0]
Use ``.count()`` to assign the number of entries which equal ``0`` to ``zero_ratings``:
Information on the
.count()
method and other list methods can be found in the documentation
zero_ratings = ratings.count(0)
zero_ratings
2
Use ``.index()`` to assign to ``high_ratings`` how many ratings are ``> 7``:
Your list must have been sorted correctly in the earlier question
high_ratings = ratings.index(7)
high_ratings
5
Dictionaries¶
guest = {'name': 'Boris Johnson',
'country': 'United Kingdom',
'partner': {'name': 'Carrie Symonds',
'relationship': 'Engaged',
},
'age': 55,
'job': {'title': 'Prime Minister',
'years': 1,
},
}
Assign to a variable called ``guest_country`` the value in the ``guest`` dictionary associated with the ``country`` key:
guest_country = guest['country']
guest_country
'United Kingdom'
Assign to a variable called ``partner_name`` the value for ``name`` from the dictionary which is itself the entry for ``partner`` in the ``guest`` dictionary:
partner_name = guest['partner']['name']
partner_name
'Carrie Symonds'
Add a new key:value pair to ``guest``, with a key of ``’order’`` and a value of ``’Roast Beef’``:
guest['order'] = 'Roast Beef'
guest['order']
'Roast Beef'
Change the value for ``relationship`` in ``partner`` to ``’Married’``:
guest['partner']['relationship'] = 'Married'
guest['partner']
{'name': 'Carrie Symonds', 'relationship': 'Married'}
Use the ``.get()`` method to assign to a variable called ``guest_notes`` the value for the key ``’notes’`` in ``guest``, using ``’None found’`` as a fallback value if the key isn’t found:
guest_notes = guest.get('notes', 'None found')
guest_notes
'None found'
Tuples¶
Assign to ``guest_tag`` a tuple containing three elements in the
following order, with values from ``guest``: - name
- title
(from inside the nested job
dictionary) - country
guest_tag = (guest['name'], guest['job']['title'], guest['country'])
print(guest_tag[0] + '\n' + guest_tag[1] + '\n' + guest_tag[2])
Boris Johnson
Prime Minister
United Kingdom
Sets¶
hobbies = {'drumming', 'cycling', 'chess', 'travelling'}
Use the ``.discard()`` method to discard ``’travelling’`` from ``hobbies`` and then the ``.add()`` method to add ``’swimming’``:
hobbies.discard('travelling')
hobbies.add('swimming')
hobbies
{'chess', 'cycling', 'drumming', 'swimming'}
Built-in functions¶
Use the built-in function ``list()`` to create a list from ``hobbies``, assigning it to ``hobbies_list``:
hobbies_list = list(hobbies)
hobbies_list
['cycling', 'drumming', 'swimming', 'chess']
Use the built-in function ``sorted()``, and list indexing, to assign to ``first_hobby`` the item from ``hobbies_list`` which is first alphabetically:
first_hobby = sorted(hobbies_list)[0]
first_hobby
'chess'