SEESenv/web/html/ch4strings_dicts.html
changeset 28 514098969b11
parent 27 cb14131583c6
equal deleted inserted replaced
27:cb14131583c6 28:514098969b11
     1 <html>
     1 <html>
     2  <head>
     2 <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Chapter. strings_dicts</title><link rel="stylesheet" href="/review/support/styles.css" type="text/css" /><meta name="generator" content="DocBook XSL Stylesheets V1.74.3" /><link rel="shortcut icon" type="image/png" href="/review/support/figs/favicon.png" /><script type="text/javascript" src="/review/support/jquery-min.js"></script><script type="text/javascript" src="/review/support/form.js"></script><script type="text/javascript" src="/review/support/hsbook.js"></script></head>
     3   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     3 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="chapter" id="ch4strings_dicts">
     4   <title>
     4 <div class="titlepage"></div>
     5    Chapter. strings_dicts
     5 <div class="toc">
     6   </title>
     6 <p><b>Table of Contents</b></p>
     7   <link rel="stylesheet" href="/review/support/styles.css" type="text/css" />
     7 <dl>
     8   <meta name="generator" content="DocBook XSL Stylesheets V1.74.3" />
     8 <dt><span class="article"><a href="#id2983086">Strings</a></span></dt>
     9   <link rel="shortcut icon" type="image/png" href="/review/support/figs/favicon.png" />
     9 <dd><dl>
    10   <script type="text/javascript" src="/review/support/jquery-min.js">
    10 <dt><span class="section"><a href="#id2981673">1. String Formatting</a></span></dt>
    11   </script>
    11 <dt><span class="section"><a href="#id3034090">2. String Methods</a></span></dt>
    12   <script type="text/javascript" src="/review/support/form.js">
    12 <dd><dl>
    13   </script>
    13 <dt><span class="section"><a href="#id3034105">2.1. <span class="strong"><strong>find</strong></span></a></span></dt>
    14   <script type="text/javascript" src="/review/support/hsbook.js">
    14 <dt><span class="section"><a href="#id3034135">2.2. <span class="strong"><strong>join</strong></span></a></span></dt>
    15   </script>
    15 <dt><span class="section"><a href="#id3034175">2.3. <span class="strong"><strong>lower</strong></span></a></span></dt>
    16  </head>
    16 <dt><span class="section"><a href="#id3034206">2.4. <span class="strong"><strong>replace</strong></span></a></span></dt>
    17  <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
    17 <dt><span class="section"><a href="#id3034262">2.5. <span class="strong"><strong>split</strong></span></a></span></dt>
    18   <div class="chapter" id="ch4strings_dicts">
    18 <dt><span class="section"><a href="#id3034321">2.6. <span class="strong"><strong>strip</strong></span></a></span></dt>
    19    <div class="titlepage">
    19 </dl></dd>
    20    </div>
    20 <dt><span class="section"><a href="#id3034366">3. Introduction to the standard library</a></span></dt>
    21    <div class="toc">
    21 <dt><span class="section"><a href="#id3034442">4. I/O: Reading and Writing Files</a></span></dt>
    22     <p>
    22 <dd><dl>
    23      <b>
    23 <dt><span class="section"><a href="#id3034472">4.1. Opening Files</a></span></dt>
    24       Table of Contents
    24 <dt><span class="section"><a href="#id3034646">4.2. Reading and Writing files</a></span></dt>
    25      </b>
    25 </dl></dd>
    26     </p>
    26 <dt><span class="section"><a href="#id3034811">5. Dictionaries</a></span></dt>
    27     <dl>
    27 <dd><dl>
    28      <dt>
    28 <dt><span class="section"><a href="#id3034868">5.1. <span class="strong"><strong>dict()</strong></span></a></span></dt>
    29       <span class="article">
    29 <dt><span class="section"><a href="#id3034932">5.2. Dictionary Methods</a></span></dt>
    30        <a href="#id2580313">
    30 </dl></dd>
    31         Strings
    31 </dl></dd>
    32        </a>
    32 </dl>
    33       </span>
    33 </div>
    34      </dt>
    34 <div class="article" title="Strings">
    35      <dd>
    35 <div class="titlepage">
    36       <dl>
    36 <div><div><h2 class="title">
    37        <dt>
    37 <a name="id2983086"></a>Strings</h2></div></div>
    38         <span class="section">
    38 <hr />
    39          <a href="#id2578900">
    39 </div>
    40           1. String Formatting
    40 <div class="toc">
    41          </a>
    41 <p><b>Table of Contents</b></p>
    42         </span>
    42 <dl>
    43        </dt>
    43 <dt><span class="section"><a href="#id2981673">1. String Formatting</a></span></dt>
    44        <dt>
    44 <dt><span class="section"><a href="#id3034090">2. String Methods</a></span></dt>
    45         <span class="section">
    45 <dd><dl>
    46          <a href="#id2631317">
    46 <dt><span class="section"><a href="#id3034105">2.1. <span class="strong"><strong>find</strong></span></a></span></dt>
    47           2. String Methods
    47 <dt><span class="section"><a href="#id3034135">2.2. <span class="strong"><strong>join</strong></span></a></span></dt>
    48          </a>
    48 <dt><span class="section"><a href="#id3034175">2.3. <span class="strong"><strong>lower</strong></span></a></span></dt>
    49         </span>
    49 <dt><span class="section"><a href="#id3034206">2.4. <span class="strong"><strong>replace</strong></span></a></span></dt>
    50        </dt>
    50 <dt><span class="section"><a href="#id3034262">2.5. <span class="strong"><strong>split</strong></span></a></span></dt>
    51        <dd>
    51 <dt><span class="section"><a href="#id3034321">2.6. <span class="strong"><strong>strip</strong></span></a></span></dt>
    52         <dl>
    52 </dl></dd>
    53          <dt>
    53 <dt><span class="section"><a href="#id3034366">3. Introduction to the standard library</a></span></dt>
    54           <span class="section">
    54 <dt><span class="section"><a href="#id3034442">4. I/O: Reading and Writing Files</a></span></dt>
    55            <a href="#id2631332">
    55 <dd><dl>
    56             2.1.
    56 <dt><span class="section"><a href="#id3034472">4.1. Opening Files</a></span></dt>
    57             <span class="strong">
    57 <dt><span class="section"><a href="#id3034646">4.2. Reading and Writing files</a></span></dt>
    58              <strong>
    58 </dl></dd>
    59               find
    59 <dt><span class="section"><a href="#id3034811">5. Dictionaries</a></span></dt>
    60              </strong>
    60 <dd><dl>
    61             </span>
    61 <dt><span class="section"><a href="#id3034868">5.1. <span class="strong"><strong>dict()</strong></span></a></span></dt>
    62            </a>
    62 <dt><span class="section"><a href="#id3034932">5.2. Dictionary Methods</a></span></dt>
    63           </span>
    63 </dl></dd>
    64          </dt>
    64 </dl>
    65          <dt>
    65 </div>
    66           <span class="section">
    66 <p id="ch4strings_dicts_1">Strings were briefly introduced previously in the introduction document. In this
    67            <a href="#id2631362">
       
    68             2.2.
       
    69             <span class="strong">
       
    70              <strong>
       
    71               join
       
    72              </strong>
       
    73             </span>
       
    74            </a>
       
    75           </span>
       
    76          </dt>
       
    77          <dt>
       
    78           <span class="section">
       
    79            <a href="#id2631402">
       
    80             2.3.
       
    81             <span class="strong">
       
    82              <strong>
       
    83               lower
       
    84              </strong>
       
    85             </span>
       
    86            </a>
       
    87           </span>
       
    88          </dt>
       
    89          <dt>
       
    90           <span class="section">
       
    91            <a href="#id2631432">
       
    92             2.4.
       
    93             <span class="strong">
       
    94              <strong>
       
    95               replace
       
    96              </strong>
       
    97             </span>
       
    98            </a>
       
    99           </span>
       
   100          </dt>
       
   101          <dt>
       
   102           <span class="section">
       
   103            <a href="#id2631489">
       
   104             2.5.
       
   105             <span class="strong">
       
   106              <strong>
       
   107               split
       
   108              </strong>
       
   109             </span>
       
   110            </a>
       
   111           </span>
       
   112          </dt>
       
   113          <dt>
       
   114           <span class="section">
       
   115            <a href="#id2631548">
       
   116             2.6.
       
   117             <span class="strong">
       
   118              <strong>
       
   119               strip
       
   120              </strong>
       
   121             </span>
       
   122            </a>
       
   123           </span>
       
   124          </dt>
       
   125         </dl>
       
   126        </dd>
       
   127        <dt>
       
   128         <span class="section">
       
   129          <a href="#id2631592">
       
   130           3. Introduction to the standard library
       
   131          </a>
       
   132         </span>
       
   133        </dt>
       
   134        <dt>
       
   135         <span class="section">
       
   136          <a href="#id2631669">
       
   137           4. I/O: Reading and Writing Files
       
   138          </a>
       
   139         </span>
       
   140        </dt>
       
   141        <dd>
       
   142         <dl>
       
   143          <dt>
       
   144           <span class="section">
       
   145            <a href="#id2631699">
       
   146             4.1. Opening Files
       
   147            </a>
       
   148           </span>
       
   149          </dt>
       
   150          <dt>
       
   151           <span class="section">
       
   152            <a href="#id2631873">
       
   153             4.2. Reading and Writing files
       
   154            </a>
       
   155           </span>
       
   156          </dt>
       
   157         </dl>
       
   158        </dd>
       
   159        <dt>
       
   160         <span class="section">
       
   161          <a href="#id2632038">
       
   162           5. Dictionaries
       
   163          </a>
       
   164         </span>
       
   165        </dt>
       
   166        <dd>
       
   167         <dl>
       
   168          <dt>
       
   169           <span class="section">
       
   170            <a href="#id2632094">
       
   171             5.1.
       
   172             <span class="strong">
       
   173              <strong>
       
   174               dict()
       
   175              </strong>
       
   176             </span>
       
   177            </a>
       
   178           </span>
       
   179          </dt>
       
   180          <dt>
       
   181           <span class="section">
       
   182            <a href="#id2632158">
       
   183             5.2. Dictionary Methods
       
   184            </a>
       
   185           </span>
       
   186          </dt>
       
   187         </dl>
       
   188        </dd>
       
   189       </dl>
       
   190      </dd>
       
   191     </dl>
       
   192    </div>
       
   193    <div class="article" title="Strings">
       
   194     <div class="titlepage">
       
   195      <div>
       
   196       <div>
       
   197        <h2 class="title">
       
   198         <a name="id2580313">
       
   199         </a>
       
   200         Strings
       
   201        </h2>
       
   202       </div>
       
   203      </div>
       
   204      <hr />
       
   205     </div>
       
   206     <div class="toc">
       
   207      <p>
       
   208       <b>
       
   209        Table of Contents
       
   210       </b>
       
   211      </p>
       
   212      <dl>
       
   213       <dt>
       
   214        <span class="section">
       
   215         <a href="#id2578900">
       
   216          1. String Formatting
       
   217         </a>
       
   218        </span>
       
   219       </dt>
       
   220       <dt>
       
   221        <span class="section">
       
   222         <a href="#id2631317">
       
   223          2. String Methods
       
   224         </a>
       
   225        </span>
       
   226       </dt>
       
   227       <dd>
       
   228        <dl>
       
   229         <dt>
       
   230          <span class="section">
       
   231           <a href="#id2631332">
       
   232            2.1.
       
   233            <span class="strong">
       
   234             <strong>
       
   235              find
       
   236             </strong>
       
   237            </span>
       
   238           </a>
       
   239          </span>
       
   240         </dt>
       
   241         <dt>
       
   242          <span class="section">
       
   243           <a href="#id2631362">
       
   244            2.2.
       
   245            <span class="strong">
       
   246             <strong>
       
   247              join
       
   248             </strong>
       
   249            </span>
       
   250           </a>
       
   251          </span>
       
   252         </dt>
       
   253         <dt>
       
   254          <span class="section">
       
   255           <a href="#id2631402">
       
   256            2.3.
       
   257            <span class="strong">
       
   258             <strong>
       
   259              lower
       
   260             </strong>
       
   261            </span>
       
   262           </a>
       
   263          </span>
       
   264         </dt>
       
   265         <dt>
       
   266          <span class="section">
       
   267           <a href="#id2631432">
       
   268            2.4.
       
   269            <span class="strong">
       
   270             <strong>
       
   271              replace
       
   272             </strong>
       
   273            </span>
       
   274           </a>
       
   275          </span>
       
   276         </dt>
       
   277         <dt>
       
   278          <span class="section">
       
   279           <a href="#id2631489">
       
   280            2.5.
       
   281            <span class="strong">
       
   282             <strong>
       
   283              split
       
   284             </strong>
       
   285            </span>
       
   286           </a>
       
   287          </span>
       
   288         </dt>
       
   289         <dt>
       
   290          <span class="section">
       
   291           <a href="#id2631548">
       
   292            2.6.
       
   293            <span class="strong">
       
   294             <strong>
       
   295              strip
       
   296             </strong>
       
   297            </span>
       
   298           </a>
       
   299          </span>
       
   300         </dt>
       
   301        </dl>
       
   302       </dd>
       
   303       <dt>
       
   304        <span class="section">
       
   305         <a href="#id2631592">
       
   306          3. Introduction to the standard library
       
   307         </a>
       
   308        </span>
       
   309       </dt>
       
   310       <dt>
       
   311        <span class="section">
       
   312         <a href="#id2631669">
       
   313          4. I/O: Reading and Writing Files
       
   314         </a>
       
   315        </span>
       
   316       </dt>
       
   317       <dd>
       
   318        <dl>
       
   319         <dt>
       
   320          <span class="section">
       
   321           <a href="#id2631699">
       
   322            4.1. Opening Files
       
   323           </a>
       
   324          </span>
       
   325         </dt>
       
   326         <dt>
       
   327          <span class="section">
       
   328           <a href="#id2631873">
       
   329            4.2. Reading and Writing files
       
   330           </a>
       
   331          </span>
       
   332         </dt>
       
   333        </dl>
       
   334       </dd>
       
   335       <dt>
       
   336        <span class="section">
       
   337         <a href="#id2632038">
       
   338          5. Dictionaries
       
   339         </a>
       
   340        </span>
       
   341       </dt>
       
   342       <dd>
       
   343        <dl>
       
   344         <dt>
       
   345          <span class="section">
       
   346           <a href="#id2632094">
       
   347            5.1.
       
   348            <span class="strong">
       
   349             <strong>
       
   350              dict()
       
   351             </strong>
       
   352            </span>
       
   353           </a>
       
   354          </span>
       
   355         </dt>
       
   356         <dt>
       
   357          <span class="section">
       
   358           <a href="#id2632158">
       
   359            5.2. Dictionary Methods
       
   360           </a>
       
   361          </span>
       
   362         </dt>
       
   363        </dl>
       
   364       </dd>
       
   365      </dl>
       
   366     </div>
       
   367     <p id="ch4strings_dicts_1">
       
   368      Strings were briefly introduced previously in the introduction document. In this
       
   369 section strings will be presented in greater detail. All the standard operations
    67 section strings will be presented in greater detail. All the standard operations
   370 that can be performed on sequences such as indexing, slicing, multiplication, length
    68 that can be performed on sequences such as indexing, slicing, multiplication, length
   371 minimum and maximum can be performed on string variables as well. One thing to
    69 minimum and maximum can be performed on string variables as well. One thing to
   372 be noted is that strings are immutable, which means that string variables are
    70 be noted is that strings are immutable, which means that string variables are
   373 unchangeable. Hence, all item and slice assignments on strings are illegal.
    71 unchangeable. Hence, all item and slice assignments on strings are illegal.
   374 Let us look at a few example.
    72 Let us look at a few example.</p>
   375     </p>
    73 <pre class="programlisting">
   376     <pre class="programlisting">
    74 &gt;&gt;&gt; name = 'PythonFreak'
   377      &gt;&gt;&gt; name = 'PythonFreak'
       
   378 &gt;&gt;&gt; print name[3]
    75 &gt;&gt;&gt; print name[3]
   379 h
    76 h
   380 &gt;&gt;&gt; print name[-1]
    77 &gt;&gt;&gt; print name[-1]
   381 k
    78 k
   382 &gt;&gt;&gt; print name[6:]
    79 &gt;&gt;&gt; print name[6:]
   383 Freak
    80 Freak
   384 &gt;&gt;&gt; name[6:0] = 'Maniac'
    81 &gt;&gt;&gt; name[6:0] = 'Maniac'
   385 Traceback (most recent call last):
    82 Traceback (most recent call last):
   386   File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    83   File "&lt;stdin&gt;", line 1, in &lt;module&gt;
   387 TypeError: 'str' object does not support item assignment
    84 TypeError: 'str' object does not support item assignment</pre>
   388     </pre>
    85 <p id="ch4strings_dicts_2">This is quite expected, since string objects are immutable as already mentioned.
   389     <p id="ch4strings_dicts_2">
       
   390      This is quite expected, since string objects are immutable as already mentioned.
       
   391 The error message is clear in mentioning that 'str' object does not support item
    86 The error message is clear in mentioning that 'str' object does not support item
   392 assignment.
    87 assignment.</p>
   393     </p>
    88 <div class="section" title="1.String Formatting">
   394     <div class="section" title="1.String Formatting">
    89 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
   395      <div class="titlepage">
    90 <a name="id2981673"></a>1.String Formatting</h2></div></div></div>
   396       <div>
    91 <p id="ch4strings_dicts_3">String formatting can be performed using the string formatting operator represented
   397        <div>
       
   398         <h2 class="title" style="clear: both">
       
   399          <a name="id2578900">
       
   400          </a>
       
   401          1.String Formatting
       
   402         </h2>
       
   403        </div>
       
   404       </div>
       
   405      </div>
       
   406      <p id="ch4strings_dicts_3">
       
   407       String formatting can be performed using the string formatting operator represented
       
   408 as the percent (%) sign. The string placed before the % sign is formatted with
    92 as the percent (%) sign. The string placed before the % sign is formatted with
   409 the value placed to the right of it. Let us look at a simple example.
    93 the value placed to the right of it. Let us look at a simple example.</p>
   410      </p>
    94 <pre class="programlisting">
   411      <pre class="programlisting">
    95 &gt;&gt;&gt; format = 'Hello %s, from PythonFreak'
   412       &gt;&gt;&gt; format = 'Hello %s, from PythonFreak'
       
   413 &gt;&gt;&gt; str1 = 'world!'
    96 &gt;&gt;&gt; str1 = 'world!'
   414 &gt;&gt;&gt; print format % str1
    97 &gt;&gt;&gt; print format % str1
   415 Hello world!, from PythonFreak
    98 Hello world!, from PythonFreak</pre>
   416      </pre>
    99 <p id="ch4strings_dicts_4">The %s parts of the format string are called the coversion specifiers. The coversion
   417      <p id="ch4strings_dicts_4">
       
   418       The %s parts of the format string are called the coversion specifiers. The coversion
       
   419 specifiers mark the places where the formatting has to be performed in a string.
   100 specifiers mark the places where the formatting has to be performed in a string.
   420 In the example the %s is replaced by the value of str1. More than one value can
   101 In the example the %s is replaced by the value of str1. More than one value can
   421 also be formatted at a time by specifying the values to be formatted using tuples
   102 also be formatted at a time by specifying the values to be formatted using tuples
   422 and dictionaries (explained in later sections). Let us look at an example.
   103 and dictionaries (explained in later sections). Let us look at an example.</p>
   423      </p>
   104 <pre class="programlisting">
   424      <pre class="programlisting">
   105 &gt;&gt;&gt; format = 'Hello %s, from %s'
   425       &gt;&gt;&gt; format = 'Hello %s, from %s'
       
   426 &gt;&gt;&gt; values = ('world!', 'PythonFreak')
   106 &gt;&gt;&gt; values = ('world!', 'PythonFreak')
   427 &gt;&gt;&gt; print format % values
   107 &gt;&gt;&gt; print format % values
   428 Hello world!, from PythonFreak
   108 Hello world!, from PythonFreak</pre>
   429      </pre>
   109 <p id="ch4strings_dicts_5">In this example it can be observed that the format string contains two conversion
   430      <p id="ch4strings_dicts_5">
   110 specifiers and they are formatted using the tuple of values as shown.</p>
   431       In this example it can be observed that the format string contains two conversion
   111 <p id="ch4strings_dicts_6">The s in %s specifies that the value to be replaced is of type string. Values of
   432 specifiers and they are formatted using the tuple of values as shown.
       
   433      </p>
       
   434      <p id="ch4strings_dicts_6">
       
   435       The s in %s specifies that the value to be replaced is of type string. Values of
       
   436 other types can be specified as well such as integers and floats. Integers are
   112 other types can be specified as well such as integers and floats. Integers are
   437 specified as %d and floats as %f. The precision with which the integer or the
   113 specified as %d and floats as %f. The precision with which the integer or the
   438 float values are to be represented can also be specified using a
   114 float values are to be represented can also be specified using a <span class="strong"><strong>.</strong></span> (<span class="strong"><strong>dot</strong></span>)
   439       <span class="strong">
   115 followed by the precision value.</p>
   440        <strong>
   116 </div>
   441         .
   117 <div class="section" title="2.String Methods">
   442        </strong>
   118 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
   443       </span>
   119 <a name="id3034090"></a>2.String Methods</h2></div></div></div>
   444       (
   120 <p id="ch4strings_dicts_7">Similar to list methods, strings also have a rich set of methods to perform various
   445       <span class="strong">
       
   446        <strong>
       
   447         dot
       
   448        </strong>
       
   449       </span>
       
   450       )
       
   451 followed by the precision value.
       
   452      </p>
       
   453     </div>
       
   454     <div class="section" title="2.String Methods">
       
   455      <div class="titlepage">
       
   456       <div>
       
   457        <div>
       
   458         <h2 class="title" style="clear: both">
       
   459          <a name="id2631317">
       
   460          </a>
       
   461          2.String Methods
       
   462         </h2>
       
   463        </div>
       
   464       </div>
       
   465      </div>
       
   466      <p id="ch4strings_dicts_7">
       
   467       Similar to list methods, strings also have a rich set of methods to perform various
       
   468 operations on strings. Some of the most important and popular ones are presented
   121 operations on strings. Some of the most important and popular ones are presented
   469 in this section.
   122 in this section.</p>
   470      </p>
   123 <div class="section" title="2.1.find">
   471      <div class="section" title="2.1.find">
   124 <div class="titlepage"><div><div><h3 class="title">
   472       <div class="titlepage">
   125 <a name="id3034105"></a>2.1.<span class="strong"><strong>find</strong></span>
   473        <div>
   126 </h3></div></div></div>
   474         <div>
   127 <p id="ch4strings_dicts_8">The <span class="strong"><strong>find</strong></span> method is used to search for a substring within a given string. It
   475          <h3 class="title">
       
   476           <a name="id2631332">
       
   477           </a>
       
   478           2.1.
       
   479           <span class="strong">
       
   480            <strong>
       
   481             find
       
   482            </strong>
       
   483           </span>
       
   484          </h3>
       
   485         </div>
       
   486        </div>
       
   487       </div>
       
   488       <p id="ch4strings_dicts_8">
       
   489        The
       
   490        <span class="strong">
       
   491         <strong>
       
   492          find
       
   493         </strong>
       
   494        </span>
       
   495        method is used to search for a substring within a given string. It
       
   496 returns the left most index of the first occurence of the substring. If the
   128 returns the left most index of the first occurence of the substring. If the
   497 substring is not found in the string then it returns -1. Let us look at a few
   129 substring is not found in the string then it returns -1. Let us look at a few
   498 examples.
   130 examples.</p>
   499       </p>
   131 <pre class="programlisting">
   500       <pre class="programlisting">
   132 &gt;&gt;&gt; longstring = 'Hello world!, from PythonFreak'
   501        &gt;&gt;&gt; longstring = 'Hello world!, from PythonFreak'
       
   502 &gt;&gt;&gt; longstring.find('Python')
   133 &gt;&gt;&gt; longstring.find('Python')
   503 19
   134 19
   504 &gt;&gt;&gt; longstring.find('Perl')
   135 &gt;&gt;&gt; longstring.find('Perl')
   505 -1
   136 -1</pre>
   506       </pre>
   137 </div>
   507      </div>
   138 <div class="section" title="2.2.join">
   508      <div class="section" title="2.2.join">
   139 <div class="titlepage"><div><div><h3 class="title">
   509       <div class="titlepage">
   140 <a name="id3034135"></a>2.2.<span class="strong"><strong>join</strong></span>
   510        <div>
   141 </h3></div></div></div>
   511         <div>
   142 <p id="ch4strings_dicts_9">The <span class="strong"><strong>join</strong></span> method is used to join the elements of a sequence. The sequence
   512          <h3 class="title">
       
   513           <a name="id2631362">
       
   514           </a>
       
   515           2.2.
       
   516           <span class="strong">
       
   517            <strong>
       
   518             join
       
   519            </strong>
       
   520           </span>
       
   521          </h3>
       
   522         </div>
       
   523        </div>
       
   524       </div>
       
   525       <p id="ch4strings_dicts_9">
       
   526        The
       
   527        <span class="strong">
       
   528         <strong>
       
   529          join
       
   530         </strong>
       
   531        </span>
       
   532        method is used to join the elements of a sequence. The sequence
       
   533 elements that are to be join ed should all be strings. Let us look at a few
   143 elements that are to be join ed should all be strings. Let us look at a few
   534 examples.
   144 examples.</p>
   535       </p>
   145 <pre class="programlisting">
   536       <pre class="programlisting">
   146 &gt;&gt;&gt; seq = ['With', 'great', 'power', 'comes', 'great', 'responsibility']
   537        &gt;&gt;&gt; seq = ['With', 'great', 'power', 'comes', 'great', 'responsibility']
       
   538 &gt;&gt;&gt; sep = ' '
   147 &gt;&gt;&gt; sep = ' '
   539 &gt;&gt;&gt; sep.join(seq)
   148 &gt;&gt;&gt; sep.join(seq)
   540 'With great power comes great responsibility'
   149 'With great power comes great responsibility'
   541 &gt;&gt;&gt; sep = ',!'
   150 &gt;&gt;&gt; sep = ',!'
   542 &gt;&gt;&gt; sep.join(seq)
   151 &gt;&gt;&gt; sep.join(seq)
   543 'With,!great,!power,!comes,!great,!responsibility'
   152 'With,!great,!power,!comes,!great,!responsibility'</pre>
   544       </pre>
   153 <p id="ch4strings_dicts_a"><span class="emphasis"><em>Try this yourself</em></span></p>
   545       <p id="ch4strings_dicts_a">
   154 <pre class="programlisting">
   546        <span class="emphasis">
   155 &gt;&gt;&gt; seq = [12,34,56,78]
   547         <em>
   156 &gt;&gt;&gt; sep.join(seq)</pre>
   548          Try this yourself
   157 </div>
   549         </em>
   158 <div class="section" title="2.3.lower">
   550        </span>
   159 <div class="titlepage"><div><div><h3 class="title">
   551       </p>
   160 <a name="id3034175"></a>2.3.<span class="strong"><strong>lower</strong></span>
   552       <pre class="programlisting">
   161 </h3></div></div></div>
   553        &gt;&gt;&gt; seq = [12,34,56,78]
   162 <p id="ch4strings_dicts_b">The <span class="strong"><strong>lower</strong></span> method, as the name indicates, converts the entire text of a string
   554 &gt;&gt;&gt; sep.join(seq)
       
   555       </pre>
       
   556      </div>
       
   557      <div class="section" title="2.3.lower">
       
   558       <div class="titlepage">
       
   559        <div>
       
   560         <div>
       
   561          <h3 class="title">
       
   562           <a name="id2631402">
       
   563           </a>
       
   564           2.3.
       
   565           <span class="strong">
       
   566            <strong>
       
   567             lower
       
   568            </strong>
       
   569           </span>
       
   570          </h3>
       
   571         </div>
       
   572        </div>
       
   573       </div>
       
   574       <p id="ch4strings_dicts_b">
       
   575        The
       
   576        <span class="strong">
       
   577         <strong>
       
   578          lower
       
   579         </strong>
       
   580        </span>
       
   581        method, as the name indicates, converts the entire text of a string
       
   582 to lower case. It is specially useful in cases where the programmers deal with case
   163 to lower case. It is specially useful in cases where the programmers deal with case
   583 insensitive data. Let us look at a few examples.
   164 insensitive data. Let us look at a few examples.</p>
   584       </p>
   165 <pre class="programlisting">
   585       <pre class="programlisting">
   166 &gt;&gt;&gt; sometext = 'Hello world!, from PythonFreak'
   586        &gt;&gt;&gt; sometext = 'Hello world!, from PythonFreak'
       
   587 &gt;&gt;&gt; sometext.lower()
   167 &gt;&gt;&gt; sometext.lower()
   588 'hello world!, from pythonfreak'
   168 'hello world!, from pythonfreak'</pre>
   589       </pre>
   169 </div>
   590      </div>
   170 <div class="section" title="2.4.replace">
   591      <div class="section" title="2.4.replace">
   171 <div class="titlepage"><div><div><h3 class="title">
   592       <div class="titlepage">
   172 <a name="id3034206"></a>2.4.<span class="strong"><strong>replace</strong></span>
   593        <div>
   173 </h3></div></div></div>
   594         <div>
   174 <p id="ch4strings_dicts_c">The <span class="strong"><strong>replace</strong></span> method replaces a substring with another substring within
   595          <h3 class="title">
   175 a given string and returns the new string. Let us look at an example.</p>
   596           <a name="id2631432">
   176 <pre class="programlisting">
   597           </a>
   177 &gt;&gt;&gt; sometext = 'Concise, precise and criticise is some of the words that end with ise'
   598           2.4.
       
   599           <span class="strong">
       
   600            <strong>
       
   601             replace
       
   602            </strong>
       
   603           </span>
       
   604          </h3>
       
   605         </div>
       
   606        </div>
       
   607       </div>
       
   608       <p id="ch4strings_dicts_c">
       
   609        The
       
   610        <span class="strong">
       
   611         <strong>
       
   612          replace
       
   613         </strong>
       
   614        </span>
       
   615        method replaces a substring with another substring within
       
   616 a given string and returns the new string. Let us look at an example.
       
   617       </p>
       
   618       <pre class="programlisting">
       
   619        &gt;&gt;&gt; sometext = 'Concise, precise and criticise is some of the words that end with ise'
       
   620 &gt;&gt;&gt; sometext.replace('is', 'are')
   178 &gt;&gt;&gt; sometext.replace('is', 'are')
   621 'Concaree, precaree and criticaree are some of the words that end with aree'
   179 'Concaree, precaree and criticaree are some of the words that end with aree'</pre>
   622       </pre>
   180 <p id="ch4strings_dicts_d">Observe here that all the occurences of the substring <span class="emphasis"><em>is</em></span> have been replaced,
   623       <p id="ch4strings_dicts_d">
   181 even the <span class="emphasis"><em>is</em></span> in <span class="emphasis"><em>concise</em></span>, <span class="emphasis"><em>precise</em></span> and <span class="emphasis"><em>criticise</em></span> have been replaced.</p>
   624        Observe here that all the occurences of the substring
   182 </div>
   625        <span class="emphasis">
   183 <div class="section" title="2.5.split">
   626         <em>
   184 <div class="titlepage"><div><div><h3 class="title">
   627          is
   185 <a name="id3034262"></a>2.5.<span class="strong"><strong>split</strong></span>
   628         </em>
   186 </h3></div></div></div>
   629        </span>
   187 <p id="ch4strings_dicts_e">The <span class="strong"><strong>split</strong></span> is one of the very important string methods. split is the opposite of the
   630        have been replaced,
   188 <span class="strong"><strong>join</strong></span> method. It is used to split a string based on the argument passed as the
   631 even the
       
   632        <span class="emphasis">
       
   633         <em>
       
   634          is
       
   635         </em>
       
   636        </span>
       
   637        in
       
   638        <span class="emphasis">
       
   639         <em>
       
   640          concise
       
   641         </em>
       
   642        </span>
       
   643        ,
       
   644        <span class="emphasis">
       
   645         <em>
       
   646          precise
       
   647         </em>
       
   648        </span>
       
   649        and
       
   650        <span class="emphasis">
       
   651         <em>
       
   652          criticise
       
   653         </em>
       
   654        </span>
       
   655        have been replaced.
       
   656       </p>
       
   657      </div>
       
   658      <div class="section" title="2.5.split">
       
   659       <div class="titlepage">
       
   660        <div>
       
   661         <div>
       
   662          <h3 class="title">
       
   663           <a name="id2631489">
       
   664           </a>
       
   665           2.5.
       
   666           <span class="strong">
       
   667            <strong>
       
   668             split
       
   669            </strong>
       
   670           </span>
       
   671          </h3>
       
   672         </div>
       
   673        </div>
       
   674       </div>
       
   675       <p id="ch4strings_dicts_e">
       
   676        The
       
   677        <span class="strong">
       
   678         <strong>
       
   679          split
       
   680         </strong>
       
   681        </span>
       
   682        is one of the very important string methods. split is the opposite of the
       
   683        <span class="strong">
       
   684         <strong>
       
   685          join
       
   686         </strong>
       
   687        </span>
       
   688        method. It is used to split a string based on the argument passed as the
       
   689 delimiter. It returns a list of strings. By default when no argument is passed it
   189 delimiter. It returns a list of strings. By default when no argument is passed it
   690 splits with
   190 splits with <span class="emphasis"><em>space</em></span> (' ') as the delimiter. Let us look at an example.</p>
   691        <span class="emphasis">
   191 <pre class="programlisting">
   692         <em>
   192 &gt;&gt;&gt; grocerylist = 'butter, cucumber, beer(a grocery item??), wheatbread'
   693          space
       
   694         </em>
       
   695        </span>
       
   696        (' ') as the delimiter. Let us look at an example.
       
   697       </p>
       
   698       <pre class="programlisting">
       
   699        &gt;&gt;&gt; grocerylist = 'butter, cucumber, beer(a grocery item??), wheatbread'
       
   700 &gt;&gt;&gt; grocerylist.split(',')
   193 &gt;&gt;&gt; grocerylist.split(',')
   701 ['butter', ' cucumber', ' beer(a grocery item??)', ' wheatbread']
   194 ['butter', ' cucumber', ' beer(a grocery item??)', ' wheatbread']
   702 &gt;&gt;&gt; grocerylist.split()
   195 &gt;&gt;&gt; grocerylist.split()
   703 ['butter,', 'cucumber,', 'beer(a', 'grocery', 'item??),', 'wheatbread']
   196 ['butter,', 'cucumber,', 'beer(a', 'grocery', 'item??),', 'wheatbread']</pre>
   704       </pre>
   197 <p id="ch4strings_dicts_f">Observe here that in the second case when the delimiter argument was not set
   705       <p id="ch4strings_dicts_f">
   198 <span class="strong"><strong>split</strong></span> was done with <span class="emphasis"><em>space</em></span> as the delimiter.</p>
   706        Observe here that in the second case when the delimiter argument was not set
   199 </div>
   707        <span class="strong">
   200 <div class="section" title="2.6.strip">
   708         <strong>
   201 <div class="titlepage"><div><div><h3 class="title">
   709          split
   202 <a name="id3034321"></a>2.6.<span class="strong"><strong>strip</strong></span>
   710         </strong>
   203 </h3></div></div></div>
   711        </span>
   204 <p id="ch4strings_dicts_10">The <span class="strong"><strong>strip</strong></span> method is used to remove or <span class="strong"><strong>strip</strong></span> off any whitespaces that exist
   712        was done with
       
   713        <span class="emphasis">
       
   714         <em>
       
   715          space
       
   716         </em>
       
   717        </span>
       
   718        as the delimiter.
       
   719       </p>
       
   720      </div>
       
   721      <div class="section" title="2.6.strip">
       
   722       <div class="titlepage">
       
   723        <div>
       
   724         <div>
       
   725          <h3 class="title">
       
   726           <a name="id2631548">
       
   727           </a>
       
   728           2.6.
       
   729           <span class="strong">
       
   730            <strong>
       
   731             strip
       
   732            </strong>
       
   733           </span>
       
   734          </h3>
       
   735         </div>
       
   736        </div>
       
   737       </div>
       
   738       <p id="ch4strings_dicts_10">
       
   739        The
       
   740        <span class="strong">
       
   741         <strong>
       
   742          strip
       
   743         </strong>
       
   744        </span>
       
   745        method is used to remove or
       
   746        <span class="strong">
       
   747         <strong>
       
   748          strip
       
   749         </strong>
       
   750        </span>
       
   751        off any whitespaces that exist
       
   752 to the left and right of a string, but not the whitespaces within a string. Let
   205 to the left and right of a string, but not the whitespaces within a string. Let
   753 us look at an example.
   206 us look at an example.</p>
   754       </p>
   207 <pre class="programlisting">
   755       <pre class="programlisting">
   208 &gt;&gt;&gt; spacedtext = "               Where's the text??                 "
   756        &gt;&gt;&gt; spacedtext = "               Where's the text??                 "
       
   757 &gt;&gt;&gt; spacedtext.strip()
   209 &gt;&gt;&gt; spacedtext.strip()
   758 "Where's the text??"
   210 "Where's the text??"</pre>
   759       </pre>
   211 <p id="ch4strings_dicts_11">Observe that the whitespaces between the words have not been removed.</p>
   760       <p id="ch4strings_dicts_11">
   212 <pre class="programlisting">
   761        Observe that the whitespaces between the words have not been removed.
   213 Note: Very important thing to note is that all the methods shown above do not
   762       </p>
       
   763       <pre class="programlisting">
       
   764        Note: Very important thing to note is that all the methods shown above do not
       
   765       transform the source string. The source string still remains the same.
   214       transform the source string. The source string still remains the same.
   766       Remember that **strings are immutable**.
   215       Remember that **strings are immutable**.</pre>
   767       </pre>
   216 </div>
   768      </div>
   217 </div>
   769     </div>
   218 <div class="section" title="3.Introduction to the standard library">
   770     <div class="section" title="3.Introduction to the standard library">
   219 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
   771      <div class="titlepage">
   220 <a name="id3034366"></a>3.Introduction to the standard library</h2></div></div></div>
   772       <div>
   221 <p id="ch4strings_dicts_12">Python is often referred to as a "Batteries included!" language, mainly because
   773        <div>
       
   774         <h2 class="title" style="clear: both">
       
   775          <a name="id2631592">
       
   776          </a>
       
   777          3.Introduction to the standard library
       
   778         </h2>
       
   779        </div>
       
   780       </div>
       
   781      </div>
       
   782      <p id="ch4strings_dicts_12">
       
   783       Python is often referred to as a "Batteries included!" language, mainly because
       
   784 of the Python Standard Library. The Python Standard Library provides an extensive
   222 of the Python Standard Library. The Python Standard Library provides an extensive
   785 set of features some of which are available directly for use while some require to
   223 set of features some of which are available directly for use while some require to
   786 import a few
   224 import a few <span class="strong"><strong>modules</strong></span>. The Standard Library provides various built-in functions
   787       <span class="strong">
   225 like:</p>
   788        <strong>
   226 <div class="itemizedlist"><ul class="itemizedlist" type="*">
   789         modules
   227 <li class="listitem" style="list-style-type: *"><p id="ch4strings_dicts_13"><span class="strong"><strong>abs()</strong></span></p></li>
   790        </strong>
   228 <li class="listitem" style="list-style-type: *"><p id="ch4strings_dicts_14"><span class="strong"><strong>dict()</strong></span></p></li>
   791       </span>
   229 <li class="listitem" style="list-style-type: *"><p id="ch4strings_dicts_15"><span class="strong"><strong>enumerate()</strong></span></p></li>
   792       . The Standard Library provides various built-in functions
   230 </ul></div>
   793 like:
   231 <p id="ch4strings_dicts_16">The built-in constants like <span class="strong"><strong>True</strong></span> and <span class="strong"><strong>False</strong></span> are provided by the Standard Library.
   794      </p>
   232 More information about the Python Standard Library is available </p>
   795      <div class="itemizedlist">
   233 <div class="reference">
   796       <ul class="itemizedlist" type="*">
   234 <div class="titlepage"><hr /></div>http://docs.python.org/library/</div>
   797        <li class="listitem" style="list-style-type: *">
   235 </div>
   798         <p id="ch4strings_dicts_13">
   236 <div class="section" title="4.I/O: Reading and Writing Files">
   799          <span class="strong">
   237 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
   800           <strong>
   238 <a name="id3034442"></a>4.I/O: Reading and Writing Files</h2></div></div></div>
   801            abs()
   239 <p id="ch4strings_dicts_17">Files are very important aspects when it comes to computing and programming.
   802           </strong>
       
   803          </span>
       
   804         </p>
       
   805        </li>
       
   806        <li class="listitem" style="list-style-type: *">
       
   807         <p id="ch4strings_dicts_14">
       
   808          <span class="strong">
       
   809           <strong>
       
   810            dict()
       
   811           </strong>
       
   812          </span>
       
   813         </p>
       
   814        </li>
       
   815        <li class="listitem" style="list-style-type: *">
       
   816         <p id="ch4strings_dicts_15">
       
   817          <span class="strong">
       
   818           <strong>
       
   819            enumerate()
       
   820           </strong>
       
   821          </span>
       
   822         </p>
       
   823        </li>
       
   824       </ul>
       
   825      </div>
       
   826      <p id="ch4strings_dicts_16">
       
   827       The built-in constants like
       
   828       <span class="strong">
       
   829        <strong>
       
   830         True
       
   831        </strong>
       
   832       </span>
       
   833       and
       
   834       <span class="strong">
       
   835        <strong>
       
   836         False
       
   837        </strong>
       
   838       </span>
       
   839       are provided by the Standard Library.
       
   840 More information about the Python Standard Library is available
       
   841      </p>
       
   842      <div class="reference">
       
   843       <div class="titlepage">
       
   844        <hr />
       
   845       </div>
       
   846       http://docs.python.org/library/
       
   847      </div>
       
   848     </div>
       
   849     <div class="section" title="4.I/O: Reading and Writing Files">
       
   850      <div class="titlepage">
       
   851       <div>
       
   852        <div>
       
   853         <h2 class="title" style="clear: both">
       
   854          <a name="id2631669">
       
   855          </a>
       
   856          4.I/O: Reading and Writing Files
       
   857         </h2>
       
   858        </div>
       
   859       </div>
       
   860      </div>
       
   861      <p id="ch4strings_dicts_17">
       
   862       Files are very important aspects when it comes to computing and programming.
       
   863 Up until now the focus has been on small programs that interacted with users
   240 Up until now the focus has been on small programs that interacted with users
   864 through
   241 through <span class="strong"><strong>input()</strong></span> and <span class="strong"><strong>raw_input()</strong></span>. Generally, for computational purposes
   865       <span class="strong">
       
   866        <strong>
       
   867         input()
       
   868        </strong>
       
   869       </span>
       
   870       and
       
   871       <span class="strong">
       
   872        <strong>
       
   873         raw_input()
       
   874        </strong>
       
   875       </span>
       
   876       . Generally, for computational purposes
       
   877 it becomes necessary to handle files, which are usually large in size as well.
   242 it becomes necessary to handle files, which are usually large in size as well.
   878 This section focuses on basics of file handling.
   243 This section focuses on basics of file handling.</p>
   879      </p>
   244 <div class="section" title="4.1.Opening Files">
   880      <div class="section" title="4.1.Opening Files">
   245 <div class="titlepage"><div><div><h3 class="title">
   881       <div class="titlepage">
   246 <a name="id3034472"></a>4.1.Opening Files</h3></div></div></div>
   882        <div>
   247 <p id="ch4strings_dicts_18">Files can be opened using the <span class="strong"><strong>open()</strong></span> method. <span class="strong"><strong>open()</strong></span> accepts 3 arguments
   883         <div>
   248 out of which 2 are optional. Let us look at the syntax of <span class="strong"><strong>open()</strong></span>:</p>
   884          <h3 class="title">
   249 <p id="ch4strings_dicts_19"><span class="emphasis"><em>f = open( filename, mode, buffering)</em></span></p>
   885           <a name="id2631699">
   250 <p id="ch4strings_dicts_1a">The <span class="emphasis"><em>filename</em></span> is a compulsory argument while the <span class="emphasis"><em>mode</em></span> and <span class="emphasis"><em>buffering</em></span> are
   886           </a>
   251 optional. The <span class="emphasis"><em>filename</em></span> should be a string and it should be the complete path
   887           4.1.Opening Files
       
   888          </h3>
       
   889         </div>
       
   890        </div>
       
   891       </div>
       
   892       <p id="ch4strings_dicts_18">
       
   893        Files can be opened using the
       
   894        <span class="strong">
       
   895         <strong>
       
   896          open()
       
   897         </strong>
       
   898        </span>
       
   899        method.
       
   900        <span class="strong">
       
   901         <strong>
       
   902          open()
       
   903         </strong>
       
   904        </span>
       
   905        accepts 3 arguments
       
   906 out of which 2 are optional. Let us look at the syntax of
       
   907        <span class="strong">
       
   908         <strong>
       
   909          open()
       
   910         </strong>
       
   911        </span>
       
   912        :
       
   913       </p>
       
   914       <p id="ch4strings_dicts_19">
       
   915        <span class="emphasis">
       
   916         <em>
       
   917          f = open( filename, mode, buffering)
       
   918         </em>
       
   919        </span>
       
   920       </p>
       
   921       <p id="ch4strings_dicts_1a">
       
   922        The
       
   923        <span class="emphasis">
       
   924         <em>
       
   925          filename
       
   926         </em>
       
   927        </span>
       
   928        is a compulsory argument while the
       
   929        <span class="emphasis">
       
   930         <em>
       
   931          mode
       
   932         </em>
       
   933        </span>
       
   934        and
       
   935        <span class="emphasis">
       
   936         <em>
       
   937          buffering
       
   938         </em>
       
   939        </span>
       
   940        are
       
   941 optional. The
       
   942        <span class="emphasis">
       
   943         <em>
       
   944          filename
       
   945         </em>
       
   946        </span>
       
   947        should be a string and it should be the complete path
       
   948 to the file to be opened (The path can be absolute or relative). Let us look at
   252 to the file to be opened (The path can be absolute or relative). Let us look at
   949 an example.
   253 an example.</p>
   950       </p>
   254 <pre class="programlisting">
   951       <pre class="programlisting">
   255 &gt;&gt;&gt; f = open ('basic_python/interim_assessment.rst')</pre>
   952        &gt;&gt;&gt; f = open ('basic_python/interim_assessment.rst')
   256 <p id="ch4strings_dicts_1b">The <span class="emphasis"><em>mode</em></span> argument specifies the mode in which the file has to be opened.
   953       </pre>
   257 The following are the valid mode arguments:</p>
   954       <p id="ch4strings_dicts_1b">
   258 <p id="ch4strings_dicts_1c"><span class="strong"><strong>r</strong></span> - Read mode
   955        The
   259 <span class="strong"><strong>w</strong></span> - Write mode
   956        <span class="emphasis">
   260 <span class="strong"><strong>a</strong></span> - Append mode
   957         <em>
   261 <span class="strong"><strong>b</strong></span> - Binary mode
   958          mode
   262 <span class="strong"><strong>+</strong></span> - Read/Write mode</p>
   959         </em>
   263 <p id="ch4strings_dicts_1d">The read mode opens the file as a read-only document. The write mode opens the
   960        </span>
       
   961        argument specifies the mode in which the file has to be opened.
       
   962 The following are the valid mode arguments:
       
   963       </p>
       
   964       <p id="ch4strings_dicts_1c">
       
   965        <span class="strong">
       
   966         <strong>
       
   967          r
       
   968         </strong>
       
   969        </span>
       
   970        - Read mode
       
   971        <span class="strong">
       
   972         <strong>
       
   973          w
       
   974         </strong>
       
   975        </span>
       
   976        - Write mode
       
   977        <span class="strong">
       
   978         <strong>
       
   979          a
       
   980         </strong>
       
   981        </span>
       
   982        - Append mode
       
   983        <span class="strong">
       
   984         <strong>
       
   985          b
       
   986         </strong>
       
   987        </span>
       
   988        - Binary mode
       
   989        <span class="strong">
       
   990         <strong>
       
   991          +
       
   992         </strong>
       
   993        </span>
       
   994        - Read/Write mode
       
   995       </p>
       
   996       <p id="ch4strings_dicts_1d">
       
   997        The read mode opens the file as a read-only document. The write mode opens the
       
   998 file in the Write only mode. In the write mode, if the file existed prior to the
   264 file in the Write only mode. In the write mode, if the file existed prior to the
   999 opening, the previous contents of the file are erased. The append mode opens the
   265 opening, the previous contents of the file are erased. The append mode opens the
  1000 file in the write mode but the previous contents of the file are not erased and
   266 file in the write mode but the previous contents of the file are not erased and
  1001 the current data is appended onto the file.
   267 the current data is appended onto the file.
  1002 The binary and the read/write modes are special in the sense that they are added
   268 The binary and the read/write modes are special in the sense that they are added
  1003 onto other modes. The read/write mode opens the file in the reading and writing
   269 onto other modes. The read/write mode opens the file in the reading and writing
  1004 mode combined. The binary mode can be used to open a files that do not contain
   270 mode combined. The binary mode can be used to open a files that do not contain
  1005 text. Binary files such as images should be opened in the binary mode. Let us look
   271 text. Binary files such as images should be opened in the binary mode. Let us look
  1006 at a few examples.
   272 at a few examples.</p>
  1007       </p>
   273 <pre class="programlisting">
  1008       <pre class="programlisting">
   274 &gt;&gt;&gt; f = open ('basic_python/interim_assessment.rst', 'r')
  1009        &gt;&gt;&gt; f = open ('basic_python/interim_assessment.rst', 'r')
   275 &gt;&gt;&gt; f = open ('armstrong.py', 'r+')</pre>
  1010 &gt;&gt;&gt; f = open ('armstrong.py', 'r+')
   276 <p id="ch4strings_dicts_1e">The third argument to the <span class="strong"><strong>open()</strong></span> method is the <span class="emphasis"><em>buffering</em></span> argument. This takes
  1011       </pre>
   277 a boolean value, <span class="emphasis"><em>True</em></span> or <span class="emphasis"><em>1</em></span> indicates that buffering has to be enabled on the file,
  1012       <p id="ch4strings_dicts_1e">
       
  1013        The third argument to the
       
  1014        <span class="strong">
       
  1015         <strong>
       
  1016          open()
       
  1017         </strong>
       
  1018        </span>
       
  1019        method is the
       
  1020        <span class="emphasis">
       
  1021         <em>
       
  1022          buffering
       
  1023         </em>
       
  1024        </span>
       
  1025        argument. This takes
       
  1026 a boolean value,
       
  1027        <span class="emphasis">
       
  1028         <em>
       
  1029          True
       
  1030         </em>
       
  1031        </span>
       
  1032        or
       
  1033        <span class="emphasis">
       
  1034         <em>
       
  1035          1
       
  1036         </em>
       
  1037        </span>
       
  1038        indicates that buffering has to be enabled on the file,
       
  1039 that is the file is loaded on to the main memory and the changes made to the file are
   278 that is the file is loaded on to the main memory and the changes made to the file are
  1040 not immediately written to the disk. If the
   279 not immediately written to the disk. If the <span class="emphasis"><em>buffering</em></span> argument is <span class="emphasis"><em>0</em></span> or <span class="emphasis"><em>False</em></span> the
  1041        <span class="emphasis">
   280 changes are directly written on to the disk immediately.</p>
  1042         <em>
   281 </div>
  1043          buffering
   282 <div class="section" title="4.2.Reading and Writing files">
  1044         </em>
   283 <div class="titlepage"><div><div><h3 class="title">
  1045        </span>
   284 <a name="id3034646"></a>4.2.Reading and Writing files</h3></div></div></div>
  1046        argument is
   285 <div class="section" title="4.2.1.write()">
  1047        <span class="emphasis">
   286 <div class="titlepage"><div><div><h4 class="title">
  1048         <em>
   287 <a name="id3034655"></a>4.2.1.<span class="strong"><strong>write()</strong></span>
  1049          0
   288 </h4></div></div></div>
  1050         </em>
   289 <p id="ch4strings_dicts_1f"><span class="strong"><strong>write()</strong></span>, evidently, is used to write data onto a file. It takes the data to
  1051        </span>
       
  1052        or
       
  1053        <span class="emphasis">
       
  1054         <em>
       
  1055          False
       
  1056         </em>
       
  1057        </span>
       
  1058        the
       
  1059 changes are directly written on to the disk immediately.
       
  1060       </p>
       
  1061      </div>
       
  1062      <div class="section" title="4.2.Reading and Writing files">
       
  1063       <div class="titlepage">
       
  1064        <div>
       
  1065         <div>
       
  1066          <h3 class="title">
       
  1067           <a name="id2631873">
       
  1068           </a>
       
  1069           4.2.Reading and Writing files
       
  1070          </h3>
       
  1071         </div>
       
  1072        </div>
       
  1073       </div>
       
  1074       <div class="section" title="4.2.1.write()">
       
  1075        <div class="titlepage">
       
  1076         <div>
       
  1077          <div>
       
  1078           <h4 class="title">
       
  1079            <a name="id2631882">
       
  1080            </a>
       
  1081            4.2.1.
       
  1082            <span class="strong">
       
  1083             <strong>
       
  1084              write()
       
  1085             </strong>
       
  1086            </span>
       
  1087           </h4>
       
  1088          </div>
       
  1089         </div>
       
  1090        </div>
       
  1091        <p id="ch4strings_dicts_1f">
       
  1092         <span class="strong">
       
  1093          <strong>
       
  1094           write()
       
  1095          </strong>
       
  1096         </span>
       
  1097         , evidently, is used to write data onto a file. It takes the data to
       
  1098 be written as the argument. The data can be a string, an integer, a float or any
   290 be written as the argument. The data can be a string, an integer, a float or any
  1099 other datatype. In order to be able to write data onto a file, the file has to
   291 other datatype. In order to be able to write data onto a file, the file has to
  1100 be opened in one of
   292 be opened in one of <span class="strong"><strong>w</strong></span>, <span class="strong"><strong>a</strong></span> or <span class="strong"><strong>+</strong></span> modes.</p>
  1101         <span class="strong">
   293 </div>
  1102          <strong>
   294 <div class="section" title="4.2.2.read()">
  1103           w
   295 <div class="titlepage"><div><div><h4 class="title">
  1104          </strong>
   296 <a name="id3034694"></a>4.2.2.<span class="strong"><strong>read()</strong></span>
  1105         </span>
   297 </h4></div></div></div>
  1106         ,
   298 <p id="ch4strings_dicts_20"><span class="strong"><strong>read()</strong></span> is used to read data from a file. It takes the number of bytes of data
  1107         <span class="strong">
       
  1108          <strong>
       
  1109           a
       
  1110          </strong>
       
  1111         </span>
       
  1112         or
       
  1113         <span class="strong">
       
  1114          <strong>
       
  1115           +
       
  1116          </strong>
       
  1117         </span>
       
  1118         modes.
       
  1119        </p>
       
  1120       </div>
       
  1121       <div class="section" title="4.2.2.read()">
       
  1122        <div class="titlepage">
       
  1123         <div>
       
  1124          <div>
       
  1125           <h4 class="title">
       
  1126            <a name="id2631921">
       
  1127            </a>
       
  1128            4.2.2.
       
  1129            <span class="strong">
       
  1130             <strong>
       
  1131              read()
       
  1132             </strong>
       
  1133            </span>
       
  1134           </h4>
       
  1135          </div>
       
  1136         </div>
       
  1137        </div>
       
  1138        <p id="ch4strings_dicts_20">
       
  1139         <span class="strong">
       
  1140          <strong>
       
  1141           read()
       
  1142          </strong>
       
  1143         </span>
       
  1144         is used to read data from a file. It takes the number of bytes of data
       
  1145 to be read as the argument. If nothing is specified by default it reads the entire
   299 to be read as the argument. If nothing is specified by default it reads the entire
  1146 contents from the current position to the end of file.
   300 contents from the current position to the end of file.</p>
  1147        </p>
   301 <p id="ch4strings_dicts_21">Let us look at a few examples:</p>
  1148        <p id="ch4strings_dicts_21">
   302 <pre class="programlisting">
  1149         Let us look at a few examples:
   303 &gt;&gt;&gt; f = open ('randomtextfile', 'w')
  1150        </p>
       
  1151        <pre class="programlisting">
       
  1152         &gt;&gt;&gt; f = open ('randomtextfile', 'w')
       
  1153 &gt;&gt;&gt; f.write('Hello all, this is PythonFreak. This is a random text file.')
   304 &gt;&gt;&gt; f.write('Hello all, this is PythonFreak. This is a random text file.')
  1154 &gt;&gt;&gt; f = open ('../randomtextfile', 'r')
   305 &gt;&gt;&gt; f = open ('../randomtextfile', 'r')
  1155 &gt;&gt;&gt; f = open ('../randomtextfile', 'r')
   306 &gt;&gt;&gt; f = open ('../randomtextfile', 'r')
  1156 &gt;&gt;&gt; f.read(5)
   307 &gt;&gt;&gt; f.read(5)
  1157 'Hello'
   308 'Hello'
  1158 &gt;&gt;&gt; f.read()
   309 &gt;&gt;&gt; f.read()
  1159 ' all, this is PythonFreak. This is a random text file.'
   310 ' all, this is PythonFreak. This is a random text file.'
  1160 &gt;&gt;&gt; f.close()
   311 &gt;&gt;&gt; f.close()</pre>
  1161        </pre>
   312 </div>
  1162       </div>
   313 <div class="section" title="4.2.3.readline()">
  1163       <div class="section" title="4.2.3.readline()">
   314 <div class="titlepage"><div><div><h4 class="title">
  1164        <div class="titlepage">
   315 <a name="id3034729"></a>4.2.3.<span class="strong"><strong>readline()</strong></span>
  1165         <div>
   316 </h4></div></div></div>
  1166          <div>
   317 <p id="ch4strings_dicts_22"><span class="strong"><strong>readline()</strong></span> is used to read a file line by line. <span class="strong"><strong>readline()</strong></span> reads a line
  1167           <h4 class="title">
   318 of a file at a time. When an argument is passed to <span class="strong"><strong>readline()</strong></span> it reads that
  1168            <a name="id2631956">
   319 many bytes from the current line.</p>
  1169            </a>
   320 <p id="ch4strings_dicts_23">One other method to read a file line by line is using the <span class="strong"><strong>read()</strong></span> and the
  1170            4.2.3.
   321 <span class="strong"><strong>for</strong></span> construct. Let us look at this block of code as an example.</p>
  1171            <span class="strong">
   322 <pre class="programlisting">
  1172             <strong>
   323 &gt;&gt;&gt; f = open('../randomtextfile', 'r')
  1173              readline()
       
  1174             </strong>
       
  1175            </span>
       
  1176           </h4>
       
  1177          </div>
       
  1178         </div>
       
  1179        </div>
       
  1180        <p id="ch4strings_dicts_22">
       
  1181         <span class="strong">
       
  1182          <strong>
       
  1183           readline()
       
  1184          </strong>
       
  1185         </span>
       
  1186         is used to read a file line by line.
       
  1187         <span class="strong">
       
  1188          <strong>
       
  1189           readline()
       
  1190          </strong>
       
  1191         </span>
       
  1192         reads a line
       
  1193 of a file at a time. When an argument is passed to
       
  1194         <span class="strong">
       
  1195          <strong>
       
  1196           readline()
       
  1197          </strong>
       
  1198         </span>
       
  1199         it reads that
       
  1200 many bytes from the current line.
       
  1201        </p>
       
  1202        <p id="ch4strings_dicts_23">
       
  1203         One other method to read a file line by line is using the
       
  1204         <span class="strong">
       
  1205          <strong>
       
  1206           read()
       
  1207          </strong>
       
  1208         </span>
       
  1209         and the
       
  1210         <span class="strong">
       
  1211          <strong>
       
  1212           for
       
  1213          </strong>
       
  1214         </span>
       
  1215         construct. Let us look at this block of code as an example.
       
  1216        </p>
       
  1217        <pre class="programlisting">
       
  1218         &gt;&gt;&gt; f = open('../randomtextfile', 'r')
       
  1219 &gt;&gt;&gt; for line in f:
   324 &gt;&gt;&gt; for line in f:
  1220 ...     print line
   325 ...     print line
  1221 ...
   326 ...
  1222 Hello all!
   327 Hello all!
  1223 
   328 
  1224 This is PythonFreak on the second line.
   329 This is PythonFreak on the second line.
  1225 
   330 
  1226 This is a random text file on line 3
   331 This is a random text file on line 3</pre>
  1227        </pre>
   332 </div>
  1228       </div>
   333 <div class="section" title="4.2.4.close()">
  1229       <div class="section" title="4.2.4.close()">
   334 <div class="titlepage"><div><div><h4 class="title">
  1230        <div class="titlepage">
   335 <a name="id3034788"></a>4.2.4.<span class="strong"><strong>close()</strong></span>
  1231         <div>
   336 </h4></div></div></div>
  1232          <div>
   337 <p id="ch4strings_dicts_24">One must always close all the files that have been opened. Although, files opened
  1233           <h4 class="title">
       
  1234            <a name="id2632015">
       
  1235            </a>
       
  1236            4.2.4.
       
  1237            <span class="strong">
       
  1238             <strong>
       
  1239              close()
       
  1240             </strong>
       
  1241            </span>
       
  1242           </h4>
       
  1243          </div>
       
  1244         </div>
       
  1245        </div>
       
  1246        <p id="ch4strings_dicts_24">
       
  1247         One must always close all the files that have been opened. Although, files opened
       
  1248 will be closed automatically when the program ends. When files opened in read mode
   338 will be closed automatically when the program ends. When files opened in read mode
  1249 are not closed it might lead to uselessly locked sometimes. In case of files
   339 are not closed it might lead to uselessly locked sometimes. In case of files
  1250 opened in the write mode it is more important to close the files. This is because,
   340 opened in the write mode it is more important to close the files. This is because,
  1251 Python maybe using the file in the buffering mode and when the file is not closed
   341 Python maybe using the file in the buffering mode and when the file is not closed
  1252 the buffer maybe lost completely and the changes made to the file are lost forever.
   342 the buffer maybe lost completely and the changes made to the file are lost forever.</p>
  1253        </p>
   343 </div>
  1254       </div>
   344 </div>
  1255      </div>
   345 </div>
  1256     </div>
   346 <div class="section" title="5.Dictionaries">
  1257     <div class="section" title="5.Dictionaries">
   347 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
  1258      <div class="titlepage">
   348 <a name="id3034811"></a>5.Dictionaries</h2></div></div></div>
  1259       <div>
   349 <p id="ch4strings_dicts_25">A dictionary in general, are designed to be able to look up meanings of words.
  1260        <div>
       
  1261         <h2 class="title" style="clear: both">
       
  1262          <a name="id2632038">
       
  1263          </a>
       
  1264          5.Dictionaries
       
  1265         </h2>
       
  1266        </div>
       
  1267       </div>
       
  1268      </div>
       
  1269      <p id="ch4strings_dicts_25">
       
  1270       A dictionary in general, are designed to be able to look up meanings of words.
       
  1271 Similarly, the Python dictionaries are also designed to look up for a specific
   350 Similarly, the Python dictionaries are also designed to look up for a specific
  1272 key and retrieve the corresponding value. Dictionaries are data structures that
   351 key and retrieve the corresponding value. Dictionaries are data structures that
  1273 provide key-value mappings. Dictionaries are similar to lists except that instead
   352 provide key-value mappings. Dictionaries are similar to lists except that instead
  1274 of the values having integer indexes, dictionaries have keys or strings as indexes.
   353 of the values having integer indexes, dictionaries have keys or strings as indexes.
  1275 Let us look at an example of how to define dictionaries.
   354 Let us look at an example of how to define dictionaries.</p>
  1276      </p>
   355 <pre class="programlisting">
  1277      <pre class="programlisting">
   356 &gt;&gt;&gt; dct = { 'Sachin': 'Tendulkar', 'Rahul': 'Dravid', 'Anil': 'Kumble'}</pre>
  1278       &gt;&gt;&gt; dct = { 'Sachin': 'Tendulkar', 'Rahul': 'Dravid', 'Anil': 'Kumble'}
   357 <p id="ch4strings_dicts_26">The dictionary consists of pairs of strings, which are called <span class="emphasis"><em>keys</em></span> and their
  1279      </pre>
   358 corresponding <span class="emphasis"><em>values</em></span> separated by <span class="emphasis"><em>:</em></span> and each of these <span class="emphasis"><em>key-value</em></span> pairs are
  1280      <p id="ch4strings_dicts_26">
   359 comma(',') separated and the entire structure wrapped in a pair curly braces <span class="emphasis"><em>{}</em></span>.</p>
  1281       The dictionary consists of pairs of strings, which are called
   360 <pre class="programlisting">
  1282       <span class="emphasis">
   361 Note: The data inside a dictionary is not ordered. The order in which you enter
  1283        <em>
       
  1284         keys
       
  1285        </em>
       
  1286       </span>
       
  1287       and their
       
  1288 corresponding
       
  1289       <span class="emphasis">
       
  1290        <em>
       
  1291         values
       
  1292        </em>
       
  1293       </span>
       
  1294       separated by
       
  1295       <span class="emphasis">
       
  1296        <em>
       
  1297         :
       
  1298        </em>
       
  1299       </span>
       
  1300       and each of these
       
  1301       <span class="emphasis">
       
  1302        <em>
       
  1303         key-value
       
  1304        </em>
       
  1305       </span>
       
  1306       pairs are
       
  1307 comma(',') separated and the entire structure wrapped in a pair curly braces
       
  1308       <span class="emphasis">
       
  1309        <em>
       
  1310         {}
       
  1311        </em>
       
  1312       </span>
       
  1313       .
       
  1314      </p>
       
  1315      <pre class="programlisting">
       
  1316       Note: The data inside a dictionary is not ordered. The order in which you enter
       
  1317 the key-value pairs is not the order in which they are stored in the dictionary.
   362 the key-value pairs is not the order in which they are stored in the dictionary.
  1318 Python has an internal storage mechanism for that which is out of the purview
   363 Python has an internal storage mechanism for that which is out of the purview
  1319 of this document.
   364 of this document.</pre>
  1320      </pre>
   365 <div class="section" title="5.1.dict()">
  1321      <div class="section" title="5.1.dict()">
   366 <div class="titlepage"><div><div><h3 class="title">
  1322       <div class="titlepage">
   367 <a name="id3034868"></a>5.1.<span class="strong"><strong>dict()</strong></span>
  1323        <div>
   368 </h3></div></div></div>
  1324         <div>
   369 <p id="ch4strings_dicts_27">The <span class="strong"><strong>dict()</strong></span> function is used to create dictionaries from other mappings or other
  1325          <h3 class="title">
   370 dictionaries. Let us look at an example.</p>
  1326           <a name="id2632094">
   371 <pre class="programlisting">
  1327           </a>
   372 &gt;&gt;&gt; diction = dict(mat = 133, avg = 52.53)</pre>
  1328           5.1.
   373 <p id="ch4strings_dicts_28"><span class="strong"><strong>String Formatting with Dictionaries:</strong></span></p>
  1329           <span class="strong">
   374 <p id="ch4strings_dicts_29">String formatting was discussed in the previous section and it was mentioned that
  1330            <strong>
       
  1331             dict()
       
  1332            </strong>
       
  1333           </span>
       
  1334          </h3>
       
  1335         </div>
       
  1336        </div>
       
  1337       </div>
       
  1338       <p id="ch4strings_dicts_27">
       
  1339        The
       
  1340        <span class="strong">
       
  1341         <strong>
       
  1342          dict()
       
  1343         </strong>
       
  1344        </span>
       
  1345        function is used to create dictionaries from other mappings or other
       
  1346 dictionaries. Let us look at an example.
       
  1347       </p>
       
  1348       <pre class="programlisting">
       
  1349        &gt;&gt;&gt; diction = dict(mat = 133, avg = 52.53)
       
  1350       </pre>
       
  1351       <p id="ch4strings_dicts_28">
       
  1352        <span class="strong">
       
  1353         <strong>
       
  1354          String Formatting with Dictionaries:
       
  1355         </strong>
       
  1356        </span>
       
  1357       </p>
       
  1358       <p id="ch4strings_dicts_29">
       
  1359        String formatting was discussed in the previous section and it was mentioned that
       
  1360 dictionaries can also be used for formatting more than one value. This section
   375 dictionaries can also be used for formatting more than one value. This section
  1361 focuses on the formatting of strings using dictionaries. String formatting using
   376 focuses on the formatting of strings using dictionaries. String formatting using
  1362 dictionaries is more appealing than doing the same with tuples. Here the
   377 dictionaries is more appealing than doing the same with tuples. Here the <span class="emphasis"><em>keyword</em></span>
  1363        <span class="emphasis">
   378 can be used as a place holder and the <span class="emphasis"><em>value</em></span> corresponding to it is replaced in
  1364         <em>
   379 the formatted string. Let us look at an example.</p>
  1365          keyword
   380 <pre class="programlisting">
  1366         </em>
   381 &gt;&gt;&gt; player = { 'Name':'Rahul Dravid', 'Matches':133, 'Avg':52.53, '100s':26 }
  1367        </span>
       
  1368        can be used as a place holder and the
       
  1369        <span class="emphasis">
       
  1370         <em>
       
  1371          value
       
  1372         </em>
       
  1373        </span>
       
  1374        corresponding to it is replaced in
       
  1375 the formatted string. Let us look at an example.
       
  1376       </p>
       
  1377       <pre class="programlisting">
       
  1378        &gt;&gt;&gt; player = { 'Name':'Rahul Dravid', 'Matches':133, 'Avg':52.53, '100s':26 }
       
  1379 &gt;&gt;&gt; strng = '%(Name)s has played %(Matches)d with an average of %(Avg).2f and has %(100s)d hundreds to his name.'
   382 &gt;&gt;&gt; strng = '%(Name)s has played %(Matches)d with an average of %(Avg).2f and has %(100s)d hundreds to his name.'
  1380 &gt;&gt;&gt; print strng % player
   383 &gt;&gt;&gt; print strng % player
  1381 Rahul Dravid has played 133 with an average of 52.53 and has 26 hundreds to his name.
   384 Rahul Dravid has played 133 with an average of 52.53 and has 26 hundreds to his name.</pre>
  1382       </pre>
   385 </div>
  1383      </div>
   386 <div class="section" title="5.2.Dictionary Methods">
  1384      <div class="section" title="5.2.Dictionary Methods">
   387 <div class="titlepage"><div><div><h3 class="title">
  1385       <div class="titlepage">
   388 <a name="id3034932"></a>5.2.Dictionary Methods</h3></div></div></div>
  1386        <div>
   389 <div class="section" title="5.2.1.clear()">
  1387         <div>
   390 <div class="titlepage"><div><div><h4 class="title">
  1388          <h3 class="title">
   391 <a name="id3034940"></a>5.2.1.<span class="strong"><strong>clear()</strong></span>
  1389           <a name="id2632158">
   392 </h4></div></div></div>
  1390           </a>
   393 <p id="ch4strings_dicts_2a">The <span class="strong"><strong>clear()</strong></span> method removes all the existing <span class="emphasis"><em>key-value</em></span> pairs from a dictionary.
  1391           5.2.Dictionary Methods
   394 It returns <span class="emphasis"><em>None</em></span> or rather does not return anything. It is a method that changes
  1392          </h3>
       
  1393         </div>
       
  1394        </div>
       
  1395       </div>
       
  1396       <div class="section" title="5.2.1.clear()">
       
  1397        <div class="titlepage">
       
  1398         <div>
       
  1399          <div>
       
  1400           <h4 class="title">
       
  1401            <a name="id2632167">
       
  1402            </a>
       
  1403            5.2.1.
       
  1404            <span class="strong">
       
  1405             <strong>
       
  1406              clear()
       
  1407             </strong>
       
  1408            </span>
       
  1409           </h4>
       
  1410          </div>
       
  1411         </div>
       
  1412        </div>
       
  1413        <p id="ch4strings_dicts_2a">
       
  1414         The
       
  1415         <span class="strong">
       
  1416          <strong>
       
  1417           clear()
       
  1418          </strong>
       
  1419         </span>
       
  1420         method removes all the existing
       
  1421         <span class="emphasis">
       
  1422          <em>
       
  1423           key-value
       
  1424          </em>
       
  1425         </span>
       
  1426         pairs from a dictionary.
       
  1427 It returns
       
  1428         <span class="emphasis">
       
  1429          <em>
       
  1430           None
       
  1431          </em>
       
  1432         </span>
       
  1433         or rather does not return anything. It is a method that changes
       
  1434 the object. It has to be noted here that dictionaries are not immutable. Let us
   395 the object. It has to be noted here that dictionaries are not immutable. Let us
  1435 look at an example.
   396 look at an example.</p>
  1436        </p>
   397 <pre class="programlisting">
  1437        <pre class="programlisting">
   398 &gt;&gt;&gt; dct
  1438         &gt;&gt;&gt; dct
       
  1439 {'Anil': 'Kumble', 'Sachin': 'Tendulkar', 'Rahul': 'Dravid'}
   399 {'Anil': 'Kumble', 'Sachin': 'Tendulkar', 'Rahul': 'Dravid'}
  1440 &gt;&gt;&gt; dct.clear()
   400 &gt;&gt;&gt; dct.clear()
  1441 &gt;&gt;&gt; dct
   401 &gt;&gt;&gt; dct
  1442 {}
   402 {}</pre>
  1443        </pre>
   403 </div>
  1444       </div>
   404 <div class="section" title="5.2.2.copy()">
  1445       <div class="section" title="5.2.2.copy()">
   405 <div class="titlepage"><div><div><h4 class="title">
  1446        <div class="titlepage">
   406 <a name="id3034976"></a>5.2.2.<span class="strong"><strong>copy()</strong></span>
  1447         <div>
   407 </h4></div></div></div>
  1448          <div>
   408 <p id="ch4strings_dicts_2b">The <span class="strong"><strong>copy()</strong></span> returns a copy of a given dictionary. Let us look at an example.</p>
  1449           <h4 class="title">
   409 <pre class="programlisting">
  1450            <a name="id2632202">
   410 &gt;&gt;&gt; dct = {'Anil': 'Kumble', 'Sachin': 'Tendulkar', 'Rahul': 'Dravid'}
  1451            </a>
       
  1452            5.2.2.
       
  1453            <span class="strong">
       
  1454             <strong>
       
  1455              copy()
       
  1456             </strong>
       
  1457            </span>
       
  1458           </h4>
       
  1459          </div>
       
  1460         </div>
       
  1461        </div>
       
  1462        <p id="ch4strings_dicts_2b">
       
  1463         The
       
  1464         <span class="strong">
       
  1465          <strong>
       
  1466           copy()
       
  1467          </strong>
       
  1468         </span>
       
  1469         returns a copy of a given dictionary. Let us look at an example.
       
  1470        </p>
       
  1471        <pre class="programlisting">
       
  1472         &gt;&gt;&gt; dct = {'Anil': 'Kumble', 'Sachin': 'Tendulkar', 'Rahul': 'Dravid'}
       
  1473 &gt;&gt;&gt; dctcopy = dct.copy()
   411 &gt;&gt;&gt; dctcopy = dct.copy()
  1474 &gt;&gt;&gt; dctcopy
   412 &gt;&gt;&gt; dctcopy
  1475 {'Anil': 'Kumble', 'Sachin': 'Tendulkar', 'Rahul': 'Dravid'}
   413 {'Anil': 'Kumble', 'Sachin': 'Tendulkar', 'Rahul': 'Dravid'}</pre>
  1476        </pre>
   414 </div>
  1477       </div>
   415 <div class="section" title="5.2.3.get()">
  1478       <div class="section" title="5.2.3.get()">
   416 <div class="titlepage"><div><div><h4 class="title">
  1479        <div class="titlepage">
   417 <a name="id3035006"></a>5.2.3.<span class="strong"><strong>get()</strong></span>
  1480         <div>
   418 </h4></div></div></div>
  1481          <div>
   419 <p id="ch4strings_dicts_2c"><span class="strong"><strong>get()</strong></span> returns the <span class="emphasis"><em>value</em></span> for the <span class="emphasis"><em>key</em></span> passed as the argument and if the
  1482           <h4 class="title">
   420 <span class="emphasis"><em>key</em></span> does not exist in the dictionary, it returns <span class="emphasis"><em>None</em></span>. Let us look at an
  1483            <a name="id2632233">
   421 example.</p>
  1484            </a>
   422 <pre class="programlisting">
  1485            5.2.3.
   423 &gt;&gt;&gt; print dctcopy.get('Saurav')
  1486            <span class="strong">
       
  1487             <strong>
       
  1488              get()
       
  1489             </strong>
       
  1490            </span>
       
  1491           </h4>
       
  1492          </div>
       
  1493         </div>
       
  1494        </div>
       
  1495        <p id="ch4strings_dicts_2c">
       
  1496         <span class="strong">
       
  1497          <strong>
       
  1498           get()
       
  1499          </strong>
       
  1500         </span>
       
  1501         returns the
       
  1502         <span class="emphasis">
       
  1503          <em>
       
  1504           value
       
  1505          </em>
       
  1506         </span>
       
  1507         for the
       
  1508         <span class="emphasis">
       
  1509          <em>
       
  1510           key
       
  1511          </em>
       
  1512         </span>
       
  1513         passed as the argument and if the
       
  1514         <span class="emphasis">
       
  1515          <em>
       
  1516           key
       
  1517          </em>
       
  1518         </span>
       
  1519         does not exist in the dictionary, it returns
       
  1520         <span class="emphasis">
       
  1521          <em>
       
  1522           None
       
  1523          </em>
       
  1524         </span>
       
  1525         . Let us look at an
       
  1526 example.
       
  1527        </p>
       
  1528        <pre class="programlisting">
       
  1529         &gt;&gt;&gt; print dctcopy.get('Saurav')
       
  1530 None
   424 None
  1531 &gt;&gt;&gt; print dctcopy.get('Anil')
   425 &gt;&gt;&gt; print dctcopy.get('Anil')
  1532 Kumble
   426 Kumble</pre>
  1533        </pre>
   427 </div>
  1534       </div>
   428 <div class="section" title="5.2.4.has_key()">
  1535       <div class="section" title="5.2.4.has_key()">
   429 <div class="titlepage"><div><div><h4 class="title">
  1536        <div class="titlepage">
   430 <a name="id3035044"></a>5.2.4.<span class="strong"><strong>has_key()</strong></span>
  1537         <div>
   431 </h4></div></div></div>
  1538          <div>
   432 <p id="ch4strings_dicts_2d">This method returns <span class="emphasis"><em>True</em></span> if the given <span class="emphasis"><em>key</em></span> is in the dictionary, else it returns
  1539           <h4 class="title">
   433 <span class="emphasis"><em>False</em></span>.</p>
  1540            <a name="id2632271">
   434 <pre class="programlisting">
  1541            </a>
   435 &gt;&gt;&gt; dctcopy.has_key('Saurav')
  1542            5.2.4.
       
  1543            <span class="strong">
       
  1544             <strong>
       
  1545              has_key()
       
  1546             </strong>
       
  1547            </span>
       
  1548           </h4>
       
  1549          </div>
       
  1550         </div>
       
  1551        </div>
       
  1552        <p id="ch4strings_dicts_2d">
       
  1553         This method returns
       
  1554         <span class="emphasis">
       
  1555          <em>
       
  1556           True
       
  1557          </em>
       
  1558         </span>
       
  1559         if the given
       
  1560         <span class="emphasis">
       
  1561          <em>
       
  1562           key
       
  1563          </em>
       
  1564         </span>
       
  1565         is in the dictionary, else it returns
       
  1566         <span class="emphasis">
       
  1567          <em>
       
  1568           False
       
  1569          </em>
       
  1570         </span>
       
  1571         .
       
  1572        </p>
       
  1573        <pre class="programlisting">
       
  1574         &gt;&gt;&gt; dctcopy.has_key('Saurav')
       
  1575 False
   436 False
  1576 &gt;&gt;&gt; dctcopy.has_key('Sachin')
   437 &gt;&gt;&gt; dctcopy.has_key('Sachin')
  1577 True
   438 True</pre>
  1578        </pre>
   439 </div>
  1579       </div>
   440 <div class="section" title="5.2.5.pop()">
  1580       <div class="section" title="5.2.5.pop()">
   441 <div class="titlepage"><div><div><h4 class="title">
  1581        <div class="titlepage">
   442 <a name="id3035074"></a>5.2.5.<span class="strong"><strong>pop()</strong></span>
  1582         <div>
   443 </h4></div></div></div>
  1583          <div>
   444 <p id="ch4strings_dicts_2e">This method is used to retrieve the <span class="emphasis"><em>value</em></span> of a given <span class="emphasis"><em>key</em></span> and subsequently
  1584           <h4 class="title">
   445 remove the <span class="emphasis"><em>key-value</em></span> pair from the dictionary. Let us look at an example.</p>
  1585            <a name="id2632301">
   446 <pre class="programlisting">
  1586            </a>
   447 &gt;&gt;&gt; print dctcopy.pop('Sachin')
  1587            5.2.5.
       
  1588            <span class="strong">
       
  1589             <strong>
       
  1590              pop()
       
  1591             </strong>
       
  1592            </span>
       
  1593           </h4>
       
  1594          </div>
       
  1595         </div>
       
  1596        </div>
       
  1597        <p id="ch4strings_dicts_2e">
       
  1598         This method is used to retrieve the
       
  1599         <span class="emphasis">
       
  1600          <em>
       
  1601           value
       
  1602          </em>
       
  1603         </span>
       
  1604         of a given
       
  1605         <span class="emphasis">
       
  1606          <em>
       
  1607           key
       
  1608          </em>
       
  1609         </span>
       
  1610         and subsequently
       
  1611 remove the
       
  1612         <span class="emphasis">
       
  1613          <em>
       
  1614           key-value
       
  1615          </em>
       
  1616         </span>
       
  1617         pair from the dictionary. Let us look at an example.
       
  1618        </p>
       
  1619        <pre class="programlisting">
       
  1620         &gt;&gt;&gt; print dctcopy.pop('Sachin')
       
  1621 Tendulkar
   448 Tendulkar
  1622 &gt;&gt;&gt; dctcopy
   449 &gt;&gt;&gt; dctcopy
  1623 {'Anil': 'Kumble', 'Rahul': 'Dravid'}
   450 {'Anil': 'Kumble', 'Rahul': 'Dravid'}</pre>
  1624        </pre>
   451 </div>
  1625       </div>
   452 <div class="section" title="5.2.6.popitem()">
  1626       <div class="section" title="5.2.6.popitem()">
   453 <div class="titlepage"><div><div><h4 class="title">
  1627        <div class="titlepage">
   454 <a name="id3035108"></a>5.2.6.<span class="strong"><strong>popitem()</strong></span>
  1628         <div>
   455 </h4></div></div></div>
  1629          <div>
   456 <p id="ch4strings_dicts_2f">This method randomly pops a <span class="emphasis"><em>key-value</em></span> pair from a dictionary and returns it.
  1630           <h4 class="title">
   457 The <span class="emphasis"><em>key-value</em></span> pair returned is removed from the dictionary. Let us look at an
  1631            <a name="id2632335">
   458 example.</p>
  1632            </a>
   459 <pre class="programlisting">
  1633            5.2.6.
   460 &gt;&gt;&gt; print dctcopy.popitem()
  1634            <span class="strong">
       
  1635             <strong>
       
  1636              popitem()
       
  1637             </strong>
       
  1638            </span>
       
  1639           </h4>
       
  1640          </div>
       
  1641         </div>
       
  1642        </div>
       
  1643        <p id="ch4strings_dicts_2f">
       
  1644         This method randomly pops a
       
  1645         <span class="emphasis">
       
  1646          <em>
       
  1647           key-value
       
  1648          </em>
       
  1649         </span>
       
  1650         pair from a dictionary and returns it.
       
  1651 The
       
  1652         <span class="emphasis">
       
  1653          <em>
       
  1654           key-value
       
  1655          </em>
       
  1656         </span>
       
  1657         pair returned is removed from the dictionary. Let us look at an
       
  1658 example.
       
  1659        </p>
       
  1660        <pre class="programlisting">
       
  1661         &gt;&gt;&gt; print dctcopy.popitem()
       
  1662 ('Anil', 'Kumble')
   461 ('Anil', 'Kumble')
  1663 &gt;&gt;&gt; dctcopy
   462 &gt;&gt;&gt; dctcopy
  1664 {'Rahul': 'Dravid'}
   463 {'Rahul': 'Dravid'}
  1665 
   464 
  1666 Note that the item chosen is completely random since dictionaries are unordered
   465 Note that the item chosen is completely random since dictionaries are unordered
  1667 as mentioned earlier.
   466 as mentioned earlier.</pre>
  1668        </pre>
   467 </div>
  1669       </div>
   468 <div class="section" title="5.2.7.update()">
  1670       <div class="section" title="5.2.7.update()">
   469 <div class="titlepage"><div><div><h4 class="title">
  1671        <div class="titlepage">
   470 <a name="id3035144"></a>5.2.7.<span class="strong"><strong>update()</strong></span>
  1672         <div>
   471 </h4></div></div></div>
  1673          <div>
   472 <p id="ch4strings_dicts_30">The <span class="strong"><strong>update()</strong></span> method updates the contents of one dictionary with the contents
  1674           <h4 class="title">
   473 of another dictionary. For items with existing <span class="emphasis"><em>keys</em></span> their <span class="emphasis"><em>values</em></span> are updated,
  1675            <a name="id2632371">
   474 and the rest of the items are added. Let us look at an example.</p>
  1676            </a>
   475 <pre class="programlisting">
  1677            5.2.7.
   476 &gt;&gt;&gt; dctcopy.update(dct)
  1678            <span class="strong">
       
  1679             <strong>
       
  1680              update()
       
  1681             </strong>
       
  1682            </span>
       
  1683           </h4>
       
  1684          </div>
       
  1685         </div>
       
  1686        </div>
       
  1687        <p id="ch4strings_dicts_30">
       
  1688         The
       
  1689         <span class="strong">
       
  1690          <strong>
       
  1691           update()
       
  1692          </strong>
       
  1693         </span>
       
  1694         method updates the contents of one dictionary with the contents
       
  1695 of another dictionary. For items with existing
       
  1696         <span class="emphasis">
       
  1697          <em>
       
  1698           keys
       
  1699          </em>
       
  1700         </span>
       
  1701         their
       
  1702         <span class="emphasis">
       
  1703          <em>
       
  1704           values
       
  1705          </em>
       
  1706         </span>
       
  1707         are updated,
       
  1708 and the rest of the items are added. Let us look at an example.
       
  1709        </p>
       
  1710        <pre class="programlisting">
       
  1711         &gt;&gt;&gt; dctcopy.update(dct)
       
  1712 &gt;&gt;&gt; dct
   477 &gt;&gt;&gt; dct
  1713 {'Anil': 'Kumble', 'Sachin': 'Tendulkar', 'Rahul': 'Dravid'}
   478 {'Anil': 'Kumble', 'Sachin': 'Tendulkar', 'Rahul': 'Dravid'}
  1714 &gt;&gt;&gt; dctcopy
   479 &gt;&gt;&gt; dctcopy
  1715 {'Anil': 'Kumble', 'Sachin': 'Tendulkar', 'Rahul': 'Dravid'}
   480 {'Anil': 'Kumble', 'Sachin': 'Tendulkar', 'Rahul': 'Dravid'}</pre>
  1716        </pre>
   481 </div>
  1717       </div>
   482 </div>
  1718      </div>
   483 </div>
  1719     </div>
   484 </div>
  1720    </div>
   485 </div></body>
  1721   </div>
       
  1722  </body>
       
  1723 </html>
   486 </html>
       
   487