04 Advanced Python
04 Advanced Python
1
Reference
2
Outline
3
Outline
4
Tuple
• A tuple is a fixed-length, In [5]: tuple([4, 0, 2])
Out[5]: (4, 0, 2)
immutable sequence of
Python objects.
In [6]: tup = tuple('string')
• How to convert objects to In [7]: tup
tuples? Out[7]: ('s', 't', 'r', 'i', 'n', 'g')
• What are the functions of
operators + and * on tuples? In [13]: (4, None, 'foo') + ('bar',)
Out[13]: (4, None, 'foo', 'bar')
• Swapping: What is Python
way to swap two vars? In [24]: b, a = a, b
5
List In [37]: tup = ('foo', 'bar')
In [38]: b_list = list(tup)
• List are ordered and In [39]: b_list
mutable. Out[39]: ['foo', 'bar']
6
List – Add and remove In [48]: b_list
Out[48]: ['foo', 'red', 'bar', 'baz']
• What is the difference
between .pop() and In [49]: b_list.pop(0)
.remove()? Out[49]: 'foo'
In [53]: b_list.remove('red')
• What is the difference In [54]: b_list
between .append() and Out[54]: ['bar', 'baz']
.extend()?
In [59]: b_list.extend([7, 8])
In [60]: b_list
Out[60]: ['bar', 'baz', 7, 8]
7
List – Sort
In [61]: a = [7, 2, 5, 1, 3]
• You can sort lists using In [62]: sorted(a)
.sort() and sorted(). Out[62]: [1, 2, 3, 5, 7]
What is the difference? In [63]: a
Out[63]: [7, 2, 5, 1, 3]
In [64]: a.sort()
In [65]: a
Out[65]: [1, 2, 3, 5, 7]
Syntax:
list.sort(reverse=True|False, key=myFunc)
8
Built-in Sequence Functions
l1 = ['foo', 'bar', 'baz']
• What does each of the
l2 = ['one', 'two', 'three']
following functions do? for i, (a, b) in enumerate(zip(l1, l2)):
• enumerate() print('{0}: {1}, {2}'.format(i, a, b))
• zip() 0: foo, one
1: bar, two
• reversed()
2: baz, three
• What is the difference
between
list(reversed(range(10)))
sorted(reverse=True) and
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
reversed()?
9
Dictionary
d1 = {'a' : 1, 'b' : 2}
• Hash map or associative d1['c'] = 's'
array between key-value d1
pairs. {'a' : 1, 'b' : 2, 'c' : 's'}
del d1['a']
• What is the difference ret = d1.pop('c')
between .pop() and del? ret
's'
d1
{'b' : 2}
10
Dictionary Methods
mapping = {}
• Creating a dictionary for key, value in zip(k_l, v_l):
mapping[key] = value
11
Outline
• Namespaces, Scope, and
3.1 Data Structures and Local Functions
Sequences • Returning Multiple Values
3.2 Functions • Functions Are Objects
3.3 Files and the Operating • Anonymous (Lambda)
System Functions
• Currying: Partial Argument
Application
• Generators
12
Namespaces, Scope, and Local Functions
• Functions can access def func():
variables in two different a = []
a.append(1)
scopes: global and local. ### ### ###
• Variables that are assigned a = []
within a function by default def func():
a.append(1)
are assigned to the local ### ### ###
namespace. def func():
• What happens to a after the global a
calling func()? a = []
a.append(1)
13
Returning Multiple Values
• How to return multiple def f():
values from a function? a = 5
b = 6
c = 7
return a, b, c
a, b, c = f()
### ### ###
• What do you think about def f():
this alternative? a = 5
b = 6
return {'a' : a, 'b' : b}
14
Functions Are Objects
• Since Python functions are
objects, you can: string = '...'
• Put them in lists func_list = [f1, f2, f3]
• Iterate on them for func in func_list:
string = func(string)
15
Anonymous (Lambda) Functions
• Writing functions consisting def short_function(x):
of a single statement return x * 2
17
Generators
some_dict = {'a': 1, 'b': 2, 'c': 3}
list(dict_iterator)
['b', 'c']
18
itertools module
list(itertools.combinations(['a','b','c'], 2))
[('a', 'b'), ('a', 'c'), ('b', 'c')]
19
itertools module
list(itertools.permutations(['a','b','c'], 2))
[('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'c'), ('c', 'a'), ('c', 'b')]
20
itertools module
• Group the following list by the first letter.
names = ['Alan', 'Adam', 'Wes', 'Will', 'Albert', 'Steven']
import itertools
first_letter = lambda x: x[0]
names.sort(key=first_letter)
for letter, n in itertools.groupby(names, first_letter):
print(letter, list(n)) # n is a generator
A ['Alan', 'Adam', 'Albert']
S ['Steven']
W ['Wes', 'Will']
21
itertools module
23
Python File Support
• The built-in open function path = 'folder\\file1.txt' # or /
f = open(path) # read default mode
supports opening a file for for line in f:
reading or writing. # remove right white space
line = line.rstrip()
• You can iterate on the file print(line)
f.close()
handle.
Line 1
Line 2
with open(path) as f:
• Alternative syntax for line in f:
print(line.rstrip())
24
Python File Modes
25
Python File Support
• How to read a file into a list with open(path) as f:
lines = [x.rstrip() for x in f]
of strings?
>>> print(f.read(3))
Lin
>>> f.tell()
• Use read, tell, and seek to 3
control the reading process. >>> f.seek(8)
8
>>> print(f.read(6))
Line 2
26
Important Python File Methods or
Attributes
27
Writing to Files
• How to copy a text file skipping empty lines?
28
Homework
• Solve the homework on Advanced Python Programming
29
Summary
30