This is an old revision of the document!
Introduction to Python
Python basics
Python is an interpreted, dynamically typed language which is easy to use for scripting and prototyping applications.
C and Java programmers quickly adjust to the Python syntax. Unlike C, in Python, lists are predefined and usually more used than arrays:
l = [] l.append("1") l.append(0) l.append([]) print l
Remarks
- in Python we often use side-effects to modify data
- Python is not strongly-typed. As a consequence, lists may have different 'types' of elements
for i in range(0,len(l)): print l[i] print range(0,len(l)) for elem in l: print elem
Remarks
- List traversal may be achieved using indexing. Indexes are integers taken from a range.
range(0,len(l))
is a list in itself. - Lists (and other data structures) can be iterated using
in
Syntax
- unlike C or Java where we often use
{…}
for scoping, in Python we use tabs - control instructions such as
for
,while
,if
do not require(…)
for conditions but they must end with:
def func (l1, l2): l1.append(3) return l1 + l2 x = [1] y = [2] print func(x,y) print x
Remarks
- although it is possible to add type annotations, in Python a function's signature only consists of the number of parameters and their names
- objects are generally are passed as reference (hence, when printing
x
, we see the list[1,3]
) (for details see: Python data model) +
denotes list concatenation
Exercise 1 Write a function which determines the maximum number from a list. Exercise 2 Write a function which prints EACH repeating character from a string. (Hint: strings are lists of characters).
Useful data structures: dictionaries
Dictionaries are another useful data structure. A dictionary is a <key> : <value>
mapping. Unlike lists, keys may be of any type (integers, strings, or any other datatype).
d = {} d["X"] = ["X"] if "X" in d: print "d[X] is defined in the dictionary" if not "Y" in d: print "d[Y] is not defined in the dictionary"
Exercise 3 Write a function returns the number of repetitions of each character from a text. (Hint: use dictionaries to store the number of repetitions.)
Exercise 4 Write a function returns the number of unique characters from a list.
Exercise 5 Write a function which takes a pattern and a text and prints all indexes where an occurrence of pattern in text are found.
Remark:
- lists can be sliced in Python using the following syntax:
l[start_index:end_index]
Test it to see how slicing is performed.
Exercise 6 Modify the previous implementation to use slicing.
Remark:
- in Python we can use arbitrarily nested functions
Exercise 7 Write a function which searches for a list of patterns in a text.
def find_patterns (pattern_list, text): # checks if pattern is found at position index in text def inner_search (pattern,index):
For example, find_patterns([“ab”,“cd”], “abcdabcd”)
should print out “0,2,4,6”.
Remark:
- Matrices can be represented as lists of lists.
matrix = [[1,2,3],[4,5,6],[7,8,9]]
Exercise 8 Write a function which returns the elements from the principal diagonal of a matrix, as a list. Example: diag(matrix)=[1,5,9]
.
Exercise 9 Write a function which adds two matrices.
Exercise 10 Write a function which implements matrix transposition.
Exercise 11 Write a function which multiplies two matrices.