Getting started with Google Colab

In this course we will use Google Colab for the practical sessions. Colab runs in your browser, so there is nothing to install. You only need a Google account.

If you have never used it, watch this short introduction before the first lab (about 12 minutes): https://www.youtube.com/watch?v=tQBDr5fdtXo

Together, we will see the structure of a Google Colab notebook. Click here

How to work with the course notebooks:

  1. Open the notebook from the link provided on the course page.
  2. Immediately go to File > Save a copy in Drive. Changes made to the original are not saved, so always work on your own copy.
  3. Run a code cell with Shift + Enter.

Reviewing Python

Access this Google Colab notebook at this link to do some exercises on python


Other Python Exercises

Exercise 1

Write a simple function that, given a list of numbers, outputs a histogram based on these numbers, using asterisks to draw it.

For example, given the list [3, 7, 9, 5], the function should produce this sequence:

***

*******

*********

*****

Exercise 2

In Sweden, children often play using a somewhat peculiar language called rövarspråket: it consists of doubling every consonant in a word and inserting an “o” in the middle. For example, the word ‘mangiare’ (to eat) becomes “momanongogiarore.”

Write a function that can translate a word or phrase passed as input into rövarspråket. After translating a phrase, the program should ask the user if they want to translate another one, and if so, it should wait for the user to enter a new word.

Exercise 3

You are given a list that contains integers. You need to write a function that decrement each element of the list by 1 and return the list.

Exercise 4

The task of finding the sum of all items in a dictionary in Python involves calculating the total of all values stored in a dictionary. For example,* given a dictionary {‘a’: 100, ‘b’: 200, ‘c’: 300}, the sum of values would be 100 + 200 + 300 = 600.

Exercise 5: CSV files in Python

In this tutorial, we see how to use Python to manage CSV files following an online tutorial.

  1. Copy and save this snippet of code in a .csv file:
Title,Release Date,Director
And Now For Something Completely Different,1971,Ian MacNaughton
Monty Python And The Holy Grail,1975,Terry Gilliam and Terry Jones
Monty Python's Life Of Brian,1979,Terry Jones
Monty Python Live At The Hollywood Bowl,1982,Terry Hughes
Monty Python's The Meaning Of Life,1983,Terry Jones
  1. Run the following examples on this newly saved .csv file (taken from this tutorial) :

    a. Run this example reading the previous .csv file

import csv
 
filename = "film.csv"
 
fields = []
rows = []
 
 
with open(filename, 'r') as csvfile:
    csvreader = csv.reader(csvfile)
    fields = next(csvreader)
    for row in csvreader:
        rows.append(row)
    print("Total no. of rows: %d" % (csvreader.line_num))
 
print('Field names are:' + ', '.join(field for field in fields))
 
print('\nFirst 5 rows are:\n')
for row in rows[:5]:
    for col in row:
        print("%10s" % col, end=" "),
    print('\n')

b. Run this other example reading the same .csv file

import csv
 
with open('film.csv', mode='r') as file:
    csv_reader = csv.DictReader(file)
    data_list = []
    for row in csv_reader:
        data_list.append(row)
 
for data in data_list:
    print(data)
 

c. Run this example writing a .csv file

import csv
fields = ['Name', 'Branch', 'Year', 'CGPA']
rows = [['Nikhil', 'COE', '2', '9.0'],
        ['Sanchit', 'COE', '2', '9.1'],
        ['Aditya', 'IT', '2', '9.3'],
        ['Sagar', 'SE', '1', '9.5'],
        ['Prateek', 'MCE', '3', '7.8'],
        ['Sahil', 'EP', '2', '9.1']]
 
filename = "university_records.csv"
 
with open(filename, 'w') as csvfile:
    csvwriter = csv.writer(csvfile)
    csvwriter.writerow(fields)
    csvwriter.writerows(rows)
 

d. Create a Python script that reads from the previous .csv file and create a new one with the content below and create a third .csv file with the content of both the files (choose on the reading/writing technique from this tutorial)

Title,Release Date,Director
Monty Python's Flying Circus - Series 1,1969-1970,Various Directors
Monty Python's Flying Circus - Series 2,1970-1971,Various Directors
Monty Python's Flying Circus - Series 3,1972-1973,Various Directors
Monty Python's Flying Circus - Series 4,1974,Various Directors
Monty Python Conquers America,2003,Frank Cvitanovich
Monty Python Live (Mostly),2014,Eric Idle