15.04.2013 Views

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Like numbers and strings, tuples are immutable, which means you cannot update them or change values<br />

of tuple elements. In Sections 6.2 and 6.3.2, we were able to take portions of an existing string to<br />

create a new string. The same applies for tuples.<br />

>>> aTuple = aTuple[0], aTuple[1], aTuple[-1]<br />

>>> aTuple<br />

(123, 'abc', (7-9j))<br />

>>> tup1 = (12, 34.56)<br />

>>> tup2 = ('abc', 'xyz')<br />

>>> tup3 = tup1 + tup2<br />

>>> tup3<br />

(12, 34.56, 'abc', 'xyz')<br />

How to Remove Tuple Elements and Tuples<br />

Removing individual tuple elements is not possible. There is, of course, nothing wrong with putting<br />

together another tuple with the undesired elements discarded.<br />

To explicitly remove an entire tuple, just use the del statement to reduce an object's reference count. It<br />

will be deallocated when that count is zero. Keep in mind that most of the time one will just let an object<br />

go out of scope rather than using del, a rare occurrence in everyday <strong>Python</strong> programming.<br />

del aTuple

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!