Session 2 - Data Structures: List & Tuples
Today we will work with data structures in python. You will by the end of these sessions be able to use Lists and Tuples, read and Write to Files and work with the for and while loop.
List and tuples are two of the build-in datastructures in python.
Learning goals
After this week you will be able to:
Work with lists, tuples
Loop over sequences with a for, foreach & while loops.
Sort sequences using the build in sorted function and use its key parameter to perform custom sorting.
Read from files and write to files using the build in “open” function.
Materials
Exercises
Ex 0.1: Slicing
By using the slicing syntax change the following collections.
After slicing:
1['Hello', 'World', 'Huston', 'we', 'are', 'here'] should become -> ['World', 'Huston', 'we', 'are']
2['Hello', 'World', 'Huston', 'we', 'are', 'here'] -> ['Hello', 'World']
3['Hello', 'World', 'Huston', 'we', 'are', 'here'] -> ['are', 'here']
4['Hello', 'World', 'Huston', 'we', 'are', 'here'] -> ['are']
5['Hello', 'World', 'Huston', 'we', 'are', 'here'] -> ['Hello', 'Huston', 'are']
6['Hello', 'World', 'Huston', 'we', 'are', 'here'] -> ['here', 'are', 'we', 'Huston', 'World', 'Hello']
7('Hello', 'World', 'Huston', 'we', 'are', 'here') should become -> ['World', 'Huston', 'we', 'are']
8'Hello World Huston we are here' -> 'World Huston we'
Figure out more on your own and practice this a lot!
Ex 1: Build-in functions on lists
names = ['George', 'Bejamin', 'Thomas', 'John']
len(names)
Ex 1.1: Is it a tuple or a list?
Claus, 51, male, clbo@kea.dk, 31011970-1313
Bmw, Toyota, Hyundai, Skoda, Fiat, Volvo
Claus, Henning, Torben, Carl, Tine
‘Hello’, ‘World’, ‘Huston’, ‘we’, ‘are’, ‘here’
Rolling Stones, Goats Head Soup, 31 August 1973, 46:56
40.730610, -73.935242, New York City, NY, USA; 31.739847, 65.755920, Kandahar, Kandahar Province, Afghanistan;
Ex 2: Sort a Text
Create a function that takes a string as a parameter and returns a list.
The function should remove all vowels and sort the consonants in alphabetic order, and the return the result.
Ex 3: Sort a list
Create a list of strings with names in it. (l = [‘Claus’, ‘Ib’, ‘Per’])
Sort this list by using the sorted() build in function.
Sort the list in reversed order.
Sort the list on the lenght of the name.
Sort the list based on the last letter in the name.
Sort the list in a way that the names that has an ‘a’ in it should be sorted first (still alphabetically).
Ex 4: Text editor plugin simulation
s = 'This is just a sample text that could have been a million times longer.\n\nYours Johnny'
Count the number of characters including blank spaces.
Count the number of characters excluding blank spaces.
Count the number of words.
Ex 4: Files
Create a file and call it lyrics.txt (it does not need to have any content)
Create a new file and call it songs.docx and in this file write 3 lines of text to it.
open and read the content and write it to your terminal window. * you should use both the read(), readline(), and readlines() methods for this. (so 3 times the same output).
Ex 5: Sort a list of tuples
1. Based on this list of tuples:
[(1,2),(2,2),(3,2),(2,1),(2,2),(1,5), (10,4), (10, 1), (3, 1)]
2. Sort the list so the result looks like this:
[(2, 1), (3, 1), (10, 1), (1, 2), (2, 2), (2, 2), (3, 2), (10, 4), (1, 5)]
Note