Example 1:

  • Save the 4 first elements of the list below to the variable new_list
  • Append value "8" to new_list.
  • print result

Result [0,1,2,3,"8"]


In [14]:
#initial list
initial_list = [0,1,2,3,4,5,6,7,8,9]
#Save the 4th first elements of the list below to new_list (tip: use [something:something])
new_list = 

#Append value "8" to new_list.
seomthing = something + something

#print result


[0, 1, 2, 3]
[0, 1, 2, 3, 8]

Example 2:

  • Find the unique numubers in this list
  • Sort the result
  • Print it

Result [0,1,2,3,4,5,6,7,8,9]


In [17]:
#initial list
initial_list = [6, 2, 8, 5, 6, 3, 9, 9, 8, 6, 7, 0, 7, 7, 0, 6, 4, 6, 6, 4, 3, 6, 0,
       1, 3, 6, 1, 9, 8, 9, 0, 6, 2, 1, 4, 4, 1, 7, 5, 3, 2, 2, 2, 0, 5, 8,
       2, 0, 8, 3, 7, 0, 9, 9, 6, 3, 8, 8, 7, 7, 2, 2, 6, 5, 5, 0, 8, 7, 6,
       9, 8, 0, 6, 2, 2, 8, 4, 6, 0, 3, 4, 8, 1, 4, 3, 9, 7, 9, 7, 0, 4, 7,
       1, 7, 9, 8, 8, 0, 7, 7]

#find unique words (tip: convert the list to a set)
initial_set = set(something)
#sort the result (tip: convert to a list before)
new_list = l
new_list.something()
print(new_list)
#print it


[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Example 3:

  • Add Friend3 to the dictionary
  • Change the number of Friend2

Result [0,1,2,3,4,5,6,7,8,9]


In [20]:
#initial_dict
this_is_a_dict = {"Javier": 63434234234, "Friend1": 4234423243, "Friend2": 424233345}
#change the phone number of friend2 (tip: use this_is_a_dict["Friend2"] = ...)


#add friend3


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-20-5761e368adc2> in <module>()
      5 
      6 #add friend3
----> 7 this_is_a_dict = this_is_a_dict + {"Friend3" : 378037383}
      8 this_is_a_dict

TypeError: unsupported operand type(s) for +: 'dict' and 'dict'

Example 4 (a bit harder):

  • Convert all the names starting with A to upercase, and the rest to lowercase

Result ["AARON","bart","AMANDA","rik"]


In [ ]:
initial_list = ["aArOn","bArR","AmAdDa","RIk"]

#create an empty list called new_list

#for each_element in initial_list: 
# check the first letter
# if == "A" make the element uppercase and append it to "new_list"

Example 5 (fill )

  • Make a csv file (separated by tabs) line by line with two fields: occurences of python, and year
  • Read a csv line by line, finding the number of times "python" in year 1990
  • Make a bar plot for the different newspapers

In [2]:
#make csv
with open("./data/example_python_year.csv",fill) as f:
    f.write("10\t\2000\n")
    f.write("20\t\2000\n")
    f.write("15\t\2000\n")
    f.write("50\t\2000\n")
    f.write("8\t\2000\n")
    f.write("30\t\2000\n")
    f.write("40\t\2000\n")    
    f.write("3\t\1990\n")
    f.write("4\t\1990\n")
    f.write("6\t\1990\n")
    f.write("1\t\1990\n")
    f.write("10\t\1990\n")
    f.write("20\t\1990\n")
    f.write("5\t\1990\n")

In [ ]:
import numpy as np

def keepyear(list_lines):
    times_1990 = []
    times_2000 = []
    for each_element in list_lines:
        times,year = each_element.split(fill)
        if year.fill == fill:
            times_1990.append(times)
        else:
            times_2000.append(times)
    return times_1990,times_2000
    
def plot(x,y):
    plt.bar(x,y)
    plt.show()
    
#read data
with open("./data/example_python_year.csv",fill) as f:
    list_lines = f.fill
    
#get lists and means
times_1990,times_2000 = fill
mean_1990 = np.mean(fill)
mean_2000 = np.mean(fill)

#plot
fill

In [8]:
#Make the two last steps with pandas
import pandas as pd
import pylab as plt

df = pd.read_csv("./data/example_python_year.csv",sep="\t",header=None,index_col=None)
df.columns = ["Times","Year"]

meanData = df.groupby("Year").mean().reset_index()
meanData.plot(x = "Year", y = "Times", kind="bar")
plt.show()

Example 6 (significance of results)

  • Use a non-parametric method

In [9]:
from scipy.stats import mannwhitneyu
print(mannwhitneyu(times_1990,times_2000))


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-9-2e0e20ac1159> in <module>()
      1 from scipy.stats import mannwhitneyu
----> 2 print(mannwhitneyu(times_1990,times_2000))

NameError: name 'times_1990' is not defined