36
GC3: Grid Computing Competence Center Sequences and iteration in Python GC3: Grid Computing Competence Center, University of Zurich Sep. 11–12, 2013

Sequences and iteration in Python · Sequences Python provides a few built-in sequence classes: list mutable, possibly heterogeneous tuple immutable, possibly heterogeneous str immutable,

  • Upload
    others

  • View
    25

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Sequences and iteration in Python · Sequences Python provides a few built-in sequence classes: list mutable, possibly heterogeneous tuple immutable, possibly heterogeneous str immutable,

GC3: Grid Computing Competence Center

Sequences and iteration inPythonGC3: Grid Computing Competence Center,University of Zurich

Sep. 11–12, 2013

Page 2: Sequences and iteration in Python · Sequences Python provides a few built-in sequence classes: list mutable, possibly heterogeneous tuple immutable, possibly heterogeneous str immutable,

Sequences

Python provides a few built-in sequence classes:

list mutable, possibly heterogeneous

tuple immutable, possibly heterogeneous

str immutable, only holds characters

Additional sequence types are provided by externalmodules. For instance, the NumPy package, which iscommonly used in scientific Python codes, defines:

array mutable, homogeneous (like C/Fortranarrays)

GC3 Python training Sequences, Iteration Sep. 11–12, 2013

Page 3: Sequences and iteration in Python · Sequences Python provides a few built-in sequence classes: list mutable, possibly heterogeneous tuple immutable, possibly heterogeneous str immutable,

Lists - (mutable, heterogeneous)

Lists are by far the most common and used sequencetype in Python.

Lists are created and initialized by enclosing valuesinto ‘[’ and ‘]’:

>>> L = [ ’G’, ’C’ ]

You can append and remove items from a list:

>>> L.append(’3’)>>> print (L)[’G’, ’C’, ’3’]

You can append any object to it:

>>> L.append(L)>>> print(L)[’G’, ’C’, ’3’, [...]]

GC3 Python training Sequences, Iteration Sep. 11–12, 2013

Page 4: Sequences and iteration in Python · Sequences Python provides a few built-in sequence classes: list mutable, possibly heterogeneous tuple immutable, possibly heterogeneous str immutable,

Sequences, II

You can access individual items in a sequence usingthe postfix [] operator.

Sequence indices start at 0.

>>> L = [’G’, ’C’, ’3’]>>> print L[0], L[1], L[2]’G’ ’C’ ’3’>>> S = ’GC3’>>> print S[0], S[1], S[2]’G’ ’C’ ’3’

The len() function returns the number of elements ina sequence (not just lists).

>>> len(L)3

GC3 Python training Sequences, Iteration Sep. 11–12, 2013

Page 5: Sequences and iteration in Python · Sequences Python provides a few built-in sequence classes: list mutable, possibly heterogeneous tuple immutable, possibly heterogeneous str immutable,

Slices

The notation [n:m] is used for accessing a slice ofsequence (the items at positions n, n + 1, . . . , m − 1).

>>> # list numbers from 0 to 9>>> R = range(0,10)>>> R[1:4][1, 2, 3]

If n is omitted it defaults to 0, if m is omitted itdefaults to the length of the sequence.

GC3 Python training Sequences, Iteration Sep. 11–12, 2013

Page 6: Sequences and iteration in Python · Sequences Python provides a few built-in sequence classes: list mutable, possibly heterogeneous tuple immutable, possibly heterogeneous str immutable,

List mutation (1)

You can replace items in a mutable sequence byassigning them a new value:

>>> L[2] = ’Z’>>> print L[’G’, ’C’, ’Z’]

You can also replace an entire slice of a mutablesequence:

>>> L[1:3] = [1, 2, 3]>>> print L[’G’, 1, 2, 3]

The new slice does not need to have the same length:

>>> L[1:] = ’Zurich’>>> print L[’G’, ’Z’, ’u’, ’r’, ’i’, ’c’, ’h’]

