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 X Write a function which prints EACH repeating character from a string. (Hint: strings are lists of characters).
Useful data structures: dictionaries and sets
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 X Write a function returns the number of repetitions of each character from a text. (Hint: use dictionaries to store the number of repetitions.)
Exercise X Write a function returns the number of unique characters from a list.
Remark:
- a simpler way to ensure uniqueness is to use sets.
- a set may be created from a list:
s = set([1,2,3,1])
- and in turn, a list may be created from a set:
l = list(s)
Exercise X 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 X Modify the previous implementation to use slicing.
Remark:
- in Python we can use arbitrarily nested functions
Exercise X 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):
Remark:
- Python supports functional-style programming to some extent.
def plus1(x): return x + 1 print map(plus1,[1,2,3]) print map(lambda x:x+1, [1,2,3])