CME193: IntroductiontoScientificPython Lecture3: Tuples,sets ...

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

Contents

Tuples

Dictionaries

Sets

Strings

Modules

Exercises

3: Tuples, sets, dictionaries and strings

3-2

Tuples

Seemingly similar to lists

>>>

>>>

2

>>>

(2,

3: Tuples, sets, dictionaries and strings

myTuple = (1, 2, 3)

myTuple[1]

myTuple[1:3]

3)

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

print y

3: Tuples, sets, dictionaries and strings

# (1, 2, 3)

# 2

3-5

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

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

Google Online Preview   Download