GC3 Python training Sequences, Iteration Sep. 11–12, 2013

Page 7: Sequences and iteration in Python · Sequences Python provides a few built-in sequence classes: list mutable, possibly heterogeneous tuple immutable, possibly heterogeneous str immutable,

List mutation (2)

You can remove individual elements from a list eitherby specifying the element:>>> L.remove(’Z’)>>> L[’G’, ’u’, ’r’, ’i’, ’c’, ’h’]

or the position:>>> L.pop(2)’r’>>> L[’G’, ’u’, ’i’, ’c’, ’h’]

Please note that the remove() method only removesone element:>>> L = [’a’, ’a’, ’a’]>>> L.remove(’a’)>>> L[’a’, ’a’]

GC3 Python training Sequences, Iteration Sep. 11–12, 2013

Page 8: Sequences and iteration in Python · Sequences Python provides a few built-in sequence classes: list mutable, possibly heterogeneous tuple immutable, possibly heterogeneous str immutable,

Other containers

Theb following builtin containers are always available:

dict mutable key/value mapping.

set mutable, unordered set of unique elements.

frozenset immutable, unordered set of uniqueelements.

Other specialized containers are available in thecollections module:

dequeue a generalization of stacks and queues

namedtuple similar to a tuple, but allows you toaccess the elements by name

OrderedDict dictionary that remembers the order thatthe items were inserted.

GC3 Python training Sequences, Iteration Sep. 11–12, 2013

Page 9: Sequences and iteration in Python · Sequences Python provides a few built-in sequence classes: list mutable, possibly heterogeneous tuple immutable, possibly heterogeneous str immutable,

Other containers

Theb following builtin containers are always available:

dict mutable key/value mapping.

set mutable, unordered set of unique elements.

frozenset immutable, unordered set of uniqueelements.

Other specialized containers are available in thecollections module:

dequeue a generalization of stacks and queues

namedtuple similar to a tuple, but allows you toaccess the elements by name

OrderedDict dictionary that remembers the order thatthe items were inserted.

GC3 Python training Sequences, Iteration Sep. 11–12, 2013

Page 10: Sequences and iteration in Python · Sequences Python provides a few built-in sequence classes: list mutable, possibly heterogeneous tuple immutable, possibly heterogeneous str immutable,

Dictionaries

The dict type implements a key/value mapping:

>>> D = { }>>> D[’a’] = 1>>> D[2] = ’b’>>> D{’a’: 1, 2: ’b’}

Dictionaries can be created and initialized using twodifferent but equivalent syntaxes:

>>> D1 = { ’a’:1, ’b’:2 }>>> D2 = dict(a=1, b=2)>>> D1 == D2True

Keys must be hashable objects.

GC3 Python training Sequences, Iteration Sep. 11–12, 2013

Page 11: Sequences and iteration in Python · Sequences Python provides a few built-in sequence classes: list mutable, possibly heterogeneous tuple immutable, possibly heterogeneous str immutable,

Sets

The set type implements an unordered container thatholds exactly one object per equivalence class:

>>> S = set()>>> S.add(1)>>> S.add(’some string’)>>> S.add(1)>>> Sset([1, ’some string’])

You can create a set and add elements to it in one go:

>>> S2 = set([1, 2, 3, 4])

and remove elements:

>>> S2.remove(2)>>> S2.pop()1>>> S2set([3,4])

GC3 Python training Sequences, Iteration Sep. 11–12, 2013

Page 12: Sequences and iteration in Python · Sequences Python provides a few built-in sequence classes: list mutable, possibly heterogeneous tuple immutable, possibly heterogeneous str immutable,

Mutable vs Immutable

Some objects (e.g., tuple, int, str) are immutableand cannot be modified.

>>> S = ’GC3’>>> S[2] = ’2’Traceback (most recent call last):

File "<stdin>", line 1, in <module>TypeError: ’str’ object does not support item assignment

list, dict, set and user-defined objects are mutableand can be modified in-place.

