










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
→ Indentation is a requirement in Python! • Structures that introduce blocks end with a colon “:” from math import sqrt my_list = ...
Typology: Slides
1 / 18
This page cannot be seen from the preview
Don't miss anything!











DATABASE SYSTEMS GROUP
DATABASE SYSTEMS GROUP
DATABASE SYSTEMS GROUP
DATABASE SYSTEMS GROUP
s[2] 'N' M U N I C H 0 1 2 3 4 5 -6 -5 -4 -3 -2 - s = ' ' s[-1] 'H' s[2:] 'NICH' s[:-2] 'MUNI' s[2:-2] 'NI'
DATABASE SYSTEMS GROUP
DATABASE SYSTEMS GROUP
a = [x2 for x in range(7)] a [0, 1, 4, 9, 16, 25, 36] sum(a) 91 a + [x2 for x in range(7,9)] [0, 1, 4, 9, 16, 25, 36, 49, 64] del a[:3] [9, 16, 25, 36, 49, 64]
DATABASE SYSTEMS GROUP
a = ['one',['one'‚'two']] b = a[:] print(id(a),id(b)) 85992520 85995336 b[0] = 'three‚ >>> b[1][1] = 'three' print(id(a),id(b)) >>> print(id(a),id(b)) 85992520 85995336 85992520 85995336 one two
one sublist
sublist one two
one sublist
sublist
one three
one sublist
sublist Solution: the method deepcopy from the module copy
from copy import deepcopy b = deepcopy(a)
DATABASE SYSTEMS GROUP
t = 1, [2], 'tuple' #tuple packing t[2] 'tuple' t[0] = 3 TypeError t[1][0] = 3 t (1, [3], 'tuple') x, y, z = t #sequence unpacking
DATABASE SYSTEMS GROUP
d = { i2: i for i in range(7) } d {0: 0, 1: 1, 4: 2, 9: 3, 16: 4, 25: 5, 36: 6} d[4] 2 for entry in d.items(): if entry[0] == 4: print(entry) (4,2) [key for key in d.keys()] #iterating over values is supported, too [0, 1, 4, 9, 16, 25, 36] d[49] = 7 #delete values by using del key word, e.g. del d[36] d print(id(a),id(b)) 85992520 85995336 85992520 85995336 one two ##### a #### No Side Effect #### Shallow Copy one sublist ##### b^ one sublist one two ##### a one sublist ##### b^ three sublist #### Side Effect one three ##### a one sublist ##### b^ one sublist Solution: the method deepcopy from the module copy >>> from copy import deepcopy >>> b = deepcopy(a) DATABASE SYSTEMS GROUP ## Data Structures: Tuples ## ('A tuple with', 3, 'entries') - A tuple is a sequence of comma separated values - Values can have different types - Tuples are immutable (but can contain mutable values) >>> t = 1, [2], 'tuple' #tuple packing >>> t[2] 'tuple' >>> t[0] = 3 TypeError >>> t[1][0] = 3 >>> t (1, [3], 'tuple') >>> x, y, z = t #sequence unpacking DATABASE SYSTEMS GROUP ## Data Structures: Dictionaries (cont.) - Each key must be unique, since values are obtainable via the key - Dictionaries also support comprehension >>> d = { i2: i for i in range(7) } >>> d {0: 0, 1: 1, 4: 2, 9: 3, 16: 4, 25: 5, 36: 6} >>> d[4] 2 >>> for entry in d.items(): if entry[0] == 4: print(entry) (4,2) >>> [key for key in d.keys()] #iterating over values is supported, too [0, 1, 4, 9, 16, 25, 36] >>> d[49] = 7 #delete values by using del key word, e.g. del d[36] >>> d {0: 0, 1: 1, 4: 2, 49: 7, 9: 3, 16: 4, 25: 5, 36: 6}
DATABASE SYSTEMS GROUP
if
: <block 1> elif :
<block 2> else : <block 3>
a = 1 if (b > 2) else 0
while
: <block 1> else : #else case can be avoided by using break or simply be omitted <block 2> for in : <block 1> else : <block 2>
DATABASE SYSTEMS GROUP
def feet_to_meter(x): return x0. feet = [5.92, 49000, 1066.3] list(map(feet_to_meter, feet)) [1.804416, 14935.2, 325.00824] list(map( lambda x: x0.3048, feet)) [1.804416, 14935.2, 325.00824]
list(filter( lambda x: x%2 == 1, [1,2,3,4,5])) [1, 3, 5]
from functools import reduce reduce( lambda x,y: x+y, [1,2,3,4,5]) 15
5.920.3048 490000.3048 1066.3*0.
True False^ True False True
plus(plus(plus(plus(1,2),3),4),5)
DATABASE SYSTEMS GROUP
import numpy as np A = np.array([[1,2],[1,1]]) A array([[1, 2], [1, 1]]) B = np.array([[0,1],[2,1]]) AB : <block 1> elif
: <block 2> else : <block 3> >>> a = 1 if (b > 2) else 0 - Loops: >>> while 0. >>> feet = [5.92, 49000, 1066.3] >>> list(map(feet_to_meter, feet)) [1.804416, 14935.2, 325.00824] >>> list(map( lambda x: x0.3048, feet)) [1.804416, 14935.2, 325.00824] - filter(func,seq) >>> list(filter( lambda x: x%2 == 1, [1,2,3,4,5])) [1, 3, 5] - reduce(func,seq) >>> from functools import reduce >>> reduce( lambda x,y: x+y, [1,2,3,4,5]) 15 5.920.3048 490000.3048 1066.30. #### map True False^ True False True #### filter plus(plus(plus(plus(1,2),3),4),5) #### reduce DATABASE SYSTEMS GROUP ## NumPy - The fundamental package for scientific computing and core part of the #### SciPy stack - Homogeneous multidimensional arrays as main objects - Provides many arithmetic operations on arrays >>> import numpy as np >>> A = np.array([[1,2],[1,1]]) >>> A array([[1, 2], [1, 1]]) >>> B = np.array([[0,1],[2,1]]) >>> A*B array([[0, 2],: <block 1> else : #else case can be avoided by using break or simply be omitted <block 2> >>> for in : <block 1> else : <block 2> DATABASE SYSTEMS GROUP ## map() , filter() and reduce() - map(func,seq) >>> def feet_to_meter(x): return x elif
: <block 2> else : <block 3> >>> a = 1 if (b > 2) else 0 - Loops: >>> while : <block 1> else : #else case can be avoided by using break or simply be omitted <block 2> >>> for in : <block 1> else : <block 2> DATABASE SYSTEMS GROUP ## map() , filter() and reduce() - map(func,seq) >>> def feet_to_meter(x): return x0. >>> feet = [5.92, 49000, 1066.3] >>> list(map(feet_to_meter, feet)) [1.804416, 14935.2, 325.00824] >>> list(map( lambda x: x0.3048, feet)) [1.804416, 14935.2, 325.00824] - filter(func,seq) >>> list(filter( lambda x: x%2 == 1, [1,2,3,4,5])) [1, 3, 5] - reduce(func,seq) >>> from functools import reduce >>> reduce( lambda x,y: x+y, [1,2,3,4,5]) 15 5.920.3048 490000.3048 1066.30. #### map True False^ True False True #### filter plus(plus(plus(plus(1,2),3),4),5) #### reduce DATABASE SYSTEMS GROUP ## NumPy - The fundamental package for scientific computing and core part of the #### SciPy stack - Homogeneous multidimensional arrays as main objects - Provides many arithmetic operations on arrays >>> import numpy as np >>> A = np.array([[1,2],[1,1]]) >>> A array([[1, 2], [1, 1]]) >>> B = np.array([[0,1],[2,1]]) >>> AB array([[0, 2], [2, 1]]) np.dot(A,B) array([[4, 3], [2, 2]])