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