GC3 Python training Sequences, Iteration Sep. 11–12, 2013

Page 13: Sequences and iteration in Python · Sequences Python provides a few built-in sequence classes: list mutable, possibly heterogeneous tuple immutable, possibly heterogeneous str immutable,

The ‘in’ operator

Use the in operator to test for presence of an item in acollection.

x in SEvaluates to True if x is equal to a value contained inthe S sequence (list, tuple, set).

x in DEvaluates to True if x is equal to a key in the D

dictionary.

x in TEvaluates to True if x is a substring of string T.

GC3 Python training Sequences, Iteration Sep. 11–12, 2013

Page 14: Sequences and iteration in Python · Sequences Python provides a few built-in sequence classes: list mutable, possibly heterogeneous tuple immutable, possibly heterogeneous str immutable,

The in operator and the if conditional

Testing for the existence of an element in a containeris a very common pattern: in operator:

>>> L = [1, 2, 3, 4]>>> if 1 in L:... print "Found!"...Found!

Which is equivalent to the following, more verbose,less pythonic form:

>>> L = [1, 2, 3, 4]>>> for item in L:... if item == 1:... print "Found!"... break

GC3 Python training Sequences, Iteration Sep. 11–12, 2013

Page 15: Sequences and iteration in Python · Sequences Python provides a few built-in sequence classes: list mutable, possibly heterogeneous tuple immutable, possibly heterogeneous str immutable,

All variables are references

In Python, all objects are ever passed by reference.

In particular, variables always store a reference toan object, never a copy!

Hence, you have to be careful when modifying objects:

>>> a = ["a", "b", "c"]>>> b = a>>> b.remove("b")>>> print a["a", "c"]

Run this example in the Online Python Tutor to betterunderstand what’s going on.

This applies particularly for variables that capture thearguments to a function call!

GC3 Python training Sequences, Iteration Sep. 11–12, 2013

Page 16: Sequences and iteration in Python · Sequences Python provides a few built-in sequence classes: list mutable, possibly heterogeneous tuple immutable, possibly heterogeneous str immutable,

All variables are references (demo)

www.pythontutor.com

GC3 Python training Sequences, Iteration Sep. 11–12, 2013

Page 17: Sequences and iteration in Python · Sequences Python provides a few built-in sequence classes: list mutable, possibly heterogeneous tuple immutable, possibly heterogeneous str immutable,

All variables are references (demo)

www.pythontutor.com

GC3 Python training Sequences, Iteration Sep. 11–12, 2013

Page 18: Sequences and iteration in Python · Sequences Python provides a few built-in sequence classes: list mutable, possibly heterogeneous tuple immutable, possibly heterogeneous str immutable,

All variables are references (demo)

www.pythontutor.com

GC3 Python training Sequences, Iteration Sep. 11–12, 2013

Page 19: Sequences and iteration in Python · Sequences Python provides a few built-in sequence classes: list mutable, possibly heterogeneous tuple immutable, possibly heterogeneous str immutable,

All variables are references (demo)

www.pythontutor.com

GC3 Python training Sequences, Iteration Sep. 11–12, 2013

Page 20: Sequences and iteration in Python · Sequences Python provides a few built-in sequence classes: list mutable, possibly heterogeneous tuple immutable, possibly heterogeneous str immutable,

for-loops

With the for statement, you can loop over the valuesin a list:

for i in range(0, 4):# loop blockprint (i*i)

To break out of a for loop, use the break statement.

To jump to the next iteration of a for loop, use thecontinue statement.

GC3 Python training Sequences, Iteration Sep. 11–12, 2013

Page 21: Sequences and iteration in Python · Sequences Python provides a few built-in sequence classes: list mutable, possibly heterogeneous tuple immutable, possibly heterogeneous str immutable,

The for statement can be used to loop over elementsin any sequence.

>>> for val in 1,2,3 :

... print val123

Loop over tuples

GC3 Python training Sequences, Iteration Sep. 11–12, 2013

