|
|
| (3 intermediate revisions by 2 users not shown) |
| Line 1: |
Line 1: |
| <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
| | {{Think Python/Page}} |
| "http://www.w3.org/TR/REC-html40/loose.dtd">
| |
| <HTML>
| |
| <HEAD>
| |
|
| |
|
| <META http-equiv="Content-Type" content="text/html; charset=US-ASCII">
| | == Chapter 8  Strings == |
| <META name="GENERATOR" content="hevea 1.10">
| | |
| <LINK rel="stylesheet" type="text/css" href="book.css">
| | |
| <TITLE>Strings</TITLE>
| | |
| </HEAD>
| | === 8.1  A string is a sequence === |
| <BODY >
| | |
| <A HREF="book008.html"><IMG SRC="previous_motif.gif" ALT="Previous"></A>
| | |
| <A HREF="index.html"><IMG SRC="contents_motif.gif" ALT="Up"></A>
| | {{?}} |
| <A HREF="book010.html"><IMG SRC="next_motif.gif" ALT="Next"></A>
| | |
| <HR>
| | |
| <H1 CLASS="chapter"><A NAME="htoc94"><FONT COLOR=black><FONT SIZE=3>Chapter 8</FONT></FONT></A><FONT COLOR=black><FONT SIZE=3>  Strings</FONT></FONT></H1><P><FONT COLOR=black><FONT SIZE=3>
| | |
| </FONT></FONT><A NAME="strings"></A></P><H2 CLASS="section"><A NAME="toc85"></A><A NAME="htoc95"><FONT COLOR=black><FONT SIZE=3>8.1</FONT></FONT></A><FONT COLOR=black><FONT SIZE=3>  A string is a sequence</FONT></FONT></H2><P><FONT COLOR=black><FONT SIZE=3>
| | |
| </FONT></FONT><A NAME="@default568"></A><FONT COLOR=black><FONT SIZE=3>
| | A string is a '''sequence''' of characters. |
| </FONT></FONT><A NAME="@default569"></A><FONT COLOR=black><FONT SIZE=3>
| |
| </FONT></FONT><A NAME="@default570"></A><FONT COLOR=black><FONT SIZE=3>
| |
| </FONT></FONT><A NAME="@default571"></A></P><P><FONT COLOR=black><FONT SIZE=3>A string is a </FONT></FONT><FONT COLOR=black><FONT SIZE=3><B>sequence</B></FONT></FONT><FONT COLOR=black><FONT SIZE=3> of characters.
| |
| You can access the characters one at a time with the | | You can access the characters one at a time with the |
| bracket operator:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> fruit = 'banana' | | bracket operator: |
| | <PRE CLASS="verbatim">>>> fruit = 'banana' |
| >>> letter = fruit[1] | | >>> letter = fruit[1] |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>The second statement selects character number 1 from </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>fruit</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> and assigns it to </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>letter</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>. </FONT></FONT></P><P><A NAME="@default572"></A></P><P><FONT COLOR=black><FONT SIZE=3>The expression in brackets is called an </FONT></FONT><FONT COLOR=black><FONT SIZE=3><B>index</B></FONT></FONT><FONT COLOR=black><FONT SIZE=3>.
| | </PRE> |
| | The second statement selects character number 1 from <TT>fruit</TT> and assigns it to <TT>letter</TT>. |
| | |
| | The expression in brackets is called an '''index'''. |
| The index indicates which character in the sequence you | | The index indicates which character in the sequence you |
| want (hence the name).</FONT></FONT></P><P><FONT COLOR=black><FONT SIZE=3>But you might not get what you expect:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> print letter | | want (hence the name). |
| | |
| | But you might not get what you expect: |
| | <PRE CLASS="verbatim">>>> print letter |
| a | | a |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>For most people, the first letter of </FONT></FONT><CODE><FONT COLOR=black><FONT SIZE=3>'banana'</FONT></FONT></CODE><FONT COLOR=black><FONT SIZE=3> is </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>b</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>, not
| | </PRE> |
| </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>a</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>. But for computer scientists, the index is an offset from the
| | For most people, the first letter of <CODE>'banana'</CODE> is <TT>b</TT>, not |
| beginning of the string, and the offset of the first letter is zero.</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> letter = fruit[0] | | <TT>a</TT>. But for computer scientists, the index is an offset from the |
| | beginning of the string, and the offset of the first letter is zero. |
| | <PRE CLASS="verbatim">>>> letter = fruit[0] |
| >>> print letter | | >>> print letter |
| b | | b |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>So </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>b</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> is the 0th letter (“zero-eth”) of </FONT></FONT><CODE><FONT COLOR=black><FONT SIZE=3>'banana'</FONT></FONT></CODE><FONT COLOR=black><FONT SIZE=3>, </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>a</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>
| | </PRE> |
| is the 1th letter (“one-eth”), and </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>n</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> is the 2th (“two-eth”) | | So <TT>b</TT> is the 0th letter (“zero-eth”) of <CODE>'banana'</CODE>, <TT>a</TT> |
| letter.</FONT></FONT></P><P><A NAME="@default573"></A><FONT COLOR=black><FONT SIZE=3> | | is the 1th letter (“one-eth”), and <TT>n</TT> is the 2th (“two-eth”) |
| </FONT></FONT><A NAME="@default574"></A></P><P><FONT COLOR=black><FONT SIZE=3>You can use any expression, including variables and operators, as an
| | letter. |
| | |
| | |
| | |
| | |
| | You can use any expression, including variables and operators, as an |
| index, but the value of the index has to be an integer. Otherwise you | | index, but the value of the index has to be an integer. Otherwise you |
| get:</FONT></FONT></P><P><A NAME="@default575"></A><FONT COLOR=black><FONT SIZE=3> | | get: |
| </FONT></FONT><A NAME="@default576"></A><FONT COLOR=black><FONT SIZE=3>
| | |
| </FONT></FONT><A NAME="@default577"></A></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> letter = fruit[1.5]
| | |
| | |
| | |
| | <PRE CLASS="verbatim">>>> letter = fruit[1.5] |
| TypeError: string indices must be integers | | TypeError: string indices must be integers |
| </FONT></FONT></PRE><H2 CLASS="section"><A NAME="toc86"></A><A NAME="htoc96"><FONT COLOR=black><FONT SIZE=3>8.2</FONT></FONT></A><FONT COLOR=black><FONT SIZE=3>  </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>len</TT></FONT></FONT></H2><P><A NAME="@default578"></A><FONT COLOR=black><FONT SIZE=3>
| | </PRE>=== 8.2  <TT>len</TT> === |
| </FONT></FONT><A NAME="@default579"></A></P><P><FONT COLOR=black><FONT SIZE=3><TT>len</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> is a built-in function that returns the number of characters
| | |
| in a string:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> fruit = 'banana' | | |
| | |
| | |
| | <TT>len</TT> is a built-in function that returns the number of characters |
| | in a string: |
| | <PRE CLASS="verbatim">>>> fruit = 'banana' |
| >>> len(fruit) | | >>> len(fruit) |
| 6 | | 6 |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>To get the last letter of a string, you might be tempted to try something
| | </PRE> |
| like this:</FONT></FONT></P><P><A NAME="@default580"></A><FONT COLOR=black><FONT SIZE=3> | | To get the last letter of a string, you might be tempted to try something |
| </FONT></FONT><A NAME="@default581"></A></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> length = len(fruit)
| | like this: |
| | |
| | |
| | |
| | <PRE CLASS="verbatim">>>> length = len(fruit) |
| >>> last = fruit[length] | | >>> last = fruit[length] |
| IndexError: string index out of range | | IndexError: string index out of range |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>The reason for the </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>IndexError</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> is that there is no letter in </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>’banana’</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> with the index 6. Since we started counting at zero, the
| | </PRE> |
| | The reason for the <TT>IndexError</TT> is that there is no letter in <TT>’banana’</TT> with the index 6. Since we started counting at zero, the |
| six letters are numbered 0 to 5. To get the last character, you have | | six letters are numbered 0 to 5. To get the last character, you have |
| to subtract 1 from </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>length</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> last = fruit[length-1] | | to subtract 1 from <TT>length</TT>: |
| | <PRE CLASS="verbatim">>>> last = fruit[length-1] |
| >>> print last | | >>> print last |
| a | | a |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>Alternatively, you can use negative indices, which count backward from
| | </PRE> |
| the end of the string. The expression </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>fruit[-1]</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> yields the last | | Alternatively, you can use negative indices, which count backward from |
| letter, </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>fruit[-2]</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> yields the second to last, and so on.</FONT></FONT></P><P><A NAME="@default582"></A><FONT COLOR=black><FONT SIZE=3> | | the end of the string. The expression <TT>fruit[-1]</TT> yields the last |
| </FONT></FONT><A NAME="@default583"></A></P><H2 CLASS="section"><A NAME="toc87"></A><A NAME="htoc97"><FONT COLOR=black><FONT SIZE=3>8.3</FONT></FONT></A><FONT COLOR=black><FONT SIZE=3>  Traversal with a </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>for</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> loop</FONT></FONT></H2><P><FONT COLOR=black><FONT SIZE=3>
| | letter, <TT>fruit[-2]</TT> yields the second to last, and so on. |
| </FONT></FONT><A NAME="for"></A></P><P><A NAME="@default584"></A><FONT COLOR=black><FONT SIZE=3>
| | |
| </FONT></FONT><A NAME="@default585"></A><FONT COLOR=black><FONT SIZE=3>
| | |
| </FONT></FONT><A NAME="@default586"></A><FONT COLOR=black><FONT SIZE=3>
| | |
| </FONT></FONT><A NAME="@default587"></A><FONT COLOR=black><FONT SIZE=3>
| | === 8.3  Traversal with a <TT>for</TT> loop === |
| </FONT></FONT><A NAME="@default588"></A><FONT COLOR=black><FONT SIZE=3>
| | |
| </FONT></FONT><A NAME="@default589"></A></P><P><FONT COLOR=black><FONT SIZE=3>A lot of computations involve processing a string one character at a
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | A lot of computations involve processing a string one character at a |
| time. Often they start at the beginning, select each character in | | time. Often they start at the beginning, select each character in |
| turn, do something to it, and continue until the end. This pattern of | | turn, do something to it, and continue until the end. This pattern of |
| processing is called a </FONT></FONT><FONT COLOR=black><FONT SIZE=3><B>traversal</B></FONT></FONT><FONT COLOR=black><FONT SIZE=3>. One way to write a traversal | | processing is called a '''traversal'''. One way to write a traversal |
| is with a </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>while</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> loop:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>index = 0 | | is with a <TT>while</TT> loop: |
| | <PRE CLASS="verbatim">index = 0 |
| while index < len(fruit): | | while index < len(fruit): |
| letter = fruit[index] | | letter = fruit[index] |
| print letter | | print letter |
| index = index + 1 | | index = index + 1 |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>This loop traverses the string and displays each letter on a line by
| | </PRE> |
| itself. The loop condition is </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>index < len(fruit)</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>, so | | This loop traverses the string and displays each letter on a line by |
| when </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>index</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> is equal to the length of the string, the | | itself. The loop condition is <TT>index < len(fruit)</TT>, so |
| | when <TT>index</TT> is equal to the length of the string, the |
| condition is false, and the body of the loop is not executed. The | | condition is false, and the body of the loop is not executed. The |
| last character accessed is the one with the index </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>len(fruit)-1</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>, | | last character accessed is the one with the index <TT>len(fruit)-1</TT>, |
| which is the last character in the string.</FONT></FONT></P><DIV CLASS="theorem"><FONT COLOR=black><FONT SIZE=3><B>Exercise 1</B></FONT></FONT><FONT COLOR=black><FONT SIZE=3>  <EM> | | which is the last character in the string. |
| | <DIV CLASS="theorem">'''Exercise 1'''  '' |
| Write a function that takes a string as an argument | | Write a function that takes a string as an argument |
| and displays the letters backward, one per line. | | and displays the letters backward, one per line. |
| </EM></FONT></FONT></DIV><P><FONT COLOR=black><FONT SIZE=3>Another way to write a traversal is with a </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>for</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> loop:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>for char in fruit:
| | ''</DIV> |
| | Another way to write a traversal is with a <TT>for</TT> loop: |
| | <PRE CLASS="verbatim">for char in fruit: |
| print char | | print char |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>Each time through the loop, the next character in the string is assigned
| | </PRE> |
| to the variable </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>char</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>. The loop continues until no characters are | | Each time through the loop, the next character in the string is assigned |
| left.</FONT></FONT></P><P><A NAME="@default590"></A><FONT COLOR=black><FONT SIZE=3> | | to the variable <TT>char</TT>. The loop continues until no characters are |
| </FONT></FONT><A NAME="@default591"></A><FONT COLOR=black><FONT SIZE=3>
| | left. |
| </FONT></FONT><A NAME="@default592"></A></P><P><FONT COLOR=black><FONT SIZE=3>The following example shows how to use concatenation (string addition)
| | |
| and a </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>for</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> loop to generate an abecedarian series (that is, in | | |
| alphabetical order). In Robert McCloskey’s book </FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM>Make | | |
| Way for Ducklings</EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3>, the names of the ducklings are Jack, Kack, Lack, | | |
| | |
| | The following example shows how to use concatenation (string addition) |
| | and a <TT>for</TT> loop to generate an abecedarian series (that is, in |
| | alphabetical order). In Robert McCloskey’s book ''Make |
| | Way for Ducklings'', the names of the ducklings are Jack, Kack, Lack, |
| Mack, Nack, Ouack, Pack, and Quack. This loop outputs these names in | | Mack, Nack, Ouack, Pack, and Quack. This loop outputs these names in |
| order:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>prefixes = 'JKLMNOPQ' | | order: |
| | <PRE CLASS="verbatim">prefixes = 'JKLMNOPQ' |
| suffix = 'ack' | | suffix = 'ack' |
|
| |
|
| for letter in prefixes: | | for letter in prefixes: |
| print letter + suffix | | print letter + suffix |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>The output is:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>Jack
| | </PRE> |
| | The output is: |
| | <PRE CLASS="verbatim">Jack |
| Kack | | Kack |
| Lack | | Lack |
| Line 106: |
Line 151: |
| Pack | | Pack |
| Qack | | Qack |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>Of course, that’s not quite right because “Ouack” and
| | </PRE> |
| “Quack” are misspelled.</FONT></FONT></P><DIV CLASS="theorem"><FONT COLOR=black><FONT SIZE=3><B>Exercise 2</B></FONT></FONT><FONT COLOR=black><FONT SIZE=3>  <EM> | | Of course, that’s not quite right because “Ouack” and |
| | “Quack” are misspelled. |
| | <DIV CLASS="theorem">'''Exercise 2'''  '' |
| Modify the program to fix this error. | | Modify the program to fix this error. |
| </EM></FONT></FONT></DIV><H2 CLASS="section"><A NAME="toc88"></A><A NAME="htoc98"><FONT COLOR=black><FONT SIZE=3>8.4</FONT></FONT></A><FONT COLOR=black><FONT SIZE=3>  String slices</FONT></FONT></H2><P><FONT COLOR=black><FONT SIZE=3>
| | ''</DIV>=== 8.4  String slices === |
| </FONT></FONT><A NAME="slice"></A></P><P><A NAME="@default593"></A><FONT COLOR=black><FONT SIZE=3>
| | |
| </FONT></FONT><A NAME="@default594"></A><FONT COLOR=black><FONT SIZE=3>
| | |
| </FONT></FONT><A NAME="@default595"></A><FONT COLOR=black><FONT SIZE=3>
| | |
| </FONT></FONT><A NAME="@default596"></A><FONT COLOR=black><FONT SIZE=3>
| | |
| </FONT></FONT><A NAME="@default597"></A></P><P><FONT COLOR=black><FONT SIZE=3>A segment of a string is called a </FONT></FONT><FONT COLOR=black><FONT SIZE=3><B>slice</B></FONT></FONT><FONT COLOR=black><FONT SIZE=3>. Selecting a slice is
| | |
| similar to selecting a character:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> s = 'Monty Python' | | |
| | |
| | |
| | |
| | |
| | A segment of a string is called a '''slice'''. Selecting a slice is |
| | similar to selecting a character: |
| | <PRE CLASS="verbatim">>>> s = 'Monty Python' |
| >>> print s[0:5] | | >>> print s[0:5] |
| Monty | | Monty |
| >>> print s[6:13] | | >>> print s[6:13] |
| Python | | Python |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>The operator </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>[n:m]</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> returns the part of the string from the
| | </PRE> |
| | The operator <TT>[n:m]</TT> returns the part of the string from the |
| “n-eth” character to the “m-eth” character, including the first but | | “n-eth” character to the “m-eth” character, including the first but |
| excluding the last. This behavior is counterintuitive, but it might | | excluding the last. This behavior is counterintuitive, but it might |
| help to imagine the indices pointing </FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM>between</EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3> the | | help to imagine the indices pointing ''between'' the |
| characters, as in the following diagram:</FONT></FONT></P><DIV CLASS="center"><FONT COLOR=black><FONT SIZE=3><IMG SRC="book011.png"></FONT></FONT></DIV><P><FONT COLOR=black><FONT SIZE=3>If you omit the first index (before the colon), the slice starts at | | characters, as in the following diagram: |
| | <DIV CLASS="center"><IMG SRC="book011.png"></DIV> |
| | If you omit the first index (before the colon), the slice starts at |
| the beginning of the string. If you omit the second index, the slice | | the beginning of the string. If you omit the second index, the slice |
| goes to the end of the string:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> fruit = 'banana' | | goes to the end of the string: |
| | <PRE CLASS="verbatim">>>> fruit = 'banana' |
| >>> fruit[:3] | | >>> fruit[:3] |
| 'ban' | | 'ban' |
| >>> fruit[3:] | | >>> fruit[3:] |
| 'ana' | | 'ana' |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>If the first index is greater than or equal to the second the result
| | </PRE> |
| is an </FONT></FONT><FONT COLOR=black><FONT SIZE=3><B>empty string</B></FONT></FONT><FONT COLOR=black><FONT SIZE=3>, represented by two quotation marks:</FONT></FONT></P><P><A NAME="@default598"></A></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> fruit = 'banana' | | If the first index is greater than or equal to the second the result |
| | is an '''empty string''', represented by two quotation marks: |
| | |
| | <PRE CLASS="verbatim">>>> fruit = 'banana' |
| >>> fruit[3:3] | | >>> fruit[3:3] |
| '' | | '' |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>An empty string contains no characters and has length 0, but other
| | </PRE> |
| than that, it is the same as any other string.</FONT></FONT></P><DIV CLASS="theorem"><FONT COLOR=black><FONT SIZE=3><B>Exercise 3</B></FONT></FONT><FONT COLOR=black><FONT SIZE=3>  <EM> | | An empty string contains no characters and has length 0, but other |
| Given that </EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM><TT>fruit</TT></EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM> is a string, what does | | than that, it is the same as any other string. |
| </EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM><TT>fruit[:]</TT></EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM> mean?</EM></FONT></FONT><P><A NAME="@default599"></A><FONT COLOR=black><FONT SIZE=3><EM>
| | <DIV CLASS="theorem">'''Exercise 3'''  '' |
| </EM></FONT></FONT><A NAME="@default600"></A></P></DIV><H2 CLASS="section"><A NAME="toc89"></A><A NAME="htoc99"><FONT COLOR=black><FONT SIZE=3>8.5</FONT></FONT></A><FONT COLOR=black><FONT SIZE=3>  Strings are immutable</FONT></FONT></H2><P><FONT COLOR=black><FONT SIZE=3>
| | Given that ''''<TT>fruit</TT>'''' is a string, what does |
| </FONT></FONT><A NAME="@default601"></A><FONT COLOR=black><FONT SIZE=3>
| | ''''<TT>fruit[:]</TT>'''' mean?'' |
| </FONT></FONT><A NAME="@default602"></A><FONT COLOR=black><FONT SIZE=3>
| | '' |
| </FONT></FONT><A NAME="@default603"></A></P><P><FONT COLOR=black><FONT SIZE=3>It is tempting to use the </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>[]</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> operator on the left side of an
| | '' |
| | </DIV>=== 8.5  Strings are immutable === |
| | |
| | |
| | |
| | |
| | |
| | |
| | It is tempting to use the <TT>[]</TT> operator on the left side of an |
| assignment, with the intention of changing a character in a string. | | assignment, with the intention of changing a character in a string. |
| For example:</FONT></FONT></P><P><A NAME="@default604"></A><FONT COLOR=black><FONT SIZE=3> | | For example: |
| </FONT></FONT><A NAME="@default605"></A></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> greeting = 'Hello, world!'
| | |
| | |
| | |
| | <PRE CLASS="verbatim">>>> greeting = 'Hello, world!' |
| >>> greeting[0] = 'J' | | >>> greeting[0] = 'J' |
| TypeError: object does not support item assignment | | TypeError: object does not support item assignment |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>The “object” in this case is the string and the “item” is
| | </PRE> |
| the character you tried to assign. For now, an </FONT></FONT><FONT COLOR=black><FONT SIZE=3><B>object</B></FONT></FONT><FONT COLOR=black><FONT SIZE=3> is | | The “object” in this case is the string and the “item” is |
| | the character you tried to assign. For now, an '''object''' is |
| the same thing as a value, but we will refine that definition | | the same thing as a value, but we will refine that definition |
| later. An </FONT></FONT><FONT COLOR=black><FONT SIZE=3><B>item</B></FONT></FONT><FONT COLOR=black><FONT SIZE=3> is one of the values in a sequence.</FONT></FONT></P><P><A NAME="@default606"></A><FONT COLOR=black><FONT SIZE=3> | | later. An '''item''' is one of the values in a sequence. |
| </FONT></FONT><A NAME="@default607"></A><FONT COLOR=black><FONT SIZE=3>
| | |
| </FONT></FONT><A NAME="@default608"></A><FONT COLOR=black><FONT SIZE=3>
| | |
| </FONT></FONT><A NAME="@default609"></A></P><P><FONT COLOR=black><FONT SIZE=3>The reason for the error is that
| | |
| strings are </FONT></FONT><FONT COLOR=black><FONT SIZE=3><B>immutable</B></FONT></FONT><FONT COLOR=black><FONT SIZE=3>, which means you can’t change an | | |
| | |
| | |
| | The reason for the error is that |
| | strings are '''immutable''', which means you can’t change an |
| existing string. The best you can do is create a new string | | existing string. The best you can do is create a new string |
| that is a variation on the original:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> greeting = 'Hello, world!' | | that is a variation on the original: |
| | <PRE CLASS="verbatim">>>> greeting = 'Hello, world!' |
| >>> new_greeting = 'J' + greeting[1:] | | >>> new_greeting = 'J' + greeting[1:] |
| >>> print new_greeting | | >>> print new_greeting |
| Jello, world! | | Jello, world! |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>This example concatenates a new first letter onto
| | </PRE> |
| a slice of </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>greeting</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>. It has no effect on | | This example concatenates a new first letter onto |
| the original string.</FONT></FONT></P><P><A NAME="@default610"></A></P><H2 CLASS="section"><A NAME="toc90"></A><A NAME="htoc100"><FONT COLOR=black><FONT SIZE=3>8.6</FONT></FONT></A><FONT COLOR=black><FONT SIZE=3>  Searching</FONT></FONT></H2><P><FONT COLOR=black><FONT SIZE=3> | | a slice of <TT>greeting</TT>. It has no effect on |
| </FONT></FONT><A NAME="find"></A></P><P><FONT COLOR=black><FONT SIZE=3>What does the following function do?</FONT></FONT></P><P><A NAME="@default611"></A><FONT COLOR=black><FONT SIZE=3>
| | the original string. |
| </FONT></FONT><A NAME="@default612"></A></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>def find(word, letter):
| | |
| | === 8.6  Searching === |
| | |
| | |
| | |
| | |
| | What does the following function do? |
| | |
| | |
| | |
| | <PRE CLASS="verbatim">def find(word, letter): |
| index = 0 | | index = 0 |
| while index < len(word): | | while index < len(word): |
| Line 172: |
Line 260: |
| index = index + 1 | | index = index + 1 |
| return -1 | | return -1 |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>In a sense, </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>find</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> is the opposite of the </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>[]</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> operator.
| | </PRE> |
| | In a sense, <TT>find</TT> is the opposite of the <TT>[]</TT> operator. |
| Instead of taking an index and extracting the corresponding character, | | Instead of taking an index and extracting the corresponding character, |
| it takes a character and finds the index where that character | | it takes a character and finds the index where that character |
| appears. If the character is not found, the function returns </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>-1</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>.</FONT></FONT></P><P><FONT COLOR=black><FONT SIZE=3>This is the first example we have seen of a </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>return</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> statement | | appears. If the character is not found, the function returns <TT>-1</TT>. |
| inside a loop. If </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>word[index] == letter</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>, the function breaks | | |
| out of the loop and returns immediately.</FONT></FONT></P><P><FONT COLOR=black><FONT SIZE=3>If the character doesn’t appear in the string, the program | | This is the first example we have seen of a <TT>return</TT> statement |
| exits the loop normally and returns </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>-1</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>.</FONT></FONT></P><P><FONT COLOR=black><FONT SIZE=3>This pattern of computation—traversing a sequence and returning | | inside a loop. If <TT>word[index] == letter</TT>, the function breaks |
| when we find what we are looking for—is a called a </FONT></FONT><FONT COLOR=black><FONT SIZE=3><B>search</B></FONT></FONT><FONT COLOR=black><FONT SIZE=3>.</FONT></FONT></P><P><A NAME="@default613"></A><FONT COLOR=black><FONT SIZE=3> | | out of the loop and returns immediately. |
| </FONT></FONT><A NAME="@default614"></A><FONT COLOR=black><FONT SIZE=3>
| | |
| </FONT></FONT><A NAME="@default615"></A></P><DIV CLASS="theorem"><FONT COLOR=black><FONT SIZE=3><B>Exercise 4</B></FONT></FONT><FONT COLOR=black><FONT SIZE=3>  <EM>
| | If the character doesn’t appear in the string, the program |
| Modify </EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM><TT>find</TT></EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM> so that it has a | | exits the loop normally and returns <TT>-1</TT>. |
| third parameter, the index in </EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM><TT>word</TT></EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM> where it should start | | |
| | This pattern of computation—traversing a sequence and returning |
| | when we find what we are looking for—is a called a '''search'''. |
| | |
| | |
| | |
| | |
| | <DIV CLASS="theorem">'''Exercise 4'''  '' |
| | Modify ''''<TT>find</TT>'''' so that it has a |
| | third parameter, the index in ''''<TT>word</TT>'''' where it should start |
| looking. | | looking. |
| </EM></FONT></FONT></DIV><H2 CLASS="section"><A NAME="toc91"></A><A NAME="htoc101"><FONT COLOR=black><FONT SIZE=3>8.7</FONT></FONT></A><FONT COLOR=black><FONT SIZE=3>  Looping and counting</FONT></FONT></H2><P><FONT COLOR=black><FONT SIZE=3>
| | ''</DIV>=== 8.7  Looping and counting === |
| </FONT></FONT><A NAME="counter"></A></P><P><A NAME="@default616"></A><FONT COLOR=black><FONT SIZE=3>
| | |
| </FONT></FONT><A NAME="@default617"></A><FONT COLOR=black><FONT SIZE=3>
| | |
| </FONT></FONT><A NAME="@default618"></A><FONT COLOR=black><FONT SIZE=3>
| | |
| </FONT></FONT><A NAME="@default619"></A></P><P><FONT COLOR=black><FONT SIZE=3>The following program counts the number of times the letter </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>a</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>
| | |
| appears in a string:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>word = 'banana' | | |
| | |
| | |
| | |
| | |
| | The following program counts the number of times the letter <TT>a</TT> |
| | appears in a string: |
| | <PRE CLASS="verbatim">word = 'banana' |
| count = 0 | | count = 0 |
| for letter in word: | | for letter in word: |
| Line 196: |
Line 301: |
| count = count + 1 | | count = count + 1 |
| print count | | print count |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>This program demonstrates another pattern of computation called a </FONT></FONT><FONT COLOR=black><FONT SIZE=3><B>counter</B></FONT></FONT><FONT COLOR=black><FONT SIZE=3>. The variable </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>count</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> is initialized to 0 and then
| | </PRE> |
| incremented each time an </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>a</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> is found. | | This program demonstrates another pattern of computation called a '''counter'''. The variable <TT>count</TT> is initialized to 0 and then |
| When the loop exits, </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>count</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> | | incremented each time an <TT>a</TT> is found. |
| contains the result—the total number of </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>a</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>’s.</FONT></FONT></P><DIV CLASS="theorem"><FONT COLOR=black><FONT SIZE=3><B>Exercise 5</B></FONT></FONT><FONT COLOR=black><FONT SIZE=3>  <EM> | | When the loop exits, <TT>count</TT> |
| </EM></FONT></FONT><A NAME="@default620"></A><P><FONT COLOR=black><FONT SIZE=3><EM>Encapsulate this code in a function named </EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM><TT>count</TT></EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM>, and generalize it so that it accepts the string and the
| | contains the result—the total number of <TT>a</TT>’s. |
| | <DIV CLASS="theorem">'''Exercise 5'''  '' |
| | '' |
| | ''Encapsulate this code in a function named ''''<TT>count</TT>'''', and generalize it so that it accepts the string and the |
| letter as arguments. | | letter as arguments. |
| </EM></FONT></FONT></P></DIV><DIV CLASS="theorem"><FONT COLOR=black><FONT SIZE=3><B>Exercise 6</B></FONT></FONT><FONT COLOR=black><FONT SIZE=3>  <EM>
| | '' |
| | </DIV><DIV CLASS="theorem">'''Exercise 6'''  '' |
| Rewrite this function so that instead of | | Rewrite this function so that instead of |
| traversing the string, it uses the three-parameter version of </EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM><TT>find</TT></EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM> from the previous section. | | traversing the string, it uses the three-parameter version of ''''<TT>find</TT>'''' from the previous section. |
| </EM></FONT></FONT></DIV><H2 CLASS="section"><A NAME="toc92"></A><A NAME="htoc102"><FONT COLOR=black><FONT SIZE=3>8.8</FONT></FONT></A><FONT COLOR=black><FONT SIZE=3>  </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>string</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> methods</FONT></FONT></H2><P><FONT COLOR=black><FONT SIZE=3>A </FONT></FONT><FONT COLOR=black><FONT SIZE=3><B>method</B></FONT></FONT><FONT COLOR=black><FONT SIZE=3> is similar to a function—it takes arguments and
| | ''</DIV>=== 8.8  <TT>string</TT> methods === |
| | |
| | A '''method''' is similar to a function—it takes arguments and |
| returns a value—but the syntax is different. For example, the | | returns a value—but the syntax is different. For example, the |
| method </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>upper</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> takes a string and returns a new string with | | method <TT>upper</TT> takes a string and returns a new string with |
| all uppercase letters:</FONT></FONT></P><P><A NAME="@default621"></A><FONT COLOR=black><FONT SIZE=3> | | all uppercase letters: |
| </FONT></FONT><A NAME="@default622"></A></P><P><FONT COLOR=black><FONT SIZE=3>Instead of the function syntax </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>upper(word)</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>, it uses
| | |
| the method syntax </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>word.upper()</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>.</FONT></FONT></P><P><A NAME="@default623"></A></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> word = 'banana' | | |
| | |
| | |
| | Instead of the function syntax <TT>upper(word)</TT>, it uses |
| | the method syntax <TT>word.upper()</TT>. |
| | |
| | <PRE CLASS="verbatim">>>> word = 'banana' |
| >>> new_word = word.upper() | | >>> new_word = word.upper() |
| >>> print new_word | | >>> print new_word |
| BANANA | | BANANA |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>This form of dot notation specifies the name of the method, </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>upper</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>, and the name of the string to apply the method to, </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>word</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>. The empty parentheses indicate that this method takes no
| | </PRE> |
| argument.</FONT></FONT></P><P><A NAME="@default624"></A></P><P><FONT COLOR=black><FONT SIZE=3>A method call is called an </FONT></FONT><FONT COLOR=black><FONT SIZE=3><B>invocation</B></FONT></FONT><FONT COLOR=black><FONT SIZE=3>; in this case, we would | | This form of dot notation specifies the name of the method, <TT>upper</TT>, and the name of the string to apply the method to, <TT>word</TT>. The empty parentheses indicate that this method takes no |
| say that we are invoking </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>upper</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> on the </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>word</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>.</FONT></FONT></P><P><A NAME="@default625"></A></P><P><FONT COLOR=black><FONT SIZE=3>As it turns out, there is a string method named </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>find</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> that | | argument. |
| is remarkably similar to the function we wrote:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> word = 'banana' | | |
| | A method call is called an '''invocation'''; in this case, we would |
| | say that we are invoking <TT>upper</TT> on the <TT>word</TT>. |
| | |
| | As it turns out, there is a string method named <TT>find</TT> that |
| | is remarkably similar to the function we wrote: |
| | <PRE CLASS="verbatim">>>> word = 'banana' |
| >>> index = word.find('a') | | >>> index = word.find('a') |
| >>> print index | | >>> print index |
| 1 | | 1 |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>In this example, we invoke </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>find</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> on </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>word</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> and pass
| | </PRE> |
| the letter we are looking for as a parameter.</FONT></FONT></P><P><FONT COLOR=black><FONT SIZE=3>Actually, the </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>find</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> method is more general than our function; | | In this example, we invoke <TT>find</TT> on <TT>word</TT> and pass |
| it can find substrings, not just characters:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> word.find('na') | | the letter we are looking for as a parameter. |
| | |
| | Actually, the <TT>find</TT> method is more general than our function; |
| | it can find substrings, not just characters: |
| | <PRE CLASS="verbatim">>>> word.find('na') |
| 2 | | 2 |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>It can take as a second argument the index where it should start:</FONT></FONT></P><P><A NAME="@default626"></A><FONT COLOR=black><FONT SIZE=3>
| | </PRE> |
| </FONT></FONT><A NAME="@default627"></A></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> word.find('na', 3)
| | It can take as a second argument the index where it should start: |
| | |
| | |
| | |
| | <PRE CLASS="verbatim">>>> word.find('na', 3) |
| 4 | | 4 |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>And as a third argument the index where it should stop:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> name = 'bob'
| | </PRE> |
| | And as a third argument the index where it should stop: |
| | <PRE CLASS="verbatim">>>> name = 'bob' |
| >>> name.find('b', 1, 2) | | >>> name.find('b', 1, 2) |
| -1 | | -1 |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>This search fails because </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>b</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> does not
| | </PRE> |
| appear in the index range from </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>1</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> to </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>2</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> (not including </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>2</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>).</FONT></FONT></P><DIV CLASS="theorem"><FONT COLOR=black><FONT SIZE=3><B>Exercise 7</B></FONT></FONT><FONT COLOR=black><FONT SIZE=3>  <EM> | | This search fails because <TT>b</TT> does not |
| </EM></FONT></FONT><A NAME="@default628"></A><FONT COLOR=black><FONT SIZE=3><EM>
| | appear in the index range from <TT>1</TT> to <TT>2</TT> (not including <TT>2</TT>). |
| </EM></FONT></FONT><A NAME="@default629"></A><P><FONT COLOR=black><FONT SIZE=3><EM>There is a string method called </EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM><TT>count</TT></EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM> that is similar
| | <DIV CLASS="theorem">'''Exercise 7'''  '' |
| | '''' |
| | '' |
| | ''There is a string method called ''''<TT>count</TT>'''' that is similar |
| to the function in the previous exercise. Read the documentation | | to the function in the previous exercise. Read the documentation |
| of this method | | of this method |
| and write an invocation that counts the number of </EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM><TT>a</TT></EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM>s | | and write an invocation that counts the number of ''''<TT>a</TT>''''s |
| in </EM></FONT></FONT><CODE><FONT COLOR=black><FONT SIZE=3><EM>'banana'</EM></FONT></FONT></CODE><FONT COLOR=black><FONT SIZE=3><EM>. | | in ''<CODE>'''banana'''</CODE>''. |
| </EM></FONT></FONT></P></DIV><H2 CLASS="section"><A NAME="toc93"></A><A NAME="htoc103"><FONT COLOR=black><FONT SIZE=3>8.9</FONT></FONT></A><FONT COLOR=black><FONT SIZE=3>  The </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>in</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> operator</FONT></FONT></H2><P><FONT COLOR=black><FONT SIZE=3>
| | '' |
| </FONT></FONT><A NAME="inboth"></A></P><P><A NAME="@default630"></A><FONT COLOR=black><FONT SIZE=3>
| | </DIV>=== 8.9  The <TT>in</TT> operator === |
| </FONT></FONT><A NAME="@default631"></A><FONT COLOR=black><FONT SIZE=3>
| | |
| </FONT></FONT><A NAME="@default632"></A><FONT COLOR=black><FONT SIZE=3>
| | |
| </FONT></FONT><A NAME="@default633"></A></P><P><FONT COLOR=black><FONT SIZE=3>The word </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>in</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> is a boolean operator that takes two strings and
| | |
| returns </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>True</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> if the first appears as a substring in the second:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> 'a' in 'banana' | | |
| | |
| | |
| | |
| | |
| | |
| | The word <TT>in</TT> is a boolean operator that takes two strings and |
| | returns <TT>True</TT> if the first appears as a substring in the second: |
| | <PRE CLASS="verbatim">>>> 'a' in 'banana' |
| True | | True |
| >>> 'seed' in 'banana' | | >>> 'seed' in 'banana' |
| False | | False |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>For example, the following function prints all the
| | </PRE> |
| letters from </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>word1</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> that also appear in </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>word2</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>def in_both(word1, word2): | | For example, the following function prints all the |
| | letters from <TT>word1</TT> that also appear in <TT>word2</TT>: |
| | <PRE CLASS="verbatim">def in_both(word1, word2): |
| for letter in word1: | | for letter in word1: |
| if letter in word2: | | if letter in word2: |
| print letter | | print letter |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>With well-chosen variable names,
| | </PRE> |
| | With well-chosen variable names, |
| Python sometimes reads like English. You could read | | Python sometimes reads like English. You could read |
| this loop, “for (each) letter in (the first) word, if (the) letter | | this loop, “for (each) letter in (the first) word, if (the) letter |
| (appears) in (the second) word, print (the) letter.”</FONT></FONT></P><P><FONT COLOR=black><FONT SIZE=3>Here’s what you get if you compare apples and oranges:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> in_both('apples', 'oranges') | | (appears) in (the second) word, print (the) letter.” |
| | |
| | Here’s what you get if you compare apples and oranges: |
| | <PRE CLASS="verbatim">>>> in_both('apples', 'oranges') |
| a | | a |
| e | | e |
| s | | s |
| </FONT></FONT></PRE><H2 CLASS="section"><A NAME="toc94"></A><A NAME="htoc104"><FONT COLOR=black><FONT SIZE=3>8.10</FONT></FONT></A><FONT COLOR=black><FONT SIZE=3>  String comparison</FONT></FONT></H2><P><A NAME="@default634"></A><FONT COLOR=black><FONT SIZE=3>
| | </PRE>=== 8.10  String comparison === |
| </FONT></FONT><A NAME="@default635"></A></P><P><FONT COLOR=black><FONT SIZE=3>The comparison operators work on strings. To see if two strings are equal:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>if word == 'banana':
| | |
| | |
| | |
| | |
| | The comparison operators work on strings. To see if two strings are equal: |
| | <PRE CLASS="verbatim">if word == 'banana': |
| print 'All right, bananas.' | | print 'All right, bananas.' |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>Other comparison operations are useful for putting words in alphabetical
| | </PRE> |
| order:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>if word < 'banana': | | Other comparison operations are useful for putting words in alphabetical |
| | order: |
| | <PRE CLASS="verbatim">if word < 'banana': |
| print 'Your word,' + word + ', comes before banana.' | | print 'Your word,' + word + ', comes before banana.' |
| elif word > 'banana': | | elif word > 'banana': |
| Line 270: |
Line 427: |
| else: | | else: |
| print 'All right, bananas.' | | print 'All right, bananas.' |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>Python does not handle uppercase and lowercase letters the same way
| | </PRE> |
| | Python does not handle uppercase and lowercase letters the same way |
| that people do. All the uppercase letters come before all the | | that people do. All the uppercase letters come before all the |
| lowercase letters, so:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>Your word, Pineapple, comes before banana. | | lowercase letters, so: |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>A common way to address this problem is to convert strings to a
| | <PRE CLASS="verbatim">Your word, Pineapple, comes before banana. |
| | </PRE> |
| | A common way to address this problem is to convert strings to a |
| standard format, such as all lowercase, before performing the | | standard format, such as all lowercase, before performing the |
| comparison. Keep that in mind in case you have to defend yourself | | comparison. Keep that in mind in case you have to defend yourself |
| against a man armed with a Pineapple.</FONT></FONT></P><H2 CLASS="section"><A NAME="toc95"></A><A NAME="htoc105"><FONT COLOR=black><FONT SIZE=3>8.11</FONT></FONT></A><FONT COLOR=black><FONT SIZE=3>  Debugging</FONT></FONT></H2><P><FONT COLOR=black><FONT SIZE=3> | | against a man armed with a Pineapple. |
| </FONT></FONT><A NAME="@default636"></A></P><P><A NAME="@default637"></A></P><P><FONT COLOR=black><FONT SIZE=3>When you use indices to traverse the values in a sequence,
| | === 8.11  Debugging === |
| | |
| | |
| | |
| | |
| | When you use indices to traverse the values in a sequence, |
| it is tricky to get the beginning and end of the traversal | | it is tricky to get the beginning and end of the traversal |
| right. Here is a function that is supposed to compare two | | right. Here is a function that is supposed to compare two |
| words and return </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>True</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> if one of the words is the reverse | | words and return <TT>True</TT> if one of the words is the reverse |
| of the other, but it contains two errors:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>def is_reverse(word1, word2): | | of the other, but it contains two errors: |
| | <PRE CLASS="verbatim">def is_reverse(word1, word2): |
| if len(word1) != len(word2): | | if len(word1) != len(word2): |
| return False | | return False |
| Line 295: |
Line 461: |
|
| |
|
| return True | | return True |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>The first </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>if</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> statement checks whether the words are the
| | </PRE> |
| same length. If not, we can return </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>False</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> immediately | | The first <TT>if</TT> statement checks whether the words are the |
| | same length. If not, we can return <TT>False</TT> immediately |
| and then, for the rest of the function, we can assume that the words | | and then, for the rest of the function, we can assume that the words |
| are the same length. This is an example of the guardian pattern | | are the same length. This is an example of the guardian pattern |
| in Section </FONT></FONT><A HREF="book007.html#guardian"><FONT COLOR=black><FONT SIZE=3>6.8</FONT></FONT></A><FONT COLOR=black><FONT SIZE=3>.</FONT></FONT></P><P><A NAME="@default638"></A><FONT COLOR=black><FONT SIZE=3> | | in Section 6.8. |
| </FONT></FONT><A NAME="@default639"></A><FONT COLOR=black><FONT SIZE=3>
| | |
| </FONT></FONT><A NAME="@default640"></A></P><P><FONT COLOR=black><FONT SIZE=3><TT>i</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> and </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>j</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> are indices: </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>i</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> traverses </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>word1</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>
| | |
| forward while </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>j</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> traverses </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>word2</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> backward. If we find | | |
| two letters that don’t match, we can return </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>False</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> immediately. | | |
| | |
| | <TT>i</TT> and <TT>j</TT> are indices: <TT>i</TT> traverses <TT>word1</TT> |
| | forward while <TT>j</TT> traverses <TT>word2</TT> backward. If we find |
| | two letters that don’t match, we can return <TT>False</TT> immediately. |
| If we get through the whole loop and all the letters match, we | | If we get through the whole loop and all the letters match, we |
| return </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>True</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>.</FONT></FONT></P><P><FONT COLOR=black><FONT SIZE=3>If we test this function with the words “pots” and “stop”, we | | return <TT>True</TT>. |
| expect the return value </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>True</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>, but we get an IndexError:</FONT></FONT></P><P><A NAME="@default641"></A><FONT COLOR=black><FONT SIZE=3> | | |
| </FONT></FONT><A NAME="@default642"></A></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> is_reverse('pots', 'stop')
| | If we test this function with the words “pots” and “stop”, we |
| | expect the return value <TT>True</TT>, but we get an IndexError: |
| | |
| | |
| | |
| | <PRE CLASS="verbatim">>>> is_reverse('pots', 'stop') |
| ... | | ... |
| File "reverse.py", line 15, in is_reverse | | File "reverse.py", line 15, in is_reverse |
| if word1[i] != word2[j]: | | if word1[i] != word2[j]: |
| IndexError: string index out of range | | IndexError: string index out of range |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>For debugging this kind of error, my first move is to
| | </PRE> |
| | For debugging this kind of error, my first move is to |
| print the values of the indices immediately before the line | | print the values of the indices immediately before the line |
| where the error appears.</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4> while j > 0: | | where the error appears. |
| | <PRE CLASS="verbatim"> while j > 0: |
| print i, j # print here | | print i, j # print here |
| | | |
| Line 321: |
Line 499: |
| i = i+1 | | i = i+1 |
| j = j-1 | | j = j-1 |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>Now when I run the program again, I get more information:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> is_reverse('pots', 'stop')
| | </PRE> |
| | Now when I run the program again, I get more information: |
| | <PRE CLASS="verbatim">>>> is_reverse('pots', 'stop') |
| 0 4 | | 0 4 |
| ... | | ... |
| IndexError: string index out of range | | IndexError: string index out of range |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>The first time through the loop, the value of </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>j</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> is 4,
| | </PRE> |
| which is out of range for the string </FONT></FONT><CODE><FONT COLOR=black><FONT SIZE=3>'pots'</FONT></FONT></CODE><FONT COLOR=black><FONT SIZE=3>. | | The first time through the loop, the value of <TT>j</TT> is 4, |
| | which is out of range for the string <CODE>'pots'</CODE>. |
| The index of the last character is 3, so the | | The index of the last character is 3, so the |
| initial value for </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>j</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> should be </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>len(word2)-1</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>.</FONT></FONT></P><P><A NAME="@default643"></A><FONT COLOR=black><FONT SIZE=3> | | initial value for <TT>j</TT> should be <TT>len(word2)-1</TT>. |
| </FONT></FONT><A NAME="@default644"></A></P><P><FONT COLOR=black><FONT SIZE=3>If I fix that error and run the program again, I get:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> is_reverse('pots', 'stop')
| | |
| | |
| | |
| | |
| | If I fix that error and run the program again, I get: |
| | <PRE CLASS="verbatim">>>> is_reverse('pots', 'stop') |
| 0 3 | | 0 3 |
| 1 2 | | 1 2 |
| 2 1 | | 2 1 |
| True | | True |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>This time we get the right answer, but it looks like the loop only ran
| | </PRE> |
| | This time we get the right answer, but it looks like the loop only ran |
| three times, which is suspicious. To get a better idea of what is | | three times, which is suspicious. To get a better idea of what is |
| happening, it is useful to draw a state diagram. During the first | | happening, it is useful to draw a state diagram. During the first |
| iteration, the frame for </FONT></FONT><CODE><FONT COLOR=black><FONT SIZE=3>is_reverse</FONT></FONT></CODE><FONT COLOR=black><FONT SIZE=3> looks like this:</FONT></FONT></P><P><A NAME="@default645"></A><FONT COLOR=black><FONT SIZE=3> | | iteration, the frame for <CODE>is_reverse</CODE> looks like this: |
| </FONT></FONT><A NAME="@default646"></A></P><DIV CLASS="center"><FONT COLOR=black><FONT SIZE=3><IMG SRC="book012.png"></FONT></FONT></DIV><P><FONT COLOR=black><FONT SIZE=3>I took a little license by arranging the variables in the frame
| | |
| and adding dotted lines to show that the values of </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>i</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> and | | |
| </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>j</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> indicate characters in </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>word1</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> and </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>word2</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>.</FONT></FONT></P><DIV CLASS="theorem"><FONT COLOR=black><FONT SIZE=3><B>Exercise 8</B></FONT></FONT><FONT COLOR=black><FONT SIZE=3>  <EM>
| | |
| </EM></FONT></FONT><A NAME="is_reverse"></A><FONT COLOR=black><FONT SIZE=3><EM>
| | <DIV CLASS="center"><IMG SRC="book012.png"></DIV> |
| | I took a little license by arranging the variables in the frame |
| | and adding dotted lines to show that the values of <TT>i</TT> and |
| | <TT>j</TT> indicate characters in <TT>word1</TT> and <TT>word2</TT>. |
| | <DIV CLASS="theorem">'''Exercise 8'''  '' |
| | '''' |
| Starting with this diagram, execute the program on paper, changing the | | Starting with this diagram, execute the program on paper, changing the |
| values of </EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM><TT>i</TT></EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM> and </EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM><TT>j</TT></EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM> during each iteration. Find and fix the | | values of ''''<TT>i</TT>'''' and ''''<TT>j</TT>'''' during each iteration. Find and fix the |
| second error in this function. | | second error in this function. |
| </EM></FONT></FONT></DIV><H2 CLASS="section"><A NAME="toc96"></A><A NAME="htoc106"><FONT COLOR=black><FONT SIZE=3>8.12</FONT></FONT></A><FONT COLOR=black><FONT SIZE=3>  Glossary</FONT></FONT></H2><DL CLASS="description"><DT CLASS="dt-description"><FONT COLOR=black><FONT SIZE=3><B>object:</B></FONT></FONT></DT><DD CLASS="dd-description"><FONT COLOR=black><FONT SIZE=3> Something a variable can refer to. For now,
| | ''</DIV>=== 8.12  Glossary === |
| | |
| | <DL CLASS="description"><DT CLASS="dt-description">'''object:'''</DT><DD CLASS="dd-description"> Something a variable can refer to. For now, |
| you can use “object” and “value” interchangeably. | | you can use “object” and “value” interchangeably. |
| </FONT></FONT><A NAME="@default647"></A></DD><DT CLASS="dt-description"><FONT COLOR=black><FONT SIZE=3><B>sequence:</B></FONT></FONT></DT><DD CLASS="dd-description"><FONT COLOR=black><FONT SIZE=3> An ordered set; that is, a set of
| | </DD><DT CLASS="dt-description">'''sequence:'''</DT><DD CLASS="dd-description"> An ordered set; that is, a set of |
| values where each value is identified by an integer index. | | values where each value is identified by an integer index. |
| </FONT></FONT><A NAME="@default648"></A></DD><DT CLASS="dt-description"><FONT COLOR=black><FONT SIZE=3><B>item:</B></FONT></FONT></DT><DD CLASS="dd-description"><FONT COLOR=black><FONT SIZE=3> One of the values in a sequence.
| | </DD><DT CLASS="dt-description">'''item:'''</DT><DD CLASS="dd-description"> One of the values in a sequence. |
| </FONT></FONT><A NAME="@default649"></A></DD><DT CLASS="dt-description"><FONT COLOR=black><FONT SIZE=3><B>index:</B></FONT></FONT></DT><DD CLASS="dd-description"><FONT COLOR=black><FONT SIZE=3> An integer value used to select an item in
| | </DD><DT CLASS="dt-description">'''index:'''</DT><DD CLASS="dd-description"> An integer value used to select an item in |
| a sequence, such as a character in a string. | | a sequence, such as a character in a string. |
| </FONT></FONT><A NAME="@default650"></A></DD><DT CLASS="dt-description"><FONT COLOR=black><FONT SIZE=3><B>slice:</B></FONT></FONT></DT><DD CLASS="dd-description"><FONT COLOR=black><FONT SIZE=3> A part of a string specified by a range of indices.
| | </DD><DT CLASS="dt-description">'''slice:'''</DT><DD CLASS="dd-description"> A part of a string specified by a range of indices. |
| </FONT></FONT><A NAME="@default651"></A></DD><DT CLASS="dt-description"><FONT COLOR=black><FONT SIZE=3><B>empty string:</B></FONT></FONT></DT><DD CLASS="dd-description"><FONT COLOR=black><FONT SIZE=3> A string with no characters and length 0, represented
| | </DD><DT CLASS="dt-description">'''empty string:'''</DT><DD CLASS="dd-description"> A string with no characters and length 0, represented |
| by two quotation marks. | | by two quotation marks. |
| </FONT></FONT><A NAME="@default652"></A></DD><DT CLASS="dt-description"><FONT COLOR=black><FONT SIZE=3><B>immutable:</B></FONT></FONT></DT><DD CLASS="dd-description"><FONT COLOR=black><FONT SIZE=3> The property of a sequence whose items cannot
| | </DD><DT CLASS="dt-description">'''immutable:'''</DT><DD CLASS="dd-description"> The property of a sequence whose items cannot |
| be assigned. | | be assigned. |
| </FONT></FONT><A NAME="@default653"></A></DD><DT CLASS="dt-description"><FONT COLOR=black><FONT SIZE=3><B>traverse:</B></FONT></FONT></DT><DD CLASS="dd-description"><FONT COLOR=black><FONT SIZE=3> To iterate through the items in a sequence,
| | </DD><DT CLASS="dt-description">'''traverse:'''</DT><DD CLASS="dd-description"> To iterate through the items in a sequence, |
| performing a similar operation on each. | | performing a similar operation on each. |
| </FONT></FONT><A NAME="@default654"></A></DD><DT CLASS="dt-description"><FONT COLOR=black><FONT SIZE=3><B>search:</B></FONT></FONT></DT><DD CLASS="dd-description"><FONT COLOR=black><FONT SIZE=3> A pattern of traversal that stops
| | </DD><DT CLASS="dt-description">'''search:'''</DT><DD CLASS="dd-description"> A pattern of traversal that stops |
| when it finds what it is looking for. | | when it finds what it is looking for. |
| </FONT></FONT><A NAME="@default655"></A><FONT COLOR=black><FONT SIZE=3>
| | |
| </FONT></FONT><A NAME="@default656"></A></DD><DT CLASS="dt-description"><FONT COLOR=black><FONT SIZE=3><B>counter:</B></FONT></FONT></DT><DD CLASS="dd-description"><FONT COLOR=black><FONT SIZE=3> A variable used to count something, usually initialized
| | </DD><DT CLASS="dt-description">'''counter:'''</DT><DD CLASS="dd-description"> A variable used to count something, usually initialized |
| to zero and then incremented. | | to zero and then incremented. |
| </FONT></FONT><A NAME="@default657"></A></DD><DT CLASS="dt-description"><FONT COLOR=black><FONT SIZE=3><B>method:</B></FONT></FONT></DT><DD CLASS="dd-description"><FONT COLOR=black><FONT SIZE=3> A function that is associated with an object and called
| | </DD><DT CLASS="dt-description">'''method:'''</DT><DD CLASS="dd-description"> A function that is associated with an object and called |
| using dot notation. | | using dot notation. |
| </FONT></FONT><A NAME="@default658"></A></DD><DT CLASS="dt-description"><FONT COLOR=black><FONT SIZE=3><B>invocation:</B></FONT></FONT></DT><DD CLASS="dd-description"><FONT COLOR=black><FONT SIZE=3> A statement that calls a method.
| | </DD><DT CLASS="dt-description">'''invocation:'''</DT><DD CLASS="dd-description"> A statement that calls a method. |
| </FONT></FONT><A NAME="@default659"></A></DD></DL><H2 CLASS="section"><A NAME="toc97"></A><A NAME="htoc107"><FONT COLOR=black><FONT SIZE=3>8.13</FONT></FONT></A><FONT COLOR=black><FONT SIZE=3>  Exercises</FONT></FONT></H2><DIV CLASS="theorem"><FONT COLOR=black><FONT SIZE=3><B>Exercise 9</B></FONT></FONT><FONT COLOR=black><FONT SIZE=3>  </FONT></FONT><P><A NAME="@default660"></A><FONT COLOR=black><FONT SIZE=3><EM>
| | </DD></DL>=== 8.13  Exercises === |
| </EM></FONT></FONT><A NAME="@default661"></A><FONT COLOR=black><FONT SIZE=3><EM>
| | |
| </EM></FONT></FONT><A NAME="@default662"></A></P><P><FONT COLOR=black><FONT SIZE=3><EM>A string slice can take a third index that specifies the “step
| | <DIV CLASS="theorem">'''Exercise 9'''   |
| | '' |
| | '''' |
| | '' |
| | |
| | ''A string slice can take a third index that specifies the “step |
| size;” that is, the number of spaces between successive characters. | | size;” that is, the number of spaces between successive characters. |
| A step size of 2 means every other character; 3 means every third, | | A step size of 2 means every other character; 3 means every third, |
| etc.</EM></FONT></FONT></P><PRE CLASS="verbatim"><EM><FONT COLOR=blue><FONT SIZE=4>>>> fruit = 'banana' | | etc.'' |
| | <PRE CLASS="verbatim">''>>> fruit = 'banana' |
| >>> fruit[0:5:2] | | >>> fruit[0:5:2] |
| 'bnn' | | 'bnn' |
| </FONT></FONT></EM></PRE><P><EM><FONT COLOR=black><FONT SIZE=3>A step size of -1 goes through the word backwards, so
| | ''</PRE> |
| the slice </FONT></FONT></EM><CODE><EM><FONT COLOR=black><FONT SIZE=3>[::-1]</FONT></FONT></EM></CODE><EM><FONT COLOR=black><FONT SIZE=3> generates a reversed string.</FONT></FONT></EM></P><P><A NAME="@default663"></A></P><P><EM><FONT COLOR=black><FONT SIZE=3>Use this idiom to write a one-line version of </FONT></FONT></EM><CODE><EM><FONT COLOR=black><FONT SIZE=3>is_palindrome</FONT></FONT></EM></CODE><EM><FONT COLOR=black><FONT SIZE=3> | | ''A step size of -1 goes through the word backwards, so |
| from Exercise </FONT></FONT></EM><A HREF="book007.html#palindrome"><EM><FONT COLOR=black><FONT SIZE=3>6.6</FONT></FONT></EM></A><EM><FONT COLOR=black><FONT SIZE=3>. | | the slice ''<CODE>''[::-1]''</CODE>'' generates a reversed string.'' |
| </FONT></FONT></EM></P></DIV><DIV CLASS="theorem"><FONT COLOR=black><FONT SIZE=3><B>Exercise 10</B></FONT></FONT><FONT COLOR=black><FONT SIZE=3>  <EM>
| | |
| </EM></FONT></FONT><A NAME="@default664"></A><FONT COLOR=black><FONT SIZE=3><EM>
| | ''Use this idiom to write a one-line version of ''<CODE>''is_palindrome''</CODE>'' |
| </EM></FONT></FONT><A NAME="@default665"></A><P><FONT COLOR=black><FONT SIZE=3><EM>Read the documentation of the string methods at
| | from Exercise ''''6.6''''. |
| </EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM><TT>docs.python.org/lib/string-methods.html</TT></EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM>. You
| | '' |
| | </DIV><DIV CLASS="theorem">'''Exercise 10'''  '' |
| | '''' |
| | '' |
| | ''Read the documentation of the string methods at |
| | ''''<TT>docs.python.org/lib/string-methods.html</TT>''''. You |
| might want to experiment with some of them to make sure | | might want to experiment with some of them to make sure |
| you understand how they work. </EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM><TT>strip</TT></EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM> and | | you understand how they work. ''''<TT>strip</TT>'''' and |
| </EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM><TT>replace</TT></EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM> are particularly useful.</EM></FONT></FONT></P><P><FONT COLOR=black><FONT SIZE=3><EM>The documentation uses a syntax that might be confusing.
| | ''''<TT>replace</TT>'''' are particularly useful.'' |
| For example, in </EM></FONT></FONT><CODE><FONT COLOR=black><FONT SIZE=3><EM>find(sub[, start[, end]])</EM></FONT></FONT></CODE><FONT COLOR=black><FONT SIZE=3><EM>, the brackets | | |
| indicate optional arguments. So </EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM><TT>sub</TT></EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM> is required, but | | ''The documentation uses a syntax that might be confusing. |
| </EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM><TT>start</TT></EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM> is optional, and if you include </EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM><TT>start</TT></EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM>,
| | For example, in ''<CODE>''find(sub[, start[, end]])''</CODE>'', the brackets |
| then </EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM><TT>end</TT></EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM> is optional. | | indicate optional arguments. So ''''<TT>sub</TT>'''' is required, but |
| </EM></FONT></FONT></P></DIV><DIV CLASS="theorem"><FONT COLOR=black><FONT SIZE=3><B>Exercise 11</B></FONT></FONT><FONT COLOR=black><FONT SIZE=3>  <EM>
| | ''''<TT>start</TT>'''' is optional, and if you include ''''<TT>start</TT>'''', |
| The following functions are all </EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3>intended</FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM> to check whether a | | then ''''<TT>end</TT>'''' is optional. |
| | '' |
| | </DIV><DIV CLASS="theorem">'''Exercise 11'''  '' |
| | The following functions are all ''intended'' to check whether a |
| string contains any lowercase letters, but at least some of them are | | string contains any lowercase letters, but at least some of them are |
| wrong. For each function, describe what the function actually does.</EM></FONT></FONT><PRE CLASS="verbatim"><EM><FONT COLOR=blue><FONT SIZE=4>def any_lowercase1(s): | | wrong. For each function, describe what the function actually does.''<PRE CLASS="verbatim">''def any_lowercase1(s): |
| for c in s: | | for c in s: |
| if c.islower(): | | if c.islower(): |
| Line 422: |
Line 630: |
| return False | | return False |
| return True | | return True |
| </FONT></FONT></EM></PRE></DIV><DIV CLASS="theorem"><FONT COLOR=black><FONT SIZE=3><B>Exercise 12</B></FONT></FONT><FONT COLOR=black><FONT SIZE=3>  <EM>
| | ''</PRE></DIV><DIV CLASS="theorem">'''Exercise 12'''  '' |
| </EM></FONT></FONT><A NAME="@default666"></A><FONT COLOR=black><FONT SIZE=3><EM>
| | '''' |
| </EM></FONT></FONT><A NAME="@default667"></A><P><A NAME="exrotate"></A><FONT COLOR=black><FONT SIZE=3><EM>
| | '' |
| | '' |
| ROT13 is a weak form of encryption that involves “rotating” each | | ROT13 is a weak form of encryption that involves “rotating” each |
| letter in a word by 13 places</EM></FONT></FONT><SUP><A NAME="text15" HREF="#note15"><FONT COLOR=black><FONT SIZE=3><EM>1</EM></FONT></FONT></A></SUP><FONT COLOR=black><FONT SIZE=3><EM>. To rotate a letter means | | letter in a word by 13 places''<SUP>''1''</SUP>''. To rotate a letter means |
| to shift it through the alphabet, wrapping around to the beginning if | | to shift it through the alphabet, wrapping around to the beginning if |
| necessary, so ’A’ shifted by 3 is ’D’ and ’Z’ shifted by 1 is ’A’.</EM></FONT></FONT></P><P><FONT COLOR=black><FONT SIZE=3><EM>Write a function called </EM></FONT></FONT><CODE><FONT COLOR=black><FONT SIZE=3><EM>rotate_word</EM></FONT></FONT></CODE><FONT COLOR=black><FONT SIZE=3><EM> | | necessary, so ’A’ shifted by 3 is ’D’ and ’Z’ shifted by 1 is ’A’.'' |
| | |
| | ''Write a function called ''<CODE>''rotate_word''</CODE>'' |
| that takes a string and an integer as parameters, and that returns | | that takes a string and an integer as parameters, and that returns |
| a new string that contains the letters from the original string | | a new string that contains the letters from the original string |
| “rotated” by the given amount. </EM></FONT></FONT></P><P><FONT COLOR=black><FONT SIZE=3><EM>For example, “cheer” rotated by 7 is “jolly” and “melon” rotated | | “rotated” by the given amount. '' |
| by -10 is “cubed”. </EM></FONT></FONT></P><P><FONT COLOR=black><FONT SIZE=3><EM>You might want to use the built-in functions </EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM><TT>ord</TT></EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM>, which converts | | |
| a character to a numeric code, and </EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM><TT>chr</TT></EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM>, which converts numeric | | ''For example, “cheer” rotated by 7 is “jolly” and “melon” rotated |
| codes to characters.</EM></FONT></FONT></P><P><FONT COLOR=black><FONT SIZE=3><EM>Potentially offensive jokes on the Internet are sometimes encoded | | by -10 is “cubed”. '' |
| | |
| | ''You might want to use the built-in functions ''''<TT>ord</TT>'''', which converts |
| | a character to a numeric code, and ''''<TT>chr</TT>'''', which converts numeric |
| | codes to characters.'' |
| | |
| | ''Potentially offensive jokes on the Internet are sometimes encoded |
| in ROT13. If you are not easily offended, find and decode some | | in ROT13. If you are not easily offended, find and decode some |
| of them. | | of them. |
| </EM></FONT></FONT></P></DIV><HR CLASS="footnoterule"><DL CLASS="thefootnotes"><DT CLASS="dt-thefootnotes"><FONT COLOR=black><FONT SIZE=3>
| | '' |
| </FONT></FONT><A NAME="note15" HREF="#text15"><FONT COLOR=black><FONT SIZE=3>1</FONT></FONT></A></DT><DD CLASS="dd-thefootnotes"><FONT COLOR=black><FONT SIZE=3>See
| | </DIV><HR CLASS="footnoterule"><DL CLASS="thefootnotes"><DT CLASS="dt-thefootnotes"> |
| | 1</DT><DD CLASS="dd-thefootnotes">See |
| <TT>wikipedia.org/wiki/ROT13</TT> | | <TT>wikipedia.org/wiki/ROT13</TT> |
| </FONT></FONT></DD></DL>
| | </DD></DL> |
| <HR> | | <HR> |
| <A HREF="book008.html"><IMG SRC="previous_motif.gif" ALT="Previous"></A>
| | <IMG SRC="previous_motif.gif" ALT="Previous"> |
| <A HREF="index.html"><IMG SRC="contents_motif.gif" ALT="Up"></A>
| | <IMG SRC="contents_motif.gif" ALT="Up"> |
| <A HREF="book010.html"><IMG SRC="next_motif.gif" ALT="Next"></A>
| | <IMG SRC="next_motif.gif" ALT="Next"> |
| </BODY>
| |
| </HTML>
| |