0% found this document useful (0 votes)
2 views30 pages

04 Advanced Python

This document outlines a course on Advanced Python, covering data structures, functions, and file handling. It includes detailed explanations of tuples, lists, dictionaries, and various built-in functions, as well as advanced topics like lambda functions, currying, and the itertools module. Additionally, it provides practical examples and homework assignments related to Python programming.

Uploaded by

Khattab Abdalah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
2 views30 pages

04 Advanced Python

This document outlines a course on Advanced Python, covering data structures, functions, and file handling. It includes detailed explanations of tuples, lists, dictionaries, and various built-in functions, as well as advanced topics like lambda functions, currying, and the itertools module. Additionally, it provides practical examples and homework assignments related to Python programming.

Uploaded by

Khattab Abdalah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 30

Advanced Python

Prof. Gheith Abandah

1
Reference

• Wes McKinney, Python for Data Analysis: Data Wrangling


with Pandas, NumPy, and IPython, O’Reilly Media, 2nd
Edition, 2018.
• Material: https://github.com/wesm/pydata-book

2
Outline

3.1 Data Structures and Sequences


3.2 Functions
3.3 Files and the Operating System

3
Outline

3.1 Data Structures and • Tuple


Sequences • List
3.2 Functions • Sequence Functions
3.3 Files and the Operating • Dict
System

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']

• How to convert objects to


In [44]: list(range(0, 7))
lists? Out[44]: [0, 1, 2, 3, 4, 5, 6]
• What is the difference
between .append() and In [45]: b_list.append('baz')
.insert()? In [47]: b_list.insert(1, 'red')
In [48]: b_list
Out[48]: ['foo', 'red', 'bar', 'baz']

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

• .update() d1.update({'b' : 11, 'c' : 12})


• .get()
• .keys() value = d.get(key, default_value)
• .values()
• .items() for key, value in d.items():
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)

• Use them as arguments to


for x in map(f1, iter):
other functions
print(x)

15
Anonymous (Lambda) Functions
• Writing functions consisting def short_function(x):
of a single statement return x * 2

• How to sort a collection of


equiv_anon = lambda x: x * 2
strings by the number of
distinct letters in each
string? strings.sort(key=lambda x:
len(set(list(x))))

strings = ['card', 'aaaa', 'abab']

['aaaa', 'abab', 'card']


16
Currying: Partial Argument Application
def add_numbers(x, y):
return x + y
• Currying is deriving new
functions from existing ones.
from functools import partial
• In Python, use partial() add_five = partial(add_numbers, 5)
add_five(4)
9

17
Generators
some_dict = {'a': 1, 'b': 2, 'c': 3}

• Use iter() to create an


dict_iterator = iter(some_dict)
iterable object.
next(dict_iterator)
'a'

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

list(itertools.product([1, 2], ['a', 'b']))


[(1, 'a'), (1, 'b'), (2, 'a'), (2, 'b')]
22
Outline

3.1 Data Structures and Sequences


3.2 Functions
3.3 Files and the Operating System

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

The default is text mode


and utf-8 encoding.

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?

with open('tmp.txt', 'w') as handle:


handle.writelines(x for x in open(path) if len(x) > 1)

28
Homework
• Solve the homework on Advanced Python Programming

29
Summary

3.1 Data Structures and Sequences


3.2 Functions
3.3 Files and the Operating System

30

You might also like