Page 22: Sequences and iteration in Python · Sequences Python provides a few built-in sequence classes: list mutable, possibly heterogeneous tuple immutable, possibly heterogeneous str immutable,

The for statement can be used to loop over elementsin any sequence.

>>> for val in ’GC3’ :... print val’G’’C’’3’

Loop over strings

GC3 Python training Sequences, Iteration Sep. 11–12, 2013

Page 23: Sequences and iteration in Python · Sequences Python provides a few built-in sequence classes: list mutable, possibly heterogeneous tuple immutable, possibly heterogeneous str immutable,

The for statement can be used to loop over elementsin any sequence.

>>> D = dict(a=1, b=2)

>>> for val in D.keys() :

... print val’a’’b’

Loop overdictionary keys.

The .keys() part canbe omitted, as it’s the

default!

GC3 Python training Sequences, Iteration Sep. 11–12, 2013

Page 24: Sequences and iteration in Python · Sequences Python provides a few built-in sequence classes: list mutable, possibly heterogeneous tuple immutable, possibly heterogeneous str immutable,

If you want to loop over dictionary values, you have toexplicitly request it.

>>> D = dict(a=1, b=2)

>>> for val in D.values() :

... print val12

Loop overdictionary values

The .values()cannot be omitted!

GC3 Python training Sequences, Iteration Sep. 11–12, 2013

Page 25: Sequences and iteration in Python · Sequences Python provides a few built-in sequence classes: list mutable, possibly heterogeneous tuple immutable, possibly heterogeneous str immutable,

If you want to loop over a sorted sequence you can usethe function sorted() :

>>> for val in sorted([1,3,4,2]):... print val1234

and to loop over a sequence in inverted order you canuse the reversed() function:

>>> for val in reversed([1,3,4,2]):... print val2431

GC3 Python training Sequences, Iteration Sep. 11–12, 2013

Page 26: Sequences and iteration in Python · Sequences Python provides a few built-in sequence classes: list mutable, possibly heterogeneous tuple immutable, possibly heterogeneous str immutable,

Multiple assignment can be used in for statements aswell.

>>> L = [(1,’a’), (2,’b’), (3, ’c’)]>>> for num, char in L:... print ("num is " + str(num)... + ’ and char is ’ + char)

This is particularly useful with functions that return atuple. For instance the enumerate() function (look itup with help()!).

GC3 Python training Sequences, Iteration Sep. 11–12, 2013

Page 27: Sequences and iteration in Python · Sequences Python provides a few built-in sequence classes: list mutable, possibly heterogeneous tuple immutable, possibly heterogeneous str immutable,

Exercise A: Write a function invert(D) that takes adictionary D and returns a dictionary Dinv with keysand values swapped. (We assume that D is 1-1.)

Exercise B: Implement a zip2 function, that takes alist of 2-tuples and returns two lists: a list of all thefirst items in the pairs, and a list of all the seconditems in the pairs.

GC3 Python training Sequences, Iteration Sep. 11–12, 2013

Page 28: Sequences and iteration in Python · Sequences Python provides a few built-in sequence classes: list mutable, possibly heterogeneous tuple immutable, possibly heterogeneous str immutable,

Default values

Function arguments can have default values.

>>> def hello(name=’world’):... print ("Hello, " + name)...>>> hello()’Hello, world’

GC3 Python training Sequences, Iteration Sep. 11–12, 2013

Page 29: Sequences and iteration in Python · Sequences Python provides a few built-in sequence classes: list mutable, possibly heterogeneous tuple immutable, possibly heterogeneous str immutable,

Variadic functions, I

Functions like max and min take a variable number ofarguments.

That is possible with user-defined functions too.

If the last argument is prefixed with a * character,Python will make that argument into a tuple ofarguments passed to the function. (Except for theones that have already been assigned names.)

GC3 Python training Sequences, Iteration Sep. 11–12, 2013

Page 30: Sequences and iteration in Python · Sequences Python provides a few built-in sequence classes: list mutable, possibly heterogeneous tuple immutable, possibly heterogeneous str immutable,

