Exercise 8: Lists

In Python you define a list using brackets.  An empty list would simply be list = [].  A list can have any number of elements. To access a list  add the…

Exercise 9: Dictionary

Python supports a data structure known as dictionary:  a set which contains key, value pairs. For every key in a dictionary,  there is a value. Unlike lists, dictionaries do not…

Exercise 10: Read file

In Python you reading files is part of the standard library.  To read files, you can use the readlines() function. This will read a file line by line and store…

Exercise 11: Write file

Creating new files To create new files, you can use this code: #!/usr/bin/env python # create and open file f = open(“test.txt”,”w”) # write data to file f.write(“Hello World, \n”)…

Slices

A slice can be taken from a string or list, just as you can take a slice from a pizza. Python starts numbering of string and list elements from zero….