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 strongly-typed. However, lists may have different 'types' of elements
for i in range(0, len(l)): print(l[i]) print(list(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 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 and tuples
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 which returns the number of repetitions of each character from a text. (Hint: use dictionaries to store the number of repetitions.)
Remarks
- Classes are the usual means for storing heterogeneous data (e.g. for a student, his name, email, list of attended lectures, etc.). However it is sometimes easier to use tuples instead. The example below illustrates the creation of a tuple and accessing parts of it.
stud_info = ("Mihai", "mihai@upb.ro", ["AA", "PA", "SD", "Programare"]) str = stud_info[0] + "'s email is " + stud_info[1] print(str)
Exercise 4 Write a function which takes a list of tuples containing student info and returns only those corresponding to students which have attended at least 3 lectures.
Exercise 5 Write a function which returns the number of unique characters from a list.
Exercise 6 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 7 Modify the previous implementation to use slicing.
Remark:
- in Python we can use arbitrarily nested functions
Exercise 8 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 9 Write a function which returns the elements from the principal diagonal of a matrix, as a list. Example: diag(matrix) = [1, 5, 9]
.
Exercise 10 Write a function which adds two matrices.
Exercise 11 Write a function which implements matrix transposition.
Exercise 12 Write a function which multiplies two matrices.