12.07.2015 Views

Think Python - Denison University

Think Python - Denison University

Think Python - Denison University

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

8.12. Glossary 798.12 Glossaryobject: Something a variable can refer to. For now, you can use “object” and “value” interchangeably.sequence: Anorderedset;thatis,asetofvalueswhereeachvalueisidentifiedbyanintegerindex.item: One of thevalues inasequence.index: Aninteger value used toselect an item inasequence, such as acharacter inastring.slice: A part of astringspecified by arange ofindices.empty string: A stringwithnocharacters and length 0, represented by twoquotation marks.immutable: The property of asequence whose items cannot be assigned.traverse: To iteratethrough theitems inasequence, performing a similaroperation on each.search: A pattern of traversal that stops when itfinds what itislooking for.counter: A variable used tocount something, usually initializedtozero and then incremented.method: A function that isassociated withan object and called using dot notation.invocation: A statement that calls amethod.8.13 ExercisesExercise8.9 Astringslicecantakeathirdindexthatspecifiesthe“stepsize;”thatis,thenumberofspaces between successive characters. A step size of 2 means every other character; 3 means everythird,etc.>>> fruit = 'banana'>>> fruit[0:5:2]'bnn'A stepsizeof -1goes through theword backwards, sothe slice[::-1]generates areversed string.Usethisidiom towriteaone-line version ofis_palindromefrom Exercise 6.6.Exercise 8.10 Read the documentation of the string methods at docs.python.org/lib/string-methods.html. You might want to experiment with some of them to make sure you understandhow they work. stripandreplaceare particularly useful.The documentation uses a syntax that might be confusing. For example, infind(sub[, start[, end]]), the brackets indicate optional arguments. So sub is required, butstartis optional, and ifyou includestart,thenendisoptional.Exercise8.11 Thefollowingfunctionsareallintendedtocheck whetherastringcontains anylowercaseletters, but at least some of them are wrong. For each function, describe what the functionactually does (assumingthat theparameter isastring).

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

Saved successfully!

Ooh no, something went wrong!