---
# System prepended metadata

title: Untitled

---

## Archived answers to exercises
### Python for social science - day 2


Status: notebook open ready to go?
| Name                | Done                          |
|:------------------- |:----------------------------- |
| Barbara (example)   | :question:                    |
| Francesco (example) | :heavy_check_mark:            |
| Adri                | :heavy_check_mark:            |
| Anne Maaike         | :heavy_check_mark:            |
| Babette             | :heavy_check_mark:            |
| Carissa             | :heavy_check_mark:            |
| Cecilia             | :heavy_check_mark:            |
| Daniela             | :heavy_check_mark:            |
| Dominika            | :heavy_check_mark:            |
| Hekmat              | :heavy_check_mark:            |
| Ilaria              | :heavy_check_mark:            |
| Jeanette            | :heavy_check_mark:            |
| Kasimir             | :heavy_check_mark:            |
| Kevin               | :heavy_check_mark:            |
| Kyri                | :heavy_check_mark:            |
| Lianne              | :::::::::::::::               |
| Marilù              | :heavy_check_mark:            |
| Melisa              | :heavy_check_mark:            |
| Michael Q           | :heavy_check_mark:            |
| Philippine          |                               |
| Rael O.             |                               |
| Ranran Li           | :heavy_check_mark:            |
| Reshmi              | :heavy_check_mark:            |
| Roxane              | :heavy_check_mark:            |
| ruidong             | :heavy_check_mark:            |
| Samareen            | :heavy_check_mark:            |
| Signe               | :heavy_check_mark:            |
| Swee Chye           | :heavy_check_mark:            |
| Yunfeng             | :heavy_check_mark:            |
| Rael                | :heavy_check_mark:            |

### Exercise: Arithmetic and printing
Create a new cell and paste the code from the example into it:
```python=
print("a =", a, "and b =", b)
print(a + 2*b)
print(a + (2*b))
print((a + b)*2)
```
1. Remove all of the calls to the print function so you only have the expressions that were to be printed and run the code. What is returned?
2. Now remove all but the first line (with the 4 items in it) and run the cell again. How does this output differ from when we used the print function?

Bonus if you are done early:
* Practice assigning values to variables using as many different operators as you can think of.
* Create some expressions to be evaluated using parentheses to enforce the order of mathematical operations that you require


MD:
(1)16 
(2) ('a =', 3, 'and b =', 5) with print is: a = 3 and b = 5
    
Carissa: 1- 16 / 2-16, same, just references the code from above
Adri: 16, "a = 3 and b = 5"
Reshmi : 1. 16, 2- ('a =', 3, 'and b =', 5) with print it is a = 3 and b = 5
Roxane: 1. 16, 2. a = and b = are str, the values of a and b are int so you combine those

('a =', 5, 'and b =', 3) 

AM.1      16 
AM.2      ('a =', 3, 'and b =', 5) without print
          a = 3 and b = 5 with print

Signe: 
1. 16 ; only last value is returned
2. 'a =', 3, 'and b =', 5 ; returns a "tuple" 

Kyri: 1. 16, 2. ('a =', 3, 'and b =', 5)
Babette: 
1. 16
2. ('a =', 3, 'and b =', 5)

Kevin, 1: last line (16), 2: first line (tuple)

Michael: 
(1) 16
(2) ('a =', 3, 'and b =', 5)

Samareen:
1. 16
2. ('a =', 3, 'and b =', 5)

Hekmat
1. 16
2. ('a =', 3, 'and b =', 5)

Swee Chye:
1. a = 2; b = 3
2. a = 2 and b = 3; 
3. a + 2*b    # 8
4. a + (2*b)  # 8
5. (a + b)*2  # 10
6. c = 10
7. 

Daniela
1. 16
2.("a =", a, "and b =", b)


