Python Guide V1.2.11

# Age5 what you can do with a tuple you cannot

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: .0!#!nearest!integer round(3.276,2)! !3.28!#!2!decimals sum(!(1,!5.5,!28)!)! !21.5!#!add!items zip(listx,!listy)! !list of (x,y) tuples ! ! ! Lists and tuples • Both are sequences of arbitrary objects with index positions starting at 0, going up by 1 • A tuple has optional round brackets • xt!=!1,!!!#!a!single!member!tuple! • xt!=!(1,!8.9,!"TV")! • A list has square brackets (required) • xl!=![1,!8.9,!"TV"] • list("too")! !['t','o','o'] • tuple(["age",5])! !('age',5) What you can do with a tuple • • • • • • • • • • You cannot change length or contents (immutable) len(xt) 3 8.9!in!xt! !True xt.count(8.9) 1!#!how!many!8.9!entries xt.index("TV")! !2!#!first!TV!in!position!2 xt[2]! !'TV'!!!!#!in!position!2 xt[21]! !'TV'!!!#!in!last!position xt[0:2]! !(1,!8.9)!!!!#!extract!slice xt[1:]! !(8.9,!'TV')!!#!open2ended!slice xt[0:!:2]! !(1,!'TV')!#!slice!by!twos! What you can do with a list almost anything you can do with a tuple, plus… xl.append(5.7)!!!#!adds!5.7!to!end! xl.insert(3,...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online