Week 3
Types, Lists and Dictonaries
Types
The different kinds (or representations) of data which programming languages are able to store and manipulate are referred to as types, and we have already encountered a few of these.
Simple Types
The types we've encountered so far can be though of as the building blocks for more complicated types - in the same way you might think of a string as being built out of characters.
These "simple" types are shown in the following example:
Little tip:
Fun fact: Booleans are actually implemented as integers, with 1 = True and 0 = False:
Typecasting
Python, like many other languages, has the ability to transform one type of data to another - although it will only work if this conversion makes sense. For example, you could convert a string containing a number to an integer:
But, if you tried to convert a string containing letters or symbols to a number, python will get confused and throw an error (see extension material for more detail). Here's an example that results in an error:
For reference, here's a table of casts that can (and will) come in handy:
Type
Cast
Example Use case
Integer Number
int()
Do maths on user input
Floating Point Number
float()
Output numbers with decimal places
String
str()
Concatenate numbers with text when printing
Boolean
bool()
(Not often in practice - output formatting?)
Characters
chr()
Convert integer to ASCII character [string]
List
list()
Convert tuples / arrays / range() objects to lists
Lists
Lists are the first proper "data structure" that we will cover:
Lists allow us to store multiple values in one object
Lists can change size without us needing to re-declare them all the time
Lists can also be used for carrying out vector and matrix arithmetic (though in practice there are libraries like numpy which are far better for such things
Lists are created by using square brackets, [], and in python they can contain multiple different types of items:
Accessing list items
In python (and many other programming languages) we count from 0. So the first item in a list is always at the "zeroeth" position. For example:
Can be visualized as:
Then. If we wish to get items from a specific location in our list, we can do this by using square brackets next to the list's name: e.g. list[index]
A more involved example:
Append
To add items to the end of a list, we use the .append() function:
Pop
We can remove items from a list by using .pop(index) to remove the item at the specified index. (Note: the function also returns the value that it removes)
Reverse Iteration
If you want to iterate backwards through a list, the following syntax allows it.
This syntax is a special case of a general language feature called list slicing (see extensions for detail).
List Operations
We can add (concatenate) and multiply lists using the familiar + and * operators:
Dictionaries
Along with lists, dictionaries are among the most useful and commonly used data structures. Dictionaries allow us to store Key : Value pairs.
Whereas lists are created using [], we create dictionaries using {}
Adding Elements
To add elements to a dictionary we again use the square bracket syntax, except with a key rather than an index:
Keys in a dictionary must be unique. If a key already exists in the dictionary, then its corresponding value will be overridden when you assign it:
Pop
Similar to with lists, we can use .pop(key) on a dictionary to remove a key-value pair; usefully, the function also returns the value which it is removing, so we can print it or store it somewhere else.
Keys
They .keys() (member) function can be called on a dictionary to get a list of its keys:
Used alongside in (see extensions) this provides us with an easy way of checking whether we have already stored a particular key in our dictionary - useful for a username:password database!
Last updated