### Exercise: List indexing
1. Create a cell with the code below.
```python=
num_list = [4,5,6,11]
```
2. Select the 1st element from this list. (Your code should return `4`)
3. Select the last element from this list. (Your code should return `11`)
4. Make a new list with the second and fourth element in this list. (Your code should return `[5,11]`)

-Reshmi: 
1. num_list[0]
2. num_list[-1]
3. [num_list[1],num_list[-1]] 

-Roxane: num_list[0], num_list[-1], print(new_list = [num_list[1], num_list[3]])

Swee Chye:
1. num_list[0]
2. num_list[-1]
3. num_list[1::2]

Hekmat:
1. num_list[0]
2. num_list[-1]

Kyri
1. num_list[0]
2. num_list[-1]
3. mylist = [num_list[1],num_list[-1]]

Michael: 
(1) num_list[0]
(2) num_list[-1]
(3) new_list = num_list[1], num_list[3]

Samareen:
num_list[0]
num_list[-1]
mylist = [num_list[1],num_list[-1]]

Babette:
1. num_list[0]
2. num_list[-1]


Kevin:
```python=
num_list = [4,5,6,11]
print(num_list[0]) #  First element
print(num_list[-1]) #  Last element
newlist = list(num_list[i] for i in [1,3]) #List comprehension
print(newlist) #List with second and fourth element
```

Signe: 
```python=
2. num_list[0]
3. num_list[-1]
4. new_list = [num_list[1], num_list[3]]
```

### Exercise: if-statements

1. Start with the following code:
```python=
apple_cost = 0.5
bread_cost = 2.5
money = 2
```
2. Write an `if`-statement, replacing the ____ in the code below, that checks whether you can buy a bread with your money:
```python=
if ___
    print("I can buy bread!")
```

3. Add an `elif` to your statement, where you check if you can buy an apple instead.
```python=
elif _________
    print("At least I can buy an apple!")
```

4. Bonus: write a final `else` for the tragic scenario where you can buy neither bread nor apple.


Kevin:
```python=
apple_cost = 0.5
bread_cost = 2.5
money = 2

if money >= bread_cost:
    print("I can buy bread!")
elif money >= apple_cost:
    print("At least I can buy an apple!")
else:
    print("I should not have picked a career as PhD!")
```
Michael:
if money >= bread_cost:
    print('I can buy the bread.')
elif money < bread_cost and money >= apple_cost:
    print('I cannot buy the bread but I can buy an apple')
else:
    print("I don't make enough money to buy either")

Kasimir:
apple_cost = 0.5
bread_cost = 2.5
money = 2

if money >=bread_cost:
    print("I can buy bread!")
elif money >=apple_cost:
    print("At least I can buy an apple!")
else:
    print("I can neither buy an apple nor a bread")
    
    
Roxane:
if money >= bread_cost
    print("I can buy bread!")
elif money >= apple_cost
    print("At least I can buy an apple!")
else money < apple_cost
    print("I can't even buy an apple..")
  File "/var/folders/m1/zqjgzbnn3bv4rxvw98hj1nr40000gq/T/ipykernel_76254/3243460098.py", line 1
    if (money >= bread_cost)
                            ^
SyntaxError: invalid syntax
Reshmi:
1. if (money>=bread_cost):
    print("I can buy bread!")
2. elif (money>=apple_cost):
    print("At least I can buy an apple!")
3. else:
    print("I can buy neither bread nor apple")


Swee Chye:
```python=
apple_cost = 0.5
bread_cost = 2.5
money = 2

if money >= bread_cost:
    print("I can buy bread!")
elif money >= apple_cost:
    print("At least I can buy an apple!")
else:
    print("I don't have enough money to buy anything!!!")
```

    
    
Adri:
```python=
if money >= bread_cost:
    print("I can buy", round(money/bread_cost), "bread!")
elif money >= apple_cost:
    print("I can buy", round(money/apple_cost), "apples!")
else:
    print("I am too broke to buy anything")
 ```   
    
