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….

Multiple return

Python functions can return multiple variables. These variables can be stored in variables directly. A function is not required to return a variable, it can return zero, one, two or…

Scope

Variables have a certain reach within a program. A global variable can be used anywhere in a program, but a local variable is known only in a certain area (function,…

Time and date

Python can get the system time using the module time. To use this module type: import time. The first line returns all variables required (year,month,day,hour,minute). The function time.time() returns ticks,…

Class

Python can create objects. An object is a collection of methods and variables. Objects are created from classes. If an object is created, the method __init__ is called. The self…