Commit cc50d7f2 authored by Swicech's avatar Swicech
Browse files

upload assignment 6

parent 6397d8c2
%% Cell type:markdown id: tags:
[Back to Assignment 6](_Oving6.ipynb)
# Basics of Lists
**Learning goals:**
* Lists
**Starting Out with Python:**
* Kap. 7.1-7.3
In this assignment, we will get to know how lists work in Python by creating simple lists, modifying them, and printing the lists.
%% Cell type:markdown id: tags:
### Lists intro
%% Cell type:markdown id: tags:
Lists is a widely used data structure that makes it possible to save a multiple values to one variable. Lists can contain numbers, strings, booleans and even other lists.
To create an empty list named `empty_list` in Python, we write `empty_list = []`.
We can create a list of any length, here named `not_empty_list`, by writing `not_empty_list = [a, b, c, ......]`, where `a, b and c` are elements in the list.
%% Cell type:code id: tags:
``` python
# example of how to create list named my_list with five elements. One is an int and four are strings
my_list = [5, 'elements', 'in', 'the', 'list']
```
%% Cell type:markdown id: tags:
Every element in a list has an **index** which describes the position of the element in the list. This is used to find or change an element in a list.
Lists in python are indexed on zero. This means that the first element has index 0, the next has index 1 and so on.
By writing `list[index]`, we can find or change elements in a list.
From `my_list` in the code box above, we have `my_list[0]` is `5`, `my_list[1]` is `elements`, `my_list[2]` is `in` and so on.
Example: (**Feel free to change the code to see what happens**)
%% Cell type:code id: tags:
``` python
my_list = [5, 'elements', 'in', 'the', 'list']
print(my_list[0]) # print the element with index 0 in my_list: 5
print(my_list[1]) # print the element at index 1 in my_list: 'elements'
my_list[1] = 'stuffs' # change the element at index 1 to 'stuffs'
print(my_list[1]) # print the newly changed value at index 1.
```
%% Cell type:markdown id: tags:
### a)
%% Cell type:markdown id: tags:
Create a list named **my_first_list** with all the integers from 1 to 6
Example run:
```python
print(my_first_list)
[1, 2, 3, 4, 5, 6]
```
***Write code in block below.***
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
### List Length
%% Cell type:markdown id: tags:
In order to find the length of a list, you can use the built-in function `len()`. If we take `len(my_list)` we will get the value 5 if my_list is similar to the one we created in *Lists Intro* . The length equals the number of elements in the list. As lists are zero-indexed, the index of the last element will always be the length of the list - 1. For example, the last element in `my_list` is located at index 4. Another way of getting the last element of the list is by using index -1.
%% Cell type:markdown id: tags:
### b)
%% Cell type:markdown id: tags:
Print the last element in the list to the screen. **Remember that you can use the list from task a) without defining my_first_list again**
Example run:
```python
6
```
***Write code in the block below.***
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
### c)
%% Cell type:markdown id: tags:
c) Change the second to last element (`5`) to the string `'plus'`.
Example run:
```python
print(my_first_list)
[1, 2, 3, 4, 'plus', 6]
```
***Write the code in the block below.***
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
### List slicing
%% Cell type:markdown id: tags:
Let's say we have a list containing 5 elements.
%% Cell type:code id: tags:
``` python
letters = ['a', 'b', 'c', 'd', 'e']
```
%% Cell type:markdown id: tags:
We want a new list containing only the first three elements of `letters`. We want to name it `letters_abc` and it should contain `'a' , 'b'` and `'c'`. This can be done with **slicing**:
%% Cell type:code id: tags:
``` python
letters_abc = letters[:3] # [:3] is the slicing. It looks the same as finding one element, but with the added colon.
print(bokstaver_abc)
```
%% Cell type:markdown id: tags:
The general form of slicing is:
```python
new_list = old_list[start:end]
```
where `start` is the index of the *first element* you want in new_list, and `end` is the index+1 of the *last element* you want in new_list. The slicing is in the form *from* `start` and *until but not including* `end`. If you do not write anything before the colon(`[:end]`), as we did in the example above, python assumes that we want start to be from the beginning of the list. If you do not write anything after the colon (`[start:]`), python assumes that we want end to be the end of the list.
%% Cell type:markdown id: tags:
### d)
%% Cell type:markdown id: tags:
Create a new list named **my_second_list** that should include the three last elements of `my_first_list`.
Example
```python
print(my_second_list)
[4, 'plus', 6]```
***Write code in the block below.***
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
### e)
%% Cell type:markdown id: tags:
Print `my_second_list` and the text string `equals 10`to the console.
Example:
```python
[4, 'plus', 6] equals 10
```
***Write code in the block below***
%% Cell type:code id: tags:
``` python
```