Python Shorts: 07 Dictionaries

You may have spotted a problem with lists and tuples. When we talked about functions we worried about the situation where we use the wrong order of parameters to a call. We can get the same problem with lists and tuples:

The statement above would set the age of person y to “James”, which would not be a good thing if the age value was stored in location 1. It would be lovely if we had a data structure that let us explicitly name the data elements in it. It turns out that we do, and it is called a dictionary:

z={"name":"Anne",
"age":21}

When you make a dictionary you give it a set of key – value pairs. This means that everything that is stored has a name – the key which is used to access it. The dictionary referred to by z has two keys, name and age. You can use these to get hold of the values:

n = z["name"]

The above statement gets the value of the name item in z. In this case the value of n would be “Anne”. Dictionaries are fun. We’ll have more about them in the next short.

Python Shorts 06: List Fun

Python lists are nice. We can make them very easily:

x = [1,2,3]

The order of the list is preserved. If we work our way through the elements they will always come out in the same order. We can use the len function to find out how many things are in a list:

l = len(x)

If we want to add something to a list we can use the append behaviour provided by lists:

x.append(5)

This will pop 5 on the end of the list. There’s no “prepend” function for putting something at the start of a list. Instead there’s an insert function that can be used to insert something at a particular position:

x.insert(0,0)

This statement would put the value 0 at the start of list x. You can use the remove behaviour to get rid of an element at a particular position in the list and pop, which removes an item and returns it.

Python Shorts 05: Lists

Making a list looks very like making a tuple:

y = ["Jim",25]

The statement above makes a list called y. Note the use of square brackets, which is different from how we make a tuple (which uses round brackets). Lists are a lot more flexible than tuples. For one thing we can change the content of their elements:

y[0]="James"

This statement changes the item at the start of y to “James”. This would work fine.  

If you want to, you can make a list bigger just by appending something

Python Shorts 04: Tuple of One

We’ve seen that we can create a tuple by using brackets to “squish” things together:

x = (“Rob”,21)

Tuples are very useful if you want to return multiple values from a function:

return (“Rob”,21)

This lets me return name and age values. However, sometimes you might want to return just one item in a tuple. You might think you do this:

x = (1)

However, this doesn’t work. Python thinks that the brackets are part of an expression, gets rid of them and puts the value 1 in x. If you want to make it clear to Python that you are making a tuple, put a “spare” comma on the end:

x = (1,)

This would “tupleise” the value.

Python Shorts 03: Tuples

Tuples should be called “lumps”. You can squish things together to make a lump. Tuples let you squish data together.

x = ("Jim",25)

The statement above creates a tuple called x which contains name and age information. Tuples can contain lots of data. The best way to unpick them is to use a cunning assignment:

(name,age)=x

This creates variables called name and age which hold “Jim” and 25 respectively.  Another way to get values out of a tuple is to index it:

name=x[0]

age=x[1]

The item at the start of the tuple has index value 0. Tuples are read only:

x[0]="James"

The statement above tries to change the in the x tuple. This will not work. Tuples are great for passing lumps of data around a program but no good for things you might want to modify. If you want to be able to change things in a lump of data you need use a list.

I find tuples very useful, but I do worry that the thing making the tuple and the thing using the tuple have to agree on the order of the elements. As we saw with name and age in function parameters this might lead to confusion. If you want both ends of a conversation to be absolutely clear about which data means what, you can use dictionaries to do that, but they are coming later.

Python Shorts 02: Default parameter values in functions and methods

We’ve seen how to make parameters clearer in calls, now let’s add some magic to the function itself. Sometimes we might only have the name of the person, can we make an add_person which will handle only one input? Yes, we can.

def add_person(name, age=3):
   print("Adding a person")

The code above creates an add_person function which has name and age parameters. The age parameter is set to 3 if it is missed out. Programmers call the value 3 the “default” value.

add_person(name="Simon")

This would add a person with the name Simon with age 3. You may be wondering why I picked 3 as the default. If we were writing a system to control access to fairground rides, we want to make it as safe as possible. Simon would only be able to go on rides which are suitable for 3-year-olds. If they want to go on the bigger rides, they must give their age.

add_person(name="Rob")

Note that if you want to use default values like these you must put the parameters with the default values at the end of the parameter list when the function is defined.

Get to sleep using your kitchens

If you are having bother getting to sleep, spend some time walking down memory lane thinking about kitchens. Works for me. Thinking about stuff that hasn’t happened yet gets the brain whizzing with plans and things to do. But thinking about stuff in the past is quite different (at least for me). So at the moment I’m working my way through the various kitchens we’ve had over the years. The memories are all good ones - like when we used to have a high chair and baby food all over the floor.

if you’re having trouble getting to sleep think about something that you liked doing in he past and see if it works for you..

Python Shorts 01: Named parameters in functions and methods

Welcome to the first “Python Sort”. I hope you find it useful. Let me know what you think in the comments…..

Python functions and methods can accept parameters that feed data into them. But once you have more than one parameter it can get tricky. For example, perhaps we have a function called add_person which adds a person to our system. The function accepts the name and the age of the person:

add_person("Jim",12)

This would add Jim to the system. But what happens if I get the call wrong?

add_person(12,"Fred")

The code and the name have been entered in the wrong order.  We are making someone with the name “21” and the age “Fred”. This will end in tears. The good news is that Python lets you specify the parameter name in the call:

add_person(name="Jim",age=21)

Now the destinations are explicitly defined and there is no chance of error and the code is much clearer.