Introduction to Programming with Python
Day 5

David Garcia, University of Konstanz

Quiz I

  1. Write the output of the following code:
short_list = [1, 2, '3', 4]

sum = 0
for i in short_list:
    try:
        sum += i
    except Exception as e:
        print("Invalid value")
print("Total sum:", sum)
  1. The following code outputs an error. Rewrite it so it calculates the mean of the values:
mean([1,2,3])

> Traceback (most recent call last):
>   File "<python-input-3>", line 1, in <module>
>     mean([1,2,3])
>     ^^^^
> NameError: name 'mean' is not defined
  1. Consider the code below. Briefly describe what it does:
with open('helloworld.csv', 'a') as f:
    line = f.readline()
    while line:
        print(line)
        line = f.readline()
  1. Out of the lines below, select the ones that return the first row in a pandas data frame:
  1. df.loc[0]
  2. df[0]
  3. df.get(0)
  1. Describe in your own words the output of the following code:
df.columns()
> Index(['age', 'workclass', 'fnlwgt', 'education', 'education-num',
>        'marital-status', 'occupation', 'relationship', 'race', 'sex',
>        'capital-gain', 'capital-loss', 'hours-per-week', 'native-country',
>        'class'],
>       dtype='object')
df.education-num.mean()
  1. Assuming that df contains information on individual people, describe in your own words what the following code does:
df.groupby("marital-status")["capital-gain"].mean()

Visual Studio Code for ICSS

https://code.visualstudio.com/

Install Visual Studio Code to start with ICSS next week

Feedback on Day 4