SEESenv/web/html/ch4strings_dicts.html
author amit@thunder
Thu, 25 Feb 2010 00:45:20 +0530
changeset 28 514098969b11
parent 27 cb14131583c6
permissions -rw-r--r--
Testing
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     1
<html>
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
     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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
     3
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="chapter" id="ch4strings_dicts">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
     4
<div class="titlepage"></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
     5
<div class="toc">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
     6
<p><b>Table of Contents</b></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
     7
<dl>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
     8
<dt><span class="article"><a href="#id2983086">Strings</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
     9
<dd><dl>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    10
<dt><span class="section"><a href="#id2981673">1. String Formatting</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    11
<dt><span class="section"><a href="#id3034090">2. String Methods</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    12
<dd><dl>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    13
<dt><span class="section"><a href="#id3034105">2.1. <span class="strong"><strong>find</strong></span></a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    14
<dt><span class="section"><a href="#id3034135">2.2. <span class="strong"><strong>join</strong></span></a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    15
<dt><span class="section"><a href="#id3034175">2.3. <span class="strong"><strong>lower</strong></span></a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    16
<dt><span class="section"><a href="#id3034206">2.4. <span class="strong"><strong>replace</strong></span></a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    17
<dt><span class="section"><a href="#id3034262">2.5. <span class="strong"><strong>split</strong></span></a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    18
<dt><span class="section"><a href="#id3034321">2.6. <span class="strong"><strong>strip</strong></span></a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    19
</dl></dd>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    20
<dt><span class="section"><a href="#id3034366">3. Introduction to the standard library</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    21
<dt><span class="section"><a href="#id3034442">4. I/O: Reading and Writing Files</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    22
<dd><dl>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    23
<dt><span class="section"><a href="#id3034472">4.1. Opening Files</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    24
<dt><span class="section"><a href="#id3034646">4.2. Reading and Writing files</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    25
</dl></dd>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    26
<dt><span class="section"><a href="#id3034811">5. Dictionaries</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    27
<dd><dl>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    28
<dt><span class="section"><a href="#id3034868">5.1. <span class="strong"><strong>dict()</strong></span></a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    29
<dt><span class="section"><a href="#id3034932">5.2. Dictionary Methods</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    30
</dl></dd>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    31
</dl></dd>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    32
</dl>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    33
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    34
<div class="article" title="Strings">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    35
<div class="titlepage">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    36
<div><div><h2 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    37
<a name="id2983086"></a>Strings</h2></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    38
<hr />
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    39
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    40
<div class="toc">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    41
<p><b>Table of Contents</b></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    42
<dl>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    43
<dt><span class="section"><a href="#id2981673">1. String Formatting</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    44
<dt><span class="section"><a href="#id3034090">2. String Methods</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    45
<dd><dl>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    46
<dt><span class="section"><a href="#id3034105">2.1. <span class="strong"><strong>find</strong></span></a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    47
<dt><span class="section"><a href="#id3034135">2.2. <span class="strong"><strong>join</strong></span></a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    48
<dt><span class="section"><a href="#id3034175">2.3. <span class="strong"><strong>lower</strong></span></a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    49
<dt><span class="section"><a href="#id3034206">2.4. <span class="strong"><strong>replace</strong></span></a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    50
<dt><span class="section"><a href="#id3034262">2.5. <span class="strong"><strong>split</strong></span></a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    51
<dt><span class="section"><a href="#id3034321">2.6. <span class="strong"><strong>strip</strong></span></a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    52
</dl></dd>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    53
<dt><span class="section"><a href="#id3034366">3. Introduction to the standard library</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    54
<dt><span class="section"><a href="#id3034442">4. I/O: Reading and Writing Files</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    55
<dd><dl>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    56
<dt><span class="section"><a href="#id3034472">4.1. Opening Files</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    57
<dt><span class="section"><a href="#id3034646">4.2. Reading and Writing files</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    58
</dl></dd>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    59
<dt><span class="section"><a href="#id3034811">5. Dictionaries</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    60
<dd><dl>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    61
<dt><span class="section"><a href="#id3034868">5.1. <span class="strong"><strong>dict()</strong></span></a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    62
<dt><span class="section"><a href="#id3034932">5.2. Dictionary Methods</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    63
</dl></dd>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    64
</dl>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    65
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    66
<p id="ch4strings_dicts_1">Strings were briefly introduced previously in the introduction document. In this
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    67
section strings will be presented in greater detail. All the standard operations
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    68
that can be performed on sequences such as indexing, slicing, multiplication, length
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    69
minimum and maximum can be performed on string variables as well. One thing to
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    70
be noted is that strings are immutable, which means that string variables are
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    71
unchangeable. Hence, all item and slice assignments on strings are illegal.
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    72
Let us look at a few example.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    73
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    74
&gt;&gt;&gt; name = 'PythonFreak'
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    75
&gt;&gt;&gt; print name[3]
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    76
h
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    77
&gt;&gt;&gt; print name[-1]
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    78
k
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    79
&gt;&gt;&gt; print name[6:]
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    80
Freak
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    81
&gt;&gt;&gt; name[6:0] = 'Maniac'
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    82
Traceback (most recent call last):
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    83
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    84
TypeError: 'str' object does not support item assignment</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    85
<p id="ch4strings_dicts_2">This is quite expected, since string objects are immutable as already mentioned.
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    86
The error message is clear in mentioning that 'str' object does not support item
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    87
assignment.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    88
<div class="section" title="1.String Formatting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    89
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    90
<a name="id2981673"></a>1.String Formatting</h2></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    91
<p id="ch4strings_dicts_3">String formatting can be performed using the string formatting operator represented
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    92
as the percent (%) sign. The string placed before the % sign is formatted with
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    93
the value placed to the right of it. Let us look at a simple example.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    94
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    95
&gt;&gt;&gt; format = 'Hello %s, from PythonFreak'
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    96
&gt;&gt;&gt; str1 = 'world!'
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
    97
