---
# System prepended metadata

title: Objects and Methods

---

# Objects and Methods

(under construction)

([home](https://github.com/alexhkurz/introduction-to-programming/blob/master/README.md) ... [previous](https://hackmd.io/@alexhkurz/rkXh-CUGP) ... [next](https://hackmd.io/@alexhkurz/HJo0RmvGv) ... )

(this write up was worth 3 or 4 sessions)

Objects come with methods that can be used to create and update objects. So far our main example of an object is a list. [Section 5.1](https://docs.python.org/3/tutorial/datastructures.html#more-on-lists) of the Python tutorial explains the built-in methods that come with lists.

## Built-in methods for lists

**Activity:** Let us write some Python programs that use these methods. 

For example,

	>>> l = []
	>>> l.append(1)
	>>> l.insert(0,2)
	>>> l.insert(0,3)
	>>> l
	[3, 2, 1]
	>>> l.reverse()
    >>> l
    [1,2,3]
    
Continue this dialogue by experimenting with all methods in [Section 5.1](https://docs.python.org/3/tutorial/datastructures.html#more-on-lists). What is the meaning of the first argument of `insert`? What happens if the first argument of `insert` is bigger than the length of the list? What is the difference between `append` and `extend`? (What Python calls `extend` is called `append` in other languages ... note that the argument of `extend` should be another list.) Which of the methods return sth and which do not? What are Python's answers to the following questions?

    >>> l = [1,2,3]
    >>> l.pop() + 4
    
and

    >>> l = [1,2,3]
    >>> l.reverse().sort()
    
What happens in the following case?

    >>> a = 2
    >>> l = [a]
    >>> l
    
How can you make a list that contains the letter `b`? Why does the following lead to an error message?

    >>> l = [b]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'b' is not defined
    
Do you remember how to force Python to think of symbols as strings and not as variable names?
    
## Using methods to write your own functions

### Insertion sort

Now let us use `insert` to implement the sorting algorithm we discussed in [this section](https://hackmd.io/@alexhkurz/r1erdGSlP).

Recall that from an aribtrary `list` we want to build a new `sorted_list` by successively inserting the elements of `list` into `sorted_list`.

So we start with

    def insertion_sort(list):
    
which gives our sorting function the name `insertion_sort` and names the list that is to be sorted as `list`. Next we create a new empty list (note that this line has been indented to the right)

        sorted_list = []
    
(It is worth observing that the empty list is always sorted.)

        <your code goes here>
    
At the end the [local variable](https://hackmd.io/@alexhkurz/Hkc7HoSC8) `sorted_list` should contain the same elements as `list` but sorted. So we can return it:

        return sorted_list
        
Hint: Before attempting to write your code in Python, write it down in English with pen and paper. Then translate English into Python. 

Do not forget to test your program with various lists.

### Matrix multiplication

In the previous exercise we have seen an algorithm that contains two nested for-loops. Maybe the best known example of nested for-loops is matrix multiplication. 

I turned this material now in an [Excursion on Linear Algebra]().

### Bubble sort

There are also sorting algorithms that do not require to create a new list. Instead, they sort in place, by swapping elements so that they eventually end up sorted. The simplest example is [bubble sort](https://en.wikipedia.org/wiki/Bubble_sort).

Implement bubble sort in Python.
