CME193: IntroductiontoScientificPython Lecture3: Tuples ...

CME 193: Introduction to Scientific Python Lecture 3: Tuples, sets, dictionaries and strings

Sven Schmit

stanford.edu/~schmit/cme193

3: Tuples, sets, dictionaries and strings

3-1

Tuples Dictionaries Sets Strings Modules Exercises

Contents

3: Tuples, sets, dictionaries and strings

3-2

Tuples

Seemingly similar to lists

>>> myTuple = (1, 2, 3) >>> myTuple[1] 2 >>> myTuple[1:3] (2, 3)

3: Tuples, sets, dictionaries and strings

3-3

Tuples are immutable

Unlike lists, we cannot change elements.

>>> myTuple = ([1, 2], [2, 3]) >>> myTuple[0] = [3,4] Traceback (most recent call last):

File "", line 1, in TypeError: 'tuple' object does not support item assignment >>> myTuple[0][1] = 3 >>> myTuple ([1, 3], [2, 3])

3: Tuples, sets, dictionaries and strings

3-4

Packing and unpacking

t = 1, 2, 3 x, y, z = t print t # (1, 2, 3) print y # 2

3: Tuples, sets, dictionaries and strings

3-5

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download