Variadic functions, II

>>> def varfn1(*args):... print args>>> varfn1(1,2,3)(1, 2, 3)

>>> def varfn2(a, b, *rest):... print rest>>> varfn1(1,2,3)(3,)

GC3 Python training Sequences, Iteration Sep. 11–12, 2013

Page 31: Sequences and iteration in Python · Sequences Python provides a few built-in sequence classes: list mutable, possibly heterogeneous tuple immutable, possibly heterogeneous str immutable,

Variadic functions, III

You can call a function with an argument list that isonly determined at run time.

The unary * operator takes any sequence and makesit into a function argument list:

>>> L = [1, 2, 3]>>> max(*L)3

GC3 Python training Sequences, Iteration Sep. 11–12, 2013

Page 32: Sequences and iteration in Python · Sequences Python provides a few built-in sequence classes: list mutable, possibly heterogeneous tuple immutable, possibly heterogeneous str immutable,

Named arguments

Python allows calling a function with namedarguments:

hello(name="Alice")

When passing arguments by name, they can bepassed in any order:

>>> from fractions import Fraction>>> Fraction(numerator=1, denominator=2)Fraction(1, 2)>>> Fraction(denominator=2, numerator=1)Fraction(1, 2)

GC3 Python training Sequences, Iteration Sep. 11–12, 2013

Page 33: Sequences and iteration in Python · Sequences Python provides a few built-in sequence classes: list mutable, possibly heterogeneous tuple immutable, possibly heterogeneous str immutable,

Keyword arguments, I

Python lets you catch arbitrary named arguments.

Prefix the very last argument name with **, andPython will make it into a dictionary: keys are actualargument names and dictionary values are actualargument values.

>>> def kwfn1(**kwargs):... print kwargs>>> kwfn1(a=1, b=2){’a’:1, ’b’:2}

GC3 Python training Sequences, Iteration Sep. 11–12, 2013

Page 34: Sequences and iteration in Python · Sequences Python provides a few built-in sequence classes: list mutable, possibly heterogeneous tuple immutable, possibly heterogeneous str immutable,

Keyword arguments, II

Keyword argument can be combined with positionalarguments:

>>> def kwfn1(x, y, **kwargs):... print "x=%s y=%s kwargs=%s" % (x, y, kwargs)>>> kwfn1(0, 42, a=1, b=2)x=0 y=42 kwargs={’a’:1, ’b’:2}

. . . and also with variadic arguments:

>>> def kwfn2(x, *rest, **kwargs):... print ("x=%s rest=%s kwargs=%s"... % (x, rest, kwargs))>>> kwfn2(0, 1, 2, 3, a=1, b=2)x=0 rest=(1, 2, 3) kwargs={’a’:1, ’b’:2}

GC3 Python training Sequences, Iteration Sep. 11–12, 2013

Page 35: Sequences and iteration in Python · Sequences Python provides a few built-in sequence classes: list mutable, possibly heterogeneous tuple immutable, possibly heterogeneous str immutable,

Keyword arguments, III

You can pass to a function a set of keyword argumentsthat is determined only at run time.The ** operator takes any dictionary and turns it intothe bundle of keyword arguments for a function:

>>> D = { ’c’:4, ’d’:2 }>>> kwfn2(x=1, **D)x=1 rest=() kwargs={ ’c’:4, ’d’:2 }

GC3 Python training Sequences, Iteration Sep. 11–12, 2013

Page 36: Sequences and iteration in Python · Sequences Python provides a few built-in sequence classes: list mutable, possibly heterogeneous tuple immutable, possibly heterogeneous str immutable,

Exercise C: Write a maxarg function, that takesarbitrary keyword arguments (but assume the valuesare all numbers), and returns the name of theargument with the largest value.

Example:

>>> maxarg(a=1, b=2)’b’

GC3 Python training Sequences, Iteration Sep. 11–12, 2013