Samareen:
```python=
1. if money >= bread_cost:
    print ('I can buy bread!')
2. elif money < bread_cost and money >= apple_cost:
    print('at least I can buy an apple :(')
3. else:
    print('guess I will just starve')
```

Hekmat:
```python=
2. if money >= bread_cost:
    print("I can buy bread!")
3. elif money >= apple_cost:
    print("At least I can buy an apple!")
4. else:
    print("I cannot buy anything")
```


    
MD: 
```python=
if money >= bread_cost: 
    print("I can buy bread!")
elif money >= apple_cost: 
    print("At least I can buy an apple!")
else: 
    print("I cannot buy either bread or an apple")
```
Babette
if money >= bread_cost:
    print("I can buy bread!")
elif money >= apple_cost:
    print("At least I can buy an apple!")
else:
    print("I cannot buy anything")

Kyri
```python=
1. if money >= bread_cost:
    print("I can buy bread")
2. elif money >= apple_cost:
    print("At least I can buy and apple!")
3. else:
    print("not enough money for bread or apples")
```    

Signe: 
```python=
apple_cost = 0.5
bread_cost = 2.5
money = 2

if money >= bread_cost:
    print("I can buy bread!")
elif money >= apple_cost:
    print("At least I can buy an apple!")
else: print("I need more money...")
```

Daniela:
if money >= bread_cost:
    print("I can buy bread!")
elif money >= apple_cost :
    print("at least i can buy an apple")
else :
    print ("won't have dinner tonight")
    
Lianne:
if money >= bread_cost:
    print("I can buy bread!")
elif money >= apple_cost:
    print("At least I can buy an apple!")
else:
    print("I cannot buy anything")
    
    
### Exercise: for-loop
Suppose that we have a string containing a set of 4 different types of values separated by , like this:
```python=
variablelist = "01/01/2010,34.5,Yellow,True"
```
Research the `split()` method and write a for-loop that prints each of the 4 components of `variablelist`

--Reshmi---
for i in variablelist.split(","):
    print(i)
    
    
--Carissa - 
variablelist = "01/01/2010, 34.5, Yellow, True"
print(variablelist)
print(variablelist)
01/01/2010, 34.5, Yellow, True
elist.split()
variablelist.split()
['01/01/2010,', '34.5,', 'Yellow,', 'True']

--Kasimir
variablelist_sep = variablelist.split(sep = ",", maxsplit=4)

print(variablelist_sep)
for i in variablelist_sep:
    print(i)
    
    
--Kevin
```pyton=
for i in variablelist.split(sep=","):
    print(i)
```
--Roxane:
```python=
variablelist = "01/01/2010,34.5,Yellow,True"
print (variablelist)
variablelist.split()
for i in variablelist.split(','):
    print(i)
```
--Michael:
```python=
items = variablelist.split(',')
for item in items:
    print(item)
```
--Adri:
```python=
for numberitem in variablelist.split(","):
    print(numberitem)
```

--Samareen:
```python=
variablelist = "01/01/2010,34.5,Yellow,True"
variablelist = variablelist.split(',')
for i in variablelist:
    print(i)
```

Babette
```python =
variablelist = "01/01/2010,34.5,Yellow,True"
variables=variablelist.split(',')
print(variables)
for i in variables:
    print(i)
```

MD: 
```python =
items=variablelist.split(',')
for i in items: 
    print("my item is", i)
```

Kyri
``` python=
variablelist="01/01/2010,34.5,5,Yellow,True"
items = variablelist.split(',')
for i in items:
    print (i)


```


Swee Chye:
```python=
variablelist = "01/01/2010,34.5,Yellow,True"

for i in variablelist.split(","):
    print(i)
```

 Signe
```python=
variablelist = "01/01/2010,34.5,Yellow,True"
    
items = variablelist.split(sep = ",")
for i in items:
    print(i)   

```