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