&gt;&gt;&gt; print format % str1
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    98
Hello world!, from PythonFreak</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    99
<p id="ch4strings_dicts_4">The %s parts of the format string are called the coversion specifiers. The coversion
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   100
specifiers mark the places where the formatting has to be performed in a string.
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   101
In the example the %s is replaced by the value of str1. More than one value can
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   102
also be formatted at a time by specifying the values to be formatted using tuples
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   103
and dictionaries (explained in later sections). Let us look at an example.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   104
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   105
&gt;&gt;&gt; format = 'Hello %s, from %s'
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   106
&gt;&gt;&gt; values = ('world!', 'PythonFreak')
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   107
&gt;&gt;&gt; print format % values
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   108
Hello world!, from PythonFreak</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   109
<p id="ch4strings_dicts_5">In this example it can be observed that the format string contains two conversion
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   110
specifiers and they are formatted using the tuple of values as shown.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   111
<p id="ch4strings_dicts_6">The s in %s specifies that the value to be replaced is of type string. Values of
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   112
other types can be specified as well such as integers and floats. Integers are
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   113
specified as %d and floats as %f. The precision with which the integer or the
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   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>)
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   115
followed by the precision value.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   116
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   117
<div class="section" title="2.String Methods">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   118
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   119
<a name="id3034090"></a>2.String Methods</h2></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   120
<p id="ch4strings_dicts_7">Similar to list methods, strings also have a rich set of methods to perform various
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   121
operations on strings. Some of the most important and popular ones are presented
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   122
in this section.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   123
<div class="section" title="2.1.find">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   124
<div class="titlepage"><div><div><h3 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   125
<a name="id3034105"></a>2.1.<span class="strong"><strong>find</strong></span>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   126
</h3></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   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
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   128
returns the left most index of the first occurence of the substring. If the
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   129
substring is not found in the string then it returns -1. Let us look at a few
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   130
examples.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   131
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   132
&gt;&gt;&gt; longstring = 'Hello world!, from PythonFreak'
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   133
&gt;&gt;&gt; longstring.find('Python')
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   134
19
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   135
&gt;&gt;&gt; longstring.find('Perl')
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   136
-1</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   137
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   138
<div class="section" title="2.2.join">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   139
<div class="titlepage"><div><div><h3 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   140
<a name="id3034135"></a>2.2.<span class="strong"><strong>join</strong></span>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   141
</h3></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   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
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   143
elements that are to be join ed should all be strings. Let us look at a few
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   144
examples.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   145
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   146
&gt;&gt;&gt; seq = ['With', 'great', 'power', 'comes', 'great', 'responsibility']
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   147
&gt;&gt;&gt; sep = ' '
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   148
&gt;&gt;&gt; sep.join(seq)
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   149
'With great power comes great responsibility'
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   150
&gt;&gt;&gt; sep = ',!'
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   151
&gt;&gt;&gt; sep.join(seq)
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   152
'With,!great,!power,!comes,!great,!responsibility'</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   153
<p id="ch4strings_dicts_a"><span class="emphasis"><em>Try this yourself</em></span></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   154
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   155
&gt;&gt;&gt; seq = [12,34,56,78]
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   156
&gt;&gt;&gt; sep.join(seq)</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   157
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   158
<div class="section" title="2.3.lower">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   159
<div class="titlepage"><div><div><h3 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   160
<a name="id3034175"></a>2.3.<span class="strong"><strong>lower</strong></span>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   161
</h3></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   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
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   163
to lower case. It is specially useful in cases where the programmers deal with case
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   164
insensitive data. Let us look at a few examples.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   165
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   166
&gt;&gt;&gt; sometext = 'Hello world!, from PythonFreak'
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   167
&gt;&gt;&gt; sometext.lower()
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   168
'hello world!, from pythonfreak'</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   169
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   170
<div class="section" title="2.4.replace">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   171
<div class="titlepage"><div><div><h3 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   172
<a name="id3034206"></a>2.4.<span class="strong"><strong>replace</strong></span>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   173
</h3></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   174
<p id="ch4strings_dicts_c">The <span class="strong"><strong>replace</strong></span> method replaces a substring with another substring within
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   175
a given string and returns the new string. Let us look at an example.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   176
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   177
&gt;&gt;&gt; sometext = 'Concise, precise and criticise is some of the words that end with ise'
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   178
&gt;&gt;&gt; sometext.replace('is', 'are')
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   179
'Concaree, precaree and criticaree are some of the words that end with aree'</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   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,
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   182
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   183
<div class="section" title="2.5.split">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   184
<div class="titlepage"><div><div><h3 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   185
<a name="id3034262"></a>2.5.<span class="strong"><strong>split</strong></span>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   186
</h3></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   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
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   188
<span class="strong"><strong>join</strong></span> method. It is used to split a string based on the argument passed as the
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   189
delimiter. It returns a list of strings. By default when no argument is passed it
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   190
splits with <span class="emphasis"><em>space</em></span> (' ') as the delimiter. Let us look at an example.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   191
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   192
&gt;&gt;&gt; grocerylist = 'butter, cucumber, beer(a grocery item??), wheatbread'
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   193
&gt;&gt;&gt; grocerylist.split(',')
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   194
['butter', ' cucumber', ' beer(a grocery item??)', ' wheatbread']
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   195
&gt;&gt;&gt; grocerylist.split()
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   196
['butter,', 'cucumber,', 'beer(a', 'grocery', 'item??),', 'wheatbread']</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   197
<p id="ch4strings_dicts_f">Observe here that in the second case when the delimiter argument was not set
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   198
<span class="strong"><strong>split</strong></span> was done with <span class="emphasis"><em>space</em></span> as the delimiter.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   199
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   200
<div class="section" title="2.6.strip">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   201
<div class="titlepage"><div><div><h3 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   202
<a name="id3034321"></a>2.6.<span class="strong"><strong>strip</strong></span>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   203
</h3></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   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
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   205
to the left and right of a string, but not the whitespaces within a string. Let
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   206
us look at an example.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   207
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   208
&gt;&gt;&gt; spacedtext = "               Where's the text??                 "
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   209
&gt;&gt;&gt; spacedtext.strip()
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   210
"Where's the text??"</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   211
<p id="ch4strings_dicts_11">Observe that the whitespaces between the words have not been removed.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   212
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   213
Note: Very important thing to note is that all the methods shown above do not
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   214
      transform the source string. The source string still remains the same.
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   215
      Remember that **strings are immutable**.</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   216
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   217
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   218
<div class="section" title="3.Introduction to the standard library">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   219
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   220
<a name="id3034366"></a>3.Introduction to the standard library</h2></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   221
<p id="ch4strings_dicts_12">Python is often referred to as a "Batteries included!" language, mainly because
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   222
of the Python Standard Library. The Python Standard Library provides an extensive
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   223
set of features some of which are available directly for use while some require to
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   224
import a few <span class="strong"><strong>modules</strong></span>. The Standard Library provides various built-in functions
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   225
like:</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   226
<div class="itemizedlist"><ul class="itemizedlist" type="*">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   227
<li class="listitem" style="list-style-type: *"><p id="ch4strings_dicts_13"><span class="strong"><strong>abs()</strong></span></p></li>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   228
<li class="listitem" style="list-style-type: *"><p id="ch4strings_dicts_14"><span class="strong"><strong>dict()</strong></span></p></li>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   229
<li class="listitem" style="list-style-type: *"><p id="ch4strings_dicts_15"><span class="strong"><strong>enumerate()</strong></span></p></li>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   230
</ul></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   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.
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   232
More information about the Python Standard Library is available </p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   233
<div class="reference">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   234
<div class="titlepage"><hr /></div>http://docs.python.org/library/</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   235
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   236
<div class="section" title="4.I/O: Reading and Writing Files">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   237
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   238
<a name="id3034442"></a>4.I/O: Reading and Writing Files</h2></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   239
<p id="ch4strings_dicts_17">Files are very important aspects when it comes to computing and programming.
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   240
Up until now the focus has been on small programs that interacted with users
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   241
through <span class="strong"><strong>input()</strong></span> and <span class="strong"><strong>raw_input()</strong></span>. Generally, for computational purposes
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   242
it becomes necessary to handle files, which are usually large in size as well.
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   243
This section focuses on basics of file handling.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   244
<div class="section" title="4.1.Opening Files">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   245
<div class="titlepage"><div><div><h3 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   246
<a name="id3034472"></a>4.1.Opening Files</h3></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   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
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   248
out of which 2 are optional. Let us look at the syntax of <span class="strong"><strong>open()</strong></span>:</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   249
<p id="ch4strings_dicts_19"><span class="emphasis"><em>f = open( filename, mode, buffering)</em></span></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   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
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   251
optional. The <span class="emphasis"><em>filename</em></span> should be a string and it should be the complete path
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   252
to the file to be opened (The path can be absolute or relative). Let us look at
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   253
an example.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   254
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   255
&gt;&gt;&gt; f = open ('basic_python/interim_assessment.rst')</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   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.
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   257
The following are the valid mode arguments:</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   258
<p id="ch4strings_dicts_1c"><span class="strong"><strong>r</strong></span> - Read mode
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   259
<span class="strong"><strong>w</strong></span> - Write mode
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   260
<span class="strong"><strong>a</strong></span> - Append mode
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   261
<span class="strong"><strong>b</strong></span> - Binary mode
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   262
<span class="strong"><strong>+</strong></span> - Read/Write mode</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   263
<p id="ch4strings_dicts_1d">The read mode opens the file as a read-only document. The write mode opens the
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   264
file in the Write only mode. In the write mode, if the file existed prior to the
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   265
opening, the previous contents of the file are erased. The append mode opens the
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   266
file in the write mode but the previous contents of the file are not erased and
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   267
the current data is appended onto the file.
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   268
The binary and the read/write modes are special in the sense that they are added
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   269
onto other modes. The read/write mode opens the file in the reading and writing
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   270
mode combined. The binary mode can be used to open a files that do not contain
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   271
text. Binary files such as images should be opened in the binary mode. Let us look
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   272
at a few examples.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   273
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   274
&gt;&gt;&gt; f = open ('basic_python/interim_assessment.rst', 'r')
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   275
&gt;&gt;&gt; f = open ('armstrong.py', 'r+')</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   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
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   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,
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   278
that is the file is loaded on to the main memory and the changes made to the file are
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   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
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   280
changes are directly written on to the disk immediately.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   281
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   282
<div class="section" title="4.2.Reading and Writing files">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   283
<div class="titlepage"><div><div><h3 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   284
<a name="id3034646"></a>4.2.Reading and Writing files</h3></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   285
<div class="section" title="4.2.1.write()">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   286
<div class="titlepage"><div><div><h4 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   287
<a name="id3034655"></a>4.2.1.<span class="strong"><strong>write()</strong></span>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   288
</h4></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   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
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   290
be written as the argument. The data can be a string, an integer, a float or any
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   291
other datatype. In order to be able to write data onto a file, the file has to
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   293
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   294
<div class="section" title="4.2.2.read()">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   295
<div class="titlepage"><div><div><h4 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   296
<a name="id3034694"></a>4.2.2.<span class="strong"><strong>read()</strong></span>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   297
</h4></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   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
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   299
to be read as the argument. If nothing is specified by default it reads the entire
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   300
contents from the current position to the end of file.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   301
<p id="ch4strings_dicts_21">Let us look at a few examples:</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   302
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   303
&gt;&gt;&gt; f = open ('randomtextfile', 'w')
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   304
&gt;&gt;&gt; f.write('Hello all, this is PythonFreak. This is a random text file.')
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   305
&gt;&gt;&gt; f = open ('../randomtextfile', 'r')
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   306
&gt;&gt;&gt; f = open ('../randomtextfile', 'r')
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   307
&gt;&gt;&gt; f.read(5)
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   308
'Hello'
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   309
&gt;&gt;&gt; f.read()
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   310
' all, this is PythonFreak. This is a random text file.'
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   311
&gt;&gt;&gt; f.close()</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   312
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   313
<div class="section" title="4.2.3.readline()">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   314
<div class="titlepage"><div><div><h4 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   315
<a name="id3034729"></a>4.2.3.<span class="strong"><strong>readline()</strong></span>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   316
</h4></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   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
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   318
of a file at a time. When an argument is passed to <span class="strong"><strong>readline()</strong></span> it reads that
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   319
many bytes from the current line.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   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
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   321
<span class="strong"><strong>for</strong></span> construct. Let us look at this block of code as an example.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   322
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   323
&gt;&gt;&gt; f = open('../randomtextfile', 'r')
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   324
&gt;&gt;&gt; for line in f:
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   325
...     print line
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   326
...
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   327
Hello all!
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   328
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   329
This is PythonFreak on the second line.
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   330
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   331
This is a random text file on line 3</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   332
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   333
<div class="section" title="4.2.4.close()">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   334
<div class="titlepage"><div><div><h4 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   335
<a name="id3034788"></a>4.2.4.<span class="strong"><strong>close()</strong></span>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   336
</h4></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   337
<p id="ch4strings_dicts_24">One must always close all the files that have been opened. Although, files opened
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   338
will be closed automatically when the program ends. When files opened in read mode
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   339
are not closed it might lead to uselessly locked sometimes. In case of files
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   340
opened in the write mode it is more important to close the files. This is because,
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   341
Python maybe using the file in the buffering mode and when the file is not closed
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   342
the buffer maybe lost completely and the changes made to the file are lost forever.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   343
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   344
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   345
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   346
<div class="section" title="5.Dictionaries">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   347
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   348
<a name="id3034811"></a>5.Dictionaries</h2></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   349
<p id="ch4strings_dicts_25">A dictionary in general, are designed to be able to look up meanings of words.
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   350
Similarly, the Python dictionaries are also designed to look up for a specific
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   351
key and retrieve the corresponding value. Dictionaries are data structures that
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   352
provide key-value mappings. Dictionaries are similar to lists except that instead
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   353
of the values having integer indexes, dictionaries have keys or strings as indexes.
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   354
Let us look at an example of how to define dictionaries.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   355
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   356
&gt;&gt;&gt; dct = { 'Sachin': 'Tendulkar', 'Rahul': 'Dravid', 'Anil': 'Kumble'}</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   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
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   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
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   359
comma(',') separated and the entire structure wrapped in a pair curly braces <span class="emphasis"><em>{}</em></span>.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   360
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   361
Note: The data inside a dictionary is not ordered. The order in which you enter
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   362
the key-value pairs is not the order in which they are stored in the dictionary.
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   363
Python has an internal storage mechanism for that which is out of the purview
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   364
of this document.</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   365
<div class="section" title="5.1.dict()">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   366
<div class="titlepage"><div><div><h3 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   367
<a name="id3034868"></a>5.1.<span class="strong"><strong>dict()</strong></span>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   368
</h3></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   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
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   370
dictionaries. Let us look at an example.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   371
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   372
&gt;&gt;&gt; diction = dict(mat = 133, avg = 52.53)</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   373
<p id="ch4strings_dicts_28"><span class="strong"><strong>String Formatting with Dictionaries:</strong></span></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   374
<p id="ch4strings_dicts_29">String formatting was discussed in the previous section and it was mentioned that
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   375
dictionaries can also be used for formatting more than one value. This section
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   376
focuses on the formatting of strings using dictionaries. String formatting using
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   377
dictionaries is more appealing than doing the same with tuples. Here the <span class="emphasis"><em>keyword</em></span>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   378
can be used as a place holder and the <span class="emphasis"><em>value</em></span> corresponding to it is replaced in
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   379
the formatted string. Let us look at an example.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   380
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   381
&gt;&gt;&gt; player = { 'Name':'Rahul Dravid', 'Matches':133, 'Avg':52.53, '100s':26 }
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   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.'
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   383
&gt;&gt;&gt; print strng % player
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   384
Rahul Dravid has played 133 with an average of 52.53 and has 26 hundreds to his name.</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   385
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   386
<div class="section" title="5.2.Dictionary Methods">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   387
<div class="titlepage"><div><div><h3 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   388
<a name="id3034932"></a>5.2.Dictionary Methods</h3></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   389
<div class="section" title="5.2.1.clear()">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   390
<div class="titlepage"><div><div><h4 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   391
<a name="id3034940"></a>5.2.1.<span class="strong"><strong>clear()</strong></span>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   392
</h4></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   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.
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   394
It returns <span class="emphasis"><em>None</em></span> or rather does not return anything. It is a method that changes
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   395
the object. It has to be noted here that dictionaries are not immutable. Let us
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   396
look at an example.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   397
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   398
&gt;&gt;&gt; dct
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   399
{'Anil': 'Kumble', 'Sachin': 'Tendulkar', 'Rahul': 'Dravid'}
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   400
&gt;&gt;&gt; dct.clear()
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   401
&gt;&gt;&gt; dct
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   402
{}</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   403
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   404
<div class="section" title="5.2.2.copy()">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   405
<div class="titlepage"><div><div><h4 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   406
<a name="id3034976"></a>5.2.2.<span class="strong"><strong>copy()</strong></span>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   407
</h4></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   409
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   410
&gt;&gt;&gt; dct = {'Anil': 'Kumble', 'Sachin': 'Tendulkar', 'Rahul': 'Dravid'}
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   411
&gt;&gt;&gt; dctcopy = dct.copy()
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   412
&gt;&gt;&gt; dctcopy
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   413
{'Anil': 'Kumble', 'Sachin': 'Tendulkar', 'Rahul': 'Dravid'}</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   414
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   415
<div class="section" title="5.2.3.get()">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   416
<div class="titlepage"><div><div><h4 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   417
<a name="id3035006"></a>5.2.3.<span class="strong"><strong>get()</strong></span>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   418
</h4></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   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
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   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
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   421
example.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   422
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   423
&gt;&gt;&gt; print dctcopy.get('Saurav')
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   424
None
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   425
&gt;&gt;&gt; print dctcopy.get('Anil')
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   426
Kumble</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   427
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   428
<div class="section" title="5.2.4.has_key()">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   429
<div class="titlepage"><div><div><h4 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   430
<a name="id3035044"></a>5.2.4.<span class="strong"><strong>has_key()</strong></span>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   431
</h4></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   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
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   433
<span class="emphasis"><em>False</em></span>.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   434
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   435
&gt;&gt;&gt; dctcopy.has_key('Saurav')
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   436
False
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   437
&gt;&gt;&gt; dctcopy.has_key('Sachin')
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   438
True</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   439
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   440
<div class="section" title="5.2.5.pop()">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   441
<div class="titlepage"><div><div><h4 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   442
<a name="id3035074"></a>5.2.5.<span class="strong"><strong>pop()</strong></span>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   443
</h4></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   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
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   445
remove the <span class="emphasis"><em>key-value</em></span> pair from the dictionary. Let us look at an example.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   446
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   447
&gt;&gt;&gt; print dctcopy.pop('Sachin')
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   448
Tendulkar
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   449
&gt;&gt;&gt; dctcopy
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   450
{'Anil': 'Kumble', 'Rahul': 'Dravid'}</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   451
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   452
<div class="section" title="5.2.6.popitem()">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   453
<div class="titlepage"><div><div><h4 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   454
<a name="id3035108"></a>5.2.6.<span class="strong"><strong>popitem()</strong></span>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   455
</h4></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   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.
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   457
The <span class="emphasis"><em>key-value</em></span> pair returned is removed from the dictionary. Let us look at an
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   458
example.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   459
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   460
&gt;&gt;&gt; print dctcopy.popitem()
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   461
('Anil', 'Kumble')
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   462
&gt;&gt;&gt; dctcopy
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   463
{'Rahul': 'Dravid'}
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   464
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   465
Note that the item chosen is completely random since dictionaries are unordered
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   466
as mentioned earlier.</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   467
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   468
<div class="section" title="5.2.7.update()">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   469
<div class="titlepage"><div><div><h4 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   470
<a name="id3035144"></a>5.2.7.<span class="strong"><strong>update()</strong></span>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   471
</h4></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   472
<p id="ch4strings_dicts_30">The <span class="strong"><strong>update()</strong></span> method updates the contents of one dictionary with the contents
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   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,
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   474
and the rest of the items are added. Let us look at an example.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   475
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   476
&gt;&gt;&gt; dctcopy.update(dct)
24
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   477
&gt;&gt;&gt; dct
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   478
{'Anil': 'Kumble', 'Sachin': 'Tendulkar', 'Rahul': 'Dravid'}
10074d1357ff Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents: 16
diff changeset
   479
&gt;&gt;&gt; dctcopy
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   480
{'Anil': 'Kumble', 'Sachin': 'Tendulkar', 'Rahul': 'Dravid'}</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   481
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   482
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   483
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   484
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   485
</div></body>
0
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   486
</html>
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   487